blob: a376cc79273286420fd3898faddaefa21655f753 [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 {
kwiberg73987c92016-05-04 05:12:19 -070016
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000017AudioCoder::AudioCoder(uint32_t instanceID)
wu@webrtc.org2259f852012-06-19 14:56:50 +000018 : _acm(AudioCodingModule::Create(instanceID)),
niklase@google.com470e71d2011-07-07 08:21:25 +000019 _receiveCodec(),
20 _encodeTimestamp(0),
21 _encodedData(NULL),
22 _encodedLengthInBytes(0),
kwiberg73987c92016-05-04 05:12:19 -070023 _decodeTimestamp(0) {
24 _acm->InitializeReceiver();
25 _acm->RegisterTransportCallback(this);
niklase@google.com470e71d2011-07-07 08:21:25 +000026}
27
kwiberg73987c92016-05-04 05:12:19 -070028AudioCoder::~AudioCoder() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000029
kwibergc8d071e2016-04-06 12:22:38 -070030int32_t AudioCoder::SetEncodeCodec(const CodecInst& codecInst) {
31 const bool success = codec_manager_.RegisterEncoder(codecInst) &&
32 codec_manager_.MakeEncoder(&rent_a_codec_, _acm.get());
33 return success ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +000034}
35
kwibergc8d071e2016-04-06 12:22:38 -070036int32_t AudioCoder::SetDecodeCodec(const CodecInst& codecInst) {
37 if (_acm->RegisterReceiveCodec(
38 codecInst, [&] { return rent_a_codec_.RentIsacDecoder(); }) == -1) {
39 return -1;
40 }
41 memcpy(&_receiveCodec, &codecInst, sizeof(CodecInst));
42 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000043}
44
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000045int32_t AudioCoder::Decode(AudioFrame& decodedAudio,
46 uint32_t sampFreqHz,
kwiberg73987c92016-05-04 05:12:19 -070047 const int8_t* incomingPayload,
48 size_t payloadLength) {
49 if (payloadLength > 0) {
50 const uint8_t payloadType = _receiveCodec.pltype;
51 _decodeTimestamp += _receiveCodec.pacsize;
52 if (_acm->IncomingPayload((const uint8_t*)incomingPayload, payloadLength,
53 payloadType, _decodeTimestamp) == -1) {
54 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +000055 }
kwiberg73987c92016-05-04 05:12:19 -070056 }
57 return _acm->PlayoutData10Ms((uint16_t)sampFreqHz, &decodedAudio);
niklase@google.com470e71d2011-07-07 08:21:25 +000058}
59
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000060int32_t AudioCoder::PlayoutData(AudioFrame& decodedAudio,
kwiberg73987c92016-05-04 05:12:19 -070061 uint16_t& sampFreqHz) {
62 return _acm->PlayoutData10Ms(sampFreqHz, &decodedAudio);
niklase@google.com470e71d2011-07-07 08:21:25 +000063}
64
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000065int32_t AudioCoder::Encode(const AudioFrame& audio,
66 int8_t* encodedData,
kwiberg73987c92016-05-04 05:12:19 -070067 size_t& encodedLengthInBytes) {
68 // Fake a timestamp in case audio doesn't contain a correct timestamp.
69 // Make a local copy of the audio frame since audio is const
70 AudioFrame audioFrame;
71 audioFrame.CopyFrom(audio);
72 audioFrame.timestamp_ = _encodeTimestamp;
73 _encodeTimestamp += static_cast<uint32_t>(audioFrame.samples_per_channel_);
niklase@google.com470e71d2011-07-07 08:21:25 +000074
kwiberg73987c92016-05-04 05:12:19 -070075 // For any codec with a frame size that is longer than 10 ms the encoded
76 // length in bytes should be zero until a a full frame has been encoded.
77 _encodedLengthInBytes = 0;
78 if (_acm->Add10MsData((AudioFrame&)audioFrame) == -1) {
79 return -1;
80 }
81 _encodedData = encodedData;
82 encodedLengthInBytes = _encodedLengthInBytes;
83 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000084}
85
kwiberg73987c92016-05-04 05:12:19 -070086int32_t AudioCoder::SendData(FrameType /* frameType */,
87 uint8_t /* payloadType */,
88 uint32_t /* timeStamp */,
89 const uint8_t* payloadData,
90 size_t payloadSize,
91 const RTPFragmentationHeader* /* fragmentation*/) {
92 memcpy(_encodedData, payloadData, sizeof(uint8_t) * payloadSize);
93 _encodedLengthInBytes = payloadSize;
94 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000095}
kwiberg73987c92016-05-04 05:12:19 -070096
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000097} // namespace webrtc