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" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 12 | #include "webrtc/modules/include/module_common_types.h" |
pbos@webrtc.org | 8b06200 | 2013-07-12 08:28:10 +0000 | [diff] [blame] | 13 | #include "webrtc/modules/utility/source/coder.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | namespace webrtc { |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 16 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 17 | AudioCoder::AudioCoder(uint32_t instanceID) |
wu@webrtc.org | 2259f85 | 2012-06-19 14:56:50 +0000 | [diff] [blame] | 18 | : _acm(AudioCodingModule::Create(instanceID)), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | _receiveCodec(), |
| 20 | _encodeTimestamp(0), |
| 21 | _encodedData(NULL), |
| 22 | _encodedLengthInBytes(0), |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 23 | _decodeTimestamp(0) { |
| 24 | _acm->InitializeReceiver(); |
| 25 | _acm->RegisterTransportCallback(this); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | } |
| 27 | |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 28 | AudioCoder::~AudioCoder() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 30 | int32_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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | } |
| 35 | |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 36 | int32_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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | } |
| 44 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 45 | int32_t AudioCoder::Decode(AudioFrame& decodedAudio, |
| 46 | uint32_t sampFreqHz, |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 47 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | } |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 56 | } |
| 57 | return _acm->PlayoutData10Ms((uint16_t)sampFreqHz, &decodedAudio); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | } |
| 59 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 60 | int32_t AudioCoder::PlayoutData(AudioFrame& decodedAudio, |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 61 | uint16_t& sampFreqHz) { |
| 62 | return _acm->PlayoutData10Ms(sampFreqHz, &decodedAudio); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 63 | } |
| 64 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 65 | int32_t AudioCoder::Encode(const AudioFrame& audio, |
| 66 | int8_t* encodedData, |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 67 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 74 | |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 75 | // 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | } |
| 85 | |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 86 | int32_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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | } |
kwiberg | 73987c9 | 2016-05-04 05:12:19 -0700 | [diff] [blame] | 96 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 97 | } // namespace webrtc |