Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: content/browser/renderer_host/video_capture_host.cc

Issue 7002027: VideoCaptureHost (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 // Delete all Memory handlers. The map is not empty if the
15 // VideoCaptureMessageFilter have not returned all
16 // TransportDIBs.
17 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.
18 }
19
20 void VideoCaptureHost::OnChannelClosing() {
21 BrowserMessageFilter::OnChannelClosing();
22
23 // Since the IPC channel is gone, close all requested VideCaptureDevices.
24 for (EntryMap::iterator it = entries_.begin(); it != entries_.end(); it++) {
25 VideoCaptureMemory* memory_handler = it->second;
26 // Since the channel is closing we need a task to make sure VideoCaptureHost
27 // is not deleted before VideoCaptureMemory.
28 memory_handler->StopCapture(
29 NewRunnableMethod(this, &VideoCaptureHost::OnReadyToDelete, it->first));
30 }
31 }
32
33 void VideoCaptureHost::OnDestruct() const {
34 BrowserThread::DeleteOnIOThread::Destruct(this);
35 }
36
37 ///////////////////////////////////////////////////////////////////////////////
38
39 // Implements VideoCaptureMemory::EventHandler.
40 void VideoCaptureHost::OnError(VideoCaptureMemory::VideoCaptureMemoryId id) {
41 BrowserThread::PostTask(
42 BrowserThread::IO, FROM_HERE,
43 NewRunnableMethod(this, &VideoCaptureHost::DoHandleError, id.first,
44 id.second));
45 }
46
47 void VideoCaptureHost::OnBufferReady(
48 VideoCaptureMemory::VideoCaptureMemoryId id,
49 TransportDIB::Handle handle,
50 base::Time timestamp) {
51 BrowserThread::PostTask(
52 BrowserThread::IO, FROM_HERE,
53 NewRunnableMethod(this, &VideoCaptureHost::DoSendFilledBuffer, id.first,
54 id.second, handle, timestamp));
55 }
56
57 void VideoCaptureHost::OnFrameInfo(VideoCaptureMemory::VideoCaptureMemoryId id,
58 int width,
59 int height,
60 int frame_per_second) {
61 BrowserThread::PostTask(
62 BrowserThread::IO, FROM_HERE,
63 NewRunnableMethod(this, &VideoCaptureHost::DoSendFrameInfo, id.first,
64 id.second, width, height, frame_per_second));
65 }
66
67 void VideoCaptureHost::OnReadyToDelete(
68 VideoCaptureMemory::VideoCaptureMemoryId id) {
69 BrowserThread::PostTask(
70 BrowserThread::IO, FROM_HERE,
71 NewRunnableMethod(this, &VideoCaptureHost::DoDeleteVideoCaptureMemory,
72 id));
73 }
74
75 void VideoCaptureHost::DoSendFilledBuffer(int32 routing_id,
76 int device_id,
77 TransportDIB::Handle handle,
78 base::Time timestamp) {
79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
80
81 Send(new VideoCaptureMsg_BufferReady(routing_id, device_id, handle,
82 timestamp));
83 }
84
85 void VideoCaptureHost::DoHandleError(int32 routing_id, int device_id) {
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
87
88 Send(new VideoCaptureMsg_StateChanged(routing_id, device_id,
89 media::VideoCapture::kError));
90
91 VideoCaptureMemory::VideoCaptureMemoryId id(routing_id, device_id);
92 EntryMap::iterator it = entries_.find(id);
93 if (it != entries_.end()) {
94 VideoCaptureMemory* memory_handler = it->second;
95 memory_handler->StopCapture(NULL);
96 }
97 }
98
99 void VideoCaptureHost::DoSendFrameInfo(int32 routing_id,
100 int device_id,
101 int width,
102 int height,
103 int frame_per_second) {
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
105
106 media::VideoCaptureParams params;
107 params.width = width;
108 params.height = height;
109 params.frame_per_second = frame_per_second;
110 Send(new VideoCaptureMsg_DeviceInfo(routing_id, device_id, params));
111 Send(new VideoCaptureMsg_StateChanged(routing_id, device_id,
112 media::VideoCapture::kStarted));
113 }
114
115 ///////////////////////////////////////////////////////////////////////////////
116 // IPC Messages handler.
117 bool VideoCaptureHost::OnMessageReceived(const IPC::Message& message,
118 bool* message_was_ok) {
119 bool handled = true;
120 IPC_BEGIN_MESSAGE_MAP_EX(VideoCaptureHost, message, *message_was_ok)
121 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Start, OnStartCapture)
122 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Pause, OnPauseCapture)
123 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Stop, OnStopCapture)
124 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady, OnReceiveEmptyBuffer)
125 IPC_MESSAGE_UNHANDLED(handled = false)
126 IPC_END_MESSAGE_MAP_EX()
127
128 return handled;
129 }
130
131 void VideoCaptureHost::OnStartCapture(const IPC::Message& msg, int device_id,
132 const media::VideoCaptureParams& params) {
133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
134
135 VideoCaptureMemory::VideoCaptureMemoryId memory_id(msg.routing_id(),
136 device_id);
137
138 DCHECK(entries_.find(memory_id) == entries_.end());
139
140 scoped_refptr<VideoCaptureMemory> memory_handler =
141 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
142 entries_.insert(std::make_pair(memory_id, memory_handler));
143 memory_handler->StartCapture(params);
144 }
145
146 void VideoCaptureHost::OnStopCapture(const IPC::Message& msg, int device_id) {
147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
148
149 VideoCaptureMemory::VideoCaptureMemoryId memory_id(msg.routing_id(),
150 device_id);
151 EntryMap::iterator it = entries_.find(memory_id);
152 if (it != entries_.end()) {
153 scoped_refptr<VideoCaptureMemory> memory_handler = it->second;
154 memory_handler->StopCapture(NULL);
155 } else {
156 // 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.
157 Send(new VideoCaptureMsg_StateChanged(msg.routing_id(), device_id,
158 media::VideoCapture::kStopped));
159 }
160 }
161
162 void VideoCaptureHost::OnPauseCapture(const IPC::Message& msg, int device_id) {
163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
164 // Not used.
165 Send(new VideoCaptureMsg_StateChanged(msg.routing_id(), device_id,
166 media::VideoCapture::kError));
167 }
168
169 void VideoCaptureHost::OnReceiveEmptyBuffer(const IPC::Message& msg,
170 int device_id,
171 TransportDIB::Handle handle) {
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
173
174 VideoCaptureMemory::VideoCaptureMemoryId memory_id(msg.routing_id(),
175 device_id);
wjia(left Chromium) 2011/05/19 04:24:37 indent
Per K 2011/05/19 08:55:31 Done.
176 EntryMap::iterator it = entries_.find(memory_id);
177 if (it != entries_.end()) {
178 scoped_refptr<VideoCaptureMemory> memory_handler = it->second;
179 memory_handler->ReturnTransportDIB(handle);
180 }
181 }
182
183 void VideoCaptureHost::DoDeleteVideoCaptureMemory
184 (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.
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
186
187 // Report that the device have successfully been stopped.
188 Send(new VideoCaptureMsg_StateChanged(id.first, id.second,
189 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
190 entries_.erase(id);
191 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698