blob: a610daa6bd82cda3b011808d66c5b87649736336 [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
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +000095 AudioFrame audioFrame;
96 audioFrame.CopyFrom(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