phoneplugins/csplugin/src/tmshandler.cpp
changeset 27 2f8f8080a020
child 65 2a5d4ab426d3
equal deleted inserted replaced
22:6bb1b21d2484 27:2f8f8080a020
       
     1 /*
       
     2  * Copyright (c) 2010 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:  Starts and stops audio streams.
       
    15  *
       
    16  */
       
    17 
       
    18 #include <tmsfactory.h>
       
    19 #include <tmscall.h>
       
    20 #include <tmsstream.h>
       
    21 #include "tmshandler.h"
       
    22 #include "csplogger.h"
       
    23 #include "csptimer.h"
       
    24 #include "csppanic.pan"
       
    25 
       
    26 /**
       
    27  * Timeout initial value.
       
    28  */
       
    29 const TInt KTimeoutInitial = 200000; // 0.2s
       
    30 
       
    31 /**
       
    32  * Double the timeout for every retry.
       
    33  */
       
    34 const TInt KTimeoutMultiplier = 2;
       
    35 
       
    36 // ======== MEMBER FUNCTIONS ========
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // Static constructor
       
    40 // ---------------------------------------------------------------------------
       
    41 //
       
    42 TmsHandler* TmsHandler::NewL()
       
    43     {
       
    44     TmsHandler* self = TmsHandler::NewLC();
       
    45     CleanupStack::Pop(self);
       
    46     return self;
       
    47     }
       
    48 
       
    49 // ---------------------------------------------------------------------------
       
    50 // Static constructor
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 TmsHandler* TmsHandler::NewLC()
       
    54     {
       
    55     TmsHandler* self = new (ELeave) TmsHandler();
       
    56     CleanupStack::PushL(self);
       
    57     self->ConstructL();
       
    58     return self;
       
    59     }
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // Destructor
       
    63 // ---------------------------------------------------------------------------
       
    64 //
       
    65 TmsHandler::~TmsHandler()
       
    66     {
       
    67     if (iTimer)
       
    68         {
       
    69         iTimer->CancelNotify();
       
    70         delete iTimer;
       
    71         }
       
    72     if (iTmsUplink && iTmsCall)
       
    73         {
       
    74         // CloseUplink();
       
    75         iTmsCall->DeleteStream(iTmsUplink);
       
    76         }
       
    77     if (iTmsDnlink && iTmsCall)
       
    78         {
       
    79         // CloseDownlink();
       
    80         iTmsCall->DeleteStream(iTmsDnlink);
       
    81         }
       
    82     if (iFactory && iTmsCall)
       
    83         {
       
    84         iFactory->DeleteCall(iTmsCall);
       
    85         }
       
    86     if (iFactory && iTmsMicSource)
       
    87         {
       
    88         iFactory->DeleteSource(iTmsMicSource);
       
    89         }
       
    90     if (iFactory && iTmsModemSource)
       
    91         {
       
    92         iFactory->DeleteSource(iTmsModemSource);
       
    93         }
       
    94     if (iFactory && iTmsSpeakerSink)
       
    95         {
       
    96         iFactory->DeleteSink(iTmsSpeakerSink);
       
    97         }
       
    98     if (iFactory && iTmsModemSink)
       
    99         {
       
   100         iFactory->DeleteSink(iTmsModemSink);
       
   101         }
       
   102 
       
   103     delete iFactory;
       
   104 
       
   105     }
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // From class MCSPAudioStream
       
   109 // Activates mic and speaker.
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 void TmsHandler::StartStreams()
       
   113     {
       
   114     CSPLOGSTRING(CSPINT, "TmsHandler::StartStreams");
       
   115 
       
   116     StartMicAndSpeaker();
       
   117     }
       
   118 
       
   119 // ---------------------------------------------------------------------------
       
   120 // From class MCSPAudioStream
       
   121 // Deactivates mic and speaker if the streams are active or they are
       
   122 // activating.
       
   123 // ---------------------------------------------------------------------------
       
   124 //
       
   125 void TmsHandler::StopStreams()
       
   126     {
       
   127     CSPLOGSTRING(CSPINT, "TmsHandler::StopStreams");
       
   128     gint status(TMS_RESULT_SUCCESS);
       
   129     if (iTimer && IsMicAndSpeakerStarted())
       
   130         {
       
   131         CSPLOGSTRING(CSPINT, "TmsHandler::StopStreams Stopping");
       
   132         iTimer->CancelNotify();
       
   133         iTimeout = KTimeoutInitial;
       
   134         status = iTmsUplink->Stop();
       
   135         iUplinkStarted = FALSE;
       
   136         status |= iTmsDnlink->Stop();
       
   137         iDnlinkStarted = FALSE;
       
   138 
       
   139         if (status != TMS_RESULT_SUCCESS)
       
   140             {
       
   141             status = TMS_RESULT_GENERAL_ERROR;
       
   142             }
       
   143         }
       
   144     CSPLOGSTRING2(CSPINT, "TmsHandler::StopStreams status %d", status);
       
   145     }
       
   146 
       
   147 // ---------------------------------------------------------------------------
       
   148 // From class MCSPTimerObserver
       
   149 // Notify from CSPTimer that timeout passed. Try to start mic and
       
   150 // speaker again.
       
   151 // ---------------------------------------------------------------------------
       
   152 //
       
   153 void TmsHandler::TimerEvent()
       
   154     {
       
   155     CSPLOGSTRING(CSPINT, "TmsHandler.TimerEvent" );
       
   156     iTimeout *= KTimeoutMultiplier;
       
   157     StartMicAndSpeaker();
       
   158     }
       
   159 
       
   160 // ---------------------------------------------------------------------------
       
   161 // Constructor
       
   162 // ---------------------------------------------------------------------------
       
   163 //
       
   164 TmsHandler::TmsHandler() :
       
   165     iTimeout(KTimeoutInitial)
       
   166     {
       
   167     }
       
   168 
       
   169 // ---------------------------------------------------------------------------
       
   170 // Second phase constructor
       
   171 // ---------------------------------------------------------------------------
       
   172 //
       
   173 void TmsHandler::ConstructL()
       
   174     {
       
   175     CSPLOGSTRING(CSPINT, "TmsHandler::ConstructL");
       
   176     iTimer = CSPTimer::NewL();
       
   177 
       
   178     if (CreateTMSCallControl())
       
   179         {
       
   180         User::LeaveIfError(TMS_RESULT_UNINITIALIZED_OBJECT);
       
   181         }
       
   182     }
       
   183 
       
   184 // ---------------------------------------------------------------------------
       
   185 // TmsHandler::CreateTMSCallControl()
       
   186 // ---------------------------------------------------------------------------
       
   187 //
       
   188 gint TmsHandler::CreateTMSCallControl()
       
   189     {
       
   190     TMSVer* v = NULL;
       
   191     TInt status;
       
   192     status = TMSFactory::CreateFactory(iFactory, *v);
       
   193 
       
   194     __ASSERT_ALWAYS(iFactory, Panic( ECSPPanicBadHandle));
       
   195 
       
   196     status = iFactory->CreateCall(TMS_CALL_CS, iTmsCall, 0);
       
   197 
       
   198     status |= CreateUplink();
       
   199     status |= CreateDownlink();
       
   200     status |= CreateMicSource();
       
   201     status |= AddMicSourceToStream();
       
   202     status |= CreateModemSink();
       
   203     status |= AddModemSinkToStream();
       
   204     status |= CreateModemSource();
       
   205     status |= AddModemSourceToStream();
       
   206     status |= CreateSpeakerSink();
       
   207     status |= AddSpeakerSinkToStream();
       
   208     status |= OpenDownlink();
       
   209 
       
   210     return status;
       
   211     }
       
   212 
       
   213 // ---------------------------------------------------------------------------
       
   214 // Resets timer
       
   215 // ---------------------------------------------------------------------------
       
   216 //
       
   217 void TmsHandler::AudioStreamsStarted()
       
   218     {
       
   219     CSPLOGSTRING(CSPINT, "TmsHandler::AudioStreamsStarted" );
       
   220     iTimeout = KTimeoutInitial;
       
   221     iTimer->CancelNotify();
       
   222     }
       
   223 
       
   224 // ---------------------------------------------------------------------------
       
   225 // Starts timer
       
   226 // ---------------------------------------------------------------------------
       
   227 //
       
   228 void TmsHandler::StartTimer()
       
   229     {
       
   230     CSPLOGSTRING(CSPINT, "TmsHandler::StartTimer" );
       
   231     iTimer->NotifyAfter(iTimeout, *this);
       
   232     }
       
   233 
       
   234 // ---------------------------------------------------------------------------
       
   235 // Starts mic and speaker
       
   236 // ---------------------------------------------------------------------------
       
   237 //
       
   238 void TmsHandler::StartMicAndSpeaker()
       
   239     {
       
   240     // if speaker and mic is active then activation does not cause any actions.
       
   241     gint status(TMS_RESULT_SUCCESS);
       
   242 
       
   243     if (iTmsUplink)
       
   244         {
       
   245         status = iTmsUplink->Start();
       
   246         }
       
   247     CSPLOGSTRING2( CSPINT, "TmsHandler::StartMicAndSpeaker status %d", status );
       
   248     }
       
   249 
       
   250 // ---------------------------------------------------------------------------
       
   251 // Indicated if mic and speaker are started or starting up.
       
   252 // ---------------------------------------------------------------------------
       
   253 //
       
   254 TBool TmsHandler::IsMicAndSpeakerStarted()
       
   255     {
       
   256     return (iUplinkStarted || iDnlinkStarted);
       
   257     }
       
   258 
       
   259 // ----------------------------------------------------------------------------
       
   260 // CreateUplink
       
   261 // ----------------------------------------------------------------------------
       
   262 //
       
   263 gint TmsHandler::CreateUplink()
       
   264     {
       
   265     CSPLOGSTRING(CSPINT, "TmsHandler::CreateUplink");
       
   266     gint status(TMS_RESULT_SUCCESS);
       
   267     if (iTmsCall)
       
   268         {
       
   269         status = iTmsCall->CreateStream(TMS_STREAM_UPLINK, iTmsUplink);
       
   270         }
       
   271     return status;
       
   272     }
       
   273 
       
   274 // ----------------------------------------------------------------------------
       
   275 // CreateDownlink
       
   276 // ----------------------------------------------------------------------------
       
   277 //
       
   278 gint TmsHandler::CreateDownlink()
       
   279     {
       
   280     CSPLOGSTRING(CSPINT, "TmsHandler::CreateDownlink");
       
   281     gint status(TMS_RESULT_SUCCESS);
       
   282 
       
   283     if (iTmsCall)
       
   284         {
       
   285         status = iTmsCall->CreateStream(TMS_STREAM_DOWNLINK, iTmsDnlink);
       
   286         }
       
   287     return status;
       
   288     }
       
   289 
       
   290 // ----------------------------------------------------------------------------
       
   291 // OpenUplink
       
   292 // ----------------------------------------------------------------------------
       
   293 //
       
   294 gint TmsHandler::OpenUplink()
       
   295     {
       
   296     CSPLOGSTRING(CSPINT, "TmsHandler::OpenUplink");
       
   297     gint status = TMS_RESULT_SUCCESS;
       
   298 
       
   299     if (iTmsUplink)
       
   300         {
       
   301         status = iTmsUplink->AddObserver(*this, NULL);
       
   302         if (status == TMS_RESULT_SUCCESS)
       
   303             {
       
   304             status = iTmsUplink->Init();
       
   305             }
       
   306         }
       
   307     return status;
       
   308     }
       
   309 
       
   310 // ----------------------------------------------------------------------------
       
   311 // OpenDownlink
       
   312 // ----------------------------------------------------------------------------
       
   313 //
       
   314 gint TmsHandler::OpenDownlink()
       
   315     {
       
   316     CSPLOGSTRING(CSPINT, "TmsHandler::OpenDownlink");
       
   317     gint status = TMS_RESULT_SUCCESS;
       
   318 
       
   319     if (iTmsDnlink)
       
   320         {
       
   321         status = iTmsDnlink->AddObserver(*this, NULL);
       
   322         if (status == TMS_RESULT_SUCCESS)
       
   323             {
       
   324             status = iTmsDnlink->Init();
       
   325             }
       
   326         }
       
   327     return status;
       
   328     }
       
   329 
       
   330 // ----------------------------------------------------------------------------
       
   331 // CreateModemSource
       
   332 // ----------------------------------------------------------------------------
       
   333 //
       
   334 gint TmsHandler::CreateModemSource()
       
   335     {
       
   336     gint status(TMS_RESULT_SUCCESS);
       
   337 
       
   338     if (iFactory && !iTmsModemSource)
       
   339         {
       
   340         status = iFactory->CreateSource(TMS_SOURCE_MODEM, iTmsModemSource);
       
   341         }
       
   342     return status;
       
   343     }
       
   344 
       
   345 // ----------------------------------------------------------------------------
       
   346 // AddModemSourceToStream
       
   347 // ----------------------------------------------------------------------------
       
   348 //
       
   349 gint TmsHandler::AddModemSourceToStream()
       
   350     {
       
   351     gint status(TMS_RESULT_SUCCESS);
       
   352     if (iTmsDnlink && iTmsModemSource)
       
   353         {
       
   354         status = iTmsDnlink->AddSource(iTmsModemSource);
       
   355         }
       
   356     return status;
       
   357     }
       
   358 
       
   359 // ----------------------------------------------------------------------------
       
   360 // CreateModemSink
       
   361 // ----------------------------------------------------------------------------
       
   362 //
       
   363 gint TmsHandler::CreateModemSink()
       
   364     {
       
   365     gint status(TMS_RESULT_SUCCESS);
       
   366 
       
   367     if (iFactory && !iTmsModemSink)
       
   368         {
       
   369         status = iFactory->CreateSink(TMS_SINK_MODEM, iTmsModemSink);
       
   370         }
       
   371     return status;
       
   372     }
       
   373 
       
   374 // ----------------------------------------------------------------------------
       
   375 // AddModemSinkToStream
       
   376 // ----------------------------------------------------------------------------
       
   377 //
       
   378 gint TmsHandler::AddModemSinkToStream()
       
   379     {
       
   380     gint status(TMS_RESULT_SUCCESS);
       
   381     if (iTmsUplink && iTmsModemSink)
       
   382         {
       
   383         status = iTmsUplink->AddSink(iTmsModemSink);
       
   384         }
       
   385     return status;
       
   386     }
       
   387 
       
   388 // ----------------------------------------------------------------------------
       
   389 // CreateMicSource
       
   390 // ----------------------------------------------------------------------------
       
   391 //
       
   392 gint TmsHandler::CreateMicSource()
       
   393     {
       
   394     gint status(TMS_RESULT_SUCCESS);
       
   395 
       
   396     if (iFactory && !iTmsMicSource)
       
   397         {
       
   398         status = iFactory->CreateSource(TMS_SOURCE_MIC, iTmsMicSource);
       
   399         }
       
   400     return status;
       
   401     }
       
   402 
       
   403 // ----------------------------------------------------------------------------
       
   404 // AddMicSourceToStream
       
   405 // ----------------------------------------------------------------------------
       
   406 //
       
   407 gint TmsHandler::AddMicSourceToStream()
       
   408     {
       
   409     gint status(TMS_RESULT_SUCCESS);
       
   410     if (iTmsUplink && iTmsMicSource)
       
   411         {
       
   412         status = iTmsUplink->AddSource(iTmsMicSource);
       
   413         }
       
   414     return status;
       
   415     }
       
   416 
       
   417 // ----------------------------------------------------------------------------
       
   418 // CreateSpeakerSink
       
   419 // ----------------------------------------------------------------------------
       
   420 //
       
   421 gint TmsHandler::CreateSpeakerSink()
       
   422     {
       
   423     gint status(TMS_RESULT_SUCCESS);
       
   424 
       
   425     if (iFactory && !iTmsSpeakerSink)
       
   426         {
       
   427         status = iFactory->CreateSink(TMS_SINK_SPEAKER, iTmsSpeakerSink);
       
   428         }
       
   429     return status;
       
   430     }
       
   431 
       
   432 // ----------------------------------------------------------------------------
       
   433 // AddSpeakerSinkToStream
       
   434 // ----------------------------------------------------------------------------
       
   435 //
       
   436 gint TmsHandler::AddSpeakerSinkToStream()
       
   437     {
       
   438     gint status(TMS_RESULT_SUCCESS);
       
   439     if (iTmsDnlink && iTmsSpeakerSink)
       
   440         {
       
   441         status = iTmsDnlink->AddSink(iTmsSpeakerSink);
       
   442         }
       
   443     return status;
       
   444     }
       
   445 
       
   446 // TMS CALLBACKS
       
   447 
       
   448 // ----------------------------------------------------------------------------
       
   449 // TmsHandler::TMSStreamEvent
       
   450 // ----------------------------------------------------------------------------
       
   451 //
       
   452 void TmsHandler::TMSStreamEvent(const TMSStream& stream, TMSSignalEvent event)
       
   453     {
       
   454     CSPLOGSTRING2(CSPINT, "TmsHandler::TMSStreamEvent status %d", event.reason);
       
   455 
       
   456     TMSStreamType strmType = const_cast<TMSStream&>(stream).GetStreamType();
       
   457 
       
   458     if (strmType == TMS_STREAM_UPLINK &&
       
   459             event.type == TMS_EVENT_STREAM_STATE_CHANGED)
       
   460         {
       
   461         switch (event.curr_state)
       
   462             {
       
   463             case TMS_STREAM_INITIALIZED:
       
   464                 //notify stream ready state
       
   465                 break;
       
   466             case TMS_STREAM_UNINITIALIZED:
       
   467                 //notify initialization error
       
   468                 break;
       
   469             case TMS_STREAM_PAUSED:
       
   470                 break;
       
   471             case TMS_STREAM_STARTED:
       
   472                 iUplinkStarted = TRUE;
       
   473                 iTmsDnlink->Start();
       
   474                 break;
       
   475             default:
       
   476                 break;
       
   477             }
       
   478         }
       
   479     else if (strmType == TMS_STREAM_DOWNLINK &&
       
   480             event.type == TMS_EVENT_STREAM_STATE_CHANGED)
       
   481         {
       
   482         switch (event.curr_state)
       
   483             {
       
   484             case TMS_STREAM_INITIALIZED:
       
   485                 {
       
   486                 if ((iTmsCall->GetCallType() == TMS_CALL_CS)
       
   487                         && (!iUplinkStarted))
       
   488                     {
       
   489                     OpenUplink();
       
   490                     }
       
   491                 //notify stream ready state
       
   492                 }
       
   493                 break;
       
   494             case TMS_STREAM_UNINITIALIZED:
       
   495                 //notify initialization error
       
   496                 break;
       
   497             case TMS_STREAM_PAUSED:
       
   498                 break;
       
   499             case TMS_STREAM_STARTED:
       
   500                 iDnlinkStarted = TRUE;
       
   501                 break;
       
   502             default:
       
   503                 break;
       
   504             }
       
   505         }
       
   506     }
       
   507 
       
   508 //  End of File