blob: 18b690dc67c2ed154d3b2c079970859ec18f11aa [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"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010012#include "webrtc/modules/include/module_common_types.h"
pbos@webrtc.org8b062002013-07-12 08:28:10 +000013#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
henrik.lundin8387c5f2015-09-28 09:24:51 -070032int32_t AudioCoder::SetEncodeCodec(const CodecInst& codecInst)
niklase@google.com470e71d2011-07-07 08:21:25 +000033{
34 if(_acm->RegisterSendCodec((CodecInst&)codecInst) == -1)
35 {
36 return -1;
37 }
38 return 0;
39}
40
henrik.lundin8387c5f2015-09-28 09:24:51 -070041int32_t AudioCoder::SetDecodeCodec(const CodecInst& codecInst)
niklase@google.com470e71d2011-07-07 08:21:25 +000042{
43 if(_acm->RegisterReceiveCodec((CodecInst&)codecInst) == -1)
44 {
45 return -1;
46 }
47 memcpy(&_receiveCodec,&codecInst,sizeof(CodecInst));
48 return 0;
49}
50
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000051int32_t AudioCoder::Decode(AudioFrame& decodedAudio,
52 uint32_t sampFreqHz,
53 const int8_t* incomingPayload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000054 size_t payloadLength)
niklase@google.com470e71d2011-07-07 08:21:25 +000055{
56 if (payloadLength > 0)
57 {
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000058 const uint8_t payloadType = _receiveCodec.pltype;
niklase@google.com470e71d2011-07-07 08:21:25 +000059 _decodeTimestamp += _receiveCodec.pacsize;
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000060 if(_acm->IncomingPayload((const uint8_t*) incomingPayload,
niklase@google.com470e71d2011-07-07 08:21:25 +000061 payloadLength,
62 payloadType,
63 _decodeTimestamp) == -1)
64 {
65 return -1;
66 }
67 }
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000068 return _acm->PlayoutData10Ms((uint16_t)sampFreqHz, &decodedAudio);
niklase@google.com470e71d2011-07-07 08:21:25 +000069}
70
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000071int32_t AudioCoder::PlayoutData(AudioFrame& decodedAudio,
72 uint16_t& sampFreqHz)
niklase@google.com470e71d2011-07-07 08:21:25 +000073{
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +000074 return _acm->PlayoutData10Ms(sampFreqHz, &decodedAudio);
niklase@google.com470e71d2011-07-07 08:21:25 +000075}
76
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000077int32_t AudioCoder::Encode(const AudioFrame& audio,
78 int8_t* encodedData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000079 size_t& encodedLengthInBytes)
niklase@google.com470e71d2011-07-07 08:21:25 +000080{
81 // Fake a timestamp in case audio doesn't contain a correct timestamp.
82 // Make a local copy of the audio frame since audio is const
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +000083 AudioFrame audioFrame;
84 audioFrame.CopyFrom(audio);
andrew@webrtc.org63a50982012-05-02 23:56:37 +000085 audioFrame.timestamp_ = _encodeTimestamp;
Peter Kastingb7e50542015-06-11 12:55:50 -070086 _encodeTimestamp += static_cast<uint32_t>(audioFrame.samples_per_channel_);
niklase@google.com470e71d2011-07-07 08:21:25 +000087
88 // For any codec with a frame size that is longer than 10 ms the encoded
89 // length in bytes should be zero until a a full frame has been encoded.
90 _encodedLengthInBytes = 0;
91 if(_acm->Add10MsData((AudioFrame&)audioFrame) == -1)
92 {
93 return -1;
94 }
95 _encodedData = encodedData;
niklase@google.com470e71d2011-07-07 08:21:25 +000096 encodedLengthInBytes = _encodedLengthInBytes;
97 return 0;
98}
99
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000100int32_t AudioCoder::SendData(
niklase@google.com470e71d2011-07-07 08:21:25 +0000101 FrameType /* frameType */,
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000102 uint8_t /* payloadType */,
103 uint32_t /* timeStamp */,
104 const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000105 size_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000106 const RTPFragmentationHeader* /* fragmentation*/)
107{
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000108 memcpy(_encodedData,payloadData,sizeof(uint8_t) * payloadSize);
niklase@google.com470e71d2011-07-07 08:21:25 +0000109 _encodedLengthInBytes = payloadSize;
110 return 0;
111}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000112} // namespace webrtc