| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef MEDIA_REMOTING_REMOTE_RENDERER_IMPL_H_ | |
| 6 #define MEDIA_REMOTING_REMOTE_RENDERER_IMPL_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "base/memory/weak_ptr.h" | |
| 16 #include "base/optional.h" | |
| 17 #include "base/synchronization/lock.h" | |
| 18 #include "base/timer/timer.h" | |
| 19 #include "media/base/buffering_state.h" | |
| 20 #include "media/base/pipeline_status.h" | |
| 21 #include "media/base/renderer.h" | |
| 22 #include "media/base/renderer_client.h" | |
| 23 #include "media/mojo/interfaces/remoting.mojom.h" | |
| 24 #include "media/remoting/metrics.h" | |
| 25 #include "media/remoting/remoting_interstitial_ui.h" | |
| 26 #include "media/remoting/rpc/rpc_broker.h" | |
| 27 #include "mojo/public/cpp/system/data_pipe.h" | |
| 28 | |
| 29 namespace media { | |
| 30 | |
| 31 class RemotingRendererController; | |
| 32 class Renderer; | |
| 33 | |
| 34 namespace remoting { | |
| 35 class RemoteDemuxerStreamAdapter; | |
| 36 }; | |
| 37 | |
| 38 // A media::Renderer implementation that use a media::Renderer to render | |
| 39 // media streams. | |
| 40 class RemoteRendererImpl : public Renderer { | |
| 41 public: | |
| 42 // The whole class except for constructor and GetMediaTime() runs on | |
| 43 // |media_task_runner|. The constructor and GetMediaTime() run on render main | |
| 44 // thread. | |
| 45 RemoteRendererImpl( | |
| 46 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner, | |
| 47 const base::WeakPtr<RemotingRendererController>& | |
| 48 remoting_renderer_controller, | |
| 49 VideoRendererSink* video_renderer_sink); | |
| 50 ~RemoteRendererImpl() final; | |
| 51 | |
| 52 private: | |
| 53 // Callback when attempting to establish data pipe. The function is set to | |
| 54 // static in order to post task to media thread in order to avoid threading | |
| 55 // race condition. | |
| 56 static void OnDataPipeCreatedOnMainThread( | |
| 57 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner, | |
| 58 base::WeakPtr<RemoteRendererImpl> self, | |
| 59 base::WeakPtr<remoting::RpcBroker> rpc_broker, | |
| 60 mojom::RemotingDataStreamSenderPtrInfo audio, | |
| 61 mojom::RemotingDataStreamSenderPtrInfo video, | |
| 62 mojo::ScopedDataPipeProducerHandle audio_handle, | |
| 63 mojo::ScopedDataPipeProducerHandle video_handle); | |
| 64 | |
| 65 // Callback function when RPC message is received. The function is set to | |
| 66 // static in order to post task to media thread in order to avoid threading | |
| 67 // race condition. | |
| 68 static void OnMessageReceivedOnMainThread( | |
| 69 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner, | |
| 70 base::WeakPtr<RemoteRendererImpl> self, | |
| 71 std::unique_ptr<remoting::pb::RpcMessage> message); | |
| 72 | |
| 73 // Callback when remoting interstitial needs to be updated. Will post task to | |
| 74 // media thread to avoid threading race condition. | |
| 75 static void RequestUpdateInterstitialOnMainThread( | |
| 76 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner, | |
| 77 base::WeakPtr<RemoteRendererImpl> remote_renderer_impl, | |
| 78 const base::Optional<SkBitmap>& background_image, | |
| 79 const gfx::Size& canvas_size, | |
| 80 RemotingInterstitialType interstitial_type); | |
| 81 | |
| 82 public: | |
| 83 // media::Renderer implementation. | |
| 84 void Initialize(DemuxerStreamProvider* demuxer_stream_provider, | |
| 85 media::RendererClient* client, | |
| 86 const PipelineStatusCB& init_cb) final; | |
| 87 void SetCdm(CdmContext* cdm_context, | |
| 88 const CdmAttachedCB& cdm_attached_cb) final; | |
| 89 void Flush(const base::Closure& flush_cb) final; | |
| 90 void StartPlayingFrom(base::TimeDelta time) final; | |
| 91 void SetPlaybackRate(double playback_rate) final; | |
| 92 void SetVolume(float volume) final; | |
| 93 base::TimeDelta GetMediaTime() final; | |
| 94 | |
| 95 private: | |
| 96 friend class RemoteRendererImplTest; | |
| 97 | |
| 98 enum State { | |
| 99 STATE_UNINITIALIZED, | |
| 100 STATE_CREATE_PIPE, | |
| 101 STATE_ACQUIRING, | |
| 102 STATE_INITIALIZING, | |
| 103 STATE_FLUSHING, | |
| 104 STATE_PLAYING, | |
| 105 STATE_ERROR | |
| 106 }; | |
| 107 | |
| 108 // Callback when attempting to establish data pipe. Runs on media thread only. | |
| 109 void OnDataPipeCreated(mojom::RemotingDataStreamSenderPtrInfo audio, | |
| 110 mojom::RemotingDataStreamSenderPtrInfo video, | |
| 111 mojo::ScopedDataPipeProducerHandle audio_handle, | |
| 112 mojo::ScopedDataPipeProducerHandle video_handle, | |
| 113 int audio_rpc_handle, | |
| 114 int video_rpc_handle); | |
| 115 | |
| 116 // Callback function when RPC message is received. Runs on media thread only. | |
| 117 void OnReceivedRpc(std::unique_ptr<remoting::pb::RpcMessage> message); | |
| 118 | |
| 119 // Function to post task to main thread in order to send RPC message. | |
| 120 void SendRpcToRemote(std::unique_ptr<remoting::pb::RpcMessage> message); | |
| 121 | |
| 122 // Functions when RPC message is received. | |
| 123 void AcquireRendererDone(std::unique_ptr<remoting::pb::RpcMessage> message); | |
| 124 void InitializeCallback(std::unique_ptr<remoting::pb::RpcMessage> message); | |
| 125 void FlushUntilCallback(); | |
| 126 void SetCdmCallback(std::unique_ptr<remoting::pb::RpcMessage> message); | |
| 127 void OnTimeUpdate(std::unique_ptr<remoting::pb::RpcMessage> message); | |
| 128 void OnBufferingStateChange( | |
| 129 std::unique_ptr<remoting::pb::RpcMessage> message); | |
| 130 void OnVideoNaturalSizeChange( | |
| 131 std::unique_ptr<remoting::pb::RpcMessage> message); | |
| 132 void OnVideoOpacityChange(std::unique_ptr<remoting::pb::RpcMessage> message); | |
| 133 void OnStatisticsUpdate(std::unique_ptr<remoting::pb::RpcMessage> message); | |
| 134 void OnDurationChange(std::unique_ptr<remoting::pb::RpcMessage> message); | |
| 135 | |
| 136 // Called to update the remoting interstitial. Update | |
| 137 // |interstitial_background_| if |background_image| is set. | |
| 138 void UpdateInterstitial(const base::Optional<SkBitmap>& background_image, | |
| 139 const gfx::Size& canvas_size, | |
| 140 RemotingInterstitialType interstitial_type); | |
| 141 | |
| 142 // Called when |current_media_time_| is updated. | |
| 143 void OnMediaTimeUpdated(); | |
| 144 | |
| 145 // Called to update the |video_stats_queue_|. | |
| 146 void UpdateVideoStatsQueue(int video_frames_decoded, | |
| 147 int video_frames_dropped); | |
| 148 | |
| 149 // Called to clear all recent measurements history and schedule resuming after | |
| 150 // a stabilization period elapses. | |
| 151 void ResetMeasurements(); | |
| 152 | |
| 153 // Called when a fatal runtime error occurs. |stop_trigger| is the error code | |
| 154 // handed to the RemotingRendererController. | |
| 155 void OnFatalError(remoting::StopTrigger stop_trigger); | |
| 156 | |
| 157 // Called periodically to measure the data flows from the | |
| 158 // DemuxerStreamAdapters and record this information in the metrics. | |
| 159 void MeasureAndRecordDataRates(); | |
| 160 | |
| 161 State state_; | |
| 162 const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | |
| 163 const scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; | |
| 164 | |
| 165 // Current renderer playback time information. | |
| 166 base::TimeDelta current_media_time_; | |
| 167 base::TimeDelta current_max_time_; | |
| 168 // Both |current_media_time_| and |current_max_time_| should be protected by | |
| 169 // lock because it can be accessed from both media and render main thread. | |
| 170 base::Lock time_lock_; | |
| 171 | |
| 172 DemuxerStreamProvider* demuxer_stream_provider_; | |
| 173 media::RendererClient* client_; | |
| 174 std::unique_ptr<remoting::RemoteDemuxerStreamAdapter> | |
| 175 audio_demuxer_stream_adapter_; | |
| 176 std::unique_ptr<remoting::RemoteDemuxerStreamAdapter> | |
| 177 video_demuxer_stream_adapter_; | |
| 178 | |
| 179 // Component to establish mojo remoting service on browser process. | |
| 180 const base::WeakPtr<RemotingRendererController> remoting_renderer_controller_; | |
| 181 // Broker class to process incoming and outgoing RPC message. | |
| 182 const base::WeakPtr<remoting::RpcBroker> rpc_broker_; | |
| 183 // RPC handle value for RemoteRendererImpl component. | |
| 184 const int rpc_handle_; | |
| 185 | |
| 186 // RPC handle value for render on receiver endpoint. | |
| 187 int remote_renderer_handle_; | |
| 188 | |
| 189 // Callbacks. | |
| 190 PipelineStatusCB init_workflow_done_callback_; | |
| 191 CdmAttachedCB cdm_attached_cb_; | |
| 192 base::Closure flush_cb_; | |
| 193 | |
| 194 VideoRendererSink* const video_renderer_sink_; // Outlives this class. | |
| 195 // The background image for remoting interstitial. When |this| is destructed, | |
| 196 // |interstitial_background_| will be paint to clear the cast messages on | |
| 197 // the interstitial. | |
| 198 SkBitmap interstitial_background_; | |
| 199 gfx::Size canvas_size_; | |
| 200 | |
| 201 // Current playback rate. | |
| 202 double playback_rate_ = 0; | |
| 203 | |
| 204 // Ignores updates until this time. | |
| 205 base::TimeTicks ignore_updates_until_time_; | |
| 206 | |
| 207 // Indicates whether stats has been updated. | |
| 208 bool stats_updated_ = false; | |
| 209 | |
| 210 // Stores all |current_media_time_| and the local time when updated in the | |
| 211 // moving time window. This is used to check whether the playback duration | |
| 212 // matches the update duration in the window. | |
| 213 std::deque<std::pair<base::TimeTicks, base::TimeDelta>> media_time_queue_; | |
| 214 | |
| 215 // Stores all updates on the number of video frames decoded/dropped, and the | |
| 216 // local time when updated in the moving time window. This is used to check | |
| 217 // whether too many video frames were dropped. | |
| 218 std::deque<std::tuple<base::TimeTicks, int, int>> video_stats_queue_; | |
| 219 | |
| 220 // The total number of frames decoded/dropped in the time window. | |
| 221 int sum_video_frames_decoded_ = 0; | |
| 222 int sum_video_frames_dropped_ = 0; | |
| 223 | |
| 224 // Records the number of consecutive times that remoting playback was delayed. | |
| 225 int times_playback_delayed_ = 0; | |
| 226 | |
| 227 // Records events and measurements of interest. | |
| 228 remoting::RendererMetricsRecorder metrics_recorder_; | |
| 229 | |
| 230 // A timer that polls the RemoteDemuxerStreamAdapters periodically to measure | |
| 231 // the data flow rates for metrics. | |
| 232 base::RepeatingTimer data_flow_poll_timer_; | |
| 233 | |
| 234 base::WeakPtrFactory<RemoteRendererImpl> weak_factory_; | |
| 235 | |
| 236 DISALLOW_COPY_AND_ASSIGN(RemoteRendererImpl); | |
| 237 }; | |
| 238 | |
| 239 } // namespace media | |
| 240 | |
| 241 #endif // MEDIA_REMOTING_REMOTE_RENDERER_IMPL_H_ | |
| OLD | NEW |