| 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_sender/audio_encoder.h" | 5 #include "media/cast/audio_sender/audio_encoder.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "media/cast/cast_defines.h" | 10 #include "media/cast/cast_defines.h" |
| 11 #include "media/cast/cast_environment.h" | 11 #include "media/cast/cast_environment.h" |
| 12 #include "third_party/webrtc/modules/audio_coding/main/interface/audio_coding_mo
dule.h" | 12 #include "third_party/webrtc/modules/audio_coding/main/interface/audio_coding_mo
dule.h" |
| 13 #include "third_party/webrtc/modules/interface/module_common_types.h" | 13 #include "third_party/webrtc/modules/interface/module_common_types.h" |
| 14 | 14 |
| 15 namespace media { | 15 namespace media { |
| 16 namespace cast { | 16 namespace cast { |
| 17 | 17 |
| 18 // 48KHz, 2 channels and 100 ms. | 18 // 48KHz, 2 channels and 100 ms. |
| 19 static const int kMaxNumberOfSamples = 48 * 2 * 100; | 19 static const int kMaxNumberOfSamples = 48 * 2 * 100; |
| 20 | 20 |
| 21 // This class is only called from the cast audio encoder thread. | 21 // This class is only called from the cast audio encoder thread. |
| 22 class WebrtEncodedDataCallback : public webrtc::AudioPacketizationCallback { | 22 class WebrtEncodedDataCallback : public webrtc::AudioPacketizationCallback { |
| 23 public: | 23 public: |
| 24 WebrtEncodedDataCallback(scoped_refptr<CastEnvironment> cast_environment, | 24 WebrtEncodedDataCallback(scoped_refptr<CastEnvironment> cast_environment, |
| 25 AudioCodec codec, | 25 AudioCodec codec, |
| 26 int frequency) | 26 int frequency) |
| 27 : codec_(codec), | 27 : codec_(codec), |
| 28 frequency_(frequency), | 28 frequency_(frequency), |
| 29 cast_environment_(cast_environment), | 29 cast_environment_(cast_environment), |
| 30 last_timestamp_(0) {} | 30 last_timestamp_(0), |
| 31 frame_id_(0) {} |
| 31 | 32 |
| 32 virtual int32 SendData( | 33 virtual int32 SendData( |
| 33 webrtc::FrameType /*frame_type*/, | 34 webrtc::FrameType /*frame_type*/, |
| 34 uint8 /*payload_type*/, | 35 uint8 /*payload_type*/, |
| 35 uint32 timestamp, | 36 uint32 timestamp, |
| 36 const uint8* payload_data, | 37 const uint8* payload_data, |
| 37 uint16 payload_size, | 38 uint16 payload_size, |
| 38 const webrtc::RTPFragmentationHeader* /*fragmentation*/) OVERRIDE { | 39 const webrtc::RTPFragmentationHeader* /*fragmentation*/) OVERRIDE { |
| 39 scoped_ptr<EncodedAudioFrame> audio_frame(new EncodedAudioFrame()); | 40 scoped_ptr<EncodedAudioFrame> audio_frame(new EncodedAudioFrame()); |
| 40 audio_frame->codec = codec_; | 41 audio_frame->codec = codec_; |
| 41 audio_frame->samples = timestamp - last_timestamp_; | 42 audio_frame->samples = timestamp - last_timestamp_; |
| 43 audio_frame->frame_id = frame_id_++; |
| 42 DCHECK(audio_frame->samples <= kMaxNumberOfSamples); | 44 DCHECK(audio_frame->samples <= kMaxNumberOfSamples); |
| 43 last_timestamp_ = timestamp; | 45 last_timestamp_ = timestamp; |
| 44 audio_frame->data.insert(audio_frame->data.begin(), | 46 audio_frame->data.insert(audio_frame->data.begin(), |
| 45 payload_data, | 47 payload_data, |
| 46 payload_data + payload_size); | 48 payload_data + payload_size); |
| 47 | 49 |
| 48 cast_environment_->PostTask(CastEnvironment::MAIN, FROM_HERE, | 50 cast_environment_->PostTask(CastEnvironment::MAIN, FROM_HERE, |
| 49 base::Bind(*frame_encoded_callback_, base::Passed(&audio_frame), | 51 base::Bind(*frame_encoded_callback_, base::Passed(&audio_frame), |
| 50 recorded_time_)); | 52 recorded_time_)); |
| 51 return 0; | 53 return 0; |
| 52 } | 54 } |
| 53 | 55 |
| 54 void SetEncodedCallbackInfo( | 56 void SetEncodedCallbackInfo( |
| 55 const base::TimeTicks& recorded_time, | 57 const base::TimeTicks& recorded_time, |
| 56 const AudioEncoder::FrameEncodedCallback* frame_encoded_callback) { | 58 const AudioEncoder::FrameEncodedCallback* frame_encoded_callback) { |
| 57 recorded_time_ = recorded_time; | 59 recorded_time_ = recorded_time; |
| 58 frame_encoded_callback_ = frame_encoded_callback; | 60 frame_encoded_callback_ = frame_encoded_callback; |
| 59 } | 61 } |
| 60 | 62 |
| 61 private: | 63 private: |
| 62 const AudioCodec codec_; | 64 const AudioCodec codec_; |
| 63 const int frequency_; | 65 const int frequency_; |
| 64 scoped_refptr<CastEnvironment> cast_environment_; | 66 scoped_refptr<CastEnvironment> cast_environment_; |
| 65 uint32 last_timestamp_; | 67 uint32 last_timestamp_; |
| 68 uint32 frame_id_; |
| 66 base::TimeTicks recorded_time_; | 69 base::TimeTicks recorded_time_; |
| 67 const AudioEncoder::FrameEncodedCallback* frame_encoded_callback_; | 70 const AudioEncoder::FrameEncodedCallback* frame_encoded_callback_; |
| 68 }; | 71 }; |
| 69 | 72 |
| 70 AudioEncoder::AudioEncoder(scoped_refptr<CastEnvironment> cast_environment, | 73 AudioEncoder::AudioEncoder(scoped_refptr<CastEnvironment> cast_environment, |
| 71 const AudioSenderConfig& audio_config) | 74 const AudioSenderConfig& audio_config) |
| 72 : cast_environment_(cast_environment), | 75 : cast_environment_(cast_environment), |
| 73 audio_encoder_(webrtc::AudioCodingModule::Create(0)), | 76 audio_encoder_(webrtc::AudioCodingModule::Create(0)), |
| 74 webrtc_encoder_callback_( | 77 webrtc_encoder_callback_( |
| 75 new WebrtEncodedDataCallback(cast_environment, audio_config.codec, | 78 new WebrtEncodedDataCallback(cast_environment, audio_config.codec, |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 // Note: Not all insert of 10 ms will generate a callback with encoded data. | 169 // Note: Not all insert of 10 ms will generate a callback with encoded data. |
| 167 webrtc_encoder_callback_->SetEncodedCallbackInfo(recorded_time, | 170 webrtc_encoder_callback_->SetEncodedCallbackInfo(recorded_time, |
| 168 &frame_encoded_callback); | 171 &frame_encoded_callback); |
| 169 for (size_t i = 0; i < number_of_10ms_blocks; ++i) { | 172 for (size_t i = 0; i < number_of_10ms_blocks; ++i) { |
| 170 audio_encoder_->Process(); | 173 audio_encoder_->Process(); |
| 171 } | 174 } |
| 172 } | 175 } |
| 173 | 176 |
| 174 } // namespace cast | 177 } // namespace cast |
| 175 } // namespace media | 178 } // namespace media |
| OLD | NEW |