Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "remoting/codec/audio_decoder_speex.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/basictypes.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/stl_util.h" | |
| 13 #include "remoting/proto/audio.pb.h" | |
| 14 | |
| 15 extern "C" { | |
| 16 #include "third_party/speex/speex.h" | |
| 17 #include "third_party/speex/include/speex/speex_callbacks.h" | |
| 18 #include "third_party/speex/include/speex/speex_stereo.h" | |
| 19 } | |
|
Sergey Ulanov
2012/08/20 22:50:39
nit: // extern "C"
kxing
2012/08/21 16:56:11
Done.
| |
| 20 | |
| 21 namespace remoting { | |
| 22 | |
| 23 AudioDecoderSpeex::AudioDecoderSpeex() { | |
| 24 // Create and initialize the Speex structures. | |
| 25 speex_bits_.reset(new SpeexBits()); | |
| 26 speex_bits_init(speex_bits_.get()); | |
| 27 speex_state_ = speex_decoder_init(&speex_wb_mode); | |
| 28 | |
| 29 // Create and initialize the Speex stereo state. | |
| 30 speex_stereo_state_ = speex_stereo_state_init(); | |
| 31 | |
| 32 // Create and initialize the stereo callback. | |
| 33 speex_callback_.reset(new SpeexCallback()); | |
| 34 speex_callback_->callback_id = SPEEX_INBAND_STEREO; | |
| 35 speex_callback_->func = speex_std_stereo_request_handler; | |
| 36 speex_callback_->data = speex_stereo_state_; | |
| 37 | |
| 38 int result; | |
| 39 | |
| 40 // Turn perceptual enhancer. | |
|
Wez
2012/08/21 01:14:13
nit: Turn on perceptual enhancer? Perhaps indicate
kxing
2012/08/21 16:56:11
Done.
| |
| 41 int enhancer = 1; | |
| 42 result = speex_decoder_ctl(speex_state_, SPEEX_SET_ENH, &enhancer); | |
| 43 if (result < 0) { | |
| 44 LOG(ERROR) << "Failed to turn on the perceptual enhancer"; | |
| 45 return; | |
|
Wez
2012/08/21 01:14:13
You're early exiting here, but you don't have any
kxing
2012/08/21 16:56:11
Done.
| |
| 46 } | |
| 47 | |
| 48 // Get the frame size. | |
|
Wez
2012/08/21 01:14:13
nit: Similarly, indicate _why_ we need to get the
kxing
2012/08/21 16:56:11
Done.
| |
| 49 result = speex_decoder_ctl(speex_state_, | |
| 50 SPEEX_GET_FRAME_SIZE, | |
| 51 &speex_frame_size_); | |
| 52 if (result < 0) { | |
| 53 LOG(ERROR) << "Failed to get the frame size"; | |
|
Sergey Ulanov
2012/08/20 22:50:39
Should we also remember that we failed to initiali
kxing
2012/08/21 16:56:11
Done.
| |
| 54 return; | |
| 55 } | |
| 56 | |
| 57 // Set the stereo callback. | |
|
Wez
2012/08/21 01:14:13
nit: Similarly, indicate what the significance of
kxing
2012/08/21 16:56:11
Done.
| |
| 58 result = speex_decoder_ctl(speex_state_, | |
| 59 SPEEX_SET_HANDLER, | |
| 60 speex_callback_.get()); | |
| 61 if (result < 0) { | |
| 62 LOG(ERROR) << "Failed to set the stereo callback."; | |
| 63 return; | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 AudioDecoderSpeex::~AudioDecoderSpeex() { | |
| 68 speex_stereo_state_destroy(speex_stereo_state_); | |
| 69 speex_decoder_destroy(speex_state_); | |
| 70 speex_bits_destroy(speex_bits_.get()); | |
| 71 } | |
| 72 | |
| 73 scoped_ptr<AudioPacket> AudioDecoderSpeex::Decode( | |
| 74 scoped_ptr<AudioPacket> packet) { | |
| 75 if ((packet->encoding() != AudioPacket::ENCODING_SPEEX) || | |
| 76 (packet->bytes_per_sample() != AudioPacket::BYTES_PER_SAMPLE_2) || | |
| 77 (packet->sampling_rate() == AudioPacket::SAMPLING_RATE_INVALID) || | |
| 78 (packet->channels() != AudioPacket::CHANNELS_STEREO)) { | |
| 79 LOG(WARNING) << "Received a corrupted packet."; | |
|
Sergey Ulanov
2012/08/20 22:50:39
It's not necessarily corrupt. It's just not suppor
kxing
2012/08/21 16:56:11
Done.
| |
| 80 return scoped_ptr<AudioPacket>(NULL); | |
| 81 } | |
| 82 | |
| 83 // Create a new packet of decoded data. | |
| 84 scoped_ptr<AudioPacket> decoded_packet(new AudioPacket()); | |
| 85 decoded_packet->set_encoding(AudioPacket::ENCODING_RAW); | |
| 86 decoded_packet->set_sampling_rate(packet->sampling_rate()); | |
| 87 decoded_packet->set_bytes_per_sample(packet->bytes_per_sample()); | |
| 88 decoded_packet->set_channels(packet->channels()); | |
| 89 | |
| 90 std::string* decoded_data = decoded_packet->add_data(); | |
| 91 decoded_data->resize(packet->data_size() * | |
| 92 speex_frame_size_ * | |
| 93 packet->bytes_per_sample() * | |
| 94 packet->channels()); | |
| 95 int16* samples = reinterpret_cast<int16*>(string_as_array(decoded_data)); | |
| 96 | |
| 97 for (int i = 0; i < packet->data_size(); ++i) { | |
| 98 // Read the bytes into the bits structure. | |
| 99 speex_bits_read_from(speex_bits_.get(), | |
| 100 string_as_array(packet->mutable_data(i)), | |
| 101 packet->data(i).size()); | |
| 102 | |
| 103 // Decode the frame and store it in the buffer. | |
| 104 int status = speex_decode_int(speex_state_, speex_bits_.get(), samples); | |
| 105 if (status < 0) { | |
| 106 LOG(ERROR) << "Error in decoding Speex data."; | |
| 107 return scoped_ptr<AudioPacket>(NULL); | |
| 108 } | |
| 109 // Transform mono to stereo. | |
| 110 speex_decode_stereo_int(samples, speex_frame_size_, speex_stereo_state_); | |
| 111 | |
| 112 samples += (speex_frame_size_ * packet->channels()); | |
| 113 } | |
| 114 | |
| 115 return decoded_packet.Pass(); | |
| 116 } | |
| 117 | |
| 118 } // namespace remoting | |
| OLD | NEW |