AudioEncoder: add method MaxEncodedBytes

Added method AudioEncoder::MaxEncodedBytes() and provided implementations in derived encoders. This method returns the number of bytes that can be produced by the encoder at each Encode() call.
Unit tests were updated to use the new method.
Buffer allocation was not changed in AudioCodingModuleImpl::Encode(). It will be done after additional investigation.
Other refactoring work that was done, that may not be obvious why:
1. Moved some code into AudioEncoderCng::EncodePassive() to make it more consistent with EncodeActive().
2. Changed the order of NumChannels() and  RtpTimestampRateHz() declarations in AudioEncoderG722 and AudioEncoderCopyRed classes. It just bothered me that the order was not the same as in AudioEncoder class and its other derived classes.

R=kwiberg@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/40259005

Cr-Commit-Position: refs/heads/master@{#8671}
git-svn-id: http://webrtc.googlecode.com/svn/trunk@8671 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
jmarusic@webrtc.org
2015-03-10 15:41:26 +00:00
parent d7452a0168
commit 51ccf37638
19 changed files with 151 additions and 60 deletions

View File

@ -111,6 +111,16 @@ int AudioEncoderOpus::NumChannels() const {
return num_channels_;
}
size_t AudioEncoderOpus::MaxEncodedBytes() const {
// Calculate the number of bytes we expect the encoder to produce,
// then multiply by two to give a wide margin for error.
int frame_size_ms = num_10ms_frames_per_packet_ * 10;
int bytes_per_millisecond = bitrate_bps_ / (1000 * 8) + 1;
size_t approx_encoded_bytes =
static_cast<size_t>(frame_size_ms * bytes_per_millisecond);
return 2 * approx_encoded_bytes;
}
int AudioEncoderOpus::Num10MsFramesInNextPacket() const {
return num_10ms_frames_per_packet_;
}
@ -120,10 +130,9 @@ int AudioEncoderOpus::Max10MsFramesInAPacket() const {
}
void AudioEncoderOpus::SetTargetBitrate(int bits_per_second) {
CHECK_EQ(WebRtcOpus_SetBitRate(
inst_, std::max(std::min(bits_per_second, kMaxBitrateBps),
kMinBitrateBps)),
0);
bitrate_bps_ = std::max(std::min(bits_per_second, kMaxBitrateBps),
kMinBitrateBps);
CHECK_EQ(WebRtcOpus_SetBitRate(inst_, bitrate_bps_), 0);
}
void AudioEncoderOpus::SetProjectedPacketLossRate(double fraction) {

View File

@ -47,10 +47,12 @@ class AudioEncoderOpus final : public AudioEncoder {
int SampleRateHz() const override;
int NumChannels() const override;
size_t MaxEncodedBytes() const override;
int Num10MsFramesInNextPacket() const override;
int Max10MsFramesInAPacket() const override;
void SetTargetBitrate(int bits_per_second) override;
void SetProjectedPacketLossRate(double fraction) override;
double packet_loss_rate() const { return packet_loss_rate_; }
ApplicationMode application() const { return application_; }
@ -66,6 +68,7 @@ class AudioEncoderOpus final : public AudioEncoder {
const int num_channels_;
const int payload_type_;
const ApplicationMode application_;
int bitrate_bps_;
const int samples_per_10ms_frame_;
std::vector<int16_t> input_buffer_;
OpusEncInst* inst_;