Chromium Code Reviews| 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..05b0981f71ec16dac3ea3b6ad6c5990d13434551 |
| --- /dev/null |
| +++ b/remoting/codec/audio_decoder_speex.cc |
| @@ -0,0 +1,84 @@ |
| +// 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" |
|
Sergey Ulanov
2012/08/10 20:41:37
is this used anywhere?
kxing
2012/08/13 21:39:45
It's used to call string_as_array().
|
| +#include "remoting/proto/audio.pb.h" |
| +#include "third_party/speex/speex.h" |
| + |
| +namespace remoting { |
| + |
| +AudioDecoderSpeex::AudioDecoderSpeex() { |
| + // Create and initialize the Speex structures. |
| + speex_bits_init(&speex_bits_); |
| + speex_state_ = speex_decoder_init(&speex_wb_mode); |
| + |
| + // Turn perceptual enhancer. |
| + int enhancer = 1; |
| + speex_decoder_ctl(speex_state_, SPEEX_SET_ENH, &enhancer); |
| + |
| + // Get the frame size and construct the output buffer acccordingly. |
| + speex_decoder_ctl(speex_state_, SPEEX_GET_FRAME_SIZE, &speex_frame_size_); |
| + buffer_.reset(new spx_int16_t[speex_frame_size_]); |
| +} |
| + |
| +AudioDecoderSpeex::~AudioDecoderSpeex() { |
| + speex_decoder_destroy(speex_state_); |
| + speex_bits_destroy(&speex_bits_); |
| +} |
| + |
| +scoped_ptr<AudioPacket> AudioDecoderSpeex::Decode( |
| + scoped_ptr<AudioPacket> packet) { |
| + DCHECK_EQ(AudioPacket::ENCODING_SPEEX, packet->encoding()); |
| + DCHECK_EQ(AudioPacket::BYTES_PER_SAMPLE_2, packet->bytes_per_sample()); |
| + |
| + int bytes_left = packet->data().size(); |
| + // Either because the Speex decoder 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())); |
| + char* ptr = string_as_array(data); |
| + |
| + std::string decoded_data; |
| + |
| + while (bytes_left > 0) { |
| + // Fetch the number of bytes in the frame. |
| + int32 bytes_to_read = *(reinterpret_cast<uint8*>(ptr)); |
| + ptr++; |
| + bytes_left--; |
| + |
| + DCHECK_GT(bytes_to_read, 0); |
| + DCHECK_GE(bytes_left, bytes_to_read); |
| + |
| + // Read the bytes into the bits structure. |
| + speex_bits_read_from(&speex_bits_, ptr, bytes_to_read); |
| + |
| + // Decode the frame and store it in the buffer. |
| + speex_decode_int(speex_state_, &speex_bits_, buffer_.get()); |
|
Sergey Ulanov
2012/08/10 20:41:37
This call may fail (e.g. if other side sends garba
kxing
2012/08/13 21:39:45
Done.
|
| + ptr += bytes_to_read; |
| + bytes_left -= bytes_to_read; |
| + |
| + DCHECK_GE(bytes_left, 0); |
| + |
| + decoded_data.append(reinterpret_cast<char*>(buffer_.get()), |
| + speex_frame_size_ * sizeof(spx_int16_t)); |
| + } |
| + |
| + // Create a new packet of decoded data. |
| + scoped_ptr<AudioPacket> decoded_packet(new AudioPacket()); |
| + decoded_packet->set_data(decoded_data); |
|
Sergey Ulanov
2012/08/10 20:41:37
you can avoid copying the data by allocating decod
kxing
2012/08/13 21:39:45
Done.
|
| + 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()); |
| + return decoded_packet.Pass(); |
| +} |
| + |
| +} // namespace remoting |