blob: dc0799a2455a963b7827981da466ea85708291e9 [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{
niklase@google.com470e71d2011-07-07 08:21:25 +000024 _acm->InitializeReceiver();
25 _acm->RegisterTransportCallback(this);
26}
27
28AudioCoder::~AudioCoder()
29{
niklase@google.com470e71d2011-07-07 08:21:25 +000030}
31
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000032int32_t AudioCoder::SetEncodeCodec(const CodecInst& codecInst,
33 ACMAMRPackingFormat amrFormat)
niklase@google.com470e71d2011-07-07 08:21:25 +000034{
35 if(_acm->RegisterSendCodec((CodecInst&)codecInst) == -1)
36 {
37 return -1;
38 }
39 return 0;
40}
41
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000042int32_t AudioCoder::SetDecodeCodec(const CodecInst& codecInst,
43 ACMAMRPackingFormat amrFormat)
niklase@google.com470e71d2011-07-07 08:21:25 +000044{
45 if(_acm->RegisterReceiveCodec((CodecInst&)codecInst) == -1)
46 {
47 return -1;
48 }
49 memcpy(&_receiveCodec,&codecInst,sizeof(CodecInst));
50 return 0;
51}
52
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000053int32_t AudioCoder::Decode(AudioFrame& decodedAudio,
54 uint32_t sampFreqHz,
55 const int8_t* incomingPayload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000056 size_t payloadLength)
niklase@google.com470e71d2011-07-07 08:21:25 +000057{
58 if (payloadLength > 0)
59 {
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000060 const uint8_t payloadType = _receiveCodec.pltype;
niklase@google.com470e71d2011-07-07 08:21:25 +000061 _decodeTimestamp += _receiveCodec.pacsize;
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000062 if(_acm->IncomingPayload((const uint8_t*) incomingPayload,
niklase@google.com470e71d2011-07-07 08:21:25 +000063 payloadLength,
64 payloadType,
65 _decodeTimestamp) == -1)
66 {
67 return -1;
68 }
69 }
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000070 return _acm->PlayoutData10Ms((uint16_t)sampFreqHz, &decodedAudio);
niklase@google.com470e71d2011-07-07 08:21:25 +000071}
72
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000073int32_t AudioCoder::PlayoutData(AudioFrame& decodedAudio,
74 uint16_t& sampFreqHz)
niklase@google.com470e71d2011-07-07 08:21:25 +000075{
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +000076 return _acm->PlayoutData10Ms(sampFreqHz, &decodedAudio);
niklase@google.com470e71d2011-07-07 08:21:25 +000077}
78
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000079int32_t AudioCoder::Encode(const AudioFrame& audio,
80 int8_t* encodedData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000081 size_t& encodedLengthInBytes)
niklase@google.com470e71d2011-07-07 08:21:25 +000082{
83 // Fake a timestamp in case audio doesn't contain a correct timestamp.
84 // Make a local copy of the audio frame since audio is const
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +000085 AudioFrame audioFrame;
86 audioFrame.CopyFrom(audio);
andrew@webrtc.org63a50982012-05-02 23:56:37 +000087 audioFrame.timestamp_ = _encodeTimestamp;
88 _encodeTimestamp += audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +000089
90 // For any codec with a frame size that is longer than 10 ms the encoded
91 // length in bytes should be zero until a a full frame has been encoded.
92 _encodedLengthInBytes = 0;
93 if(_acm->Add10MsData((AudioFrame&)audioFrame) == -1)
94 {
95 return -1;
96 }
97 _encodedData = encodedData;
niklase@google.com470e71d2011-07-07 08:21:25 +000098 encodedLengthInBytes = _encodedLengthInBytes;
99 return 0;
100}
101
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000102int32_t AudioCoder::SendData(
niklase@google.com470e71d2011-07-07 08:21:25 +0000103 FrameType /* frameType */,
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000104 uint8_t /* payloadType */,
105 uint32_t /* timeStamp */,
106 const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000107 size_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000108 const RTPFragmentationHeader* /* fragmentation*/)
109{
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000110 memcpy(_encodedData,payloadData,sizeof(uint8_t) * payloadSize);
niklase@google.com470e71d2011-07-07 08:21:25 +0000111 _encodedLengthInBytes = payloadSize;
112 return 0;
113}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000114} // namespace webrtc