org.musicontroller.streaming
Class MpegOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by org.musicontroller.streaming.MpegOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class MpegOutputStream
extends java.io.FilterOutputStream

An MpegOutputStream filters all (meta)data from a stream, so that a clean stream of Mpeg-frames remains. This class tries to achieve this by detecting and decoding mp3-frame-headers in the stream. Correct headers and the subsequent data are let through, whereas incorrect data is filtered. This method has a downside: metadata in a mp3-file could in theory contain data which looks like a valid mp3-frame-header too. The solution is to not only read mp3-frame-headers, but metadata-headers as well. If we encounter an ID3-header, we just skip the following data.

Author:
Varienaja

Field Summary
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
MpegOutputStream(java.io.OutputStream out)
          Creates a new MpegOutputStream.
 
Method Summary
 void flush()
           
 long getBytesStreamed()
          Gets the number of bytes streamed.
 int getStreamedLengthInMillis()
          Gets the playtime.
 void reset()
          Resets the internal state of the MpegOutputStream.
 void write(byte[] b, int off, int len)
          Disregards bytes until a valid Mpeg-header is found.
 void write(int b)
           
 
Methods inherited from class java.io.FilterOutputStream
close, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MpegOutputStream

public MpegOutputStream(java.io.OutputStream out)
Creates a new MpegOutputStream.

Parameters:
out - The OutputStream to filter.
Method Detail

reset

public void reset()

Resets the internal state of the MpegOutputStream. This is particularly useful when skipping songs. In the case of skipping, the stream is cut off at an arbitrary position, causing the mpeg-stream to become distorted.

This method also sets the seconds-counter back to 0.

When this class is busy copying an mpeg-frame which is only partly handed over, the output can still contain pops. The new frame would come too early, causing bogus-data to be played as music. A call to this reset method invalidates the current mpeg frame, and therefore rules out another source of nasty popping sounds.


write

public void write(int b)
           throws java.io.IOException
Overrides:
write in class java.io.FilterOutputStream
Throws:
java.io.IOException

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Disregards bytes until a valid Mpeg-header is found. Calculates the expected frame-length. When the full header + frame is received, the full frame is handed over to the delegating OutputStream. Only correct MP3-frames are written, all other information is disregarded.

Overrides:
write in class java.io.FilterOutputStream
Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.FilterOutputStream
Throws:
java.io.IOException

getStreamedLengthInMillis

public int getStreamedLengthInMillis()
Gets the playtime.

Returns:
The amount of milliseconds that were transported through this object since the last reset.

getBytesStreamed

public long getBytesStreamed()
Gets the number of bytes streamed.

Returns:
The number of bytes streamed since the last reset.


Copyright © 2010 A.J.V.. All Rights Reserved.