blob: 11a462abac7ac3585419b04c81dacddd01dc22dc [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
pbos@webrtc.org8b062002013-07-12 08:28:10 +000011#include "webrtc/common_types.h"
12#include "webrtc/modules/interface/module_common_types.h"
13#include "webrtc/modules/utility/source/coder.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000014
niklase@google.com470e71d2011-07-07 08:21:25 +000015namespace webrtc {
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000016AudioCoder::AudioCoder(uint32_t instanceID)
wu@webrtc.org2259f852012-06-19 14:56:50 +000017 : _acm(AudioCodingModule::Create(instanceID)),
niklase@google.com470e71d2011-07-07 08:21:25 +000018 _receiveCodec(),
19 _encodeTimestamp(0),
20 _encodedData(NULL),
21 _encodedLengthInBytes(0),
22 _decodeTimestamp(0)
23{
24 _acm->InitializeSender();
25 _acm->InitializeReceiver();
26 _acm->RegisterTransportCallback(this);
27}
28
29AudioCoder::~AudioCoder()
30{
niklase@google.com470e71d2011-07-07 08:21:25 +000031}
32
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000033int32_t AudioCoder::SetEncodeCodec(const CodecInst& codecInst,
34 ACMAMRPackingFormat amrFormat)
niklase@google.com470e71d2011-07-07 08:21:25 +000035{
36 if(_acm->RegisterSendCodec((CodecInst&)codecInst) == -1)
37 {
38 return -1;
39 }
40 return 0;
41}
42
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000043int32_t AudioCoder::SetDecodeCodec(const CodecInst& codecInst,
44 ACMAMRPackingFormat amrFormat)
niklase@google.com470e71d2011-07-07 08:21:25 +000045{
46 if(_acm->RegisterReceiveCodec((CodecInst&)codecInst) == -1)
47 {
48 return -1;
49 }
50 memcpy(&_receiveCodec,&codecInst,sizeof(CodecInst));
51 return 0;
52}
53
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000054int32_t AudioCoder::Decode(AudioFrame& decodedAudio,
55 uint32_t sampFreqHz,
56 const int8_t* incomingPayload,
57 int32_t payloadLength)
niklase@google.com470e71d2011-07-07 08:21:25 +000058{
59 if (payloadLength > 0)
60 {
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000061 const uint8_t payloadType = _receiveCodec.pltype;
niklase@google.com470e71d2011-07-07 08:21:25 +000062 _decodeTimestamp += _receiveCodec.pacsize;
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000063 if(_acm->IncomingPayload((const uint8_t*) incomingPayload,
niklase@google.com470e71d2011-07-07 08:21:25 +000064 payloadLength,
65 payloadType,
66 _decodeTimestamp) == -1)
67 {
68 return -1;
69 }
70 }
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000071 return _acm->PlayoutData10Ms((uint16_t)sampFreqHz, &decodedAudio);
niklase@google.com470e71d2011-07-07 08:21:25 +000072}
73
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000074int32_t AudioCoder::PlayoutData(AudioFrame& decodedAudio,
75 uint16_t& sampFreqHz)
niklase@google.com470e71d2011-07-07 08:21:25 +000076{
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +000077 return _acm->PlayoutData10Ms(sampFreqHz, &decodedAudio);
niklase@google.com470e71d2011-07-07 08:21:25 +000078}
79
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000080int32_t AudioCoder::Encode(const AudioFrame& audio,
81 int8_t* encodedData,
82 uint32_t& encodedLengthInBytes)
niklase@google.com470e71d2011-07-07 08:21:25 +000083{
84 // Fake a timestamp in case audio doesn't contain a correct timestamp.
85 // Make a local copy of the audio frame since audio is const
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +000086 AudioFrame audioFrame;
87 audioFrame.CopyFrom(audio);
andrew@webrtc.org63a50982012-05-02 23:56:37 +000088 audioFrame.timestamp_ = _encodeTimestamp;
89 _encodeTimestamp += audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +000090
91 // For any codec with a frame size that is longer than 10 ms the encoded
92 // length in bytes should be zero until a a full frame has been encoded.
93 _encodedLengthInBytes = 0;
94 if(_acm->Add10MsData((AudioFrame&)audioFrame) == -1)
95 {
96 return -1;
97 }
98 _encodedData = encodedData;
99 if(_acm->Process() == -1)
100 {
101 return -1;
102 }
103 encodedLengthInBytes = _encodedLengthInBytes;
104 return 0;
105}
106
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000107int32_t AudioCoder::SendData(
niklase@google.com470e71d2011-07-07 08:21:25 +0000108 FrameType /* frameType */,
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000109 uint8_t /* payloadType */,
110 uint32_t /* timeStamp */,
111 const uint8_t* payloadData,
112 uint16_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000113 const RTPFragmentationHeader* /* fragmentation*/)
114{
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000115 memcpy(_encodedData,payloadData,sizeof(uint8_t) * payloadSize);
niklase@google.com470e71d2011-07-07 08:21:25 +0000116 _encodedLengthInBytes = payloadSize;
117 return 0;
118}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000119} // namespace webrtc