OLD | NEW |
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 "media/remoting/courier_renderer.h" | 5 #include "media/remoting/courier_renderer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/callback_helpers.h" | 13 #include "base/callback_helpers.h" |
14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/numerics/safe_math.h" | 16 #include "base/numerics/safe_math.h" |
17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
19 #include "media/base/bind_to_current_loop.h" | 19 #include "media/base/bind_to_current_loop.h" |
20 #include "media/base/buffering_state.h" | 20 #include "media/base/buffering_state.h" |
21 #include "media/base/demuxer_stream_provider.h" | 21 #include "media/base/media_resource.h" |
22 #include "media/base/renderer_client.h" | 22 #include "media/base/renderer_client.h" |
23 #include "media/remoting/demuxer_stream_adapter.h" | 23 #include "media/remoting/demuxer_stream_adapter.h" |
24 #include "media/remoting/proto_enum_utils.h" | 24 #include "media/remoting/proto_enum_utils.h" |
25 #include "media/remoting/proto_utils.h" | 25 #include "media/remoting/proto_utils.h" |
26 #include "media/remoting/renderer_controller.h" | 26 #include "media/remoting/renderer_controller.h" |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 // The moving time window to track the media time and statistics updates. | 30 // The moving time window to track the media time and statistics updates. |
31 constexpr base::TimeDelta kTrackingWindow = base::TimeDelta::FromSeconds(5); | 31 constexpr base::TimeDelta kTrackingWindow = base::TimeDelta::FromSeconds(5); |
(...skipping 25 matching lines...) Expand all Loading... |
57 namespace media { | 57 namespace media { |
58 namespace remoting { | 58 namespace remoting { |
59 | 59 |
60 CourierRenderer::CourierRenderer( | 60 CourierRenderer::CourierRenderer( |
61 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner, | 61 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner, |
62 const base::WeakPtr<RendererController>& controller, | 62 const base::WeakPtr<RendererController>& controller, |
63 VideoRendererSink* video_renderer_sink) | 63 VideoRendererSink* video_renderer_sink) |
64 : state_(STATE_UNINITIALIZED), | 64 : state_(STATE_UNINITIALIZED), |
65 main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 65 main_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
66 media_task_runner_(std::move(media_task_runner)), | 66 media_task_runner_(std::move(media_task_runner)), |
67 demuxer_stream_provider_(nullptr), | 67 media_resource_(nullptr), |
68 client_(nullptr), | 68 client_(nullptr), |
69 controller_(controller), | 69 controller_(controller), |
70 rpc_broker_(controller_->GetRpcBroker()), | 70 rpc_broker_(controller_->GetRpcBroker()), |
71 rpc_handle_(rpc_broker_->GetUniqueHandle()), | 71 rpc_handle_(rpc_broker_->GetUniqueHandle()), |
72 remote_renderer_handle_(RpcBroker::kInvalidHandle), | 72 remote_renderer_handle_(RpcBroker::kInvalidHandle), |
73 video_renderer_sink_(video_renderer_sink), | 73 video_renderer_sink_(video_renderer_sink), |
74 weak_factory_(this) { | 74 weak_factory_(this) { |
75 VLOG(2) << __func__; | 75 VLOG(2) << __func__; |
76 // Note: The constructor is running on the main thread, but will be destroyed | 76 // Note: The constructor is running on the main thread, but will be destroyed |
77 // on the media thread. Therefore, all weak pointers must be dereferenced on | 77 // on the media thread. Therefore, all weak pointers must be dereferenced on |
(...skipping 19 matching lines...) Expand all Loading... |
97 FROM_HERE, | 97 FROM_HERE, |
98 base::Bind(&RendererController::SetShowInterstitialCallback, controller_, | 98 base::Bind(&RendererController::SetShowInterstitialCallback, controller_, |
99 RendererController::ShowInterstitialCallback())); | 99 RendererController::ShowInterstitialCallback())); |
100 | 100 |
101 // Post task on main thread to unregister message receiver. | 101 // Post task on main thread to unregister message receiver. |
102 main_task_runner_->PostTask( | 102 main_task_runner_->PostTask( |
103 FROM_HERE, base::Bind(&RpcBroker::UnregisterMessageReceiverCallback, | 103 FROM_HERE, base::Bind(&RpcBroker::UnregisterMessageReceiverCallback, |
104 rpc_broker_, rpc_handle_)); | 104 rpc_broker_, rpc_handle_)); |
105 } | 105 } |
106 | 106 |
107 void CourierRenderer::Initialize(DemuxerStreamProvider* demuxer_stream_provider, | 107 void CourierRenderer::Initialize(MediaResource* media_resource, |
108 RendererClient* client, | 108 RendererClient* client, |
109 const PipelineStatusCB& init_cb) { | 109 const PipelineStatusCB& init_cb) { |
110 VLOG(2) << __func__; | 110 VLOG(2) << __func__; |
111 DCHECK(media_task_runner_->BelongsToCurrentThread()); | 111 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
112 DCHECK(demuxer_stream_provider); | 112 DCHECK(media_resource); |
113 DCHECK(client); | 113 DCHECK(client); |
114 | 114 |
115 if (state_ != STATE_UNINITIALIZED) { | 115 if (state_ != STATE_UNINITIALIZED) { |
116 media_task_runner_->PostTask( | 116 media_task_runner_->PostTask( |
117 FROM_HERE, base::Bind(init_cb, PIPELINE_ERROR_INVALID_STATE)); | 117 FROM_HERE, base::Bind(init_cb, PIPELINE_ERROR_INVALID_STATE)); |
118 return; | 118 return; |
119 } | 119 } |
120 | 120 |
121 demuxer_stream_provider_ = demuxer_stream_provider; | 121 media_resource_ = media_resource; |
122 client_ = client; | 122 client_ = client; |
123 init_workflow_done_callback_ = init_cb; | 123 init_workflow_done_callback_ = init_cb; |
124 | 124 |
125 state_ = STATE_CREATE_PIPE; | 125 state_ = STATE_CREATE_PIPE; |
| 126 |
| 127 std::vector<::media::DemuxerStream*> streams = media_resource_->GetStreams(); |
| 128 ::media::DemuxerStream* audio_demuxer_stream = nullptr; |
| 129 ::media::DemuxerStream* video_demuxer_stream = nullptr; |
| 130 for (const auto& s : streams) { |
| 131 if (s->type() == DemuxerStream::AUDIO && s->enabled() && |
| 132 !audio_demuxer_stream) |
| 133 audio_demuxer_stream = s; |
| 134 if (s->type() == DemuxerStream::VIDEO && s->enabled() && |
| 135 !video_demuxer_stream) |
| 136 video_demuxer_stream = s; |
| 137 } |
| 138 |
126 // Create audio mojo data pipe handles if audio is available. | 139 // Create audio mojo data pipe handles if audio is available. |
127 DemuxerStream* audio_demuxer_stream = | |
128 demuxer_stream_provider_->GetStream(DemuxerStream::AUDIO); | |
129 std::unique_ptr<mojo::DataPipe> audio_data_pipe; | 140 std::unique_ptr<mojo::DataPipe> audio_data_pipe; |
130 if (audio_demuxer_stream) { | 141 if (audio_demuxer_stream) { |
131 audio_data_pipe = base::WrapUnique(DemuxerStreamAdapter::CreateDataPipe()); | 142 audio_data_pipe = base::WrapUnique(DemuxerStreamAdapter::CreateDataPipe()); |
132 } | 143 } |
133 | 144 |
134 // Create video mojo data pipe handles if video is available. | 145 // Create video mojo data pipe handles if video is available. |
135 DemuxerStream* video_demuxer_stream = | |
136 demuxer_stream_provider_->GetStream(DemuxerStream::VIDEO); | |
137 std::unique_ptr<mojo::DataPipe> video_data_pipe; | 146 std::unique_ptr<mojo::DataPipe> video_data_pipe; |
138 if (video_demuxer_stream) { | 147 if (video_demuxer_stream) { |
139 video_data_pipe = base::WrapUnique(DemuxerStreamAdapter::CreateDataPipe()); | 148 video_data_pipe = base::WrapUnique(DemuxerStreamAdapter::CreateDataPipe()); |
140 } | 149 } |
141 | 150 |
142 // Establish remoting data pipe connection using main thread. | 151 // Establish remoting data pipe connection using main thread. |
143 const SharedSession::DataPipeStartCallback data_pipe_callback = | 152 const SharedSession::DataPipeStartCallback data_pipe_callback = |
144 base::Bind(&CourierRenderer::OnDataPipeCreatedOnMainThread, | 153 base::Bind(&CourierRenderer::OnDataPipeCreatedOnMainThread, |
145 media_task_runner_, weak_factory_.GetWeakPtr(), rpc_broker_); | 154 media_task_runner_, weak_factory_.GetWeakPtr(), rpc_broker_); |
146 main_task_runner_->PostTask( | 155 main_task_runner_->PostTask( |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 int audio_rpc_handle, | 325 int audio_rpc_handle, |
317 int video_rpc_handle) { | 326 int video_rpc_handle) { |
318 VLOG(2) << __func__; | 327 VLOG(2) << __func__; |
319 DCHECK(media_task_runner_->BelongsToCurrentThread()); | 328 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
320 DCHECK(!init_workflow_done_callback_.is_null()); | 329 DCHECK(!init_workflow_done_callback_.is_null()); |
321 | 330 |
322 if (state_ == STATE_ERROR) | 331 if (state_ == STATE_ERROR) |
323 return; // Abort because something went wrong in the meantime. | 332 return; // Abort because something went wrong in the meantime. |
324 DCHECK_EQ(state_, STATE_CREATE_PIPE); | 333 DCHECK_EQ(state_, STATE_CREATE_PIPE); |
325 | 334 |
| 335 // TODO(erickung): Make remote renderer multi-stream capable. |
| 336 std::vector<::media::DemuxerStream*> streams = media_resource_->GetStreams(); |
| 337 ::media::DemuxerStream* audio_demuxer_stream = nullptr; |
| 338 ::media::DemuxerStream* video_demuxer_stream = nullptr; |
| 339 for (const auto& s : streams) { |
| 340 if (s->type() == DemuxerStream::AUDIO && s->enabled() && |
| 341 !audio_demuxer_stream) |
| 342 audio_demuxer_stream = s; |
| 343 if (s->type() == DemuxerStream::VIDEO && s->enabled() && |
| 344 !video_demuxer_stream) |
| 345 video_demuxer_stream = s; |
| 346 } |
| 347 |
326 // Create audio demuxer stream adapter if audio is available. | 348 // Create audio demuxer stream adapter if audio is available. |
327 DemuxerStream* audio_demuxer_stream = | |
328 demuxer_stream_provider_->GetStream(DemuxerStream::AUDIO); | |
329 if (audio_demuxer_stream && audio.is_valid() && audio_handle.is_valid() && | 349 if (audio_demuxer_stream && audio.is_valid() && audio_handle.is_valid() && |
330 audio_rpc_handle != RpcBroker::kInvalidHandle) { | 350 audio_rpc_handle != RpcBroker::kInvalidHandle) { |
331 VLOG(2) << "Initialize audio"; | 351 VLOG(2) << "Initialize audio"; |
332 audio_demuxer_stream_adapter_.reset(new DemuxerStreamAdapter( | 352 audio_demuxer_stream_adapter_.reset(new DemuxerStreamAdapter( |
333 main_task_runner_, media_task_runner_, "audio", audio_demuxer_stream, | 353 main_task_runner_, media_task_runner_, "audio", audio_demuxer_stream, |
334 rpc_broker_, audio_rpc_handle, std::move(audio), | 354 rpc_broker_, audio_rpc_handle, std::move(audio), |
335 std::move(audio_handle), | 355 std::move(audio_handle), |
336 base::Bind(&CourierRenderer::OnFatalError, base::Unretained(this)))); | 356 base::Bind(&CourierRenderer::OnFatalError, base::Unretained(this)))); |
337 } | 357 } |
338 | 358 |
339 // Create video demuxer stream adapter if video is available. | 359 // Create video demuxer stream adapter if video is available. |
340 DemuxerStream* video_demuxer_stream = | |
341 demuxer_stream_provider_->GetStream(DemuxerStream::VIDEO); | |
342 if (video_demuxer_stream && video.is_valid() && video_handle.is_valid() && | 360 if (video_demuxer_stream && video.is_valid() && video_handle.is_valid() && |
343 video_rpc_handle != RpcBroker::kInvalidHandle) { | 361 video_rpc_handle != RpcBroker::kInvalidHandle) { |
344 VLOG(2) << "Initialize video"; | 362 VLOG(2) << "Initialize video"; |
345 video_demuxer_stream_adapter_.reset(new DemuxerStreamAdapter( | 363 video_demuxer_stream_adapter_.reset(new DemuxerStreamAdapter( |
346 main_task_runner_, media_task_runner_, "video", video_demuxer_stream, | 364 main_task_runner_, media_task_runner_, "video", video_demuxer_stream, |
347 rpc_broker_, video_rpc_handle, std::move(video), | 365 rpc_broker_, video_rpc_handle, std::move(video), |
348 std::move(video_handle), | 366 std::move(video_handle), |
349 base::Bind(&CourierRenderer::OnFatalError, base::Unretained(this)))); | 367 base::Bind(&CourierRenderer::OnFatalError, base::Unretained(this)))); |
350 } | 368 } |
351 | 369 |
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
840 kBytesPerKilobit; | 858 kBytesPerKilobit; |
841 DCHECK_GE(kilobits_per_second, 0); | 859 DCHECK_GE(kilobits_per_second, 0); |
842 const base::CheckedNumeric<int> checked_kbps = kilobits_per_second; | 860 const base::CheckedNumeric<int> checked_kbps = kilobits_per_second; |
843 metrics_recorder_.OnVideoRateEstimate( | 861 metrics_recorder_.OnVideoRateEstimate( |
844 checked_kbps.ValueOrDefault(std::numeric_limits<int>::max())); | 862 checked_kbps.ValueOrDefault(std::numeric_limits<int>::max())); |
845 } | 863 } |
846 } | 864 } |
847 | 865 |
848 } // namespace remoting | 866 } // namespace remoting |
849 } // namespace media | 867 } // namespace media |
OLD | NEW |