| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/sender/audio_sender.h" | 5 #include "media/cast/sender/audio_sender.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/net/cast_transport_config.h" | 11 #include "media/cast/net/cast_transport_config.h" |
| 12 #include "media/cast/sender/audio_encoder.h" | 12 #include "media/cast/sender/audio_encoder.h" |
| 13 | 13 |
| 14 namespace media { | 14 namespace media { |
| 15 namespace cast { | 15 namespace cast { |
| 16 | 16 |
| 17 AudioSender::AudioSender(scoped_refptr<CastEnvironment> cast_environment, | 17 AudioSender::AudioSender(scoped_refptr<CastEnvironment> cast_environment, |
| 18 const AudioSenderConfig& audio_config, | 18 const AudioSenderConfig& audio_config, |
| 19 const StatusChangeCallback& status_change_cb, |
| 19 CastTransportSender* const transport_sender) | 20 CastTransportSender* const transport_sender) |
| 20 : FrameSender(cast_environment, | 21 : FrameSender(cast_environment, |
| 21 true, | 22 true, |
| 22 transport_sender, | 23 transport_sender, |
| 23 base::TimeDelta::FromMilliseconds(audio_config.rtcp_interval), | 24 base::TimeDelta::FromMilliseconds(audio_config.rtcp_interval), |
| 24 audio_config.frequency, | 25 audio_config.frequency, |
| 25 audio_config.ssrc, | 26 audio_config.ssrc, |
| 26 0, // |max_frame_rate_| is set after encoder initialization. | 27 0, // |max_frame_rate_| is set after encoder initialization. |
| 27 audio_config.min_playout_delay, | 28 audio_config.min_playout_delay, |
| 28 audio_config.max_playout_delay, | 29 audio_config.max_playout_delay, |
| 29 NewFixedCongestionControl(audio_config.bitrate)), | 30 NewFixedCongestionControl(audio_config.bitrate)), |
| 30 samples_in_encoder_(0), | 31 samples_in_encoder_(0), |
| 31 weak_factory_(this) { | 32 weak_factory_(this) { |
| 32 cast_initialization_status_ = STATUS_AUDIO_UNINITIALIZED; | |
| 33 | |
| 34 if (!audio_config.use_external_encoder) { | 33 if (!audio_config.use_external_encoder) { |
| 35 audio_encoder_.reset( | 34 audio_encoder_.reset( |
| 36 new AudioEncoder(cast_environment, | 35 new AudioEncoder(cast_environment, |
| 37 audio_config.channels, | 36 audio_config.channels, |
| 38 audio_config.frequency, | 37 audio_config.frequency, |
| 39 audio_config.bitrate, | 38 audio_config.bitrate, |
| 40 audio_config.codec, | 39 audio_config.codec, |
| 41 base::Bind(&AudioSender::OnEncodedAudioFrame, | 40 base::Bind(&AudioSender::OnEncodedAudioFrame, |
| 42 weak_factory_.GetWeakPtr(), | 41 weak_factory_.GetWeakPtr(), |
| 43 audio_config.bitrate))); | 42 audio_config.bitrate))); |
| 44 cast_initialization_status_ = audio_encoder_->InitializationResult(); | |
| 45 } else { | |
| 46 NOTREACHED(); // No support for external audio encoding. | |
| 47 cast_initialization_status_ = STATUS_AUDIO_UNINITIALIZED; | |
| 48 } | 43 } |
| 49 | 44 |
| 45 // AudioEncoder provides no operational status changes during normal use. |
| 46 // Post a task now with its initialization result status to allow the client |
| 47 // to start sending frames. |
| 48 cast_environment_->PostTask( |
| 49 CastEnvironment::MAIN, |
| 50 FROM_HERE, |
| 51 base::Bind(status_change_cb, |
| 52 audio_encoder_ ? audio_encoder_->InitializationResult() : |
| 53 STATUS_INVALID_CONFIGURATION)); |
| 54 |
| 50 // The number of samples per encoded audio frame depends on the codec and its | 55 // The number of samples per encoded audio frame depends on the codec and its |
| 51 // initialization parameters. Now that we have an encoder, we can calculate | 56 // initialization parameters. Now that we have an encoder, we can calculate |
| 52 // the maximum frame rate. | 57 // the maximum frame rate. |
| 53 max_frame_rate_ = | 58 max_frame_rate_ = |
| 54 audio_config.frequency / audio_encoder_->GetSamplesPerFrame(); | 59 audio_config.frequency / audio_encoder_->GetSamplesPerFrame(); |
| 55 | 60 |
| 56 media::cast::CastTransportRtpConfig transport_config; | 61 media::cast::CastTransportRtpConfig transport_config; |
| 57 transport_config.ssrc = audio_config.ssrc; | 62 transport_config.ssrc = audio_config.ssrc; |
| 58 transport_config.feedback_ssrc = audio_config.receiver_ssrc; | 63 transport_config.feedback_ssrc = audio_config.receiver_ssrc; |
| 59 transport_config.rtp_payload_type = audio_config.rtp_payload_type; | 64 transport_config.rtp_payload_type = audio_config.rtp_payload_type; |
| 60 transport_config.aes_key = audio_config.aes_key; | 65 transport_config.aes_key = audio_config.aes_key; |
| 61 transport_config.aes_iv_mask = audio_config.aes_iv_mask; | 66 transport_config.aes_iv_mask = audio_config.aes_iv_mask; |
| 62 | 67 |
| 63 transport_sender->InitializeAudio( | 68 transport_sender->InitializeAudio( |
| 64 transport_config, | 69 transport_config, |
| 65 base::Bind(&AudioSender::OnReceivedCastFeedback, | 70 base::Bind(&AudioSender::OnReceivedCastFeedback, |
| 66 weak_factory_.GetWeakPtr()), | 71 weak_factory_.GetWeakPtr()), |
| 67 base::Bind(&AudioSender::OnMeasuredRoundTripTime, | 72 base::Bind(&AudioSender::OnMeasuredRoundTripTime, |
| 68 weak_factory_.GetWeakPtr())); | 73 weak_factory_.GetWeakPtr())); |
| 69 } | 74 } |
| 70 | 75 |
| 71 AudioSender::~AudioSender() {} | 76 AudioSender::~AudioSender() {} |
| 72 | 77 |
| 73 void AudioSender::InsertAudio(scoped_ptr<AudioBus> audio_bus, | 78 void AudioSender::InsertAudio(scoped_ptr<AudioBus> audio_bus, |
| 74 const base::TimeTicks& recorded_time) { | 79 const base::TimeTicks& recorded_time) { |
| 75 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 80 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 76 if (cast_initialization_status_ != STATUS_AUDIO_INITIALIZED) { | 81 |
| 82 if (!audio_encoder_) { |
| 77 NOTREACHED(); | 83 NOTREACHED(); |
| 78 return; | 84 return; |
| 79 } | 85 } |
| 80 DCHECK(audio_encoder_.get()) << "Invalid internal state"; | |
| 81 | 86 |
| 82 const base::TimeDelta next_frame_duration = | 87 const base::TimeDelta next_frame_duration = |
| 83 RtpDeltaToTimeDelta(audio_bus->frames(), rtp_timebase()); | 88 RtpDeltaToTimeDelta(audio_bus->frames(), rtp_timebase()); |
| 84 if (ShouldDropNextFrame(next_frame_duration)) | 89 if (ShouldDropNextFrame(next_frame_duration)) |
| 85 return; | 90 return; |
| 86 | 91 |
| 87 samples_in_encoder_ += audio_bus->frames(); | 92 samples_in_encoder_ += audio_bus->frames(); |
| 88 | 93 |
| 89 audio_encoder_->InsertAudio(audio_bus.Pass(), recorded_time); | 94 audio_encoder_->InsertAudio(audio_bus.Pass(), recorded_time); |
| 90 } | 95 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 111 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 116 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 112 | 117 |
| 113 samples_in_encoder_ -= audio_encoder_->GetSamplesPerFrame() + samples_skipped; | 118 samples_in_encoder_ -= audio_encoder_->GetSamplesPerFrame() + samples_skipped; |
| 114 DCHECK_GE(samples_in_encoder_, 0); | 119 DCHECK_GE(samples_in_encoder_, 0); |
| 115 | 120 |
| 116 SendEncodedFrame(encoder_bitrate, encoded_frame.Pass()); | 121 SendEncodedFrame(encoder_bitrate, encoded_frame.Pass()); |
| 117 } | 122 } |
| 118 | 123 |
| 119 } // namespace cast | 124 } // namespace cast |
| 120 } // namespace media | 125 } // namespace media |
| OLD | NEW |