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" |
| 12 | #include "webrtc/modules/interface/module_common_types.h" |
| 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 { |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 16 | AudioCoder::AudioCoder(uint32_t instanceID) |
wu@webrtc.org | 2259f85 | 2012-06-19 14:56:50 +0000 | [diff] [blame] | 17 | : _acm(AudioCodingModule::Create(instanceID)), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | _receiveCodec(), |
| 19 | _encodeTimestamp(0), |
| 20 | _encodedData(NULL), |
| 21 | _encodedLengthInBytes(0), |
| 22 | _decodeTimestamp(0) |
| 23 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | _acm->InitializeReceiver(); |
| 25 | _acm->RegisterTransportCallback(this); |
| 26 | } |
| 27 | |
| 28 | AudioCoder::~AudioCoder() |
| 29 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | } |
| 31 | |
henrik.lundin | 8387c5f | 2015-09-28 09:24:51 -0700 | [diff] [blame^] | 32 | int32_t AudioCoder::SetEncodeCodec(const CodecInst& codecInst) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | { |
| 34 | if(_acm->RegisterSendCodec((CodecInst&)codecInst) == -1) |
| 35 | { |
| 36 | return -1; |
| 37 | } |
| 38 | return 0; |
| 39 | } |
| 40 | |
henrik.lundin | 8387c5f | 2015-09-28 09:24:51 -0700 | [diff] [blame^] | 41 | int32_t AudioCoder::SetDecodeCodec(const CodecInst& codecInst) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | { |
| 43 | if(_acm->RegisterReceiveCodec((CodecInst&)codecInst) == -1) |
| 44 | { |
| 45 | return -1; |
| 46 | } |
| 47 | memcpy(&_receiveCodec,&codecInst,sizeof(CodecInst)); |
| 48 | return 0; |
| 49 | } |
| 50 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 51 | int32_t AudioCoder::Decode(AudioFrame& decodedAudio, |
| 52 | uint32_t sampFreqHz, |
| 53 | const int8_t* incomingPayload, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 54 | size_t payloadLength) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | { |
| 56 | if (payloadLength > 0) |
| 57 | { |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 58 | const uint8_t payloadType = _receiveCodec.pltype; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | _decodeTimestamp += _receiveCodec.pacsize; |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 60 | if(_acm->IncomingPayload((const uint8_t*) incomingPayload, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 61 | payloadLength, |
| 62 | payloadType, |
| 63 | _decodeTimestamp) == -1) |
| 64 | { |
| 65 | return -1; |
| 66 | } |
| 67 | } |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 68 | return _acm->PlayoutData10Ms((uint16_t)sampFreqHz, &decodedAudio); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | } |
| 70 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 71 | int32_t AudioCoder::PlayoutData(AudioFrame& decodedAudio, |
| 72 | uint16_t& sampFreqHz) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 73 | { |
tina.legrand@webrtc.org | 7a7a008 | 2013-02-21 10:27:48 +0000 | [diff] [blame] | 74 | return _acm->PlayoutData10Ms(sampFreqHz, &decodedAudio); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | } |
| 76 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 77 | int32_t AudioCoder::Encode(const AudioFrame& audio, |
| 78 | int8_t* encodedData, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 79 | size_t& encodedLengthInBytes) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | { |
| 81 | // Fake a timestamp in case audio doesn't contain a correct timestamp. |
| 82 | // Make a local copy of the audio frame since audio is const |
andrew@webrtc.org | ae1a58b | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 83 | AudioFrame audioFrame; |
| 84 | audioFrame.CopyFrom(audio); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 85 | audioFrame.timestamp_ = _encodeTimestamp; |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 86 | _encodeTimestamp += static_cast<uint32_t>(audioFrame.samples_per_channel_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 87 | |
| 88 | // For any codec with a frame size that is longer than 10 ms the encoded |
| 89 | // length in bytes should be zero until a a full frame has been encoded. |
| 90 | _encodedLengthInBytes = 0; |
| 91 | if(_acm->Add10MsData((AudioFrame&)audioFrame) == -1) |
| 92 | { |
| 93 | return -1; |
| 94 | } |
| 95 | _encodedData = encodedData; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | encodedLengthInBytes = _encodedLengthInBytes; |
| 97 | return 0; |
| 98 | } |
| 99 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 100 | int32_t AudioCoder::SendData( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 101 | FrameType /* frameType */, |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 102 | uint8_t /* payloadType */, |
| 103 | uint32_t /* timeStamp */, |
| 104 | const uint8_t* payloadData, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 105 | size_t payloadSize, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 106 | const RTPFragmentationHeader* /* fragmentation*/) |
| 107 | { |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 108 | memcpy(_encodedData,payloadData,sizeof(uint8_t) * payloadSize); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 109 | _encodedLengthInBytes = payloadSize; |
| 110 | return 0; |
| 111 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 112 | } // namespace webrtc |