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); | |
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.
| |
23 | |
24 // Turn perceptual enhancer. | |
25 int enhancer = 1; | |
26 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.
| |
27 | |
28 // Get the frame size. | |
29 speex_decoder_ctl(speex_state_, SPEEX_GET_FRAME_SIZE, &speex_frame_size_); | |
30 } | |
31 | |
32 AudioDecoderSpeex::~AudioDecoderSpeex() { | |
33 speex_decoder_destroy(speex_state_); | |
34 speex_bits_destroy(speex_bits_.get()); | |
35 } | |
36 | |
37 scoped_ptr<AudioPacket> AudioDecoderSpeex::Decode( | |
38 scoped_ptr<AudioPacket> packet) { | |
39 if ((packet->encoding() != AudioPacket::ENCODING_SPEEX) || | |
40 (packet->bytes_per_sample() != AudioPacket::BYTES_PER_SAMPLE_2)) { | |
41 LOG(WARNING) << "Received a corrupted packet."; | |
42 return scoped_ptr<AudioPacket>(NULL); | |
43 } | |
44 | |
45 // Create a new packet of decoded data. | |
46 scoped_ptr<AudioPacket> decoded_packet(new AudioPacket()); | |
47 decoded_packet->set_encoding(AudioPacket::ENCODING_RAW); | |
48 decoded_packet->set_sampling_rate(packet->sampling_rate()); | |
49 decoded_packet->set_bytes_per_sample(packet->bytes_per_sample()); | |
50 decoded_packet->set_channels(packet->channels()); | |
51 | |
52 std::string* decoded_data = decoded_packet->add_data(); | |
53 decoded_data->resize( | |
54 packet->data_size() * speex_frame_size_ * packet->bytes_per_sample()); | |
55 int16* samples = reinterpret_cast<int16*>(string_as_array(decoded_data)); | |
56 | |
57 for (int i = 0; i < packet->data_size(); ++i) { | |
58 // Read the bytes into the bits structure. | |
59 speex_bits_read_from(speex_bits_.get(), | |
60 string_as_array(packet->mutable_data(i)), | |
61 packet->data(i).size()); | |
62 | |
63 // Decode the frame and store it in the buffer. | |
64 int status = speex_decode_int(speex_state_, speex_bits_.get(), samples); | |
65 if (status < 0) { | |
66 LOG(ERROR) << "Error in decoding Speex data."; | |
67 return scoped_ptr<AudioPacket>(NULL); | |
68 } | |
69 | |
70 samples += speex_frame_size_; | |
71 } | |
72 | |
73 return decoded_packet.Pass(); | |
74 } | |
75 | |
76 } // namespace remoting | |
OLD | NEW |