Chromium Code Reviews| Index: content/browser/renderer_host/video_capture_host.cc |
| =================================================================== |
| --- content/browser/renderer_host/video_capture_host.cc (revision 0) |
| +++ content/browser/renderer_host/video_capture_host.cc (revision 0) |
| @@ -0,0 +1,191 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/renderer_host/video_capture_host.h" |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/stl_util-inl.h" |
| +#include "content/common/video_capture_messages.h" |
| + |
| +VideoCaptureHost::VideoCaptureHost() {} |
| + |
| +VideoCaptureHost::~VideoCaptureHost() { |
| + // Delete all Memory handlers. The map is not empty if the |
| + // VideoCaptureMessageFilter have not returned all |
| + // TransportDIBs. |
| + entries_.clear(); |
|
wjia(left Chromium)
2011/05/19 04:24:37
the dtor of entries_ will do clear() by default.
Per K
2011/05/19 08:55:31
Removed.
|
| +} |
| + |
| +void VideoCaptureHost::OnChannelClosing() { |
| + BrowserMessageFilter::OnChannelClosing(); |
| + |
| + // Since the IPC channel is gone, close all requested VideCaptureDevices. |
| + for (EntryMap::iterator it = entries_.begin(); it != entries_.end(); it++) { |
| + VideoCaptureMemory* memory_handler = it->second; |
| + // Since the channel is closing we need a task to make sure VideoCaptureHost |
| + // is not deleted before VideoCaptureMemory. |
| + memory_handler->StopCapture( |
| + NewRunnableMethod(this, &VideoCaptureHost::OnReadyToDelete, it->first)); |
| + } |
| +} |
| + |
| +void VideoCaptureHost::OnDestruct() const { |
| + BrowserThread::DeleteOnIOThread::Destruct(this); |
| +} |
| + |
| +/////////////////////////////////////////////////////////////////////////////// |
| + |
| +// Implements VideoCaptureMemory::EventHandler. |
| +void VideoCaptureHost::OnError(VideoCaptureMemory::VideoCaptureMemoryId id) { |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + NewRunnableMethod(this, &VideoCaptureHost::DoHandleError, id.first, |
| + id.second)); |
| +} |
| + |
| +void VideoCaptureHost::OnBufferReady( |
| + VideoCaptureMemory::VideoCaptureMemoryId id, |
| + TransportDIB::Handle handle, |
| + base::Time timestamp) { |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + NewRunnableMethod(this, &VideoCaptureHost::DoSendFilledBuffer, id.first, |
| + id.second, handle, timestamp)); |
| +} |
| + |
| +void VideoCaptureHost::OnFrameInfo(VideoCaptureMemory::VideoCaptureMemoryId id, |
| + int width, |
| + int height, |
| + int frame_per_second) { |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + NewRunnableMethod(this, &VideoCaptureHost::DoSendFrameInfo, id.first, |
| + id.second, width, height, frame_per_second)); |
| +} |
| + |
| +void VideoCaptureHost::OnReadyToDelete( |
| + VideoCaptureMemory::VideoCaptureMemoryId id) { |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + NewRunnableMethod(this, &VideoCaptureHost::DoDeleteVideoCaptureMemory, |
| + id)); |
| +} |
| + |
| +void VideoCaptureHost::DoSendFilledBuffer(int32 routing_id, |
| + int device_id, |
| + TransportDIB::Handle handle, |
| + base::Time timestamp) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + Send(new VideoCaptureMsg_BufferReady(routing_id, device_id, handle, |
| + timestamp)); |
| +} |
| + |
| +void VideoCaptureHost::DoHandleError(int32 routing_id, int device_id) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + Send(new VideoCaptureMsg_StateChanged(routing_id, device_id, |
| + media::VideoCapture::kError)); |
| + |
| + VideoCaptureMemory::VideoCaptureMemoryId id(routing_id, device_id); |
| + EntryMap::iterator it = entries_.find(id); |
| + if (it != entries_.end()) { |
| + VideoCaptureMemory* memory_handler = it->second; |
| + memory_handler->StopCapture(NULL); |
| + } |
| +} |
| + |
| +void VideoCaptureHost::DoSendFrameInfo(int32 routing_id, |
| + int device_id, |
| + int width, |
| + int height, |
| + int frame_per_second) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + media::VideoCaptureParams params; |
| + params.width = width; |
| + params.height = height; |
| + params.frame_per_second = frame_per_second; |
| + Send(new VideoCaptureMsg_DeviceInfo(routing_id, device_id, params)); |
| + Send(new VideoCaptureMsg_StateChanged(routing_id, device_id, |
| + media::VideoCapture::kStarted)); |
| +} |
| + |
| +/////////////////////////////////////////////////////////////////////////////// |
| +// IPC Messages handler. |
| +bool VideoCaptureHost::OnMessageReceived(const IPC::Message& message, |
| + bool* message_was_ok) { |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP_EX(VideoCaptureHost, message, *message_was_ok) |
| + IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Start, OnStartCapture) |
| + IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Pause, OnPauseCapture) |
| + IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Stop, OnStopCapture) |
| + IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady, OnReceiveEmptyBuffer) |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP_EX() |
| + |
| + return handled; |
| +} |
| + |
| +void VideoCaptureHost::OnStartCapture(const IPC::Message& msg, int device_id, |
| + const media::VideoCaptureParams& params) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + VideoCaptureMemory::VideoCaptureMemoryId memory_id(msg.routing_id(), |
| + device_id); |
| + |
| + DCHECK(entries_.find(memory_id) == entries_.end()); |
| + |
| + scoped_refptr<VideoCaptureMemory> memory_handler = |
| + new VideoCaptureMemory(memory_id, this); |
|
wjia(left Chromium)
2011/05/19 04:24:37
does Memory have to know memory_id? I think it's r
Per K
2011/05/19 08:55:31
True- but this way we don't need to iterate throug
|
| + entries_.insert(std::make_pair(memory_id, memory_handler)); |
| + memory_handler->StartCapture(params); |
| +} |
| + |
| +void VideoCaptureHost::OnStopCapture(const IPC::Message& msg, int device_id) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + VideoCaptureMemory::VideoCaptureMemoryId memory_id(msg.routing_id(), |
| + device_id); |
| + EntryMap::iterator it = entries_.find(memory_id); |
| + if (it != entries_.end()) { |
| + scoped_refptr<VideoCaptureMemory> memory_handler = it->second; |
| + memory_handler->StopCapture(NULL); |
| + } else { |
| + // It does not exist so it must have been stopped already. |
|
wjia(left Chromium)
2011/05/19 04:24:37
indent
Per K
2011/05/19 08:55:31
Done.
|
| + Send(new VideoCaptureMsg_StateChanged(msg.routing_id(), device_id, |
| + media::VideoCapture::kStopped)); |
| + } |
| +} |
| + |
| +void VideoCaptureHost::OnPauseCapture(const IPC::Message& msg, int device_id) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + // Not used. |
| + Send(new VideoCaptureMsg_StateChanged(msg.routing_id(), device_id, |
| + media::VideoCapture::kError)); |
| +} |
| + |
| +void VideoCaptureHost::OnReceiveEmptyBuffer(const IPC::Message& msg, |
| + int device_id, |
| + TransportDIB::Handle handle) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + VideoCaptureMemory::VideoCaptureMemoryId memory_id(msg.routing_id(), |
| + device_id); |
|
wjia(left Chromium)
2011/05/19 04:24:37
indent
Per K
2011/05/19 08:55:31
Done.
|
| + EntryMap::iterator it = entries_.find(memory_id); |
| + if (it != entries_.end()) { |
| + scoped_refptr<VideoCaptureMemory> memory_handler = it->second; |
| + memory_handler->ReturnTransportDIB(handle); |
| + } |
| +} |
| + |
| +void VideoCaptureHost::DoDeleteVideoCaptureMemory |
| + (VideoCaptureMemory::VideoCaptureMemoryId id) { |
|
wjia(left Chromium)
2011/05/19 04:24:37
the open "(" would go with function name.
Per K
2011/05/19 08:55:31
Done.
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + // Report that the device have successfully been stopped. |
| + Send(new VideoCaptureMsg_StateChanged(id.first, id.second, |
| + media::VideoCapture::kStopped)); |
|
wjia(left Chromium)
2011/05/19 04:24:37
"kStopped" could be sent multiple time for a devic
Per K
2011/05/19 08:55:31
This can happen if an Error occur and the message
|
| + entries_.erase(id); |
| +} |