| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "media/cast/audio_receiver/audio_decoder.h" | 5 #include "media/cast/audio_receiver/audio_decoder.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/stl_util.h" | |
| 13 #include "base/sys_byteorder.h" | 12 #include "base/sys_byteorder.h" |
| 14 #include "media/cast/cast_defines.h" | 13 #include "media/cast/cast_defines.h" |
| 15 #include "third_party/opus/src/include/opus.h" | 14 #include "third_party/opus/src/include/opus.h" |
| 16 | 15 |
| 17 namespace media { | 16 namespace media { |
| 18 namespace cast { | 17 namespace cast { |
| 19 | 18 |
| 20 // Base class that handles the common problem of detecting dropped frames, and | 19 // Base class that handles the common problem of detecting dropped frames, and |
| 21 // then invoking the Decode() method implemented by the subclasses to convert | 20 // then invoking the Decode() method implemented by the subclasses to convert |
| 22 // the encoded payload data into usable audio data. | 21 // the encoded payload data into usable audio data. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 33 cast_initialization_status_(STATUS_AUDIO_UNINITIALIZED), | 32 cast_initialization_status_(STATUS_AUDIO_UNINITIALIZED), |
| 34 seen_first_frame_(false) { | 33 seen_first_frame_(false) { |
| 35 if (num_channels_ <= 0 || sampling_rate <= 0 || sampling_rate % 100 != 0) | 34 if (num_channels_ <= 0 || sampling_rate <= 0 || sampling_rate % 100 != 0) |
| 36 cast_initialization_status_ = STATUS_INVALID_AUDIO_CONFIGURATION; | 35 cast_initialization_status_ = STATUS_INVALID_AUDIO_CONFIGURATION; |
| 37 } | 36 } |
| 38 | 37 |
| 39 CastInitializationStatus InitializationResult() const { | 38 CastInitializationStatus InitializationResult() const { |
| 40 return cast_initialization_status_; | 39 return cast_initialization_status_; |
| 41 } | 40 } |
| 42 | 41 |
| 43 void DecodeFrame(scoped_ptr<transport::EncodedAudioFrame> encoded_frame, | 42 void DecodeFrame(scoped_ptr<transport::EncodedFrame> encoded_frame, |
| 44 const DecodeFrameCallback& callback) { | 43 const DecodeFrameCallback& callback) { |
| 45 DCHECK_EQ(cast_initialization_status_, STATUS_AUDIO_INITIALIZED); | 44 DCHECK_EQ(cast_initialization_status_, STATUS_AUDIO_INITIALIZED); |
| 46 | 45 |
| 47 scoped_ptr<AudioBus> decoded_audio; | |
| 48 if (encoded_frame->codec != codec_) { | |
| 49 NOTREACHED(); | |
| 50 cast_environment_->PostTask(CastEnvironment::MAIN, | |
| 51 FROM_HERE, | |
| 52 base::Bind(callback, | |
| 53 base::Passed(&decoded_audio), | |
| 54 false)); | |
| 55 } | |
| 56 | |
| 57 COMPILE_ASSERT(sizeof(encoded_frame->frame_id) == sizeof(last_frame_id_), | 46 COMPILE_ASSERT(sizeof(encoded_frame->frame_id) == sizeof(last_frame_id_), |
| 58 size_of_frame_id_types_do_not_match); | 47 size_of_frame_id_types_do_not_match); |
| 59 bool is_continuous = true; | 48 bool is_continuous = true; |
| 60 if (seen_first_frame_) { | 49 if (seen_first_frame_) { |
| 61 const uint32 frames_ahead = encoded_frame->frame_id - last_frame_id_; | 50 const uint32 frames_ahead = encoded_frame->frame_id - last_frame_id_; |
| 62 if (frames_ahead > 1) { | 51 if (frames_ahead > 1) { |
| 63 RecoverBecauseFramesWereDropped(); | 52 RecoverBecauseFramesWereDropped(); |
| 64 is_continuous = false; | 53 is_continuous = false; |
| 65 } | 54 } |
| 66 } else { | 55 } else { |
| 67 seen_first_frame_ = true; | 56 seen_first_frame_ = true; |
| 68 } | 57 } |
| 69 last_frame_id_ = encoded_frame->frame_id; | 58 last_frame_id_ = encoded_frame->frame_id; |
| 70 | 59 |
| 71 decoded_audio = Decode( | 60 scoped_ptr<AudioBus> decoded_audio = Decode( |
| 72 reinterpret_cast<uint8*>(string_as_array(&encoded_frame->data)), | 61 encoded_frame->mutable_bytes(), |
| 73 static_cast<int>(encoded_frame->data.size())); | 62 static_cast<int>(encoded_frame->data.size())); |
| 74 cast_environment_->PostTask(CastEnvironment::MAIN, | 63 cast_environment_->PostTask(CastEnvironment::MAIN, |
| 75 FROM_HERE, | 64 FROM_HERE, |
| 76 base::Bind(callback, | 65 base::Bind(callback, |
| 77 base::Passed(&decoded_audio), | 66 base::Passed(&decoded_audio), |
| 78 is_continuous)); | 67 is_continuous)); |
| 79 } | 68 } |
| 80 | 69 |
| 81 protected: | 70 protected: |
| 82 friend class base::RefCountedThreadSafe<ImplBase>; | 71 friend class base::RefCountedThreadSafe<ImplBase>; |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 | 221 |
| 233 AudioDecoder::~AudioDecoder() {} | 222 AudioDecoder::~AudioDecoder() {} |
| 234 | 223 |
| 235 CastInitializationStatus AudioDecoder::InitializationResult() const { | 224 CastInitializationStatus AudioDecoder::InitializationResult() const { |
| 236 if (impl_) | 225 if (impl_) |
| 237 return impl_->InitializationResult(); | 226 return impl_->InitializationResult(); |
| 238 return STATUS_UNSUPPORTED_AUDIO_CODEC; | 227 return STATUS_UNSUPPORTED_AUDIO_CODEC; |
| 239 } | 228 } |
| 240 | 229 |
| 241 void AudioDecoder::DecodeFrame( | 230 void AudioDecoder::DecodeFrame( |
| 242 scoped_ptr<transport::EncodedAudioFrame> encoded_frame, | 231 scoped_ptr<transport::EncodedFrame> encoded_frame, |
| 243 const DecodeFrameCallback& callback) { | 232 const DecodeFrameCallback& callback) { |
| 244 DCHECK(encoded_frame.get()); | 233 DCHECK(encoded_frame.get()); |
| 245 DCHECK(!callback.is_null()); | 234 DCHECK(!callback.is_null()); |
| 246 if (!impl_ || impl_->InitializationResult() != STATUS_AUDIO_INITIALIZED) { | 235 if (!impl_ || impl_->InitializationResult() != STATUS_AUDIO_INITIALIZED) { |
| 247 callback.Run(make_scoped_ptr<AudioBus>(NULL), false); | 236 callback.Run(make_scoped_ptr<AudioBus>(NULL), false); |
| 248 return; | 237 return; |
| 249 } | 238 } |
| 250 cast_environment_->PostTask(CastEnvironment::AUDIO, | 239 cast_environment_->PostTask(CastEnvironment::AUDIO, |
| 251 FROM_HERE, | 240 FROM_HERE, |
| 252 base::Bind(&AudioDecoder::ImplBase::DecodeFrame, | 241 base::Bind(&AudioDecoder::ImplBase::DecodeFrame, |
| 253 impl_, | 242 impl_, |
| 254 base::Passed(&encoded_frame), | 243 base::Passed(&encoded_frame), |
| 255 callback)); | 244 callback)); |
| 256 } | 245 } |
| 257 | 246 |
| 258 } // namespace cast | 247 } // namespace cast |
| 259 } // namespace media | 248 } // namespace media |
| OLD | NEW |