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..755dbd516a6758bbd85522739dc6827b5da730ed |
| --- /dev/null |
| +++ b/remoting/codec/audio_decoder_speex.cc |
| @@ -0,0 +1,89 @@ |
| +// 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); |
| + |
| + // 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 int16[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) { |
| + 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 |
|
Sergey Ulanov
2012/08/14 00:54:32
looking at the speex code it's the latter. speex_b
kxing
2012/08/16 13:57:54
Done.
|
| + // payload into a non-const pointer. |
| + std::string* data = const_cast<std::string*>(&(packet->data())); |
| + char* ptr = string_as_array(data); |
| + |
| + // Create a new packet of decoded data. |
| + scoped_ptr<AudioPacket> decoded_packet(new AudioPacket()); |
| + decoded_packet->set_data(std::string()); |
|
Sergey Ulanov
2012/08/14 00:54:32
don't need this. mutable_data() will do it for you
kxing
2012/08/16 13:57:54
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()); |
| + |
| + while (bytes_left > 0) { |
| + // Fetch the number of bytes in the frame. |
| + int32 bytes_to_read = *(reinterpret_cast<uint8*>(ptr)); |
| + ptr++; |
|
Sergey Ulanov
2012/08/14 00:54:32
nit: in remoting code it's more common to use pref
kxing
2012/08/16 13:57:54
Done.
|
| + bytes_left--; |
|
Sergey Ulanov
2012/08/14 00:54:32
nit: same here
kxing
2012/08/16 13:57:54
Done.
|
| + |
| + 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_.get(), ptr, bytes_to_read); |
| + |
| + // Decode the frame and store it in the buffer. |
| + int status = |
| + speex_decode_int(speex_state_, speex_bits_.get(), buffer_.get()); |
| + if (status < 0) |
| + LOG(ERROR) << "Error in decoding Speex data."; |
|
Sergey Ulanov
2012/08/14 00:54:32
Do you need to return or break here?
kxing
2012/08/16 13:57:54
Done.
|
| + |
| + ptr += bytes_to_read; |
| + bytes_left -= bytes_to_read; |
| + |
| + DCHECK_GE(bytes_left, 0); |
| + |
| + decoded_packet->mutable_data()->append( |
|
Sergey Ulanov
2012/08/14 00:54:32
instead of appending data here can you specify mut
kxing
2012/08/16 13:57:54
Done.
|
| + reinterpret_cast<char*>(buffer_.get()), |
| + speex_frame_size_ * sizeof(int16)); |
| + } |
| + |
| + return decoded_packet.Pass(); |
| +} |
| + |
| +} // namespace remoting |