blob: 248cd488b6dea7e16552fcbd42da74adaac94250 [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"
ossue3525782016-05-25 07:37:43 -070012#include "webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010013#include "webrtc/modules/include/module_common_types.h"
pbos@webrtc.org8b062002013-07-12 08:28:10 +000014#include "webrtc/modules/utility/source/coder.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015
niklase@google.com470e71d2011-07-07 08:21:25 +000016namespace webrtc {
henrik.lundind4ccb002016-05-17 12:21:55 -070017namespace {
18AudioCodingModule::Config GetAcmConfig(uint32_t id) {
19 AudioCodingModule::Config config;
20 // This class does not handle muted output.
21 config.neteq_config.enable_muted_state = false;
22 config.id = id;
ossue3525782016-05-25 07:37:43 -070023 config.decoder_factory = CreateBuiltinAudioDecoderFactory();
henrik.lundind4ccb002016-05-17 12:21:55 -070024 return config;
25}
26} // namespace
kwiberg73987c92016-05-04 05:12:19 -070027
kwiberg8a707142016-05-11 04:26:39 -070028AudioCoder::AudioCoder(uint32_t instance_id)
henrik.lundind4ccb002016-05-17 12:21:55 -070029 : acm_(AudioCodingModule::Create(GetAcmConfig(instance_id))),
kwiberg8a707142016-05-11 04:26:39 -070030 receive_codec_(),
31 encode_timestamp_(0),
32 encoded_data_(nullptr),
33 encoded_length_in_bytes_(0),
34 decode_timestamp_(0) {
35 acm_->InitializeReceiver();
36 acm_->RegisterTransportCallback(this);
niklase@google.com470e71d2011-07-07 08:21:25 +000037}
38
kwiberg73987c92016-05-04 05:12:19 -070039AudioCoder::~AudioCoder() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000040
kwiberg8a707142016-05-11 04:26:39 -070041int32_t AudioCoder::SetEncodeCodec(const CodecInst& codec_inst) {
42 const bool success = codec_manager_.RegisterEncoder(codec_inst) &&
43 codec_manager_.MakeEncoder(&rent_a_codec_, acm_.get());
kwibergc8d071e2016-04-06 12:22:38 -070044 return success ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +000045}
46
kwiberg8a707142016-05-11 04:26:39 -070047int32_t AudioCoder::SetDecodeCodec(const CodecInst& codec_inst) {
48 if (acm_->RegisterReceiveCodec(
49 codec_inst, [&] { return rent_a_codec_.RentIsacDecoder(); }) == -1) {
kwibergc8d071e2016-04-06 12:22:38 -070050 return -1;
51 }
kwiberg8a707142016-05-11 04:26:39 -070052 memcpy(&receive_codec_, &codec_inst, sizeof(CodecInst));
kwibergc8d071e2016-04-06 12:22:38 -070053 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000054}
55
kwiberg8a707142016-05-11 04:26:39 -070056int32_t AudioCoder::Decode(AudioFrame& decoded_audio,
57 uint32_t samp_freq_hz,
58 const int8_t* incoming_payload,
59 size_t payload_length) {
60 if (payload_length > 0) {
61 const uint8_t payload_type = receive_codec_.pltype;
62 decode_timestamp_ += receive_codec_.pacsize;
63 if (acm_->IncomingPayload((const uint8_t*)incoming_payload, payload_length,
64 payload_type, decode_timestamp_) == -1) {
kwiberg73987c92016-05-04 05:12:19 -070065 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +000066 }
kwiberg73987c92016-05-04 05:12:19 -070067 }
henrik.lundind4ccb002016-05-17 12:21:55 -070068 bool muted;
69 int32_t ret =
70 acm_->PlayoutData10Ms((uint16_t)samp_freq_hz, &decoded_audio, &muted);
71 RTC_DCHECK(!muted);
72 return ret;
niklase@google.com470e71d2011-07-07 08:21:25 +000073}
74
kwiberg8a707142016-05-11 04:26:39 -070075int32_t AudioCoder::PlayoutData(AudioFrame& decoded_audio,
76 uint16_t& samp_freq_hz) {
henrik.lundind4ccb002016-05-17 12:21:55 -070077 bool muted;
78 int32_t ret = acm_->PlayoutData10Ms(samp_freq_hz, &decoded_audio, &muted);
79 RTC_DCHECK(!muted);
80 return ret;
niklase@google.com470e71d2011-07-07 08:21:25 +000081}
82
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000083int32_t AudioCoder::Encode(const AudioFrame& audio,
kwiberg8a707142016-05-11 04:26:39 -070084 int8_t* encoded_data,
85 size_t& encoded_length_in_bytes) {
kwiberg73987c92016-05-04 05:12:19 -070086 // Fake a timestamp in case audio doesn't contain a correct timestamp.
87 // Make a local copy of the audio frame since audio is const
kwiberg8a707142016-05-11 04:26:39 -070088 AudioFrame audio_frame;
89 audio_frame.CopyFrom(audio);
90 audio_frame.timestamp_ = encode_timestamp_;
91 encode_timestamp_ += static_cast<uint32_t>(audio_frame.samples_per_channel_);
niklase@google.com470e71d2011-07-07 08:21:25 +000092
kwiberg73987c92016-05-04 05:12:19 -070093 // For any codec with a frame size that is longer than 10 ms the encoded
94 // length in bytes should be zero until a a full frame has been encoded.
kwiberg8a707142016-05-11 04:26:39 -070095 encoded_length_in_bytes_ = 0;
96 if (acm_->Add10MsData((AudioFrame&)audio_frame) == -1) {
kwiberg73987c92016-05-04 05:12:19 -070097 return -1;
98 }
kwiberg8a707142016-05-11 04:26:39 -070099 encoded_data_ = encoded_data;
100 encoded_length_in_bytes = encoded_length_in_bytes_;
kwiberg73987c92016-05-04 05:12:19 -0700101 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000102}
103
kwiberg8a707142016-05-11 04:26:39 -0700104int32_t AudioCoder::SendData(FrameType /* frame_type */,
105 uint8_t /* payload_type */,
106 uint32_t /* time_stamp */,
107 const uint8_t* payload_data,
108 size_t payload_size,
kwiberg73987c92016-05-04 05:12:19 -0700109 const RTPFragmentationHeader* /* fragmentation*/) {
kwiberg8a707142016-05-11 04:26:39 -0700110 memcpy(encoded_data_, payload_data, sizeof(uint8_t) * payload_size);
111 encoded_length_in_bytes_ = payload_size;
kwiberg73987c92016-05-04 05:12:19 -0700112 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000113}
kwiberg73987c92016-05-04 05:12:19 -0700114
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000115} // namespace webrtc