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" |
(...skipping 17 matching lines...) Expand all Loading... |
28 frequency_(frequency), | 28 frequency_(frequency), |
29 cast_thread_(cast_thread), | 29 cast_thread_(cast_thread), |
30 last_timestamp_(0) {} | 30 last_timestamp_(0) {} |
31 | 31 |
32 virtual int32 SendData( | 32 virtual int32 SendData( |
33 webrtc::FrameType /*frame_type*/, | 33 webrtc::FrameType /*frame_type*/, |
34 uint8 /*payload_type*/, | 34 uint8 /*payload_type*/, |
35 uint32 timestamp, | 35 uint32 timestamp, |
36 const uint8* payload_data, | 36 const uint8* payload_data, |
37 uint16 payload_size, | 37 uint16 payload_size, |
38 const webrtc::RTPFragmentationHeader* /*fragmentation*/) { | 38 const webrtc::RTPFragmentationHeader* /*fragmentation*/) OVERRIDE { |
39 scoped_ptr<EncodedAudioFrame> audio_frame(new EncodedAudioFrame()); | 39 scoped_ptr<EncodedAudioFrame> audio_frame(new EncodedAudioFrame()); |
40 audio_frame->codec = codec_; | 40 audio_frame->codec = codec_; |
41 audio_frame->samples = timestamp - last_timestamp_; | 41 audio_frame->samples = timestamp - last_timestamp_; |
42 DCHECK(audio_frame->samples <= kMaxNumberOfSamples); | 42 DCHECK(audio_frame->samples <= kMaxNumberOfSamples); |
43 last_timestamp_ = timestamp; | 43 last_timestamp_ = timestamp; |
44 audio_frame->data.insert(audio_frame->data.begin(), | 44 audio_frame->data.insert(audio_frame->data.begin(), |
45 payload_data, | 45 payload_data, |
46 payload_data + payload_size); | 46 payload_data + payload_size); |
47 | 47 |
48 cast_thread_->PostTask(CastThread::MAIN, FROM_HERE, | 48 cast_thread_->PostTask(CastThread::MAIN, FROM_HERE, |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 default: | 103 default: |
104 DCHECK(false) << "Codec must be specified for audio encoder"; | 104 DCHECK(false) << "Codec must be specified for audio encoder"; |
105 return; | 105 return; |
106 } | 106 } |
107 if (audio_encoder_->RegisterSendCodec(send_codec) != 0) { | 107 if (audio_encoder_->RegisterSendCodec(send_codec) != 0) { |
108 DCHECK(false) << "Invalid webrtc return value; failed to register codec"; | 108 DCHECK(false) << "Invalid webrtc return value; failed to register codec"; |
109 } | 109 } |
110 } | 110 } |
111 | 111 |
112 AudioEncoder::~AudioEncoder() { | 112 AudioEncoder::~AudioEncoder() { |
113 webrtc::AudioCodingModule::Destroy(audio_encoder_); | |
114 } | 113 } |
115 | 114 |
116 // Called from main cast thread. | 115 // Called from main cast thread. |
117 void AudioEncoder::InsertRawAudioFrame( | 116 void AudioEncoder::InsertRawAudioFrame( |
118 const PcmAudioFrame* audio_frame, | 117 const PcmAudioFrame* audio_frame, |
119 const base::TimeTicks& recorded_time, | 118 const base::TimeTicks& recorded_time, |
120 const FrameEncodedCallback& frame_encoded_callback, | 119 const FrameEncodedCallback& frame_encoded_callback, |
121 const base::Closure release_callback) { | 120 const base::Closure release_callback) { |
122 cast_thread_->PostTask(CastThread::AUDIO_ENCODER, FROM_HERE, | 121 cast_thread_->PostTask(CastThread::AUDIO_ENCODER, FROM_HERE, |
123 base::Bind(&AudioEncoder::EncodeAudioFrameThread, this, audio_frame, | 122 base::Bind(&AudioEncoder::EncodeAudioFrameThread, this, audio_frame, |
124 recorded_time, frame_encoded_callback, release_callback)); | 123 recorded_time, frame_encoded_callback, release_callback)); |
125 } | 124 } |
126 | 125 |
127 // Called from cast audio encoder thread. | 126 // Called from cast audio encoder thread. |
128 void AudioEncoder::EncodeAudioFrameThread( | 127 void AudioEncoder::EncodeAudioFrameThread( |
129 const PcmAudioFrame* audio_frame, | 128 const PcmAudioFrame* audio_frame, |
130 const base::TimeTicks& recorded_time, | 129 const base::TimeTicks& recorded_time, |
131 const FrameEncodedCallback& frame_encoded_callback, | 130 const FrameEncodedCallback& frame_encoded_callback, |
132 const base::Closure release_callback) { | 131 const base::Closure release_callback) { |
133 DCHECK(cast_thread_->CurrentlyOn(CastThread::AUDIO_ENCODER)); | 132 DCHECK(cast_thread_->CurrentlyOn(CastThread::AUDIO_ENCODER)); |
134 int samples_per_10ms = audio_frame->frequency / 100; | 133 int samples_per_10ms = audio_frame->frequency / 100; |
135 int number_of_10ms_blocks = audio_frame->samples.size() / | 134 size_t number_of_10ms_blocks = audio_frame->samples.size() / |
136 (samples_per_10ms * audio_frame->channels); | 135 (samples_per_10ms * audio_frame->channels); |
137 DCHECK(webrtc::AudioFrame::kMaxDataSizeSamples > samples_per_10ms) | 136 DCHECK(webrtc::AudioFrame::kMaxDataSizeSamples > samples_per_10ms) |
138 << "webrtc sanity check failed"; | 137 << "webrtc sanity check failed"; |
139 | 138 |
140 for (int i = 0; i < number_of_10ms_blocks; ++i) { | 139 for (size_t i = 0; i < number_of_10ms_blocks; ++i) { |
141 webrtc::AudioFrame webrtc_audio_frame; | 140 webrtc::AudioFrame webrtc_audio_frame; |
142 webrtc_audio_frame.timestamp_ = timestamp_; | 141 webrtc_audio_frame.timestamp_ = timestamp_; |
143 | 142 |
144 // Due to the webrtc::AudioFrame declaration we need to copy our data into | 143 // Due to the webrtc::AudioFrame declaration we need to copy our data into |
145 // the webrtc structure. | 144 // the webrtc structure. |
146 memcpy(&webrtc_audio_frame.data_[0], | 145 memcpy(&webrtc_audio_frame.data_[0], |
147 &audio_frame->samples[i * samples_per_10ms * audio_frame->channels], | 146 &audio_frame->samples[i * samples_per_10ms * audio_frame->channels], |
148 samples_per_10ms * audio_frame->channels * sizeof(int16)); | 147 samples_per_10ms * audio_frame->channels * sizeof(int16)); |
149 webrtc_audio_frame.samples_per_channel_ = samples_per_10ms; | 148 webrtc_audio_frame.samples_per_channel_ = samples_per_10ms; |
150 webrtc_audio_frame.sample_rate_hz_ = audio_frame->frequency; | 149 webrtc_audio_frame.sample_rate_hz_ = audio_frame->frequency; |
151 webrtc_audio_frame.num_channels_ = audio_frame->channels; | 150 webrtc_audio_frame.num_channels_ = audio_frame->channels; |
152 | 151 |
153 // webrtc::AudioCodingModule is thread safe. | 152 // webrtc::AudioCodingModule is thread safe. |
154 if (audio_encoder_->Add10MsData(webrtc_audio_frame) != 0) { | 153 if (audio_encoder_->Add10MsData(webrtc_audio_frame) != 0) { |
155 DCHECK(false) << "Invalid webrtc return value"; | 154 DCHECK(false) << "Invalid webrtc return value"; |
156 } | 155 } |
157 timestamp_ += samples_per_10ms; | 156 timestamp_ += samples_per_10ms; |
158 } | 157 } |
159 // We are done with the audio frame release it. | 158 // We are done with the audio frame release it. |
160 cast_thread_->PostTask(CastThread::MAIN, FROM_HERE, release_callback); | 159 cast_thread_->PostTask(CastThread::MAIN, FROM_HERE, release_callback); |
161 | 160 |
162 // Note: | 161 // Note: |
163 // Not all insert of 10 ms will generate a callback with encoded data. | 162 // Not all insert of 10 ms will generate a callback with encoded data. |
164 webrtc_encoder_callback_->SetEncodedCallbackInfo(recorded_time, | 163 webrtc_encoder_callback_->SetEncodedCallbackInfo(recorded_time, |
165 &frame_encoded_callback); | 164 &frame_encoded_callback); |
166 for (int i = 0; i < number_of_10ms_blocks; ++i) { | 165 for (size_t i = 0; i < number_of_10ms_blocks; ++i) { |
167 audio_encoder_->Process(); | 166 audio_encoder_->Process(); |
168 } | 167 } |
169 } | 168 } |
170 | 169 |
171 } // namespace media | 170 } // namespace media |
172 } // namespace cast | 171 } // namespace cast |
OLD | NEW |