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 #include "rtc_base/constructor_magic.h"
24 
25 namespace webrtc {
26 
27 class AudioEncoderG722Impl final : public AudioEncoder {
28  public:
29   AudioEncoderG722Impl(const AudioEncoderG722Config& config, int payload_type);
30   ~AudioEncoderG722Impl() override;
31 
32   int SampleRateHz() const override;
33   size_t NumChannels() const override;
34   int RtpTimestampRateHz() const override;
35   size_t Num10MsFramesInNextPacket() const override;
36   size_t Max10MsFramesInAPacket() const override;
37   int GetTargetBitrate() const override;
38   void Reset() override;
39   absl::optional<std::pair<TimeDelta, TimeDelta>> GetFrameLengthRange()
40       const override;
41 
42  protected:
43   EncodedInfo EncodeImpl(uint32_t rtp_timestamp,
44                          rtc::ArrayView<const int16_t> audio,
45                          rtc::Buffer* encoded) override;
46 
47  private:
48   // The encoder state for one channel.
49   struct EncoderState {
50     G722EncInst* encoder;
51     std::unique_ptr<int16_t[]> speech_buffer;  // Queued up for encoding.
52     rtc::Buffer encoded_buffer;                // Already encoded.
53     EncoderState();
54     ~EncoderState();
55   };
56 
57   size_t SamplesPerChannel() const;
58 
59   const size_t num_channels_;
60   const int payload_type_;
61   const size_t num_10ms_frames_per_packet_;
62   size_t num_10ms_frames_buffered_;
63   uint32_t first_timestamp_in_buffer_;
64   const std::unique_ptr<EncoderState[]> encoders_;
65   rtc::Buffer interleave_buffer_;
66   RTC_DISALLOW_COPY_AND_ASSIGN(AudioEncoderG722Impl);
67 };
68 
69 }  // namespace webrtc
70 #endif  // MODULES_AUDIO_CODING_CODECS_G722_AUDIO_ENCODER_G722_H_
71