xref: /aosp_15_r20/external/webrtc/modules/audio_coding/codecs/g722/audio_encoder_g722.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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_G722_AUDIO_ENCODER_G722_H_
12 #define MODULES_AUDIO_CODING_CODECS_G722_AUDIO_ENCODER_G722_H_
13 
14 #include <memory>
15 #include <utility>
16 
17 #include "absl/types/optional.h"
18 #include "api/audio_codecs/audio_encoder.h"
19 #include "api/audio_codecs/g722/audio_encoder_g722_config.h"
20 #include "api/units/time_delta.h"
21 #include "modules/audio_coding/codecs/g722/g722_interface.h"
22 #include "rtc_base/buffer.h"
23 
24 namespace webrtc {
25 
26 class AudioEncoderG722Impl final : public AudioEncoder {
27  public:
28   AudioEncoderG722Impl(const AudioEncoderG722Config& config, int payload_type);
29   ~AudioEncoderG722Impl() override;
30 
31   AudioEncoderG722Impl(const AudioEncoderG722Impl&) = delete;
32   AudioEncoderG722Impl& operator=(const AudioEncoderG722Impl&) = delete;
33 
34   int SampleRateHz() const override;
35   size_t NumChannels() const override;
36   int RtpTimestampRateHz() const override;
37   size_t Num10MsFramesInNextPacket() const override;
38   size_t Max10MsFramesInAPacket() const override;
39   int GetTargetBitrate() const override;
40   void Reset() override;
41   absl::optional<std::pair<TimeDelta, TimeDelta>> GetFrameLengthRange()
42       const override;
43 
44  protected:
45   EncodedInfo EncodeImpl(uint32_t rtp_timestamp,
46                          rtc::ArrayView<const int16_t> audio,
47                          rtc::Buffer* encoded) override;
48 
49  private:
50   // The encoder state for one channel.
51   struct EncoderState {
52     G722EncInst* encoder;
53     std::unique_ptr<int16_t[]> speech_buffer;  // Queued up for encoding.
54     rtc::Buffer encoded_buffer;                // Already encoded.
55     EncoderState();
56     ~EncoderState();
57   };
58 
59   size_t SamplesPerChannel() const;
60 
61   const size_t num_channels_;
62   const int payload_type_;
63   const size_t num_10ms_frames_per_packet_;
64   size_t num_10ms_frames_buffered_;
65   uint32_t first_timestamp_in_buffer_;
66   const std::unique_ptr<EncoderState[]> encoders_;
67   rtc::Buffer interleave_buffer_;
68 };
69 
70 }  // namespace webrtc
71 #endif  // MODULES_AUDIO_CODING_CODECS_G722_AUDIO_ENCODER_G722_H_
72