blob: 00ff969c2b551b5f581459b11a3e1d90b4693034 [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)
24 : _instanceID(instanceID),
25 _acm(AudioCodingModule::Create(instanceID)),
26 _receiveCodec(),
27 _encodeTimestamp(0),
28 _encodedData(NULL),
29 _encodedLengthInBytes(0),
30 _decodeTimestamp(0)
31{
32 _acm->InitializeSender();
33 _acm->InitializeReceiver();
34 _acm->RegisterTransportCallback(this);
35}
36
37AudioCoder::~AudioCoder()
38{
39 AudioCodingModule::Destroy(_acm);
40}
41
42WebRtc_Word32 AudioCoder::SetEncodeCodec(const CodecInst& codecInst,
43 ACMAMRPackingFormat amrFormat)
44{
45 if(_acm->RegisterSendCodec((CodecInst&)codecInst) == -1)
46 {
47 return -1;
48 }
49 return 0;
50}
51
52WebRtc_Word32 AudioCoder::SetDecodeCodec(const CodecInst& codecInst,
53 ACMAMRPackingFormat amrFormat)
54{
55 if(_acm->RegisterReceiveCodec((CodecInst&)codecInst) == -1)
56 {
57 return -1;
58 }
59 memcpy(&_receiveCodec,&codecInst,sizeof(CodecInst));
60 return 0;
61}
62
63WebRtc_Word32 AudioCoder::Decode(AudioFrame& decodedAudio,
64 WebRtc_UWord32 sampFreqHz,
65 const WebRtc_Word8* incomingPayload,
66 WebRtc_Word32 payloadLength)
67{
68 if (payloadLength > 0)
69 {
70 const WebRtc_UWord8 payloadType = _receiveCodec.pltype;
71 _decodeTimestamp += _receiveCodec.pacsize;
tina.legrand@webrtc.org16b6b902012-04-12 11:02:38 +000072 if(_acm->IncomingPayload((const WebRtc_UWord8*) incomingPayload,
niklase@google.com470e71d2011-07-07 08:21:25 +000073 payloadLength,
74 payloadType,
75 _decodeTimestamp) == -1)
76 {
77 return -1;
78 }
79 }
80 return _acm->PlayoutData10Ms((WebRtc_UWord16)sampFreqHz,
81 (AudioFrame&)decodedAudio);
82}
83
84WebRtc_Word32 AudioCoder::PlayoutData(AudioFrame& decodedAudio,
85 WebRtc_UWord16& sampFreqHz)
86{
87 return _acm->PlayoutData10Ms(sampFreqHz, (AudioFrame&)decodedAudio);
88}
89
90WebRtc_Word32 AudioCoder::Encode(const AudioFrame& audio,
91 WebRtc_Word8* encodedData,
92 WebRtc_UWord32& encodedLengthInBytes)
93{
94 // Fake a timestamp in case audio doesn't contain a correct timestamp.
95 // Make a local copy of the audio frame since audio is const
96 AudioFrame audioFrame = audio;
andrew@webrtc.org63a50982012-05-02 23:56:37 +000097 audioFrame.timestamp_ = _encodeTimestamp;
98 _encodeTimestamp += audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +000099
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
116WebRtc_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