| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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 "content/browser/renderer_host/video_capture_host.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/stl_util-inl.h" |
| 9 #include "content/common/video_capture_messages.h" |
| 10 |
| 11 VideoCaptureHost::VideoCaptureHost() {} |
| 12 |
| 13 VideoCaptureHost::~VideoCaptureHost() {} |
| 14 |
| 15 void VideoCaptureHost::OnChannelClosing() { |
| 16 BrowserMessageFilter::OnChannelClosing(); |
| 17 |
| 18 // Since the IPC channel is gone, close all requested VideCaptureDevices. |
| 19 for (EntryMap::iterator it = entries_.begin(); it != entries_.end(); it++) { |
| 20 VideoCaptureController* controller = it->second; |
| 21 // Since the channel is closing we need a task to make sure VideoCaptureHost |
| 22 // is not deleted before VideoCaptureController. |
| 23 controller->StopCapture( |
| 24 NewRunnableMethod(this, &VideoCaptureHost::OnReadyToDelete, it->first)); |
| 25 } |
| 26 } |
| 27 |
| 28 void VideoCaptureHost::OnDestruct() const { |
| 29 BrowserThread::DeleteOnIOThread::Destruct(this); |
| 30 } |
| 31 |
| 32 /////////////////////////////////////////////////////////////////////////////// |
| 33 |
| 34 // Implements VideoCaptureController::EventHandler. |
| 35 void VideoCaptureHost::OnError(VideoCaptureController::ControllerId id) { |
| 36 BrowserThread::PostTask( |
| 37 BrowserThread::IO, FROM_HERE, |
| 38 NewRunnableMethod(this, &VideoCaptureHost::DoHandleError, id.first, |
| 39 id.second)); |
| 40 } |
| 41 |
| 42 void VideoCaptureHost::OnBufferReady( |
| 43 VideoCaptureController::ControllerId id, |
| 44 TransportDIB::Handle handle, |
| 45 base::Time timestamp) { |
| 46 BrowserThread::PostTask( |
| 47 BrowserThread::IO, FROM_HERE, |
| 48 NewRunnableMethod(this, &VideoCaptureHost::DoSendFilledBuffer, id.first, |
| 49 id.second, handle, timestamp)); |
| 50 } |
| 51 |
| 52 void VideoCaptureHost::OnFrameInfo(VideoCaptureController::ControllerId id, |
| 53 int width, |
| 54 int height, |
| 55 int frame_per_second) { |
| 56 BrowserThread::PostTask( |
| 57 BrowserThread::IO, FROM_HERE, |
| 58 NewRunnableMethod(this, &VideoCaptureHost::DoSendFrameInfo, id.first, |
| 59 id.second, width, height, frame_per_second)); |
| 60 } |
| 61 |
| 62 void VideoCaptureHost::OnReadyToDelete( |
| 63 VideoCaptureController::ControllerId id) { |
| 64 BrowserThread::PostTask( |
| 65 BrowserThread::IO, FROM_HERE, |
| 66 NewRunnableMethod(this, &VideoCaptureHost::DoDeleteVideoCaptureController, |
| 67 id)); |
| 68 } |
| 69 |
| 70 void VideoCaptureHost::DoSendFilledBuffer(int32 routing_id, |
| 71 int device_id, |
| 72 TransportDIB::Handle handle, |
| 73 base::Time timestamp) { |
| 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 75 |
| 76 Send(new VideoCaptureMsg_BufferReady(routing_id, device_id, handle, |
| 77 timestamp)); |
| 78 } |
| 79 |
| 80 void VideoCaptureHost::DoHandleError(int32 routing_id, int device_id) { |
| 81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 82 |
| 83 Send(new VideoCaptureMsg_StateChanged(routing_id, device_id, |
| 84 media::VideoCapture::kError)); |
| 85 |
| 86 VideoCaptureController::ControllerId id(routing_id, device_id); |
| 87 EntryMap::iterator it = entries_.find(id); |
| 88 if (it != entries_.end()) { |
| 89 VideoCaptureController* controller = it->second; |
| 90 controller->StopCapture(NULL); |
| 91 } |
| 92 } |
| 93 |
| 94 void VideoCaptureHost::DoSendFrameInfo(int32 routing_id, |
| 95 int device_id, |
| 96 int width, |
| 97 int height, |
| 98 int frame_per_second) { |
| 99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 100 |
| 101 media::VideoCaptureParams params; |
| 102 params.width = width; |
| 103 params.height = height; |
| 104 params.frame_per_second = frame_per_second; |
| 105 Send(new VideoCaptureMsg_DeviceInfo(routing_id, device_id, params)); |
| 106 Send(new VideoCaptureMsg_StateChanged(routing_id, device_id, |
| 107 media::VideoCapture::kStarted)); |
| 108 } |
| 109 |
| 110 /////////////////////////////////////////////////////////////////////////////// |
| 111 // IPC Messages handler. |
| 112 bool VideoCaptureHost::OnMessageReceived(const IPC::Message& message, |
| 113 bool* message_was_ok) { |
| 114 bool handled = true; |
| 115 IPC_BEGIN_MESSAGE_MAP_EX(VideoCaptureHost, message, *message_was_ok) |
| 116 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Start, OnStartCapture) |
| 117 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Pause, OnPauseCapture) |
| 118 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Stop, OnStopCapture) |
| 119 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady, OnReceiveEmptyBuffer) |
| 120 IPC_MESSAGE_UNHANDLED(handled = false) |
| 121 IPC_END_MESSAGE_MAP_EX() |
| 122 |
| 123 return handled; |
| 124 } |
| 125 |
| 126 void VideoCaptureHost::OnStartCapture(const IPC::Message& msg, int device_id, |
| 127 const media::VideoCaptureParams& params) { |
| 128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 129 |
| 130 VideoCaptureController::ControllerId controller_id(msg.routing_id(), |
| 131 device_id); |
| 132 |
| 133 DCHECK(entries_.find(controller_id) == entries_.end()); |
| 134 |
| 135 scoped_refptr<VideoCaptureController> controller = |
| 136 new VideoCaptureController(controller_id, peer_handle(), this); |
| 137 entries_.insert(std::make_pair(controller_id, controller)); |
| 138 controller->StartCapture(params); |
| 139 } |
| 140 |
| 141 void VideoCaptureHost::OnStopCapture(const IPC::Message& msg, int device_id) { |
| 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 143 |
| 144 VideoCaptureController::ControllerId controller_id(msg.routing_id(), |
| 145 device_id); |
| 146 EntryMap::iterator it = entries_.find(controller_id); |
| 147 if (it != entries_.end()) { |
| 148 scoped_refptr<VideoCaptureController> controller = it->second; |
| 149 controller->StopCapture(NULL); |
| 150 } else { |
| 151 // It does not exist so it must have been stopped already. |
| 152 Send(new VideoCaptureMsg_StateChanged(msg.routing_id(), device_id, |
| 153 media::VideoCapture::kStopped)); |
| 154 } |
| 155 } |
| 156 |
| 157 void VideoCaptureHost::OnPauseCapture(const IPC::Message& msg, int device_id) { |
| 158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 159 // Not used. |
| 160 Send(new VideoCaptureMsg_StateChanged(msg.routing_id(), device_id, |
| 161 media::VideoCapture::kError)); |
| 162 } |
| 163 |
| 164 void VideoCaptureHost::OnReceiveEmptyBuffer(const IPC::Message& msg, |
| 165 int device_id, |
| 166 TransportDIB::Handle handle) { |
| 167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 168 |
| 169 VideoCaptureController::ControllerId controller_id(msg.routing_id(), |
| 170 device_id); |
| 171 EntryMap::iterator it = entries_.find(controller_id); |
| 172 if (it != entries_.end()) { |
| 173 scoped_refptr<VideoCaptureController> controller = it->second; |
| 174 controller->ReturnTransportDIB(handle); |
| 175 } |
| 176 } |
| 177 |
| 178 void VideoCaptureHost::DoDeleteVideoCaptureController( |
| 179 VideoCaptureController::ControllerId id) { |
| 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 181 |
| 182 // Report that the device have successfully been stopped. |
| 183 Send(new VideoCaptureMsg_StateChanged(id.first, id.second, |
| 184 media::VideoCapture::kStopped)); |
| 185 entries_.erase(id); |
| 186 } |
| OLD | NEW |