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 <algorithm> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
10 #include "media/cast/cast_defines.h" | 12 #include "media/cast/cast_defines.h" |
11 #include "media/cast/net/cast_transport_config.h" | 13 #include "media/cast/net/cast_transport_config.h" |
12 #include "media/cast/sender/audio_encoder.h" | 14 #include "media/cast/sender/audio_encoder.h" |
13 | 15 |
14 namespace media { | 16 namespace media { |
15 namespace cast { | 17 namespace cast { |
16 namespace { | 18 namespace { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 cast_initialization_status_ = audio_encoder_->InitializationResult(); | 56 cast_initialization_status_ = audio_encoder_->InitializationResult(); |
55 } else { | 57 } else { |
56 NOTREACHED(); // No support for external audio encoding. | 58 NOTREACHED(); // No support for external audio encoding. |
57 cast_initialization_status_ = STATUS_AUDIO_UNINITIALIZED; | 59 cast_initialization_status_ = STATUS_AUDIO_UNINITIALIZED; |
58 } | 60 } |
59 | 61 |
60 media::cast::CastTransportRtpConfig transport_config; | 62 media::cast::CastTransportRtpConfig transport_config; |
61 transport_config.ssrc = audio_config.ssrc; | 63 transport_config.ssrc = audio_config.ssrc; |
62 transport_config.feedback_ssrc = audio_config.incoming_feedback_ssrc; | 64 transport_config.feedback_ssrc = audio_config.incoming_feedback_ssrc; |
63 transport_config.rtp_payload_type = audio_config.rtp_payload_type; | 65 transport_config.rtp_payload_type = audio_config.rtp_payload_type; |
64 // TODO(miu): AudioSender needs to be like VideoSender in providing an upper | |
65 // limit on the number of in-flight frames. | |
66 transport_config.stored_frames = max_unacked_frames_; | |
67 transport_config.aes_key = audio_config.aes_key; | 66 transport_config.aes_key = audio_config.aes_key; |
68 transport_config.aes_iv_mask = audio_config.aes_iv_mask; | 67 transport_config.aes_iv_mask = audio_config.aes_iv_mask; |
69 | 68 |
70 transport_sender->InitializeAudio( | 69 transport_sender->InitializeAudio( |
71 transport_config, | 70 transport_config, |
72 base::Bind(&AudioSender::OnReceivedCastFeedback, | 71 base::Bind(&AudioSender::OnReceivedCastFeedback, |
73 weak_factory_.GetWeakPtr()), | 72 weak_factory_.GetWeakPtr()), |
74 base::Bind(&AudioSender::OnMeasuredRoundTripTime, | 73 base::Bind(&AudioSender::OnMeasuredRoundTripTime, |
75 weak_factory_.GetWeakPtr())); | 74 weak_factory_.GetWeakPtr())); |
76 } | 75 } |
77 | 76 |
78 AudioSender::~AudioSender() {} | 77 AudioSender::~AudioSender() {} |
79 | 78 |
80 void AudioSender::InsertAudio(scoped_ptr<AudioBus> audio_bus, | 79 void AudioSender::InsertAudio(scoped_ptr<AudioBus> audio_bus, |
81 const base::TimeTicks& recorded_time) { | 80 const base::TimeTicks& recorded_time) { |
82 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 81 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
83 if (cast_initialization_status_ != STATUS_AUDIO_INITIALIZED) { | 82 if (cast_initialization_status_ != STATUS_AUDIO_INITIALIZED) { |
84 NOTREACHED(); | 83 NOTREACHED(); |
85 return; | 84 return; |
86 } | 85 } |
87 DCHECK(audio_encoder_.get()) << "Invalid internal state"; | 86 DCHECK(audio_encoder_.get()) << "Invalid internal state"; |
88 | 87 |
89 // TODO(miu): An |audio_bus| that represents more duration than a single | 88 // Check that enqueuing the samples in |audio_bus| won't cause more frames to |
90 // frame's duration can defeat our logic here, causing too much data to become | 89 // become in-flight than the system's design limit. |
91 // enqueued. This will be addressed in a soon-upcoming change. | 90 const int count_unacked_frames = GetUnackedFrameCount(); |
92 if (ShouldDropNextFrame(recorded_time)) { | 91 const int64 samples_unacked = |
93 VLOG(1) << "Dropping frame due to too many frames currently in-flight."; | 92 count_unacked_frames * audio_encoder_->GetSamplesPerFrame(); |
| 93 const int64 samples_would_be_in_flight = |
| 94 samples_unacked + samples_in_encoder_ + audio_bus->frames(); |
| 95 const int frames_would_be_in_flight = |
| 96 samples_would_be_in_flight / audio_encoder_->GetSamplesPerFrame(); |
| 97 if (frames_would_be_in_flight > kMaxUnackedFrames) { |
| 98 VLOG(1) << "Dropping audio: Too many frames would be in-flight."; |
| 99 return; |
| 100 } |
| 101 |
| 102 // Check that enqueuing the samples in |audio_bus| won't exceed the allowed |
| 103 // in-flight media duration. |
| 104 const int64 max_samples_in_flight = |
| 105 TimeDeltaToRtpDelta(GetAllowedInFlightMediaDuration(), rtp_timebase()); |
| 106 VLOG(2) << "Audio samples in-flight: " |
| 107 << samples_unacked << " unacked + " |
| 108 << samples_in_encoder_ << " in encoder + " |
| 109 << audio_bus->frames() << " additional would be " |
| 110 << (max_samples_in_flight > 0 ? |
| 111 100 * samples_would_be_in_flight / max_samples_in_flight : |
| 112 kint64max) << "% of allowed in-flight."; |
| 113 if (samples_would_be_in_flight > max_samples_in_flight) { |
| 114 VLOG(1) << "Dropping audio: Too long an audio duration would be in-flight."; |
94 return; | 115 return; |
95 } | 116 } |
96 | 117 |
97 samples_in_encoder_ += audio_bus->frames(); | 118 samples_in_encoder_ += audio_bus->frames(); |
98 | |
99 audio_encoder_->InsertAudio(audio_bus.Pass(), recorded_time); | 119 audio_encoder_->InsertAudio(audio_bus.Pass(), recorded_time); |
100 } | 120 } |
101 | 121 |
102 int AudioSender::GetNumberOfFramesInEncoder() const { | |
103 // Note: It's possible for a partial frame to be in the encoder, but returning | |
104 // the floor() is good enough for the "design limit" check in FrameSender. | |
105 return samples_in_encoder_ / audio_encoder_->GetSamplesPerFrame(); | |
106 } | |
107 | |
108 void AudioSender::OnAck(uint32 frame_id) { | 122 void AudioSender::OnAck(uint32 frame_id) { |
109 } | 123 } |
110 | 124 |
111 void AudioSender::OnEncodedAudioFrame( | 125 void AudioSender::OnEncodedAudioFrame( |
112 int encoder_bitrate, | 126 int encoder_bitrate, |
113 scoped_ptr<EncodedFrame> encoded_frame, | 127 scoped_ptr<EncodedFrame> encoded_frame, |
114 int samples_skipped) { | 128 int samples_skipped) { |
115 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 129 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
116 | 130 |
117 samples_in_encoder_ -= audio_encoder_->GetSamplesPerFrame() + samples_skipped; | 131 samples_in_encoder_ -= audio_encoder_->GetSamplesPerFrame() + samples_skipped; |
118 DCHECK_GE(samples_in_encoder_, 0); | 132 DCHECK_GE(samples_in_encoder_, 0); |
119 | 133 |
120 SendEncodedFrame(encoder_bitrate, encoded_frame.Pass()); | 134 SendEncodedFrame(encoder_bitrate, encoded_frame.Pass()); |
121 } | 135 } |
122 | 136 |
123 } // namespace cast | 137 } // namespace cast |
124 } // namespace media | 138 } // namespace media |
OLD | NEW |