Index: remoting/codec/audio_decoder_speex.cc |
diff --git a/remoting/codec/audio_decoder_speex.cc b/remoting/codec/audio_decoder_speex.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..87a4a21af976b9ab96cef8b57a754953bd0b1d53 |
--- /dev/null |
+++ b/remoting/codec/audio_decoder_speex.cc |
@@ -0,0 +1,76 @@ |
+// 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_decoder_speex.h" |
+ |
+#include <string> |
+ |
+#include "base/basictypes.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/logging.h" |
+#include "base/stl_util.h" |
+#include "remoting/proto/audio.pb.h" |
+#include "third_party/speex/speex.h" |
+ |
+namespace remoting { |
+ |
+AudioDecoderSpeex::AudioDecoderSpeex() { |
+ // Create and initialize the Speex structures. |
+ speex_bits_.reset(new SpeexBits()); |
+ speex_bits_init(speex_bits_.get()); |
+ speex_state_ = speex_decoder_init(&speex_wb_mode); |
Sergey Ulanov
2012/08/17 21:52:17
Do we tell speex anywhere that this is a stereo st
kxing
2012/08/20 21:50:21
Done.
|
+ |
+ // Turn perceptual enhancer. |
+ int enhancer = 1; |
+ speex_decoder_ctl(speex_state_, SPEEX_SET_ENH, &enhancer); |
Sergey Ulanov
2012/08/17 21:52:17
DCHECK here and below to verify that it returns 0.
kxing
2012/08/20 21:50:21
Done.
|
+ |
+ // Get the frame size. |
+ speex_decoder_ctl(speex_state_, SPEEX_GET_FRAME_SIZE, &speex_frame_size_); |
+} |
+ |
+AudioDecoderSpeex::~AudioDecoderSpeex() { |
+ speex_decoder_destroy(speex_state_); |
+ speex_bits_destroy(speex_bits_.get()); |
+} |
+ |
+scoped_ptr<AudioPacket> AudioDecoderSpeex::Decode( |
+ scoped_ptr<AudioPacket> packet) { |
+ if ((packet->encoding() != AudioPacket::ENCODING_SPEEX) || |
+ (packet->bytes_per_sample() != AudioPacket::BYTES_PER_SAMPLE_2)) { |
+ LOG(WARNING) << "Received a corrupted packet."; |
+ return scoped_ptr<AudioPacket>(NULL); |
+ } |
+ |
+ // Create a new packet of decoded data. |
+ scoped_ptr<AudioPacket> decoded_packet(new AudioPacket()); |
+ decoded_packet->set_encoding(AudioPacket::ENCODING_RAW); |
+ decoded_packet->set_sampling_rate(packet->sampling_rate()); |
+ decoded_packet->set_bytes_per_sample(packet->bytes_per_sample()); |
+ decoded_packet->set_channels(packet->channels()); |
+ |
+ std::string* decoded_data = decoded_packet->add_data(); |
+ decoded_data->resize( |
+ packet->data_size() * speex_frame_size_ * packet->bytes_per_sample()); |
+ int16* samples = reinterpret_cast<int16*>(string_as_array(decoded_data)); |
+ |
+ for (int i = 0; i < packet->data_size(); ++i) { |
+ // Read the bytes into the bits structure. |
+ speex_bits_read_from(speex_bits_.get(), |
+ string_as_array(packet->mutable_data(i)), |
+ packet->data(i).size()); |
+ |
+ // Decode the frame and store it in the buffer. |
+ int status = speex_decode_int(speex_state_, speex_bits_.get(), samples); |
+ if (status < 0) { |
+ LOG(ERROR) << "Error in decoding Speex data."; |
+ return scoped_ptr<AudioPacket>(NULL); |
+ } |
+ |
+ samples += speex_frame_size_; |
+ } |
+ |
+ return decoded_packet.Pass(); |
+} |
+ |
+} // namespace remoting |