| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/common_types.h" | 11 #include "webrtc/common_types.h" |
| 12 #include "webrtc/modules/include/module_common_types.h" | 12 #include "webrtc/modules/include/module_common_types.h" |
| 13 #include "webrtc/modules/utility/source/coder.h" | 13 #include "webrtc/modules/utility/source/coder.h" |
| 14 | 14 |
| 15 namespace webrtc { | 15 namespace webrtc { |
| 16 |
| 16 AudioCoder::AudioCoder(uint32_t instanceID) | 17 AudioCoder::AudioCoder(uint32_t instanceID) |
| 17 : _acm(AudioCodingModule::Create(instanceID)), | 18 : _acm(AudioCodingModule::Create(instanceID)), |
| 18 _receiveCodec(), | 19 _receiveCodec(), |
| 19 _encodeTimestamp(0), | 20 _encodeTimestamp(0), |
| 20 _encodedData(NULL), | 21 _encodedData(NULL), |
| 21 _encodedLengthInBytes(0), | 22 _encodedLengthInBytes(0), |
| 22 _decodeTimestamp(0) | 23 _decodeTimestamp(0) { |
| 23 { | 24 _acm->InitializeReceiver(); |
| 24 _acm->InitializeReceiver(); | 25 _acm->RegisterTransportCallback(this); |
| 25 _acm->RegisterTransportCallback(this); | |
| 26 } | 26 } |
| 27 | 27 |
| 28 AudioCoder::~AudioCoder() | 28 AudioCoder::~AudioCoder() {} |
| 29 { | |
| 30 } | |
| 31 | 29 |
| 32 int32_t AudioCoder::SetEncodeCodec(const CodecInst& codecInst) { | 30 int32_t AudioCoder::SetEncodeCodec(const CodecInst& codecInst) { |
| 33 const bool success = codec_manager_.RegisterEncoder(codecInst) && | 31 const bool success = codec_manager_.RegisterEncoder(codecInst) && |
| 34 codec_manager_.MakeEncoder(&rent_a_codec_, _acm.get()); | 32 codec_manager_.MakeEncoder(&rent_a_codec_, _acm.get()); |
| 35 return success ? 0 : -1; | 33 return success ? 0 : -1; |
| 36 } | 34 } |
| 37 | 35 |
| 38 int32_t AudioCoder::SetDecodeCodec(const CodecInst& codecInst) { | 36 int32_t AudioCoder::SetDecodeCodec(const CodecInst& codecInst) { |
| 39 if (_acm->RegisterReceiveCodec( | 37 if (_acm->RegisterReceiveCodec( |
| 40 codecInst, [&] { return rent_a_codec_.RentIsacDecoder(); }) == -1) { | 38 codecInst, [&] { return rent_a_codec_.RentIsacDecoder(); }) == -1) { |
| 41 return -1; | 39 return -1; |
| 42 } | 40 } |
| 43 memcpy(&_receiveCodec, &codecInst, sizeof(CodecInst)); | 41 memcpy(&_receiveCodec, &codecInst, sizeof(CodecInst)); |
| 44 return 0; | 42 return 0; |
| 45 } | 43 } |
| 46 | 44 |
| 47 int32_t AudioCoder::Decode(AudioFrame& decodedAudio, | 45 int32_t AudioCoder::Decode(AudioFrame& decodedAudio, |
| 48 uint32_t sampFreqHz, | 46 uint32_t sampFreqHz, |
| 49 const int8_t* incomingPayload, | 47 const int8_t* incomingPayload, |
| 50 size_t payloadLength) | 48 size_t payloadLength) { |
| 51 { | 49 if (payloadLength > 0) { |
| 52 if (payloadLength > 0) | 50 const uint8_t payloadType = _receiveCodec.pltype; |
| 53 { | 51 _decodeTimestamp += _receiveCodec.pacsize; |
| 54 const uint8_t payloadType = _receiveCodec.pltype; | 52 if (_acm->IncomingPayload((const uint8_t*)incomingPayload, payloadLength, |
| 55 _decodeTimestamp += _receiveCodec.pacsize; | 53 payloadType, _decodeTimestamp) == -1) { |
| 56 if(_acm->IncomingPayload((const uint8_t*) incomingPayload, | 54 return -1; |
| 57 payloadLength, | |
| 58 payloadType, | |
| 59 _decodeTimestamp) == -1) | |
| 60 { | |
| 61 return -1; | |
| 62 } | |
| 63 } | 55 } |
| 64 return _acm->PlayoutData10Ms((uint16_t)sampFreqHz, &decodedAudio); | 56 } |
| 57 return _acm->PlayoutData10Ms((uint16_t)sampFreqHz, &decodedAudio); |
| 65 } | 58 } |
| 66 | 59 |
| 67 int32_t AudioCoder::PlayoutData(AudioFrame& decodedAudio, | 60 int32_t AudioCoder::PlayoutData(AudioFrame& decodedAudio, |
| 68 uint16_t& sampFreqHz) | 61 uint16_t& sampFreqHz) { |
| 69 { | 62 return _acm->PlayoutData10Ms(sampFreqHz, &decodedAudio); |
| 70 return _acm->PlayoutData10Ms(sampFreqHz, &decodedAudio); | |
| 71 } | 63 } |
| 72 | 64 |
| 73 int32_t AudioCoder::Encode(const AudioFrame& audio, | 65 int32_t AudioCoder::Encode(const AudioFrame& audio, |
| 74 int8_t* encodedData, | 66 int8_t* encodedData, |
| 75 size_t& encodedLengthInBytes) | 67 size_t& encodedLengthInBytes) { |
| 76 { | 68 // Fake a timestamp in case audio doesn't contain a correct timestamp. |
| 77 // 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 |
| 78 // Make a local copy of the audio frame since audio is const | 70 AudioFrame audioFrame; |
| 79 AudioFrame audioFrame; | 71 audioFrame.CopyFrom(audio); |
| 80 audioFrame.CopyFrom(audio); | 72 audioFrame.timestamp_ = _encodeTimestamp; |
| 81 audioFrame.timestamp_ = _encodeTimestamp; | 73 _encodeTimestamp += static_cast<uint32_t>(audioFrame.samples_per_channel_); |
| 82 _encodeTimestamp += static_cast<uint32_t>(audioFrame.samples_per_channel_); | |
| 83 | 74 |
| 84 // For any codec with a frame size that is longer than 10 ms the encoded | 75 // For any codec with a frame size that is longer than 10 ms the encoded |
| 85 // length in bytes should be zero until a a full frame has been encoded. | 76 // length in bytes should be zero until a a full frame has been encoded. |
| 86 _encodedLengthInBytes = 0; | 77 _encodedLengthInBytes = 0; |
| 87 if(_acm->Add10MsData((AudioFrame&)audioFrame) == -1) | 78 if (_acm->Add10MsData((AudioFrame&)audioFrame) == -1) { |
| 88 { | 79 return -1; |
| 89 return -1; | 80 } |
| 90 } | 81 _encodedData = encodedData; |
| 91 _encodedData = encodedData; | 82 encodedLengthInBytes = _encodedLengthInBytes; |
| 92 encodedLengthInBytes = _encodedLengthInBytes; | 83 return 0; |
| 93 return 0; | |
| 94 } | 84 } |
| 95 | 85 |
| 96 int32_t AudioCoder::SendData( | 86 int32_t AudioCoder::SendData(FrameType /* frameType */, |
| 97 FrameType /* frameType */, | 87 uint8_t /* payloadType */, |
| 98 uint8_t /* payloadType */, | 88 uint32_t /* timeStamp */, |
| 99 uint32_t /* timeStamp */, | 89 const uint8_t* payloadData, |
| 100 const uint8_t* payloadData, | 90 size_t payloadSize, |
| 101 size_t payloadSize, | 91 const RTPFragmentationHeader* /* fragmentation*/) { |
| 102 const RTPFragmentationHeader* /* fragmentation*/) | 92 memcpy(_encodedData, payloadData, sizeof(uint8_t) * payloadSize); |
| 103 { | 93 _encodedLengthInBytes = payloadSize; |
| 104 memcpy(_encodedData,payloadData,sizeof(uint8_t) * payloadSize); | 94 return 0; |
| 105 _encodedLengthInBytes = payloadSize; | |
| 106 return 0; | |
| 107 } | 95 } |
| 96 |
| 108 } // namespace webrtc | 97 } // namespace webrtc |
| OLD | NEW |