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