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_encoder_speex.h" | |
6 | |
7 #include <string> | |
8 #include <sstream> | |
9 | |
10 #include "base/basictypes.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 { | |
17 const int kSpeexQuality = 8; | |
18 const int kEncodedDataBufferSize = 0xFF; | |
19 } // namespace | |
20 | |
21 namespace remoting { | |
22 | |
23 AudioEncoderSpeex::AudioEncoderSpeex() { | |
24 // Create and initialize the Speex structures. | |
25 speex_bits_.reset(new SpeexBits()); | |
26 speex_bits_init(speex_bits_.get()); | |
27 speex_state_ = speex_encoder_init(&speex_wb_mode); | |
28 | |
29 // Set the encoding quality. | |
30 int quality = kSpeexQuality; | |
31 speex_encoder_ctl(speex_state_, SPEEX_SET_QUALITY, &quality); | |
32 | |
33 // Get the frame size and construct the input buffer accordingly. | |
34 speex_encoder_ctl(speex_state_, SPEEX_GET_FRAME_SIZE, &speex_frame_size_); | |
35 raw_data_buffer_.reset(new int16[speex_frame_size_]); | |
36 | |
37 encoded_data_buffer_.reset(new char[kEncodedDataBufferSize]); | |
38 } | |
39 | |
40 AudioEncoderSpeex::~AudioEncoderSpeex() { | |
41 speex_encoder_destroy(speex_state_); | |
42 speex_bits_destroy(speex_bits_.get()); | |
43 } | |
44 | |
45 scoped_ptr<AudioPacket> AudioEncoderSpeex::Encode( | |
46 scoped_ptr<AudioPacket> packet) { | |
47 DCHECK_EQ(AudioPacket::ENCODING_RAW, packet->encoding()); | |
48 DCHECK_EQ(AudioPacket::BYTES_PER_SAMPLE_2, packet->bytes_per_sample()); | |
49 | |
50 // Put the samples onto the queue. | |
51 int number_of_samples = packet->data().size() / sizeof(spx_int16_t); | |
Sergey Ulanov
2012/08/14 00:54:32
nit: divide by packet->bytes_per_sample(). It's th
kxing
2012/08/16 13:57:54
Done.
| |
52 const int16* sample = reinterpret_cast<const int16*>(packet->data().data()); | |
53 | |
54 for (int i = 0; i < number_of_samples; i++) { | |
55 queued_samples_.push_back(*sample); | |
Sergey Ulanov
2012/08/14 00:54:32
The data we store between Encode() calls is always
kxing
2012/08/16 13:57:54
Done.
| |
56 sample++; | |
57 } | |
58 | |
59 // Create a new packet of encoded data. | |
60 scoped_ptr<AudioPacket> encoded_packet(new AudioPacket()); | |
61 encoded_packet->set_data(std::string()); | |
Sergey Ulanov
2012/08/14 00:54:32
don't need this line.
kxing
2012/08/16 13:57:54
Done.
| |
62 encoded_packet->set_encoding(AudioPacket::ENCODING_SPEEX); | |
63 encoded_packet->set_sampling_rate(packet->sampling_rate()); | |
64 encoded_packet->set_bytes_per_sample(packet->bytes_per_sample()); | |
65 encoded_packet->set_channels(packet->channels()); | |
66 | |
67 while (static_cast<int>(queued_samples_.size()) >= speex_frame_size_) { | |
68 // Copy the bytes from the sample queue. | |
69 for (int i = 0; i < speex_frame_size_; i++) { | |
70 raw_data_buffer_[i] = queued_samples_.front(); | |
Sergey Ulanov
2012/08/14 00:54:32
ouch. we just copied data to queued_samples, now w
kxing
2012/08/16 13:57:54
Done.
| |
71 queued_samples_.pop_front(); | |
72 } | |
73 | |
74 // Reset the bits structure for this frame. | |
75 speex_bits_reset(speex_bits_.get()); | |
Sergey Ulanov
2012/08/14 00:54:32
why do we need to reset it here?
kxing
2012/08/16 13:57:54
Done.
| |
76 | |
77 // Encode the frame, treating all samples as integers. | |
78 speex_encode_int(speex_state_, raw_data_buffer_.get(), speex_bits_.get()); | |
79 | |
80 DCHECK_LE(speex_bits_nbytes(speex_bits_.get()), kEncodedDataBufferSize); | |
81 | |
82 // Copy the encoded data from the bits structure into the buffer. | |
83 int bytes_written = speex_bits_write(speex_bits_.get(), | |
84 encoded_data_buffer_.get(), | |
Sergey Ulanov
2012/08/14 00:54:32
pass a buffer inside encoded_packet->mutable_data(
kxing
2012/08/16 13:57:54
Done.
| |
85 kEncodedDataBufferSize); | |
86 DCHECK_LE(bytes_written, kEncodedDataBufferSize); | |
87 | |
88 // Write out the data as the data size followed by the encoded data. | |
89 // The data size is an unsigned int8 so we can support sizes up to 255 | |
90 // bytes. | |
91 uint8 bytes_written_as_byte = static_cast<uint8>(bytes_written); | |
Sergey Ulanov
2012/08/14 00:54:32
can we be sure that each encoded frame is smaller
kxing
2012/08/16 13:57:54
Done.
| |
92 encoded_packet->mutable_data()->append( | |
Sergey Ulanov
2012/08/14 00:54:32
Would it be better to make data field in AudioPack
kxing
2012/08/16 13:57:54
Done.
| |
93 reinterpret_cast<char*>(&bytes_written_as_byte), sizeof(uint8)); | |
94 encoded_packet->mutable_data()->append( | |
95 encoded_data_buffer_.get(), bytes_written); | |
96 } | |
97 | |
98 return encoded_packet.Pass(); | |
Sergey Ulanov
2012/08/14 00:54:32
we may return an empty packet that will be wasting
kxing
2012/08/16 13:57:54
The empty packet filtering happens at the capturer
| |
99 } | |
100 | |
101 } // namespace remoting | |
OLD | NEW |