webrtc_m130/modules/audio_coding/codecs/opus/audio_encoder_opus.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

173 lines
6.6 KiB
C
Raw Normal View History

/*
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
*
* 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.
*/
#ifndef MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_
#define MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include "api/audio_codecs/audio_encoder.h"
#include "api/audio_codecs/audio_format.h"
#include "api/audio_codecs/opus/audio_encoder_opus_config.h"
#include "api/optional.h"
#include "common_audio/smoothing_filter.h"
#include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor.h"
#include "modules/audio_coding/codecs/opus/opus_interface.h"
#include "rtc_base/constructormagic.h"
#include "rtc_base/protobuf_utils.h"
namespace webrtc {
class RtcEventLog;
struct CodecInst;
class AudioEncoderOpusImpl final : public AudioEncoder {
public:
static AudioEncoderOpusConfig CreateConfig(const CodecInst& codec_inst);
// Returns empty if the current bitrate falls within the hysteresis window,
// defined by complexity_threshold_bps +/- complexity_threshold_window_bps.
// Otherwise, returns the current complexity depending on whether the
// current bitrate is above or below complexity_threshold_bps.
static rtc::Optional<int> GetNewComplexity(
const AudioEncoderOpusConfig& config);
using AudioNetworkAdaptorCreator =
std::function<std::unique_ptr<AudioNetworkAdaptor>(const std::string&,
RtcEventLog*)>;
AudioEncoderOpusImpl(const AudioEncoderOpusConfig& config, int payload_type);
// Dependency injection for testing.
AudioEncoderOpusImpl(
const AudioEncoderOpusConfig& config,
int payload_type,
const AudioNetworkAdaptorCreator& audio_network_adaptor_creator,
std::unique_ptr<SmoothingFilter> bitrate_smoother);
explicit AudioEncoderOpusImpl(const CodecInst& codec_inst);
AudioEncoderOpusImpl(int payload_type, const SdpAudioFormat& format);
~AudioEncoderOpusImpl() override;
// Static interface for use by BuiltinAudioEncoderFactory.
static constexpr const char* GetPayloadName() { return "opus"; }
static rtc::Optional<AudioCodecInfo> QueryAudioEncoder(
const SdpAudioFormat& format);
int SampleRateHz() const override;
size_t NumChannels() const override;
Update a ton of audio code to use size_t more correctly and in general reduce use of int16_t/uint16_t. This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects. This was be reviewed and approved in pieces: https://codereview.webrtc.org/1224093003 https://codereview.webrtc.org/1224123002 https://codereview.webrtc.org/1224163002 https://codereview.webrtc.org/1225133003 https://codereview.webrtc.org/1225173002 https://codereview.webrtc.org/1227163003 https://codereview.webrtc.org/1227203003 https://codereview.webrtc.org/1227213002 https://codereview.webrtc.org/1227893002 https://codereview.webrtc.org/1228793004 https://codereview.webrtc.org/1228803003 https://codereview.webrtc.org/1228823002 https://codereview.webrtc.org/1228823003 https://codereview.webrtc.org/1228843002 https://codereview.webrtc.org/1230693002 https://codereview.webrtc.org/1231713002 The change is being landed as TBR to all the folks who reviewed the above. BUG=chromium:81439 TEST=none R=andrew@webrtc.org, pbos@webrtc.org TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher Review URL: https://codereview.webrtc.org/1230503003 . Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
size_t Num10MsFramesInNextPacket() const override;
size_t Max10MsFramesInAPacket() const override;
int GetTargetBitrate() const override;
void Reset() override;
bool SetFec(bool enable) override;
// Set Opus DTX. Once enabled, Opus stops transmission, when it detects
// voice being inactive. During that, it still sends 2 packets (one for
// content, one for signaling) about every 400 ms.
bool SetDtx(bool enable) override;
bool GetDtx() const override;
bool SetApplication(Application application) override;
void SetMaxPlaybackRate(int frequency_hz) override;
bool EnableAudioNetworkAdaptor(const std::string& config_string,
RtcEventLog* event_log) override;
void DisableAudioNetworkAdaptor() override;
void OnReceivedUplinkPacketLossFraction(
float uplink_packet_loss_fraction) override;
void OnReceivedUplinkRecoverablePacketLossFraction(
float uplink_recoverable_packet_loss_fraction) override;
void OnReceivedUplinkBandwidth(
int target_audio_bitrate_bps,
rtc::Optional<int64_t> bwe_period_ms) override;
void OnReceivedRtt(int rtt_ms) override;
void OnReceivedOverhead(size_t overhead_bytes_per_packet) override;
void SetReceiverFrameLengthRange(int min_frame_length_ms,
int max_frame_length_ms) override;
ANAStats GetANAStats() const override;
rtc::ArrayView<const int> supported_frame_lengths_ms() const {
return config_.supported_frame_lengths_ms;
}
// Getters for testing.
float packet_loss_rate() const { return packet_loss_rate_; }
AudioEncoderOpusConfig::ApplicationMode application() const {
return config_.application;
}
bool fec_enabled() const { return config_.fec_enabled; }
size_t num_channels_to_encode() const { return num_channels_to_encode_; }
int next_frame_length_ms() const { return next_frame_length_ms_; }
protected:
EncodedInfo EncodeImpl(uint32_t rtp_timestamp,
rtc::ArrayView<const int16_t> audio,
rtc::Buffer* encoded) override;
private:
class PacketLossFractionSmoother;
static rtc::Optional<AudioEncoderOpusConfig> SdpToConfig(
const SdpAudioFormat& format);
static void AppendSupportedEncoders(std::vector<AudioCodecSpec>* specs);
static AudioCodecInfo QueryAudioEncoder(const AudioEncoderOpusConfig& config);
static std::unique_ptr<AudioEncoder> MakeAudioEncoder(
const AudioEncoderOpusConfig&,
int payload_type);
size_t Num10msFramesPerPacket() const;
size_t SamplesPer10msFrame() const;
size_t SufficientOutputBufferSize() const;
bool RecreateEncoderInstance(const AudioEncoderOpusConfig& config);
void SetFrameLength(int frame_length_ms);
void SetNumChannelsToEncode(size_t num_channels_to_encode);
void SetProjectedPacketLossRate(float fraction);
// TODO(minyue): remove "override" when we can deprecate
// |AudioEncoder::SetTargetBitrate|.
void SetTargetBitrate(int target_bps) override;
void ApplyAudioNetworkAdaptor();
std::unique_ptr<AudioNetworkAdaptor> DefaultAudioNetworkAdaptorCreator(
Reland of Loosening the coupling between WebRTC and //third_party/protobuf (patchset #1 id:1 of https://codereview.webrtc.org/2786363002/ ) Reason for revert: Trying to re-land after solving some related issues. There are no changes compared to the original CL. Original issue's description: > Revert of Loosening the coupling between WebRTC and //third_party/protobuf (patchset #16 id:300001 of https://codereview.webrtc.org/2747863003/ ) > > Reason for revert: > I will try to reland next week because it is causing some problems. > > Original issue's description: > > To accommodate some downstream WebRTC users we need to loosen > > the coupling between our code and the //third_party/protobuf. > > > > This includes using typedefs to define strings instead of > > assuming std::string. > > > > After this refactoring it will be possible to link with other > > protobuf implementations than the current one. > > > > We moved the PRESUBMIT check to another CL [1]. The goal of this > > presubmit is to avoid the direct usage of google::protobuf outside > > of the webrtc/base/protobuf_utils.h header file. > > > > [1] - https://codereview.webrtc.org/2753823003/ > > > > BUG=webrtc:7340 > > NOTRY=True > > > > Review-Url: https://codereview.webrtc.org/2747863003 > > Cr-Commit-Position: refs/heads/master@{#17466} > > Committed: https://chromium.googlesource.com/external/webrtc/+/16ab93b952f9e8268f2e663ffe49548e8043d5af > > TBR=kjellander@webrtc.org,henrik.lundin@webrtc.org,kwiberg@webrtc.org,michaelt@webrtc.org,peah@webrtc.org > # Not skipping CQ checks because original CL landed more than 1 days ago. > BUG=webrtc:7340 > > Review-Url: https://codereview.webrtc.org/2786363002 > Cr-Commit-Position: refs/heads/master@{#17483} > Committed: https://chromium.googlesource.com/external/webrtc/+/d00aad5eb2fa5a7b5aeda714f7702b50cd26ee28 TBR=kjellander@webrtc.org,henrik.lundin@webrtc.org,kwiberg@webrtc.org,michaelt@webrtc.org,peah@webrtc.org # Not skipping CQ checks because original CL landed more than 1 days ago. BUG=webrtc:7340 NOTRY=True Review-Url: https://codereview.webrtc.org/2791963003 Cr-Commit-Position: refs/heads/master@{#17584}
2017-04-07 00:59:12 -07:00
const ProtoString& config_string,
RtcEventLog* event_log) const;
void MaybeUpdateUplinkBandwidth();
AudioEncoderOpusConfig config_;
const int payload_type_;
const bool send_side_bwe_with_overhead_;
float packet_loss_rate_;
std::vector<int16_t> input_buffer_;
OpusEncInst* inst_;
uint32_t first_timestamp_in_buffer_;
size_t num_channels_to_encode_;
int next_frame_length_ms_;
int complexity_;
std::unique_ptr<PacketLossFractionSmoother> packet_loss_fraction_smoother_;
const AudioNetworkAdaptorCreator audio_network_adaptor_creator_;
std::unique_ptr<AudioNetworkAdaptor> audio_network_adaptor_;
rtc::Optional<size_t> overhead_bytes_per_packet_;
const std::unique_ptr<SmoothingFilter> bitrate_smoother_;
rtc::Optional<int64_t> bitrate_smoother_last_update_time_;
int consecutive_dtx_frames_;
friend struct AudioEncoderOpus;
RTC_DISALLOW_COPY_AND_ASSIGN(AudioEncoderOpusImpl);
};
} // namespace webrtc
#endif // MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_