• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
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 
11 #ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_
12 #define WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_
13 
14 #include <vector>
15 
16 #include "webrtc/base/constructormagic.h"
17 #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h"
18 #include "webrtc/modules/audio_coding/codecs/audio_encoder.h"
19 
20 namespace webrtc {
21 
22 struct CodecInst;
23 
24 class AudioEncoderOpus final : public AudioEncoder {
25  public:
26   enum ApplicationMode {
27     kVoip = 0,
28     kAudio = 1,
29   };
30 
31   struct Config {
32     bool IsOk() const;
33     int frame_size_ms = 20;
34     size_t num_channels = 1;
35     int payload_type = 120;
36     ApplicationMode application = kVoip;
37     int bitrate_bps = 64000;
38     bool fec_enabled = false;
39     int max_playback_rate_hz = 48000;
40     int complexity = kDefaultComplexity;
41     bool dtx_enabled = false;
42 
43    private:
44 #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_ARCH_ARM)
45     // If we are on Android, iOS and/or ARM, use a lower complexity setting as
46     // default, to save encoder complexity.
47     static const int kDefaultComplexity = 5;
48 #else
49     static const int kDefaultComplexity = 9;
50 #endif
51   };
52 
53   explicit AudioEncoderOpus(const Config& config);
54   explicit AudioEncoderOpus(const CodecInst& codec_inst);
55   ~AudioEncoderOpus() override;
56 
57   size_t MaxEncodedBytes() const override;
58   int SampleRateHz() const override;
59   size_t NumChannels() const override;
60   size_t Num10MsFramesInNextPacket() const override;
61   size_t Max10MsFramesInAPacket() const override;
62   int GetTargetBitrate() const override;
63 
64   EncodedInfo EncodeInternal(uint32_t rtp_timestamp,
65                              rtc::ArrayView<const int16_t> audio,
66                              size_t max_encoded_bytes,
67                              uint8_t* encoded) override;
68 
69   void Reset() override;
70   bool SetFec(bool enable) override;
71 
72   // Set Opus DTX. Once enabled, Opus stops transmission, when it detects voice
73   // being inactive. During that, it still sends 2 packets (one for content, one
74   // for signaling) about every 400 ms.
75   bool SetDtx(bool enable) override;
76 
77   bool SetApplication(Application application) override;
78   void SetMaxPlaybackRate(int frequency_hz) override;
79   void SetProjectedPacketLossRate(double fraction) override;
80   void SetTargetBitrate(int target_bps) override;
81 
82   // Getters for testing.
packet_loss_rate()83   double packet_loss_rate() const { return packet_loss_rate_; }
application()84   ApplicationMode application() const { return config_.application; }
dtx_enabled()85   bool dtx_enabled() const { return config_.dtx_enabled; }
86 
87  private:
88   size_t Num10msFramesPerPacket() const;
89   size_t SamplesPer10msFrame() const;
90   bool RecreateEncoderInstance(const Config& config);
91 
92   Config config_;
93   double packet_loss_rate_;
94   std::vector<int16_t> input_buffer_;
95   OpusEncInst* inst_;
96   uint32_t first_timestamp_in_buffer_;
97   RTC_DISALLOW_COPY_AND_ASSIGN(AudioEncoderOpus);
98 };
99 
100 }  // namespace webrtc
101 
102 #endif  // WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_
103