videoengine.cpp
上传用户:xinrui0099
上传日期:2010-03-05
资源大小:48k
文件大小:12k
源码类别:

Symbian

开发平台:

C/C++

  1. /*
  2. * ============================================================================
  3. *  Name     : CVideoEngine from CVideoEngine.h
  4. *  Part of  : Video
  5. *  Created  : 10/14/2003 by Forum Nokia
  6. *  Implementation notes:
  7. *
  8. *  Version  :
  9. *  Copyright: Nokia Corporation, 2003
  10. * ============================================================================
  11. */
  12. #include "videoengine.h"
  13. #include <eikenv.h>
  14. #include <VideoPlayer.h>
  15. #include <mmferrors.h>
  16. #include "MPlayerUIControllerListener.h"
  17. #include <documenthandler.h>
  18. #include "VideoFileDetails.h"
  19. #include <drmcommon.h>
  20. #include <f32file.h>
  21. /*
  22. -----------------------------------------------------------------------------
  23.     CVideoEngine* CVideoEngine::NewL( )
  24. Description: Create an engine object
  25. Comments:
  26.     Return values: CVideoEngine object pointer
  27. -----------------------------------------------------------------------------
  28. */
  29. CVideoEngine* CVideoEngine::NewL( )
  30.     {
  31.     CVideoEngine* self = 
  32.         new (ELeave) CVideoEngine( );
  33.     
  34.     CleanupStack::PushL( self );
  35.     self->ConstructL( );
  36.     CleanupStack::Pop();
  37.     return self;
  38.     }
  39. /*
  40. -----------------------------------------------------------------------------
  41.     CVideoEngine::CVideoEngine( )
  42. Description: Constructor
  43. Comments:
  44.     Return values: N/A
  45. -----------------------------------------------------------------------------
  46. */    
  47. CVideoEngine::CVideoEngine( )
  48. {
  49. }
  50. /*
  51. -----------------------------------------------------------------------------
  52.     CVideoEngine::ConstructL()
  53. Description: Second-phase constructor
  54. Comments:
  55.     Return values: N/A
  56. -----------------------------------------------------------------------------
  57. */
  58. void CVideoEngine::ConstructL()
  59. {
  60.     iMediaFile = HBufC::NewL(0);
  61. iMyState = EPNotInitialised; 
  62.     iFileDetails = new (ELeave) CVideoFileDetails();
  63. // Create a progress updater
  64.     iProgressUpdater = CHeartbeat::NewL(0);
  65. }
  66. /*
  67. -----------------------------------------------------------------------------
  68.     CVideoEngine::~CVideoEngine()
  69. Description: Destructor
  70. Comments:
  71.     Return values: N/A
  72. -----------------------------------------------------------------------------
  73. */    
  74. CVideoEngine::~CVideoEngine()
  75. {
  76.     delete iDocHandler;
  77. delete iMediaFile;
  78. delete iFileDetails;
  79.     delete iProgressUpdater;
  80. }
  81. /*
  82. -----------------------------------------------------------------------------
  83. void CVideoEngine::SetNewFileL(const TDesC &aFileName)
  84. Description : Set a file name for a video clip
  85. Comments : Possibly, we need to do something here to verify the file 
  86.      existence.
  87.     Return values: N/A
  88. -----------------------------------------------------------------------------
  89. */    
  90. void CVideoEngine::SetNewFileL(const TDesC &aFileName)
  91. {
  92.     HBufC* newFile = aFileName.AllocL();
  93.     delete iMediaFile; // after the AllocL succeeds 
  94.     iMediaFile = newFile;
  95. }
  96. /*
  97. -----------------------------------------------------------------------------
  98. void CVideoEngine::DoPlayL()
  99. Description : Play a video clip
  100. Comments : 
  101.     Return values: N/A
  102. -----------------------------------------------------------------------------
  103. */    
  104. void CVideoEngine::DoPlayL()
  105. {
  106. // If the engine is not in the paused state, start the progress update.
  107.     if ( !iProgressUpdater->IsActive())
  108.         {
  109.         iProgressUpdater->Start(ETwelveOClock,this); 
  110.         }
  111. iPlayer->Play();
  112.     switch ( iMyState )
  113.         {
  114.         case EPStopped:
  115. {
  116.             iPlayPosition = 0;
  117.     iCallback->PlaybackPositionChangedL(iPlayPosition,iFileDetails->iDurationInSeconds);
  118.             break;
  119. }
  120.         case EPPaused:
  121.             iCallback->PlaybackPositionChangedL(iPlayPosition,iFileDetails->iDurationInSeconds);
  122.             break;
  123.         default:
  124.             break;
  125.         }
  126. iMyState = EPPlaying;
  127. }
  128. /*
  129. -----------------------------------------------------------------------------
  130. void CVideoEngine::DoStop()
  131. Description : Stop a playing video clip
  132. Comments : 
  133.     Return values: N/A
  134. -----------------------------------------------------------------------------
  135. */    
  136. void CVideoEngine::DoStop()
  137. {
  138. if ( iMyState != EPPlaying && iMyState != EPPaused )
  139. return;
  140.     if (iProgressUpdater->IsActive())
  141.         {
  142.         iProgressUpdater->Cancel();
  143.         }
  144.     iPlayPosition = 0;
  145.     iPlayer->Stop();
  146. iPlayer->Close();
  147.     iMyState = EPStopped;
  148. }
  149. /*
  150. -----------------------------------------------------------------------------
  151. void CVideoEngine::MvpuoOpenComplete(TInt aError )
  152. Description : File open is completed
  153. Comments : 
  154.     Return values: N/A
  155. -----------------------------------------------------------------------------
  156. */    
  157. void CVideoEngine::MvpuoOpenComplete(TInt /*aError*/ )
  158.     {
  159. // Prepare for the loading the file
  160.     iPlayer->Prepare();
  161. }
  162. /*
  163. -----------------------------------------------------------------------------
  164. void CVideoEngine::MvpuoFrameReady(CFbsBitmap& aFrame,TInt aError)
  165. Description : Frame is ready
  166. Comments : 
  167.     Return values: N/A
  168. -----------------------------------------------------------------------------
  169. */    
  170. void CVideoEngine::MvpuoFrameReady(CFbsBitmap& /*aFrame*/,TInt /*aError*/)
  171.     {
  172.     // do nothing...
  173.     }
  174. /*
  175. -----------------------------------------------------------------------------
  176. void CVideoEngine::MvpuoPlayComplete(TInt aError)
  177. Description : Play is complete
  178. Comments : 
  179.     Return values: N/A
  180. -----------------------------------------------------------------------------
  181. */    
  182. void CVideoEngine::MvpuoPlayComplete(TInt aError)
  183.     {
  184.     if (iProgressUpdater->IsActive())
  185.         {
  186.         iProgressUpdater->Cancel();
  187.         }
  188.     iPlayPosition = 0;
  189.     iCallback->PlayCompletedL(aError);
  190.     iMyState = EPStopped;
  191. iPlayer->Close();
  192.     }
  193. /*
  194. -----------------------------------------------------------------------------
  195. void CVideoEngine::MvpuoPrepareComplete(TInt aError )
  196. Description : Prepare is complete
  197. Comments : 
  198.     Return values: N/A
  199. -----------------------------------------------------------------------------
  200. */    
  201. void CVideoEngine::MvpuoPrepareComplete(TInt aError )
  202.     {
  203.     
  204.     TInt ret = aError;
  205.  
  206.     iMyState = EPStopped;
  207.     iFileDetails->iDurationInSeconds = 0;
  208. if ( ret == KErrNone || ret == KErrMMPartialPlayback )
  209. {
  210.         TSize size( 0, 0 );
  211.         iPlayer->VideoFrameSizeL(size);
  212.         iFileDetails->iResolutionHeight = size.iHeight;
  213.         iFileDetails->iResolutionWidth = size.iWidth;
  214. // Is there any audio track?
  215.         iFileDetails->iAudioTrack = iPlayer->AudioEnabledL();
  216.             
  217.         // video track
  218.         iFileDetails->iVideoTrack = iPlayer->VideoBitRateL();
  219.             
  220.         // duration of the video clip
  221.         iFileDetails->iDurationInSeconds = iPlayer->DurationL().Int64() / KMPOneSecond;           
  222. }
  223.     if ( iCallback ) 
  224.         {
  225.         TRAPD( ignore,iCallback->InitControllerCompletedL( ret ) );
  226.         }
  227.     }
  228. /*
  229. -----------------------------------------------------------------------------
  230. void CVideoEngine::MvpuoEvent(const TMMFEvent& aEvent)
  231. Description : Mvpuo Event
  232. Comments : 
  233.     Return values: N/A
  234. -----------------------------------------------------------------------------
  235. */    
  236. void CVideoEngine::MvpuoEvent(const TMMFEvent& aEvent)
  237.     {
  238.     // Higher priority application has taken over the audio device. --> Do pause.
  239.     if (aEvent.iEventType == KMMFEventCategoryVideoPlayerGeneralError &&
  240.         aEvent.iErrorCode == KErrHardwareNotAvailable)
  241.         {
  242.         TRAPD(ignore,iCallback->PlayCompletedL(KErrAudioLost));    
  243.         }
  244.     }
  245. /*
  246. -----------------------------------------------------------------------------
  247. void CVideoEngine::InitControllerL( MPlayerUIControllerListener* aCallback,
  248.                                     RWsSession& aWs,
  249.                                     CWsScreenDevice& aScreenDevice,
  250.                                     RWindowBase& aWindow,
  251.                                     TRect& aScreenRect,
  252.                                     TRect& aClipRect )   
  253. Description : Initialize the video controller
  254. Comments : 
  255.     Return values: N/A
  256. -----------------------------------------------------------------------------
  257. */    
  258. void CVideoEngine::InitControllerL( MPlayerUIControllerListener* aCallback,
  259.                                     RWsSession& aWs,
  260.                                     CWsScreenDevice& aScreenDevice,
  261.                                     RWindowBase& aWindow,
  262.                                     TRect& aScreenRect,
  263.                                     TRect& aClipRect )   
  264.     {
  265.     iMyState = EPInitialising;
  266.     iCallback = aCallback;
  267.     
  268.     delete iPlayer;
  269.     iPlayer = NULL;
  270.     iPlayer = CVideoPlayerUtility::NewL( *this, EMdaPriorityNormal, 
  271.                                  EMdaPriorityPreferenceNone, aWs, 
  272.  aScreenDevice,aWindow,aScreenRect,
  273.  aClipRect );
  274.     iPlayer->OpenFileL( iMediaFile->Des() );
  275.     }
  276. /*
  277. -----------------------------------------------------------------------------
  278. CDocumentHandler& CVideoEngine::DocumentHandlerL()
  279. Description: Open the video clip using a document handler
  280. Comments   :
  281.     Return values: None
  282. -----------------------------------------------------------------------------
  283. */
  284. CDocumentHandler& CVideoEngine::DocumentHandlerL()
  285. {
  286.     if( !iDocHandler )
  287.         {
  288.         iDocHandler = CDocumentHandler::NewL( CEikonEnv::Static()->Process() );
  289.         }
  290. return *iDocHandler;
  291. }
  292. /*
  293. -----------------------------------------------------------------------------
  294. void CVideoEngine::Beat()
  295. Description: it is called when the periodic timer times out, provided by
  296.  MBeating interface.
  297. Comments   :
  298.     Return values: None
  299. -----------------------------------------------------------------------------
  300. */
  301. void CVideoEngine::Beat()
  302.     {
  303.     // keep backlights on if clip has video
  304.     if ( iFileDetails->iVideoTrack )
  305.         {
  306.         User::ResetInactivityTime();
  307.         }
  308. if ( iMyState != EPPlaying )
  309. return;
  310.     TInt64 ret = iPlayPosition%2;
  311.     
  312.     if ( ret.GetTInt() )
  313.         {
  314.         iPlayPosition = iPlayPosition + 1;
  315.         if (iCallback)
  316.             {
  317.             TRAPD(ignore,iCallback->PlaybackPositionChangedL(iPlayPosition,iFileDetails->iDurationInSeconds));
  318.             }
  319.         }
  320.     else
  321.         {
  322.         Synchronize();
  323.         }
  324.     }
  325. /*
  326. -----------------------------------------------------------------------------
  327. void CVideoEngine::Synchronize()
  328. Description: If one or more heartbeats are missed then the 
  329.              MBeating::Synchronize() function is called
  330. Comments   :
  331.     Return values: None
  332. -----------------------------------------------------------------------------
  333. */
  334. void CVideoEngine::Synchronize()
  335.     {
  336. if ( iMyState != EPPlaying )
  337. return;
  338.     
  339.     TRAPD( ignore,
  340. // if live stream, fake progress
  341.         iPlayPosition = iPlayer->PositionL().Int64() / KMPOneSecond;
  342.                         
  343.         if (iCallback)
  344. {
  345.             iCallback->PlaybackPositionChangedL(iPlayPosition,iFileDetails->iDurationInSeconds);
  346.             }
  347.         );
  348.     }
  349. /*
  350. -----------------------------------------------------------------------------
  351. TInt CVideoEngine::GetEngineState()
  352. Description: Get the video engine state.
  353. Comments   :
  354.     Return values: None
  355. -----------------------------------------------------------------------------
  356. */
  357. TInt CVideoEngine::GetEngineState()
  358. {
  359. return iMyState;
  360. }
  361. /*
  362. -----------------------------------------------------------------------------
  363. void CVideoEngine::DoPauseL()
  364. Description: Pause a playing video if it is playing.
  365. Comments:
  366.     Return values: None
  367. -----------------------------------------------------------------------------
  368. */
  369. void CVideoEngine::DoPauseL()
  370. {
  371.     if ( iMyState == EPPlaying )
  372.        {
  373.         if (iProgressUpdater->IsActive())
  374.             {
  375.             iProgressUpdater->Cancel();
  376.             }
  377.         iPlayer->PauseL();
  378.         iMyState = EPPaused;
  379.         }
  380. }