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 MODULES_AUDIO_CODING_CODECS_ISAC_AUDIO_ENCODER_ISAC_T_H_
12 #define MODULES_AUDIO_CODING_CODECS_ISAC_AUDIO_ENCODER_ISAC_T_H_
13 
14 #include <utility>
15 #include <vector>
16 
17 #include "absl/types/optional.h"
18 #include "api/audio_codecs/audio_encoder.h"
19 #include "api/scoped_refptr.h"
20 #include "api/units/time_delta.h"
21 #include "rtc_base/constructor_magic.h"
22 #include "system_wrappers/include/field_trial.h"
23 
24 namespace webrtc {
25 
26 template <typename T>
27 class AudioEncoderIsacT final : public AudioEncoder {
28  public:
29   // Allowed combinations of sample rate, frame size, and bit rate are
30   //  - 16000 Hz, 30 ms, 10000-32000 bps
31   //  - 16000 Hz, 60 ms, 10000-32000 bps
32   //  - 32000 Hz, 30 ms, 10000-56000 bps (if T has super-wideband support)
33   struct Config {
34     bool IsOk() const;
35     int payload_type = 103;
36     int sample_rate_hz = 16000;
37     int frame_size_ms = 30;
38     int bit_rate = kDefaultBitRate;  // Limit on the short-term average bit
39                                      // rate, in bits/s.
40     int max_payload_size_bytes = -1;
41     int max_bit_rate = -1;
42   };
43 
44   explicit AudioEncoderIsacT(const Config& config);
45   ~AudioEncoderIsacT() override;
46 
47   int SampleRateHz() const override;
48   size_t NumChannels() const override;
49   size_t Num10MsFramesInNextPacket() const override;
50   size_t Max10MsFramesInAPacket() const override;
51   int GetTargetBitrate() const override;
52   void SetTargetBitrate(int target_bps) override;
53   void OnReceivedTargetAudioBitrate(int target_bps) override;
54   void OnReceivedUplinkBandwidth(
55       int target_audio_bitrate_bps,
56       absl::optional<int64_t> bwe_period_ms) override;
57   void OnReceivedUplinkAllocation(BitrateAllocationUpdate update) override;
58   void OnReceivedOverhead(size_t overhead_bytes_per_packet) override;
59   EncodedInfo EncodeImpl(uint32_t rtp_timestamp,
60                          rtc::ArrayView<const int16_t> audio,
61                          rtc::Buffer* encoded) override;
62   void Reset() override;
63   absl::optional<std::pair<TimeDelta, TimeDelta>> GetFrameLengthRange()
64       const override;
65 
66  private:
67   // This value is taken from STREAM_SIZE_MAX_60 for iSAC float (60 ms) and
68   // STREAM_MAXW16_60MS for iSAC fix (60 ms).
69   static const size_t kSufficientEncodeBufferSizeBytes = 400;
70 
71   static constexpr int kDefaultBitRate = 32000;
72   static constexpr int kMinBitrateBps = 10000;
MaxBitrateBps(int sample_rate_hz)73   static constexpr int MaxBitrateBps(int sample_rate_hz) {
74     return sample_rate_hz == 32000 ? 56000 : 32000;
75   }
76 
77   void SetTargetBitrate(int target_bps, bool subtract_per_packet_overhead);
78 
79   // Recreate the iSAC encoder instance with the given settings, and save them.
80   void RecreateEncoderInstance(const Config& config);
81 
82   Config config_;
83   typename T::instance_type* isac_state_ = nullptr;
84 
85   // Have we accepted input but not yet emitted it in a packet?
86   bool packet_in_progress_ = false;
87 
88   // Timestamp of the first input of the currently in-progress packet.
89   uint32_t packet_timestamp_;
90 
91   // Timestamp of the previously encoded packet.
92   uint32_t last_encoded_timestamp_;
93 
94   // Cache the value of the "WebRTC-SendSideBwe-WithOverhead" field trial.
95   const bool send_side_bwe_with_overhead_ =
96       field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead");
97 
98   // When we send a packet, expect this many bytes of headers to be added to it.
99   // Start out with a reasonable default that we can use until we receive a real
100   // value.
101   DataSize overhead_per_packet_ = DataSize::Bytes(28);
102 
103   RTC_DISALLOW_COPY_AND_ASSIGN(AudioEncoderIsacT);
104 };
105 
106 }  // namespace webrtc
107 
108 #endif  // MODULES_AUDIO_CODING_CODECS_ISAC_AUDIO_ENCODER_ISAC_T_H_
109