javauis/mmapi_akn/baseline/src.nga/cmmavideoplayer.cpp
branchRCL_3
changeset 19 04becd199f91
child 46 4376525cdefb
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2002-2007 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:  This class is used for playing video.
       
    15 *
       
    16 */
       
    17 
       
    18 //  INCLUDE FILES
       
    19 #include <mmf/server/mmffile.h>
       
    20 #include <jdebug.h>
       
    21 
       
    22 #include "cmmavideoplayer.h"
       
    23 #include "mmmadisplay.h"
       
    24 #include "cmmaeventsource.h"
       
    25 #include "cmmasurfacewindow.h"
       
    26 
       
    27 // CONSTANTS
       
    28 _LIT(KVideoControlName, "VideoControl");
       
    29 
       
    30 CMMAVideoPlayer* CMMAVideoPlayer::NewLC(
       
    31     CMMAMMFResolver* aResolver)
       
    32 {
       
    33     CMMAVideoPlayer* self =
       
    34         new(ELeave) CMMAVideoPlayer(aResolver);
       
    35     CleanupStack::PushL(self);
       
    36     self->ConstructL();
       
    37     return self;
       
    38 }
       
    39 
       
    40 CMMAVideoPlayer::~CMMAVideoPlayer()
       
    41 {
       
    42     DEBUG("MMA::CMMAVideoPlayer::~CMMAVideoPlayer");
       
    43 
       
    44     // Window is not able to send any
       
    45     // callback requests to UI from now.
       
    46     if (iSurfaceWindow)
       
    47     {
       
    48         iSurfaceWindow->SetDisplay(NULL);
       
    49     }
       
    50 
       
    51     if (iDisplay && iDisplay->HasContainer())
       
    52     {
       
    53         // Window will delete itself
       
    54         // after all pending events are processed
       
    55         // (lazy delete)
       
    56         iDisplay->UIGetCallback(
       
    57             *iSurfaceWindow, CMMASurfaceWindow::EDestroyWindow);
       
    58     }
       
    59     else
       
    60     {
       
    61         delete iSurfaceWindow;
       
    62     }
       
    63 
       
    64     if (iDisplay)
       
    65     {
       
    66         TRAPD(err, iDisplay->SetWindowL(NULL));
       
    67         if (err != KErrNone)
       
    68         {
       
    69             __ASSERT_DEBUG(EFalse, User::Invariant());
       
    70         }
       
    71     }
       
    72 
       
    73     delete iEmptySnapshotImage;
       
    74     delete iActiveSchedulerWait;
       
    75 }
       
    76 
       
    77 CMMAVideoPlayer::CMMAVideoPlayer(
       
    78     CMMAMMFResolver* aResolver):
       
    79         CMMAAudioPlayer(aResolver),
       
    80         iVideoControllerCustomCommands(iController),
       
    81         iVideoPlayControllerCustomCommands(iController),
       
    82         iVideoPlaySurfaceSupportCustomCommands(iController)
       
    83 {
       
    84     iMMASurface.iPrevSurfaceAvailable = EFalse;
       
    85 }
       
    86 
       
    87 void CMMAVideoPlayer::ConstructL()
       
    88 {
       
    89     CMMAAudioPlayer::ConstructL();
       
    90     iActiveSchedulerWait = new(ELeave)CActiveSchedulerWait;
       
    91 }
       
    92 
       
    93 EXPORT_C void CMMAVideoPlayer::SetPlayerListenerObjectL(jobject aListenerObject,
       
    94         JNIEnv* aJni,
       
    95         MMMAEventPoster* aEventPoster)
       
    96 {
       
    97     CMMAPlayer::SetPlayerListenerObjectL(aListenerObject,
       
    98                                          aJni,
       
    99                                          aEventPoster);
       
   100 
       
   101     // this method must be called only ones
       
   102     __ASSERT_DEBUG(!iSurfaceWindow, User::Invariant());
       
   103 
       
   104     // create window for videoplayer
       
   105     // event poster is always CMMAEventSource type.
       
   106 
       
   107     iSurfaceWindow = CMMASurfaceWindow::NewL(
       
   108                          static_cast< CMMAEventSource* >(iEventPoster),
       
   109                          this);
       
   110 }
       
   111 
       
   112 EXPORT_C void CMMAVideoPlayer::SetDisplayL(MMMADisplay* aDisplay)
       
   113 {
       
   114     // now it is ready to draw
       
   115     iDisplay = aDisplay;
       
   116 
       
   117     iSurfaceWindow->SetDisplay(aDisplay);
       
   118 
       
   119     iDisplay->SetWindowL(iSurfaceWindow);
       
   120 
       
   121     // if state < prefeteched then we dont know actual source size yet
       
   122     // and it will be set after prefetch
       
   123     if (iState >= EPrefetched)
       
   124     {
       
   125         SourceSizeChanged();
       
   126     }
       
   127 }
       
   128 void CMMAVideoPlayer::RealizeL()
       
   129 {
       
   130     DEBUG("CMMAVideoPlayer::RealizeL");
       
   131     // DataSource must have at least 1 stream or
       
   132     // we must have file to play
       
   133     if ((iSourceStreams.Count() == 0) && !iFileName)
       
   134     {
       
   135         User::Leave(KErrNotEnoughStreams);
       
   136     }
       
   137 
       
   138     // If file locator is used, then file is prefetched
       
   139     // in realized state so that FramePositioningControl
       
   140     // can acquire frame count in REALIZED state
       
   141     if (iFileName)
       
   142     {
       
   143         PrefetchFileL();
       
   144         if (!iActiveSchedulerWait->IsStarted())
       
   145         {
       
   146             iActiveSchedulerWait->Start();
       
   147         }
       
   148         // If the player has not changed state during wait,
       
   149         // Then there is an error condition.
       
   150         if (iState != ERealized)
       
   151         {
       
   152             User::Leave(KErrNotSupported);
       
   153         }
       
   154     }
       
   155     else
       
   156     {
       
   157         CMMAPlayer::RealizeL();
       
   158     }
       
   159 }
       
   160 
       
   161 void CMMAVideoPlayer::PrefetchL()
       
   162 {
       
   163     DEBUG("CMMAVideoPlayer::PrefetchL");
       
   164     if (iFileName)
       
   165     {
       
   166         // File has already been prefetched when realizing
       
   167 
       
   168         // If initDisplayMode was called before prefetch,
       
   169         // then the display must notified about source size.
       
   170         if (iDisplay)
       
   171         {
       
   172             SourceSizeChanged();
       
   173         }
       
   174 
       
   175         ChangeState(EPrefetched);
       
   176         PostActionCompleted(KErrNone);
       
   177     }
       
   178     else
       
   179     {
       
   180         // Using TDes -- load the whole video
       
   181         iSourceStreams[ 0 ]->ReadAllL();
       
   182     }
       
   183     // CMMASourceStream will notify with ReadCompleted
       
   184 }
       
   185 
       
   186 EXPORT_C void CMMAVideoPlayer::ReadCompletedL(TInt aStatus, const TDesC8& aData)
       
   187 {
       
   188     DEBUG_INT("CMMAVideoPlayer::ReadCompletedL: status = %d", aStatus);
       
   189     if (aStatus < KErrNone)
       
   190     {
       
   191         PostActionCompleted(aStatus);
       
   192     }
       
   193     else
       
   194     {
       
   195         TRAPD(err, PrefetchDataL(aData));
       
   196         if (err != KErrNone)
       
   197         {
       
   198             PostActionCompleted(err);
       
   199         }
       
   200         // action completed event will be delivered from handleEvent
       
   201     }
       
   202 }
       
   203 
       
   204 void CMMAVideoPlayer::HandleEvent(const TMMFEvent& aEvent)
       
   205 {
       
   206     DEBUG_INT("MMA:CMMAVideoPlayer::HandleEvent %d", aEvent.iEventType.iUid);
       
   207 
       
   208     // event KMMFEventCategoryPlaybackComplete is handled by both Video
       
   209     // and Audio players. first it should be handled by Video player
       
   210     if (aEvent.iEventType == KMMFEventCategoryPlaybackComplete)
       
   211     {
       
   212         iSurfaceWindow->RemoveSurface();
       
   213     }
       
   214 
       
   215     // KNotCompleteVideoError can be notified when video is not complete
       
   216     // ( missing sound ) but still can be played. Because
       
   217     // CMMAAudioPlayer::HandleEvent fails with all negative error codes,
       
   218     // do not call it with KNotCompleteVideoError error when preparing.
       
   219     if ((aEvent.iErrorCode != KNotCompleteVideoError) ||
       
   220             (aEvent.iEventType != KMMFEventCategoryVideoPrepareComplete))
       
   221     {
       
   222         CMMAAudioPlayer::HandleEvent(aEvent);
       
   223     }
       
   224 
       
   225     if (aEvent.iEventType == KMMFEventCategoryVideoSurfaceCreated)
       
   226     {
       
   227         if (aEvent.iErrorCode == KErrNone)
       
   228         {
       
   229             TSurfaceId surfaceId;
       
   230             TRect cropRect;
       
   231             TVideoAspectRatio pixelAspectRatio;
       
   232 
       
   233             iVideoPlaySurfaceSupportCustomCommands.GetSurfaceParameters(surfaceId,
       
   234                     cropRect,
       
   235                     pixelAspectRatio);
       
   236 
       
   237             if (iMMASurface.iPrevSurfaceAvailable)
       
   238             {
       
   239                 // free Surface
       
   240                 TInt error = iVideoPlaySurfaceSupportCustomCommands.SurfaceRemoved(iMMASurface.iPrevSurfaceId);
       
   241                 if (KErrNone != error)
       
   242                 {
       
   243                     DEBUG_INT("CMMAVideoPlayer::HandleEvent:SurfaceRemoved error, %d", aEvent.iErrorCode);
       
   244                 }
       
   245             }
       
   246             iMMASurface.iPrevSurfaceId = surfaceId;
       
   247             iMMASurface.iPrevSurfaceAvailable = ETrue;
       
   248 
       
   249             iSurfaceWindow->SetSurfaceParameters(surfaceId,
       
   250                                                  cropRect,
       
   251                                                  pixelAspectRatio);
       
   252 
       
   253             DEBUG("CMMAVideoPlayer::HandleEvent: KMMFEventCategoryVideoSurfaceCreated, surface parameters set");
       
   254         }
       
   255         else
       
   256         {
       
   257             DEBUG_INT("CMMAVideoPlayer::HandleEvent: error getting surface parameters, %d", aEvent.iErrorCode);
       
   258         }
       
   259     }
       
   260     else if (aEvent.iEventType == KMMFEventCategoryVideoSurfaceParametersChanged)
       
   261     {
       
   262         if (aEvent.iErrorCode == KErrNone)
       
   263         {
       
   264             TSurfaceId surfaceId;
       
   265             TRect cropRect;
       
   266             TVideoAspectRatio pixelAspectRatio;
       
   267 
       
   268             iVideoPlaySurfaceSupportCustomCommands.GetSurfaceParameters(surfaceId,
       
   269                     cropRect,
       
   270                     pixelAspectRatio);
       
   271 
       
   272             if (iMMASurface.iPrevSurfaceAvailable)
       
   273             {
       
   274                 // free Surface
       
   275                 TInt error = iVideoPlaySurfaceSupportCustomCommands.SurfaceRemoved(iMMASurface.iPrevSurfaceId);
       
   276                 if (KErrNone != error)
       
   277                 {
       
   278                     DEBUG_INT("CMMAVideoPlayer::HandleEvent:SurfaceRemoved error, %d", aEvent.iErrorCode);
       
   279                 }
       
   280             }
       
   281             iMMASurface.iPrevSurfaceAvailable = ETrue;
       
   282             iMMASurface.iPrevSurfaceId = surfaceId;
       
   283 
       
   284             iSurfaceWindow->SetChangedSurfaceParameters(surfaceId,
       
   285                     cropRect,
       
   286                     pixelAspectRatio);
       
   287 
       
   288             DEBUG("CMMAVideoPlayer::HandleEvent: KMMFEventCategoryVideoSurfaceParametersChanged");
       
   289         }
       
   290         else
       
   291         {
       
   292             DEBUG_INT("CMMAVideoPlayer::HandleEvent: surface parameters changed error, %d", aEvent.iErrorCode);
       
   293         }
       
   294     }
       
   295     else if (aEvent.iEventType == KMMFEventCategoryVideoRemoveSurface)
       
   296     {
       
   297         if (aEvent.iErrorCode == KErrNone)
       
   298         {
       
   299             if (iMMASurface.iPrevSurfaceAvailable)
       
   300             {
       
   301                 // free Surface
       
   302                 TInt error = iVideoPlaySurfaceSupportCustomCommands.SurfaceRemoved(iMMASurface.iPrevSurfaceId);
       
   303                 if (KErrNone != error)
       
   304                 {
       
   305                     DEBUG_INT("CMMAVideoPlayer::HandleEvent:SurfaceRemoved error, %d", aEvent.iErrorCode);
       
   306                 }
       
   307                 iMMASurface.iPrevSurfaceAvailable = EFalse;
       
   308             }
       
   309         }
       
   310         else
       
   311         {
       
   312             DEBUG_INT("CMMAVideoPlayer::HandleEvent:KMMFEventCategoryVideoRemoveSurface error, %d", aEvent.iErrorCode);
       
   313         }
       
   314     }
       
   315     // video opened, preparing
       
   316     else if (aEvent.iEventType == KMMFEventCategoryVideoOpenComplete)
       
   317     {
       
   318         if (aEvent.iErrorCode == KErrNone)
       
   319         {
       
   320             TInt error = iVideoPlaySurfaceSupportCustomCommands.UseSurfaces();
       
   321             DEBUG_INT("MMA::CMMAVideoPlayer::HandleEvent::After UseSurfaces(), error = %d", error);
       
   322             TInt prepareError(iVideoPlayControllerCustomCommands.Prepare());
       
   323             if (prepareError != KErrNone)
       
   324             {
       
   325                 // opening failed, notifying java
       
   326                 PostActionCompleted(prepareError);
       
   327             }
       
   328         }
       
   329         else
       
   330         {
       
   331             // opening failed, notifying java
       
   332             PostActionCompleted(aEvent.iErrorCode);
       
   333         }
       
   334     }
       
   335     // final state of prefetch ( prepare completed )
       
   336     else if (aEvent.iEventType == KMMFEventCategoryVideoPrepareComplete)
       
   337     {
       
   338         // This callback must be handled differently depending on whether
       
   339         // player is created for a file locator or for a stream. When file
       
   340         // locator is used, this callback is made in realized state. For
       
   341         // stream it is made in prefetched state.
       
   342         PrepareDisplay();
       
   343         if (iFileName)
       
   344         {
       
   345             DEBUG("CMMAVideoPlayer::HandleEvent: Using filename, change state to REALIZED");
       
   346 
       
   347             // If there is an error condition, then the player state is not
       
   348             // changed, which indicates the error condition to StartL when
       
   349             // the ActiveSchedulerWait returns. KNotCompleteVideoError is not
       
   350             // considered as an error condition, instead it indicates that some
       
   351             // elements of the media cannot be played (e.g. video OR audio)
       
   352             if (aEvent.iErrorCode == KErrNone ||
       
   353                     aEvent.iErrorCode == KNotCompleteVideoError)
       
   354             {
       
   355                 ChangeState(ERealized);
       
   356             }
       
   357             __ASSERT_DEBUG(iActiveSchedulerWait->IsStarted(), User::Invariant());
       
   358             iActiveSchedulerWait->AsyncStop();
       
   359         }
       
   360         else
       
   361         {
       
   362             DEBUG("CMMAVideoPlayer::HandleEvent: Not using filename, change state to PREFETCHED");
       
   363             CompletePrefetch(aEvent.iErrorCode);
       
   364         }
       
   365     }
       
   366     else            // in case of any other event
       
   367     {
       
   368         if (aEvent.iErrorCode != KErrNone)
       
   369         {
       
   370             PostActionCompleted(aEvent.iErrorCode);   //some other error
       
   371         }
       
   372     }
       
   373 }
       
   374 
       
   375 void CMMAVideoPlayer::CompletePrefetch(TInt aError)
       
   376 {
       
   377     DEBUG_INT("CMMAVideoPlayer::CompletePrefetch + error = %d",aError);
       
   378     // Post KNotCompleteVideoError as KErrNone to the Java side, because
       
   379     // video can be played.
       
   380     if (aError == KNotCompleteVideoError)
       
   381     {
       
   382         DEBUG("CMMAVideoPlayer::CompletePrefetch  KNotCompleteVideoError ");
       
   383         // release java
       
   384         PostActionCompleted(KErrNone);
       
   385     }
       
   386     else
       
   387     {
       
   388         DEBUG("CMMAVideoPlayer::CompletePrefetch  CompleteVideoError ");
       
   389         // release java
       
   390         PostActionCompleted(aError);
       
   391     }
       
   392 
       
   393     if (aError == KErrNone || aError == KNotCompleteVideoError)
       
   394     {
       
   395         ChangeState(EPrefetched);
       
   396     }
       
   397     DEBUG("CMMAVideoPlayer::CompletePrefetch - ");
       
   398 }
       
   399 
       
   400 void CMMAVideoPlayer::PrepareDisplay()
       
   401 {
       
   402     DEBUG("CMMAVideoPlayer::PrepareDisplay +");
       
   403     // construction should have leaved if iSurfaceWindow does not exist
       
   404     __ASSERT_DEBUG(iSurfaceWindow,
       
   405                    User::Panic(_L("CMMVideoPlayer::iSurfaceWindow is null"),
       
   406                                KErrArgument));
       
   407 
       
   408     //First place where we are certain that source size can be fetched
       
   409     TSize sourceSize;
       
   410 
       
   411     TInt err = iVideoControllerCustomCommands.GetVideoFrameSize(sourceSize);
       
   412 
       
   413     DEBUG_INT("MID::CMMAVideoPlayer::PrepareDisplay: GetVideoFrameSize err = %d", err);
       
   414 
       
   415     // Still we did not get the size of video
       
   416     if ((err != KErrNone) ||
       
   417             (sourceSize.iWidth <= 0) ||
       
   418             (sourceSize.iHeight <= 0))
       
   419     {
       
   420         DEBUG("MID::CMMAVideoPlayer::PrepareDisplay: No sourcesize found, using SurfaceWindow size");
       
   421         // setting size to window size (client rect)
       
   422         sourceSize = iSurfaceWindow->WindowSize();
       
   423     }
       
   424 
       
   425     // If 1x1 was got (the default size of form), it must be replaced
       
   426     // with a valid size as controller will not accept 1x1.
       
   427     if ((sourceSize.iWidth < KMMAVideoMinDimension) ||
       
   428             (sourceSize.iHeight < KMMAVideoMinDimension))
       
   429     {
       
   430         DEBUG("MID::CMMAVideoPlayer::PrepareDisplay: Unacceptable source size, using failsafe");
       
   431         // This is a special case and ought to be used only in
       
   432         // the rare case that real size is not got from stream.
       
   433         sourceSize = TSize(KMMAVideoMinDimension, KMMAVideoMinDimension);
       
   434     }
       
   435 
       
   436     iSourceSize = sourceSize;
       
   437 
       
   438     // If init has been already done
       
   439     if (iDisplay)
       
   440     {
       
   441         DEBUG("MID::CMMAVideoPlayer::PrepareDisplay: display exists, changing source size");
       
   442         SourceSizeChanged();
       
   443     }
       
   444 
       
   445     // Setting (in)visible if something has changed the DSA state
       
   446     // (e.g. prepare). If initDisplayMode is not called, this will always
       
   447     // set visibility to false.
       
   448     iSurfaceWindow->SetVisible(iSurfaceWindow->IsVisible(), EFalse);
       
   449     DEBUG("CMMAVideoPlayer::PrepareDisplay -");
       
   450 }
       
   451 
       
   452 EXPORT_C const TDesC& CMMAVideoPlayer::Type()
       
   453 {
       
   454     return KMMAVideoPlayer;
       
   455 }
       
   456 
       
   457 EXPORT_C TSize CMMAVideoPlayer::SourceSize()
       
   458 {
       
   459     return iSourceSize;
       
   460 }
       
   461 
       
   462 EXPORT_C MMMASnapshot::TEncoding CMMAVideoPlayer::TakeSnapshotL(TRequestStatus* aStatus,
       
   463         const TSize& /*aSize*/,
       
   464         const CMMAImageSettings& /*aSettings*/)
       
   465 {
       
   466     if (iEmptySnapshotImage)
       
   467     {
       
   468         // Ealier snapshot was not got with SnapshotBitmap method.
       
   469         User::Leave(KErrInUse);
       
   470     }
       
   471     // frame can't be got from video player, but TCK requires that it should
       
   472     // be available. So returning empty image
       
   473     iEmptySnapshotImage = new(ELeave) CFbsBitmap();
       
   474     User::LeaveIfError(iEmptySnapshotImage->Create(TSize(1, 1),
       
   475                        EColor4K));
       
   476 
       
   477 
       
   478     User::RequestComplete(aStatus, KErrNone);
       
   479 
       
   480     // Return raw bitmap encoding and thus SnapshotEncoded() should not
       
   481     // get called later on.
       
   482     return EBitmap;
       
   483 }
       
   484 
       
   485 EXPORT_C CFbsBitmap* CMMAVideoPlayer::SnapshotBitmap()
       
   486 {
       
   487     // snapshot is not supported, returning empty image
       
   488     CFbsBitmap* image = iEmptySnapshotImage;
       
   489 
       
   490     // ownership is transferred to caller
       
   491     iEmptySnapshotImage = NULL;
       
   492     return image;
       
   493 }
       
   494 
       
   495 EXPORT_C HBufC8* CMMAVideoPlayer::SnapshotEncoded()
       
   496 {
       
   497     // This method should never be called.
       
   498     // Asserted in debug build to be sure.
       
   499     __ASSERT_DEBUG(EFalse, User::Invariant());
       
   500 
       
   501     return NULL;
       
   502 }
       
   503 
       
   504 EXPORT_C void CMMAVideoPlayer::NotifyWithStringEvent(
       
   505     CMMAPlayerEvent::TEventType aEventType,
       
   506     const TDesC& aStringEventData)
       
   507 {
       
   508     PostStringEvent(aEventType, aStringEventData);
       
   509 }
       
   510 
       
   511 EXPORT_C MMMASnapshot* CMMAVideoPlayer::SnapshoterL()
       
   512 {
       
   513     return this;
       
   514 }
       
   515 
       
   516 void CMMAVideoPlayer::SourceSizeChanged()
       
   517 {
       
   518     iDisplay->SourceSizeChanged(iSourceSize);
       
   519     NotifyWithStringEvent(CMMAPlayerEvent::ESizeChanged, KVideoControlName);
       
   520 }
       
   521 
       
   522 //  END OF FILE