tsrc/musenginestub/src/musengtwowaysession.cpp
branchRCL_3
changeset 33 bc78a40cd63c
parent 32 73a1feb507fb
child 35 6c57ef9392d2
equal deleted inserted replaced
32:73a1feb507fb 33:bc78a40cd63c
     1 /*
       
     2 * Copyright (c) 2005 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 // USER
       
    20 #include "musengtwowaysession.h"
       
    21 #include "musengsessionobserver.h"
       
    22 #include "musenglivesessionobserver.h"
       
    23 #include "musunittesting.h"
       
    24 #include "musengmceutils.h"
       
    25 #include "musenglogger.h"
       
    26 #include "musengtwowaysessionobserver.h"
       
    27 
       
    28 // SYSTEM
       
    29 #include <mcemanager.h>
       
    30 #include <mcecamerasource.h>
       
    31 #include <mcevideostream.h>
       
    32 #include <mcertpsink.h>
       
    33 #include <mcedisplaysink.h>
       
    34 #include <mcesession.h>
       
    35 #include <mcertpsource.h>
       
    36 
       
    37 const TInt KMusEngJitterBufferLength = 51; //Must be bigger than treshold
       
    38 // Using following value increases treshold buffer to 1 second from 
       
    39 // default 100 ms
       
    40 const TInt KMusEngJitterBufferTreshold = 50;
       
    41 
       
    42 const TUint32 KMusEngTwoWayReceivingActivityTimeout = 3000; // 3 seconds
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 EXPORT_C CMusEngTwoWaySession* CMusEngTwoWaySession::NewL(
       
    49                         const TRect& aRemoteRect,
       
    50                         const TRect& aLocalRect,
       
    51                         MMusEngSessionObserver& aSessionObserver,
       
    52                         MMusEngOutSessionObserver& aOutSessionObserver,
       
    53                         MMusEngLiveSessionObserver& aLiveSessionObserver,
       
    54                         MMusEngTwoWaySessionObserver& aTwoWayObserver )
       
    55     {
       
    56     CMusEngTwoWaySession* self = new( ELeave ) CMusEngTwoWaySession( 
       
    57                                                     aSessionObserver,
       
    58                                                     aOutSessionObserver,
       
    59                                                     aLiveSessionObserver, 
       
    60                                                     aTwoWayObserver,
       
    61                                                     aRemoteRect,
       
    62                                                     aLocalRect );
       
    63     CleanupStack::PushL( self );
       
    64     self->ConstructL();
       
    65     CleanupStack::Pop( self );
       
    66     return self;
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 CMusEngTwoWaySession::~CMusEngTwoWaySession()
       
    74     {
       
    75     MUS_LOG( "mus: [ENGINE]  -> CMusEngTwoWaySession::~CMusEngTwoWaySession()" )
       
    76     MUS_LOG( "mus: [ENGINE]  <- CMusEngTwoWaySession::~CMusEngTwoWaySession()" )
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // 
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 void CMusEngTwoWaySession::EnableDisplayL( TBool aEnable )
       
    84     {
       
    85     MUS_LOG1( "mus: [ENGINE]     -> CMusEngTwoWaySession::EnableDisplayL() %d", 
       
    86               aEnable )
       
    87 
       
    88     __ASSERT_ALWAYS( iSession, User::Leave( KErrNotReady ) );
       
    89 
       
    90     CMceDisplaySink* display = MusEngMceUtils::GetReceivingDisplay( *iSession );
       
    91     if ( display )
       
    92         {
       
    93         MusEngMceUtils::DoEnableDisplayL( *display, aEnable );
       
    94         }     
       
    95     CMceDisplaySink* vfDisplay = MusEngMceUtils::GetDisplayL( *iSession, ETrue );
       
    96     MusEngMceUtils::DoEnableDisplayL( *vfDisplay, aEnable );     
       
    97         
       
    98     MUS_LOG( "mus: [ENGINE]  <- CMusEngTwoWaySession::EnableDisplayL()")
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 // -----------------------------------------------------------------------------
       
   104 //        
       
   105 void CMusEngTwoWaySession::SetOrientationL( TDisplayOrientation aOrientation )
       
   106     {
       
   107     MUS_LOG1( "mus: [ENGINE]  -> CMusEngTwoWaySession::SetOrientationL() %d", 
       
   108               aOrientation )
       
   109               
       
   110     __ASSERT_ALWAYS( iSession, User::Leave( KErrNotReady ) );
       
   111     
       
   112     CMceDisplaySink* display = MusEngMceUtils::GetReceivingDisplay( *iSession );            
       
   113     CMceDisplaySink* vfDisplay = MusEngMceUtils::GetDisplayL( *iSession, ETrue );
       
   114 
       
   115     CMceDisplaySink::TRotation rotation = ( aOrientation == EPortrait ) ? 
       
   116          CMceDisplaySink::ENone : CMceDisplaySink::EClockwise90Degree;
       
   117      
       
   118     vfDisplay->SetRotationL( rotation );
       
   119     if ( display )
       
   120         {
       
   121         display->SetRotationL( rotation );
       
   122         }
       
   123 
       
   124     MUS_LOG( "mus: [ENGINE]  <- CMusEngTwoWaySession::SetOrientationL()" )
       
   125     }
       
   126 
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 // -----------------------------------------------------------------------------
       
   130 //
       
   131 void CMusEngTwoWaySession::RecordL( TBool aRecord )
       
   132     {
       
   133     MUS_LOG1( "mus: [ENGINE]  -> CMusEngTwoWaySession::RecordL( %d )", aRecord )
       
   134 
       
   135     User::Leave( KErrNotSupported );
       
   136 
       
   137     MUS_LOG1( "mus: [ENGINE]  <- CMusEngTwoWaySession::RecordL( %d )", aRecord ) 
       
   138     }    
       
   139 
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 TBool CMusEngTwoWaySession::IsRecording()
       
   146     {
       
   147     MUS_LOG( "mus: [ENGINE]  -> CMusEngTwoWaySession::IsRecording()" )    
       
   148     
       
   149     return EFalse;
       
   150     }
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 //
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 EXPORT_C void CMusEngTwoWaySession::SetRectsL( 
       
   157     const TRect& aRemoteRect,
       
   158     const TRect& aLocalRect )
       
   159     {
       
   160     MUS_LOG( "mus: [ENGINE]  -> CMusEngTwoWaySession::SetRectsL()" )  
       
   161      
       
   162     iLocalRect = aLocalRect;
       
   163     iRect = aRemoteRect;
       
   164     RectChangedL();
       
   165     
       
   166     MUS_LOG( "mus: [ENGINE]  <- CMusEngTwoWaySession::SetRectsL()" )
       
   167     }
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 //
       
   171 // -----------------------------------------------------------------------------
       
   172 //
       
   173 EXPORT_C void CMusEngTwoWaySession::SetLocalRectL( const TRect& aLocalRect )
       
   174     {
       
   175     MUS_LOG( "mus: [ENGINE]  -> CMusEngTwoWaySession::SetLocalRectL()" )
       
   176     
       
   177     iLocalRect = aLocalRect;
       
   178     RectChangedL();
       
   179 
       
   180     MUS_LOG( "mus: [ENGINE]  <- CMusEngTwoWaySession::SetLocalRectL()" )
       
   181     }
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 EXPORT_C TRect CMusEngTwoWaySession::LocalRect() const
       
   188     {
       
   189     MUS_LOG( "mus: [ENGINE]  -> CMusEngTwoWaySession::LocalRect()" )
       
   190     MUS_LOG( "mus: [ENGINE]  <- CMusEngTwoWaySession::LocalRect()" )
       
   191     return iLocalRect;
       
   192     }
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 //
       
   196 // -----------------------------------------------------------------------------
       
   197 //
       
   198 void CMusEngTwoWaySession::RectChangedL()
       
   199     {
       
   200     MUS_LOG( "mus: [ENGINE]  -> CMusEngTwoWaySession::RectChangedL()" )
       
   201 
       
   202     // If session is not yet created, do nothing
       
   203     if ( iSession && iSession->State() != CMceSession::ETerminated )
       
   204         {
       
   205         TRect remoteRect( Rect() );
       
   206         MUS_LOG2( "mus: [ENGINE]  remote tl.ix=%d tl.iy=%d", 
       
   207                   remoteRect.iTl.iX, remoteRect.iTl.iY )
       
   208         MUS_LOG2( "mus: [ENGINE]  remote br.ix=%d br.iy=%d", 
       
   209                   remoteRect.iBr.iX, remoteRect.iBr.iY )    
       
   210         MUS_LOG2( "mus: [ENGINE]  local tl.ix=%d tl.iy=%d", 
       
   211                   iLocalRect.iTl.iX, iLocalRect.iTl.iY )
       
   212         MUS_LOG2( "mus: [ENGINE]  local br.ix=%d br.iy=%d", 
       
   213                   iLocalRect.iBr.iX, iLocalRect.iBr.iY )
       
   214             
       
   215         CMceDisplaySink* display = MusEngMceUtils::GetReceivingDisplay( *iSession );
       
   216         if ( display )
       
   217             {
       
   218             display->SetDisplayRectL( remoteRect );
       
   219             }
       
   220         CMceDisplaySink* vfDisplay = MusEngMceUtils::GetDisplayL( *iSession, ETrue );
       
   221         vfDisplay->SetDisplayRectL( iLocalRect );
       
   222         }
       
   223 
       
   224     MUS_LOG( "mus: [ENGINE]  <- CMusEngTwoWaySession::RectChangedL()" )
       
   225     }
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 //
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 void CMusEngTwoWaySession::CompleteSessionStructureL( 
       
   232                                             CMceStreamBundle& /*aLocalBundle*/ )
       
   233     {
       
   234     MUS_LOG( "mus: [ENGINE]  -> CMusEngTwoWaySession::CompleteSessionStructureL()" )
       
   235 
       
   236     __ASSERT_ALWAYS( iSession, User::Leave( KErrNotReady ) );
       
   237 
       
   238     iCameraHandler.SetSession( iSession );
       
   239     
       
   240     // Create outgoing video stream
       
   241     CMceVideoStream* videoStream = CMceVideoStream::NewLC();
       
   242     
       
   243     CMceRtpSink* rtpsink = CMceRtpSink::NewLC();
       
   244     videoStream->AddSinkL( rtpsink );
       
   245     CleanupStack::Pop( rtpsink );
       
   246 
       
   247     CMceCameraSource* camera = CMceCameraSource::NewLC( *iManager );
       
   248     camera->DisableL(); // Start session in pause mode.
       
   249     
       
   250     iCameraHandler.InitializeL( *camera );
       
   251 
       
   252     videoStream->SetSourceL( camera );
       
   253     CleanupStack::Pop( camera );
       
   254     
       
   255     // Create incoming video stream
       
   256     CMceVideoStream* videoInStream = CMceVideoStream::NewL();
       
   257     CleanupStack::PushL( videoInStream );
       
   258        
       
   259     MusEngMceUtils::AddDisplayL( *videoInStream, *iManager, Rect() );
       
   260 
       
   261     CMceRtpSource* rtpSource = CMceRtpSource::NewLC( KMusEngJitterBufferLength,
       
   262                                                      KMusEngJitterBufferTreshold );
       
   263     videoInStream->SetSourceL( rtpSource );
       
   264     CleanupStack::Pop( rtpSource ); 
       
   265 
       
   266     videoStream->BindL( videoInStream ); 
       
   267     CleanupStack::Pop( videoInStream );
       
   268 
       
   269     iSession->AddStreamL( videoStream );
       
   270     CleanupStack::Pop( videoStream );    
       
   271 
       
   272     MUS_LOG( "mus: [ENGINE]  <- CMusEngTwoWaySession::CompleteSessionStructureL()" )
       
   273     }
       
   274 
       
   275 // -----------------------------------------------------------------------------
       
   276 //
       
   277 // -----------------------------------------------------------------------------
       
   278 //
       
   279 void CMusEngTwoWaySession::AddDisplayL( CMceMediaStream& aStream )
       
   280     {
       
   281     MusEngMceUtils::AddDisplayL( aStream, 
       
   282                                  *iManager,
       
   283                                  iLocalRect );
       
   284     }
       
   285  
       
   286 // -----------------------------------------------------------------------------
       
   287 //
       
   288 // -----------------------------------------------------------------------------
       
   289 //
       
   290 void CMusEngTwoWaySession::StreamStateChanged( CMceMediaStream& aStream )
       
   291     {
       
   292     MUS_LOG( "mus: [ENGINE] -> CMusEngTwoWaySession::StreamStateChanged()" )
       
   293     if ( !iSession )
       
   294         {
       
   295         return;
       
   296         }
       
   297     
       
   298     MUS_ENG_LOG_STREAM_STATE( aStream )
       
   299     
       
   300     if ( aStream.Type() == KMceVideo &&
       
   301          aStream.Source() &&
       
   302          aStream.Source()->Type() == KMceRTPSource )
       
   303         {
       
   304         if ( aStream.State() == CMceMediaStream::EStreaming )
       
   305             {
       
   306             ReceivingStarted();
       
   307             }
       
   308         else if ( aStream.State() == CMceMediaStream::EBuffering )
       
   309             {
       
   310             iBuffered = ETrue;
       
   311             }
       
   312         }
       
   313     
       
   314     CMusEngMceSession::StreamStateChanged( aStream );
       
   315 
       
   316     MUS_LOG( "mus: [ENGINE] <- CMusEngTwoWaySession::StreamStateChanged()" )
       
   317     }
       
   318 
       
   319 // -----------------------------------------------------------------------------
       
   320 //
       
   321 // -----------------------------------------------------------------------------
       
   322 //
       
   323 void CMusEngTwoWaySession::InactivityTimeout( CMceMediaStream& aStream,
       
   324                                               CMceRtpSource& /*aSource*/ )
       
   325     {
       
   326     MUS_LOG( "mus: [ENGINE]  -> CMusEngTwoWaySession::InactivityTimeout()" )
       
   327     
       
   328     if ( aStream.Type() == KMceVideo )
       
   329         {
       
   330         ReceivingStopped();
       
   331         }
       
   332     
       
   333     MUS_LOG( "mus: [ENGINE]  <- CMusEngTwoWaySession::InactivityTimeout()" )
       
   334     }
       
   335 
       
   336 // -----------------------------------------------------------------------------
       
   337 //
       
   338 // -----------------------------------------------------------------------------
       
   339 //
       
   340 void CMusEngTwoWaySession::HandleSessionStateChanged( 
       
   341                                                 CMceSession& aSession,
       
   342                                                 TInt aStatusCode,
       
   343                                                 const TDesC8& aReasonPhrase )
       
   344     {
       
   345     MUS_LOG( "mus: [ENGINE]  -> CMusEngTwoWaySession::HandleSessionStateChanged" )
       
   346     
       
   347     MUS_ENG_LOG_SESSION_STATE_AND_STATUS( aSession, aStatusCode, aReasonPhrase )
       
   348     
       
   349     TBool consumed( EFalse );
       
   350     if ( iSession && 
       
   351          iSession == &aSession && 
       
   352          aSession.State() == CMceSession::EEstablished )
       
   353         {   
       
   354         // TODO: if receiving stream is disabled, inform ui with some callback
       
   355         // that it can modify display rects accordingly and continue with 
       
   356         // session.
       
   357         CMceVideoStream* stream = NULL;
       
   358         CMceVideoStream* streamout = NULL;
       
   359         TRAPD( err, stream = MusEngMceUtils::GetVideoInStreamL( *iSession ) );
       
   360         TRAPD( errout, streamout = MusEngMceUtils::GetVideoOutStreamL( *iSession ) );
       
   361         if ( err == KErrNone && stream && stream->IsEnabled() )
       
   362             {
       
   363             if ( errout != KErrNone || !streamout || !streamout->IsEnabled() )
       
   364                 {
       
   365                 MUS_LOG( "mus: [ENGINE] sendrecv downgraded to recvonly, terminate!" )
       
   366                 consumed = ETrue;
       
   367                 iSessionObserver.SessionFailed();
       
   368                 }
       
   369             }
       
   370         else
       
   371             {
       
   372             if ( errout == KErrNone && streamout && streamout->IsEnabled() )
       
   373                 {
       
   374                 MUS_LOG( "mus: [ENGINE] sendrecv downgraded to sendonly, continue!" )
       
   375                 }
       
   376             }
       
   377         }
       
   378     
       
   379     if ( !consumed )
       
   380         {
       
   381         CMusEngLiveSession::HandleSessionStateChanged( aSession,
       
   382                                                        aStatusCode, 
       
   383                                                        aReasonPhrase );
       
   384         }
       
   385     MUS_LOG( "mus: [ENGINE]  <- CMusEngTwoWaySession::HandleSessionStateChanged" )
       
   386     }
       
   387 
       
   388 // -----------------------------------------------------------------------------
       
   389 //
       
   390 // -----------------------------------------------------------------------------
       
   391 //
       
   392 CMusEngTwoWaySession::CMusEngTwoWaySession(
       
   393                         MMusEngSessionObserver& aSessionObserver,
       
   394                         MMusEngOutSessionObserver& aOutSessionObserver,
       
   395                         MMusEngLiveSessionObserver& aLiveSessionObserver,
       
   396                         MMusEngTwoWaySessionObserver& aTwoWayObserver,
       
   397                         const TRect& aRemoteRect,
       
   398                         const TRect& aLocalRect ) : 
       
   399     CMusEngLiveSession( aSessionObserver,
       
   400                         aOutSessionObserver,
       
   401                         aLiveSessionObserver,
       
   402                         aRemoteRect,
       
   403                         KNullDesC ),
       
   404     iTwoWayObserver( aTwoWayObserver ),                   
       
   405     iLocalRect( aLocalRect )
       
   406     {
       
   407     }
       
   408 
       
   409 
       
   410 // -----------------------------------------------------------------------------
       
   411 //
       
   412 // -----------------------------------------------------------------------------
       
   413 //
       
   414 void CMusEngTwoWaySession::ConstructL()
       
   415     {
       
   416     MUS_LOG( "mus: [ENGINE]  -> CMusEngTwoWaySession::ConstructL()" )
       
   417     
       
   418     CMusEngLiveSession::ConstructL();
       
   419 
       
   420     MUS_LOG( "mus: [ENGINE]  <- CMusEngTwoWaySession::ConstructL()" )
       
   421     }
       
   422 
       
   423 // -----------------------------------------------------------------------------
       
   424 //
       
   425 // -----------------------------------------------------------------------------
       
   426 //
       
   427 void CMusEngTwoWaySession::ReceivingStarted()
       
   428     {
       
   429     if ( iSession && !iReceiving && iBuffered )
       
   430         {
       
   431         MUS_LOG( "mus: [ENGINE]  -> CMusEngTwoWaySession::ReceivingStarted()" )
       
   432         
       
   433         TInt err( KErrNone );
       
   434         CMceMediaSource* source = NULL;
       
   435         TRAP( err, source = 
       
   436             MusEngMceUtils::GetVideoInStreamL( *iSession )->Source() );
       
   437         if ( source && source->Type() == KMceRTPSource )
       
   438             {
       
   439             CMceRtpSource* rtpSource = static_cast<CMceRtpSource*>( source );
       
   440             TRAP( err, rtpSource->EnableInactivityTimerL( 
       
   441                             KMusEngTwoWayReceivingActivityTimeout ) ) 
       
   442             }
       
   443         
       
   444         if ( err != KErrNone )
       
   445             {
       
   446             MUS_LOG1("mus: [ENGINE] ReceivingStarted failed %d", err)
       
   447             iSessionObserver.SessionFailed();   
       
   448             }
       
   449         else
       
   450             {
       
   451             iReceiving = ETrue;
       
   452             iTwoWayObserver.ReceivingStateChanged( ETrue );
       
   453             }
       
   454         
       
   455         MUS_LOG( "mus: [ENGINE]  <- CMusEngTwoWaySession::ReceivingStarted()" )
       
   456         }
       
   457     }
       
   458 
       
   459 // -----------------------------------------------------------------------------
       
   460 //
       
   461 // -----------------------------------------------------------------------------
       
   462 //
       
   463 void CMusEngTwoWaySession::ReceivingStopped()
       
   464     {
       
   465     if ( iReceiving )
       
   466         {
       
   467         MUS_LOG( "mus: [ENGINE]  -> CMusEngTwoWaySession::ReceivingStopped()" )
       
   468         
       
   469         iReceiving = EFalse;
       
   470         iTwoWayObserver.ReceivingStateChanged( EFalse );
       
   471         
       
   472         MUS_LOG( "mus: [ENGINE]  <- CMusEngTwoWaySession::ReceivingStopped()" )
       
   473         }
       
   474     }
       
   475 
       
   476 // End of file