bluetoothengine/btaudiostreamer/src/btaudiostreamer.cpp
changeset 0 f63038272f30
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     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:  Contains an implementation of CBTAudioStreamer class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <bt_sock.h> 
       
    20 #include <in_sock.h>
       
    21 
       
    22 #include "btaudiostreamerdebug.h" 
       
    23 #include "btaudiostreamer.h"
       
    24 #include "btaudiostreamsender.h"
       
    25 
       
    26 const TInt KL2CAPDefaultMTUSize = 672; 
       
    27 
       
    28 // ---------------------------------------------------------------------------
       
    29 // Constructor. 
       
    30 // ---------------------------------------------------------------------------
       
    31 //
       
    32 CBTAudioStreamer::CBTAudioStreamer() : iStarted(EFalse)  
       
    33     {
       
    34     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::CBTAudioStreamer() ->")));
       
    35     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::CBTAudioStreamer() <-")));
       
    36     }
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // Static factory method. 
       
    40 // ---------------------------------------------------------------------------
       
    41 //
       
    42 EXPORT_C CBTAudioStreamer* CBTAudioStreamer::NewL()
       
    43     {
       
    44     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::NewL() ->")));
       
    45     CBTAudioStreamer* self = new (ELeave) CBTAudioStreamer();
       
    46     CleanupStack::PushL(self);
       
    47     self->ConstructL();
       
    48     CleanupStack::Pop(self);
       
    49     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::NewL() <-")));
       
    50     return self;
       
    51     }
       
    52 
       
    53 // ---------------------------------------------------------------------------
       
    54 // Symbian OS second phase contruction. 
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 void CBTAudioStreamer::ConstructL()
       
    58     {
       
    59     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::ConstructL() ->")));
       
    60     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::ConstructL() <-")));
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // Destructor. 
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 CBTAudioStreamer::~CBTAudioStreamer()
       
    68     {
       
    69     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::~CBTAudioStreamer() ->")));
       
    70     Stop(); 
       
    71     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::~CBTAudioStreamer() <-")));
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // This method prepares the streamer for incoming data. 
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 EXPORT_C TInt CBTAudioStreamer::SetNewFrameLength(const TUint aFrameLength, const TUint aTargetBitrate) 
       
    79 {
       
    80     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::SetNewFrameLength() ->")));
       
    81 	TInt retVal = KErrGeneral; 
       
    82 	if(iStarted != EFalse) 
       
    83 		{
       
    84 	    TInt outboundMTUSize; 
       
    85 	//  if(aSocket.GetOpt(KL2CAPGetOutboundMTU, KSolBtL2CAP, outboundMTUSize) != KErrNone) 
       
    86 	        {
       
    87 	        BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t Failed to get outbound MTU size, using L2CAP default value")));
       
    88 	        outboundMTUSize = KL2CAPDefaultMTUSize; 
       
    89 	        }
       
    90 		retVal = iSender->SetNewFrameLength(outboundMTUSize, aFrameLength, aTargetBitrate); 
       
    91 		}
       
    92 	else 
       
    93 		{
       
    94 	    BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t Error: Not started!")));
       
    95 	    retVal = KErrNotReady; 
       
    96 		}
       
    97     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::SetNewFrameLength() <-")));
       
    98 	return retVal; 
       
    99 }
       
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // This method prepares the streamer for incoming data. 
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 EXPORT_C void CBTAudioStreamer::StartL(RSocket& aSocket, const TUint aFrameLength, CBTAudioStreamInputBase *aAudioInput, const TUint aTargetBitrate)
       
   106     {
       
   107     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::StartL() ->")));
       
   108     iAudioInput = aAudioInput; 
       
   109 
       
   110     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FPrint(_L("[BTAudioStreamer]\t Framelength: %d"), aFrameLength));
       
   111 
       
   112     if(iStarted != EFalse) 
       
   113         {
       
   114         BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t Error: Already started!")));
       
   115         User::Leave(KErrInUse); 
       
   116         }
       
   117 
       
   118     TInt inboundMTUSize = 0; 
       
   119     if(aSocket.GetOpt(KL2CAPInboundMTU, KSolBtL2CAP, inboundMTUSize) != KErrNone) 
       
   120         {
       
   121         BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t Failed to get inbound MTU size, using L2CAP default value")));
       
   122         inboundMTUSize = KL2CAPDefaultMTUSize; 
       
   123         }
       
   124 
       
   125     iRtpSession.OpenL(aSocket, inboundMTUSize); 
       
   126     iSender = CBTAudioStreamSender::NewL(*this, iRtpSession); 
       
   127 
       
   128     TInt outboundMTUSize; 
       
   129     if(aSocket.GetOpt(KL2CAPGetOutboundMTU, KSolBtL2CAP, outboundMTUSize) != KErrNone) 
       
   130         {
       
   131         BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t Failed to get outbound MTU size, using L2CAP default value")));
       
   132         outboundMTUSize = KL2CAPDefaultMTUSize; 
       
   133         }
       
   134 
       
   135     iSender->StartL(outboundMTUSize, aFrameLength, aTargetBitrate); 
       
   136 
       
   137 		iStarted = ETrue; 
       
   138 		
       
   139     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::StartL() <-")));
       
   140     }
       
   141 
       
   142 // ------------------------------------------------------------------------------------------
       
   143 // This method cleans up the streamer and releases the resources it reserved for streaming. 
       
   144 // ------------------------------------------------------------------------------------------
       
   145 //
       
   146 EXPORT_C void CBTAudioStreamer::Stop()
       
   147     {
       
   148     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::Stop() ->")));
       
   149     
       
   150     if(iStarted != EFalse) // Don't delete things unless we have started, thus created those. 
       
   151         {
       
   152         if(iSender) 
       
   153             {
       
   154             BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t Stopping and deleting the stream sender.")));
       
   155             iSender->Stop();
       
   156             delete iSender; 
       
   157             iSender = NULL; 
       
   158             }
       
   159         else 
       
   160             {
       
   161             BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t Error! Stream sender was deleted while the streamer was running!")));
       
   162             }
       
   163 
       
   164         BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t Closing RTP session.")));
       
   165         iRtpSession.Close(); 
       
   166         }
       
   167 
       
   168     iAudioInput = NULL; 
       
   169 	iStarted = EFalse;
       
   170     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::Stop() <-")));
       
   171     }
       
   172 
       
   173 TInt CBTAudioStreamer::Receive(const TDesC8& /*aBuffer*/)
       
   174     {
       
   175     return KErrNotSupported; 
       
   176     }
       
   177 
       
   178 TInt CBTAudioStreamer::Receive(const TDesC8& aBuffer, TTimeIntervalMicroSeconds aTimestamp)
       
   179     {
       
   180     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::Receive() ->")));
       
   181     TInt retval = iSender->SendThisBuffer(aBuffer, aTimestamp); 
       
   182     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::Receive() <-")));
       
   183     return retval; 
       
   184     }
       
   185 
       
   186 void CBTAudioStreamer::NotifyBufferSent( const TDesC8 &aBuffer ) 
       
   187     {
       
   188     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::NotifyBufferSent() ->")));
       
   189     if(iAudioInput) iAudioInput->BufferEmptied(aBuffer); 
       
   190     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::NotifyBufferSent() <-")));
       
   191     }
       
   192 
       
   193 void CBTAudioStreamer::NotifyErrorSending( const TDesC8 &/*aBuffer*/ ) 
       
   194     {
       
   195     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::NotifyErrorSending() ->")));
       
   196     BT_AUDIO_STREAMER_TRACE_OPT( KPRINTFTRACE, FLOG(_L("[BTAudioStreamer]\t CBTAudioStreamer::NotifyErrorSending() <-")));
       
   197     }