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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 DCHECK_GT(num_channels_, 0); | 45 DCHECK_GT(num_channels_, 0); |
46 DCHECK_GT(samples_per_10ms_, 0); | 46 DCHECK_GT(samples_per_10ms_, 0); |
47 DCHECK_EQ(sampling_rate % 100, 0); | 47 DCHECK_EQ(sampling_rate % 100, 0); |
48 DCHECK_LE(samples_per_10ms_ * num_channels_, | 48 DCHECK_LE(samples_per_10ms_ * num_channels_, |
49 transport::EncodedAudioFrame::kMaxNumberOfSamples); | 49 transport::EncodedAudioFrame::kMaxNumberOfSamples); |
50 | 50 |
51 if (num_channels_ <= 0 || samples_per_10ms_ <= 0 || | 51 if (num_channels_ <= 0 || samples_per_10ms_ <= 0 || |
52 sampling_rate % 100 != 0 || | 52 sampling_rate % 100 != 0 || |
53 samples_per_10ms_ * num_channels_ > | 53 samples_per_10ms_ * num_channels_ > |
54 transport::EncodedAudioFrame::kMaxNumberOfSamples) { | 54 transport::EncodedAudioFrame::kMaxNumberOfSamples) { |
55 initialization_status_ = STATUS_INVALID_AUDIO_CONFIGURATION; | 55 cast_initialization_cb_ = STATUS_INVALID_AUDIO_CONFIGURATION; |
56 } else { | 56 } else { |
57 initialization_status_ = STATUS_INITIALIZED; | 57 cast_initialization_cb_ = STATUS_AUDIO_INITIALIZED; |
58 } | 58 } |
59 } | 59 } |
60 | 60 |
61 virtual ~ImplBase() {} | 61 virtual ~ImplBase() {} |
62 | 62 |
63 CastInitializationStatus InitializationResult() const { | 63 CastInitializationStatus InitializationResult() const { |
64 return initialization_status_; | 64 return cast_initialization_cb_; |
65 } | 65 } |
66 | 66 |
67 void LogAudioFrameEvent(uint32 rtp_timestamp, | 67 void LogAudioFrameEvent(uint32 rtp_timestamp, |
68 uint32 frame_id, | 68 uint32 frame_id, |
69 CastLoggingEvent type) { | 69 CastLoggingEvent type) { |
70 cast_environment_->Logging()->InsertFrameEvent( | 70 cast_environment_->Logging()->InsertFrameEvent( |
71 cast_environment_->Clock()->NowTicks(), type, rtp_timestamp, frame_id); | 71 cast_environment_->Clock()->NowTicks(), type, rtp_timestamp, frame_id); |
72 } | 72 } |
73 | 73 |
74 void EncodeAudio(const AudioBus* audio_bus, | 74 void EncodeAudio(const AudioBus* audio_bus, |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 int source_offset, | 149 int source_offset, |
150 int buffer_fill_offset, | 150 int buffer_fill_offset, |
151 int num_samples) = 0; | 151 int num_samples) = 0; |
152 virtual bool EncodeFromFilledBuffer(std::string* out) = 0; | 152 virtual bool EncodeFromFilledBuffer(std::string* out) = 0; |
153 | 153 |
154 CastEnvironment* const cast_environment_; | 154 CastEnvironment* const cast_environment_; |
155 const transport::AudioCodec codec_; | 155 const transport::AudioCodec codec_; |
156 const int num_channels_; | 156 const int num_channels_; |
157 const int samples_per_10ms_; | 157 const int samples_per_10ms_; |
158 const FrameEncodedCallback callback_; | 158 const FrameEncodedCallback callback_; |
159 CastInitializationStatus initialization_status_; | 159 CastInitializationStatus cast_initialization_cb_; |
160 | 160 |
161 private: | 161 private: |
162 // In the case where a call to EncodeAudio() cannot completely fill the | 162 // In the case where a call to EncodeAudio() cannot completely fill the |
163 // buffer, this points to the position at which to populate data in a later | 163 // buffer, this points to the position at which to populate data in a later |
164 // call. | 164 // call. |
165 int buffer_fill_end_; | 165 int buffer_fill_end_; |
166 | 166 |
167 // A counter used to label EncodedAudioFrames. | 167 // A counter used to label EncodedAudioFrames. |
168 uint32 frame_id_; | 168 uint32 frame_id_; |
169 | 169 |
(...skipping 15 matching lines...) Expand all Loading... |
185 int bitrate, | 185 int bitrate, |
186 const FrameEncodedCallback& callback) | 186 const FrameEncodedCallback& callback) |
187 : ImplBase(cast_environment, | 187 : ImplBase(cast_environment, |
188 transport::kOpus, | 188 transport::kOpus, |
189 num_channels, | 189 num_channels, |
190 sampling_rate, | 190 sampling_rate, |
191 callback), | 191 callback), |
192 encoder_memory_(new uint8[opus_encoder_get_size(num_channels)]), | 192 encoder_memory_(new uint8[opus_encoder_get_size(num_channels)]), |
193 opus_encoder_(reinterpret_cast<OpusEncoder*>(encoder_memory_.get())), | 193 opus_encoder_(reinterpret_cast<OpusEncoder*>(encoder_memory_.get())), |
194 buffer_(new float[num_channels * samples_per_10ms_]) { | 194 buffer_(new float[num_channels * samples_per_10ms_]) { |
195 if (ImplBase::initialization_status_ != STATUS_INITIALIZED) { | 195 if (ImplBase::cast_initialization_cb_ != STATUS_AUDIO_INITIALIZED) { |
196 return; | 196 return; |
197 } | 197 } |
198 | 198 |
199 CHECK_EQ( | 199 CHECK_EQ( |
200 opus_encoder_init( | 200 opus_encoder_init( |
201 opus_encoder_, sampling_rate, num_channels, OPUS_APPLICATION_AUDIO), | 201 opus_encoder_, sampling_rate, num_channels, OPUS_APPLICATION_AUDIO), |
202 OPUS_OK); | 202 OPUS_OK); |
203 if (bitrate <= 0) { | 203 if (bitrate <= 0) { |
204 // Note: As of 2013-10-31, the encoder in "auto bitrate" mode would use a | 204 // Note: As of 2013-10-31, the encoder in "auto bitrate" mode would use a |
205 // variable bitrate up to 102kbps for 2-channel, 48 kHz audio and a 10 ms | 205 // variable bitrate up to 102kbps for 2-channel, 48 kHz audio and a 10 ms |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 }; | 309 }; |
310 | 310 |
311 AudioEncoder::AudioEncoder( | 311 AudioEncoder::AudioEncoder( |
312 const scoped_refptr<CastEnvironment>& cast_environment, | 312 const scoped_refptr<CastEnvironment>& cast_environment, |
313 const AudioSenderConfig& audio_config, | 313 const AudioSenderConfig& audio_config, |
314 const FrameEncodedCallback& frame_encoded_callback) | 314 const FrameEncodedCallback& frame_encoded_callback) |
315 : cast_environment_(cast_environment) { | 315 : cast_environment_(cast_environment) { |
316 // Note: It doesn't matter which thread constructs AudioEncoder, just so long | 316 // Note: It doesn't matter which thread constructs AudioEncoder, just so long |
317 // as all calls to InsertAudio() are by the same thread. | 317 // as all calls to InsertAudio() are by the same thread. |
318 insert_thread_checker_.DetachFromThread(); | 318 insert_thread_checker_.DetachFromThread(); |
319 | |
320 switch (audio_config.codec) { | 319 switch (audio_config.codec) { |
321 case transport::kOpus: | 320 case transport::kOpus: |
322 impl_.reset(new OpusImpl(cast_environment, | 321 impl_.reset(new OpusImpl(cast_environment, |
323 audio_config.channels, | 322 audio_config.channels, |
324 audio_config.frequency, | 323 audio_config.frequency, |
325 audio_config.bitrate, | 324 audio_config.bitrate, |
326 frame_encoded_callback)); | 325 frame_encoded_callback)); |
327 break; | 326 break; |
328 case transport::kPcm16: | 327 case transport::kPcm16: |
329 impl_.reset(new Pcm16Impl(cast_environment, | 328 impl_.reset(new Pcm16Impl(cast_environment, |
330 audio_config.channels, | 329 audio_config.channels, |
331 audio_config.frequency, | 330 audio_config.frequency, |
332 frame_encoded_callback)); | 331 frame_encoded_callback)); |
333 break; | 332 break; |
334 default: | 333 default: |
335 NOTREACHED() << "Unsupported or unspecified codec for audio encoder"; | 334 NOTREACHED() << "Unsupported or unspecified codec for audio encoder"; |
336 break; | 335 break; |
337 } | 336 } |
338 } | 337 } |
339 | 338 |
340 AudioEncoder::~AudioEncoder() {} | 339 AudioEncoder::~AudioEncoder() {} |
341 | 340 |
342 CastInitializationStatus AudioEncoder::InitializationResult() const { | 341 CastInitializationStatus AudioEncoder::InitializationResult() const { |
| 342 DCHECK(insert_thread_checker_.CalledOnValidThread()); |
343 if (impl_) { | 343 if (impl_) { |
344 return impl_->InitializationResult(); | 344 return impl_->InitializationResult(); |
345 } | 345 } |
346 return STATUS_UNSUPPORTED_AUDIO_CODEC; | 346 return STATUS_UNSUPPORTED_AUDIO_CODEC; |
347 } | 347 } |
348 | 348 |
349 void AudioEncoder::InsertAudio(const AudioBus* audio_bus, | 349 void AudioEncoder::InsertAudio(const AudioBus* audio_bus, |
350 const base::TimeTicks& recorded_time, | 350 const base::TimeTicks& recorded_time, |
351 const base::Closure& done_callback) { | 351 const base::Closure& done_callback) { |
352 DCHECK(insert_thread_checker_.CalledOnValidThread()); | 352 DCHECK(insert_thread_checker_.CalledOnValidThread()); |
(...skipping 14 matching lines...) Expand all Loading... |
367 | 367 |
368 void AudioEncoder::EncodeAudio(const AudioBus* audio_bus, | 368 void AudioEncoder::EncodeAudio(const AudioBus* audio_bus, |
369 const base::TimeTicks& recorded_time, | 369 const base::TimeTicks& recorded_time, |
370 const base::Closure& done_callback) { | 370 const base::Closure& done_callback) { |
371 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::AUDIO_ENCODER)); | 371 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::AUDIO_ENCODER)); |
372 impl_->EncodeAudio(audio_bus, recorded_time, done_callback); | 372 impl_->EncodeAudio(audio_bus, recorded_time, done_callback); |
373 } | 373 } |
374 | 374 |
375 } // namespace cast | 375 } // namespace cast |
376 } // namespace media | 376 } // namespace media |
OLD | NEW |