os/mm/devsound/sounddevbt/src/A2dpBlueTooth/headsetaudioif/A2dpCodecUtilities.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #ifndef __A2DPCODECUTILITIES_H__
    17 #define __A2DPCODECUTILITIES_H__
    18 
    19 
    20 #include <bluetoothav.h> //TAvdtpSEPInfo, TSEID
    21 #include <mmf/server/sounddevice.h> //TMMFStereoSupport
    22 #include "BtSBCFrameParameters.h"
    23 
    24 //[TODO] change this to 44100 when we have hardware that supports higher data rates
    25 const TUint KDefaultSampleRate = 32000;
    26 const TUint KDefaultBitPoolValue = 20;
    27 //[TODO] change this to EMMFStereo & EMMFInterleavedOnly when we have hardware that supports higher data rates
    28 const TUint KDefaultChannels = EMMFMono;
    29 const TMMFStereoSupport KDefaultStereoSupport = EMMFNone;
    30 
    31 const TUint KMinBitPoolValue = 2;
    32 const TUint KMaxBitPoolValue = 250;
    33 
    34 //A2DP codec specific data masks
    35 const TUint8 KA2dpMPEG12AudioChannelModeMask = 0x0F; //A2DP 4.4.2.3
    36 const TUint8 KA2dpMPEG12LayerMask = 0xE0; //A2DP 4.4.2.1
    37 const TUint8 KA2dpMPEG12LayerMP3Supported = 0x20; //A2DP 4.4.2.1
    38 const TUint8 KA2dpMPEG12SamplingFrequencyMask = 0x3F; //A2DP 4.4.2.5
    39 const TUint8 KA2dpMPEG12SupportedBitRateIndex = 0xFE; //A2DP 4.4.2.7 octet 3 all bit rates <=96kbs-1 except free
    40 
    41 //RTP Codec profile specific constants
    42 const TUint KMPEG12RTPTimeStampClock = 90000; //RFC2250 3.3
    43 const TUint KSbcRTPPayloadType = 0; //A2DP 4.3.3.2 isn't too clear on this but 0 seems ok
    44 const TUint KMPEG12RTPAudioPayloadType = 14; //MPA MPEG12 PT RFC3551 6
    45 const TUint KSbcRTPMediaPayloadHeaderLength = 1;//A2DP 4.3.4
    46 const TUint KMPEG12RTPMediaPayloadHeaderLength = 4;//MPF-1 RFC2250 3.5 
    47 
    48 //Frame header masks
    49 const TUint8 KSbcFrameHeaderSyncWord = 0x9C;
    50 const TUint8 KSbcFrameHeaderSamplingFrequencyMask = 0xC0;
    51 const TUint8 KSbcFrameHeaderBlocksMask = 0x30;
    52 const TUint8 KSbcFrameHeaderChannelModeMask = 0x0C;
    53 const TUint8 KSbcFrameHeaderSubbandsMask = 0x01;
    54 const TUint8 KMPEGAudioFrameHeaderSyncWord = 0xFF;
    55 const TUint8 KMp3AudioFrameHeaderIdMask = 0xFE;//mask out CRC
    56 const TUint8 KMp3AudioFrameHeaderId = 0xFA;
    57 const TUint8 KMp3FrameHeaderSamplingFrequencyMask = 0x0C;
    58 const TUint8 KMp3FrameHeaderBitRateIndexMask = 0xF0;
    59 
    60 using namespace SymbianSBC;
    61 using namespace SymbianBluetoothAV;	
    62 
    63 
    64 typedef TUint8	TMPEG12SamplingFrequencyBitmask;
    65 	
    66 /**
    67 Mask for configuring mp3 sampling frequencies
    68 */
    69 enum TMPEG12SamplingFrequency
    70 	{
    71 	EMPEG12_16kHz		= 0x20,
    72 	EMPEG12_22050Hz		= 0x10,
    73 	EMPEG12_24kHz		= 0x8,
    74 	EMPEG12_32kHz		= 0x4,
    75 	EMPEG12_44100Hz		= 0x2,
    76 	EMPEG12_48kHz		= 0x1
    77 	};
    78 
    79 
    80 /**
    81 mp3 bit rate index table
    82 */	
    83 enum TMp3BitRateIndex
    84 	{
    85 	EMp3BitRateIndex32000	= 0x01,
    86 	EMp3BitRateIndex40000	= 0x02,
    87 	EMp3BitRateIndex48000	= 0x03,
    88 	EMp3BitRateIndex56000	= 0x04,
    89 	EMp3BitRateIndex64000	= 0x05,
    90 	EMp3BitRateIndex80000	= 0x06,
    91 	EMp3BitRateIndex96000	= 0x07,
    92 	EMp3BitRateIndex112000	= 0x08,
    93 	EMp3BitRateIndex128000	= 0x09,
    94 	EMp3BitRateIndex160000	= 0x0A,
    95 	EMp3BitRateIndex192000	= 0x0B,
    96 	EMp3BitRateIndex224000	= 0x0C,
    97 	EMp3BitRateIndex256000	= 0x0D,
    98 	EMp3BitRateIndex320000	= 0x0E
    99 	};
   100 
   101 /**
   102 Utility that parses the codec capabilities to get the supported sample rates and channels
   103 
   104 @internalComponent
   105 */
   106 class TA2dpCodecCapabilityParser
   107 	{
   108 public:
   109 	static TInt GetSupportedSampleRates(const TAvdtpMediaCodecCapabilities& aCodecCaps, RArray<TUint>& aSupportedDiscreteRates);
   110 	static TInt GetSupportedChannels(const TAvdtpMediaCodecCapabilities& aCodecCaps, RArray<TUint>& aSupportedChannels, TMMFStereoSupport& aStereoSupport);
   111 private:
   112 	static TInt GetSupportedSBCSampleRates(const TSBCCodecCapabilities& aCodecCaps, RArray<TUint>& aSupportedDiscreteRates);
   113 	static TInt GetSupportedMPEG12SampleRates(const TNonSBCCodecCapabilities& aCodecCaps, RArray<TUint>& aSupportedDiscreteRates);
   114 	static TInt GetSupportedSBCChannels(const TSBCCodecCapabilities& aCodecCaps, RArray<TUint>& aSupportedChannels, TMMFStereoSupport& aStereoSupport);
   115 	static TInt GetSupportedMPEG12Channels(const TNonSBCCodecCapabilities& aCodecCaps, RArray<TUint>& aSupportedChannels, TMMFStereoSupport& aStereoSupport);
   116 	static TInt GetSupportedChannelsCommonCode(TSBCChannelModeBitmask aChannelModesBitMask, RArray<TUint>& aSupportedChannels, TMMFStereoSupport& aStereoSupport);
   117 	};
   118 
   119 
   120 /**
   121 Utility to get codec specific RTP header parameters
   122 
   123 @internalComponent
   124 */
   125 class TRTPa2dpCodecSpecificUtils
   126 	{
   127 public:
   128 	static TUint8 PayloadType(const TFourCC& aCodecDataType);
   129 	static TUint MediaPayloadHeaderLength(const TFourCC& aCodecDataType);
   130 	};
   131 
   132 
   133 /**
   134 Utility to calculate frame duration and time stamp increments
   135 
   136 @internalComponent
   137 */	
   138 class TFrameTimingUtils
   139 	{
   140 public:
   141 	static TTimeIntervalMicroSeconds32 FrameDuration(TUint aFrameLength, TUint aBitRate);
   142 	static TUint TimeStampIncrementPerFrame(const TFourCC& aCodecDataType, TUint aFrameLength, TUint aBitRate, TUint aSampleRate);
   143 private:
   144 	static TUint CalculateSBCTimeStampIncrementPerFrame(TUint aFrameLength, TUint aBitRate, TUint aSampleRate);
   145 	static TUint CalculateMPEG12TimeStampIncrementPerFrame(TUint aFrameLength, TUint aBitRate);
   146 	};
   147 
   148 
   149 /**
   150 Utility class to store the local SEP codec capabilities
   151 
   152 @internalComponent
   153 */
   154 NONSHARABLE_CLASS(CA2dpLocalCodecCapabilities) : public CBase
   155 	{
   156 public:
   157 	static CA2dpLocalCodecCapabilities* NewL();
   158 	~CA2dpLocalCodecCapabilities();
   159 	TAvdtpMediaCodecCapabilities* LocalCodecCapabilities(const TFourCC& aCodecDataType);
   160 private:
   161 	CA2dpLocalCodecCapabilities();
   162 	TSBCCodecCapabilities* LocalSBCCodecCapabilities();
   163 	TNonSBCCodecCapabilities* LocalMPEG12CodecCapabilities();
   164 private:
   165 	TAvdtpMediaCodecCapabilities* iLocalCodecCapabilities; //this is owned
   166 	};
   167 
   168 	
   169 /** 
   170 Utiltiy class to store remote (ie headset) codec configuration 
   171 and the local (ie symbian OS device) SBC codec configuration.
   172 An instance of this class is owned by CA2dpBTHeadsetAudioInterface 
   173 which sets the settings - the configuration is passed to the GAVDP state machine
   174 which needs the configuration to configure the SEPs and RTPStreeamer uses
   175 the configuration to calculate frame sizes and timings.
   176 
   177 @internalComponent
   178 */
   179 NONSHARABLE_CLASS(CA2dpAudioCodecConfiguration) : public CBase 
   180 	{
   181 public:
   182 	static CA2dpAudioCodecConfiguration* NewL();
   183 	~CA2dpAudioCodecConfiguration();
   184 	void Reset();
   185 	inline void SetHeadsetCodecDataType(const TFourCC& aHeadsetCodecDataType);
   186 	inline void SetSampleRate(TUint aSampleRate);
   187 	inline void SetChannels(TUint aChannels);
   188 	inline void SetStereoSupport(TMMFStereoSupport aStereoSupport);
   189 	inline const TFourCC& HeadsetCodecDataType() const;
   190 	inline TUint SampleRate() const;
   191 	inline TUint Channels() const;
   192 	inline TMMFStereoSupport StereoSupport() const;
   193 	TAvdtpMediaCodecCapabilities* UpdateRemoteCodecConfiguration(const TAvdtpMediaCodecCapabilities& aRemoteCodecCapabilities);
   194 	TSBCFrameParameters& UpdateLocalSBCCodecConfiguration();
   195 	inline TAvdtpMediaCodecCapabilities* RemoteCodecConfiguration() const;
   196 	inline const TSBCFrameParameters& LocalSBCCodecConfiguration() const;
   197 	TTimeIntervalMicroSeconds32 FrameDuration(TUint aFrameLength) const;
   198 	TUint TimeStampIncrementPerFrame(TUint aFrameLength) const;
   199 	TUint CalculateSBCBufferLength(TUint aPCM16BufferLength) const;
   200 	void TEST_ForceRemoteSBCCodecConfiguration(const TSBCCodecCapabilities& aRemoteCodecConfiguration);
   201 private:
   202 	CA2dpAudioCodecConfiguration();
   203 	TSBCCodecCapabilities* GetRemoteSBCCodecConfiguration(const TSBCCodecCapabilities& aCodecCaps);
   204 	TNonSBCCodecCapabilities* GetRemoteMPEG12CodecConfiguration(const TNonSBCCodecCapabilities& aCodecCaps);
   205 	TTimeIntervalMicroSeconds32 CalculateSBCFrameDuration() const;
   206 	TTimeIntervalMicroSeconds32 CalculateMPEG12FrameDuration() const;
   207 	TUint CalculateSBCTimeStampIncrementPerFrame() const;
   208 	TUint CalculateMPEG12TimeStampIncrementPerFrame() const;
   209 private:
   210 	TFourCC iHeadsetCodecDataType;
   211 	TUint	iSampleRate;
   212 	TUint	iChannels;
   213 	TMMFStereoSupport	iStereoSupport;
   214 	TUint	iBitRate;
   215 	TAvdtpMediaCodecCapabilities* iRemoteCodecConfiguration;
   216 	TSBCFrameParameters iLocalSBCCodecConfiguration;
   217 	TSBCCodecCapabilities* iForcedRemoteSBCCodecConfiguration;//just used for unit test purposes	
   218 	};
   219 
   220 
   221 /**
   222 Utiltiy class to parse the header of non pcm16 data in order to obtain
   223 the frame length and bit rate
   224 
   225 @internalComponent
   226 */
   227 NONSHARABLE_CLASS(CA2dpCodecFrameHeaderParser)
   228 	{
   229 public:
   230 	static CA2dpCodecFrameHeaderParser* NewL(const TFourCC& aCodecDataType, const TDesC8& aHeader);
   231 	~CA2dpCodecFrameHeaderParser();
   232 	inline TUint FrameLength() const;
   233 	inline TUint BitRate() const;
   234 private:
   235 	CA2dpCodecFrameHeaderParser();
   236 	void ConstructL(const TFourCC& aCodecDataType, const TDesC8& aHeader);
   237 	void ParseSBCHeaderL(const TDesC8& aHeader);
   238 	void ParseMPEG12HeaderL(const TDesC8& aHeader);
   239 private:
   240 	TUint iFrameLength;
   241 	TUint iSampleRate;
   242 	TUint iBitRate;
   243 	};
   244 
   245 #include "A2dpCodecUtilities.inl"
   246 		
   247 #endif
   248