| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/renderer/media/video_capture_impl.h" | 5 #include "content/renderer/media/video_capture_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "content/child/child_process.h" | 9 #include "content/child/child_process.h" |
| 10 #include "content/common/media/video_capture_messages.h" | 10 #include "content/common/media/video_capture_messages.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 const size_t buffer_size; | 25 const size_t buffer_size; |
| 26 | 26 |
| 27 private: | 27 private: |
| 28 friend class base::RefCountedThreadSafe<ClientBuffer>; | 28 friend class base::RefCountedThreadSafe<ClientBuffer>; |
| 29 | 29 |
| 30 virtual ~ClientBuffer() {} | 30 virtual ~ClientBuffer() {} |
| 31 | 31 |
| 32 DISALLOW_COPY_AND_ASSIGN(ClientBuffer); | 32 DISALLOW_COPY_AND_ASSIGN(ClientBuffer); |
| 33 }; | 33 }; |
| 34 | 34 |
| 35 bool VideoCaptureImpl::CaptureStarted() { | 35 VideoCaptureImpl::ClientInfo::ClientInfo() {} |
| 36 return state_ == VIDEO_CAPTURE_STATE_STARTED; | 36 VideoCaptureImpl::ClientInfo::~ClientInfo() {} |
| 37 } | |
| 38 | |
| 39 int VideoCaptureImpl::CaptureFrameRate() { | |
| 40 return last_frame_format_.frame_rate; | |
| 41 } | |
| 42 | 37 |
| 43 VideoCaptureImpl::VideoCaptureImpl( | 38 VideoCaptureImpl::VideoCaptureImpl( |
| 44 const media::VideoCaptureSessionId session_id, | 39 const media::VideoCaptureSessionId session_id, |
| 45 VideoCaptureMessageFilter* filter) | 40 VideoCaptureMessageFilter* filter) |
| 46 : VideoCapture(), | 41 : message_filter_(filter), |
| 47 message_filter_(filter), | |
| 48 io_message_loop_proxy_(ChildProcess::current()->io_message_loop_proxy()), | |
| 49 device_id_(0), | 42 device_id_(0), |
| 50 session_id_(session_id), | 43 session_id_(session_id), |
| 51 suspended_(false), | 44 suspended_(false), |
| 52 state_(VIDEO_CAPTURE_STATE_STOPPED), | 45 state_(VIDEO_CAPTURE_STATE_STOPPED), |
| 53 weak_factory_(this) { | 46 weak_factory_(this) { |
| 54 DCHECK(filter); | 47 DCHECK(filter); |
| 48 thread_checker_.DetachFromThread(); |
| 55 } | 49 } |
| 56 | 50 |
| 57 VideoCaptureImpl::~VideoCaptureImpl() {} | 51 VideoCaptureImpl::~VideoCaptureImpl() { |
| 52 DCHECK(thread_checker_.CalledOnValidThread()); |
| 53 } |
| 58 | 54 |
| 59 void VideoCaptureImpl::Init() { | 55 void VideoCaptureImpl::Init() { |
| 60 io_message_loop_proxy_->PostTask(FROM_HERE, | 56 DCHECK(thread_checker_.CalledOnValidThread()); |
| 61 base::Bind(&VideoCaptureImpl::InitOnIOThread, | 57 message_filter_->AddDelegate(this); |
| 62 base::Unretained(this))); | |
| 63 } | 58 } |
| 64 | 59 |
| 65 void VideoCaptureImpl::DeInit(base::Closure done_cb) { | 60 void VideoCaptureImpl::DeInit() { |
| 66 io_message_loop_proxy_->PostTask(FROM_HERE, | 61 DCHECK(thread_checker_.CalledOnValidThread()); |
| 67 base::Bind(&VideoCaptureImpl::DeInitOnIOThread, | 62 if (state_ == VIDEO_CAPTURE_STATE_STARTED) |
| 68 base::Unretained(this), | 63 Send(new VideoCaptureHostMsg_Stop(device_id_)); |
| 69 done_cb)); | 64 message_filter_->RemoveDelegate(this); |
| 70 } | 65 } |
| 71 | 66 |
| 72 void VideoCaptureImpl::SuspendCapture(bool suspend) { | 67 void VideoCaptureImpl::SuspendCapture(bool suspend) { |
| 73 io_message_loop_proxy_->PostTask(FROM_HERE, | 68 DCHECK(thread_checker_.CalledOnValidThread()); |
| 74 base::Bind(&VideoCaptureImpl::SuspendCaptureOnIOThread, | 69 suspended_ = suspend; |
| 75 base::Unretained(this), | |
| 76 suspend)); | |
| 77 } | 70 } |
| 78 | 71 |
| 79 void VideoCaptureImpl::StartCapture( | 72 void VideoCaptureImpl::StartCapture( |
| 80 media::VideoCapture::EventHandler* handler, | 73 int client_id, |
| 81 const media::VideoCaptureParams& params) { | 74 const media::VideoCaptureParams& params, |
| 82 io_message_loop_proxy_->PostTask(FROM_HERE, | 75 const VideoCaptureStateUpdateCB& state_update_cb, |
| 83 base::Bind(&VideoCaptureImpl::StartCaptureOnIOThread, | 76 const VideoCaptureDeliverFrameCB& deliver_frame_cb) { |
| 84 base::Unretained(this), handler, params)); | 77 DCHECK(thread_checker_.CalledOnValidThread()); |
| 85 } | 78 ClientInfo client_info; |
| 79 client_info.params = params; |
| 80 client_info.state_update_cb = state_update_cb; |
| 81 client_info.deliver_frame_cb = deliver_frame_cb; |
| 86 | 82 |
| 87 void VideoCaptureImpl::StopCapture( | |
| 88 media::VideoCapture::EventHandler* handler) { | |
| 89 io_message_loop_proxy_->PostTask(FROM_HERE, | |
| 90 base::Bind(&VideoCaptureImpl::StopCaptureOnIOThread, | |
| 91 base::Unretained(this), handler)); | |
| 92 } | |
| 93 | |
| 94 void VideoCaptureImpl::GetDeviceSupportedFormats( | |
| 95 const DeviceFormatsCallback& callback) { | |
| 96 DCHECK(!callback.is_null()); | |
| 97 io_message_loop_proxy_->PostTask(FROM_HERE, | |
| 98 base::Bind(&VideoCaptureImpl::GetDeviceSupportedFormatsOnIOThread, | |
| 99 base::Unretained(this), media::BindToCurrentLoop(callback))); | |
| 100 } | |
| 101 | |
| 102 void VideoCaptureImpl::GetDeviceFormatsInUse( | |
| 103 const DeviceFormatsInUseCallback& callback) { | |
| 104 DCHECK(!callback.is_null()); | |
| 105 io_message_loop_proxy_->PostTask(FROM_HERE, | |
| 106 base::Bind(&VideoCaptureImpl::GetDeviceFormatsInUseOnIOThread, | |
| 107 base::Unretained(this), media::BindToCurrentLoop(callback))); | |
| 108 } | |
| 109 | |
| 110 void VideoCaptureImpl::InitOnIOThread() { | |
| 111 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | |
| 112 message_filter_->AddDelegate(this); | |
| 113 } | |
| 114 | |
| 115 void VideoCaptureImpl::DeInitOnIOThread(base::Closure done_cb) { | |
| 116 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | |
| 117 if (state_ == VIDEO_CAPTURE_STATE_STARTED) | |
| 118 Send(new VideoCaptureHostMsg_Stop(device_id_)); | |
| 119 message_filter_->RemoveDelegate(this); | |
| 120 done_cb.Run(); | |
| 121 } | |
| 122 | |
| 123 void VideoCaptureImpl::SuspendCaptureOnIOThread(bool suspend) { | |
| 124 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | |
| 125 suspended_ = suspend; | |
| 126 } | |
| 127 | |
| 128 void VideoCaptureImpl::StartCaptureOnIOThread( | |
| 129 media::VideoCapture::EventHandler* handler, | |
| 130 const media::VideoCaptureParams& params) { | |
| 131 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | |
| 132 if (state_ == VIDEO_CAPTURE_STATE_ERROR) { | 83 if (state_ == VIDEO_CAPTURE_STATE_ERROR) { |
| 133 handler->OnError(this, 1); | 84 state_update_cb.Run(VIDEO_CAPTURE_STATE_ERROR); |
| 134 handler->OnRemoved(this); | 85 } else if (clients_pending_on_filter_.count(client_id) || |
| 135 } else if ((clients_pending_on_filter_.find(handler) != | 86 clients_pending_on_restart_.count(client_id) || |
| 136 clients_pending_on_filter_.end()) || | 87 clients_.count(client_id)) { |
| 137 (clients_pending_on_restart_.find(handler) != | 88 LOG(FATAL) << "This client has already started."; |
| 138 clients_pending_on_restart_.end()) || | |
| 139 clients_.find(handler) != clients_.end() ) { | |
| 140 // This client has started. | |
| 141 } else if (!device_id_) { | 89 } else if (!device_id_) { |
| 142 clients_pending_on_filter_[handler] = params; | 90 clients_pending_on_filter_[client_id] = client_info; |
| 143 } else { | 91 } else { |
| 144 handler->OnStarted(this); | 92 // Note: |state_| might not be started at this point. But we tell |
| 93 // client that we have started. |
| 94 state_update_cb.Run(VIDEO_CAPTURE_STATE_STARTED); |
| 145 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { | 95 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { |
| 146 clients_[handler] = params; | 96 clients_[client_id] = client_info; |
| 147 // TODO(sheu): Allowing resolution change will require that all | 97 // TODO(sheu): Allowing resolution change will require that all |
| 148 // outstanding clients of a capture session support resolution change. | 98 // outstanding clients of a capture session support resolution change. |
| 149 DCHECK_EQ(params_.allow_resolution_change, | 99 DCHECK_EQ(params_.allow_resolution_change, |
| 150 params.allow_resolution_change); | 100 params.allow_resolution_change); |
| 151 } else if (state_ == VIDEO_CAPTURE_STATE_STOPPING) { | 101 } else if (state_ == VIDEO_CAPTURE_STATE_STOPPING) { |
| 152 clients_pending_on_restart_[handler] = params; | 102 clients_pending_on_restart_[client_id] = client_info; |
| 153 DVLOG(1) << "StartCapture: Got new resolution " | 103 DVLOG(1) << "StartCapture: Got new resolution " |
| 154 << params.requested_format.frame_size.ToString() | 104 << params.requested_format.frame_size.ToString() |
| 155 << " during stopping."; | 105 << " during stopping."; |
| 156 } else { | 106 } else { |
| 157 clients_[handler] = params; | 107 clients_[client_id] = client_info; |
| 158 DCHECK_EQ(1ul, clients_.size()); | 108 if (state_ == VIDEO_CAPTURE_STATE_STARTED) |
| 109 return; |
| 159 params_ = params; | 110 params_ = params; |
| 160 if (params_.requested_format.frame_rate > | 111 if (params_.requested_format.frame_rate > |
| 161 media::limits::kMaxFramesPerSecond) { | 112 media::limits::kMaxFramesPerSecond) { |
| 162 params_.requested_format.frame_rate = | 113 params_.requested_format.frame_rate = |
| 163 media::limits::kMaxFramesPerSecond; | 114 media::limits::kMaxFramesPerSecond; |
| 164 } | 115 } |
| 165 DVLOG(1) << "StartCapture: starting with first resolution " | 116 DVLOG(1) << "StartCapture: starting with first resolution " |
| 166 << params_.requested_format.frame_size.ToString(); | 117 << params_.requested_format.frame_size.ToString(); |
| 167 first_frame_timestamp_ = base::TimeTicks(); | 118 first_frame_timestamp_ = base::TimeTicks(); |
| 168 StartCaptureInternal(); | 119 StartCaptureInternal(); |
| 169 } | 120 } |
| 170 } | 121 } |
| 171 } | 122 } |
| 172 | 123 |
| 173 void VideoCaptureImpl::StopCaptureOnIOThread( | 124 void VideoCaptureImpl::StopCapture(int client_id) { |
| 174 media::VideoCapture::EventHandler* handler) { | 125 DCHECK(thread_checker_.CalledOnValidThread()); |
| 175 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | |
| 176 | 126 |
| 177 // A handler can be in only one client list. | 127 // A client ID can be in only one client list. |
| 178 // If this handler is in any client list, we can just remove it from | 128 // If this ID is in any client list, we can just remove it from |
| 179 // that client list and don't have to run the other following RemoveClient(). | 129 // that client list and don't have to run the other following RemoveClient(). |
| 180 RemoveClient(handler, &clients_pending_on_filter_) || | 130 if (!RemoveClient(client_id, &clients_pending_on_filter_)) { |
| 181 RemoveClient(handler, &clients_pending_on_restart_) || | 131 if (!RemoveClient(client_id, &clients_pending_on_restart_)) { |
| 182 RemoveClient(handler, &clients_); | 132 bool removed = RemoveClient(client_id, &clients_); |
| 133 DCHECK(removed) << "Removing a non-existent client."; |
| 134 } |
| 135 } |
| 183 | 136 |
| 184 if (clients_.empty()) { | 137 if (clients_.empty()) { |
| 185 DVLOG(1) << "StopCapture: No more client, stopping ..."; | 138 DVLOG(1) << "StopCapture: No more client, stopping ..."; |
| 186 StopDevice(); | 139 StopDevice(); |
| 187 client_buffers_.clear(); | 140 client_buffers_.clear(); |
| 188 weak_factory_.InvalidateWeakPtrs(); | 141 weak_factory_.InvalidateWeakPtrs(); |
| 189 } | 142 } |
| 190 } | 143 } |
| 191 | 144 |
| 192 void VideoCaptureImpl::GetDeviceSupportedFormatsOnIOThread( | 145 void VideoCaptureImpl::GetDeviceSupportedFormats( |
| 193 const DeviceFormatsCallback& callback) { | 146 const VideoCaptureDeviceFormatsCB& callback) { |
| 194 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 147 DCHECK(thread_checker_.CalledOnValidThread()); |
| 195 device_formats_callback_queue_.push_back(callback); | 148 device_formats_cb_queue_.push_back(callback); |
| 196 if (device_formats_callback_queue_.size() == 1) | 149 if (device_formats_cb_queue_.size() == 1) |
| 197 Send(new VideoCaptureHostMsg_GetDeviceSupportedFormats(device_id_, | 150 Send(new VideoCaptureHostMsg_GetDeviceSupportedFormats(device_id_, |
| 198 session_id_)); | 151 session_id_)); |
| 199 } | 152 } |
| 200 | 153 |
| 201 void VideoCaptureImpl::GetDeviceFormatsInUseOnIOThread( | 154 void VideoCaptureImpl::GetDeviceFormatsInUse( |
| 202 const DeviceFormatsInUseCallback& callback) { | 155 const VideoCaptureDeviceFormatsCB& callback) { |
| 203 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 156 DCHECK(thread_checker_.CalledOnValidThread()); |
| 204 device_formats_in_use_callback_queue_.push_back(callback); | 157 device_formats_in_use_cb_queue_.push_back(callback); |
| 205 if (device_formats_in_use_callback_queue_.size() == 1) | 158 if (device_formats_in_use_cb_queue_.size() == 1) |
| 206 Send( | 159 Send( |
| 207 new VideoCaptureHostMsg_GetDeviceFormatsInUse(device_id_, session_id_)); | 160 new VideoCaptureHostMsg_GetDeviceFormatsInUse(device_id_, session_id_)); |
| 208 } | 161 } |
| 209 | 162 |
| 210 void VideoCaptureImpl::OnBufferCreated( | 163 void VideoCaptureImpl::OnBufferCreated( |
| 211 base::SharedMemoryHandle handle, | 164 base::SharedMemoryHandle handle, |
| 212 int length, int buffer_id) { | 165 int length, int buffer_id) { |
| 213 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 166 DCHECK(thread_checker_.CalledOnValidThread()); |
| 214 | 167 |
| 215 // In case client calls StopCapture before the arrival of created buffer, | 168 // In case client calls StopCapture before the arrival of created buffer, |
| 216 // just close this buffer and return. | 169 // just close this buffer and return. |
| 217 if (state_ != VIDEO_CAPTURE_STATE_STARTED) { | 170 if (state_ != VIDEO_CAPTURE_STATE_STARTED) { |
| 218 base::SharedMemory::CloseHandle(handle); | 171 base::SharedMemory::CloseHandle(handle); |
| 219 return; | 172 return; |
| 220 } | 173 } |
| 221 | 174 |
| 222 scoped_ptr<base::SharedMemory> shm(new base::SharedMemory(handle, false)); | 175 scoped_ptr<base::SharedMemory> shm(new base::SharedMemory(handle, false)); |
| 223 if (!shm->Map(length)) { | 176 if (!shm->Map(length)) { |
| 224 DLOG(ERROR) << "OnBufferCreated: Map failed."; | 177 DLOG(ERROR) << "OnBufferCreated: Map failed."; |
| 225 return; | 178 return; |
| 226 } | 179 } |
| 227 | 180 |
| 228 bool inserted = | 181 bool inserted = |
| 229 client_buffers_.insert(std::make_pair( | 182 client_buffers_.insert(std::make_pair( |
| 230 buffer_id, | 183 buffer_id, |
| 231 new ClientBuffer(shm.Pass(), | 184 new ClientBuffer(shm.Pass(), |
| 232 length))).second; | 185 length))).second; |
| 233 DCHECK(inserted); | 186 DCHECK(inserted); |
| 234 } | 187 } |
| 235 | 188 |
| 236 void VideoCaptureImpl::OnBufferDestroyed(int buffer_id) { | 189 void VideoCaptureImpl::OnBufferDestroyed(int buffer_id) { |
| 237 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 190 DCHECK(thread_checker_.CalledOnValidThread()); |
| 238 | 191 |
| 239 ClientBufferMap::iterator iter = client_buffers_.find(buffer_id); | 192 ClientBufferMap::iterator iter = client_buffers_.find(buffer_id); |
| 240 if (iter == client_buffers_.end()) | 193 if (iter == client_buffers_.end()) |
| 241 return; | 194 return; |
| 242 | 195 |
| 243 DCHECK(!iter->second || iter->second->HasOneRef()) | 196 DCHECK(!iter->second || iter->second->HasOneRef()) |
| 244 << "Instructed to delete buffer we are still using."; | 197 << "Instructed to delete buffer we are still using."; |
| 245 client_buffers_.erase(iter); | 198 client_buffers_.erase(iter); |
| 246 } | 199 } |
| 247 | 200 |
| 248 void VideoCaptureImpl::OnBufferReceived(int buffer_id, | 201 void VideoCaptureImpl::OnBufferReceived(int buffer_id, |
| 249 const media::VideoCaptureFormat& format, | 202 const media::VideoCaptureFormat& format, |
| 250 base::TimeTicks timestamp) { | 203 base::TimeTicks timestamp) { |
| 251 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 204 DCHECK(thread_checker_.CalledOnValidThread()); |
| 252 | 205 |
| 253 // The capture pipeline supports only I420 for now. | 206 // The capture pipeline supports only I420 for now. |
| 254 DCHECK_EQ(format.pixel_format, media::PIXEL_FORMAT_I420); | 207 DCHECK_EQ(format.pixel_format, media::PIXEL_FORMAT_I420); |
| 255 | 208 |
| 256 if (state_ != VIDEO_CAPTURE_STATE_STARTED || suspended_) { | 209 if (state_ != VIDEO_CAPTURE_STATE_STARTED || suspended_) { |
| 257 Send(new VideoCaptureHostMsg_BufferReady(device_id_, buffer_id, 0)); | 210 Send(new VideoCaptureHostMsg_BufferReady(device_id_, buffer_id, 0)); |
| 258 return; | 211 return; |
| 259 } | 212 } |
| 260 | 213 |
| 261 last_frame_format_ = format; | 214 last_frame_format_ = format; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 282 buffer->buffer_size, | 235 buffer->buffer_size, |
| 283 buffer->buffer->handle(), | 236 buffer->buffer->handle(), |
| 284 timestamp - first_frame_timestamp_, | 237 timestamp - first_frame_timestamp_, |
| 285 media::BindToCurrentLoop(base::Bind( | 238 media::BindToCurrentLoop(base::Bind( |
| 286 &VideoCaptureImpl::OnClientBufferFinished, | 239 &VideoCaptureImpl::OnClientBufferFinished, |
| 287 weak_factory_.GetWeakPtr(), | 240 weak_factory_.GetWeakPtr(), |
| 288 buffer_id, | 241 buffer_id, |
| 289 buffer, | 242 buffer, |
| 290 base::Passed(scoped_ptr<gpu::MailboxHolder>().Pass())))); | 243 base::Passed(scoped_ptr<gpu::MailboxHolder>().Pass())))); |
| 291 | 244 |
| 292 for (ClientInfo::iterator it = clients_.begin(); it != clients_.end(); ++it) | 245 for (ClientInfoMap::iterator it = clients_.begin(); it != clients_.end(); |
| 293 it->first->OnFrameReady(this, frame); | 246 ++it) { |
| 247 it->second.deliver_frame_cb.Run(frame, format, timestamp); |
| 248 } |
| 294 } | 249 } |
| 295 | 250 |
| 296 static void NullReadPixelsCB(const SkBitmap& bitmap) { NOTIMPLEMENTED(); } | 251 static void NullReadPixelsCB(const SkBitmap& bitmap) { NOTIMPLEMENTED(); } |
| 297 | 252 |
| 298 void VideoCaptureImpl::OnMailboxBufferReceived( | 253 void VideoCaptureImpl::OnMailboxBufferReceived( |
| 299 int buffer_id, | 254 int buffer_id, |
| 300 const gpu::MailboxHolder& mailbox_holder, | 255 const gpu::MailboxHolder& mailbox_holder, |
| 301 const media::VideoCaptureFormat& format, | 256 const media::VideoCaptureFormat& format, |
| 302 base::TimeTicks timestamp) { | 257 base::TimeTicks timestamp) { |
| 303 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 258 DCHECK(thread_checker_.CalledOnValidThread()); |
| 304 | 259 |
| 305 if (state_ != VIDEO_CAPTURE_STATE_STARTED || suspended_) { | 260 if (state_ != VIDEO_CAPTURE_STATE_STARTED || suspended_) { |
| 306 Send(new VideoCaptureHostMsg_BufferReady( | 261 Send(new VideoCaptureHostMsg_BufferReady( |
| 307 device_id_, buffer_id, mailbox_holder.sync_point)); | 262 device_id_, buffer_id, mailbox_holder.sync_point)); |
| 308 return; | 263 return; |
| 309 } | 264 } |
| 310 | 265 |
| 311 last_frame_format_ = format; | 266 last_frame_format_ = format; |
| 312 if (first_frame_timestamp_.is_null()) | 267 if (first_frame_timestamp_.is_null()) |
| 313 first_frame_timestamp_ = timestamp; | 268 first_frame_timestamp_ = timestamp; |
| 314 | 269 |
| 315 scoped_refptr<media::VideoFrame> frame = media::VideoFrame::WrapNativeTexture( | 270 scoped_refptr<media::VideoFrame> frame = media::VideoFrame::WrapNativeTexture( |
| 316 make_scoped_ptr(new gpu::MailboxHolder(mailbox_holder)), | 271 make_scoped_ptr(new gpu::MailboxHolder(mailbox_holder)), |
| 317 media::BindToCurrentLoop( | 272 media::BindToCurrentLoop( |
| 318 base::Bind(&VideoCaptureImpl::OnClientBufferFinished, | 273 base::Bind(&VideoCaptureImpl::OnClientBufferFinished, |
| 319 weak_factory_.GetWeakPtr(), | 274 weak_factory_.GetWeakPtr(), |
| 320 buffer_id, | 275 buffer_id, |
| 321 scoped_refptr<ClientBuffer>())), | 276 scoped_refptr<ClientBuffer>())), |
| 322 last_frame_format_.frame_size, | 277 last_frame_format_.frame_size, |
| 323 gfx::Rect(last_frame_format_.frame_size), | 278 gfx::Rect(last_frame_format_.frame_size), |
| 324 last_frame_format_.frame_size, | 279 last_frame_format_.frame_size, |
| 325 timestamp - first_frame_timestamp_, | 280 timestamp - first_frame_timestamp_, |
| 326 base::Bind(&NullReadPixelsCB)); | 281 base::Bind(&NullReadPixelsCB)); |
| 327 | 282 |
| 328 for (ClientInfo::iterator it = clients_.begin(); it != clients_.end(); ++it) | 283 for (ClientInfoMap::iterator it = clients_.begin(); it != clients_.end(); |
| 329 it->first->OnFrameReady(this, frame); | 284 ++it) { |
| 285 it->second.deliver_frame_cb.Run(frame, format, timestamp); |
| 286 } |
| 330 } | 287 } |
| 331 | 288 |
| 332 void VideoCaptureImpl::OnClientBufferFinished( | 289 void VideoCaptureImpl::OnClientBufferFinished( |
| 333 int buffer_id, | 290 int buffer_id, |
| 334 const scoped_refptr<ClientBuffer>& /* ignored_buffer */, | 291 const scoped_refptr<ClientBuffer>& /* ignored_buffer */, |
| 335 scoped_ptr<gpu::MailboxHolder> mailbox_holder) { | 292 scoped_ptr<gpu::MailboxHolder> mailbox_holder) { |
| 336 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 293 DCHECK(thread_checker_.CalledOnValidThread()); |
| 337 const uint32 sync_point = (mailbox_holder ? mailbox_holder->sync_point : 0); | 294 const uint32 sync_point = (mailbox_holder ? mailbox_holder->sync_point : 0); |
| 338 Send(new VideoCaptureHostMsg_BufferReady(device_id_, buffer_id, sync_point)); | 295 Send(new VideoCaptureHostMsg_BufferReady(device_id_, buffer_id, sync_point)); |
| 339 } | 296 } |
| 340 | 297 |
| 341 void VideoCaptureImpl::OnStateChanged(VideoCaptureState state) { | 298 void VideoCaptureImpl::OnStateChanged(VideoCaptureState state) { |
| 342 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 299 DCHECK(thread_checker_.CalledOnValidThread()); |
| 343 | 300 |
| 344 switch (state) { | 301 switch (state) { |
| 345 case VIDEO_CAPTURE_STATE_STARTED: | 302 case VIDEO_CAPTURE_STATE_STARTED: |
| 303 // Camera has started in the browser process. Since we have already |
| 304 // told all clients that we have started there's nothing to do. |
| 346 break; | 305 break; |
| 347 case VIDEO_CAPTURE_STATE_STOPPED: | 306 case VIDEO_CAPTURE_STATE_STOPPED: |
| 348 state_ = VIDEO_CAPTURE_STATE_STOPPED; | 307 state_ = VIDEO_CAPTURE_STATE_STOPPED; |
| 349 DVLOG(1) << "OnStateChanged: stopped!, device_id = " << device_id_; | 308 DVLOG(1) << "OnStateChanged: stopped!, device_id = " << device_id_; |
| 350 client_buffers_.clear(); | 309 client_buffers_.clear(); |
| 351 weak_factory_.InvalidateWeakPtrs(); | 310 weak_factory_.InvalidateWeakPtrs(); |
| 352 if (!clients_.empty() || !clients_pending_on_restart_.empty()) | 311 if (!clients_.empty() || !clients_pending_on_restart_.empty()) |
| 353 RestartCapture(); | 312 RestartCapture(); |
| 354 break; | 313 break; |
| 355 case VIDEO_CAPTURE_STATE_PAUSED: | 314 case VIDEO_CAPTURE_STATE_PAUSED: |
| 356 for (ClientInfo::iterator it = clients_.begin(); | 315 for (ClientInfoMap::iterator it = clients_.begin(); |
| 357 it != clients_.end(); ++it) { | 316 it != clients_.end(); ++it) { |
| 358 it->first->OnPaused(this); | 317 it->second.state_update_cb.Run(VIDEO_CAPTURE_STATE_PAUSED); |
| 359 } | 318 } |
| 360 break; | 319 break; |
| 361 case VIDEO_CAPTURE_STATE_ERROR: | 320 case VIDEO_CAPTURE_STATE_ERROR: |
| 362 DVLOG(1) << "OnStateChanged: error!, device_id = " << device_id_; | 321 DVLOG(1) << "OnStateChanged: error!, device_id = " << device_id_; |
| 363 for (ClientInfo::iterator it = clients_.begin(); | 322 for (ClientInfoMap::iterator it = clients_.begin(); |
| 364 it != clients_.end(); ++it) { | 323 it != clients_.end(); ++it) { |
| 365 // TODO(wjia): browser process would send error code. | 324 it->second.state_update_cb.Run(VIDEO_CAPTURE_STATE_ERROR); |
| 366 it->first->OnError(this, 1); | |
| 367 it->first->OnRemoved(this); | |
| 368 } | 325 } |
| 369 clients_.clear(); | 326 clients_.clear(); |
| 370 state_ = VIDEO_CAPTURE_STATE_ERROR; | 327 state_ = VIDEO_CAPTURE_STATE_ERROR; |
| 371 break; | 328 break; |
| 372 case VIDEO_CAPTURE_STATE_ENDED: | 329 case VIDEO_CAPTURE_STATE_ENDED: |
| 373 DVLOG(1) << "OnStateChanged: ended!, device_id = " << device_id_; | 330 DVLOG(1) << "OnStateChanged: ended!, device_id = " << device_id_; |
| 374 for (ClientInfo::iterator it = clients_.begin(); | 331 for (ClientInfoMap::iterator it = clients_.begin(); |
| 375 it != clients_.end(); ++it) { | 332 it != clients_.end(); ++it) { |
| 376 it->first->OnRemoved(this); | 333 // We'll only notify the client that the stream has stopped. |
| 334 it->second.state_update_cb.Run(VIDEO_CAPTURE_STATE_STOPPED); |
| 377 } | 335 } |
| 378 clients_.clear(); | 336 clients_.clear(); |
| 379 state_ = VIDEO_CAPTURE_STATE_ENDED; | 337 state_ = VIDEO_CAPTURE_STATE_ENDED; |
| 380 break; | 338 break; |
| 381 default: | 339 default: |
| 382 break; | 340 break; |
| 383 } | 341 } |
| 384 } | 342 } |
| 385 | 343 |
| 386 void VideoCaptureImpl::OnDeviceSupportedFormatsEnumerated( | 344 void VideoCaptureImpl::OnDeviceSupportedFormatsEnumerated( |
| 387 const media::VideoCaptureFormats& supported_formats) { | 345 const media::VideoCaptureFormats& supported_formats) { |
| 388 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 346 DCHECK(thread_checker_.CalledOnValidThread()); |
| 389 for (size_t i = 0; i < device_formats_callback_queue_.size(); ++i) | 347 for (size_t i = 0; i < device_formats_cb_queue_.size(); ++i) |
| 390 device_formats_callback_queue_[i].Run(supported_formats); | 348 device_formats_cb_queue_[i].Run(supported_formats); |
| 391 device_formats_callback_queue_.clear(); | 349 device_formats_cb_queue_.clear(); |
| 392 } | 350 } |
| 393 | 351 |
| 394 void VideoCaptureImpl::OnDeviceFormatsInUseReceived( | 352 void VideoCaptureImpl::OnDeviceFormatsInUseReceived( |
| 395 const media::VideoCaptureFormats& formats_in_use) { | 353 const media::VideoCaptureFormats& formats_in_use) { |
| 396 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 354 DCHECK(thread_checker_.CalledOnValidThread()); |
| 397 for (size_t i = 0; i < device_formats_in_use_callback_queue_.size(); ++i) | 355 for (size_t i = 0; i < device_formats_in_use_cb_queue_.size(); ++i) |
| 398 device_formats_in_use_callback_queue_[i].Run(formats_in_use); | 356 device_formats_in_use_cb_queue_[i].Run(formats_in_use); |
| 399 device_formats_in_use_callback_queue_.clear(); | 357 device_formats_in_use_cb_queue_.clear(); |
| 400 } | 358 } |
| 401 | 359 |
| 402 void VideoCaptureImpl::OnDelegateAdded(int32 device_id) { | 360 void VideoCaptureImpl::OnDelegateAdded(int32 device_id) { |
| 361 DCHECK(thread_checker_.CalledOnValidThread()); |
| 403 DVLOG(1) << "OnDelegateAdded: device_id " << device_id; | 362 DVLOG(1) << "OnDelegateAdded: device_id " << device_id; |
| 404 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | |
| 405 | 363 |
| 406 device_id_ = device_id; | 364 device_id_ = device_id; |
| 407 for (ClientInfo::iterator it = clients_pending_on_filter_.begin(); | 365 for (ClientInfoMap::iterator it = clients_pending_on_filter_.begin(); |
| 408 it != clients_pending_on_filter_.end(); ) { | 366 it != clients_pending_on_filter_.end(); ) { |
| 409 media::VideoCapture::EventHandler* handler = it->first; | 367 int client_id = it->first; |
| 410 const media::VideoCaptureParams params = it->second; | 368 VideoCaptureStateUpdateCB state_update_cb = |
| 369 it->second.state_update_cb; |
| 370 VideoCaptureDeliverFrameCB deliver_frame_cb = |
| 371 it->second.deliver_frame_cb; |
| 372 const media::VideoCaptureParams params = it->second.params; |
| 411 clients_pending_on_filter_.erase(it++); | 373 clients_pending_on_filter_.erase(it++); |
| 412 StartCapture(handler, params); | 374 StartCapture(client_id, params, state_update_cb, |
| 375 deliver_frame_cb); |
| 413 } | 376 } |
| 414 } | 377 } |
| 415 | 378 |
| 416 void VideoCaptureImpl::StopDevice() { | 379 void VideoCaptureImpl::StopDevice() { |
| 417 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 380 DCHECK(thread_checker_.CalledOnValidThread()); |
| 418 | 381 |
| 419 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { | 382 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { |
| 420 state_ = VIDEO_CAPTURE_STATE_STOPPING; | 383 state_ = VIDEO_CAPTURE_STATE_STOPPING; |
| 421 Send(new VideoCaptureHostMsg_Stop(device_id_)); | 384 Send(new VideoCaptureHostMsg_Stop(device_id_)); |
| 422 params_.requested_format.frame_size.SetSize(0, 0); | 385 params_.requested_format.frame_size.SetSize(0, 0); |
| 423 } | 386 } |
| 424 } | 387 } |
| 425 | 388 |
| 426 void VideoCaptureImpl::RestartCapture() { | 389 void VideoCaptureImpl::RestartCapture() { |
| 427 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 390 DCHECK(thread_checker_.CalledOnValidThread()); |
| 428 DCHECK_EQ(state_, VIDEO_CAPTURE_STATE_STOPPED); | 391 DCHECK_EQ(state_, VIDEO_CAPTURE_STATE_STOPPED); |
| 429 | 392 |
| 430 int width = 0; | 393 int width = 0; |
| 431 int height = 0; | 394 int height = 0; |
| 432 for (ClientInfo::iterator it = clients_.begin(); | 395 clients_.insert(clients_pending_on_restart_.begin(), |
| 396 clients_pending_on_restart_.end()); |
| 397 clients_pending_on_restart_.clear(); |
| 398 for (ClientInfoMap::iterator it = clients_.begin(); |
| 433 it != clients_.end(); ++it) { | 399 it != clients_.end(); ++it) { |
| 434 width = std::max(width, it->second.requested_format.frame_size.width()); | 400 width = std::max(width, |
| 435 height = std::max(height, it->second.requested_format.frame_size.height()); | 401 it->second.params.requested_format.frame_size.width()); |
| 436 } | 402 height = std::max(height, |
| 437 for (ClientInfo::iterator it = clients_pending_on_restart_.begin(); | 403 it->second.params.requested_format.frame_size.height()); |
| 438 it != clients_pending_on_restart_.end(); ) { | |
| 439 width = std::max(width, it->second.requested_format.frame_size.width()); | |
| 440 height = std::max(height, it->second.requested_format.frame_size.height()); | |
| 441 clients_[it->first] = it->second; | |
| 442 clients_pending_on_restart_.erase(it++); | |
| 443 } | 404 } |
| 444 params_.requested_format.frame_size.SetSize(width, height); | 405 params_.requested_format.frame_size.SetSize(width, height); |
| 445 DVLOG(1) << "RestartCapture, " | 406 DVLOG(1) << "RestartCapture, " |
| 446 << params_.requested_format.frame_size.ToString(); | 407 << params_.requested_format.frame_size.ToString(); |
| 447 StartCaptureInternal(); | 408 StartCaptureInternal(); |
| 448 } | 409 } |
| 449 | 410 |
| 450 void VideoCaptureImpl::StartCaptureInternal() { | 411 void VideoCaptureImpl::StartCaptureInternal() { |
| 451 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 412 DCHECK(thread_checker_.CalledOnValidThread()); |
| 452 DCHECK(device_id_); | 413 DCHECK(device_id_); |
| 453 | 414 |
| 454 Send(new VideoCaptureHostMsg_Start(device_id_, session_id_, params_)); | 415 Send(new VideoCaptureHostMsg_Start(device_id_, session_id_, params_)); |
| 455 state_ = VIDEO_CAPTURE_STATE_STARTED; | 416 state_ = VIDEO_CAPTURE_STATE_STARTED; |
| 456 } | 417 } |
| 457 | 418 |
| 458 void VideoCaptureImpl::Send(IPC::Message* message) { | 419 void VideoCaptureImpl::Send(IPC::Message* message) { |
| 459 io_message_loop_proxy_->PostTask(FROM_HERE, | 420 DCHECK(thread_checker_.CalledOnValidThread()); |
| 460 base::Bind(base::IgnoreResult(&VideoCaptureMessageFilter::Send), | 421 message_filter_->Send(message); |
| 461 message_filter_.get(), message)); | |
| 462 } | 422 } |
| 463 | 423 |
| 464 bool VideoCaptureImpl::RemoveClient( | 424 bool VideoCaptureImpl::RemoveClient(int client_id, ClientInfoMap* clients) { |
| 465 media::VideoCapture::EventHandler* handler, | 425 DCHECK(thread_checker_.CalledOnValidThread()); |
| 466 ClientInfo* clients) { | |
| 467 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | |
| 468 bool found = false; | 426 bool found = false; |
| 469 | 427 |
| 470 ClientInfo::iterator it = clients->find(handler); | 428 ClientInfoMap::iterator it = clients->find(client_id); |
| 471 if (it != clients->end()) { | 429 if (it != clients->end()) { |
| 472 handler->OnStopped(this); | 430 it->second.state_update_cb.Run(VIDEO_CAPTURE_STATE_STOPPED); |
| 473 handler->OnRemoved(this); | |
| 474 clients->erase(it); | 431 clients->erase(it); |
| 475 found = true; | 432 found = true; |
| 476 } | 433 } |
| 477 return found; | 434 return found; |
| 478 } | 435 } |
| 479 | 436 |
| 437 |
| 480 } // namespace content | 438 } // namespace content |
| OLD | NEW |