1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
// 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/media/video_capture_host.h"
#include "base/memory/scoped_ptr.h"
#include "base/stl_util.h"
#include "content/browser/renderer_host/media/media_stream_manager.h"
#include "content/browser/resource_context.h"
#include "content/common/media/video_capture_messages.h"
VideoCaptureHost::VideoCaptureHost(
const content::ResourceContext* resource_context)
: resource_context_(resource_context) {
}
VideoCaptureHost::~VideoCaptureHost() {}
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++) {
VideoCaptureController* controller = it->second;
// Since the channel is closing we need a task to make sure VideoCaptureHost
// is not deleted before VideoCaptureController.
controller->StopCapture(
NewRunnableMethod(this, &VideoCaptureHost::OnReadyToDelete, it->first));
}
}
void VideoCaptureHost::OnDestruct() const {
BrowserThread::DeleteOnIOThread::Destruct(this);
}
///////////////////////////////////////////////////////////////////////////////
// Implements VideoCaptureControllerEventHandler.
void VideoCaptureHost::OnError(const VideoCaptureControllerID& id) {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableMethod(this, &VideoCaptureHost::DoHandleError, id.device_id));
}
void VideoCaptureHost::OnBufferCreated(
const VideoCaptureControllerID& id,
base::SharedMemoryHandle handle,
int length,
int buffer_id) {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableMethod(this, &VideoCaptureHost::DoSendNewBuffer,
id.device_id, handle, length, buffer_id));
}
void VideoCaptureHost::OnBufferReady(
const VideoCaptureControllerID& id,
int buffer_id,
base::Time timestamp) {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableMethod(this, &VideoCaptureHost::DoSendFilledBuffer,
id.device_id, buffer_id, timestamp));
}
void VideoCaptureHost::OnFrameInfo(const VideoCaptureControllerID& id,
int width,
int height,
int frame_per_second) {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableMethod(this, &VideoCaptureHost::DoSendFrameInfo,
id.device_id, width, height, frame_per_second));
}
void VideoCaptureHost::OnReadyToDelete(const VideoCaptureControllerID& id) {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableMethod(this, &VideoCaptureHost::DoDeleteVideoCaptureController,
id));
}
void VideoCaptureHost::DoSendNewBuffer(
int device_id, base::SharedMemoryHandle handle,
int length, int buffer_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
Send(new VideoCaptureMsg_NewBuffer(device_id, handle,
length, buffer_id));
}
void VideoCaptureHost::DoSendFilledBuffer(
int device_id, int buffer_id, base::Time timestamp) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
Send(new VideoCaptureMsg_BufferReady(device_id, buffer_id,
timestamp));
}
void VideoCaptureHost::DoHandleError(int device_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
Send(new VideoCaptureMsg_StateChanged(device_id,
media::VideoCapture::kError));
VideoCaptureControllerID id(device_id);
EntryMap::iterator it = entries_.find(id);
if (it != entries_.end()) {
VideoCaptureController* controller = it->second;
controller->StopCapture(NULL);
}
}
void VideoCaptureHost::DoSendFrameInfo(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(device_id, params));
Send(new VideoCaptureMsg_StateChanged(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(int device_id,
const media::VideoCaptureParams& params) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
VideoCaptureControllerID controller_id(device_id);
DCHECK(entries_.find(controller_id) == entries_.end());
scoped_refptr<VideoCaptureController> controller =
new VideoCaptureController(
controller_id, peer_handle(), this,
resource_context_->media_stream_manager()->video_capture_manager());
entries_.insert(std::make_pair(controller_id, controller));
controller->StartCapture(params);
}
void VideoCaptureHost::OnStopCapture(int device_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
VideoCaptureControllerID controller_id(device_id);
EntryMap::iterator it = entries_.find(controller_id);
if (it != entries_.end()) {
scoped_refptr<VideoCaptureController> controller = it->second;
controller->StopCapture(NULL);
} else {
// It does not exist so it must have been stopped already.
Send(new VideoCaptureMsg_StateChanged(device_id,
media::VideoCapture::kStopped));
}
}
void VideoCaptureHost::OnPauseCapture(int device_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Not used.
Send(new VideoCaptureMsg_StateChanged(device_id,
media::VideoCapture::kError));
}
void VideoCaptureHost::OnReceiveEmptyBuffer(int device_id, int buffer_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
VideoCaptureControllerID controller_id(device_id);
EntryMap::iterator it = entries_.find(controller_id);
if (it != entries_.end()) {
scoped_refptr<VideoCaptureController> controller = it->second;
controller->ReturnBuffer(buffer_id);
}
}
void VideoCaptureHost::DoDeleteVideoCaptureController(
const VideoCaptureControllerID& id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Report that the device have successfully been stopped.
Send(new VideoCaptureMsg_StateChanged(id.device_id,
media::VideoCapture::kStopped));
entries_.erase(id);
}
|