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" | |
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().
| |
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_init(&speex_bits_); | |
21 speex_state_ = speex_decoder_init(&speex_wb_mode); | |
22 | |
23 // Turn perceptual enhancer. | |
24 int enhancer = 1; | |
25 speex_decoder_ctl(speex_state_, SPEEX_SET_ENH, &enhancer); | |
26 | |
27 // Get the frame size and construct the output buffer acccordingly. | |
28 speex_decoder_ctl(speex_state_, SPEEX_GET_FRAME_SIZE, &speex_frame_size_); | |
29 buffer_.reset(new spx_int16_t[speex_frame_size_]); | |
30 } | |
31 | |
32 AudioDecoderSpeex::~AudioDecoderSpeex() { | |
33 speex_decoder_destroy(speex_state_); | |
34 speex_bits_destroy(&speex_bits_); | |
35 } | |
36 | |
37 scoped_ptr<AudioPacket> AudioDecoderSpeex::Decode( | |
38 scoped_ptr<AudioPacket> packet) { | |
39 DCHECK_EQ(AudioPacket::ENCODING_SPEEX, packet->encoding()); | |
40 DCHECK_EQ(AudioPacket::BYTES_PER_SAMPLE_2, packet->bytes_per_sample()); | |
41 | |
42 int bytes_left = packet->data().size(); | |
43 // Either because the Speex decoder modifies the input data buffer, | |
44 // or the API is not const-correct, we have to convert the packet | |
45 // payload into a non-const pointer. | |
46 std::string* data = const_cast<std::string*>(&(packet->data())); | |
47 char* ptr = string_as_array(data); | |
48 | |
49 std::string decoded_data; | |
50 | |
51 while (bytes_left > 0) { | |
52 // Fetch the number of bytes in the frame. | |
53 int32 bytes_to_read = *(reinterpret_cast<uint8*>(ptr)); | |
54 ptr++; | |
55 bytes_left--; | |
56 | |
57 DCHECK_GT(bytes_to_read, 0); | |
58 DCHECK_GE(bytes_left, bytes_to_read); | |
59 | |
60 // Read the bytes into the bits structure. | |
61 speex_bits_read_from(&speex_bits_, ptr, bytes_to_read); | |
62 | |
63 // Decode the frame and store it in the buffer. | |
64 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.
| |
65 ptr += bytes_to_read; | |
66 bytes_left -= bytes_to_read; | |
67 | |
68 DCHECK_GE(bytes_left, 0); | |
69 | |
70 decoded_data.append(reinterpret_cast<char*>(buffer_.get()), | |
71 speex_frame_size_ * sizeof(spx_int16_t)); | |
72 } | |
73 | |
74 // Create a new packet of decoded data. | |
75 scoped_ptr<AudioPacket> decoded_packet(new AudioPacket()); | |
76 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.
| |
77 decoded_packet->set_encoding(AudioPacket::ENCODING_RAW); | |
78 decoded_packet->set_sampling_rate(packet->sampling_rate()); | |
79 decoded_packet->set_bytes_per_sample(packet->bytes_per_sample()); | |
80 decoded_packet->set_channels(packet->channels()); | |
81 return decoded_packet.Pass(); | |
82 } | |
83 | |
84 } // namespace remoting | |
OLD | NEW |