| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 : FrameSender(cast_environment, | 23 : FrameSender(cast_environment, |
| 24 transport_sender, | 24 transport_sender, |
| 25 audio_config, | 25 audio_config, |
| 26 NewFixedCongestionControl(audio_config.max_bitrate)), | 26 NewFixedCongestionControl(audio_config.max_bitrate)), |
| 27 samples_in_encoder_(0), | 27 samples_in_encoder_(0), |
| 28 weak_factory_(this) { | 28 weak_factory_(this) { |
| 29 if (!audio_config.use_external_encoder) { | 29 if (!audio_config.use_external_encoder) { |
| 30 audio_encoder_.reset(new AudioEncoder( | 30 audio_encoder_.reset(new AudioEncoder( |
| 31 cast_environment, audio_config.channels, audio_config.rtp_timebase, | 31 cast_environment, audio_config.channels, audio_config.rtp_timebase, |
| 32 audio_config.max_bitrate, audio_config.codec, | 32 audio_config.max_bitrate, audio_config.codec, |
| 33 base::Bind(&AudioSender::OnEncodedAudioFrame, | 33 base::Bind(&AudioSender::OnEncodedAudioFrame, AsWeakPtr(), |
| 34 weak_factory_.GetWeakPtr(), audio_config.max_bitrate))); | 34 audio_config.max_bitrate))); |
| 35 } | 35 } |
| 36 | 36 |
| 37 // AudioEncoder provides no operational status changes during normal use. | 37 // AudioEncoder provides no operational status changes during normal use. |
| 38 // Post a task now with its initialization result status to allow the client | 38 // Post a task now with its initialization result status to allow the client |
| 39 // to start sending frames. | 39 // to start sending frames. |
| 40 cast_environment_->PostTask( | 40 cast_environment_->PostTask( |
| 41 CastEnvironment::MAIN, | 41 CastEnvironment::MAIN, |
| 42 FROM_HERE, | 42 FROM_HERE, |
| 43 base::Bind(status_change_cb, | 43 base::Bind(status_change_cb, |
| 44 audio_encoder_ ? audio_encoder_->InitializationResult() : | 44 audio_encoder_ ? audio_encoder_->InitializationResult() : |
| (...skipping 20 matching lines...) Expand all Loading... |
| 65 const base::TimeDelta next_frame_duration = | 65 const base::TimeDelta next_frame_duration = |
| 66 RtpTimeDelta::FromTicks(audio_bus->frames()).ToTimeDelta(rtp_timebase()); | 66 RtpTimeDelta::FromTicks(audio_bus->frames()).ToTimeDelta(rtp_timebase()); |
| 67 if (ShouldDropNextFrame(next_frame_duration)) | 67 if (ShouldDropNextFrame(next_frame_duration)) |
| 68 return; | 68 return; |
| 69 | 69 |
| 70 samples_in_encoder_ += audio_bus->frames(); | 70 samples_in_encoder_ += audio_bus->frames(); |
| 71 | 71 |
| 72 audio_encoder_->InsertAudio(std::move(audio_bus), recorded_time); | 72 audio_encoder_->InsertAudio(std::move(audio_bus), recorded_time); |
| 73 } | 73 } |
| 74 | 74 |
| 75 base::WeakPtr<AudioSender> AudioSender::AsWeakPtr() { |
| 76 return weak_factory_.GetWeakPtr(); |
| 77 } |
| 78 |
| 75 int AudioSender::GetNumberOfFramesInEncoder() const { | 79 int AudioSender::GetNumberOfFramesInEncoder() const { |
| 76 // Note: It's possible for a partial frame to be in the encoder, but returning | 80 // Note: It's possible for a partial frame to be in the encoder, but returning |
| 77 // the floor() is good enough for the "design limit" check in FrameSender. | 81 // the floor() is good enough for the "design limit" check in FrameSender. |
| 78 return samples_in_encoder_ / audio_encoder_->GetSamplesPerFrame(); | 82 return samples_in_encoder_ / audio_encoder_->GetSamplesPerFrame(); |
| 79 } | 83 } |
| 80 | 84 |
| 81 base::TimeDelta AudioSender::GetInFlightMediaDuration() const { | 85 base::TimeDelta AudioSender::GetInFlightMediaDuration() const { |
| 82 const int samples_in_flight = samples_in_encoder_ + | 86 const int samples_in_flight = samples_in_encoder_ + |
| 83 GetUnacknowledgedFrameCount() * audio_encoder_->GetSamplesPerFrame(); | 87 GetUnacknowledgedFrameCount() * audio_encoder_->GetSamplesPerFrame(); |
| 84 return RtpTimeDelta::FromTicks(samples_in_flight).ToTimeDelta(rtp_timebase()); | 88 return RtpTimeDelta::FromTicks(samples_in_flight).ToTimeDelta(rtp_timebase()); |
| 85 } | 89 } |
| 86 | 90 |
| 87 void AudioSender::OnEncodedAudioFrame( | 91 void AudioSender::OnEncodedAudioFrame( |
| 88 int encoder_bitrate, | 92 int encoder_bitrate, |
| 89 std::unique_ptr<SenderEncodedFrame> encoded_frame, | 93 std::unique_ptr<SenderEncodedFrame> encoded_frame, |
| 90 int samples_skipped) { | 94 int samples_skipped) { |
| 91 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 95 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 92 | 96 |
| 93 samples_in_encoder_ -= audio_encoder_->GetSamplesPerFrame() + samples_skipped; | 97 samples_in_encoder_ -= audio_encoder_->GetSamplesPerFrame() + samples_skipped; |
| 94 DCHECK_GE(samples_in_encoder_, 0); | 98 DCHECK_GE(samples_in_encoder_, 0); |
| 95 | 99 |
| 96 SendEncodedFrame(encoder_bitrate, std::move(encoded_frame)); | 100 SendEncodedFrame(encoder_bitrate, std::move(encoded_frame)); |
| 97 } | 101 } |
| 98 | 102 |
| 99 } // namespace cast | 103 } // namespace cast |
| 100 } // namespace media | 104 } // namespace media |
| OLD | NEW |