blob: 1476e02d9c5057fac58d46511582acdb3b5a982f [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
kwibergc8d071e2016-04-06 12:22:38 -070032int32_t AudioCoder::SetEncodeCodec(const CodecInst& codecInst) {
33 const bool success = codec_manager_.RegisterEncoder(codecInst) &&
34 codec_manager_.MakeEncoder(&rent_a_codec_, _acm.get());
35 return success ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +000036}
37
kwibergc8d071e2016-04-06 12:22:38 -070038int32_t AudioCoder::SetDecodeCodec(const CodecInst& codecInst) {
39 if (_acm->RegisterReceiveCodec(
40 codecInst, [&] { return rent_a_codec_.RentIsacDecoder(); }) == -1) {
41 return -1;
42 }
43 memcpy(&_receiveCodec, &codecInst, sizeof(CodecInst));
44 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000045}
46
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000047int32_t AudioCoder::Decode(AudioFrame& decodedAudio,
48 uint32_t sampFreqHz,
49 const int8_t* incomingPayload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000050 size_t payloadLength)
niklase@google.com470e71d2011-07-07 08:21:25 +000051{
52 if (payloadLength > 0)
53 {
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000054 const uint8_t payloadType = _receiveCodec.pltype;
niklase@google.com470e71d2011-07-07 08:21:25 +000055 _decodeTimestamp += _receiveCodec.pacsize;
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000056 if(_acm->IncomingPayload((const uint8_t*) incomingPayload,
niklase@google.com470e71d2011-07-07 08:21:25 +000057 payloadLength,
58 payloadType,
59 _decodeTimestamp) == -1)
60 {
61 return -1;
62 }
63 }
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000064 return _acm->PlayoutData10Ms((uint16_t)sampFreqHz, &decodedAudio);
niklase@google.com470e71d2011-07-07 08:21:25 +000065}
66
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000067int32_t AudioCoder::PlayoutData(AudioFrame& decodedAudio,
68 uint16_t& sampFreqHz)
niklase@google.com470e71d2011-07-07 08:21:25 +000069{
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +000070 return _acm->PlayoutData10Ms(sampFreqHz, &decodedAudio);
niklase@google.com470e71d2011-07-07 08:21:25 +000071}
72
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000073int32_t AudioCoder::Encode(const AudioFrame& audio,
74 int8_t* encodedData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000075 size_t& encodedLengthInBytes)
niklase@google.com470e71d2011-07-07 08:21:25 +000076{
77 // Fake a timestamp in case audio doesn't contain a correct timestamp.
78 // Make a local copy of the audio frame since audio is const
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +000079 AudioFrame audioFrame;
80 audioFrame.CopyFrom(audio);
andrew@webrtc.org63a50982012-05-02 23:56:37 +000081 audioFrame.timestamp_ = _encodeTimestamp;
Peter Kastingb7e50542015-06-11 12:55:50 -070082 _encodeTimestamp += static_cast<uint32_t>(audioFrame.samples_per_channel_);
niklase@google.com470e71d2011-07-07 08:21:25 +000083
84 // For any codec with a frame size that is longer than 10 ms the encoded
85 // length in bytes should be zero until a a full frame has been encoded.
86 _encodedLengthInBytes = 0;
87 if(_acm->Add10MsData((AudioFrame&)audioFrame) == -1)
88 {
89 return -1;
90 }
91 _encodedData = encodedData;
niklase@google.com470e71d2011-07-07 08:21:25 +000092 encodedLengthInBytes = _encodedLengthInBytes;
93 return 0;
94}
95
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000096int32_t AudioCoder::SendData(
niklase@google.com470e71d2011-07-07 08:21:25 +000097 FrameType /* frameType */,
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000098 uint8_t /* payloadType */,
99 uint32_t /* timeStamp */,
100 const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000101 size_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000102 const RTPFragmentationHeader* /* fragmentation*/)
103{
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000104 memcpy(_encodedData,payloadData,sizeof(uint8_t) * payloadSize);
niklase@google.com470e71d2011-07-07 08:21:25 +0000105 _encodedLengthInBytes = payloadSize;
106 return 0;
107}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000108} // namespace webrtc