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

Side by Side Diff: chrome/browser/media/cast_remoting_sender.cc

Issue 2310753002: Media Remoting: Data/Control plumbing between renderer and Media Router. (Closed)
Patch Set: xjz's PS4 comments addressed. Created 4 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "chrome/browser/media/cast_remoting_sender.h" 5 #include "chrome/browser/media/cast_remoting_sender.h"
6 6
7 #include <algorithm>
7 #include <map> 8 #include <map>
8 9
9 #include "base/bind.h" 10 #include "base/bind.h"
10 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
11 #include "base/callback.h" 12 #include "base/callback.h"
12 #include "base/lazy_instance.h" 13 #include "base/lazy_instance.h"
13 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
14 #include "base/numerics/safe_conversions.h" 15 #include "base/numerics/safe_conversions.h"
16 #include "base/stl_util.h"
15 #include "base/trace_event/trace_event.h" 17 #include "base/trace_event/trace_event.h"
16 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
17 #include "media/base/bind_to_current_loop.h" 19 #include "media/base/bind_to_current_loop.h"
18 #include "media/cast/constants.h" 20 #include "media/cast/constants.h"
19 21
22 using content::BrowserThread;
23
20 namespace { 24 namespace {
21 25
22 // Global map for looking-up CastRemotingSender instances by their 26 // Global map for looking-up CastRemotingSender instances by their
23 // |remoting_stream_id_|. 27 // |rtp_stream_id|.
24 using CastRemotingSenderMap = std::map<int32_t, cast::CastRemotingSender*>; 28 using CastRemotingSenderMap = std::map<int32_t, cast::CastRemotingSender*>;
25 base::LazyInstance<CastRemotingSenderMap>::Leaky g_sender_map = 29 base::LazyInstance<CastRemotingSenderMap>::Leaky g_sender_map =
26 LAZY_INSTANCE_INITIALIZER; 30 LAZY_INSTANCE_INITIALIZER;
27 31
28 constexpr base::TimeDelta kMinSchedulingDelay = 32 constexpr base::TimeDelta kMinSchedulingDelay =
29 base::TimeDelta::FromMilliseconds(1); 33 base::TimeDelta::FromMilliseconds(1);
30 constexpr base::TimeDelta kMaxAckDelay = base::TimeDelta::FromMilliseconds(800); 34 constexpr base::TimeDelta kMaxAckDelay = base::TimeDelta::FromMilliseconds(800);
31 constexpr base::TimeDelta kMinAckDelay = base::TimeDelta::FromMilliseconds(400);
32 constexpr base::TimeDelta kReceiverProcessTime = 35 constexpr base::TimeDelta kReceiverProcessTime =
33 base::TimeDelta::FromMilliseconds(250); 36 base::TimeDelta::FromMilliseconds(250);
34 37
35 } // namespace 38 } // namespace
36 39
37 namespace cast { 40 namespace cast {
38 41
39 class CastRemotingSender::RemotingRtcpClient final 42 class CastRemotingSender::RemotingRtcpClient final
40 : public media::cast::RtcpObserver { 43 : public media::cast::RtcpObserver {
41 public: 44 public:
(...skipping 23 matching lines...) Expand all
65 DISALLOW_COPY_AND_ASSIGN(RemotingRtcpClient); 68 DISALLOW_COPY_AND_ASSIGN(RemotingRtcpClient);
66 }; 69 };
67 70
68 // Convenience macro used in logging statements throughout this file. 71 // Convenience macro used in logging statements throughout this file.
69 #define SENDER_SSRC (is_audio_ ? "AUDIO[" : "VIDEO[") << ssrc_ << "] " 72 #define SENDER_SSRC (is_audio_ ? "AUDIO[" : "VIDEO[") << ssrc_ << "] "
70 73
71 CastRemotingSender::CastRemotingSender( 74 CastRemotingSender::CastRemotingSender(
72 scoped_refptr<media::cast::CastEnvironment> cast_environment, 75 scoped_refptr<media::cast::CastEnvironment> cast_environment,
73 media::cast::CastTransport* transport, 76 media::cast::CastTransport* transport,
74 const media::cast::CastTransportRtpConfig& config) 77 const media::cast::CastTransportRtpConfig& config)
75 : remoting_stream_id_(config.rtp_stream_id), 78 : rtp_stream_id_(config.rtp_stream_id),
76 cast_environment_(std::move(cast_environment)), 79 cast_environment_(std::move(cast_environment)),
77 transport_(transport), 80 transport_(transport),
78 ssrc_(config.ssrc), 81 ssrc_(config.ssrc),
79 is_audio_(config.rtp_payload_type == 82 is_audio_(config.rtp_payload_type ==
80 media::cast::RtpPayloadType::REMOTE_AUDIO), 83 media::cast::RtpPayloadType::REMOTE_AUDIO),
84 binding_(this),
81 max_ack_delay_(kMaxAckDelay), 85 max_ack_delay_(kMaxAckDelay),
82 last_sent_frame_id_(media::cast::FrameId::first() - 1), 86 last_sent_frame_id_(media::cast::FrameId::first() - 1),
83 latest_acked_frame_id_(media::cast::FrameId::first() - 1), 87 latest_acked_frame_id_(media::cast::FrameId::first() - 1),
84 duplicate_ack_counter_(0), 88 duplicate_ack_counter_(0),
85 last_frame_was_canceled_(false), 89 flow_control_state_(STARTING),
86 weak_factory_(this) { 90 weak_factory_(this) {
91 // Confirm this constructor is running on the IO BrowserThread and the
92 // CastEnvironment::MAIN thread is the same thread.
93 DCHECK_CURRENTLY_ON(BrowserThread::IO);
87 DCHECK(cast_environment_->CurrentlyOn(media::cast::CastEnvironment::MAIN)); 94 DCHECK(cast_environment_->CurrentlyOn(media::cast::CastEnvironment::MAIN));
88 95
89 CastRemotingSender*& pointer_in_map = g_sender_map.Get()[remoting_stream_id_]; 96 CastRemotingSender*& pointer_in_map = g_sender_map.Get()[rtp_stream_id_];
90 DCHECK(!pointer_in_map); 97 DCHECK(!pointer_in_map);
91 pointer_in_map = this; 98 pointer_in_map = this;
99
92 transport_->InitializeStream( 100 transport_->InitializeStream(
93 config, base::MakeUnique<RemotingRtcpClient>(weak_factory_.GetWeakPtr())); 101 config, base::MakeUnique<RemotingRtcpClient>(weak_factory_.GetWeakPtr()));
94 } 102 }
95 103
96 CastRemotingSender::~CastRemotingSender() { 104 CastRemotingSender::~CastRemotingSender() {
97 DCHECK(cast_environment_->CurrentlyOn(media::cast::CastEnvironment::MAIN)); 105 DCHECK_CURRENTLY_ON(BrowserThread::IO);
106 g_sender_map.Pointer()->erase(rtp_stream_id_);
107 }
98 108
99 g_sender_map.Pointer()->erase(remoting_stream_id_); 109 // static
110 void CastRemotingSender::FindAndBind(
111 int32_t rtp_stream_id,
112 mojo::ScopedDataPipeConsumerHandle pipe,
113 media::mojom::RemotingDataStreamSenderRequest request,
114 const base::Closure& error_callback) {
115 // CastRemotingSender lives entirely on the IO thread, so trampoline if
116 // necessary.
117 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
118 BrowserThread::PostTask(
119 BrowserThread::IO, FROM_HERE,
120 base::Bind(&CastRemotingSender::FindAndBind, rtp_stream_id,
121 base::Passed(&pipe), base::Passed(&request),
122 // Using media::BindToCurrentLoop() so the |error_callback|
123 // is trampolined back to the original thread.
124 media::BindToCurrentLoop(error_callback)));
125 return;
126 }
127
128 DCHECK(!error_callback.is_null());
129
130 // Look-up the CastRemotingSender instance by its |rtp_stream_id|.
131 const auto it = g_sender_map.Pointer()->find(rtp_stream_id);
132 if (it == g_sender_map.Pointer()->end()) {
133 DLOG(ERROR) << "Cannot find CastRemotingSender instance by ID: "
134 << rtp_stream_id;
135 error_callback.Run();
136 return;
137 }
138 CastRemotingSender* const sender = it->second;
139
140 // Confirm that the CastRemotingSender isn't already bound to a message pipe.
141 if (sender->binding_.is_bound()) {
142 DLOG(ERROR) << "Attempt to bind to CastRemotingSender a second time (id="
143 << rtp_stream_id << ")!";
144 error_callback.Run();
145 return;
146 }
147
148 DCHECK(sender->error_callback_.is_null());
149 sender->error_callback_ = error_callback;
150
151 sender->pipe_ = std::move(pipe);
152 sender->binding_.Bind(std::move(request));
153 sender->binding_.set_connection_error_handler(
154 base::Bind(&base::Closure::Run,
155 base::Unretained(&sender->error_callback_)));
dcheng 2016/09/20 08:20:12 Does set_connection_error(sender->error_callback_)
miu 2016/09/21 03:15:50 Whoops! Left-overs, ya know? Done.
100 } 156 }
101 157
102 void CastRemotingSender::OnReceivedRtt(base::TimeDelta round_trip_time) { 158 void CastRemotingSender::OnReceivedRtt(base::TimeDelta round_trip_time) {
103 DCHECK_GT(round_trip_time, base::TimeDelta()); 159 DCHECK_GT(round_trip_time, base::TimeDelta());
104 current_round_trip_time_ = round_trip_time; 160 current_round_trip_time_ = round_trip_time;
105 max_ack_delay_ = 2 * current_round_trip_time_ + kReceiverProcessTime; 161 max_ack_delay_ = 2 * std::max(current_round_trip_time_, base::TimeDelta()) +
106 max_ack_delay_ = 162 kReceiverProcessTime;
107 std::max(std::min(max_ack_delay_, kMaxAckDelay), kMinAckDelay); 163 max_ack_delay_ = std::min(max_ack_delay_, kMaxAckDelay);
108 } 164 }
109 165
110 void CastRemotingSender::ResendCheck() { 166 void CastRemotingSender::ResendCheck() {
111 DCHECK(cast_environment_->CurrentlyOn(media::cast::CastEnvironment::MAIN)); 167 DCHECK(cast_environment_->CurrentlyOn(media::cast::CastEnvironment::MAIN));
112 168
113 DCHECK(!last_send_time_.is_null()); 169 DCHECK(!last_send_time_.is_null());
114 const base::TimeDelta time_since_last_send = 170 const base::TimeDelta time_since_last_send =
115 cast_environment_->Clock()->NowTicks() - last_send_time_; 171 cast_environment_->Clock()->NowTicks() - last_send_time_;
116 if (time_since_last_send > max_ack_delay_) { 172 if (time_since_last_send > max_ack_delay_) {
117 if (latest_acked_frame_id_ == last_sent_frame_id_) { 173 if (latest_acked_frame_id_ == last_sent_frame_id_) {
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 frames_to_cancel.push_back(latest_acked_frame_id_); 292 frames_to_cancel.push_back(latest_acked_frame_id_);
237 // This is a good place to match the trace for frame ids 293 // This is a good place to match the trace for frame ids
238 // since this ensures we not only track frame ids that are 294 // since this ensures we not only track frame ids that are
239 // implicitly ACKed, but also handles duplicate ACKs 295 // implicitly ACKed, but also handles duplicate ACKs
240 TRACE_EVENT_ASYNC_END1( 296 TRACE_EVENT_ASYNC_END1(
241 "cast.stream", is_audio_ ? "Audio Transport" : "Video Transport", 297 "cast.stream", is_audio_ ? "Audio Transport" : "Video Transport",
242 latest_acked_frame_id_.lower_32_bits(), "RTT_usecs", 298 latest_acked_frame_id_.lower_32_bits(), "RTT_usecs",
243 current_round_trip_time_.InMicroseconds()); 299 current_round_trip_time_.InMicroseconds());
244 } while (latest_acked_frame_id_ < cast_feedback.ack_frame_id); 300 } while (latest_acked_frame_id_ < cast_feedback.ack_frame_id);
245 transport_->CancelSendingFrames(ssrc_, frames_to_cancel); 301 transport_->CancelSendingFrames(ssrc_, frames_to_cancel);
302
303 if (flow_control_state_ == CONSUME_PAUSED) {
304 // One or more frames were canceled, so resume reading from the Mojo data
305 // pipes and sending frames.
306 binding_.ResumeIncomingMethodCallProcessing();
307 flow_control_state_ = CONSUMING;
308 VLOG(1) << SENDER_SSRC
309 << "Now CONSUMING because one or more frames were ACK'ed.";
310
311 // The last call to SendFrame() placed the sender into the CONSUME_PAUSED
312 // state without sending the frame. So, send that frame now.
313 SendFrame();
314 }
246 } 315 }
247 } 316 }
248 317
318 void CastRemotingSender::ConsumeDataChunk(uint32_t offset, uint32_t size,
319 uint32_t total_payload_size) {
320 DCHECK(cast_environment_->CurrentlyOn(media::cast::CastEnvironment::MAIN));
321
322 // Method calls from Mojo should not be happening when in the paused state.
323 DCHECK_NE(flow_control_state_, CONSUME_PAUSED);
dcheng 2016/09/20 08:20:12 why is that? What stops these messages from being
miu 2016/09/21 03:15:50 Short answer: We call binding_.PauseIncomingMethod
dcheng 2016/09/22 02:29:15 OK,thanks, I just wanted to make sure we weren't r
324
325 // If |total_payload_size| has changed, resize the data string. If it has not
326 // changed, the following statement will be a no-op.
327 next_frame_data_.resize(total_payload_size);
328
329 do {
330 if (!pipe_.is_valid()) {
331 VLOG(1) << SENDER_SSRC << "Data pipe handle no longer valid.";
332 break;
333 }
334
335 if (offset + size > total_payload_size) {
336 LOG(DFATAL)
dcheng 2016/09/20 08:20:12 We probably shouldn't dcheck here, this is coming
miu 2016/09/21 03:15:50 Relaxed to ERROR level.
337 << "BUG: offset + size > total_payload_size ("
338 << offset << " + " << size << " > " << total_payload_size << ')';
339 break;
340 }
341
342 uint32_t num_bytes = size;
343 const MojoResult result = mojo::ReadDataRaw(
344 pipe_.get(), base::string_as_array(&next_frame_data_) + offset,
345 &num_bytes, MOJO_READ_DATA_FLAG_ALL_OR_NONE);
346 if (result != MOJO_RESULT_OK) {
dcheng 2016/09/20 08:20:12 Is it possible for MOJO_RESULT_OUT_OF_RANGE to be
miu 2016/09/21 03:15:50 This should never happen. The render process is al
dcheng 2016/09/22 02:29:15 Right, it's not clear to me if there's synchroniza
miu 2016/09/22 08:39:14 Good catch on this, and rockot@ confirmed. I re-w
347 LOG(DFATAL) << "BUG: Read from data pipe failed (" << result << ')';
348 break;
349 }
350
351 return; // Success.
352 } while (false);
353
354 // If this point is reached, there was a fatal error. Shut things down and run
355 // the error callback.
356 pipe_.reset();
357 binding_.Close();
358 error_callback_.Run();
359 }
360
249 void CastRemotingSender::SendFrame() { 361 void CastRemotingSender::SendFrame() {
250 DCHECK(cast_environment_->CurrentlyOn(media::cast::CastEnvironment::MAIN)); 362 DCHECK(cast_environment_->CurrentlyOn(media::cast::CastEnvironment::MAIN));
251 363
364 // Method calls from Mojo should not be happening when in the paused state.
365 DCHECK_NE(flow_control_state_, CONSUME_PAUSED);
366
367 // If there would be too many frames in-flight, suspend consuming more input
368 // from the Mojo data pipes until one or more frames are ACKed.
252 if (NumberOfFramesInFlight() >= media::cast::kMaxUnackedFrames) { 369 if (NumberOfFramesInFlight() >= media::cast::kMaxUnackedFrames) {
253 // TODO(xjz): Delay the sending of this frame and stop reading the next 370 binding_.PauseIncomingMethodCallProcessing();
254 // frame data. 371 flow_control_state_ = CONSUME_PAUSED;
372 VLOG(1) << SENDER_SSRC
373 << "Now CONSUME_PAUSED because too many frames are in flight.";
255 return; 374 return;
256 } 375 }
257 376
258 VLOG(2) << SENDER_SSRC 377 VLOG(2) << SENDER_SSRC
259 << "About to send another frame: last_sent=" << last_sent_frame_id_ 378 << "About to send another frame: last_sent=" << last_sent_frame_id_
260 << ", latest_acked=" << latest_acked_frame_id_; 379 << ", latest_acked=" << latest_acked_frame_id_;
261 380
262 const media::cast::FrameId frame_id = last_sent_frame_id_ + 1; 381 const media::cast::FrameId frame_id = last_sent_frame_id_ + 1;
263 const bool is_first_frame_to_be_sent = last_send_time_.is_null(); 382 const bool is_first_frame_to_be_sent = last_send_time_.is_null();
264 383
265 base::TimeTicks last_frame_reference_time = last_send_time_; 384 base::TimeTicks last_frame_reference_time = last_send_time_;
266 last_send_time_ = cast_environment_->Clock()->NowTicks(); 385 last_send_time_ = cast_environment_->Clock()->NowTicks();
267 last_sent_frame_id_ = frame_id; 386 last_sent_frame_id_ = frame_id;
268 // If this is the first frame about to be sent, fake the value of 387 // If this is the first frame about to be sent, fake the value of
269 // |latest_acked_frame_id_| to indicate the receiver starts out all caught up. 388 // |latest_acked_frame_id_| to indicate the receiver starts out all caught up.
270 // Also, schedule the periodic frame re-send checks. 389 // Also, schedule the periodic frame re-send checks.
271 if (is_first_frame_to_be_sent) 390 if (is_first_frame_to_be_sent)
272 ScheduleNextResendCheck(); 391 ScheduleNextResendCheck();
273 392
274 DVLOG(3) << "Sending remoting frame, id = " << frame_id; 393 DVLOG(3) << "Sending remoting frame, id = " << frame_id;
275 394
276 media::cast::EncodedFrame remoting_frame; 395 media::cast::EncodedFrame remoting_frame;
277 remoting_frame.frame_id = frame_id; 396 remoting_frame.frame_id = frame_id;
278 remoting_frame.dependency = 397 if (flow_control_state_ == STARTING) {
279 (is_first_frame_to_be_sent || last_frame_was_canceled_) 398 remoting_frame.dependency = media::cast::EncodedFrame::KEY;
280 ? media::cast::EncodedFrame::KEY 399 flow_control_state_ = CONSUMING;
281 : media::cast::EncodedFrame::DEPENDENT; 400 } else {
401 DCHECK(!is_first_frame_to_be_sent);
402 remoting_frame.dependency = media::cast::EncodedFrame::DEPENDENT;
403 }
282 remoting_frame.referenced_frame_id = 404 remoting_frame.referenced_frame_id =
283 remoting_frame.dependency == media::cast::EncodedFrame::KEY 405 remoting_frame.dependency == media::cast::EncodedFrame::KEY
284 ? frame_id 406 ? frame_id
285 : frame_id - 1; 407 : frame_id - 1;
286 remoting_frame.reference_time = last_send_time_; 408 remoting_frame.reference_time = last_send_time_;
287 media::cast::RtpTimeTicks last_frame_rtp_timestamp; 409 media::cast::RtpTimeTicks last_frame_rtp_timestamp;
288 if (is_first_frame_to_be_sent) { 410 if (is_first_frame_to_be_sent) {
289 last_frame_reference_time = remoting_frame.reference_time; 411 last_frame_reference_time = remoting_frame.reference_time;
290 last_frame_rtp_timestamp = 412 last_frame_rtp_timestamp =
291 media::cast::RtpTimeTicks() - media::cast::RtpTimeDelta::FromTicks(1); 413 media::cast::RtpTimeTicks() - media::cast::RtpTimeDelta::FromTicks(1);
(...skipping 18 matching lines...) Expand all
310 remoting_event->media_type = 432 remoting_event->media_type =
311 is_audio_ ? media::cast::AUDIO_EVENT : media::cast::VIDEO_EVENT; 433 is_audio_ ? media::cast::AUDIO_EVENT : media::cast::VIDEO_EVENT;
312 remoting_event->rtp_timestamp = remoting_frame.rtp_timestamp; 434 remoting_event->rtp_timestamp = remoting_frame.rtp_timestamp;
313 remoting_event->frame_id = frame_id; 435 remoting_event->frame_id = frame_id;
314 remoting_event->size = remoting_frame.data.length(); 436 remoting_event->size = remoting_frame.data.length();
315 remoting_event->key_frame = 437 remoting_event->key_frame =
316 remoting_frame.dependency == media::cast::EncodedFrame::KEY; 438 remoting_frame.dependency == media::cast::EncodedFrame::KEY;
317 cast_environment_->logger()->DispatchFrameEvent(std::move(remoting_event)); 439 cast_environment_->logger()->DispatchFrameEvent(std::move(remoting_event));
318 440
319 RecordLatestFrameTimestamps(frame_id, remoting_frame.rtp_timestamp); 441 RecordLatestFrameTimestamps(frame_id, remoting_frame.rtp_timestamp);
320 last_frame_was_canceled_ = false;
321 442
322 transport_->InsertFrame(ssrc_, remoting_frame); 443 transport_->InsertFrame(ssrc_, remoting_frame);
323 } 444 }
324 445
325 void CastRemotingSender::CancelFramesInFlight() { 446 void CastRemotingSender::CancelInFlightData() {
326 DCHECK(cast_environment_->CurrentlyOn(media::cast::CastEnvironment::MAIN)); 447 DCHECK(cast_environment_->CurrentlyOn(media::cast::CastEnvironment::MAIN));
327 448
449 // Method calls from Mojo should not be happening when in the paused state.
450 DCHECK_NE(flow_control_state_, CONSUME_PAUSED);
451
328 base::STLClearObject(&next_frame_data_); 452 base::STLClearObject(&next_frame_data_);
329 453
454 // TODO(miu): The following code is something we want to do as an
455 // optimization. However, as-is, it's not quite correct. We can only cancel
456 // frames where no packets have actually hit the network yet. Said another
457 // way, we can only cancel frames the receiver has definitely not seen any
458 // part of (including kickstarting!). http://crbug.com/647423
459 #if 0
330 if (latest_acked_frame_id_ < last_sent_frame_id_) { 460 if (latest_acked_frame_id_ < last_sent_frame_id_) {
331 std::vector<media::cast::FrameId> frames_to_cancel; 461 std::vector<media::cast::FrameId> frames_to_cancel;
332 do { 462 do {
333 ++latest_acked_frame_id_; 463 ++latest_acked_frame_id_;
334 frames_to_cancel.push_back(latest_acked_frame_id_); 464 frames_to_cancel.push_back(latest_acked_frame_id_);
335 } while (latest_acked_frame_id_ < last_sent_frame_id_); 465 } while (latest_acked_frame_id_ < last_sent_frame_id_);
336 transport_->CancelSendingFrames(ssrc_, frames_to_cancel); 466 transport_->CancelSendingFrames(ssrc_, frames_to_cancel);
337 } 467 }
468 #endif
338 469
339 last_frame_was_canceled_ = true; 470 flow_control_state_ = STARTING;
471 VLOG(1) << SENDER_SSRC
472 << "Now re-STARTING because in-flight data was just canceled.";
340 } 473 }
341 474
342 } // namespace cast 475 } // namespace cast
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698