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_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/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() { |
| 31 DestroyDecoder(); |
| 32 } |
| 33 |
| 34 void AudioDecoderOpus::InitDecoder() { |
| 35 DCHECK(!decoder_); |
| 36 int error; |
| 37 decoder_ = opus_decoder_create(kSamplingRate, channels_, &error); |
| 38 if (!decoder_) { |
| 39 LOG(ERROR) << "Failed to create OPUS decoder; Error code: " << error; |
| 40 } |
| 41 } |
| 42 |
| 43 void AudioDecoderOpus::DestroyDecoder() { |
| 44 if (decoder_) { |
| 45 opus_decoder_destroy(decoder_); |
| 46 decoder_ = NULL; |
| 47 } |
| 48 } |
| 49 |
| 50 bool AudioDecoderOpus::ResetForPacket(AudioPacket* packet) { |
| 51 if (packet->channels() != channels_ || |
| 52 packet->sampling_rate() != sampling_rate_) { |
| 53 DestroyDecoder(); |
| 54 |
| 55 channels_ = packet->channels(); |
| 56 sampling_rate_ = packet->sampling_rate(); |
| 57 |
| 58 if (channels_ <= 0 || channels_ > 2 || |
| 59 sampling_rate_ != kSamplingRate) { |
| 60 LOG(WARNING) << "Unsupported OPUS parameters: " |
| 61 << channels_ << " channels with " |
| 62 << sampling_rate_ << " samples per second."; |
| 63 return false; |
| 64 } |
| 65 } |
| 66 |
| 67 if (!decoder_) { |
| 68 InitDecoder(); |
| 69 } |
| 70 |
| 71 return decoder_ != NULL; |
| 72 } |
| 73 |
| 74 |
| 75 scoped_ptr<AudioPacket> AudioDecoderOpus::Decode( |
| 76 scoped_ptr<AudioPacket> packet) { |
| 77 if (packet->encoding() != AudioPacket::ENCODING_OPUS) { |
| 78 LOG(WARNING) << "Received a packet with encoding " << packet->encoding() |
| 79 << "when an OPUS packet was expected."; |
| 80 return scoped_ptr<AudioPacket>(); |
| 81 } |
| 82 |
| 83 if (!ResetForPacket(packet.get())) { |
| 84 return scoped_ptr<AudioPacket>(); |
| 85 } |
| 86 |
| 87 // Create a new packet of decoded data. |
| 88 scoped_ptr<AudioPacket> decoded_packet(new AudioPacket()); |
| 89 decoded_packet->set_encoding(AudioPacket::ENCODING_RAW); |
| 90 decoded_packet->set_sampling_rate(kSamplingRate); |
| 91 decoded_packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2); |
| 92 decoded_packet->set_channels(packet->channels()); |
| 93 |
| 94 int max_frame_samples = kMaxPacketSizeMs * kSamplingRate / |
| 95 base::Time::kMillisecondsPerSecond; |
| 96 int max_frame_bytes = max_frame_samples * channels_ * |
| 97 decoded_packet->bytes_per_sample(); |
| 98 |
| 99 std::string* decoded_data = decoded_packet->add_data(); |
| 100 decoded_data->resize(packet->data_size() * max_frame_bytes); |
| 101 int buffer_pos = 0; |
| 102 |
| 103 for (int i = 0; i < packet->data_size(); ++i) { |
| 104 int16* pcm_buffer = |
| 105 reinterpret_cast<int16*>(string_as_array(decoded_data) + buffer_pos); |
| 106 CHECK_LE(buffer_pos + max_frame_bytes, |
| 107 static_cast<int>(decoded_data->size())); |
| 108 std::string* frame = packet->mutable_data(i); |
| 109 unsigned char* frame_data = |
| 110 reinterpret_cast<unsigned char*>(string_as_array(frame)); |
| 111 int result = opus_decode(decoder_, frame_data, frame->size(), |
| 112 pcm_buffer, max_frame_samples, 0); |
| 113 if (result < 0) { |
| 114 LOG(ERROR) << "Failed decoding Opus frame. Error code: " << result; |
| 115 DestroyDecoder(); |
| 116 return scoped_ptr<AudioPacket>(); |
| 117 } |
| 118 |
| 119 buffer_pos += result * packet->channels() * |
| 120 decoded_packet->bytes_per_sample(); |
| 121 } |
| 122 |
| 123 if (!buffer_pos) { |
| 124 return scoped_ptr<AudioPacket>(); |
| 125 } |
| 126 |
| 127 decoded_data->resize(buffer_pos); |
| 128 |
| 129 return decoded_packet.Pass(); |
| 130 } |
| 131 |
| 132 } // namespace remoting |
OLD | NEW |