niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
tina.legrand@webrtc.org | 16b6b90 | 2012-04-12 11:02:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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.org | 8b06200 | 2013-07-12 08:28:10 +0000 | [diff] [blame] | 11 | #include "webrtc/common_types.h" |
ossu | e352578 | 2016-05-25 07:37:43 -0700 | [diff] [blame] | 12 | #include "webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 13 | #include "webrtc/modules/include/module_common_types.h" |
kwiberg | c8c71f4 | 2016-08-15 11:43:51 -0700 | [diff] [blame^] | 14 | #include "webrtc/modules/utility/source/coder.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | namespace webrtc { |
henrik.lundin | d4ccb00 | 2016-05-17 12:21:55 -0700 | [diff] [blame] | 17 | namespace { |
| 18 | AudioCodingModule::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; |
ossu | e352578 | 2016-05-25 07:37:43 -0700 | [diff] [blame] | 23 | config.decoder_factory = CreateBuiltinAudioDecoderFactory(); |
henrik.lundin | d4ccb00 | 2016-05-17 12:21:55 -0700 | [diff] [blame] | 24 | return config; |
| 25 | } |
| 26 | } // namespace |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 27 | |
kwiberg | 8a70714 | 2016-05-11 04:26:39 -0700 | [diff] [blame] | 28 | AudioCoder::AudioCoder(uint32_t instance_id) |
henrik.lundin | d4ccb00 | 2016-05-17 12:21:55 -0700 | [diff] [blame] | 29 | : acm_(AudioCodingModule::Create(GetAcmConfig(instance_id))), |
kwiberg | 8a70714 | 2016-05-11 04:26:39 -0700 | [diff] [blame] | 30 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | } |
| 38 | |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 39 | AudioCoder::~AudioCoder() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | |
kwiberg | 8a70714 | 2016-05-11 04:26:39 -0700 | [diff] [blame] | 41 | int32_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()); |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 44 | return success ? 0 : -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | } |
| 46 | |
kwiberg | 8a70714 | 2016-05-11 04:26:39 -0700 | [diff] [blame] | 47 | int32_t AudioCoder::SetDecodeCodec(const CodecInst& codec_inst) { |
kwiberg | abe95ba | 2016-06-02 02:58:59 -0700 | [diff] [blame] | 48 | if (acm_->RegisterReceiveCodec(codec_inst, [&] { |
| 49 | return rent_a_codec_.RentIsacDecoder(codec_inst.plfreq); |
| 50 | }) == -1) { |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 51 | return -1; |
| 52 | } |
kwiberg | 8a70714 | 2016-05-11 04:26:39 -0700 | [diff] [blame] | 53 | memcpy(&receive_codec_, &codec_inst, sizeof(CodecInst)); |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 54 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | } |
| 56 | |
kwiberg | 8a70714 | 2016-05-11 04:26:39 -0700 | [diff] [blame] | 57 | int32_t AudioCoder::Decode(AudioFrame& decoded_audio, |
| 58 | uint32_t samp_freq_hz, |
| 59 | const int8_t* incoming_payload, |
| 60 | size_t payload_length) { |
| 61 | if (payload_length > 0) { |
| 62 | const uint8_t payload_type = receive_codec_.pltype; |
| 63 | decode_timestamp_ += receive_codec_.pacsize; |
| 64 | if (acm_->IncomingPayload((const uint8_t*)incoming_payload, payload_length, |
| 65 | payload_type, decode_timestamp_) == -1) { |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 66 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 67 | } |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 68 | } |
henrik.lundin | d4ccb00 | 2016-05-17 12:21:55 -0700 | [diff] [blame] | 69 | bool muted; |
| 70 | int32_t ret = |
| 71 | acm_->PlayoutData10Ms((uint16_t)samp_freq_hz, &decoded_audio, &muted); |
| 72 | RTC_DCHECK(!muted); |
| 73 | return ret; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 74 | } |
| 75 | |
kwiberg | 8a70714 | 2016-05-11 04:26:39 -0700 | [diff] [blame] | 76 | int32_t AudioCoder::PlayoutData(AudioFrame& decoded_audio, |
| 77 | uint16_t& samp_freq_hz) { |
henrik.lundin | d4ccb00 | 2016-05-17 12:21:55 -0700 | [diff] [blame] | 78 | bool muted; |
| 79 | int32_t ret = acm_->PlayoutData10Ms(samp_freq_hz, &decoded_audio, &muted); |
| 80 | RTC_DCHECK(!muted); |
| 81 | return ret; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | } |
| 83 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 84 | int32_t AudioCoder::Encode(const AudioFrame& audio, |
kwiberg | 8a70714 | 2016-05-11 04:26:39 -0700 | [diff] [blame] | 85 | int8_t* encoded_data, |
| 86 | size_t& encoded_length_in_bytes) { |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 87 | // Fake a timestamp in case audio doesn't contain a correct timestamp. |
| 88 | // Make a local copy of the audio frame since audio is const |
kwiberg | 8a70714 | 2016-05-11 04:26:39 -0700 | [diff] [blame] | 89 | AudioFrame audio_frame; |
| 90 | audio_frame.CopyFrom(audio); |
| 91 | audio_frame.timestamp_ = encode_timestamp_; |
| 92 | encode_timestamp_ += static_cast<uint32_t>(audio_frame.samples_per_channel_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 94 | // For any codec with a frame size that is longer than 10 ms the encoded |
| 95 | // length in bytes should be zero until a a full frame has been encoded. |
kwiberg | 8a70714 | 2016-05-11 04:26:39 -0700 | [diff] [blame] | 96 | encoded_length_in_bytes_ = 0; |
| 97 | if (acm_->Add10MsData((AudioFrame&)audio_frame) == -1) { |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 98 | return -1; |
| 99 | } |
kwiberg | 8a70714 | 2016-05-11 04:26:39 -0700 | [diff] [blame] | 100 | encoded_data_ = encoded_data; |
| 101 | encoded_length_in_bytes = encoded_length_in_bytes_; |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 102 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | } |
| 104 | |
kwiberg | 8a70714 | 2016-05-11 04:26:39 -0700 | [diff] [blame] | 105 | int32_t AudioCoder::SendData(FrameType /* frame_type */, |
| 106 | uint8_t /* payload_type */, |
| 107 | uint32_t /* time_stamp */, |
| 108 | const uint8_t* payload_data, |
| 109 | size_t payload_size, |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 110 | const RTPFragmentationHeader* /* fragmentation*/) { |
kwiberg | 8a70714 | 2016-05-11 04:26:39 -0700 | [diff] [blame] | 111 | memcpy(encoded_data_, payload_data, sizeof(uint8_t) * payload_size); |
| 112 | encoded_length_in_bytes_ = payload_size; |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 113 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | } |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 115 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 116 | } // namespace webrtc |