2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/modules/audio_coding/codecs/opus/interface/audio_encoder_opus.h"
13 #include "webrtc/modules/audio_coding/codecs/opus/interface/opus_interface.h"
19 // We always encode at 48 kHz.
20 const int kSampleRateHz = 48000;
22 int DivExact(int a, int b) {
27 int16_t ClampInt16(size_t x) {
28 return static_cast<int16_t>(
29 std::min(x, static_cast<size_t>(std::numeric_limits<int16_t>::max())));
32 int16_t CastInt16(size_t x) {
33 DCHECK_LE(x, static_cast<size_t>(std::numeric_limits<int16_t>::max()));
34 return static_cast<int16_t>(x);
39 AudioEncoderOpus::Config::Config() : frame_size_ms(20), num_channels(1) {}
41 bool AudioEncoderOpus::Config::IsOk() const {
42 if (frame_size_ms <= 0 || frame_size_ms % 10 != 0)
44 if (num_channels <= 0)
49 AudioEncoderOpus::AudioEncoderOpus(const Config& config)
50 : num_10ms_frames_per_packet_(DivExact(config.frame_size_ms, 10)),
51 num_channels_(config.num_channels),
52 samples_per_10ms_frame_(DivExact(kSampleRateHz, 100) * num_channels_) {
54 input_buffer_.reserve(num_10ms_frames_per_packet_ * samples_per_10ms_frame_);
55 CHECK_EQ(0, WebRtcOpus_EncoderCreate(&inst_, num_channels_));
58 AudioEncoderOpus::~AudioEncoderOpus() {
59 CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_));
62 int AudioEncoderOpus::sample_rate_hz() const {
66 int AudioEncoderOpus::num_channels() const {
70 int AudioEncoderOpus::Num10MsFramesInNextPacket() const {
71 return num_10ms_frames_per_packet_;
74 bool AudioEncoderOpus::Encode(uint32_t timestamp,
76 size_t max_encoded_bytes,
78 size_t* encoded_bytes,
79 uint32_t* encoded_timestamp) {
80 if (input_buffer_.empty())
81 first_timestamp_in_buffer_ = timestamp;
82 input_buffer_.insert(input_buffer_.end(), audio,
83 audio + samples_per_10ms_frame_);
84 if (input_buffer_.size() < (static_cast<size_t>(num_10ms_frames_per_packet_) *
85 samples_per_10ms_frame_)) {
89 CHECK_EQ(input_buffer_.size(),
90 static_cast<size_t>(num_10ms_frames_per_packet_) *
91 samples_per_10ms_frame_);
92 int16_t r = WebRtcOpus_Encode(
93 inst_, &input_buffer_[0],
94 DivExact(CastInt16(input_buffer_.size()), num_channels_),
95 ClampInt16(max_encoded_bytes), encoded);
96 input_buffer_.clear();
100 *encoded_timestamp = first_timestamp_in_buffer_;
104 } // namespace webrtc