javauis/mmapi_akn/baseline/src/cmmavideourlplayer.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 
       
    19 //  INCLUDE FILES
       
    20 #include <mmf/server/mmfurl.h>
       
    21 #include <es_enum.h>
       
    22 #include <jdebug.h>
       
    23 
       
    24 #include "cmmavideourlplayer.h"
       
    25 #include "mmmadisplay.h"
       
    26 #include "cmmasourcestream.h"
       
    27 
       
    28 const TUid KUrlSourceUid = { KMmfUidUrlSource };
       
    29 _LIT(KMMALiveStreamMetaDataKeyword, "LiveStream");
       
    30 _LIT(KMMALiveStreamMetaDataValue, "1");
       
    31 
       
    32 CMMAVideoUrlPlayer* CMMAVideoUrlPlayer::NewLC(
       
    33     CMMAMMFResolver* aResolver,
       
    34     const TDesC& aUrl)
       
    35 {
       
    36     CMMAVideoUrlPlayer* self = new(ELeave) CMMAVideoUrlPlayer(
       
    37         aResolver);
       
    38     CleanupStack::PushL(self);
       
    39     self->ConstructL(aUrl);
       
    40     return self;
       
    41 }
       
    42 
       
    43 EXPORT_C CMMAVideoUrlPlayer::~CMMAVideoUrlPlayer()
       
    44 {
       
    45     iConnection.Close();
       
    46     iSocketServ.Close();
       
    47     delete iUrl;
       
    48     delete iPlayerDelegate;
       
    49 }
       
    50 
       
    51 EXPORT_C void CMMAVideoUrlPlayer::ConstructL(const TDesC& aUrl)
       
    52 {
       
    53     CMMAVideoPlayer::ConstructL();
       
    54     iUrl = aUrl.AllocL();
       
    55 
       
    56     User::LeaveIfError(iSocketServ.Connect());
       
    57     User::LeaveIfError(iConnection.Open(iSocketServ));
       
    58 }
       
    59 
       
    60 EXPORT_C CMMAVideoUrlPlayer::CMMAVideoUrlPlayer(
       
    61     CMMAMMFResolver* aResolver):
       
    62         CMMAVideoPlayer(aResolver)
       
    63 {
       
    64 }
       
    65 
       
    66 EXPORT_C void CMMAVideoUrlPlayer::DeallocateL()
       
    67 {
       
    68     __ASSERT_DEBUG(iPlayerDelegate != NULL, User::Invariant());
       
    69     delete iPlayerDelegate;
       
    70     iPlayerDelegate = NULL;
       
    71     CMMAMMFPlayerBase::DeallocateL();
       
    72 }
       
    73 
       
    74 EXPORT_C void CMMAVideoUrlPlayer::CloseL()
       
    75 {
       
    76     // Do not call the CMMAVideoPlayer CloseL because it deletes
       
    77     // file source stream.
       
    78     CMMAAudioPlayer::CloseL();
       
    79 }
       
    80 
       
    81 EXPORT_C void CMMAVideoUrlPlayer::StartL()
       
    82 {
       
    83     __ASSERT_DEBUG(iPlayerDelegate != NULL, User::Invariant());
       
    84     DEBUG("CMMAVideoUrlPlayer::StartL() +");
       
    85     iPlayerDelegate->StartL();
       
    86     DEBUG("CMMAVideoUrlPlayer::StartL() -");
       
    87 }
       
    88 
       
    89 EXPORT_C void CMMAVideoUrlPlayer::StopL(TBool aPostEvent)
       
    90 {
       
    91     DEBUG("MMA::CMMAVideoUrlPlayer::StopL ");
       
    92     __ASSERT_DEBUG(iPlayerDelegate != NULL, User::Invariant());
       
    93     iPlayerDelegate->StopL(aPostEvent);
       
    94 }
       
    95 
       
    96 EXPORT_C void CMMAVideoUrlPlayer::GetMediaTime(TInt64* aMediaTime)
       
    97 {
       
    98     DEBUG("MMA::CMMAVideoUrlPlayer::GetMediaTime +");
       
    99     if (iPlayerDelegate)
       
   100     {
       
   101         DEBUG("MMA::CMMAVideoUrlPlayer::GetMediaTime .iPlayerDelegate->GetMediaTime ");
       
   102         iPlayerDelegate->GetMediaTime(aMediaTime);
       
   103     }
       
   104     else
       
   105     {
       
   106         DEBUG("MMA::CMMAVideoUrlPlayer::GetMediaTime Not found ");
       
   107         *aMediaTime = KErrNotFound;
       
   108     }
       
   109     DEBUG("MMA::CMMAVideoUrlPlayer::GetMediaTime -");
       
   110 }
       
   111 
       
   112 EXPORT_C void CMMAVideoUrlPlayer::RealizeL()
       
   113 {
       
   114     CMMAPlayer::RealizeL(); // Changes state to ERealized
       
   115 }
       
   116 
       
   117 
       
   118 EXPORT_C void CMMAVideoUrlPlayer::PrefetchL()
       
   119 {
       
   120     const TUint KConnectionTreshold(1);
       
   121     TUint connectIap((TUint)KUseDefaultIap);
       
   122     TUint connectionCount(0);
       
   123     User::LeaveIfError(iConnection.EnumerateConnections(connectionCount));
       
   124     DEBUG_INT("MMA::CMMAVideoUrlPlayer::PrefetchL: connectionCount = %d", connectionCount);
       
   125     if (connectionCount == KConnectionTreshold)
       
   126     {
       
   127         // One active connection - find it and try using it
       
   128         FindActiveIap(connectionCount, connectIap);
       
   129     }
       
   130     // else No active connections try using the default one
       
   131     DEBUG_INT("MMA::CMMAVideoUrlPlayer::PrefetchL: connectIap = %d", connectIap);
       
   132 
       
   133     CMMFUrlParams* urlCfg = CMMFUrlParams::NewLC(*iUrl, (TInt)connectIap);
       
   134     CBufFlat* urlCfgBuffer = urlCfg->ExternalizeToCBufFlatLC();
       
   135 
       
   136     // Store current thread priority to give
       
   137     // more CPU time to Java threads i.e. this
       
   138     // is work around for not locking the Java
       
   139     // thread while RTSP connection is being
       
   140     // opened
       
   141     iOrigPriority = RThread().Priority();
       
   142 
       
   143     // player priority settings
       
   144     TMMFPrioritySettings prioritySettings;
       
   145     prioritySettings.iPriority = EMdaPriorityMax;
       
   146     prioritySettings.iPref = EMdaPriorityPreferenceTimeAndQuality;
       
   147     prioritySettings.iState = EMMFStatePlaying;
       
   148 
       
   149     User::LeaveIfError(DoOpen(KUrlSourceUid,
       
   150                               urlCfgBuffer->Ptr(0),
       
   151                               KUidMmfAudioOutput,
       
   152                               KNullDesC8,
       
   153                               prioritySettings));
       
   154 
       
   155     User::LeaveIfError(iController.Prime());
       
   156 
       
   157     CleanupStack::PopAndDestroy(urlCfgBuffer);
       
   158     CleanupStack::PopAndDestroy(urlCfg);
       
   159 
       
   160     // Prefetch will be completed in HandleEvent method.
       
   161     // Next event KMMFEventCategoryVideoOpenComplete will be handled in
       
   162     // CMMAVideoPlayer.
       
   163 }
       
   164 
       
   165 void CMMAVideoUrlPlayer::FindActiveIap(const TUint aConnectionCount, TUint& aActiveIap)
       
   166 {
       
   167     DEBUG_INT("MMA:CMMAVideoUrlPlayer::FindActiveIap: aConnectionCount = %d +", aConnectionCount);
       
   168 
       
   169     TPckgBuf<TConnectionInfo> connectionInfo;
       
   170     for (TUint i = 1; i <= aConnectionCount; ++i)
       
   171     {
       
   172         if (iConnection.GetConnectionInfo(i, connectionInfo) == KErrNone)
       
   173         {
       
   174             aActiveIap = connectionInfo().iIapId;
       
   175             break;
       
   176         }
       
   177     }
       
   178 
       
   179     DEBUG_INT("MMA:CMMAVideoUrlPlayer::FindActiveIap: aActiveIap = %d -", aActiveIap);
       
   180 }
       
   181 
       
   182 TBool CMMAVideoUrlPlayer::IsLiveStreamL()
       
   183 {
       
   184     DEBUG("CMMAVideoUrlPlayer::IsLiveStream: Checking if this is a live stream..");
       
   185 
       
   186     CMMFMetaDataEntry* currEntry = NULL;
       
   187 
       
   188     TInt nEntries = 0;
       
   189     User::LeaveIfError(iController.GetNumberOfMetaDataEntries(nEntries));
       
   190 
       
   191     for (TInt i = 0; i < nEntries; ++i)
       
   192     {
       
   193         currEntry = iController.GetMetaDataEntryL(i);
       
   194 
       
   195         if ((0 == currEntry->Name().Compare(KMMALiveStreamMetaDataKeyword)) &&
       
   196                 (0 == currEntry->Value().Compare(KMMALiveStreamMetaDataValue)))
       
   197         {
       
   198             DEBUG("CMMAVideoUrlPlayer::IsLiveStream: Stream is a live stream");
       
   199             delete currEntry;
       
   200             return ETrue;
       
   201         }
       
   202 
       
   203         delete currEntry;
       
   204     }
       
   205 
       
   206     DEBUG("CMMAVideoUrlPlayer::IsLiveStream: Not a live stream");
       
   207     return EFalse;
       
   208 }
       
   209 
       
   210 EXPORT_C void CMMAVideoUrlPlayer::HandleEvent(const TMMFEvent& aEvent)
       
   211 {
       
   212     DEBUG_INT("MMA:CMMAVideoUrlPlayer::HandleEvent 0x%X", aEvent.iEventType.iUid);
       
   213     DEBUG_INT("MMA:CMMAVideoUrlPlayer::HandleEvent error code: %d", aEvent.iErrorCode);
       
   214     RThread currentThread;
       
   215 
       
   216     if (iPlayerDelegate != NULL)
       
   217     {
       
   218         DEBUG("MMA::CMMAVideoUrlPlayer::HandleEvent: iPlayerDelegate != NULL");
       
   219         iPlayerDelegate->HandleEvent(aEvent);
       
   220     }
       
   221     else
       
   222     {
       
   223         DEBUG("MMA::CMMAVideoUrlPlayer::HandleEvent: else");
       
   224 
       
   225         __ASSERT_DEBUG(iPlayerDelegate == NULL, User::Invariant());
       
   226         TInt err = aEvent.iErrorCode;
       
   227 
       
   228         DEBUG_INT("MMA:CMMAVideoUrlPlayer::HandleEvent: currentThread.Priority() I = %d", currentThread.Priority());
       
   229 
       
   230         // Lower thread priority to give more CPU time to Java-threads
       
   231         // i.e. current thread has one increment higher priority than
       
   232         // EPriorityNormal
       
   233         if (aEvent.iEventType == KMMFEventCategoryVideoOpenComplete &&
       
   234                 err == KErrNone)
       
   235         {
       
   236             currentThread.SetPriority(EPriorityNormal);
       
   237         }
       
   238 
       
   239         DEBUG_INT("MMA:CMMAVideoUrlPlayer::HandleEvent: currentThread.Priority() II = %d", currentThread.Priority());
       
   240 
       
   241         if (aEvent.iEventType == KMMFEventCategoryVideoPrepareComplete)
       
   242         {
       
   243             // Restore original thread priority
       
   244             currentThread.SetPriority(iOrigPriority);
       
   245             DEBUG_INT("MMA:CMMAVideoUrlPlayer::HandleEvent: currentThread.Priority() III = %d", currentThread.Priority());
       
   246 
       
   247             // NotCompleteVideoError is not considered as an error condition, instead it indicates
       
   248             // that some elements of the media cannot be played (e.g. video OR audio)
       
   249             if (err != KErrNone && err != KNotCompleteVideoError)
       
   250             {
       
   251                 CompletePrefetch(err);
       
   252                 return;
       
   253             }
       
   254 
       
   255             // Find out of this is a live stream
       
   256             TBool liveStream = EFalse;
       
   257             TRAP(err, liveStream = IsLiveStreamL());
       
   258 
       
   259             if (err != KErrNone)
       
   260             {
       
   261                 CompletePrefetch(err);
       
   262                 return;
       
   263             }
       
   264 
       
   265             if (liveStream)
       
   266             {
       
   267                 TRAP(err, iPlayerDelegate =
       
   268                          CMMAVideoUrlPlayerLiveStreamDelegate::NewL(*this));
       
   269             }
       
   270             else
       
   271             {
       
   272                 iPlayerDelegate = new CMMAVideoUrlPlayerClipStreamDelegate(*this);
       
   273                 err = iPlayerDelegate ? KErrNone : KErrNoMemory;
       
   274             }
       
   275 
       
   276             if (err != KErrNone)
       
   277             {
       
   278                 // could not create delegate
       
   279                 CompletePrefetch(err);
       
   280                 return;
       
   281             }
       
   282 
       
   283             iPlayerDelegate->HandleEvent(aEvent);
       
   284         }
       
   285         else
       
   286         {
       
   287             // All other events.
       
   288             DEBUG("MMA::CMMAVideoUrlPlayer::HandleEvent: Calling CMMAVideoPlayer::HandleEvent()");
       
   289             CMMAVideoPlayer::HandleEvent(aEvent);
       
   290         }
       
   291     }
       
   292 }
       
   293 
       
   294 void CMMAVideoUrlPlayer::HandleEventToParent(const TMMFEvent& aEvent)
       
   295 {
       
   296     CMMAVideoPlayer::HandleEvent(aEvent);
       
   297 }
       
   298 
       
   299 // ----------------------------------------------------------------------------
       
   300 //
       
   301 // CMMAVideoUrlPlayerClipStreamDelegate
       
   302 //
       
   303 // ----------------------------------------------------------------------------
       
   304 
       
   305 CMMAVideoUrlPlayer::CMMAVideoUrlPlayerDelegate::
       
   306 CMMAVideoUrlPlayerDelegate(CMMAVideoUrlPlayer& aPlayer) :
       
   307         iPlayer(aPlayer)
       
   308 {
       
   309     // Nothing to be done.
       
   310 }
       
   311 
       
   312 CMMAVideoUrlPlayer::CMMAVideoUrlPlayerDelegate::~CMMAVideoUrlPlayerDelegate()
       
   313 {
       
   314     // Nothing to be done.
       
   315 }
       
   316 
       
   317 //  END OF FILE