multimediacommscontroller/mmccfilesourcesink/src/mccfilesourceimpl_withtimer.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:    
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20         
       
    21 // INCLUDE FILES
       
    22 #include <ES_SOCK.H>
       
    23 #include <e32std.h>
       
    24 #include "MccFileSourceImpl.h"
       
    25 #include "MmccCodecInformation.h"
       
    26 #include "MccFileSourceLogs.h"
       
    27 #include "MccTickVideoObserver.h"
       
    28 #include "MccTickAudioObserver.h"
       
    29 
       
    30 
       
    31 // ============================ MEMBER FUNCTIONS ===============================
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // CMccFileSourceImpl::NewL
       
    35 // Static constructor.
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 CMccFileSourceImpl* CMccFileSourceImpl::NewL()
       
    39     {
       
    40     __FILESOURCE_CONTROLL( "CMccFileSourceImpl::NewL" )
       
    41     
       
    42     CMccFileSourceImpl* self = new ( ELeave ) CMccFileSourceImpl();
       
    43     CleanupStack::PushL(self);
       
    44     self->ConstructL();
       
    45     CleanupStack::Pop(self);
       
    46     return self;
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CMccFileSourceImpl::ConstructL
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 void CMccFileSourceImpl::ConstructL()
       
    54     {
       
    55     __FILESOURCE_CONTROLL( "CMccFileSourceImpl::ConstructL" )
       
    56     }
       
    57     
       
    58 // -----------------------------------------------------------------------------
       
    59 // Destructor.
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 CMccFileSourceImpl::CMccFileSourceImpl()
       
    63     {
       
    64     iObserver = 0;
       
    65     iVideoLength = 0;
       
    66     iFrameRate = 0;
       
    67     iVideoType = 0;
       
    68     iVideoWidth = 0;
       
    69     iVideoHeight = 0;
       
    70     iVideoTimeScale = 0;
       
    71     iAudioLength = 0;
       
    72     iAudioType = 0;
       
    73     iFramesPerSample = 0;
       
    74     iAudioTimeScale = 0;
       
    75     iAverageBitRate = 0;
       
    76     iStreamSize = 0;
       
    77     iStreamAverageBitRate = 0;
       
    78     iAudioPosition = 0;
       
    79     iVideoPosition = 0;
       
    80     iPosition = 0;
       
    81     iCurrentVideoType = CCMRMediaBuffer::EVideoH263;
       
    82     iCurrentAudioType = CCMRMediaBuffer::EAudioAMRNB;
       
    83     iBufferSize = 0;
       
    84     iAudioSize = 0;
       
    85     iReturnedFrames = 0;
       
    86     iNextFrameType = 0;
       
    87     iKeyFrame = 0;
       
    88     iVideoFrameSize = 0;
       
    89     iLastPosition = 0;
       
    90     iFrameSize.iWidth = 0;
       
    91     iFrameSize.iHeight = 0;
       
    92     iCurrentState = EConstructed;
       
    93     iMP4Handle = NULL;
       
    94     iIsAudioLeft = EFalse;
       
    95     iIsVideoLeft = EFalse;
       
    96     iIsAudio = EFalse;
       
    97     iIsVideo = EFalse;
       
    98 	iRecursiveCalls = 0;
       
    99 	iIsFirstVideoFrame = EFalse;
       
   100 
       
   101 	iAudioConsumer = 0;
       
   102 	iAudioConsumerBuffer = 0;
       
   103 	iVideoConsumer = 0;
       
   104 	iVideoConsumerBuffer = 0;    
       
   105     }
       
   106         
       
   107 // -----------------------------------------------------------------------------
       
   108 // CMccFileSourceImpl::~CMccFileSourceImpl.
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 CMccFileSourceImpl::~CMccFileSourceImpl()
       
   112     {
       
   113     __FILESOURCE_CONTROLL( "CMccFileSourceImpl::~CMccFileSourceImpl" )
       
   114   /*
       
   115     delete iPeriodicVideo;
       
   116     delete iPeriodicVideo;
       
   117    */
       
   118     if ( iMP4Handle )
       
   119 	    {
       
   120 	    MP4Err error = MP4ParseClose( iMP4Handle );
       
   121 	    }
       
   122   
       
   123     iObserver = NULL;
       
   124     iVideoConsumer = NULL;
       
   125     iAudioConsumer = NULL;
       
   126     
       
   127     iAudioFrames.Reset();
       
   128     iAudioFrames.Close();
       
   129     }
       
   130 
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // CMccFileSourceImpl::AddDataSink(MCMRMediaSink* aSink)
       
   134 //
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 void CMccFileSourceImpl::AddDataSink( MCMRMediaSink* /*aSink*/ )
       
   138     {
       
   139     __FILESOURCE_CONTROLL( "CMccFileSourceImpl::AddDataSink" )
       
   140     }
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // CMccFileSourceImpl::OpenFileL(TFileName aFileName)
       
   144 //
       
   145 // Opens a 3gp file for streaming and reads media descriptions into
       
   146 // member variables
       
   147 // -----------------------------------------------------------------------------
       
   148 //
       
   149 void CMccFileSourceImpl::OpenFileL( TFileName aFileName )
       
   150     {
       
   151     __FILESOURCE_CONTROLL( "CMccFileSourceImpl::OpenFileL" )
       
   152     
       
   153     __ASSERT_ALWAYS( iCurrentState == EConstructed, User::Leave( KErrNotReady ) );
       
   154 
       
   155     TFileName fullFileName;
       
   156     MP4Err error = MP4_OK;
       
   157     
       
   158     RFs fS;
       
   159     User::LeaveIfError( fS.Connect() );
       
   160 	CleanupClosePushL( fS );
       
   161     TParse fp;	
       
   162     if ( fS.Parse( aFileName, fp ) != KErrNone )
       
   163         {
       
   164         RDebug::Print(_L("CMccFileSourceImpl::OpenFileL file name parse error!"));
       
   165 	    User::Leave( KErrGeneral );
       
   166         }
       
   167 	
       
   168     fullFileName = fp.FullName();
       
   169 
       
   170   	CleanupStack::PopAndDestroy( &fS ); 
       
   171   	
       
   172   	error = MP4ParseOpen( &iMP4Handle, (wchar_t *) fullFileName.PtrZ() );
       
   173 
       
   174     if ( error != MP4_OK )
       
   175 	    {
       
   176 	    RDebug::Print(_L("CMccFileSourceImpl::OpenFileL parse open error!"));
       
   177         RDebug::Print(_L("CMccFileSourceImpl::OpenFileL parse open error=%d"), error);
       
   178 
       
   179 	    FileOpenErrorL( error );	
       
   180 	    }
       
   181 	
       
   182  	ParseUpdateVideoDescriptions();
       
   183  	
       
   184  	ParseUpdateAudioDescriptions();
       
   185 
       
   186     if ( ( !iIsAudio ) && ( !iIsVideo ) )
       
   187         {
       
   188         User::Leave( KErrNotSupported );
       
   189         }
       
   190     
       
   191     error = MP4ParseRequestStreamDescription( iMP4Handle,
       
   192         ( mp4_u32* ) &iStreamSize, ( mp4_u32* ) &iStreamAverageBitRate );
       
   193        
       
   194     __FILESOURCE_CONTROLL_INT1("CMccFileSourceImpl::OpenFileL, \
       
   195 MP4ParseRequestStreamDescription error=",error)
       
   196     
       
   197   
       
   198 	#ifdef TIMESTAMP
       
   199 		iReplayTimeStamp = 0;
       
   200 		iTimeDifference = 0;
       
   201 		iPausedTime = 0;
       
   202 	#endif
       
   203 
       
   204     iCurrentState = EReady;
       
   205     }
       
   206 
       
   207 // -----------------------------------------------------------------------------
       
   208 // CMccFileSourceImpl::CloseFileL()
       
   209 //
       
   210 // Closes the 3gp file
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 void CMccFileSourceImpl::CloseFileL()
       
   214     {
       
   215     __FILESOURCE_CONTROLL( "CMccFileSourceImpl::CloseFileL" )
       
   216 
       
   217     __ASSERT_ALWAYS( iCurrentState != EConstructed, User::Leave( KErrNotReady ) );
       
   218 
       
   219     if ( iCurrentState == EPlaying )
       
   220         {
       
   221         TRAP_IGNORE( SourceStopL() );
       
   222         }
       
   223 
       
   224     MP4Err error;
       
   225     error = MP4ParseClose( iMP4Handle );
       
   226 
       
   227     if ( error != MP4_OK )
       
   228         {
       
   229         User::Leave( KErrGeneral );
       
   230         }
       
   231 
       
   232     iCurrentState = EConstructed;
       
   233     iMP4Handle = NULL;
       
   234     }
       
   235 
       
   236 // -----------------------------------------------------------------------------
       
   237 // CMccFileSourceImpl::SetPositionL(TTimeIntervalMicroSeconds aPosition)
       
   238 // 
       
   239 // Sets a new streaming position
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 void CMccFileSourceImpl::SetPositionL( TTimeIntervalMicroSeconds aPosition )
       
   243     {
       
   244     __FILESOURCE_CONTROLL_INT1( "CMccFileSourceImpl::SetPositionL", Int64() )
       
   245     
       
   246     __ASSERT_ALWAYS( iCurrentState != EConstructed, User::Leave( KErrNotReady ) );
       
   247 
       
   248     if ( iCurrentState == EStopped ) 
       
   249     	{
       
   250     	return;
       
   251     	}
       
   252 
       
   253     MP4Err error;
       
   254 
       
   255     TInt64 tempPosition = aPosition.Int64();
       
   256     TUint tempPosition2 = tempPosition; 
       
   257     tempPosition2 = tempPosition2 / 1000; 
       
   258     iLastPosition = iVideoLength;
       
   259 
       
   260     TRAP_IGNORE( UpdatePositionL());
       
   261        
       
   262     if ( iLastPosition < iAudioLength )
       
   263         {
       
   264         iLastPosition = iAudioLength;
       
   265         }
       
   266 
       
   267     if ( tempPosition2 > iLastPosition )
       
   268         {
       
   269         User::Leave( KErrArgument );
       
   270         }
       
   271 
       
   272     error = MP4ParseSeek( iMP4Handle, ( mp4_u32 ) tempPosition2,
       
   273         ( mp4_u32* ) &iAudioPosition, ( mp4_u32* ) &iVideoPosition, MP4TRUE );
       
   274 	
       
   275     if ( error != MP4_OK )
       
   276         {   
       
   277         User::Leave( KErrGeneral );
       
   278         }
       
   279 
       
   280     #ifdef TIMESTAMP
       
   281 		if ( iIsAudioLeft == EFalse && iIsVideoLeft == EFalse)
       
   282 			{
       
   283 			iPosition = 0;
       
   284 			}
       
   285         iTimeDifference = iTimeDifference + iPosition - iVideoPosition;
       
   286         iPosition = iVideoPosition;
       
   287     #endif
       
   288 	}
       
   289 
       
   290 // -----------------------------------------------------------------------------
       
   291 // CMccFileSourceImpl::VideoTypeL()
       
   292 // 
       
   293 // Returns current video type
       
   294 // -----------------------------------------------------------------------------
       
   295 //
       
   296 CCMRMediaBuffer::TBufferType CMccFileSourceImpl::VideoTypeL()
       
   297     {
       
   298     __FILESOURCE_CONTROLL( "CMccFileSourceImpl::VideoTypeL" )
       
   299 
       
   300     __ASSERT_ALWAYS( iCurrentState != EConstructed, User::Leave( KErrNotReady ) );
       
   301 
       
   302     return iCurrentVideoType;
       
   303     }
       
   304 
       
   305 // -----------------------------------------------------------------------------
       
   306 // CMccFileSourceImpl::AudioTypeL()
       
   307 // 
       
   308 // Returns current audio type
       
   309 // -----------------------------------------------------------------------------
       
   310 //
       
   311 CCMRMediaBuffer::TBufferType CMccFileSourceImpl::AudioTypeL()
       
   312     {
       
   313     __FILESOURCE_CONTROLL( "CMccFileSourceImpl::AudioTypeL" )
       
   314 
       
   315     __ASSERT_ALWAYS( iCurrentState != EConstructed, User::Leave( KErrNotReady ) );
       
   316     
       
   317     return iCurrentAudioType;
       
   318     }
       
   319     
       
   320 // -----------------------------------------------------------------------------
       
   321 // CMccFileSourceImpl::VideoFrameRateL()
       
   322 // 
       
   323 // Returns current video frame rate
       
   324 // -----------------------------------------------------------------------------
       
   325 //
       
   326 TReal32 CMccFileSourceImpl::VideoFrameRateL()
       
   327     {
       
   328     __FILESOURCE_CONTROLL( "CMccFileSourceImpl::VideoFrameRateL" )
       
   329 
       
   330     __ASSERT_ALWAYS( iCurrentState != EConstructed, User::Leave( KErrNotReady ) );
       
   331 
       
   332     return TReal32 ( iFrameRate );
       
   333     }
       
   334 
       
   335 // -----------------------------------------------------------------------------
       
   336 // CMccFileSourceImpl::AudioBitRateL()
       
   337 // 
       
   338 // Returns current audio bit rate
       
   339 // -----------------------------------------------------------------------------
       
   340 //
       
   341 TUint CMccFileSourceImpl::AudioBitRateL()
       
   342     {
       
   343     __FILESOURCE_CONTROLL_INT1( "CMccFileSourceImpl::AudioBitRateL", iAverageBitRate )
       
   344 
       
   345     __ASSERT_ALWAYS( iCurrentState != EConstructed, User::Leave( KErrNotReady ) );
       
   346     
       
   347     return iAverageBitRate;
       
   348     }
       
   349 
       
   350 // -----------------------------------------------------------------------------
       
   351 // CMccFileSourceImpl::DurationL()
       
   352 // 
       
   353 // Returns duration of current media clip
       
   354 // -----------------------------------------------------------------------------
       
   355 //
       
   356 TTimeIntervalMicroSeconds CMccFileSourceImpl::DurationL()
       
   357     {
       
   358     __FILESOURCE_CONTROLL_INT1( "CMccFileSourceImpl::DurationL", 
       
   359                                 iVideoLength * 1000 )
       
   360 
       
   361     __ASSERT_ALWAYS( iCurrentState != EConstructed, User::Leave( KErrNotReady ) );
       
   362     
       
   363     return TTimeIntervalMicroSeconds( iVideoLength * 1000 );
       
   364     }
       
   365 
       
   366 // -----------------------------------------------------------------------------
       
   367 // CMccFileSourceImpl::VideoFrameSizeL()
       
   368 // 
       
   369 // Returns current video frame size
       
   370 // -----------------------------------------------------------------------------
       
   371 //
       
   372 TSize CMccFileSourceImpl::VideoFrameSizeL()
       
   373     {        
       
   374     __FILESOURCE_CONTROLL_INT1( "CMccFileSourceImpl::VideoFrameSizeL iWidth", 
       
   375                                 iFrameSize.iWidth )
       
   376 	__FILESOURCE_CONTROLL_INT1("CMccFileSourceImpl::VideoFrameSizeL iHeight",
       
   377 								iFrameSize.iHeight )                            
       
   378     
       
   379     __ASSERT_ALWAYS( iCurrentState != EConstructed, User::Leave( KErrNotReady ) );
       
   380     
       
   381     return iFrameSize;
       
   382     }
       
   383 // -----------------------------------------------------------------------------
       
   384 // CMccFileSourceImpl::VideoBitRateL()
       
   385 // 
       
   386 // Returns current video bitrate
       
   387 // -----------------------------------------------------------------------------
       
   388 //
       
   389 TUint CMccFileSourceImpl::VideoBitRateL()
       
   390     {
       
   391     __FILESOURCE_CONTROLL( "CMccFileSourceImpl::VideoBitRateL, videoBitRate" )
       
   392     
       
   393     __ASSERT_ALWAYS( iCurrentState != EConstructed, User::Leave( KErrNotReady ) );
       
   394     
       
   395     TUint videoBitRate = iStreamAverageBitRate - iAverageBitRate;
       
   396    
       
   397     __FILESOURCE_CONTROLL_INT1( "CMccFileSourceImpl::VideoBitRateL, videoBitRate", 
       
   398                                 videoBitRate )
       
   399     return videoBitRate;
       
   400     }
       
   401 
       
   402 // -----------------------------------------------------------------------------
       
   403 // CMccFileSourceImpl::PositionL()
       
   404 // 
       
   405 // Returns current streaming position
       
   406 // -----------------------------------------------------------------------------
       
   407 //
       
   408 TTimeIntervalMicroSeconds CMccFileSourceImpl::PositionL()
       
   409     {
       
   410     __FILESOURCE_CONTROLL_INT1( "CMccFileSourceImpl::PositionL", 
       
   411                                 iPosition * 1000 )
       
   412 
       
   413     __ASSERT_ALWAYS( iCurrentState != EConstructed, User::Leave( KErrNotReady ) );
       
   414 
       
   415     return TTimeIntervalMicroSeconds( iPosition * 1000 ); 
       
   416 
       
   417     }
       
   418 // -----------------------------------------------------------------------------
       
   419 // CMccFileSourceImpl::UpdatePositionL()
       
   420 // 
       
   421 // Updates the position parameter
       
   422 // -----------------------------------------------------------------------------
       
   423 //
       
   424 void CMccFileSourceImpl::UpdatePositionL()
       
   425 	{
       
   426 	__FILESOURCE_CONTROLL( "CMccFileSourceImpl::UpdatePosition" )
       
   427 	MP4Err error;
       
   428 	
       
   429     error=MP4ParseGetNextVideoTimestamp( iMP4Handle,
       
   430         ( mp4_u32* ) &iPosition, NULL );
       
   431 
       
   432     if ( error != MP4_OK )
       
   433         {
       
   434         User::Leave( KErrGeneral );
       
   435         }
       
   436 
       
   437     error = MP4ParseSeek( iMP4Handle, ( mp4_u32 ) iPosition,
       
   438         ( mp4_u32* ) &iAudioPosition, ( mp4_u32* ) &iVideoPosition, MP4FALSE );
       
   439 	
       
   440      if ( error != MP4_OK )
       
   441         {
       
   442         User::Leave( KErrGeneral );
       
   443         }
       
   444 
       
   445 	}
       
   446 
       
   447 // -----------------------------------------------------------------------------
       
   448 // CMccFileSourceImpl::ReadAudioFrameL()
       
   449 // 
       
   450 // Reads the next audio frame from 3gp file
       
   451 // -----------------------------------------------------------------------------
       
   452 //
       
   453 void CMccFileSourceImpl::ReadAudioFrameL()
       
   454     {
       
   455 	__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadAudioFrameL" )
       
   456 	
       
   457      
       
   458     TBool brokenFrame = EFalse;
       
   459     
       
   460   	if ( iAudioConsumer == NULL)
       
   461   		{
       
   462   		__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadAudioFrameL, \
       
   463 source not available" )
       
   464   		return;
       
   465   		}
       
   466   	else
       
   467   		{
       
   468   		__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadAudioFrameL, \
       
   469 writing buffer" )  		
       
   470   		}	
       
   471 
       
   472 	if (!iAudioFrames.Count())
       
   473 		{		
       
   474 	    MP4Err error;
       
   475 
       
   476 	    error = MP4ParseIsFrameAvailable( iMP4Handle, MP4_TYPE_AMR_NB );
       
   477 	    
       
   478 	    if ( error != MP4_OK )
       
   479 	        {
       
   480 	  		__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadAudioFrameL, \
       
   481 no audio" ) 
       
   482 			iIsAudioLeft = EFalse;
       
   483 	        }
       
   484 	    else 
       
   485 	    	{
       
   486 	  		__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadAudioFrameL, \
       
   487 	audio OK" )
       
   488 	    	iIsAudioLeft = ETrue;
       
   489 	    	}
       
   490 	    	
       
   491 	    if ( iIsAudioLeft == EFalse && iIsVideoLeft == EFalse )
       
   492 	        {
       
   493 	  		__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadAudioFrameL, \
       
   494 no video & audio available" ) 		
       
   495 	        		
       
   496 			#ifdef TIMESTAMP
       
   497 				iReplayTimeStamp = iReplayTimeStamp + iPosition + 10;
       
   498 			#endif
       
   499 
       
   500 			SetPositionL(  TTimeIntervalMicroSeconds ( 0 ) );
       
   501 			SourcePauseL();
       
   502 
       
   503 	        if ( iObserver )
       
   504 	            {
       
   505 	            iObserver->EndOfFile( KErrNone );
       
   506 	            }
       
   507 	        return;
       
   508 		    }
       
   509 
       
   510 		// Timing correction code
       
   511 		TTimeIntervalMicroSeconds elapsedTime;
       
   512 	    TTime currentTime;
       
   513 	    currentTime.HomeTime();
       
   514 	    elapsedTime = currentTime.MicroSecondsFrom( iStartTime );
       
   515 	    TInt64 tempTime = elapsedTime.Int64();
       
   516 	    TInt timeNow = tempTime  / 1000;
       
   517 
       
   518 		if ( ( timeNow + 500 ) < ( ( TInt ) iAudioPosition + iTimeDifference + iReplayTimeStamp + ( iPausedTime / 1000 ) ) )
       
   519 			{
       
   520 	  		__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadAudioFrameL, \
       
   521 Position check, returning" ) 
       
   522 	  		__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadAudioFrameL, IGNORING" ) 
       
   523 	  		//return    				
       
   524 			}
       
   525 
       
   526 		MP4ParseNextFrameSize( iMP4Handle, MP4_TYPE_AMR_NB,
       
   527 	        ( mp4_u32* ) &iBufferSize );
       
   528 
       
   529 	    HBufC8* iMediaBuffer = HBufC8::NewL( iBufferSize );
       
   530 	    TPtr8 iMediaSinkBuffer = iMediaBuffer->Des();
       
   531 
       
   532 	    iMediaSinkBuffer.SetLength( iBufferSize );
       
   533 
       
   534 	    if ( MP4ParseReadAudioFrames( iMP4Handle,
       
   535 	        ( mp4_u8* ) iMediaSinkBuffer.Ptr(),
       
   536 	        ( mp4_u32 ) iBufferSize, ( mp4_u32* ) &iAudioSize,
       
   537 	        ( mp4_u32* ) &iPosition, ( mp4_u32* ) &iReturnedFrames,
       
   538 			NULL ) == MP4_OK )
       
   539 			{
       
   540 			__FILESOURCE_CONTROLL_INT1( "CMccFileSourceImpl::ReadAudioFrameL, \
       
   541 read, time position", iPosition ) 		
       
   542 
       
   543 			iAudioPosition = iPosition ;
       
   544 			brokenFrame = EFalse;
       
   545 			
       
   546 			TBool areFramesNormal = EFalse;
       
   547 		
       
   548 			// If brokenFrame = ETrue don't send the frame forward!
       
   549 					
       
   550 			// Calculate real frame size from sample size
       
   551 	        TInt FrameSize = iBufferSize / iReturnedFrames;
       
   552 	        TUint Counter = 0;
       
   553 	        TInt AddToTimeStamp = 0;
       
   554 	        TInt startPosition = 0;
       
   555 	        
       
   556 	        while (Counter < iReturnedFrames)
       
   557 	            {
       
   558 	            Counter++;
       
   559 
       
   560 				if ( areFramesNormal == EFalse )
       
   561 					{
       
   562 					FrameSize = GetFrameLength( iMediaBuffer->Mid( startPosition ) ); 
       
   563 					if ( FrameSize == 0 )
       
   564 						{
       
   565 						brokenFrame = ETrue;
       
   566 						}
       
   567 					else 
       
   568 						{
       
   569 						brokenFrame = EFalse;
       
   570 						}
       
   571 					}
       
   572 
       
   573 				
       
   574 				TTimeIntervalMicroSeconds timeStamp(0);
       
   575 					
       
   576 	            // Sets a different timestamp than in file (forward/rewind functionality)
       
   577 	            #ifdef TIMESTAMP
       
   578 				timeStamp = TTimeIntervalMicroSeconds( ( (iPosition + iTimeDifference + iReplayTimeStamp ) * 1000 ) 
       
   579 							+ AddToTimeStamp 
       
   580 				#ifdef TIMESTAMPPAUSE			
       
   581 							+  iPausedTime 
       
   582 				#endif //TIMESTAMPPAUSE
       
   583 							); 
       
   584             	#else //TIMESTAMP   
       
   585 					timeStamp =TTimeIntervalMicroSeconds( ( iPosition * 1000 ) + AddToTimeStamp ) ;
       
   586             	#endif //TIMESTAMP
       
   587 
       
   588 				TAudioFrameItem item;
       
   589 				item.iFrame = HBufC8::NewL( FrameSize );
       
   590     			item.iFrame->Des().Copy( iMediaBuffer->Mid( startPosition , FrameSize ) );
       
   591     			item.iTimeStamp = timeStamp;
       
   592     			User::LeaveIfError( iAudioFrames.Append(item) );
       
   593 	            
       
   594 			    AddToTimeStamp += 20000;
       
   595 	            startPosition += FrameSize;
       
   596 	            }    
       
   597 		    delete iMediaBuffer;
       
   598 		    iMediaBuffer = NULL; 
       
   599 
       
   600 			if ( ( timeNow - 500 ) < ( ( TInt ) iAudioPosition + iTimeDifference + iReplayTimeStamp + ( iPausedTime / 1000 ) ) )
       
   601 				{
       
   602 				__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadAudioFrameL, \
       
   603 Position check, returning" )
       
   604 				__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadAudioFrameL, \
       
   605 IGNORING" )
       
   606 				//return;
       
   607 				}
       
   608 
       
   609 			}
       
   610 		else
       
   611 			{
       
   612 			__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadAudioFrameL, \
       
   613 reading frames failed" )
       
   614 			}
       
   615     	}
       
   616     else
       
   617     	{
       
   618 		__FILESOURCE_CONTROLL_INT1( "CMccFileSourceImpl::ReadAudioFrameL, \
       
   619 existing frames, count", iAudioFrames.Count() )    		
       
   620     	}
       
   621 
       
   622 	__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadAudioFrameL, \
       
   623 writing buffer, copying" ) 
       
   624 	CMMFDataBuffer* buf = static_cast<CMMFDataBuffer*>(iAudioConsumerBuffer); 		
       
   625 	__FILESOURCE_CONTROLL_INT1( "CMccFileSourceImpl::ReadAudioFrameL, \
       
   626 writing buffer, len", buf->BufferSize() )
       
   627 	HBufC8* audioFrame = iAudioFrames[0].iFrame; 
       
   628     buf->Data().Copy( *audioFrame );
       
   629  	iAudioConsumerBuffer->SetTimeToPlay(iAudioFrames[0].iTimeStamp);
       
   630 	iAudioConsumerBuffer->SetLastBuffer(EFalse);
       
   631     iAudioFrames.Remove(0);
       
   632     delete audioFrame;
       
   633       	
       
   634 	__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadAudioFrameL, \
       
   635 writing buffer, complete" ) 
       
   636 
       
   637 	__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadAudioFrameL, \
       
   638 writing buffer to sink" )   		
       
   639 	iAudioConsumer->BufferFilledL( iAudioConsumerBuffer );
       
   640 	iAudioConsumer = 0;
       
   641 	iAudioConsumerBuffer = 0;
       
   642 	__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadAudioFrameL, \
       
   643 writing buffer to sink complete" )  	
       
   644 	}
       
   645 // -----------------------------------------------------------------------------
       
   646 // CMccFileSourceImpl::ReadVideoFrameL()
       
   647 // 
       
   648 // Reads the next video frame from 3gp file
       
   649 // -----------------------------------------------------------------------------
       
   650 //
       
   651 void CMccFileSourceImpl::ReadVideoFrameL()
       
   652     {
       
   653     __FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadVideoFrameL" )
       
   654 
       
   655     MP4Err error;
       
   656     
       
   657   	if ( iVideoConsumer == NULL)
       
   658   		{
       
   659   		__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadVideoFrameL, \
       
   660 source not available" ) 
       
   661   		return;
       
   662   		}
       
   663   
       
   664   	__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadVideoFrameL, \
       
   665 writing buffer" ) 
       
   666   			
       
   667     
       
   668     error = MP4ParseIsFrameAvailable( iMP4Handle, iVideoType );
       
   669     error = MP4ParseIsFrameAvailable( iMP4Handle, MP4_TYPE_AVC_PROFILE_BASELINE );
       
   670     error = MP4ParseIsFrameAvailable( iMP4Handle, iVideoType );
       
   671     error = MP4ParseIsFrameAvailable( iMP4Handle, MP4_TYPE_AVC_PROFILE_MAIN );
       
   672     error = MP4ParseIsFrameAvailable( iMP4Handle, MP4_TYPE_AVC_PROFILE_EXTENDED );
       
   673     error = MP4ParseIsFrameAvailable( iMP4Handle, iVideoType );
       
   674 
       
   675 
       
   676 	if ( error != MP4_OK )
       
   677         {
       
   678         iIsVideoLeft = EFalse;
       
   679   		__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadVideoFrameL, \
       
   680 no video" )  		
       
   681         }
       
   682     else 
       
   683     	{
       
   684     	iIsVideoLeft = ETrue;
       
   685   		__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadVideoFrameL, \
       
   686 video available" ) 		
       
   687     	}
       
   688 
       
   689     // If no more frames available inform observer and stop playing
       
   690     if ( iIsAudioLeft == EFalse && iIsVideoLeft == EFalse )
       
   691         {
       
   692   		__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadVideoFrameL, \
       
   693 no video & audio available" )  		
       
   694 
       
   695 		#ifdef TIMESTAMP
       
   696 			iReplayTimeStamp = iReplayTimeStamp + iPosition + 10;
       
   697 		#endif
       
   698 		
       
   699 		SetPositionL(  TTimeIntervalMicroSeconds ( 0 ) );
       
   700 		SourcePauseL();
       
   701 
       
   702         if ( iObserver ) 
       
   703         	{
       
   704   			__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadVideoFrameL, \
       
   705   	sending EOF" ) 
       
   706         	iObserver->EndOfFile( KErrNone );
       
   707         	}
       
   708         return;
       
   709         }
       
   710 
       
   711 	// Timing correction code
       
   712 	TTimeIntervalMicroSeconds elapsedTime;
       
   713     TTime currentTime;
       
   714     currentTime.HomeTime();
       
   715     elapsedTime = currentTime.MicroSecondsFrom( iStartTime );
       
   716     TInt64 tempTime = elapsedTime.Int64();
       
   717     TInt timeNow = tempTime /*.GetTInt()*/ / 1000;
       
   718 
       
   719 	if ( ( timeNow + 500 ) < ( ( TInt ) iVideoPosition + iTimeDifference + iReplayTimeStamp + ( iPausedTime / 1000 ) ) )
       
   720 		{
       
   721   		__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadVideoFrameL, Position check, returning" ) 
       
   722   		__FILESOURCE_CONTROLL_INT1( "CMccFileSourceImpl::ReadVideoFrameL", (timeNow + 500))
       
   723   		__FILESOURCE_CONTROLL_INT1( "CMccFileSourceImpl::ReadVideoFrameL", 
       
   724   			(( TInt ) iVideoPosition + iTimeDifference + iReplayTimeStamp + ( iPausedTime / 1000 ) ) )  				
       
   725   		__FILESOURCE_CONTROLL_INT4( "CMccFileSourceImpl::ReadVideoFrameL, videoPosition", iVideoPosition, 
       
   726   			                        "TimeDifference", iTimeDifference, 
       
   727   			                        "ReplyTimeStamp", iReplayTimeStamp, 
       
   728   			                        "PausedTime", iPausedTime )  				
       
   729   		__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadVideoFrameL, IGNORING" )   				
       
   730 		//return; 
       
   731 		}
       
   732 
       
   733     // Read the size of next video frame
       
   734   	__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadVideoFrameL, reading frame" ) 
       
   735     MP4ParseNextFrameSize( iMP4Handle, iVideoType,
       
   736         ( mp4_u32* ) &iBufferSize );
       
   737     
       
   738     HBufC8* tempBuff = NULL;
       
   739     if ( IsAVCVideo() && iIsFirstVideoFrame )
       
   740     	{
       
   741     	ExtractH264ParameterSetNALUsL( &tempBuff );
       
   742     	if ( NULL != tempBuff )
       
   743     		{
       
   744     		CleanupStack::PushL( tempBuff );
       
   745     		}
       
   746     	iIsFirstVideoFrame = EFalse;
       
   747     	}
       
   748     // Allocate a buffer big enough for the next video frame
       
   749   	__FILESOURCE_CONTROLL_INT1( "CMccFileSourceImpl::ReadVideoFrameL, reading frame, size", iBufferSize )
       
   750     HBufC8* iMediaBuffer = HBufC8::NewL( iBufferSize );
       
   751     // Pointer descriptor to the buffer	
       
   752     const TUint8* iPtrMediaSinkBuffer = iMediaBuffer->Des().Ptr();
       
   753 	   
       
   754     if ( MP4ParseReadVideoFrame( iMP4Handle,
       
   755         ( mp4_u8* ) iPtrMediaSinkBuffer, ( mp4_u32 ) iBufferSize,
       
   756         ( mp4_u32* ) &iVideoFrameSize, ( mp4_u32* ) &iPosition,
       
   757         ( mp4_bool* ) &iKeyFrame, NULL ) == MP4_OK )
       
   758 		{
       
   759   		__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadVideoFrameL, reading frame, OK" )   		
       
   760         TPtr8 iMediaSinkBuffer = iMediaBuffer->Des();
       
   761 
       
   762 		iVideoPosition = iPosition ;
       
   763 		
       
   764 
       
   765         iMediaSinkBuffer.SetLength( iBufferSize ); // Sets buffer length
       
   766         if ( IsAVCVideo() )
       
   767         {
       
   768         MarkWithNALUDelimiters( iBufferSize, iMediaSinkBuffer );
       
   769     	}
       
   770 
       
   771 		TTimeIntervalMicroSeconds timeStamp( 0 );
       
   772 		
       
   773         #ifdef TIMESTAMP
       
   774 			timeStamp = TTimeIntervalMicroSeconds( ( iPosition + iTimeDifference + iReplayTimeStamp ) * 1000 
       
   775 			#ifdef TIMESTAMPPAUSE
       
   776 						 + iPausedTime  
       
   777 			#endif //TIMESTAMPPAUSE
       
   778 				); 
       
   779         	#else //TIMESTAMP
       
   780 				timeStamp = TTimeIntervalMicroSeconds ( iPosition * 1000 );
       
   781         	#endif //TIMESTAMP
       
   782    
       
   783             //buffer = new ( ELeave ) CCMRMediaBuffer(
       
   784             //iMediaSinkBuffer, CCMRMediaBuffer::EVideoH263, iBufferSize,
       
   785             //FALSE,  timeStamp );
       
   786              
       
   787   		__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadVideoFrameL, writing buffer, copying" ) 
       
   788   		CMMFDataBuffer* buf = static_cast<CMMFDataBuffer*>(iVideoConsumerBuffer); 		
       
   789   		__FILESOURCE_CONTROLL_INT1( "CMccFileSourceImpl::ReadVideoFrameL, writing buffer, len", buf->BufferSize() )
       
   790   		if( NULL != tempBuff )
       
   791   			{
       
   792   			buf->Data().Copy( *tempBuff );
       
   793   			CleanupStack::PopAndDestroy( tempBuff );
       
   794         	tempBuff = NULL;
       
   795   			}
       
   796   		else
       
   797   			{
       
   798   			buf->Data().Delete( 0, buf->BufferSize() );
       
   799   			}
       
   800         buf->Data().Append( *iMediaBuffer );
       
   801            	
       
   802 	 	iVideoConsumerBuffer->SetTimeToPlay(timeStamp);
       
   803 		iVideoConsumerBuffer->SetLastBuffer(EFalse);
       
   804   		__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadVideoFrameL, writing buffer, complete" ) 
       
   805 
       
   806         TInt err = KErrNone;
       
   807 		if ( err == KErrNone )
       
   808             {
       
   809 
       
   810   			__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadVideoFrameL, writing buffer to sink" ) 
       
   811   			iVideoConsumer->BufferFilledL( iVideoConsumerBuffer );
       
   812   			iVideoConsumer = 0;
       
   813   			iVideoConsumerBuffer = 0;
       
   814   			__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadVideoFrameL, writing buffer to sink complete" )   		
       
   815   			   
       
   816 			if ( err != KErrNone )
       
   817             	{
       
   818             	delete iMediaBuffer;
       
   819             	iMediaBuffer = NULL;
       
   820             	User::Leave( KErrGeneral );
       
   821             	}
       
   822             }
       
   823         }
       
   824         else
       
   825 	        {
       
   826 	        __FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadVideoFrameL, reading frame, NOT OK" )
       
   827 	        }
       
   828    
       
   829     delete iMediaBuffer;
       
   830     iMediaBuffer = NULL;
       
   831 
       
   832 	if ( ( timeNow - 500 ) < ( ( TInt ) iVideoPosition + iTimeDifference + iReplayTimeStamp + ( iPausedTime / 1000 ) ) )
       
   833 		{
       
   834   		__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadVideoFrameL, Time Check 2, IGNORING" )    						//return;
       
   835 		}
       
   836 
       
   837   	__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ReadVideoFrameL, writing buffer to sink complete" ) 		
       
   838     }
       
   839 
       
   840 // -----------------------------------------------------------------------------
       
   841 // CMccFileSourceImpl::PlayL()
       
   842 // 
       
   843 // Starts streaming from 3gp file
       
   844 // -----------------------------------------------------------------------------
       
   845 //
       
   846 void CMccFileSourceImpl::SourcePlayL()
       
   847     {
       
   848     __FILESOURCE_CONTROLL( "CMccFileSourceImpl::SourcePlayL" )
       
   849     
       
   850     __ASSERT_ALWAYS( iCurrentState != EConstructed, User::Leave( KErrNotReady ) );
       
   851     			
       
   852     if ( iCurrentState == EPlaying )
       
   853         {
       
   854         return;
       
   855         }
       
   856 
       
   857     #ifdef TIMESTAMP
       
   858         if ( iCurrentState == EPaused ) 
       
   859             {
       
   860             iPauseStopped.HomeTime();
       
   861 //#ifdef TIMESTAMPPAUSE
       
   862             TTimeIntervalMicroSeconds differenceTemp1 = 
       
   863             	iPauseStopped.MicroSecondsFrom(iPauseStarted);
       
   864             TInt64 differenceTemp2 = differenceTemp1.Int64();
       
   865             iPausedTime = iPausedTime + differenceTemp2 /*.GetTInt()*/;
       
   866 //#endif //TIMESTAMPPAUSE
       
   867             }
       
   868 		else
       
   869 			{
       
   870 			 iStartTime.HomeTime(); // Set start time to current time
       
   871 			}
       
   872     #endif
       
   873  /*   
       
   874 
       
   875     // Variables for periodic timers
       
   876     // temporary flagged hack for WINS to get timing right
       
   877     TInt iTickIntervalVideo = 1000000;
       
   878     TInt iTickIntervalAudio = 10000;
       
   879 
       
   880     if ( iFrameRate != 0 )
       
   881         {
       
   882         // Set interval for video
       
   883         iTickIntervalVideo = ( 1000000 / ( TInt ) iFrameRate  ) ; 
       
   884         }
       
   885         
       
   886 
       
   887     TInt ticks = 10000;
       
   888 
       
   889 	// ROP plays faster so this code was added to help syncronisation
       
   890 //	iTickIntervalAudio = TInt (iTickIntervalAudio *  ( ( 100.0 - KPlaybackSync ) / 100.0 )) ;
       
   891 	iTickIntervalVideo = TInt (iTickIntervalVideo *  ( ( 100.0 - KPlaybackSync ) / 100.0 )) ;
       
   892     	
       
   893     // Creation and starting of periodic timers
       
   894 	 
       
   895     if ( iIsAudio )
       
   896         {
       
   897         if ( !iPeriodicAudio )
       
   898 	        {
       
   899 	        iPeriodicAudio = CMccPeriodicRunner::NewL( *this );
       
   900 	        }
       
   901         
       
   902         iPeriodicAudio->Start( iTickIntervalAudio, 
       
   903                                TCallBack( TickAudioL, this ) );
       
   904         }
       
   905     if ( iIsVideo )
       
   906         {
       
   907         if ( !iPeriodicVideo )
       
   908 	        {
       
   909 	        iPeriodicVideo = CMccPeriodicRunner::NewL( *this );
       
   910 	
       
   911 	        }
       
   912         iPeriodicVideo->Start( iTickIntervalVideo,
       
   913                                TCallBack( TickVideoL, this ) );
       
   914         }		
       
   915        
       
   916        */
       
   917     iCurrentState = EPlaying;
       
   918     }
       
   919     
       
   920 // -----------------------------------------------------------------------------
       
   921 // CMccFileSourceImpl::TickAudioL
       
   922 // 
       
   923 // -----------------------------------------------------------------------------
       
   924 //
       
   925 TInt CMccFileSourceImpl::TickAudioL(  TAny* aObject  )
       
   926 	{
       
   927 	__FILESOURCE_CONTROLL( "CMccFileSourceImpl::TickAudioL" )
       
   928 
       
   929     if ( ( ( CMccFileSourceImpl* ) aObject )->iFramesPerSample <= 2 )
       
   930         {
       
   931         //Reads five frames in one tick 
       
   932         for ( int x = 0 ; x < 5; x++ )
       
   933             {
       
   934             ( ( CMccFileSourceImpl* ) aObject )->ReadAudioFrameL();
       
   935             }
       
   936         }
       
   937 
       
   938     if ( ( ( CMccFileSourceImpl* ) aObject )->iFramesPerSample == 3 ||
       
   939          ( ( CMccFileSourceImpl* ) aObject )->iFramesPerSample == 4 )
       
   940          {
       
   941                
       
   942         //Reads two frames in one tick 
       
   943         for ( int x = 0 ; x < 2; x++ )
       
   944             {
       
   945             ( ( CMccFileSourceImpl* ) aObject )->ReadAudioFrameL() ;
       
   946             }
       
   947         }
       
   948 
       
   949     if ( ( ( CMccFileSourceImpl* ) aObject )->iFramesPerSample >= 5 ) 
       
   950         {   
       
   951         ( ( CMccFileSourceImpl* ) aObject )->ReadAudioFrameL();
       
   952         }
       
   953         
       
   954     return KErrNone;
       
   955 	}
       
   956     
       
   957 // -----------------------------------------------------------------------------
       
   958 // CMccFileSourceImpl::TickVideoL
       
   959 // 
       
   960 // -----------------------------------------------------------------------------
       
   961 //
       
   962 TInt CMccFileSourceImpl::TickVideoL( TAny* aObject )
       
   963 	{
       
   964 	( ( CMccFileSourceImpl* ) aObject )->ReadVideoFrameL();
       
   965 	
       
   966 	return KErrNone;
       
   967 	}
       
   968         
       
   969 // -----------------------------------------------------------------------------
       
   970 // CMccFileSourceImpl::PauseL()
       
   971 // 
       
   972 // Pauses streaming by cancelling timers
       
   973 // -----------------------------------------------------------------------------
       
   974 //
       
   975 void CMccFileSourceImpl::SourcePauseL()
       
   976     {
       
   977     __FILESOURCE_CONTROLL( "CMccFileSourceImpl::SourcePauseL" )
       
   978 
       
   979     if ( iCurrentState != EPlaying ) 
       
   980     	{
       
   981     	return;
       
   982     	}
       
   983     /*
       
   984     __ASSERT_ALWAYS( iPeriodicAudio, User::Leave( KErrArgument ) );
       
   985     __ASSERT_ALWAYS( iPeriodicAudio, User::Leave( KErrArgument ) );
       
   986 
       
   987     iPeriodicAudio->Cancel();
       
   988     iPeriodicVideo->Cancel();
       
   989       */  
       
   990     iCurrentState = EPaused;
       
   991 	}
       
   992 
       
   993 // -----------------------------------------------------------------------------
       
   994 // CMccFileSourceImpl::StopL()
       
   995 // 
       
   996 // Stops streaming
       
   997 // -----------------------------------------------------------------------------
       
   998 //
       
   999 void CMccFileSourceImpl::SourceStopL()
       
  1000     {
       
  1001     __FILESOURCE_CONTROLL( "CMccFileSourceImpl::SourceStopL" )
       
  1002 
       
  1003     if ( iCurrentState == EConstructed )
       
  1004         {
       
  1005         return;
       
  1006         }
       
  1007 /*
       
  1008     if ( iCurrentState == EPlaying )
       
  1009         {   
       
  1010         __ASSERT_ALWAYS( iPeriodicAudio, User::Leave( KErrArgument ) );
       
  1011         __ASSERT_ALWAYS( iPeriodicAudio, User::Leave( KErrArgument ) );
       
  1012 
       
  1013 		iPeriodicAudio->Cancel();
       
  1014 		iPeriodicVideo->Cancel();
       
  1015         }
       
  1016 */	
       
  1017     SetPositionL(  TTimeIntervalMicroSeconds ( 0 ) );
       
  1018 
       
  1019     iCurrentState = EStopped;
       
  1020 
       
  1021     #ifdef TIMESTAMP
       
  1022        
       
  1023 	if ( iIsAudioLeft == TRUE  && iIsVideoLeft == TRUE  )
       
  1024 		{
       
  1025 		iTimeDifference = 0;
       
  1026 		iPausedTime=0;
       
  1027 		}
       
  1028 		
       
  1029     iPauseStarted = 0;
       
  1030     iPauseStopped = 0;
       
  1031         
       
  1032     #endif
       
  1033 
       
  1034     }
       
  1035 
       
  1036 // -----------------------------------------------------------------------------
       
  1037 // CMccFileSourceImpl::GetFrameLenght()
       
  1038 // 
       
  1039 // Gets audio frame lenght from given buffer
       
  1040 // -----------------------------------------------------------------------------
       
  1041 //
       
  1042 TUint CMccFileSourceImpl::GetFrameLength( TPtrC8 aData )
       
  1043 	{
       
  1044     __FILESOURCE_CONTROLL( "CMccFileSourceImpl::GetFrameLength" )
       
  1045 
       
  1046 	const TUint8 *ptrData=aData.Ptr();
       
  1047 	//read FT from data
       
  1048 	TUint8 fT = (TUint8) ( *ptrData & 0x78 );
       
  1049 	fT = TUint8 ( fT >> 3 );
       
  1050 	//return frame length
       
  1051 	switch ( fT )
       
  1052 		{
       
  1053 			case 0: //amr 4.75
       
  1054 			{
       
  1055                 return 13;
       
  1056             }
       
  1057             case 1: //amr 5.15
       
  1058 			{
       
  1059                 return 14;
       
  1060             }
       
  1061 			case 2: //amr 5.9
       
  1062 			{
       
  1063 				return 16;
       
  1064             }
       
  1065  			case 3: //amr 6.7
       
  1066             {
       
  1067 				return 18;
       
  1068             }
       
  1069 			case 4: //amr 7.4
       
  1070 			{
       
  1071 				return 20;
       
  1072             }
       
  1073             case 5: //amr 7.95
       
  1074             {
       
  1075 			    return 21;
       
  1076             }
       
  1077             case 6: //amr 10.2
       
  1078 			{
       
  1079                 return 27;
       
  1080             }
       
  1081             case 7: //amr 12.2
       
  1082             {
       
  1083                 return 32;
       
  1084             }
       
  1085 			case 8: //amr SID
       
  1086 			{
       
  1087 				return 6;
       
  1088 			}
       
  1089 
       
  1090 			case 15: //no data
       
  1091 				{
       
  1092 				return 1;
       
  1093 				}
       
  1094             default: //not arm narrowband
       
  1095             {
       
  1096 				return 0;
       
  1097             }
       
  1098         }
       
  1099 }
       
  1100 
       
  1101 // -----------------------------------------------------------------------------
       
  1102 // CMccFileSourceImpl::SourceDataTypeCode()
       
  1103 // -----------------------------------------------------------------------------
       
  1104 //
       
  1105 TFourCC CMccFileSourceImpl::SourceDataTypeCode( TMediaId aMediaId )
       
  1106 	{
       
  1107 	__FILESOURCE_CONTROLL( "CMccFileSourceImpl::SourceDataTypeCode" )
       
  1108 
       
  1109     if ( KUidMediaTypeVideo == aMediaId.iMediaType )
       
  1110         {
       
  1111         return iVideoFourCC;
       
  1112         }
       
  1113     else if ( KUidMediaTypeAudio == aMediaId.iMediaType  )
       
  1114         {
       
  1115         return iAudioFourCC;
       
  1116         }
       
  1117     else
       
  1118         {
       
  1119         return TFourCC( NULL );
       
  1120         }
       
  1121 	}
       
  1122 	
       
  1123 // -----------------------------------------------------------------------------
       
  1124 // CMccFileSourceImpl::SetSourceDataTypeCode()
       
  1125 // -----------------------------------------------------------------------------
       
  1126 //
       
  1127 TInt CMccFileSourceImpl::SetSourceDataTypeCode( 
       
  1128 	TFourCC aCodec, 
       
  1129     TMediaId aMediaId )
       
  1130 	{
       
  1131 	__FILESOURCE_CONTROLL( "CMccFileSourceImpl::SetSourceDataTypeCode" )
       
  1132 	
       
  1133     if ( ( KUidMediaTypeVideo == aMediaId.iMediaType &&
       
  1134     	aCodec == iVideoFourCC ) ||
       
  1135     	 ( KUidMediaTypeAudio == aMediaId.iMediaType &&
       
  1136     	aCodec == iAudioFourCC ) )
       
  1137         {
       
  1138         return KErrNone;
       
  1139         } 
       
  1140     else
       
  1141 	    {
       
  1142 	    return KErrNotSupported;
       
  1143 	    }
       
  1144 	}	
       
  1145 
       
  1146 // -----------------------------------------------------------------------------
       
  1147 // CMccFileSourceImpl::SourcePrimeL()
       
  1148 // -----------------------------------------------------------------------------
       
  1149 //
       
  1150 void CMccFileSourceImpl::SourcePrimeL()
       
  1151 	{
       
  1152 	__FILESOURCE_CONTROLL( "CMccFileSourceImpl::SourcePrimeL" )
       
  1153 	}
       
  1154 
       
  1155 // -----------------------------------------------------------------------------
       
  1156 // CMccFileSourceImpl::FillBufferL()
       
  1157 // -----------------------------------------------------------------------------
       
  1158 //
       
  1159 void CMccFileSourceImpl::FillBufferL( CMMFBuffer* aBuffer,
       
  1160 						              MDataSink*  aConsumer,
       
  1161 						              TMediaId    aMediaId )
       
  1162 	{
       
  1163 	__FILESOURCE_MEDIA( "CMccFileSourceImpl::FillBufferL" )
       
  1164 
       
  1165 	if (aMediaId.iMediaType == KUidMediaTypeAudio)
       
  1166 		{
       
  1167     	__FILESOURCE_MEDIA( "CMccFileSourceImpl::FillBufferL, audio" )
       
  1168 	    iAudioConsumer = aConsumer;
       
  1169 		iAudioConsumerBuffer = aBuffer;		
       
  1170 		}
       
  1171 	else if (aMediaId.iMediaType == KUidMediaTypeVideo )
       
  1172 		{
       
  1173     	__FILESOURCE_MEDIA( "CMccFileSourceImpl::FillBufferL, video" )    
       
  1174 	    iVideoConsumer = aConsumer;
       
  1175 		iVideoConsumerBuffer = aBuffer;		
       
  1176 		}
       
  1177 	else
       
  1178 		{
       
  1179     	__FILESOURCE_MEDIA( "CMccFileSourceImpl::FillBufferL, unknown media " ) 
       
  1180 		User::Leave( KErrNotSupported );	
       
  1181 		}
       
  1182 
       
  1183 	}	
       
  1184 	
       
  1185 // -----------------------------------------------------------------------------
       
  1186 // CMccFileSourceImpl::MarkWithNALUDelimiters()
       
  1187 // -----------------------------------------------------------------------------
       
  1188 //
       
  1189 void CMccFileSourceImpl::MarkWithNALUDelimiters( 
       
  1190 	const TInt aAccessUnitSize, TDes8& aBuffer )
       
  1191 	{
       
  1192     __FILESOURCE_CONTROLL( "CMccFileSourceImpl::MarkWithNALUDelimiters" )
       
  1193     
       
  1194     TUint8* ptrByte = NULL;
       
  1195     TInt indx = 0;
       
  1196     TUint32 size;
       
  1197     const TUint32 size_field_len = 4;
       
  1198     
       
  1199     do
       
  1200 	    {
       
  1201     	ptrByte = const_cast<TUint8*> ( aBuffer.Mid( indx ).Ptr() );	// get starting point of NALU size
       
  1202     	size = BigEndian::Get32(ptrByte);	// get NALU size
       
  1203     	aBuffer[indx+0] = 0x00;
       
  1204     	aBuffer[indx+1] = 0x00;
       
  1205     	aBuffer[indx+2] = 0x00;
       
  1206     	aBuffer[indx+3] = 0x01;
       
  1207     	indx = indx + size + size_field_len;
       
  1208     	
       
  1209 	    }while(indx < aAccessUnitSize);
       
  1210     
       
  1211 	}
       
  1212 
       
  1213 // -----------------------------------------------------------------------------
       
  1214 // CMccFileSourceImpl::ExtractH264ParameterSetNALUsL()
       
  1215 // -----------------------------------------------------------------------------
       
  1216 //
       
  1217 
       
  1218 void CMccFileSourceImpl::ExtractH264ParameterSetNALUsL( HBufC8** aBufferOut )
       
  1219 	{
       
  1220 	__FILESOURCE_CONTROLL( "CMccFileSourceImpl::ExtractH264ParameterSetNALUsL" )
       
  1221 	
       
  1222 	*aBufferOut = NULL;
       
  1223 	
       
  1224 	MP4Err error = MP4_OK;
       
  1225 	HBufC8* buffer = NULL;
       
  1226 	HBufC8* bufferOut = NULL;
       
  1227 	TUint32 decspecinfosize;
       
  1228 	TUint8* ptrByte;
       
  1229 	
       
  1230 	// Query for size
       
  1231 	error = MP4ParseReadVideoDecoderSpecificInfo( iMP4Handle,
       
  1232 												  ( mp4_u8* ) NULL,
       
  1233 												  0,
       
  1234 												  ( mp4_u32* ) &decspecinfosize
       
  1235 											 	);
       
  1236 	if( ( MP4_OK != error ) && ( MP4_BUFFER_TOO_SMALL != error ) )
       
  1237 		{
       
  1238 		User::Leave(KErrGeneral);
       
  1239 		}
       
  1240 	
       
  1241 	buffer = HBufC8::NewLC(decspecinfosize);
       
  1242 	ptrByte = const_cast<TUint8*> ( buffer->Des().Ptr() );
       
  1243 	
       
  1244 	error = MP4ParseReadVideoDecoderSpecificInfo( iMP4Handle,
       
  1245 												  ( mp4_u8* ) ptrByte,
       
  1246 												  ( mp4_u32 ) buffer->Des().MaxSize(),
       
  1247 												  ( mp4_u32* ) &decspecinfosize
       
  1248 											 	);
       
  1249 	if(MP4_OK != error)
       
  1250 		{
       
  1251 		User::Leave(KErrGeneral);
       
  1252 		}
       
  1253 	else
       
  1254 		{
       
  1255 		buffer->Des().SetLength( decspecinfosize );
       
  1256 		}
       
  1257 	
       
  1258 	bufferOut = HBufC8::NewLC( buffer->Size() * 2 );
       
  1259 	TPtr8 ptrBufferOut = bufferOut->Des();
       
  1260 	TPtr8 ptrBuffer = buffer->Des();
       
  1261 	
       
  1262 	// find NALUs in decoder info and put in buffer by marking with delimiters
       
  1263 	
       
  1264 	TUint8 tmpByte;
       
  1265 	TUint16 size16;
       
  1266 	TInt indx = 5;		// skip other info in the beginning
       
  1267 	TInt numNALUs = 0;
       
  1268 	TInt lp_indx;
       
  1269 	
       
  1270 	// get number of seq. parameter set NALUs
       
  1271 	tmpByte = ptrBuffer[indx++];
       
  1272 	numNALUs = tmpByte & 0x1F;		// get rid of reserved '111' bits
       
  1273 	
       
  1274 	// extract seq. parameter set NALUs
       
  1275 	
       
  1276 	for ( lp_indx = 0; lp_indx < numNALUs; lp_indx++ )
       
  1277 		{
       
  1278 		size16 = BigEndian::Get16( ptrBuffer.Mid( indx ).Ptr() );
       
  1279 		indx += 2;
       
  1280 		
       
  1281 		// insert delimiter
       
  1282 		tmpByte = 0x00;
       
  1283 		ptrBufferOut.Append( &tmpByte, 1 );
       
  1284 		ptrBufferOut.Append( &tmpByte, 1 );
       
  1285 		ptrBufferOut.Append( &tmpByte, 1 );
       
  1286 		tmpByte = 0x01;
       
  1287 		ptrBufferOut.Append( &tmpByte, 1 );
       
  1288 		
       
  1289 		ptrBufferOut.Append( ptrBuffer.Mid( indx, size16 ) );
       
  1290 		
       
  1291 		indx  += size16;
       
  1292 		}
       
  1293 	
       
  1294 	// get number of pic. parameter set NALUs
       
  1295 	tmpByte = ptrBuffer[indx++];
       
  1296 	numNALUs = tmpByte;
       
  1297 	
       
  1298 	// extract pic. parameter set NALUs
       
  1299 	
       
  1300 	for ( lp_indx = 0; lp_indx < numNALUs; lp_indx++ )
       
  1301 		{
       
  1302 		size16 = BigEndian::Get16( ptrBuffer.Mid( indx ).Ptr() );
       
  1303 		indx += 2;
       
  1304 		
       
  1305 		// insert delimiter
       
  1306 		tmpByte = 0x00;
       
  1307 		ptrBufferOut.Append( &tmpByte, 1 );
       
  1308 		ptrBufferOut.Append( &tmpByte, 1 );
       
  1309 		ptrBufferOut.Append( &tmpByte, 1 );
       
  1310 		tmpByte = 0x01;
       
  1311 		ptrBufferOut.Append( &tmpByte, 1 );
       
  1312 		
       
  1313 		ptrBufferOut.Append( ptrBuffer.Mid( indx, size16 ) );
       
  1314 		
       
  1315 		indx  += size16;
       
  1316 		}
       
  1317 	
       
  1318 	CleanupStack::Pop( bufferOut );	
       
  1319 	CleanupStack::PopAndDestroy( buffer );
       
  1320 	
       
  1321 	*aBufferOut = bufferOut;
       
  1322 	
       
  1323 	}
       
  1324 	
       
  1325 
       
  1326 // -----------------------------------------------------------------------------
       
  1327 // CMccFileSourceImpl::IsAVCVideo()
       
  1328 // -----------------------------------------------------------------------------
       
  1329 //
       
  1330 
       
  1331 TBool CMccFileSourceImpl::IsAVCVideo()
       
  1332 	{
       
  1333 	if ( ( iVideoType == MP4_TYPE_AVC_PROFILE_BASELINE ) ||
       
  1334          ( iVideoType == MP4_TYPE_AVC_PROFILE_MAIN )	 ||
       
  1335          ( iVideoType == MP4_TYPE_AVC_PROFILE_EXTENDED )
       
  1336        )
       
  1337 	    {
       
  1338 	   	return ETrue;
       
  1339 	    }
       
  1340     
       
  1341     return EFalse;
       
  1342 	}
       
  1343 
       
  1344 // -----------------------------------------------------------------------------
       
  1345 // CMccFileSourceImpl::FileOpenErrorL
       
  1346 // -----------------------------------------------------------------------------
       
  1347 //	
       
  1348 void CMccFileSourceImpl::FileOpenErrorL( MP4Err error )
       
  1349 	{
       
  1350 	iMP4Handle = NULL;
       
  1351 	if ( error == MP4_OUT_OF_MEMORY )
       
  1352 		{
       
  1353 		User::Leave( KErrNoMemory );	
       
  1354 		}
       
  1355 	else
       
  1356 		{
       
  1357 		User::Leave( KErrGeneral );	
       
  1358 		}
       
  1359 	}
       
  1360 	
       
  1361 // -----------------------------------------------------------------------------
       
  1362 // CMccFileSourceImpl::ParseUpdateVideoDescriptions
       
  1363 // -----------------------------------------------------------------------------
       
  1364 //
       
  1365 void CMccFileSourceImpl::ParseUpdateVideoDescriptions( )
       
  1366 	{		
       
  1367 	if ( MP4ParseRequestVideoDescription( iMP4Handle,
       
  1368 		 ( mp4_u32* ) &iVideoLength,  &iFrameRate, ( mp4_u32* ) &iVideoType,
       
  1369 	 	 ( mp4_u32* ) &iVideoWidth,  ( mp4_u32* ) &iVideoHeight,
       
  1370          ( mp4_u32* ) &iVideoTimeScale ) 
       
  1371          == MP4_OK )
       
  1372 		{
       
  1373 		iFrameSize.iWidth = iVideoWidth;
       
  1374 		iFrameSize.iHeight = iVideoHeight;
       
  1375 		  
       
  1376 		if ( iVideoType == MP4_TYPE_H263_PROFILE_0 )
       
  1377 		    {
       
  1378 		    RDebug::Print(_L("CMccFileSourceImpl::OpenFileL MP4_TYPE_H263_PROFILE_0"));
       
  1379 
       
  1380 		    iCurrentVideoType = CCMRMediaBuffer::EVideoH263;
       
  1381 		    iIsVideo = ETrue;
       
  1382 		    iIsVideoLeft = ETrue;
       
  1383 		 //   iVideoFourCC = TFourCC( KMccFourCCIdAVC );
       
  1384 		    iVideoFourCC = TFourCC( KMccFourCCIdH263 );
       
  1385 		    }
       
  1386 		if ( iVideoType == MP4_TYPE_MPEG4_VIDEO )
       
  1387 		    {
       
  1388 		    RDebug::Print(_L("CMccFileSourceImpl::OpenFileL MP4_TYPE_MPEG4_VIDEO!"));
       
  1389 
       
  1390 		    iCurrentVideoType = CCMRMediaBuffer::EVideoH263;
       
  1391 		    iIsVideo = ETrue;
       
  1392 		    iIsVideoLeft = ETrue;
       
  1393 		    iVideoFourCC = TFourCC( KMccFourCCIdAVC );
       
  1394 		    }
       
  1395 		if ( IsAVCVideo() )
       
  1396 		    {
       
  1397 		    RDebug::Print(_L("CMccFileSourceImpl::OpenFileL AVC!"));
       
  1398 
       
  1399 		    iCurrentVideoType = CCMRMediaBuffer::EVideoH263;
       
  1400 		    iIsVideo = ETrue;
       
  1401 		    iIsVideoLeft = ETrue;
       
  1402 		    iVideoFourCC = TFourCC( KMccFourCCIdAVC );
       
  1403 		    iIsFirstVideoFrame = ETrue;
       
  1404 		    }
       
  1405 		}
       
  1406 	}
       
  1407 
       
  1408 // -----------------------------------------------------------------------------
       
  1409 // CMccFileSourceImpl::ParseUpdateVideoDescriptions
       
  1410 // -----------------------------------------------------------------------------
       
  1411 //	    
       
  1412 void CMccFileSourceImpl::ParseUpdateAudioDescriptions( )
       
  1413 	{
       
  1414     if ( MP4ParseRequestAudioDescription( iMP4Handle,
       
  1415          ( mp4_u32* ) &iAudioLength, ( mp4_u32* ) &iAudioType,
       
  1416          ( mp4_u8* ) &iFramesPerSample, ( mp4_u32* ) &iAudioTimeScale,
       
  1417          ( mp4_u32* ) &iAverageBitRate )
       
  1418          == MP4_OK )
       
  1419 	    {
       
  1420         if ( iAudioType == MP4_TYPE_AMR_NB )
       
  1421 	        {
       
  1422 	        RDebug::Print(_L("CMccFileSourceImpl::OpenFileL MP4_TYPE_AMR_NB!"));
       
  1423 
       
  1424 	        iCurrentAudioType = CCMRMediaBuffer::EAudioAMRNB;
       
  1425 	        iIsAudio = ETrue;
       
  1426 	        iIsAudioLeft = ETrue;
       
  1427 	        iAudioFourCC = TFourCC( KMccFourCCIdAMRNB );
       
  1428 	        }	
       
  1429 	    }
       
  1430 	}
       
  1431 
       
  1432 #ifndef EKA2
       
  1433 // DLL interface code
       
  1434 EXPORT_C TInt E32Dll( TDllReason )
       
  1435     {
       
  1436     return KErrNone;
       
  1437     }
       
  1438 #endif