@ Override public ITag readTag ( ) { //log.debug("Read tag"); ITag tag = null ; try { lock . acquire ( ) ; //empty-out the pre-streaming tags first if ( ! firstTags . isEmpty ( ) ) { log . debug ( "Returning pre-tag" ) ; // Return first tags before media data return firstTags . removeFirst ( ) ; } //log.debug("Read tag - sample {} prevFrameSize {} audio: {} video: {}", new Object[]{currentSample, prevFrameSize, audioCount, videoCount}); //get the current frame MP4Frame frame = frames . get ( currentFrame ) ; log . debug ( "Playback {}" , frame ) ; int sampleSize = frame . getSize ( ) ; int time = ( int ) Math . round ( frame . getTime ( ) * 1000.0 ) ; //log.debug("Read tag - dst: {} base: {} time: {}", new Object[]{frameTs, baseTs, time}); long samplePos = frame . getOffset ( ) ; //log.debug("Read tag - samplePos {}", samplePos); //determine frame type and packet body padding byte type = frame . getType ( ) ; //create a byte buffer of the size of the sample ByteBuffer data = ByteBuffer . allocate ( sampleSize + 2 ) ; try { //log.debug("Writing audio prefix"); data . put ( MP4Reader . PREFIX_AUDIO_FRAME ) ; //do we need to add the mdat offset to the sample position? dataSource . position ( samplePos ) ; dataSource . read ( data ) ; } catch ( IOException e ) { log . error ( "Error on channel position / read" , e ) ; } //chunk the data IoBuffer payload = IoBuffer . wrap ( data . array ( ) ) ; //create the tag tag = new Tag ( type , time , payload . limit ( ) , payload , prevFrameSize ) ; //log.debug("Read tag - type: {} body size: {}", (type == TYPE_AUDIO ? "Audio" : "Video"), tag.getBodySize()); //increment the sample number currentFrame ++ ; //set the frame / tag size prevFrameSize = tag . getBodySize ( ) ; } catch ( InterruptedException e ) { log . warn ( "Exception acquiring lock" , e ) ; } finally { lock . release ( ) ; } //log.debug("Tag: {}", tag); return tag ; }
Packages media data for return to providers .