Index: remoting/codec/audio_encoder_speex.cc |
diff --git a/remoting/codec/audio_encoder_speex.cc b/remoting/codec/audio_encoder_speex.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fb15539c4319a4b38f93d948beaf75b641e7913b |
--- /dev/null |
+++ b/remoting/codec/audio_encoder_speex.cc |
@@ -0,0 +1,95 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "remoting/codec/audio_encoder_speex.h" |
+ |
+#include <string> |
+#include <sstream> |
+ |
+#include "base/basictypes.h" |
+#include "base/logging.h" |
+#include "base/stl_util.h" |
+#include "remoting/proto/audio.pb.h" |
+#include "third_party/speex/speex.h" |
+ |
+namespace { |
+const int kSpeexQuality = 8; |
+} // namespace |
+ |
+namespace remoting { |
+ |
+AudioEncoderSpeex::AudioEncoderSpeex() { |
+ // Create and initialize the Speex structures. |
+ speex_bits_init(&speex_bits_); |
+ speex_state_ = speex_encoder_init(&speex_wb_mode); |
+ |
+ // Set the encoding quality. |
+ int quality = kSpeexQuality; |
+ speex_encoder_ctl(speex_state_, SPEEX_SET_QUALITY, &quality); |
+ |
+ // Get the frame size and construct the output buffer accordingly. |
+ speex_encoder_ctl(speex_state_, SPEEX_GET_FRAME_SIZE, &speex_frame_size_); |
+ buffer_size_ = speex_frame_size_ * sizeof(spx_int16_t); |
+ buffer_.reset(new char[buffer_size_]); |
+} |
+ |
+AudioEncoderSpeex::~AudioEncoderSpeex() { |
+ speex_encoder_destroy(speex_state_); |
+ speex_bits_destroy(&speex_bits_); |
+} |
+ |
+scoped_ptr<AudioPacket> AudioEncoderSpeex::Encode( |
+ scoped_ptr<AudioPacket> packet) { |
+ DCHECK_EQ(AudioPacket::ENCODING_RAW, packet->encoding()); |
+ DCHECK_EQ(AudioPacket::BYTES_PER_SAMPLE_2, packet->bytes_per_sample()); |
+ |
+ int number_of_samples = packet->data().size() / sizeof(spx_int16_t); |
+ // Either because the Speex encoder modifies the input data buffer, |
+ // or the API is not const-correct, we have to convert the packet |
+ // payload into a non-const pointer. |
+ std::string* data = const_cast<std::string*>(&(packet->data())); |
+ spx_int16_t* samples = |
+ reinterpret_cast<spx_int16_t*>(string_as_array(data)); |
+ |
+ std::string encoded_data; |
+ |
+ for (int i = 0; i < number_of_samples; i += speex_frame_size_) { |
+ // Reset the bits structure for this frame. |
+ speex_bits_reset(&speex_bits_); |
+ |
+ // Encode the frame, treating all samples as integers. |
+ speex_encode_int(speex_state_, samples + i, &speex_bits_); |
+ |
+ // Sanity check to prevent overwriting the buffer. |
+ if (speex_bits_nbytes(&speex_bits_) >= buffer_size_) { |
+ LOG(ERROR) << "Speex encoder is not compressing data."; |
Sergey Ulanov
2012/08/10 20:41:37
WARNING?
kxing
2012/08/13 21:39:45
Done.
|
+ buffer_size_ = speex_bits_nbytes(&speex_bits_); |
+ buffer_.reset(new char[buffer_size_]); |
+ } |
+ |
+ // Copy the encoded data from the bits structure into the buffer. |
+ int bytes_written = |
+ speex_bits_write(&speex_bits_, buffer_.get(), buffer_size_); |
+ |
+ // Write out the data as the data size followed by the encoded data. |
+ // The data size is an unsigned int8 so we can support sizes up to 256 |
+ // bytes. |
+ DCHECK_LE(bytes_written, 0xFF); |
+ uint8 bytes_written_as_byte = static_cast<uint8>(bytes_written); |
+ encoded_data.append(reinterpret_cast<char*>(&bytes_written_as_byte), |
+ sizeof(uint8)); |
+ encoded_data.append(buffer_.get(), bytes_written); |
+ } |
+ |
+ // Create a new packet of encoded data. |
+ scoped_ptr<AudioPacket> encoded_packet(new AudioPacket()); |
+ encoded_packet->set_data(encoded_data); |
Sergey Ulanov
2012/08/10 20:41:37
To avoid copying it here it's better to allocate e
kxing
2012/08/13 21:39:45
Done.
|
+ encoded_packet->set_encoding(AudioPacket::ENCODING_SPEEX); |
+ encoded_packet->set_sampling_rate(packet->sampling_rate()); |
+ encoded_packet->set_bytes_per_sample(packet->bytes_per_sample()); |
+ encoded_packet->set_channels(packet->channels()); |
+ return encoded_packet.Pass(); |
+} |
+ |
+} // namespace remoting |