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
3 // found in the LICENSE file.
4
5 #include "content/browser/renderer_host/media/media_stream_dispatcher_host.h"
6
7 #include "content/browser/browser_main_loop.h"
8 #include "content/browser/renderer_host/media/web_contents_capture_util.h"
9 #include "content/common/media/media_stream_messages.h"
10 #include "content/common/media/media_stream_options.h"
11 #include "url/gurl.h"
12
13 namespace content {
14
MediaStreamDispatcherHost(int render_process_id,const ResourceContext::SaltCallback & salt_callback,MediaStreamManager * media_stream_manager)15 MediaStreamDispatcherHost::MediaStreamDispatcherHost(
16 int render_process_id,
17 const ResourceContext::SaltCallback& salt_callback,
18 MediaStreamManager* media_stream_manager)
19 : render_process_id_(render_process_id),
20 salt_callback_(salt_callback),
21 media_stream_manager_(media_stream_manager) {
22 }
23
StreamGenerated(int render_view_id,int page_request_id,const std::string & label,const StreamDeviceInfoArray & audio_devices,const StreamDeviceInfoArray & video_devices)24 void MediaStreamDispatcherHost::StreamGenerated(
25 int render_view_id,
26 int page_request_id,
27 const std::string& label,
28 const StreamDeviceInfoArray& audio_devices,
29 const StreamDeviceInfoArray& video_devices) {
30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
31 DVLOG(1) << "MediaStreamDispatcherHost::StreamGenerated("
32 << ", {label = " << label << "})";
33
34 Send(new MediaStreamMsg_StreamGenerated(
35 render_view_id, page_request_id, label, audio_devices,
36 video_devices));
37 }
38
StreamGenerationFailed(int render_view_id,int page_request_id)39 void MediaStreamDispatcherHost::StreamGenerationFailed(int render_view_id,
40 int page_request_id) {
41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
42 DVLOG(1) << "MediaStreamDispatcherHost::StreamGenerationFailed("
43 << ", {page_request_id = " << page_request_id << "})";
44
45
46 Send(new MediaStreamMsg_StreamGenerationFailed(render_view_id,
47 page_request_id));
48 }
49
DeviceStopped(int render_view_id,const std::string & label,const StreamDeviceInfo & device)50 void MediaStreamDispatcherHost::DeviceStopped(int render_view_id,
51 const std::string& label,
52 const StreamDeviceInfo& device) {
53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
54 DVLOG(1) << "MediaStreamDispatcherHost::DeviceStopped("
55 << "{label = " << label << "}, "
56 << "{type = " << device.device.type << "}, "
57 << "{device_id = " << device.device.id << "})";
58
59 Send(new MediaStreamMsg_DeviceStopped(render_view_id, label, device));
60 }
61
DevicesEnumerated(int render_view_id,int page_request_id,const std::string & label,const StreamDeviceInfoArray & devices)62 void MediaStreamDispatcherHost::DevicesEnumerated(
63 int render_view_id,
64 int page_request_id,
65 const std::string& label,
66 const StreamDeviceInfoArray& devices) {
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
68 DVLOG(1) << "MediaStreamDispatcherHost::DevicesEnumerated("
69 << ", {page_request_id = " << page_request_id << "})";
70
71 Send(new MediaStreamMsg_DevicesEnumerated(render_view_id, page_request_id,
72 devices));
73 }
74
DeviceOpened(int render_view_id,int page_request_id,const std::string & label,const StreamDeviceInfo & video_device)75 void MediaStreamDispatcherHost::DeviceOpened(
76 int render_view_id,
77 int page_request_id,
78 const std::string& label,
79 const StreamDeviceInfo& video_device) {
80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
81 DVLOG(1) << "MediaStreamDispatcherHost::DeviceOpened("
82 << ", {page_request_id = " << page_request_id << "})";
83
84 Send(new MediaStreamMsg_DeviceOpened(
85 render_view_id, page_request_id, label, video_device));
86 }
87
OnMessageReceived(const IPC::Message & message,bool * message_was_ok)88 bool MediaStreamDispatcherHost::OnMessageReceived(
89 const IPC::Message& message, bool* message_was_ok) {
90 bool handled = true;
91 IPC_BEGIN_MESSAGE_MAP_EX(MediaStreamDispatcherHost, message, *message_was_ok)
92 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_GenerateStream, OnGenerateStream)
93 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CancelGenerateStream,
94 OnCancelGenerateStream)
95 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_StopStreamDevice,
96 OnStopStreamDevice)
97 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_EnumerateDevices,
98 OnEnumerateDevices)
99 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CancelEnumerateDevices,
100 OnCancelEnumerateDevices)
101 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_OpenDevice,
102 OnOpenDevice)
103 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CloseDevice,
104 OnCloseDevice)
105 IPC_MESSAGE_UNHANDLED(handled = false)
106 IPC_END_MESSAGE_MAP_EX()
107 return handled;
108 }
109
OnChannelClosing()110 void MediaStreamDispatcherHost::OnChannelClosing() {
111 DVLOG(1) << "MediaStreamDispatcherHost::OnChannelClosing";
112
113 // Since the IPC channel is gone, close all requesting/requested streams.
114 media_stream_manager_->CancelAllRequests(render_process_id_);
115 }
116
~MediaStreamDispatcherHost()117 MediaStreamDispatcherHost::~MediaStreamDispatcherHost() {
118 }
119
OnGenerateStream(int render_view_id,int page_request_id,const StreamOptions & components,const GURL & security_origin)120 void MediaStreamDispatcherHost::OnGenerateStream(
121 int render_view_id,
122 int page_request_id,
123 const StreamOptions& components,
124 const GURL& security_origin) {
125 DVLOG(1) << "MediaStreamDispatcherHost::OnGenerateStream("
126 << render_view_id << ", "
127 << page_request_id << ", ["
128 << " audio:" << components.audio_requested
129 << " video:" << components.video_requested
130 << " ], "
131 << security_origin.spec() << ")";
132
133 media_stream_manager_->GenerateStream(
134 this, render_process_id_, render_view_id, salt_callback_,
135 page_request_id, components, security_origin);
136 }
137
OnCancelGenerateStream(int render_view_id,int page_request_id)138 void MediaStreamDispatcherHost::OnCancelGenerateStream(int render_view_id,
139 int page_request_id) {
140 DVLOG(1) << "MediaStreamDispatcherHost::OnCancelGenerateStream("
141 << render_view_id << ", "
142 << page_request_id << ")";
143 media_stream_manager_->CancelRequest(render_process_id_, render_view_id,
144 page_request_id);
145 }
146
OnStopStreamDevice(int render_view_id,const std::string & device_id)147 void MediaStreamDispatcherHost::OnStopStreamDevice(
148 int render_view_id,
149 const std::string& device_id) {
150 DVLOG(1) << "MediaStreamDispatcherHost::OnStopStreamDevice("
151 << render_view_id << ", "
152 << device_id << ")";
153 media_stream_manager_->StopStreamDevice(render_process_id_, render_view_id,
154 device_id);
155 }
156
OnEnumerateDevices(int render_view_id,int page_request_id,MediaStreamType type,const GURL & security_origin)157 void MediaStreamDispatcherHost::OnEnumerateDevices(
158 int render_view_id,
159 int page_request_id,
160 MediaStreamType type,
161 const GURL& security_origin) {
162 DVLOG(1) << "MediaStreamDispatcherHost::OnEnumerateDevices("
163 << render_view_id << ", "
164 << page_request_id << ", "
165 << type << ", "
166 << security_origin.spec() << ")";
167
168 media_stream_manager_->EnumerateDevices(
169 this, render_process_id_, render_view_id, salt_callback_,
170 page_request_id, type, security_origin);
171 }
172
OnCancelEnumerateDevices(int render_view_id,int page_request_id)173 void MediaStreamDispatcherHost::OnCancelEnumerateDevices(
174 int render_view_id,
175 int page_request_id) {
176 DVLOG(1) << "MediaStreamDispatcherHost::OnCancelEnumerateDevices("
177 << render_view_id << ", "
178 << page_request_id << ")";
179 media_stream_manager_->CancelRequest(render_process_id_, render_view_id,
180 page_request_id);
181 }
182
OnOpenDevice(int render_view_id,int page_request_id,const std::string & device_id,MediaStreamType type,const GURL & security_origin)183 void MediaStreamDispatcherHost::OnOpenDevice(
184 int render_view_id,
185 int page_request_id,
186 const std::string& device_id,
187 MediaStreamType type,
188 const GURL& security_origin) {
189 DVLOG(1) << "MediaStreamDispatcherHost::OnOpenDevice("
190 << render_view_id << ", "
191 << page_request_id << ", device_id: "
192 << device_id.c_str() << ", type: "
193 << type << ", "
194 << security_origin.spec() << ")";
195
196 media_stream_manager_->OpenDevice(
197 this, render_process_id_, render_view_id, salt_callback_,
198 page_request_id, device_id, type, security_origin);
199
200 }
201
OnCloseDevice(int render_view_id,const std::string & label)202 void MediaStreamDispatcherHost::OnCloseDevice(
203 int render_view_id,
204 const std::string& label) {
205 DVLOG(1) << "MediaStreamDispatcherHost::OnCloseDevice("
206 << render_view_id << ", "
207 << label << ")";
208
209 media_stream_manager_->CancelRequest(label);
210 }
211
212 } // namespace content
213