Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: media/cast/sender/video_sender.cc

Issue 560223002: [Cast] Limit frames in flight by duration, and not by number of frames. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tweaks addressing review comments on PS6. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/video_sender.h" 5 #include "media/cast/sender/video_sender.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cstring> 8 #include <cstring>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/debug/trace_event.h" 11 #include "base/debug/trace_event.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "media/cast/cast_defines.h" 14 #include "media/cast/cast_defines.h"
15 #include "media/cast/net/cast_transport_config.h" 15 #include "media/cast/net/cast_transport_config.h"
16 #include "media/cast/sender/external_video_encoder.h" 16 #include "media/cast/sender/external_video_encoder.h"
17 #include "media/cast/sender/video_encoder_impl.h" 17 #include "media/cast/sender/video_encoder_impl.h"
18 18
19 namespace media { 19 namespace media {
20 namespace cast { 20 namespace cast {
21 21
22 namespace {
23
22 // The following two constants are used to adjust the target 24 // The following two constants are used to adjust the target
23 // playout delay (when allowed). They were calculated using 25 // playout delay (when allowed). They were calculated using
24 // a combination of cast_benchmark runs and manual testing. 26 // a combination of cast_benchmark runs and manual testing.
25 27 //
26 // This is how many round trips we think we need on the network. 28 // This is how many round trips we think we need on the network.
27 const int kRoundTripsNeeded = 4; 29 const int kRoundTripsNeeded = 4;
28 // This is an estimate of all the the constant time needed 30 // This is an estimate of all the the constant time needed independent of
29 // independent of network quality. 31 // network quality (e.g., additional time that accounts for encode and decode
32 // time).
30 const int kConstantTimeMs = 75; 33 const int kConstantTimeMs = 75;
31 34
35 } // namespace
36
32 // Note, we use a fixed bitrate value when external video encoder is used. 37 // Note, we use a fixed bitrate value when external video encoder is used.
33 // Some hardware encoder shows bad behavior if we set the bitrate too 38 // Some hardware encoder shows bad behavior if we set the bitrate too
34 // frequently, e.g. quality drop, not abiding by target bitrate, etc. 39 // frequently, e.g. quality drop, not abiding by target bitrate, etc.
35 // See details: crbug.com/392086. 40 // See details: crbug.com/392086.
36 VideoSender::VideoSender( 41 VideoSender::VideoSender(
37 scoped_refptr<CastEnvironment> cast_environment, 42 scoped_refptr<CastEnvironment> cast_environment,
38 const VideoSenderConfig& video_config, 43 const VideoSenderConfig& video_config,
39 const CastInitializationCallback& initialization_cb, 44 const CastInitializationCallback& initialization_cb,
40 const CreateVideoEncodeAcceleratorCallback& create_vea_cb, 45 const CreateVideoEncodeAcceleratorCallback& create_vea_cb,
41 const CreateVideoEncodeMemoryCallback& create_video_encode_mem_cb, 46 const CreateVideoEncodeMemoryCallback& create_video_encode_mem_cb,
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 cast_environment->PostTask( 93 cast_environment->PostTask(
89 CastEnvironment::MAIN, 94 CastEnvironment::MAIN,
90 FROM_HERE, 95 FROM_HERE,
91 base::Bind(initialization_cb, cast_initialization_status_)); 96 base::Bind(initialization_cb, cast_initialization_status_));
92 } 97 }
93 98
94 media::cast::CastTransportRtpConfig transport_config; 99 media::cast::CastTransportRtpConfig transport_config;
95 transport_config.ssrc = video_config.ssrc; 100 transport_config.ssrc = video_config.ssrc;
96 transport_config.feedback_ssrc = video_config.incoming_feedback_ssrc; 101 transport_config.feedback_ssrc = video_config.incoming_feedback_ssrc;
97 transport_config.rtp_payload_type = video_config.rtp_payload_type; 102 transport_config.rtp_payload_type = video_config.rtp_payload_type;
98 transport_config.stored_frames =
99 std::min(kMaxUnackedFrames,
100 1 + static_cast<int>(max_playout_delay_ *
101 max_frame_rate_ /
102 base::TimeDelta::FromSeconds(1)));
103 transport_config.aes_key = video_config.aes_key; 103 transport_config.aes_key = video_config.aes_key;
104 transport_config.aes_iv_mask = video_config.aes_iv_mask; 104 transport_config.aes_iv_mask = video_config.aes_iv_mask;
105 105
106 transport_sender->InitializeVideo( 106 transport_sender->InitializeVideo(
107 transport_config, 107 transport_config,
108 base::Bind(&VideoSender::OnReceivedCastFeedback, 108 base::Bind(&VideoSender::OnReceivedCastFeedback,
109 weak_factory_.GetWeakPtr()), 109 weak_factory_.GetWeakPtr()),
110 base::Bind(&VideoSender::OnMeasuredRoundTripTime, 110 base::Bind(&VideoSender::OnMeasuredRoundTripTime,
111 weak_factory_.GetWeakPtr())); 111 weak_factory_.GetWeakPtr()));
112 } 112 }
(...skipping 21 matching lines...) Expand all
134 rtp_timestamp, 134 rtp_timestamp,
135 kFrameIdUnknown); 135 kFrameIdUnknown);
136 136
137 // Used by chrome/browser/extension/api/cast_streaming/performance_test.cc 137 // Used by chrome/browser/extension/api/cast_streaming/performance_test.cc
138 TRACE_EVENT_INSTANT2( 138 TRACE_EVENT_INSTANT2(
139 "cast_perf_test", "InsertRawVideoFrame", 139 "cast_perf_test", "InsertRawVideoFrame",
140 TRACE_EVENT_SCOPE_THREAD, 140 TRACE_EVENT_SCOPE_THREAD,
141 "timestamp", capture_time.ToInternalValue(), 141 "timestamp", capture_time.ToInternalValue(),
142 "rtp_timestamp", rtp_timestamp); 142 "rtp_timestamp", rtp_timestamp);
143 143
144 if (ShouldDropNextFrame(capture_time)) { 144 // Drop the frame if its reference timestamp is not an increase over the last
145 VLOG(1) << "Dropping frame due to too many frames currently in-flight."; 145 // frame's. This protects: 1) the duration calculations that assume
146 // timestamps are monotonically non-decreasing, and 2) assumptions made deeper
147 // in the implementation where each frame's RTP timestamp needs to be unique.
148 if (!last_enqueued_frame_reference_time_.is_null() &&
149 capture_time <= last_enqueued_frame_reference_time_) {
150 VLOG(1) << "Dropping video frame: Reference time did not increase.";
151 return;
152 }
153
154 // Two video frames are needed to compute the exact media duration added by
155 // the next frame. If there are no frames in the encoder, compute a guess
156 // based on the configured |max_frame_rate_|. Any error introduced by this
157 // guess will be eliminated when |duration_in_encoder_| is updated in
158 // OnEncodedVideoFrame().
159 const base::TimeDelta duration_added_by_next_frame = frames_in_encoder_ > 0 ?
160 capture_time - last_enqueued_frame_reference_time_ :
161 base::TimeDelta::FromMicroseconds(
162 static_cast<int64>(1000000.0 / max_frame_rate_ + 0.5));
163
164 if (ShouldDropNextFrame(duration_added_by_next_frame)) {
146 base::TimeDelta new_target_delay = std::min( 165 base::TimeDelta new_target_delay = std::min(
147 current_round_trip_time_ * kRoundTripsNeeded + 166 current_round_trip_time_ * kRoundTripsNeeded +
148 base::TimeDelta::FromMilliseconds(kConstantTimeMs), 167 base::TimeDelta::FromMilliseconds(kConstantTimeMs),
149 max_playout_delay_); 168 max_playout_delay_);
150 if (new_target_delay > target_playout_delay_) { 169 if (new_target_delay > target_playout_delay_) {
151 VLOG(1) << "New target delay: " << new_target_delay.InMilliseconds(); 170 VLOG(1) << "New target delay: " << new_target_delay.InMilliseconds();
152 playout_delay_change_cb_.Run(new_target_delay); 171 playout_delay_change_cb_.Run(new_target_delay);
153 } 172 }
154 return; 173 return;
155 } 174 }
156 175
157 uint32 bitrate = congestion_control_->GetBitrate( 176 uint32 bitrate = congestion_control_->GetBitrate(
158 capture_time + target_playout_delay_, target_playout_delay_); 177 capture_time + target_playout_delay_, target_playout_delay_);
159 if (bitrate != last_bitrate_) { 178 if (bitrate != last_bitrate_) {
160 video_encoder_->SetBitRate(bitrate); 179 video_encoder_->SetBitRate(bitrate);
161 last_bitrate_ = bitrate; 180 last_bitrate_ = bitrate;
162 } 181 }
163 182
164 if (video_encoder_->EncodeVideoFrame( 183 if (video_encoder_->EncodeVideoFrame(
165 video_frame, 184 video_frame,
166 capture_time, 185 capture_time,
167 base::Bind(&VideoSender::OnEncodedVideoFrame, 186 base::Bind(&VideoSender::OnEncodedVideoFrame,
168 weak_factory_.GetWeakPtr(), 187 weak_factory_.GetWeakPtr(),
169 bitrate))) { 188 bitrate))) {
170 frames_in_encoder_++; 189 frames_in_encoder_++;
190 duration_in_encoder_ += duration_added_by_next_frame;
191 last_enqueued_frame_reference_time_ = capture_time;
171 } else { 192 } else {
172 VLOG(1) << "Encoder rejected a frame. Skipping..."; 193 VLOG(1) << "Encoder rejected a frame. Skipping...";
173 } 194 }
174 } 195 }
175 196
176 int VideoSender::GetNumberOfFramesInEncoder() const { 197 int VideoSender::GetNumberOfFramesInEncoder() const {
177 return frames_in_encoder_; 198 return frames_in_encoder_;
178 } 199 }
179 200
201 base::TimeDelta VideoSender::GetInFlightMediaDuration() const {
202 if (GetUnacknowledgedFrameCount() > 0) {
203 const uint32 oldest_unacked_frame_id = latest_acked_frame_id_ + 1;
204 return last_enqueued_frame_reference_time_ -
205 GetRecordedReferenceTime(oldest_unacked_frame_id);
206 } else {
207 return duration_in_encoder_;
208 }
209 }
210
180 void VideoSender::OnAck(uint32 frame_id) { 211 void VideoSender::OnAck(uint32 frame_id) {
181 video_encoder_->LatestFrameIdToReference(frame_id); 212 video_encoder_->LatestFrameIdToReference(frame_id);
182 } 213 }
183 214
184 void VideoSender::OnEncoderInitialized( 215 void VideoSender::OnEncoderInitialized(
185 const CastInitializationCallback& initialization_cb, 216 const CastInitializationCallback& initialization_cb,
186 CastInitializationStatus status) { 217 CastInitializationStatus status) {
187 cast_initialization_status_ = status; 218 cast_initialization_status_ = status;
188 initialization_cb.Run(status); 219 initialization_cb.Run(status);
189 } 220 }
190 221
191 void VideoSender::OnEncodedVideoFrame( 222 void VideoSender::OnEncodedVideoFrame(
192 int encoder_bitrate, 223 int encoder_bitrate,
193 scoped_ptr<EncodedFrame> encoded_frame) { 224 scoped_ptr<EncodedFrame> encoded_frame) {
194 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); 225 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
195 226
196 frames_in_encoder_--; 227 frames_in_encoder_--;
197 DCHECK_GE(frames_in_encoder_, 0); 228 DCHECK_GE(frames_in_encoder_, 0);
198 229
230 duration_in_encoder_ =
231 last_enqueued_frame_reference_time_ - encoded_frame->reference_time;
232
199 SendEncodedFrame(encoder_bitrate, encoded_frame.Pass()); 233 SendEncodedFrame(encoder_bitrate, encoded_frame.Pass());
200 } 234 }
201 235
202 } // namespace cast 236 } // namespace cast
203 } // namespace media 237 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698