| 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_receiver.h" | 5 #include "media/cast/audio_receiver/audio_receiver.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 "crypto/encryptor.h" | 10 #include "crypto/encryptor.h" |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 | 284 |
| 285 void AudioReceiver::PlayoutTimeout() { | 285 void AudioReceiver::PlayoutTimeout() { |
| 286 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 286 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 287 DCHECK(audio_buffer_) << "Invalid function call in this configuration"; | 287 DCHECK(audio_buffer_) << "Invalid function call in this configuration"; |
| 288 if (queued_encoded_callbacks_.empty()) { | 288 if (queued_encoded_callbacks_.empty()) { |
| 289 // Already released by incoming packet. | 289 // Already released by incoming packet. |
| 290 return; | 290 return; |
| 291 } | 291 } |
| 292 uint32 rtp_timestamp = 0; | 292 uint32 rtp_timestamp = 0; |
| 293 bool next_frame = false; | 293 bool next_frame = false; |
| 294 scoped_ptr<transport::EncodedAudioFrame> encoded_frame( | 294 scoped_ptr<EncodedAudioFrame> encoded_frame(new EncodedAudioFrame()); |
| 295 new transport::EncodedAudioFrame()); | |
| 296 | 295 |
| 297 if (!audio_buffer_->GetEncodedAudioFrame(encoded_frame.get(), | 296 if (!audio_buffer_->GetEncodedAudioFrame(encoded_frame.get(), |
| 298 &rtp_timestamp, &next_frame)) { | 297 &rtp_timestamp, &next_frame)) { |
| 299 // We have no audio frames. Wait for new packet(s). | 298 // We have no audio frames. Wait for new packet(s). |
| 300 // Since the application can post multiple AudioFrameEncodedCallback and | 299 // Since the application can post multiple AudioFrameEncodedCallback and |
| 301 // we only check the next frame to play out we might have multiple timeout | 300 // we only check the next frame to play out we might have multiple timeout |
| 302 // events firing after each other; however this should be a rare event. | 301 // events firing after each other; however this should be a rare event. |
| 303 VLOG(1) << "Failed to retrieved a complete frame at this point in time"; | 302 VLOG(1) << "Failed to retrieved a complete frame at this point in time"; |
| 304 return; | 303 return; |
| 305 } | 304 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 316 } | 315 } |
| 317 } | 316 } |
| 318 | 317 |
| 319 void AudioReceiver::GetEncodedAudioFrame( | 318 void AudioReceiver::GetEncodedAudioFrame( |
| 320 const AudioFrameEncodedCallback& callback) { | 319 const AudioFrameEncodedCallback& callback) { |
| 321 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 320 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 322 DCHECK(audio_buffer_) << "Invalid function call in this configuration"; | 321 DCHECK(audio_buffer_) << "Invalid function call in this configuration"; |
| 323 | 322 |
| 324 uint32 rtp_timestamp = 0; | 323 uint32 rtp_timestamp = 0; |
| 325 bool next_frame = false; | 324 bool next_frame = false; |
| 326 scoped_ptr<transport::EncodedAudioFrame> encoded_frame( | 325 scoped_ptr<EncodedAudioFrame> encoded_frame(new EncodedAudioFrame()); |
| 327 new transport::EncodedAudioFrame()); | |
| 328 | 326 |
| 329 if (!audio_buffer_->GetEncodedAudioFrame(encoded_frame.get(), | 327 if (!audio_buffer_->GetEncodedAudioFrame(encoded_frame.get(), |
| 330 &rtp_timestamp, &next_frame)) { | 328 &rtp_timestamp, &next_frame)) { |
| 331 // We have no audio frames. Wait for new packet(s). | 329 // We have no audio frames. Wait for new packet(s). |
| 332 VLOG(1) << "Wait for more audio packets in frame"; | 330 VLOG(1) << "Wait for more audio packets in frame"; |
| 333 queued_encoded_callbacks_.push_back(callback); | 331 queued_encoded_callbacks_.push_back(callback); |
| 334 return; | 332 return; |
| 335 } | 333 } |
| 336 if (decryptor_ && !DecryptAudioFrame(&encoded_frame)) { | 334 if (decryptor_ && !DecryptAudioFrame(&encoded_frame)) { |
| 337 // Logging already done. | 335 // Logging already done. |
| 338 queued_encoded_callbacks_.push_back(callback); | 336 queued_encoded_callbacks_.push_back(callback); |
| 339 return; | 337 return; |
| 340 } | 338 } |
| 341 if (!PostEncodedAudioFrame(callback, rtp_timestamp, next_frame, | 339 if (!PostEncodedAudioFrame(callback, rtp_timestamp, next_frame, |
| 342 &encoded_frame)) { | 340 &encoded_frame)) { |
| 343 // We have an audio frame; however we are missing packets and we have time | 341 // We have an audio frame; however we are missing packets and we have time |
| 344 // to wait for new packet(s). | 342 // to wait for new packet(s). |
| 345 queued_encoded_callbacks_.push_back(callback); | 343 queued_encoded_callbacks_.push_back(callback); |
| 346 } | 344 } |
| 347 } | 345 } |
| 348 | 346 |
| 349 bool AudioReceiver::PostEncodedAudioFrame( | 347 bool AudioReceiver::PostEncodedAudioFrame( |
| 350 const AudioFrameEncodedCallback& callback, | 348 const AudioFrameEncodedCallback& callback, |
| 351 uint32 rtp_timestamp, | 349 uint32 rtp_timestamp, |
| 352 bool next_frame, | 350 bool next_frame, |
| 353 scoped_ptr<transport::EncodedAudioFrame>* encoded_frame) { | 351 scoped_ptr<EncodedAudioFrame>* encoded_frame) { |
| 354 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 352 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 355 DCHECK(audio_buffer_) << "Invalid function call in this configuration"; | 353 DCHECK(audio_buffer_) << "Invalid function call in this configuration"; |
| 356 | 354 |
| 357 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); | 355 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); |
| 358 base::TimeTicks playout_time = GetPlayoutTime(now, rtp_timestamp); | 356 base::TimeTicks playout_time = GetPlayoutTime(now, rtp_timestamp); |
| 359 base::TimeDelta time_until_playout = playout_time - now; | 357 base::TimeDelta time_until_playout = playout_time - now; |
| 360 base::TimeDelta min_wait_delta = | 358 base::TimeDelta min_wait_delta = |
| 361 base::TimeDelta::FromMilliseconds(kMaxAudioFrameWaitMs); | 359 base::TimeDelta::FromMilliseconds(kMaxAudioFrameWaitMs); |
| 362 | 360 |
| 363 if (!next_frame && (time_until_playout > min_wait_delta)) { | 361 if (!next_frame && (time_until_playout > min_wait_delta)) { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 rtp_timestamp_in_ticks + time_offset_ + target_delay_delta_ : now; | 425 rtp_timestamp_in_ticks + time_offset_ + target_delay_delta_ : now; |
| 428 } | 426 } |
| 429 // Don't allow the playout time to go backwards. | 427 // Don't allow the playout time to go backwards. |
| 430 if (last_playout_time_ > playout_time) | 428 if (last_playout_time_ > playout_time) |
| 431 playout_time = last_playout_time_; | 429 playout_time = last_playout_time_; |
| 432 last_playout_time_ = playout_time; | 430 last_playout_time_ = playout_time; |
| 433 return playout_time; | 431 return playout_time; |
| 434 } | 432 } |
| 435 | 433 |
| 436 bool AudioReceiver::DecryptAudioFrame( | 434 bool AudioReceiver::DecryptAudioFrame( |
| 437 scoped_ptr<transport::EncodedAudioFrame>* audio_frame) { | 435 scoped_ptr<EncodedAudioFrame>* audio_frame) { |
| 438 DCHECK(decryptor_) << "Invalid state"; | 436 DCHECK(decryptor_) << "Invalid state"; |
| 439 | 437 |
| 440 if (!decryptor_->SetCounter(GetAesNonce((*audio_frame)->frame_id, | 438 if (!decryptor_->SetCounter(GetAesNonce((*audio_frame)->frame_id, |
| 441 iv_mask_))) { | 439 iv_mask_))) { |
| 442 NOTREACHED() << "Failed to set counter"; | 440 NOTREACHED() << "Failed to set counter"; |
| 443 return false; | 441 return false; |
| 444 } | 442 } |
| 445 std::string decrypted_audio_data; | 443 std::string decrypted_audio_data; |
| 446 if (!decryptor_->Decrypt((*audio_frame)->data, &decrypted_audio_data)) { | 444 if (!decryptor_->Decrypt((*audio_frame)->data, &decrypted_audio_data)) { |
| 447 VLOG(0) << "Decryption error"; | 445 VLOG(0) << "Decryption error"; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 } | 501 } |
| 504 if (audio_decoder_) { | 502 if (audio_decoder_) { |
| 505 // Will only send a message if it is time. | 503 // Will only send a message if it is time. |
| 506 audio_decoder_->SendCastMessage(); | 504 audio_decoder_->SendCastMessage(); |
| 507 } | 505 } |
| 508 ScheduleNextCastMessage(); | 506 ScheduleNextCastMessage(); |
| 509 } | 507 } |
| 510 | 508 |
| 511 } // namespace cast | 509 } // namespace cast |
| 512 } // namespace media | 510 } // namespace media |
| OLD | NEW |