{
final Format rendererInputFormat;
final javax.media.format.AudioFormat format;
final Buffer initialBuffer;
if (codec != null)
{
format = (javax.media.format.AudioFormat) track.getFormat();
logger.fine("codec input format=" + format);
codec.setInputFormat(format);
final Format[] supportedOutputFormats = codec.getSupportedOutputFormats(format);
if (supportedOutputFormats.length == 0)
{
logger.warning("No supported output formats for the codec");
return;
}
final Format codecOutputFormat = supportedOutputFormats[0];
codec.setOutputFormat(codecOutputFormat);
rendererInputFormat = codecOutputFormat;
logger.fine("codecOutputFormat=" + codecOutputFormat);
try
{
codec.open();
} catch (ResourceUnavailableException e2)
{
logger.log(Level.WARNING, "" + e2, e2); // TODO
return;
}
initialBuffer = new Buffer();
}
else
{
format = (javax.media.format.AudioFormat) track.getFormat();
rendererInputFormat = format;
initialBuffer = new Buffer();
}
renderer.setInputFormat(rendererInputFormat);
try
{
renderer.open();
} catch (ResourceUnavailableException e1)
{
logger.log(Level.WARNING, "" + e1, e1);
return; // TODO
}
renderer.start();
// TODO: we are feeding the renderer with data from the raw input stream, rather than the AudioInputStream.
// in the case of formats like MP3, will this work?
final Buffer buffer = initialBuffer;//is2.getBuffer(); // this allows us to pick up any unread data from TrackInputStream reading
Buffer buffer2 = new Buffer();//is.getBuffer(); // this allows us to pick up any unread data from TrackInputStream reading
while (!buffer.isEOM())
{
track.readFrame(buffer);
logger.fine("read buffer from track in loop: " + buffer.getLength() + " " + bufferToString((byte[]) buffer.getData()));
if (buffer.getFormat() == null)
buffer.setFormat(format);
if (buffer.isDiscard())
continue;
if (codec != null)
{
int codecResult = codec.process(buffer, buffer2);
if (codecResult == Codec.OUTPUT_BUFFER_NOT_FILLED)
{
logger.fine("Codec.OUTPUT_BUFFER_NOT_FILLED");
continue;
// TODO:
}
else if (codecResult == Codec.BUFFER_PROCESSED_FAILED)
{
logger.warning("Codec.BUFFER_PROCESSED_FAILED");
return;
}
if (buffer2.getFormat() == null)
buffer2.setFormat(rendererInputFormat);
logger.fine("got buffer from codec: " + buffer2.getLength());
}
else
buffer2 = buffer;
final int result = renderer.process(buffer2);
if (result == Renderer.BUFFER_PROCESSED_FAILED)
{ logger.warning("Renderer.BUFFER_PROCESSED_FAILED");
return;
}
// TODO: handle errors, incomplete processing
}
if (codec != null)
{
logger.fine("Codec still contains data, continuing processing");
while (!buffer2.isEOM())
{
// we must have data we still have to get out of the codec
buffer.setLength(0);
int codecResult = codec.process(buffer, buffer2);
if (codecResult == Codec.OUTPUT_BUFFER_NOT_FILLED)
{
logger.fine("Codec.OUTPUT_BUFFER_NOT_FILLED");
continue;
// TODO:
}
else if (codecResult == Codec.BUFFER_PROCESSED_FAILED)
{
logger.warning("Codec.BUFFER_PROCESSED_FAILED");
return;
}
if (buffer2.getFormat() == null)
buffer2.setFormat(rendererInputFormat);
logger.fine("got buffer from codec: " + buffer2.getLength());
final int result = renderer.process(buffer2);
if (result == Renderer.BUFFER_PROCESSED_FAILED)
{ logger.warning("Renderer.BUFFER_PROCESSED_FAILED");
return;