niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
tina.legrand@webrtc.org | 16b6b90 | 2012-04-12 11:02:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "coder.h" |
| 12 | #include "common_types.h" |
| 13 | #include "module_common_types.h" |
| 14 | |
| 15 | // OS independent case insensitive string comparison. |
| 16 | #ifdef WIN32 |
| 17 | #define STR_CASE_CMP(x,y) ::_stricmp(x,y) |
| 18 | #else |
| 19 | #define STR_CASE_CMP(x,y) ::strcasecmp(x,y) |
| 20 | #endif |
| 21 | |
| 22 | namespace webrtc { |
| 23 | AudioCoder::AudioCoder(WebRtc_UWord32 instanceID) |
wu@webrtc.org | 2259f85 | 2012-06-19 14:56:50 +0000 | [diff] [blame] | 24 | : _acm(AudioCodingModule::Create(instanceID)), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | _receiveCodec(), |
| 26 | _encodeTimestamp(0), |
| 27 | _encodedData(NULL), |
| 28 | _encodedLengthInBytes(0), |
| 29 | _decodeTimestamp(0) |
| 30 | { |
| 31 | _acm->InitializeSender(); |
| 32 | _acm->InitializeReceiver(); |
| 33 | _acm->RegisterTransportCallback(this); |
| 34 | } |
| 35 | |
| 36 | AudioCoder::~AudioCoder() |
| 37 | { |
| 38 | AudioCodingModule::Destroy(_acm); |
| 39 | } |
| 40 | |
| 41 | WebRtc_Word32 AudioCoder::SetEncodeCodec(const CodecInst& codecInst, |
| 42 | ACMAMRPackingFormat amrFormat) |
| 43 | { |
| 44 | if(_acm->RegisterSendCodec((CodecInst&)codecInst) == -1) |
| 45 | { |
| 46 | return -1; |
| 47 | } |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | WebRtc_Word32 AudioCoder::SetDecodeCodec(const CodecInst& codecInst, |
| 52 | ACMAMRPackingFormat amrFormat) |
| 53 | { |
| 54 | if(_acm->RegisterReceiveCodec((CodecInst&)codecInst) == -1) |
| 55 | { |
| 56 | return -1; |
| 57 | } |
| 58 | memcpy(&_receiveCodec,&codecInst,sizeof(CodecInst)); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | WebRtc_Word32 AudioCoder::Decode(AudioFrame& decodedAudio, |
| 63 | WebRtc_UWord32 sampFreqHz, |
| 64 | const WebRtc_Word8* incomingPayload, |
| 65 | WebRtc_Word32 payloadLength) |
| 66 | { |
| 67 | if (payloadLength > 0) |
| 68 | { |
| 69 | const WebRtc_UWord8 payloadType = _receiveCodec.pltype; |
| 70 | _decodeTimestamp += _receiveCodec.pacsize; |
tina.legrand@webrtc.org | 16b6b90 | 2012-04-12 11:02:38 +0000 | [diff] [blame] | 71 | if(_acm->IncomingPayload((const WebRtc_UWord8*) incomingPayload, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 72 | payloadLength, |
| 73 | payloadType, |
| 74 | _decodeTimestamp) == -1) |
| 75 | { |
| 76 | return -1; |
| 77 | } |
| 78 | } |
tina.legrand@webrtc.org | eb7ebf2 | 2013-02-20 15:57:31 +0000 | [diff] [blame] | 79 | return _acm->PlayoutData10Ms((WebRtc_UWord16)sampFreqHz, |
| 80 | (AudioFrame&)decodedAudio); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | WebRtc_Word32 AudioCoder::PlayoutData(AudioFrame& decodedAudio, |
| 84 | WebRtc_UWord16& sampFreqHz) |
| 85 | { |
tina.legrand@webrtc.org | eb7ebf2 | 2013-02-20 15:57:31 +0000 | [diff] [blame] | 86 | return _acm->PlayoutData10Ms(sampFreqHz, (AudioFrame&)decodedAudio); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | WebRtc_Word32 AudioCoder::Encode(const AudioFrame& audio, |
| 90 | WebRtc_Word8* encodedData, |
| 91 | WebRtc_UWord32& encodedLengthInBytes) |
| 92 | { |
| 93 | // Fake a timestamp in case audio doesn't contain a correct timestamp. |
| 94 | // Make a local copy of the audio frame since audio is const |
andrew@webrtc.org | ae1a58b | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 95 | AudioFrame audioFrame; |
| 96 | audioFrame.CopyFrom(audio); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 97 | audioFrame.timestamp_ = _encodeTimestamp; |
| 98 | _encodeTimestamp += audioFrame.samples_per_channel_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | |
| 100 | // For any codec with a frame size that is longer than 10 ms the encoded |
| 101 | // length in bytes should be zero until a a full frame has been encoded. |
| 102 | _encodedLengthInBytes = 0; |
| 103 | if(_acm->Add10MsData((AudioFrame&)audioFrame) == -1) |
| 104 | { |
| 105 | return -1; |
| 106 | } |
| 107 | _encodedData = encodedData; |
| 108 | if(_acm->Process() == -1) |
| 109 | { |
| 110 | return -1; |
| 111 | } |
| 112 | encodedLengthInBytes = _encodedLengthInBytes; |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | WebRtc_Word32 AudioCoder::SendData( |
| 117 | FrameType /* frameType */, |
| 118 | WebRtc_UWord8 /* payloadType */, |
| 119 | WebRtc_UWord32 /* timeStamp */, |
| 120 | const WebRtc_UWord8* payloadData, |
| 121 | WebRtc_UWord16 payloadSize, |
| 122 | const RTPFragmentationHeader* /* fragmentation*/) |
| 123 | { |
| 124 | memcpy(_encodedData,payloadData,sizeof(WebRtc_UWord8) * payloadSize); |
| 125 | _encodedLengthInBytes = payloadSize; |
| 126 | return 0; |
| 127 | } |
| 128 | } // namespace webrtc |