• 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 MODULES_AUDIO_CODING_CODECS_G711_AUDIO_ENCODER_PCM_H_
12 #define MODULES_AUDIO_CODING_CODECS_G711_AUDIO_ENCODER_PCM_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/units/time_delta.h"
20 #include "rtc_base/constructor_magic.h"
21 
22 namespace webrtc {
23 
24 class AudioEncoderPcm : public AudioEncoder {
25  public:
26   struct Config {
27    public:
28     bool IsOk() const;
29 
30     int frame_size_ms;
31     size_t num_channels;
32     int payload_type;
33 
34    protected:
ConfigConfig35     explicit Config(int pt)
36         : frame_size_ms(20), num_channels(1), payload_type(pt) {}
37   };
38 
39   ~AudioEncoderPcm() override;
40 
41   int SampleRateHz() const override;
42   size_t NumChannels() const override;
43   size_t Num10MsFramesInNextPacket() const override;
44   size_t Max10MsFramesInAPacket() const override;
45   int GetTargetBitrate() const override;
46   void Reset() override;
47   absl::optional<std::pair<TimeDelta, TimeDelta>> GetFrameLengthRange()
48       const override;
49 
50  protected:
51   AudioEncoderPcm(const Config& config, int sample_rate_hz);
52 
53   EncodedInfo EncodeImpl(uint32_t rtp_timestamp,
54                          rtc::ArrayView<const int16_t> audio,
55                          rtc::Buffer* encoded) override;
56 
57   virtual size_t EncodeCall(const int16_t* audio,
58                             size_t input_len,
59                             uint8_t* encoded) = 0;
60 
61   virtual size_t BytesPerSample() const = 0;
62 
63   // Used to set EncodedInfoLeaf::encoder_type in
64   // AudioEncoderPcm::EncodeImpl
65   virtual AudioEncoder::CodecType GetCodecType() const = 0;
66 
67  private:
68   const int sample_rate_hz_;
69   const size_t num_channels_;
70   const int payload_type_;
71   const size_t num_10ms_frames_per_packet_;
72   const size_t full_frame_samples_;
73   std::vector<int16_t> speech_buffer_;
74   uint32_t first_timestamp_in_buffer_;
75 };
76 
77 class AudioEncoderPcmA final : public AudioEncoderPcm {
78  public:
79   struct Config : public AudioEncoderPcm::Config {
ConfigConfig80     Config() : AudioEncoderPcm::Config(8) {}
81   };
82 
AudioEncoderPcmA(const Config & config)83   explicit AudioEncoderPcmA(const Config& config)
84       : AudioEncoderPcm(config, kSampleRateHz) {}
85 
86  protected:
87   size_t EncodeCall(const int16_t* audio,
88                     size_t input_len,
89                     uint8_t* encoded) override;
90 
91   size_t BytesPerSample() const override;
92 
93   AudioEncoder::CodecType GetCodecType() const override;
94 
95  private:
96   static const int kSampleRateHz = 8000;
97   RTC_DISALLOW_COPY_AND_ASSIGN(AudioEncoderPcmA);
98 };
99 
100 class AudioEncoderPcmU final : public AudioEncoderPcm {
101  public:
102   struct Config : public AudioEncoderPcm::Config {
ConfigConfig103     Config() : AudioEncoderPcm::Config(0) {}
104   };
105 
AudioEncoderPcmU(const Config & config)106   explicit AudioEncoderPcmU(const Config& config)
107       : AudioEncoderPcm(config, kSampleRateHz) {}
108 
109  protected:
110   size_t EncodeCall(const int16_t* audio,
111                     size_t input_len,
112                     uint8_t* encoded) override;
113 
114   size_t BytesPerSample() const override;
115 
116   AudioEncoder::CodecType GetCodecType() const override;
117 
118  private:
119   static const int kSampleRateHz = 8000;
120   RTC_DISALLOW_COPY_AND_ASSIGN(AudioEncoderPcmU);
121 };
122 
123 }  // namespace webrtc
124 
125 #endif  // MODULES_AUDIO_CODING_CODECS_G711_AUDIO_ENCODER_PCM_H_
126