 Chromium Code Reviews
 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 #include "third_party/speex/speex.h" | |
| 15 | |
| 16 namespace remoting { | |
| 17 | |
| 18 AudioDecoderSpeex::AudioDecoderSpeex() { | |
| 19 // Create and initialize the Speex structures. | |
| 20 speex_bits_.reset(new SpeexBits()); | |
| 21 speex_bits_init(speex_bits_.get()); | |
| 22 speex_state_ = speex_decoder_init(&speex_wb_mode); | |
| 23 | |
| 24 // Turn perceptual enhancer. | |
| 25 int enhancer = 1; | |
| 26 speex_decoder_ctl(speex_state_, SPEEX_SET_ENH, &enhancer); | |
| 27 | |
| 28 // Get the frame size and construct the output buffer acccordingly. | |
| 29 speex_decoder_ctl(speex_state_, SPEEX_GET_FRAME_SIZE, &speex_frame_size_); | |
| 30 buffer_.reset(new int16[speex_frame_size_]); | |
| 31 } | |
| 32 | |
| 33 AudioDecoderSpeex::~AudioDecoderSpeex() { | |
| 34 speex_decoder_destroy(speex_state_); | |
| 35 speex_bits_destroy(speex_bits_.get()); | |
| 36 } | |
| 37 | |
| 38 scoped_ptr<AudioPacket> AudioDecoderSpeex::Decode( | |
| 39 scoped_ptr<AudioPacket> packet) { | |
| 40 DCHECK_EQ(AudioPacket::ENCODING_SPEEX, packet->encoding()); | |
| 41 DCHECK_EQ(AudioPacket::BYTES_PER_SAMPLE_2, packet->bytes_per_sample()); | |
| 42 | |
| 43 int bytes_left = packet->data().size(); | |
| 44 // Either because the Speex decoder modifies the input data buffer, | |
| 45 // 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.
 | |
| 46 // payload into a non-const pointer. | |
| 47 std::string* data = const_cast<std::string*>(&(packet->data())); | |
| 48 char* ptr = string_as_array(data); | |
| 49 | |
| 50 // Create a new packet of decoded data. | |
| 51 scoped_ptr<AudioPacket> decoded_packet(new AudioPacket()); | |
| 52 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.
 | |
| 53 decoded_packet->set_encoding(AudioPacket::ENCODING_RAW); | |
| 54 decoded_packet->set_sampling_rate(packet->sampling_rate()); | |
| 55 decoded_packet->set_bytes_per_sample(packet->bytes_per_sample()); | |
| 56 decoded_packet->set_channels(packet->channels()); | |
| 57 | |
| 58 while (bytes_left > 0) { | |
| 59 // Fetch the number of bytes in the frame. | |
| 60 int32 bytes_to_read = *(reinterpret_cast<uint8*>(ptr)); | |
| 61 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.
 | |
| 62 bytes_left--; | |
| 
Sergey Ulanov
2012/08/14 00:54:32
nit: same here
 
kxing
2012/08/16 13:57:54
Done.
 | |
| 63 | |
| 64 DCHECK_GT(bytes_to_read, 0); | |
| 65 DCHECK_GE(bytes_left, bytes_to_read); | |
| 66 | |
| 67 // Read the bytes into the bits structure. | |
| 68 speex_bits_read_from(speex_bits_.get(), ptr, bytes_to_read); | |
| 69 | |
| 70 // Decode the frame and store it in the buffer. | |
| 71 int status = | |
| 72 speex_decode_int(speex_state_, speex_bits_.get(), buffer_.get()); | |
| 73 if (status < 0) | |
| 74 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.
 | |
| 75 | |
| 76 ptr += bytes_to_read; | |
| 77 bytes_left -= bytes_to_read; | |
| 78 | |
| 79 DCHECK_GE(bytes_left, 0); | |
| 80 | |
| 81 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.
 | |
| 82 reinterpret_cast<char*>(buffer_.get()), | |
| 83 speex_frame_size_ * sizeof(int16)); | |
| 84 } | |
| 85 | |
| 86 return decoded_packet.Pass(); | |
| 87 } | |
| 88 | |
| 89 } // namespace remoting | |
| OLD | NEW |