| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/renderer_host/media/video_capture_host.h" | 5 #include "content/browser/renderer_host/media/video_capture_host.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/stl_util-inl.h" | 8 #include "base/stl_util-inl.h" |
| 9 #include "content/common/media/video_capture_messages.h" | 9 #include "content/common/media/video_capture_messages.h" |
| 10 | 10 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 void VideoCaptureHost::OnDestruct() const { | 28 void VideoCaptureHost::OnDestruct() const { |
| 29 BrowserThread::DeleteOnIOThread::Destruct(this); | 29 BrowserThread::DeleteOnIOThread::Destruct(this); |
| 30 } | 30 } |
| 31 | 31 |
| 32 /////////////////////////////////////////////////////////////////////////////// | 32 /////////////////////////////////////////////////////////////////////////////// |
| 33 | 33 |
| 34 // Implements VideoCaptureControllerEventHandler. | 34 // Implements VideoCaptureControllerEventHandler. |
| 35 void VideoCaptureHost::OnError(const VideoCaptureControllerID& id) { | 35 void VideoCaptureHost::OnError(const VideoCaptureControllerID& id) { |
| 36 BrowserThread::PostTask( | 36 BrowserThread::PostTask( |
| 37 BrowserThread::IO, FROM_HERE, | 37 BrowserThread::IO, FROM_HERE, |
| 38 NewRunnableMethod(this, &VideoCaptureHost::DoHandleError, id.routing_id, | 38 NewRunnableMethod(this, &VideoCaptureHost::DoHandleError, id.device_id)); |
| 39 id.device_id)); | |
| 40 } | 39 } |
| 41 | 40 |
| 42 void VideoCaptureHost::OnBufferCreated( | 41 void VideoCaptureHost::OnBufferCreated( |
| 43 const VideoCaptureControllerID& id, | 42 const VideoCaptureControllerID& id, |
| 44 base::SharedMemoryHandle handle, | 43 base::SharedMemoryHandle handle, |
| 45 int length, | 44 int length, |
| 46 int buffer_id) { | 45 int buffer_id) { |
| 47 BrowserThread::PostTask( | 46 BrowserThread::PostTask( |
| 48 BrowserThread::IO, FROM_HERE, | 47 BrowserThread::IO, FROM_HERE, |
| 49 NewRunnableMethod(this, &VideoCaptureHost::DoSendNewBuffer, | 48 NewRunnableMethod(this, &VideoCaptureHost::DoSendNewBuffer, |
| 50 id.routing_id, id.device_id, handle, length, | 49 id.device_id, handle, length, buffer_id)); |
| 51 buffer_id)); | |
| 52 } | 50 } |
| 53 | 51 |
| 54 void VideoCaptureHost::OnBufferReady( | 52 void VideoCaptureHost::OnBufferReady( |
| 55 const VideoCaptureControllerID& id, | 53 const VideoCaptureControllerID& id, |
| 56 int buffer_id, | 54 int buffer_id, |
| 57 base::Time timestamp) { | 55 base::Time timestamp) { |
| 58 BrowserThread::PostTask( | 56 BrowserThread::PostTask( |
| 59 BrowserThread::IO, FROM_HERE, | 57 BrowserThread::IO, FROM_HERE, |
| 60 NewRunnableMethod(this, &VideoCaptureHost::DoSendFilledBuffer, | 58 NewRunnableMethod(this, &VideoCaptureHost::DoSendFilledBuffer, |
| 61 id.routing_id, id.device_id, buffer_id, timestamp)); | 59 id.device_id, buffer_id, timestamp)); |
| 62 } | 60 } |
| 63 | 61 |
| 64 void VideoCaptureHost::OnFrameInfo(const VideoCaptureControllerID& id, | 62 void VideoCaptureHost::OnFrameInfo(const VideoCaptureControllerID& id, |
| 65 int width, | 63 int width, |
| 66 int height, | 64 int height, |
| 67 int frame_per_second) { | 65 int frame_per_second) { |
| 68 BrowserThread::PostTask( | 66 BrowserThread::PostTask( |
| 69 BrowserThread::IO, FROM_HERE, | 67 BrowserThread::IO, FROM_HERE, |
| 70 NewRunnableMethod(this, &VideoCaptureHost::DoSendFrameInfo, id.routing_id, | 68 NewRunnableMethod(this, &VideoCaptureHost::DoSendFrameInfo, |
| 71 id.device_id, width, height, frame_per_second)); | 69 id.device_id, width, height, frame_per_second)); |
| 72 } | 70 } |
| 73 | 71 |
| 74 void VideoCaptureHost::OnReadyToDelete(const VideoCaptureControllerID& id) { | 72 void VideoCaptureHost::OnReadyToDelete(const VideoCaptureControllerID& id) { |
| 75 BrowserThread::PostTask( | 73 BrowserThread::PostTask( |
| 76 BrowserThread::IO, FROM_HERE, | 74 BrowserThread::IO, FROM_HERE, |
| 77 NewRunnableMethod(this, &VideoCaptureHost::DoDeleteVideoCaptureController, | 75 NewRunnableMethod(this, &VideoCaptureHost::DoDeleteVideoCaptureController, |
| 78 id)); | 76 id)); |
| 79 } | 77 } |
| 80 | 78 |
| 81 void VideoCaptureHost::DoSendNewBuffer( | 79 void VideoCaptureHost::DoSendNewBuffer( |
| 82 int32 routing_id, int device_id, base::SharedMemoryHandle handle, | 80 int device_id, base::SharedMemoryHandle handle, |
| 83 int length, int buffer_id) { | 81 int length, int buffer_id) { |
| 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 85 | 83 |
| 86 Send(new VideoCaptureMsg_NewBuffer(routing_id, device_id, handle, | 84 Send(new VideoCaptureMsg_NewBuffer(device_id, handle, |
| 87 length, buffer_id)); | 85 length, buffer_id)); |
| 88 } | 86 } |
| 89 | 87 |
| 90 void VideoCaptureHost::DoSendFilledBuffer( | 88 void VideoCaptureHost::DoSendFilledBuffer( |
| 91 int32 routing_id, int device_id, int buffer_id, base::Time timestamp) { | 89 int device_id, int buffer_id, base::Time timestamp) { |
| 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 93 | 91 |
| 94 Send(new VideoCaptureMsg_BufferReady(routing_id, device_id, buffer_id, | 92 Send(new VideoCaptureMsg_BufferReady(device_id, buffer_id, |
| 95 timestamp)); | 93 timestamp)); |
| 96 } | 94 } |
| 97 | 95 |
| 98 void VideoCaptureHost::DoHandleError(int32 routing_id, int device_id) { | 96 void VideoCaptureHost::DoHandleError(int device_id) { |
| 99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 100 | 98 |
| 101 Send(new VideoCaptureMsg_StateChanged(routing_id, device_id, | 99 Send(new VideoCaptureMsg_StateChanged(device_id, |
| 102 media::VideoCapture::kError)); | 100 media::VideoCapture::kError)); |
| 103 | 101 |
| 104 VideoCaptureControllerID id(routing_id, device_id); | 102 VideoCaptureControllerID id(device_id); |
| 105 EntryMap::iterator it = entries_.find(id); | 103 EntryMap::iterator it = entries_.find(id); |
| 106 if (it != entries_.end()) { | 104 if (it != entries_.end()) { |
| 107 VideoCaptureController* controller = it->second; | 105 VideoCaptureController* controller = it->second; |
| 108 controller->StopCapture(NULL); | 106 controller->StopCapture(NULL); |
| 109 } | 107 } |
| 110 } | 108 } |
| 111 | 109 |
| 112 void VideoCaptureHost::DoSendFrameInfo(int32 routing_id, | 110 void VideoCaptureHost::DoSendFrameInfo(int device_id, |
| 113 int device_id, | |
| 114 int width, | 111 int width, |
| 115 int height, | 112 int height, |
| 116 int frame_per_second) { | 113 int frame_per_second) { |
| 117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 118 | 115 |
| 119 media::VideoCaptureParams params; | 116 media::VideoCaptureParams params; |
| 120 params.width = width; | 117 params.width = width; |
| 121 params.height = height; | 118 params.height = height; |
| 122 params.frame_per_second = frame_per_second; | 119 params.frame_per_second = frame_per_second; |
| 123 Send(new VideoCaptureMsg_DeviceInfo(routing_id, device_id, params)); | 120 Send(new VideoCaptureMsg_DeviceInfo(device_id, params)); |
| 124 Send(new VideoCaptureMsg_StateChanged(routing_id, device_id, | 121 Send(new VideoCaptureMsg_StateChanged(device_id, |
| 125 media::VideoCapture::kStarted)); | 122 media::VideoCapture::kStarted)); |
| 126 } | 123 } |
| 127 | 124 |
| 128 /////////////////////////////////////////////////////////////////////////////// | 125 /////////////////////////////////////////////////////////////////////////////// |
| 129 // IPC Messages handler. | 126 // IPC Messages handler. |
| 130 bool VideoCaptureHost::OnMessageReceived(const IPC::Message& message, | 127 bool VideoCaptureHost::OnMessageReceived(const IPC::Message& message, |
| 131 bool* message_was_ok) { | 128 bool* message_was_ok) { |
| 132 bool handled = true; | 129 bool handled = true; |
| 133 IPC_BEGIN_MESSAGE_MAP_EX(VideoCaptureHost, message, *message_was_ok) | 130 IPC_BEGIN_MESSAGE_MAP_EX(VideoCaptureHost, message, *message_was_ok) |
| 134 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Start, OnStartCapture) | 131 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Start, OnStartCapture) |
| 135 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Pause, OnPauseCapture) | 132 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Pause, OnPauseCapture) |
| 136 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Stop, OnStopCapture) | 133 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Stop, OnStopCapture) |
| 137 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady, OnReceiveEmptyBuffer) | 134 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady, OnReceiveEmptyBuffer) |
| 138 IPC_MESSAGE_UNHANDLED(handled = false) | 135 IPC_MESSAGE_UNHANDLED(handled = false) |
| 139 IPC_END_MESSAGE_MAP_EX() | 136 IPC_END_MESSAGE_MAP_EX() |
| 140 | 137 |
| 141 return handled; | 138 return handled; |
| 142 } | 139 } |
| 143 | 140 |
| 144 void VideoCaptureHost::OnStartCapture(const IPC::Message& msg, int device_id, | 141 void VideoCaptureHost::OnStartCapture(int device_id, |
| 145 const media::VideoCaptureParams& params) { | 142 const media::VideoCaptureParams& params) { |
| 146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 147 | 144 |
| 148 VideoCaptureControllerID controller_id(msg.routing_id(), device_id); | 145 VideoCaptureControllerID controller_id(device_id); |
| 149 | 146 |
| 150 DCHECK(entries_.find(controller_id) == entries_.end()); | 147 DCHECK(entries_.find(controller_id) == entries_.end()); |
| 151 | 148 |
| 152 scoped_refptr<VideoCaptureController> controller = | 149 scoped_refptr<VideoCaptureController> controller = |
| 153 new VideoCaptureController(controller_id, peer_handle(), this); | 150 new VideoCaptureController(controller_id, peer_handle(), this); |
| 154 entries_.insert(std::make_pair(controller_id, controller)); | 151 entries_.insert(std::make_pair(controller_id, controller)); |
| 155 controller->StartCapture(params); | 152 controller->StartCapture(params); |
| 156 } | 153 } |
| 157 | 154 |
| 158 void VideoCaptureHost::OnStopCapture(const IPC::Message& msg, int device_id) { | 155 void VideoCaptureHost::OnStopCapture(int device_id) { |
| 159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 160 | 157 |
| 161 VideoCaptureControllerID controller_id(msg.routing_id(), device_id); | 158 VideoCaptureControllerID controller_id(device_id); |
| 162 EntryMap::iterator it = entries_.find(controller_id); | 159 EntryMap::iterator it = entries_.find(controller_id); |
| 163 if (it != entries_.end()) { | 160 if (it != entries_.end()) { |
| 164 scoped_refptr<VideoCaptureController> controller = it->second; | 161 scoped_refptr<VideoCaptureController> controller = it->second; |
| 165 controller->StopCapture(NULL); | 162 controller->StopCapture(NULL); |
| 166 } else { | 163 } else { |
| 167 // It does not exist so it must have been stopped already. | 164 // It does not exist so it must have been stopped already. |
| 168 Send(new VideoCaptureMsg_StateChanged(msg.routing_id(), device_id, | 165 Send(new VideoCaptureMsg_StateChanged(device_id, |
| 169 media::VideoCapture::kStopped)); | 166 media::VideoCapture::kStopped)); |
| 170 } | 167 } |
| 171 } | 168 } |
| 172 | 169 |
| 173 void VideoCaptureHost::OnPauseCapture(const IPC::Message& msg, int device_id) { | 170 void VideoCaptureHost::OnPauseCapture(int device_id) { |
| 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 175 // Not used. | 172 // Not used. |
| 176 Send(new VideoCaptureMsg_StateChanged(msg.routing_id(), device_id, | 173 Send(new VideoCaptureMsg_StateChanged(device_id, |
| 177 media::VideoCapture::kError)); | 174 media::VideoCapture::kError)); |
| 178 } | 175 } |
| 179 | 176 |
| 180 void VideoCaptureHost::OnReceiveEmptyBuffer( | 177 void VideoCaptureHost::OnReceiveEmptyBuffer(int device_id, int buffer_id) { |
| 181 const IPC::Message& msg, int device_id, int buffer_id) { | |
| 182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 183 | 179 |
| 184 VideoCaptureControllerID controller_id(msg.routing_id(), device_id); | 180 VideoCaptureControllerID controller_id(device_id); |
| 185 EntryMap::iterator it = entries_.find(controller_id); | 181 EntryMap::iterator it = entries_.find(controller_id); |
| 186 if (it != entries_.end()) { | 182 if (it != entries_.end()) { |
| 187 scoped_refptr<VideoCaptureController> controller = it->second; | 183 scoped_refptr<VideoCaptureController> controller = it->second; |
| 188 controller->ReturnBuffer(buffer_id); | 184 controller->ReturnBuffer(buffer_id); |
| 189 } | 185 } |
| 190 } | 186 } |
| 191 | 187 |
| 192 void VideoCaptureHost::DoDeleteVideoCaptureController( | 188 void VideoCaptureHost::DoDeleteVideoCaptureController( |
| 193 const VideoCaptureControllerID& id) { | 189 const VideoCaptureControllerID& id) { |
| 194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 195 | 191 |
| 196 // Report that the device have successfully been stopped. | 192 // Report that the device have successfully been stopped. |
| 197 Send(new VideoCaptureMsg_StateChanged(id.routing_id, id.device_id, | 193 Send(new VideoCaptureMsg_StateChanged(id.device_id, |
| 198 media::VideoCapture::kStopped)); | 194 media::VideoCapture::kStopped)); |
| 199 entries_.erase(id); | 195 entries_.erase(id); |
| 200 } | 196 } |
| OLD | NEW |