OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chromecast/renderer/media/video_pipeline_proxy.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" |
| 9 #include "base/memory/shared_memory.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/threading/thread_checker.h" |
| 12 #include "chromecast/common/media/cma_ipc_common.h" |
| 13 #include "chromecast/common/media/cma_messages.h" |
| 14 #include "chromecast/common/media/shared_memory_chunk.h" |
| 15 #include "chromecast/media/cma/base/buffering_defs.h" |
| 16 #include "chromecast/media/cma/base/cma_logging.h" |
| 17 #include "chromecast/media/cma/base/coded_frame_provider.h" |
| 18 #include "chromecast/media/cma/ipc/media_message_fifo.h" |
| 19 #include "chromecast/media/cma/ipc_streamer/av_streamer_proxy.h" |
| 20 #include "chromecast/renderer/media/cma_message_filter_proxy.h" |
| 21 #include "chromecast/renderer/media/media_channel_proxy.h" |
| 22 #include "media/base/bind_to_current_loop.h" |
| 23 #include "media/base/pipeline_status.h" |
| 24 |
| 25 namespace chromecast { |
| 26 namespace media { |
| 27 |
| 28 namespace { |
| 29 |
| 30 void IgnoreResult() { |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
| 35 // VideoPipelineProxyInternal - |
| 36 // This class is not thread safe and should run on the same thread |
| 37 // as the media channel proxy. |
| 38 class VideoPipelineProxyInternal { |
| 39 public: |
| 40 typedef base::Callback<void(scoped_ptr<base::SharedMemory>)> SharedMemCB; |
| 41 |
| 42 static void Release(scoped_ptr<VideoPipelineProxyInternal> proxy); |
| 43 |
| 44 explicit VideoPipelineProxyInternal( |
| 45 scoped_refptr<MediaChannelProxy> media_channel_proxy); |
| 46 virtual ~VideoPipelineProxyInternal(); |
| 47 |
| 48 // Notify the other side (browser process) of some activity on the video pipe. |
| 49 // TODO(erickung): either send an IPC message or write a byte on the |
| 50 // SyncSocket. |
| 51 void NotifyPipeWrite(); |
| 52 |
| 53 // These functions are almost a one to one correspondence with VideoPipeline |
| 54 // but this is an internal class and there is no reason to derive from |
| 55 // VideoPipeline. |
| 56 void SetClient(const base::Closure& pipe_read_cb, |
| 57 const VideoPipelineClient& client); |
| 58 void CreateAvPipe(const SharedMemCB& shared_mem_cb); |
| 59 void Initialize(const ::media::VideoDecoderConfig& config, |
| 60 const ::media::PipelineStatusCB& status_cb); |
| 61 |
| 62 private: |
| 63 void Shutdown(); |
| 64 |
| 65 // Callbacks for CmaMessageFilterHost::VideoDelegate. |
| 66 void OnAvPipeCreated(bool status, |
| 67 base::SharedMemoryHandle shared_mem_handle, |
| 68 base::FileDescriptor socket); |
| 69 void OnStateChanged(::media::PipelineStatus status); |
| 70 |
| 71 base::ThreadChecker thread_checker_; |
| 72 |
| 73 scoped_refptr<MediaChannelProxy> media_channel_proxy_; |
| 74 |
| 75 // Store the callback for a pending state transition. |
| 76 ::media::PipelineStatusCB status_cb_; |
| 77 |
| 78 SharedMemCB shared_mem_cb_; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(VideoPipelineProxyInternal); |
| 81 }; |
| 82 |
| 83 // static |
| 84 void VideoPipelineProxyInternal::Release( |
| 85 scoped_ptr<VideoPipelineProxyInternal> proxy) { |
| 86 proxy->Shutdown(); |
| 87 } |
| 88 |
| 89 VideoPipelineProxyInternal::VideoPipelineProxyInternal( |
| 90 scoped_refptr<MediaChannelProxy> media_channel_proxy) |
| 91 : media_channel_proxy_(media_channel_proxy) { |
| 92 DCHECK(media_channel_proxy.get()); |
| 93 |
| 94 // Creation can be done on a different thread. |
| 95 thread_checker_.DetachFromThread(); |
| 96 } |
| 97 |
| 98 VideoPipelineProxyInternal::~VideoPipelineProxyInternal() { |
| 99 } |
| 100 |
| 101 void VideoPipelineProxyInternal::Shutdown() { |
| 102 DCHECK(thread_checker_.CalledOnValidThread()); |
| 103 |
| 104 // Remove any callback on VideoPipelineProxyInternal. |
| 105 media_channel_proxy_->SetVideoDelegate( |
| 106 CmaMessageFilterProxy::VideoDelegate()); |
| 107 } |
| 108 |
| 109 void VideoPipelineProxyInternal::NotifyPipeWrite() { |
| 110 DCHECK(thread_checker_.CalledOnValidThread()); |
| 111 |
| 112 // TODO(damienv): An alternative way would be to use a dedicated socket for |
| 113 // this event. |
| 114 bool success = media_channel_proxy_->Send(scoped_ptr<IPC::Message>( |
| 115 new CmaHostMsg_NotifyPipeWrite(media_channel_proxy_->GetId(), |
| 116 kVideoTrackId))); |
| 117 VLOG_IF(4, !success) << "Sending msg failed"; |
| 118 } |
| 119 |
| 120 void VideoPipelineProxyInternal::SetClient( |
| 121 const base::Closure& pipe_read_cb, |
| 122 const VideoPipelineClient& video_client) { |
| 123 DCHECK(thread_checker_.CalledOnValidThread()); |
| 124 |
| 125 CmaMessageFilterProxy::VideoDelegate delegate; |
| 126 delegate.av_pipe_cb = |
| 127 base::Bind(&VideoPipelineProxyInternal::OnAvPipeCreated, |
| 128 base::Unretained(this)); |
| 129 delegate.state_changed_cb = |
| 130 base::Bind(&VideoPipelineProxyInternal::OnStateChanged, |
| 131 base::Unretained(this)); |
| 132 delegate.pipe_read_cb = pipe_read_cb; |
| 133 delegate.client = video_client; |
| 134 bool success = media_channel_proxy_->SetVideoDelegate(delegate); |
| 135 CHECK(success); |
| 136 } |
| 137 |
| 138 void VideoPipelineProxyInternal::CreateAvPipe( |
| 139 const SharedMemCB& shared_mem_cb) { |
| 140 DCHECK(thread_checker_.CalledOnValidThread()); |
| 141 DCHECK(shared_mem_cb_.is_null()); |
| 142 bool success = media_channel_proxy_->Send(scoped_ptr<IPC::Message>( |
| 143 new CmaHostMsg_CreateAvPipe( |
| 144 media_channel_proxy_->GetId(), kVideoTrackId, kAppVideoBufferSize))); |
| 145 if (!success) { |
| 146 shared_mem_cb.Run(scoped_ptr<base::SharedMemory>()); |
| 147 return; |
| 148 } |
| 149 shared_mem_cb_ = shared_mem_cb; |
| 150 } |
| 151 |
| 152 void VideoPipelineProxyInternal::OnAvPipeCreated( |
| 153 bool success, |
| 154 base::SharedMemoryHandle shared_mem_handle, |
| 155 base::FileDescriptor socket) { |
| 156 DCHECK(thread_checker_.CalledOnValidThread()); |
| 157 DCHECK(!shared_mem_cb_.is_null()); |
| 158 if (!success) { |
| 159 shared_mem_cb_.Run(scoped_ptr<base::SharedMemory>()); |
| 160 return; |
| 161 } |
| 162 |
| 163 CHECK(base::SharedMemory::IsHandleValid(shared_mem_handle)); |
| 164 shared_mem_cb_.Run(scoped_ptr<base::SharedMemory>( |
| 165 new base::SharedMemory(shared_mem_handle, false))); |
| 166 } |
| 167 |
| 168 void VideoPipelineProxyInternal::Initialize( |
| 169 const ::media::VideoDecoderConfig& arg1, |
| 170 const ::media::PipelineStatusCB& status_cb) { |
| 171 DCHECK(thread_checker_.CalledOnValidThread()); |
| 172 bool success = media_channel_proxy_->Send(scoped_ptr<IPC::Message>( |
| 173 new CmaHostMsg_VideoInitialize(media_channel_proxy_->GetId(), |
| 174 kVideoTrackId, arg1))); |
| 175 if (!success) { |
| 176 status_cb.Run( ::media::PIPELINE_ERROR_INITIALIZATION_FAILED); |
| 177 return; |
| 178 } |
| 179 DCHECK(status_cb_.is_null()); |
| 180 status_cb_ = status_cb; |
| 181 } |
| 182 |
| 183 void VideoPipelineProxyInternal::OnStateChanged( |
| 184 ::media::PipelineStatus status) { |
| 185 DCHECK(thread_checker_.CalledOnValidThread()); |
| 186 DCHECK(!status_cb_.is_null()); |
| 187 base::ResetAndReturn(&status_cb_).Run(status); |
| 188 } |
| 189 |
| 190 |
| 191 // A macro runs current member function on |io_message_loop_proxy_| thread. |
| 192 #define FORWARD_ON_IO_THREAD(param_fn, ...) \ |
| 193 io_message_loop_proxy_->PostTask( \ |
| 194 FROM_HERE, \ |
| 195 base::Bind(&VideoPipelineProxyInternal::param_fn, \ |
| 196 base::Unretained(proxy_.get()), ##__VA_ARGS__)) |
| 197 |
| 198 VideoPipelineProxy::VideoPipelineProxy( |
| 199 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy, |
| 200 scoped_refptr<MediaChannelProxy> media_channel_proxy) |
| 201 : io_message_loop_proxy_(io_message_loop_proxy), |
| 202 proxy_(new VideoPipelineProxyInternal(media_channel_proxy)), |
| 203 video_streamer_(new AvStreamerProxy()), |
| 204 weak_factory_(this) { |
| 205 DCHECK(io_message_loop_proxy_.get()); |
| 206 weak_this_ = weak_factory_.GetWeakPtr(); |
| 207 thread_checker_.DetachFromThread(); |
| 208 } |
| 209 |
| 210 VideoPipelineProxy::~VideoPipelineProxy() { |
| 211 DCHECK(thread_checker_.CalledOnValidThread()); |
| 212 // Release the underlying object on the right thread. |
| 213 io_message_loop_proxy_->PostTask( |
| 214 FROM_HERE, |
| 215 base::Bind(&VideoPipelineProxyInternal::Release, base::Passed(&proxy_))); |
| 216 } |
| 217 |
| 218 void VideoPipelineProxy::SetClient( |
| 219 const VideoPipelineClient& video_client) { |
| 220 DCHECK(thread_checker_.CalledOnValidThread()); |
| 221 base::Closure pipe_read_cb = |
| 222 ::media::BindToCurrentLoop( |
| 223 base::Bind(&VideoPipelineProxy::OnPipeRead, weak_this_)); |
| 224 FORWARD_ON_IO_THREAD(SetClient, pipe_read_cb, video_client); |
| 225 } |
| 226 |
| 227 void VideoPipelineProxy::Initialize( |
| 228 const ::media::VideoDecoderConfig& config, |
| 229 scoped_ptr<CodedFrameProvider> frame_provider, |
| 230 const ::media::PipelineStatusCB& status_cb) { |
| 231 CMALOG(kLogControl) << "VideoPipelineProxy::Initialize"; |
| 232 DCHECK(thread_checker_.CalledOnValidThread()); |
| 233 video_streamer_->SetCodedFrameProvider(frame_provider.Pass()); |
| 234 |
| 235 VideoPipelineProxyInternal::SharedMemCB shared_mem_cb = |
| 236 ::media::BindToCurrentLoop(base::Bind( |
| 237 &VideoPipelineProxy::OnAvPipeCreated, weak_this_, |
| 238 config, status_cb)); |
| 239 FORWARD_ON_IO_THREAD(CreateAvPipe, shared_mem_cb); |
| 240 } |
| 241 |
| 242 void VideoPipelineProxy::OnAvPipeCreated( |
| 243 const ::media::VideoDecoderConfig& config, |
| 244 const ::media::PipelineStatusCB& status_cb, |
| 245 scoped_ptr<base::SharedMemory> shared_memory) { |
| 246 CMALOG(kLogControl) << "VideoPipelineProxy::OnAvPipeCreated"; |
| 247 DCHECK(thread_checker_.CalledOnValidThread()); |
| 248 if (!shared_memory || |
| 249 !shared_memory->Map(kAppVideoBufferSize)) { |
| 250 status_cb.Run(::media::PIPELINE_ERROR_INITIALIZATION_FAILED); |
| 251 return; |
| 252 } |
| 253 CHECK(shared_memory->memory()); |
| 254 |
| 255 scoped_ptr<MediaMemoryChunk> shared_memory_chunk( |
| 256 new SharedMemoryChunk(shared_memory.Pass(), kAppVideoBufferSize)); |
| 257 scoped_ptr<MediaMessageFifo> video_pipe( |
| 258 new MediaMessageFifo(shared_memory_chunk.Pass(), false)); |
| 259 video_pipe->ObserveWriteActivity( |
| 260 base::Bind(&VideoPipelineProxy::OnPipeWrite, weak_this_)); |
| 261 |
| 262 video_streamer_->SetMediaMessageFifo(video_pipe.Pass()); |
| 263 |
| 264 // Now proceed to the decoder/renderer initialization. |
| 265 FORWARD_ON_IO_THREAD(Initialize, config, status_cb); |
| 266 } |
| 267 |
| 268 void VideoPipelineProxy::StartFeeding() { |
| 269 DCHECK(thread_checker_.CalledOnValidThread()); |
| 270 DCHECK(video_streamer_); |
| 271 video_streamer_->Start(); |
| 272 } |
| 273 |
| 274 void VideoPipelineProxy::Flush(const base::Closure& done_cb) { |
| 275 DCHECK(thread_checker_.CalledOnValidThread()); |
| 276 DCHECK(video_streamer_); |
| 277 video_streamer_->StopAndFlush(done_cb); |
| 278 } |
| 279 |
| 280 void VideoPipelineProxy::Stop() { |
| 281 DCHECK(thread_checker_.CalledOnValidThread()); |
| 282 if (!video_streamer_) |
| 283 return; |
| 284 video_streamer_->StopAndFlush(base::Bind(&IgnoreResult)); |
| 285 } |
| 286 |
| 287 void VideoPipelineProxy::OnPipeWrite() { |
| 288 DCHECK(thread_checker_.CalledOnValidThread()); |
| 289 FORWARD_ON_IO_THREAD(NotifyPipeWrite); |
| 290 } |
| 291 |
| 292 void VideoPipelineProxy::OnPipeRead() { |
| 293 DCHECK(thread_checker_.CalledOnValidThread()); |
| 294 if (video_streamer_) |
| 295 video_streamer_->OnFifoReadEvent(); |
| 296 } |
| 297 |
| 298 } // namespace media |
| 299 } // namespace chromecast |
OLD | NEW |