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

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: REBASE. Plus, merge with CastRemotingSender and add more unit tests. 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_(RESTARTING),
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_)));
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 // If |total_payload_size| has changed, resize the data string. If it has not
323 // changed, the following statement will be a no-op.
324 next_frame_data_.resize(total_payload_size);
325
326 do {
327 if (!pipe_.is_valid()) {
328 VLOG(1) << SENDER_SSRC << "Data pipe handle no longer valid.";
329 break;
330 }
331
332 if (offset + size > total_payload_size) {
333 LOG(DFATAL)
334 << "BUG: offset + size > total_payload_size ("
335 << offset << " + " << size << " > " << total_payload_size << ')';
336 break;
337 }
338
339 uint32_t num_bytes = size;
340 const MojoResult result = mojo::ReadDataRaw(
341 pipe_.get(), base::string_as_array(&next_frame_data_) + offset,
342 &num_bytes, MOJO_READ_DATA_FLAG_ALL_OR_NONE);
343 if (result != MOJO_RESULT_OK) {
344 LOG(DFATAL) << "BUG: Read from data pipe failed (" << result << ')';
345 break;
346 }
347
348 return; // Success.
349 } while (false);
350
351 // If this point is reached, there was a fatal error. Shut things down and run
352 // the error callback.
353 pipe_.reset();
354 binding_.Close();
355 error_callback_.Run();
356 }
357
249 void CastRemotingSender::SendFrame() { 358 void CastRemotingSender::SendFrame() {
250 DCHECK(cast_environment_->CurrentlyOn(media::cast::CastEnvironment::MAIN)); 359 DCHECK(cast_environment_->CurrentlyOn(media::cast::CastEnvironment::MAIN));
xjz 2016/09/15 18:01:49 nit: DCHECK_NE(flow_control_state_, CONSUME_PAUSED
miu 2016/09/16 16:39:28 Done.
251 360
361 // If there would be too many frames in-flight, suspend consuming more input
362 // from the Mojo data pipes until one or more frames are ACKed.
252 if (NumberOfFramesInFlight() >= media::cast::kMaxUnackedFrames) { 363 if (NumberOfFramesInFlight() >= media::cast::kMaxUnackedFrames) {
253 // TODO(xjz): Delay the sending of this frame and stop reading the next 364 binding_.PauseIncomingMethodCallProcessing();
254 // frame data. 365 flow_control_state_ = CONSUME_PAUSED;
366 VLOG(1) << SENDER_SSRC
367 << "Now CONSUME_PAUSED because too many frames are in flight.";
255 return; 368 return;
256 } 369 }
257 370
258 VLOG(2) << SENDER_SSRC 371 VLOG(2) << SENDER_SSRC
259 << "About to send another frame: last_sent=" << last_sent_frame_id_ 372 << "About to send another frame: last_sent=" << last_sent_frame_id_
260 << ", latest_acked=" << latest_acked_frame_id_; 373 << ", latest_acked=" << latest_acked_frame_id_;
261 374
262 const media::cast::FrameId frame_id = last_sent_frame_id_ + 1; 375 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(); 376 const bool is_first_frame_to_be_sent = last_send_time_.is_null();
264 377
265 base::TimeTicks last_frame_reference_time = last_send_time_; 378 base::TimeTicks last_frame_reference_time = last_send_time_;
266 last_send_time_ = cast_environment_->Clock()->NowTicks(); 379 last_send_time_ = cast_environment_->Clock()->NowTicks();
267 last_sent_frame_id_ = frame_id; 380 last_sent_frame_id_ = frame_id;
268 // If this is the first frame about to be sent, fake the value of 381 // 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. 382 // |latest_acked_frame_id_| to indicate the receiver starts out all caught up.
270 // Also, schedule the periodic frame re-send checks. 383 // Also, schedule the periodic frame re-send checks.
271 if (is_first_frame_to_be_sent) 384 if (is_first_frame_to_be_sent)
272 ScheduleNextResendCheck(); 385 ScheduleNextResendCheck();
273 386
274 DVLOG(3) << "Sending remoting frame, id = " << frame_id; 387 DVLOG(3) << "Sending remoting frame, id = " << frame_id;
275 388
276 media::cast::EncodedFrame remoting_frame; 389 media::cast::EncodedFrame remoting_frame;
277 remoting_frame.frame_id = frame_id; 390 remoting_frame.frame_id = frame_id;
278 remoting_frame.dependency = 391 if (flow_control_state_ == RESTARTING) {
279 (is_first_frame_to_be_sent || last_frame_was_canceled_) 392 remoting_frame.dependency = media::cast::EncodedFrame::KEY;
393 flow_control_state_ = CONSUMING;
394 } else {
395 remoting_frame.dependency = is_first_frame_to_be_sent
280 ? media::cast::EncodedFrame::KEY 396 ? media::cast::EncodedFrame::KEY
281 : media::cast::EncodedFrame::DEPENDENT; 397 : media::cast::EncodedFrame::DEPENDENT;
xjz 2016/09/15 18:01:49 When |is_first_frame_to_be_send| is true, |flow_co
miu 2016/09/16 16:39:28 Good point. Simplified this a bit, but not exactly
398 }
282 remoting_frame.referenced_frame_id = 399 remoting_frame.referenced_frame_id =
283 remoting_frame.dependency == media::cast::EncodedFrame::KEY 400 remoting_frame.dependency == media::cast::EncodedFrame::KEY
284 ? frame_id 401 ? frame_id
285 : frame_id - 1; 402 : frame_id - 1;
286 remoting_frame.reference_time = last_send_time_; 403 remoting_frame.reference_time = last_send_time_;
287 media::cast::RtpTimeTicks last_frame_rtp_timestamp; 404 media::cast::RtpTimeTicks last_frame_rtp_timestamp;
288 if (is_first_frame_to_be_sent) { 405 if (is_first_frame_to_be_sent) {
289 last_frame_reference_time = remoting_frame.reference_time; 406 last_frame_reference_time = remoting_frame.reference_time;
290 last_frame_rtp_timestamp = 407 last_frame_rtp_timestamp =
291 media::cast::RtpTimeTicks() - media::cast::RtpTimeDelta::FromTicks(1); 408 media::cast::RtpTimeTicks() - media::cast::RtpTimeDelta::FromTicks(1);
(...skipping 18 matching lines...) Expand all
310 remoting_event->media_type = 427 remoting_event->media_type =
311 is_audio_ ? media::cast::AUDIO_EVENT : media::cast::VIDEO_EVENT; 428 is_audio_ ? media::cast::AUDIO_EVENT : media::cast::VIDEO_EVENT;
312 remoting_event->rtp_timestamp = remoting_frame.rtp_timestamp; 429 remoting_event->rtp_timestamp = remoting_frame.rtp_timestamp;
313 remoting_event->frame_id = frame_id; 430 remoting_event->frame_id = frame_id;
314 remoting_event->size = remoting_frame.data.length(); 431 remoting_event->size = remoting_frame.data.length();
315 remoting_event->key_frame = 432 remoting_event->key_frame =
316 remoting_frame.dependency == media::cast::EncodedFrame::KEY; 433 remoting_frame.dependency == media::cast::EncodedFrame::KEY;
317 cast_environment_->logger()->DispatchFrameEvent(std::move(remoting_event)); 434 cast_environment_->logger()->DispatchFrameEvent(std::move(remoting_event));
318 435
319 RecordLatestFrameTimestamps(frame_id, remoting_frame.rtp_timestamp); 436 RecordLatestFrameTimestamps(frame_id, remoting_frame.rtp_timestamp);
320 last_frame_was_canceled_ = false;
321 437
322 transport_->InsertFrame(ssrc_, remoting_frame); 438 transport_->InsertFrame(ssrc_, remoting_frame);
323 } 439 }
324 440
325 void CastRemotingSender::CancelFramesInFlight() { 441 void CastRemotingSender::CancelInFlightData() {
326 DCHECK(cast_environment_->CurrentlyOn(media::cast::CastEnvironment::MAIN)); 442 DCHECK(cast_environment_->CurrentlyOn(media::cast::CastEnvironment::MAIN));
327 443
328 base::STLClearObject(&next_frame_data_); 444 base::STLClearObject(&next_frame_data_);
329 445
330 if (latest_acked_frame_id_ < last_sent_frame_id_) { 446 if (latest_acked_frame_id_ < last_sent_frame_id_) {
331 std::vector<media::cast::FrameId> frames_to_cancel; 447 std::vector<media::cast::FrameId> frames_to_cancel;
332 do { 448 do {
333 ++latest_acked_frame_id_; 449 ++latest_acked_frame_id_;
334 frames_to_cancel.push_back(latest_acked_frame_id_); 450 frames_to_cancel.push_back(latest_acked_frame_id_);
335 } while (latest_acked_frame_id_ < last_sent_frame_id_); 451 } while (latest_acked_frame_id_ < last_sent_frame_id_);
336 transport_->CancelSendingFrames(ssrc_, frames_to_cancel); 452 transport_->CancelSendingFrames(ssrc_, frames_to_cancel);
337 } 453 }
338 454
xjz 2016/09/15 18:01:49 I might miss one thing here. We need to increase t
miu 2016/09/16 16:39:28 As discussed, we shouldn't be crossing abstraction
339 last_frame_was_canceled_ = true; 455 flow_control_state_ = RESTARTING;
456 VLOG(1) << SENDER_SSRC
457 << "Now RESTARTING because in-flight data was just canceled.";
340 } 458 }
341 459
342 } // namespace cast 460 } // namespace cast
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698