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

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. 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
« no previous file with comments | « media/cast/sender/video_sender.h ('k') | media/cast/test/end2end_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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::FromSecondsD(1.0 / max_frame_rate_);
162
163 if (ShouldDropNextFrame(duration_added_by_next_frame)) {
146 base::TimeDelta new_target_delay = std::min( 164 base::TimeDelta new_target_delay = std::min(
147 current_round_trip_time_ * kRoundTripsNeeded + 165 current_round_trip_time_ * kRoundTripsNeeded +
148 base::TimeDelta::FromMilliseconds(kConstantTimeMs), 166 base::TimeDelta::FromMilliseconds(kConstantTimeMs),
149 max_playout_delay_); 167 max_playout_delay_);
150 if (new_target_delay > target_playout_delay_) { 168 if (new_target_delay > target_playout_delay_) {
151 VLOG(1) << "New target delay: " << new_target_delay.InMilliseconds(); 169 VLOG(1) << "New target delay: " << new_target_delay.InMilliseconds();
152 playout_delay_change_cb_.Run(new_target_delay); 170 playout_delay_change_cb_.Run(new_target_delay);
153 } 171 }
154 return; 172 return;
155 } 173 }
156 174
157 uint32 bitrate = congestion_control_->GetBitrate( 175 uint32 bitrate = congestion_control_->GetBitrate(
158 capture_time + target_playout_delay_, target_playout_delay_); 176 capture_time + target_playout_delay_, target_playout_delay_);
159 if (bitrate != last_bitrate_) { 177 if (bitrate != last_bitrate_) {
160 video_encoder_->SetBitRate(bitrate); 178 video_encoder_->SetBitRate(bitrate);
161 last_bitrate_ = bitrate; 179 last_bitrate_ = bitrate;
162 } 180 }
163 181
164 if (video_encoder_->EncodeVideoFrame( 182 if (video_encoder_->EncodeVideoFrame(
165 video_frame, 183 video_frame,
166 capture_time, 184 capture_time,
167 base::Bind(&VideoSender::OnEncodedVideoFrame, 185 base::Bind(&VideoSender::OnEncodedVideoFrame,
168 weak_factory_.GetWeakPtr(), 186 weak_factory_.GetWeakPtr(),
169 bitrate))) { 187 bitrate))) {
170 frames_in_encoder_++; 188 frames_in_encoder_++;
189 duration_in_encoder_ += duration_added_by_next_frame;
190 last_enqueued_frame_reference_time_ = capture_time;
171 } else { 191 } else {
172 VLOG(1) << "Encoder rejected a frame. Skipping..."; 192 VLOG(1) << "Encoder rejected a frame. Skipping...";
173 } 193 }
174 } 194 }
175 195
176 int VideoSender::GetNumberOfFramesInEncoder() const { 196 int VideoSender::GetNumberOfFramesInEncoder() const {
177 return frames_in_encoder_; 197 return frames_in_encoder_;
178 } 198 }
179 199
200 base::TimeDelta VideoSender::GetInFlightMediaDuration() const {
201 if (GetUnacknowledgedFrameCount() > 0) {
202 const uint32 oldest_unacked_frame_id = latest_acked_frame_id_ + 1;
203 return last_enqueued_frame_reference_time_ -
204 GetRecordedReferenceTime(oldest_unacked_frame_id);
205 } else {
206 return duration_in_encoder_;
207 }
208 }
209
180 void VideoSender::OnAck(uint32 frame_id) { 210 void VideoSender::OnAck(uint32 frame_id) {
181 video_encoder_->LatestFrameIdToReference(frame_id); 211 video_encoder_->LatestFrameIdToReference(frame_id);
182 } 212 }
183 213
184 void VideoSender::OnEncoderInitialized( 214 void VideoSender::OnEncoderInitialized(
185 const CastInitializationCallback& initialization_cb, 215 const CastInitializationCallback& initialization_cb,
186 CastInitializationStatus status) { 216 CastInitializationStatus status) {
187 cast_initialization_status_ = status; 217 cast_initialization_status_ = status;
188 initialization_cb.Run(status); 218 initialization_cb.Run(status);
189 } 219 }
190 220
191 void VideoSender::OnEncodedVideoFrame( 221 void VideoSender::OnEncodedVideoFrame(
192 int encoder_bitrate, 222 int encoder_bitrate,
193 scoped_ptr<EncodedFrame> encoded_frame) { 223 scoped_ptr<EncodedFrame> encoded_frame) {
194 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); 224 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
195 225
196 frames_in_encoder_--; 226 frames_in_encoder_--;
197 DCHECK_GE(frames_in_encoder_, 0); 227 DCHECK_GE(frames_in_encoder_, 0);
198 228
229 duration_in_encoder_ =
230 last_enqueued_frame_reference_time_ - encoded_frame->reference_time;
231
199 SendEncodedFrame(encoder_bitrate, encoded_frame.Pass()); 232 SendEncodedFrame(encoder_bitrate, encoded_frame.Pass());
200 } 233 }
201 234
202 } // namespace cast 235 } // namespace cast
203 } // namespace media 236 } // namespace media
OLDNEW
« no previous file with comments | « media/cast/sender/video_sender.h ('k') | media/cast/test/end2end_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698