1 /*
2  *  Copyright (c) 2019 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_OPUS_AUDIO_ENCODER_MULTI_CHANNEL_OPUS_IMPL_H_
12 #define MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_MULTI_CHANNEL_OPUS_IMPL_H_
13 
14 #include <memory>
15 #include <utility>
16 #include <vector>
17 
18 #include "absl/types/optional.h"
19 #include "api/audio_codecs/audio_encoder.h"
20 #include "api/audio_codecs/audio_format.h"
21 #include "api/audio_codecs/opus/audio_encoder_multi_channel_opus_config.h"
22 #include "api/units/time_delta.h"
23 #include "modules/audio_coding/codecs/opus/opus_interface.h"
24 
25 namespace webrtc {
26 
27 class RtcEventLog;
28 
29 class AudioEncoderMultiChannelOpusImpl final : public AudioEncoder {
30  public:
31   AudioEncoderMultiChannelOpusImpl(
32       const AudioEncoderMultiChannelOpusConfig& config,
33       int payload_type);
34   ~AudioEncoderMultiChannelOpusImpl() override;
35 
36   AudioEncoderMultiChannelOpusImpl(const AudioEncoderMultiChannelOpusImpl&) =
37       delete;
38   AudioEncoderMultiChannelOpusImpl& operator=(
39       const AudioEncoderMultiChannelOpusImpl&) = delete;
40 
41   // Static interface for use by BuiltinAudioEncoderFactory.
GetPayloadName()42   static constexpr const char* GetPayloadName() { return "multiopus"; }
43   static absl::optional<AudioCodecInfo> QueryAudioEncoder(
44       const SdpAudioFormat& format);
45 
46   int SampleRateHz() const override;
47   size_t NumChannels() const override;
48   size_t Num10MsFramesInNextPacket() const override;
49   size_t Max10MsFramesInAPacket() const override;
50   int GetTargetBitrate() const override;
51 
52   void Reset() override;
53   absl::optional<std::pair<TimeDelta, TimeDelta>> GetFrameLengthRange()
54       const override;
55 
56  protected:
57   EncodedInfo EncodeImpl(uint32_t rtp_timestamp,
58                          rtc::ArrayView<const int16_t> audio,
59                          rtc::Buffer* encoded) override;
60 
61  private:
62   static absl::optional<AudioEncoderMultiChannelOpusConfig> SdpToConfig(
63       const SdpAudioFormat& format);
64   static AudioCodecInfo QueryAudioEncoder(
65       const AudioEncoderMultiChannelOpusConfig& config);
66   static std::unique_ptr<AudioEncoder> MakeAudioEncoder(
67       const AudioEncoderMultiChannelOpusConfig&,
68       int payload_type);
69 
70   size_t Num10msFramesPerPacket() const;
71   size_t SamplesPer10msFrame() const;
72   size_t SufficientOutputBufferSize() const;
73   bool RecreateEncoderInstance(
74       const AudioEncoderMultiChannelOpusConfig& config);
75   void SetFrameLength(int frame_length_ms);
76   void SetNumChannelsToEncode(size_t num_channels_to_encode);
77   void SetProjectedPacketLossRate(float fraction);
78 
79   AudioEncoderMultiChannelOpusConfig config_;
80   const int payload_type_;
81   std::vector<int16_t> input_buffer_;
82   OpusEncInst* inst_;
83   uint32_t first_timestamp_in_buffer_;
84   size_t num_channels_to_encode_;
85   int next_frame_length_ms_;
86 
87   friend struct AudioEncoderMultiChannelOpus;
88 };
89 
90 }  // namespace webrtc
91 
92 #endif  // MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_MULTI_CHANNEL_OPUS_IMPL_H_
93