2011-07-07 08:21:25 +00:00
|
|
|
/*
|
2012-04-12 11:02:38 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-07-07 08:21:25 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-07-12 08:28:10 +00:00
|
|
|
#include "webrtc/common_types.h"
|
2015-11-04 08:31:52 +01:00
|
|
|
#include "webrtc/modules/include/module_common_types.h"
|
2013-07-12 08:28:10 +00:00
|
|
|
#include "webrtc/modules/utility/source/coder.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
2013-04-09 13:32:55 +00:00
|
|
|
AudioCoder::AudioCoder(uint32_t instanceID)
|
2012-06-19 14:56:50 +00:00
|
|
|
: _acm(AudioCodingModule::Create(instanceID)),
|
2011-07-07 08:21:25 +00:00
|
|
|
_receiveCodec(),
|
|
|
|
|
_encodeTimestamp(0),
|
|
|
|
|
_encodedData(NULL),
|
|
|
|
|
_encodedLengthInBytes(0),
|
|
|
|
|
_decodeTimestamp(0)
|
|
|
|
|
{
|
|
|
|
|
_acm->InitializeReceiver();
|
|
|
|
|
_acm->RegisterTransportCallback(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AudioCoder::~AudioCoder()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-06 12:22:38 -07:00
|
|
|
int32_t AudioCoder::SetEncodeCodec(const CodecInst& codecInst) {
|
|
|
|
|
const bool success = codec_manager_.RegisterEncoder(codecInst) &&
|
|
|
|
|
codec_manager_.MakeEncoder(&rent_a_codec_, _acm.get());
|
|
|
|
|
return success ? 0 : -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-04-06 12:22:38 -07:00
|
|
|
int32_t AudioCoder::SetDecodeCodec(const CodecInst& codecInst) {
|
|
|
|
|
if (_acm->RegisterReceiveCodec(
|
|
|
|
|
codecInst, [&] { return rent_a_codec_.RentIsacDecoder(); }) == -1) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
memcpy(&_receiveCodec, &codecInst, sizeof(CodecInst));
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-09 13:32:55 +00:00
|
|
|
int32_t AudioCoder::Decode(AudioFrame& decodedAudio,
|
|
|
|
|
uint32_t sampFreqHz,
|
|
|
|
|
const int8_t* incomingPayload,
|
Use size_t more consistently for packet/payload lengths.
See design doc at https://docs.google.com/a/chromium.org/document/d/1I6nmE9D_BmCY-IoV6MDPY2V6WYpEI-dg2apWXTfZyUI/edit?usp=sharing for more information.
This CL was reviewed and approved in pieces in the following CLs:
https://webrtc-codereview.appspot.com/24209004/
https://webrtc-codereview.appspot.com/24229004/
https://webrtc-codereview.appspot.com/24259004/
https://webrtc-codereview.appspot.com/25109004/
https://webrtc-codereview.appspot.com/26099004/
https://webrtc-codereview.appspot.com/27069004/
https://webrtc-codereview.appspot.com/27969004/
https://webrtc-codereview.appspot.com/27989004/
https://webrtc-codereview.appspot.com/29009004/
https://webrtc-codereview.appspot.com/30929004/
https://webrtc-codereview.appspot.com/30939004/
https://webrtc-codereview.appspot.com/31999004/
Committing as TBR to the original reviewers.
BUG=chromium:81439
TEST=none
TBR=pthatcher,henrik.lundin,tina.legrand,stefan,tkchin,glaznev,kjellander,perkj,mflodman,henrika,asapersson,niklas.enbom
Review URL: https://webrtc-codereview.appspot.com/23129004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7726 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-11-20 22:28:14 +00:00
|
|
|
size_t payloadLength)
|
2011-07-07 08:21:25 +00:00
|
|
|
{
|
|
|
|
|
if (payloadLength > 0)
|
|
|
|
|
{
|
2013-04-09 13:32:55 +00:00
|
|
|
const uint8_t payloadType = _receiveCodec.pltype;
|
2011-07-07 08:21:25 +00:00
|
|
|
_decodeTimestamp += _receiveCodec.pacsize;
|
2013-04-09 13:32:55 +00:00
|
|
|
if(_acm->IncomingPayload((const uint8_t*) incomingPayload,
|
2011-07-07 08:21:25 +00:00
|
|
|
payloadLength,
|
|
|
|
|
payloadType,
|
|
|
|
|
_decodeTimestamp) == -1)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-09 13:32:55 +00:00
|
|
|
return _acm->PlayoutData10Ms((uint16_t)sampFreqHz, &decodedAudio);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-09 13:32:55 +00:00
|
|
|
int32_t AudioCoder::PlayoutData(AudioFrame& decodedAudio,
|
|
|
|
|
uint16_t& sampFreqHz)
|
2011-07-07 08:21:25 +00:00
|
|
|
{
|
2013-02-21 10:27:48 +00:00
|
|
|
return _acm->PlayoutData10Ms(sampFreqHz, &decodedAudio);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-09 13:32:55 +00:00
|
|
|
int32_t AudioCoder::Encode(const AudioFrame& audio,
|
|
|
|
|
int8_t* encodedData,
|
Use size_t more consistently for packet/payload lengths.
See design doc at https://docs.google.com/a/chromium.org/document/d/1I6nmE9D_BmCY-IoV6MDPY2V6WYpEI-dg2apWXTfZyUI/edit?usp=sharing for more information.
This CL was reviewed and approved in pieces in the following CLs:
https://webrtc-codereview.appspot.com/24209004/
https://webrtc-codereview.appspot.com/24229004/
https://webrtc-codereview.appspot.com/24259004/
https://webrtc-codereview.appspot.com/25109004/
https://webrtc-codereview.appspot.com/26099004/
https://webrtc-codereview.appspot.com/27069004/
https://webrtc-codereview.appspot.com/27969004/
https://webrtc-codereview.appspot.com/27989004/
https://webrtc-codereview.appspot.com/29009004/
https://webrtc-codereview.appspot.com/30929004/
https://webrtc-codereview.appspot.com/30939004/
https://webrtc-codereview.appspot.com/31999004/
Committing as TBR to the original reviewers.
BUG=chromium:81439
TEST=none
TBR=pthatcher,henrik.lundin,tina.legrand,stefan,tkchin,glaznev,kjellander,perkj,mflodman,henrika,asapersson,niklas.enbom
Review URL: https://webrtc-codereview.appspot.com/23129004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7726 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-11-20 22:28:14 +00:00
|
|
|
size_t& encodedLengthInBytes)
|
2011-07-07 08:21:25 +00:00
|
|
|
{
|
|
|
|
|
// Fake a timestamp in case audio doesn't contain a correct timestamp.
|
|
|
|
|
// Make a local copy of the audio frame since audio is const
|
2013-01-22 04:44:30 +00:00
|
|
|
AudioFrame audioFrame;
|
|
|
|
|
audioFrame.CopyFrom(audio);
|
2012-05-02 23:56:37 +00:00
|
|
|
audioFrame.timestamp_ = _encodeTimestamp;
|
Match existing type usage better.
This makes a variety of small changes to synchronize bits of code using different types, remove useless code or casts, and add explicit casts in some places previously doing implicit ones. For example:
* Change a few type declarations to better match how the majority of code uses those objects.
* Eliminate "< 0" check for unsigned values.
* Replace "(float)sin(x)", where |x| is also a float, with "sinf(x)", and similar.
* Add casts to uint32_t in many places timestamps were used and the existing code stored signed values into the unsigned objects.
* Remove downcasts when the results would be passed to a larger type, e.g. calling "foo((int16_t)x)" with an int |x| when foo() takes an int instead of an int16_t.
* Similarly, add casts when passing a larger type to a function taking a smaller one.
* Add casts to int16_t when doing something like "int16_t = int16_t + int16_t" as the "+" operation would implicitly upconvert to int, and similar.
* Use "false" instead of "0" for setting a bool.
* Shift a few temp types when doing a multi-stage calculation involving typecasts, so as to put the most logical/semantically correct type possible into the temps. For example, when doing "int foo = int + int; size_t bar = (size_t)foo + size_t;", we might change |foo| to a size_t and move the cast if it makes more sense for |foo| to be represented as a size_t.
BUG=none
R=andrew@webrtc.org, asapersson@webrtc.org, henrika@webrtc.org, juberti@webrtc.org, kwiberg@webrtc.org
TBR=andrew, asapersson, henrika
Review URL: https://codereview.webrtc.org/1168753002
Cr-Commit-Position: refs/heads/master@{#9419}
2015-06-11 12:55:50 -07:00
|
|
|
_encodeTimestamp += static_cast<uint32_t>(audioFrame.samples_per_channel_);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
// For any codec with a frame size that is longer than 10 ms the encoded
|
|
|
|
|
// length in bytes should be zero until a a full frame has been encoded.
|
|
|
|
|
_encodedLengthInBytes = 0;
|
|
|
|
|
if(_acm->Add10MsData((AudioFrame&)audioFrame) == -1)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
_encodedData = encodedData;
|
|
|
|
|
encodedLengthInBytes = _encodedLengthInBytes;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-09 13:32:55 +00:00
|
|
|
int32_t AudioCoder::SendData(
|
2011-07-07 08:21:25 +00:00
|
|
|
FrameType /* frameType */,
|
2013-04-09 13:32:55 +00:00
|
|
|
uint8_t /* payloadType */,
|
|
|
|
|
uint32_t /* timeStamp */,
|
|
|
|
|
const uint8_t* payloadData,
|
Use size_t more consistently for packet/payload lengths.
See design doc at https://docs.google.com/a/chromium.org/document/d/1I6nmE9D_BmCY-IoV6MDPY2V6WYpEI-dg2apWXTfZyUI/edit?usp=sharing for more information.
This CL was reviewed and approved in pieces in the following CLs:
https://webrtc-codereview.appspot.com/24209004/
https://webrtc-codereview.appspot.com/24229004/
https://webrtc-codereview.appspot.com/24259004/
https://webrtc-codereview.appspot.com/25109004/
https://webrtc-codereview.appspot.com/26099004/
https://webrtc-codereview.appspot.com/27069004/
https://webrtc-codereview.appspot.com/27969004/
https://webrtc-codereview.appspot.com/27989004/
https://webrtc-codereview.appspot.com/29009004/
https://webrtc-codereview.appspot.com/30929004/
https://webrtc-codereview.appspot.com/30939004/
https://webrtc-codereview.appspot.com/31999004/
Committing as TBR to the original reviewers.
BUG=chromium:81439
TEST=none
TBR=pthatcher,henrik.lundin,tina.legrand,stefan,tkchin,glaznev,kjellander,perkj,mflodman,henrika,asapersson,niklas.enbom
Review URL: https://webrtc-codereview.appspot.com/23129004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7726 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-11-20 22:28:14 +00:00
|
|
|
size_t payloadSize,
|
2011-07-07 08:21:25 +00:00
|
|
|
const RTPFragmentationHeader* /* fragmentation*/)
|
|
|
|
|
{
|
2013-04-09 13:32:55 +00:00
|
|
|
memcpy(_encodedData,payloadData,sizeof(uint8_t) * payloadSize);
|
2011-07-07 08:21:25 +00:00
|
|
|
_encodedLengthInBytes = payloadSize;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2013-07-03 15:12:26 +00:00
|
|
|
} // namespace webrtc
|