| 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" |
| 11 #include "third_party/opus/src/include/opus.h" | 11 #include "third_party/opus/src/include/opus.h" |
| 12 | 12 |
| 13 namespace remoting { | 13 namespace remoting { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // Maximum size of an Opus frame in milliseconds. | 17 // Maximum size of an Opus frame in milliseconds. |
| 18 const int kMaxFrameSizeMs = 120; | 18 const int kMaxFrameSizeMs = 120; |
| 19 | 19 |
| 20 // Hosts will never generate more than 100 frames in a single packet. | 20 // Hosts will never generate more than 100 frames in a single packet. |
| 21 const int kMaxFramesPerPacket = 100; | 21 const int kMaxFramesPerPacket = 100; |
| 22 | 22 |
| 23 const AudioPacket::SamplingRate kSamplingRate = | 23 const AudioPacket::SamplingRate kSamplingRate = |
| 24 AudioPacket::SAMPLING_RATE_48000; | 24 AudioPacket::SAMPLING_RATE_48000; |
| 25 | 25 |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 AudioDecoderOpus::AudioDecoderOpus() | 28 AudioDecoderOpus::AudioDecoderOpus() |
| 29 : sampling_rate_(0), | 29 : sampling_rate_(0), channels_(0), decoder_(nullptr) { |
| 30 channels_(0), | |
| 31 decoder_(NULL) { | |
| 32 } | 30 } |
| 33 | 31 |
| 34 AudioDecoderOpus::~AudioDecoderOpus() { | 32 AudioDecoderOpus::~AudioDecoderOpus() { |
| 35 DestroyDecoder(); | 33 DestroyDecoder(); |
| 36 } | 34 } |
| 37 | 35 |
| 38 void AudioDecoderOpus::InitDecoder() { | 36 void AudioDecoderOpus::InitDecoder() { |
| 39 DCHECK(!decoder_); | 37 DCHECK(!decoder_); |
| 40 int error; | 38 int error; |
| 41 decoder_ = opus_decoder_create(kSamplingRate, channels_, &error); | 39 decoder_ = opus_decoder_create(kSamplingRate, channels_, &error); |
| 42 if (!decoder_) { | 40 if (!decoder_) { |
| 43 LOG(ERROR) << "Failed to create OPUS decoder; Error code: " << error; | 41 LOG(ERROR) << "Failed to create OPUS decoder; Error code: " << error; |
| 44 } | 42 } |
| 45 } | 43 } |
| 46 | 44 |
| 47 void AudioDecoderOpus::DestroyDecoder() { | 45 void AudioDecoderOpus::DestroyDecoder() { |
| 48 if (decoder_) { | 46 if (decoder_) { |
| 49 opus_decoder_destroy(decoder_); | 47 opus_decoder_destroy(decoder_); |
| 50 decoder_ = NULL; | 48 decoder_ = nullptr; |
| 51 } | 49 } |
| 52 } | 50 } |
| 53 | 51 |
| 54 bool AudioDecoderOpus::ResetForPacket(AudioPacket* packet) { | 52 bool AudioDecoderOpus::ResetForPacket(AudioPacket* packet) { |
| 55 if (packet->channels() != channels_ || | 53 if (packet->channels() != channels_ || |
| 56 packet->sampling_rate() != sampling_rate_) { | 54 packet->sampling_rate() != sampling_rate_) { |
| 57 DestroyDecoder(); | 55 DestroyDecoder(); |
| 58 | 56 |
| 59 channels_ = packet->channels(); | 57 channels_ = packet->channels(); |
| 60 sampling_rate_ = packet->sampling_rate(); | 58 sampling_rate_ = packet->sampling_rate(); |
| 61 | 59 |
| 62 if (channels_ <= 0 || channels_ > 2 || | 60 if (channels_ <= 0 || channels_ > 2 || |
| 63 sampling_rate_ != kSamplingRate) { | 61 sampling_rate_ != kSamplingRate) { |
| 64 LOG(WARNING) << "Unsupported OPUS parameters: " | 62 LOG(WARNING) << "Unsupported OPUS parameters: " |
| 65 << channels_ << " channels with " | 63 << channels_ << " channels with " |
| 66 << sampling_rate_ << " samples per second."; | 64 << sampling_rate_ << " samples per second."; |
| 67 return false; | 65 return false; |
| 68 } | 66 } |
| 69 } | 67 } |
| 70 | 68 |
| 71 if (!decoder_) { | 69 if (!decoder_) { |
| 72 InitDecoder(); | 70 InitDecoder(); |
| 73 } | 71 } |
| 74 | 72 |
| 75 return decoder_ != NULL; | 73 return decoder_; |
| 76 } | 74 } |
| 77 | 75 |
| 78 | 76 |
| 79 scoped_ptr<AudioPacket> AudioDecoderOpus::Decode( | 77 scoped_ptr<AudioPacket> AudioDecoderOpus::Decode( |
| 80 scoped_ptr<AudioPacket> packet) { | 78 scoped_ptr<AudioPacket> packet) { |
| 81 if (packet->encoding() != AudioPacket::ENCODING_OPUS) { | 79 if (packet->encoding() != AudioPacket::ENCODING_OPUS) { |
| 82 LOG(WARNING) << "Received an audio packet with encoding " | 80 LOG(WARNING) << "Received an audio packet with encoding " |
| 83 << packet->encoding() << " when an OPUS packet was expected."; | 81 << packet->encoding() << " when an OPUS packet was expected."; |
| 84 return nullptr; | 82 return nullptr; |
| 85 } | 83 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 if (!buffer_pos) { | 129 if (!buffer_pos) { |
| 132 return nullptr; | 130 return nullptr; |
| 133 } | 131 } |
| 134 | 132 |
| 135 decoded_data->resize(buffer_pos); | 133 decoded_data->resize(buffer_pos); |
| 136 | 134 |
| 137 return decoded_packet.Pass(); | 135 return decoded_packet.Pass(); |
| 138 } | 136 } |
| 139 | 137 |
| 140 } // namespace remoting | 138 } // namespace remoting |
| OLD | NEW |