blob: 31d528ddb77787b45daa169c0439f05d11fb0dde [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
tina.legrand@webrtc.org16b6b902012-04-12 11:02:38 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
22namespace webrtc {
23AudioCoder::AudioCoder(WebRtc_UWord32 instanceID)
wu@webrtc.org2259f852012-06-19 14:56:50 +000024 : _acm(AudioCodingModule::Create(instanceID)),
niklase@google.com470e71d2011-07-07 08:21:25 +000025 _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
36AudioCoder::~AudioCoder()
37{
38 AudioCodingModule::Destroy(_acm);
39}
40
41WebRtc_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
51WebRtc_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
62WebRtc_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.org16b6b902012-04-12 11:02:38 +000071 if(_acm->IncomingPayload((const WebRtc_UWord8*) incomingPayload,
niklase@google.com470e71d2011-07-07 08:21:25 +000072 payloadLength,
73 payloadType,
74 _decodeTimestamp) == -1)
75 {
76 return -1;
77 }
78 }
79 return _acm->PlayoutData10Ms((WebRtc_UWord16)sampFreqHz,
80 (AudioFrame&)decodedAudio);
81}
82
83WebRtc_Word32 AudioCoder::PlayoutData(AudioFrame& decodedAudio,
84 WebRtc_UWord16& sampFreqHz)
85{
86 return _acm->PlayoutData10Ms(sampFreqHz, (AudioFrame&)decodedAudio);
87}
88
89WebRtc_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
95 AudioFrame audioFrame = audio;
andrew@webrtc.org63a50982012-05-02 23:56:37 +000096 audioFrame.timestamp_ = _encodeTimestamp;
97 _encodeTimestamp += audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +000098
99 // For any codec with a frame size that is longer than 10 ms the encoded
100 // length in bytes should be zero until a a full frame has been encoded.
101 _encodedLengthInBytes = 0;
102 if(_acm->Add10MsData((AudioFrame&)audioFrame) == -1)
103 {
104 return -1;
105 }
106 _encodedData = encodedData;
107 if(_acm->Process() == -1)
108 {
109 return -1;
110 }
111 encodedLengthInBytes = _encodedLengthInBytes;
112 return 0;
113}
114
115WebRtc_Word32 AudioCoder::SendData(
116 FrameType /* frameType */,
117 WebRtc_UWord8 /* payloadType */,
118 WebRtc_UWord32 /* timeStamp */,
119 const WebRtc_UWord8* payloadData,
120 WebRtc_UWord16 payloadSize,
121 const RTPFragmentationHeader* /* fragmentation*/)
122{
123 memcpy(_encodedData,payloadData,sizeof(WebRtc_UWord8) * payloadSize);
124 _encodedLengthInBytes = payloadSize;
125 return 0;
126}
127} // namespace webrtc