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..4dcffd0868284755377a23573d4f8348ca2227a8 |
--- /dev/null |
+++ b/remoting/codec/audio_encoder_speex.cc |
@@ -0,0 +1,101 @@ |
+// 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; |
+const int kEncodedDataBufferSize = 0xFF; |
+} // namespace |
+ |
+namespace remoting { |
+ |
+AudioEncoderSpeex::AudioEncoderSpeex() { |
+ // Create and initialize the Speex structures. |
+ speex_bits_.reset(new SpeexBits()); |
+ speex_bits_init(speex_bits_.get()); |
+ 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 input buffer accordingly. |
+ speex_encoder_ctl(speex_state_, SPEEX_GET_FRAME_SIZE, &speex_frame_size_); |
+ raw_data_buffer_.reset(new int16[speex_frame_size_]); |
+ |
+ encoded_data_buffer_.reset(new char[kEncodedDataBufferSize]); |
+} |
+ |
+AudioEncoderSpeex::~AudioEncoderSpeex() { |
+ speex_encoder_destroy(speex_state_); |
+ speex_bits_destroy(speex_bits_.get()); |
+} |
+ |
+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()); |
+ |
+ // Put the samples onto the queue. |
+ int number_of_samples = packet->data().size() / sizeof(spx_int16_t); |
Sergey Ulanov
2012/08/14 00:54:32
nit: divide by packet->bytes_per_sample(). It's th
kxing
2012/08/16 13:57:54
Done.
|
+ const int16* sample = reinterpret_cast<const int16*>(packet->data().data()); |
+ |
+ for (int i = 0; i < number_of_samples; i++) { |
+ queued_samples_.push_back(*sample); |
Sergey Ulanov
2012/08/14 00:54:32
The data we store between Encode() calls is always
kxing
2012/08/16 13:57:54
Done.
|
+ sample++; |
+ } |
+ |
+ // Create a new packet of encoded data. |
+ scoped_ptr<AudioPacket> encoded_packet(new AudioPacket()); |
+ encoded_packet->set_data(std::string()); |
Sergey Ulanov
2012/08/14 00:54:32
don't need this line.
kxing
2012/08/16 13:57:54
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()); |
+ |
+ while (static_cast<int>(queued_samples_.size()) >= speex_frame_size_) { |
+ // Copy the bytes from the sample queue. |
+ for (int i = 0; i < speex_frame_size_; i++) { |
+ raw_data_buffer_[i] = queued_samples_.front(); |
Sergey Ulanov
2012/08/14 00:54:32
ouch. we just copied data to queued_samples, now w
kxing
2012/08/16 13:57:54
Done.
|
+ queued_samples_.pop_front(); |
+ } |
+ |
+ // Reset the bits structure for this frame. |
+ speex_bits_reset(speex_bits_.get()); |
Sergey Ulanov
2012/08/14 00:54:32
why do we need to reset it here?
kxing
2012/08/16 13:57:54
Done.
|
+ |
+ // Encode the frame, treating all samples as integers. |
+ speex_encode_int(speex_state_, raw_data_buffer_.get(), speex_bits_.get()); |
+ |
+ DCHECK_LE(speex_bits_nbytes(speex_bits_.get()), kEncodedDataBufferSize); |
+ |
+ // Copy the encoded data from the bits structure into the buffer. |
+ int bytes_written = speex_bits_write(speex_bits_.get(), |
+ encoded_data_buffer_.get(), |
Sergey Ulanov
2012/08/14 00:54:32
pass a buffer inside encoded_packet->mutable_data(
kxing
2012/08/16 13:57:54
Done.
|
+ kEncodedDataBufferSize); |
+ DCHECK_LE(bytes_written, kEncodedDataBufferSize); |
+ |
+ // 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 255 |
+ // bytes. |
+ uint8 bytes_written_as_byte = static_cast<uint8>(bytes_written); |
Sergey Ulanov
2012/08/14 00:54:32
can we be sure that each encoded frame is smaller
kxing
2012/08/16 13:57:54
Done.
|
+ encoded_packet->mutable_data()->append( |
Sergey Ulanov
2012/08/14 00:54:32
Would it be better to make data field in AudioPack
kxing
2012/08/16 13:57:54
Done.
|
+ reinterpret_cast<char*>(&bytes_written_as_byte), sizeof(uint8)); |
+ encoded_packet->mutable_data()->append( |
+ encoded_data_buffer_.get(), bytes_written); |
+ } |
+ |
+ return encoded_packet.Pass(); |
Sergey Ulanov
2012/08/14 00:54:32
we may return an empty packet that will be wasting
kxing
2012/08/16 13:57:54
The empty packet filtering happens at the capturer
|
+} |
+ |
+} // namespace remoting |