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

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

Issue 11189047: Add opus audio codec support in remoting (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 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_decoder_opus.h"
6
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9 #include "base/time.h"
10 #include "remoting/proto/audio.pb.h"
11 #include "third_party/opus/src/include/opus.h"
12
13 namespace remoting {
14
15 namespace {
16
17 const int kMaxPacketSizeMs = 120;
18
19 const AudioPacket::SamplingRate kSamplingRate =
20 AudioPacket::SAMPLING_RATE_48000;
21
22 } // namespace
23
24 AudioDecoderOpus::AudioDecoderOpus()
25 : sampling_rate_(0),
26 channels_(0),
27 decoder_(NULL) {
28 }
29
30 AudioDecoderOpus::~AudioDecoderOpus() {
Wez 2012/10/19 01:51:32 Call DestroyDecoder() here?
Sergey Ulanov 2012/10/19 20:54:30 Done.
31 }
32
33 void AudioDecoderOpus::InitDecoder() {
34 DCHECK(!decoder_);
35 int error;
36 decoder_ = opus_decoder_create(kSamplingRate, channels_, &error);
37 CHECK(decoder_);
Wez 2012/10/19 01:51:32 Don't CHECK on error; just LOG(ERROR) if an error
Sergey Ulanov 2012/10/19 20:54:30 Done.
38 CHECK(!error);
39 }
40
41 void AudioDecoderOpus::DestroyDecoder() {
42 if (decoder_) {
43 opus_decoder_destroy(decoder_);
44 decoder_ = NULL;
45 }
46 }
47
48 bool AudioDecoderOpus::ResetForPacket(AudioPacket* packet) {
49 if (packet->channels() != channels_ ||
50 packet->sampling_rate() != sampling_rate_) {
51 DestroyDecoder();
52
53 channels_ = packet->channels();
54 sampling_rate_ = packet->sampling_rate();
55
56 if (channels_ <= 0 || channels_ > 2 ||
57 sampling_rate_ != kSamplingRate) {
58 LOG(WARNING) << "Unsupported OPUS parameters: "
59 << channels_ << " channels with "
60 << sampling_rate_ << " samples per second.";
61 return false;
62 }
63
64 InitDecoder();
65 }
66
67 return decoder_ != NULL;
68 }
69
70
71 scoped_ptr<AudioPacket> AudioDecoderOpus::Decode(
72 scoped_ptr<AudioPacket> packet) {
73 if (packet->encoding() != AudioPacket::ENCODING_OPUS) {
74 LOG(WARNING) << "Received a packet with encoding " << packet->encoding()
75 << "when an OPUS packet was expected.";
Wez 2012/10/19 01:51:32 nit: This should be trapped by the calling code an
Sergey Ulanov 2012/10/19 20:54:30 Actually calling code doesn't have any knowledge o
76 return scoped_ptr<AudioPacket>();
77 }
78
79 if (!ResetForPacket(packet.get())) {
80 return scoped_ptr<AudioPacket>();
81 }
82
83 // Create a new packet of decoded data.
84 scoped_ptr<AudioPacket> decoded_packet(new AudioPacket());
85 decoded_packet->set_encoding(AudioPacket::ENCODING_RAW);
86 decoded_packet->set_sampling_rate(kSamplingRate);
87 decoded_packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2);
88 decoded_packet->set_channels(packet->channels());
89
90 int max_frame_samples = kMaxPacketSizeMs * kSamplingRate /
91 base::Time::kMillisecondsPerSecond;
92 int max_frame_bytes = max_frame_samples * channels_ *
93 decoded_packet->bytes_per_sample();
94
95 std::string* decoded_data = decoded_packet->add_data();
96 decoded_data->resize(packet->data_size() * max_frame_bytes);
97 int buffer_pos = 0;
98
99 for (int i = 0; i < packet->data_size(); ++i) {
100 int16* pcm_buffer =
101 reinterpret_cast<int16*>(string_as_array(decoded_data) + buffer_pos);
102 CHECK_LE(buffer_pos + max_frame_bytes,
103 static_cast<int>(decoded_data->size()));
Wez 2012/10/19 01:51:32 nit: DCHECK_LE
Sergey Ulanov 2012/10/19 20:54:30 CHECK is better here to avoid potential buffer ove
Wez 2012/10/22 22:50:21 If opus_decode() returns more than it should, thou
Sergey Ulanov 2012/10/23 00:43:49 Right, but this CHECK still makes it harder to exp
104 std::string* frame = packet->mutable_data(i);
105 unsigned char* frame_data =
106 reinterpret_cast<unsigned char*>(string_as_array(frame));
107 int result = opus_decode(decoder_, frame_data, frame->size(),
108 pcm_buffer, max_frame_samples, 0);
109 if (result < 0) {
110 LOG(ERROR) << "Failed decoding Opus frame. Error code: " << result;
Wez 2012/10/19 01:51:32 nit: Reset the decoder in this case, and return a
Wez 2012/10/19 01:51:32 nit: Consider using continue here rather than if..
Sergey Ulanov 2012/10/19 20:54:30 Done.
Sergey Ulanov 2012/10/19 20:54:30 Done.
111 } else {
112 buffer_pos += result * packet->channels() *
113 decoded_packet->bytes_per_sample();
114 }
115 }
116
117 decoded_data->resize(buffer_pos);
118
119 if (!decoded_data->size()) {
Wez 2012/10/19 01:51:32 nit: if (decoded_data->empty()) OR Move this tes
Sergey Ulanov 2012/10/19 20:54:30 Done.
120 return scoped_ptr<AudioPacket>();
121 }
122
123 return decoded_packet.Pass();
124 }
125
126 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698