OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/codec/audio_decoder_opus.h" | 5 #include "remoting/codec/audio_decoder_opus.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "remoting/proto/audio.pb.h" | 10 #include "remoting/proto/audio.pb.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 | 74 |
75 return decoder_ != NULL; | 75 return decoder_ != NULL; |
76 } | 76 } |
77 | 77 |
78 | 78 |
79 scoped_ptr<AudioPacket> AudioDecoderOpus::Decode( | 79 scoped_ptr<AudioPacket> AudioDecoderOpus::Decode( |
80 scoped_ptr<AudioPacket> packet) { | 80 scoped_ptr<AudioPacket> packet) { |
81 if (packet->encoding() != AudioPacket::ENCODING_OPUS) { | 81 if (packet->encoding() != AudioPacket::ENCODING_OPUS) { |
82 LOG(WARNING) << "Received an audio packet with encoding " | 82 LOG(WARNING) << "Received an audio packet with encoding " |
83 << packet->encoding() << " when an OPUS packet was expected."; | 83 << packet->encoding() << " when an OPUS packet was expected."; |
84 return scoped_ptr<AudioPacket>(); | 84 return nullptr; |
85 } | 85 } |
86 if (packet->data_size() > kMaxFramesPerPacket) { | 86 if (packet->data_size() > kMaxFramesPerPacket) { |
87 LOG(WARNING) << "Received an packet with too many frames."; | 87 LOG(WARNING) << "Received an packet with too many frames."; |
88 return scoped_ptr<AudioPacket>(); | 88 return nullptr; |
89 } | 89 } |
90 | 90 |
91 if (!ResetForPacket(packet.get())) { | 91 if (!ResetForPacket(packet.get())) { |
92 return scoped_ptr<AudioPacket>(); | 92 return nullptr; |
93 } | 93 } |
94 | 94 |
95 // Create a new packet of decoded data. | 95 // Create a new packet of decoded data. |
96 scoped_ptr<AudioPacket> decoded_packet(new AudioPacket()); | 96 scoped_ptr<AudioPacket> decoded_packet(new AudioPacket()); |
97 decoded_packet->set_encoding(AudioPacket::ENCODING_RAW); | 97 decoded_packet->set_encoding(AudioPacket::ENCODING_RAW); |
98 decoded_packet->set_sampling_rate(kSamplingRate); | 98 decoded_packet->set_sampling_rate(kSamplingRate); |
99 decoded_packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2); | 99 decoded_packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2); |
100 decoded_packet->set_channels(packet->channels()); | 100 decoded_packet->set_channels(packet->channels()); |
101 | 101 |
102 int max_frame_samples = kMaxFrameSizeMs * kSamplingRate / | 102 int max_frame_samples = kMaxFrameSizeMs * kSamplingRate / |
(...skipping 11 matching lines...) Expand all Loading... |
114 CHECK_LE(buffer_pos + max_frame_bytes, | 114 CHECK_LE(buffer_pos + max_frame_bytes, |
115 static_cast<int>(decoded_data->size())); | 115 static_cast<int>(decoded_data->size())); |
116 std::string* frame = packet->mutable_data(i); | 116 std::string* frame = packet->mutable_data(i); |
117 unsigned char* frame_data = | 117 unsigned char* frame_data = |
118 reinterpret_cast<unsigned char*>(string_as_array(frame)); | 118 reinterpret_cast<unsigned char*>(string_as_array(frame)); |
119 int result = opus_decode(decoder_, frame_data, frame->size(), | 119 int result = opus_decode(decoder_, frame_data, frame->size(), |
120 pcm_buffer, max_frame_samples, 0); | 120 pcm_buffer, max_frame_samples, 0); |
121 if (result < 0) { | 121 if (result < 0) { |
122 LOG(ERROR) << "Failed decoding Opus frame. Error code: " << result; | 122 LOG(ERROR) << "Failed decoding Opus frame. Error code: " << result; |
123 DestroyDecoder(); | 123 DestroyDecoder(); |
124 return scoped_ptr<AudioPacket>(); | 124 return nullptr; |
125 } | 125 } |
126 | 126 |
127 buffer_pos += result * packet->channels() * | 127 buffer_pos += result * packet->channels() * |
128 decoded_packet->bytes_per_sample(); | 128 decoded_packet->bytes_per_sample(); |
129 } | 129 } |
130 | 130 |
131 if (!buffer_pos) { | 131 if (!buffer_pos) { |
132 return scoped_ptr<AudioPacket>(); | 132 return nullptr; |
133 } | 133 } |
134 | 134 |
135 decoded_data->resize(buffer_pos); | 135 decoded_data->resize(buffer_pos); |
136 | 136 |
137 return decoded_packet.Pass(); | 137 return decoded_packet.Pass(); |
138 } | 138 } |
139 | 139 |
140 } // namespace remoting | 140 } // namespace remoting |
OLD | NEW |