⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 videoplayeradapter.cpp

📁 nokia S60_video_operation
💻 CPP
字号:
/*
* ============================================================================
*  Name     : CVideoPlayerAdapter from VideoPlayerAdapter.h
*  Part of  : Video Recording Example (VRex)
*  Created  : 16.03.2004 by Nokia Forum
*  Description: Declares the video player adapter class
*
*  Version  :
*  Copyright: Nokia Corporation
* ============================================================================
*/

#include "VideoPlayerAdapter.h"

#include <eikenv.h>
#include <VideoPlayer.h>
#include <mmferrors.h>
#include "MPlayerUIControllerListener.h"
#include <documenthandler.h>
#include "VideoFileDetails.h"
#include <drmcommon.h>
#include <f32file.h>

/*
-----------------------------------------------------------------------------

	CVideoPlayerAdapter* CVideoPlayerAdapter::NewL()

	Description: Two-phased constructor.
	Comments   :

    Return values: CVideoPlayerAdapter object pointer

-----------------------------------------------------------------------------
*/
CVideoPlayerAdapter* CVideoPlayerAdapter::NewL() 
	{
	CVideoPlayerAdapter* self = CVideoPlayerAdapter::NewLC();
	CleanupStack::Pop();
    return self;
	}

/*
-----------------------------------------------------------------------------

	CVideoPlayerAdapter* CVideoPlayerAdapter::NewLC()

	Description: Two-phased constructor.
	Comments   : Method leaves pointer to object in cleanup stack

    Return values: CVideoPlayerAdapter object pointer

-----------------------------------------------------------------------------
*/
CVideoPlayerAdapter* CVideoPlayerAdapter::NewLC()
	{
    CVideoPlayerAdapter* self = new (ELeave) CVideoPlayerAdapter();
    CleanupStack::PushL(self);
    self->ConstructL();
	return self;
	}

/*
-----------------------------------------------------------------------------

	void CVideoPlayerAdapter::ConstructL()

	Description: Second phase constructor.
	Comments   :

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CVideoPlayerAdapter::ConstructL()
	{
    iMediaFile = HBufC::NewL(0);
	// Create a progress updater
    iFileDetails = new (ELeave) CVideoFileDetails;
    iProgressUpdater = CHeartbeat::NewL(0);
	}

/*
-----------------------------------------------------------------------------

	CVideoPlayerAdapter::CVideoPlayerAdapter()

	Description: Default constructor.
	Comments   : Constructor may never leave

    Return values: N/A

-----------------------------------------------------------------------------
*/
CVideoPlayerAdapter::CVideoPlayerAdapter() :
	iState(ENotInitialized)
	{
	}

/*
-----------------------------------------------------------------------------

	CVideoPlayerAdapter::~CVideoPlayerAdapter()

	Description: Destructor.
	Comments   :

    Return values: N/A

-----------------------------------------------------------------------------
*/
CVideoPlayerAdapter::~CVideoPlayerAdapter()
	{
    delete iDocHandler;
    iDocHandler = NULL;
    
	delete iMediaFile;
	iMediaFile = NULL;
	
	delete iFileDetails;
	iFileDetails = NULL; 
	
    delete iProgressUpdater;
    iProgressUpdater = NULL;

	delete iPlayer;
	iPlayer = NULL;
	}

/*
-----------------------------------------------------------------------------

	void CVideoPlayerAdapter::InitControllerL(MPlayerUIControllerListener* aControllerListener,
											    RWsSession& aWs,
											    CWsScreenDevice& aScreenDevice,
											    RWindowBase& aWindow,
											    TRect& aScreenRect,
											    TRect& aClipRect)  

	Description: This method initializes the video player controller.
	Comments   :

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CVideoPlayerAdapter::InitControllerL( 
		MPlayerUIControllerListener* aControllerListener,
        RWsSession& aWs,
        CWsScreenDevice& aScreenDevice,
        RWindowBase& aWindow,
        TRect& aScreenRect,
        TRect& aClipRect)   
    {
    iState = EInitializing;
    iCtrlrListener = aControllerListener;
    
    delete iPlayer;
    iPlayer = NULL;

    iPlayer = CVideoPlayerUtility::NewL(*this, EMdaPriorityNormal, 
		                                EMdaPriorityPreferenceNone, aWs, 
										aScreenDevice,aWindow,aScreenRect,
										aClipRect);

    TRAPD(unsupported, iPlayer->OpenFileL(iMediaFile->Des()));
	if(unsupported!=KErrNone)
		{
		iPlayer->Close();
		delete iPlayer;
		iPlayer = NULL;
		iCtrlrListener->PlayCompletedL(KErrNotSupported);// Clear the status pane 
		User::Leave(unsupported);
		}
    }

/*
-----------------------------------------------------------------------------

	void CVideoPlayerAdapter::SetNewFileL(const TDesC& aFileName)

	Description: This method sets new file name to play.
	Comments   :

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CVideoPlayerAdapter::SetNewFileL(const TDesC& aFileName)
	{
    HBufC* newFile = aFileName.AllocL();
    
    delete iMediaFile;		// after the AllocL succeeds 
    iMediaFile = newFile;
	}

/*
-----------------------------------------------------------------------------

	void CVideoPlayerAdapter::PlayL()

	Description: This method plays the video file.
	Comments   :

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CVideoPlayerAdapter::PlayL()
	{
	// If the engine is not in the paused state, start the progress update.
    if(!iProgressUpdater->IsActive())
        {
        iProgressUpdater->Start(ETwelveOClock,this); 
        }

	iPlayer->Play();

    switch(iState)
        {
        case EStopped:
			{
            iPlayPosition = 0;
		    iCtrlrListener->PlaybackPositionChangedL(iPlayPosition,iFileDetails->iDurationInSeconds);
            break;
			}
        case EPaused:
            iCtrlrListener->PlaybackPositionChangedL(iPlayPosition,iFileDetails->iDurationInSeconds);
            break;
        default:
            break;
        }

	iState = EPlaying;
	}

/*
-----------------------------------------------------------------------------

	void CVideoPlayerAdapter::PauseL()

	Description: This method pauses the playback of video file.
	Comments   :

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CVideoPlayerAdapter::PauseL()
	{
    if(iState == EPlaying)
       {
        if(iProgressUpdater->IsActive())
            {
            iProgressUpdater->Cancel();
            }
        iPlayer->PauseL();
        iState = EPaused;
        }
	}

/*
-----------------------------------------------------------------------------

	void CVideoPlayerAdapter::Stop()

	Description: This method stops the playback of video file.
	Comments   :

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CVideoPlayerAdapter::Stop()
	{
	if(iState != EPlaying && iState != EPaused)
		return;

    iProgressUpdater->Cancel();
    
    iPlayPosition = 0;
    iPlayer->Stop();
	iPlayer->Close();
    iState = EStopped;
	}
	
/*
-----------------------------------------------------------------------------

	void CVideoPlayerAdapter::MvpuoOpenComplete(TInt aError)

	Description: This method defines required client behaviour when an attempt to open 
				 a video sample has completed, successfully or otherwise.
	Comments   :

    Return values: N/A

-----------------------------------------------------------------------------
*/ 
void CVideoPlayerAdapter::MvpuoOpenComplete(TInt aError)
    {
	if(aError==KErrNone)
		{
		// Prepare for the loading the file
		iPlayer->Prepare();
		}
	else 
		{
		// Stop the heart beat and close the player
		Stop();
		}
	}

/*
-----------------------------------------------------------------------------

	void CVideoPlayerAdapter::MvpuoPrepareComplete(TInt aError)

	Description:  Notification that video player is ready to begin playback. 
	Comments   :  This callback is generated in response to a call to Prepare
				  in CVideoPlayerUtility

    Return values: N/A

-----------------------------------------------------------------------------
*/  
void CVideoPlayerAdapter::MvpuoPrepareComplete(TInt aError)
    {
    iFileDetails->iDurationInSeconds = 0;

	if(aError == KErrNone || aError == KErrMMPartialPlayback)
		{
        TSize size( 0, 0 );
        iPlayer->VideoFrameSizeL(size);
        iFileDetails->iResolutionHeight = size.iHeight;
        iFileDetails->iResolutionWidth = size.iWidth;

		// Check if audio
        iFileDetails->iAudioTrack = iPlayer->AudioEnabledL();
            
        // Video track
        iFileDetails->iVideoTrack = iPlayer->VideoBitRateL();
            
        // Duration of the video clip
        iFileDetails->iDurationInSeconds = iPlayer->DurationL().Int64() / KMPOneSecond;           
		}

    if ( iCtrlrListener ) 
        {
        TRAPD(ignore,iCtrlrListener->InitControllerCompletedL(aError));
        }
    }

/*
-----------------------------------------------------------------------------

	void CVideoPlayerAdapter::MvpuoPlayComplete(TInt aError)

	Description:  This method defines required client behaviour when an attempt to 
				  play a video sample has completed, successfully or otherwise. 
	Comments   :  This method is not called if playback is explicitly stopped by 
				  calling Stop in CVideoPlayerUtility.

    Return values: N/A

-----------------------------------------------------------------------------
*/   
void CVideoPlayerAdapter::MvpuoPlayComplete(TInt aError)
    {
    if (iProgressUpdater->IsActive())
        {
        iProgressUpdater->Cancel();
        }
    iPlayPosition = 0;
    iCtrlrListener->PlayCompletedL(aError); // Pass the error forward 
    iState = EStopped;
	iPlayer->Close();
    }

/*
-----------------------------------------------------------------------------

	void CVideoPlayerAdapter::MvpuoFrameReady(CFbsBitmap& aFrame,TInt aError)

	Description: Defines required client behaviour when a request to capture a 
				 frame is made using CVideoPlayerUtility::GetFrame(). 
	Comments   :  

    Return values: N/A

-----------------------------------------------------------------------------
*/   
void CVideoPlayerAdapter::MvpuoFrameReady(CFbsBitmap& /*aFrame*/,TInt /*aError*/)
    {
    }

/*
-----------------------------------------------------------------------------

	void CVideoPlayerAdapter::Beat()

	Description: This method handles a regular heartbeat timer event. This type of  
				 event is one where the timer completes in synchronisation with the
				 system clock.	
	Comments   : 

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CVideoPlayerAdapter::Beat()
    {
    // Keep backlights on if clip has video
    if(iFileDetails->iVideoTrack)
        {
        User::ResetInactivityTime();
        }

	if(iState != EPlaying)
		return;

    TInt64 ret = iPlayPosition%2;
    
    if(ret.GetTInt())
        {
        iPlayPosition = iPlayPosition + 1;
        if(iCtrlrListener)
			{
			TRAPD(ignore,iCtrlrListener->PlaybackPositionChangedL(iPlayPosition,iFileDetails->iDurationInSeconds));
			}
		}
    else
        {
        Synchronize();
        }
    }

/*
-----------------------------------------------------------------------------

	void CVideoPlayerAdapter::Synchronize()

	Description: This method synchronises the heartbeat timer with system clock. 
	Comments   : This function handles a heartbeat timer event where the 
				 timer completes out of synchronisation with the system clock, 
				 (i.e. one or more heartbeats have been missed).

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CVideoPlayerAdapter::Synchronize()
    {
	if ( iState != EPlaying )
		return;
    
    TRAPD(ignore,
		// If live stream, fake progress
        iPlayPosition = iPlayer->PositionL().Int64() / KMPOneSecond;
                        
        if(iCtrlrListener)
			{
            iCtrlrListener->PlaybackPositionChangedL(iPlayPosition,iFileDetails->iDurationInSeconds);
            }
        );
    }

/*
-----------------------------------------------------------------------------

	CDocumentHandler& CVideoPlayerAdapter::DocumentHandlerL()

	Description: This method returns a document handler.
	Comments   : 

    Return values: Document handler

-----------------------------------------------------------------------------
*/
CDocumentHandler& CVideoPlayerAdapter::DocumentHandlerL()
	{
    if( !iDocHandler )
        {
        iDocHandler = CDocumentHandler::NewL( CEikonEnv::Static()->Process() );
        }

	return *iDocHandler;
	}

/*
-----------------------------------------------------------------------------

	void CVideoPlayerAdapter::MvpuoEvent(const TMMFEvent& aEvent)

	Description: Mvpuo Event comes in.
	Comments   : 

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CVideoPlayerAdapter::MvpuoEvent(const TMMFEvent& aEvent)
    {
    // Higher priority application has taken over the audio device --> Do pause.
    if (aEvent.iEventType == KMMFEventCategoryVideoPlayerGeneralError &&
        aEvent.iErrorCode == KErrHardwareNotAvailable)
        {
        TRAPD(ignore,iCtrlrListener->PlayCompletedL(KErrAudioLost));    
        }
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -