Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(363)

Side by Side Diff: remoting/codec/audio_encoder_speex.cc

Issue 10831246: Speex encoding/decoding. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 } // namespace
19
20 namespace remoting {
21
22 AudioEncoderSpeex::AudioEncoderSpeex() {
23 // Create and initialize the Speex structures.
24 speex_bits_init(&speex_bits_);
25 speex_state_ = speex_encoder_init(&speex_wb_mode);
26
27 // Set the encoding quality.
28 int quality = kSpeexQuality;
29 speex_encoder_ctl(speex_state_, SPEEX_SET_QUALITY, &quality);
30
31 // Get the frame size and construct the output buffer accordingly.
32 speex_encoder_ctl(speex_state_, SPEEX_GET_FRAME_SIZE, &speex_frame_size_);
33 buffer_size_ = speex_frame_size_ * sizeof(spx_int16_t);
34 buffer_.reset(new char[buffer_size_]);
35 }
36
37 AudioEncoderSpeex::~AudioEncoderSpeex() {
38 speex_encoder_destroy(speex_state_);
39 speex_bits_destroy(&speex_bits_);
40 }
41
42 scoped_ptr<AudioPacket> AudioEncoderSpeex::Encode(
43 scoped_ptr<AudioPacket> packet) {
44 DCHECK_EQ(AudioPacket::ENCODING_RAW, packet->encoding());
45 DCHECK_EQ(AudioPacket::BYTES_PER_SAMPLE_2, packet->bytes_per_sample());
46
47 int number_of_samples = packet->data().size() / sizeof(spx_int16_t);
48 // Either because the Speex encoder modifies the input data buffer,
49 // or the API is not const-correct, we have to convert the packet
50 // payload into a non-const pointer.
51 std::string* data = const_cast<std::string*>(&(packet->data()));
52 spx_int16_t* samples =
53 reinterpret_cast<spx_int16_t*>(string_as_array(data));
54
55 std::string encoded_data;
56
57 for (int i = 0; i < number_of_samples; i += speex_frame_size_) {
58 // Reset the bits structure for this frame.
59 speex_bits_reset(&speex_bits_);
60
61 // Encode the frame, treating all samples as integers.
62 speex_encode_int(speex_state_, samples + i, &speex_bits_);
63
64 // Sanity check to prevent overwriting the buffer.
65 if (speex_bits_nbytes(&speex_bits_) >= buffer_size_) {
66 LOG(ERROR) << "Speex encoder is not compressing data.";
Sergey Ulanov 2012/08/10 20:41:37 WARNING?
kxing 2012/08/13 21:39:45 Done.
67 buffer_size_ = speex_bits_nbytes(&speex_bits_);
68 buffer_.reset(new char[buffer_size_]);
69 }
70
71 // Copy the encoded data from the bits structure into the buffer.
72 int bytes_written =
73 speex_bits_write(&speex_bits_, buffer_.get(), buffer_size_);
74
75 // Write out the data as the data size followed by the encoded data.
76 // The data size is an unsigned int8 so we can support sizes up to 256
77 // bytes.
78 DCHECK_LE(bytes_written, 0xFF);
79 uint8 bytes_written_as_byte = static_cast<uint8>(bytes_written);
80 encoded_data.append(reinterpret_cast<char*>(&bytes_written_as_byte),
81 sizeof(uint8));
82 encoded_data.append(buffer_.get(), bytes_written);
83 }
84
85 // Create a new packet of encoded data.
86 scoped_ptr<AudioPacket> encoded_packet(new AudioPacket());
87 encoded_packet->set_data(encoded_data);
Sergey Ulanov 2012/08/10 20:41:37 To avoid copying it here it's better to allocate e
kxing 2012/08/13 21:39:45 Done.
88 encoded_packet->set_encoding(AudioPacket::ENCODING_SPEEX);
89 encoded_packet->set_sampling_rate(packet->sampling_rate());
90 encoded_packet->set_bytes_per_sample(packet->bytes_per_sample());
91 encoded_packet->set_channels(packet->channels());
92 return encoded_packet.Pass();
93 }
94
95 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698