| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/browser/renderer_host/media/media_stream_ui_proxy.h" | 5 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" |
| 6 | 6 |
| 7 #include "content/browser/renderer_host/render_view_host_delegate.h" | 7 #include "content/browser/renderer_host/render_view_host_delegate.h" |
| 8 #include "content/browser/renderer_host/render_view_host_impl.h" | 8 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "media/video/capture/fake_video_capture_device.h" | 10 #include "media/video/capture/fake_video_capture_device.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 MediaStreamUIProxy::Core::Core(const base::WeakPtr<MediaStreamUIProxy>& proxy, | 41 MediaStreamUIProxy::Core::Core(const base::WeakPtr<MediaStreamUIProxy>& proxy, |
| 42 RenderViewHostDelegate* test_render_delegate) | 42 RenderViewHostDelegate* test_render_delegate) |
| 43 : proxy_(proxy), | 43 : proxy_(proxy), |
| 44 test_render_delegate_(test_render_delegate), | 44 test_render_delegate_(test_render_delegate), |
| 45 weak_factory_(this) { | 45 weak_factory_(this) { |
| 46 } | 46 } |
| 47 | 47 |
| 48 MediaStreamUIProxy::Core::~Core() { | 48 MediaStreamUIProxy::Core::~Core() { |
| 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 49 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void MediaStreamUIProxy::Core::RequestAccess( | 52 void MediaStreamUIProxy::Core::RequestAccess( |
| 53 const MediaStreamRequest& request) { | 53 const MediaStreamRequest& request) { |
| 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 54 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 55 | 55 |
| 56 RenderViewHostDelegate* render_delegate; | 56 RenderViewHostDelegate* render_delegate; |
| 57 | 57 |
| 58 if (test_render_delegate_) { | 58 if (test_render_delegate_) { |
| 59 render_delegate = test_render_delegate_; | 59 render_delegate = test_render_delegate_; |
| 60 } else { | 60 } else { |
| 61 RenderViewHostImpl* host = RenderViewHostImpl::FromID( | 61 RenderViewHostImpl* host = RenderViewHostImpl::FromID( |
| 62 request.render_process_id, request.render_view_id); | 62 request.render_process_id, request.render_view_id); |
| 63 | 63 |
| 64 // Tab may have gone away. | 64 // Tab may have gone away. |
| 65 if (!host || !host->GetDelegate()) { | 65 if (!host || !host->GetDelegate()) { |
| 66 ProcessAccessRequestResponse( | 66 ProcessAccessRequestResponse( |
| 67 MediaStreamDevices(), | 67 MediaStreamDevices(), |
| 68 MEDIA_DEVICE_INVALID_STATE, | 68 MEDIA_DEVICE_INVALID_STATE, |
| 69 scoped_ptr<MediaStreamUI>()); | 69 scoped_ptr<MediaStreamUI>()); |
| 70 return; | 70 return; |
| 71 } | 71 } |
| 72 | 72 |
| 73 render_delegate = host->GetDelegate(); | 73 render_delegate = host->GetDelegate(); |
| 74 } | 74 } |
| 75 | 75 |
| 76 render_delegate->RequestMediaAccessPermission( | 76 render_delegate->RequestMediaAccessPermission( |
| 77 request, base::Bind(&Core::ProcessAccessRequestResponse, | 77 request, base::Bind(&Core::ProcessAccessRequestResponse, |
| 78 weak_factory_.GetWeakPtr())); | 78 weak_factory_.GetWeakPtr())); |
| 79 } | 79 } |
| 80 | 80 |
| 81 void MediaStreamUIProxy::Core::OnStarted(gfx::NativeViewId* window_id) { | 81 void MediaStreamUIProxy::Core::OnStarted(gfx::NativeViewId* window_id) { |
| 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 82 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 83 if (ui_) { | 83 if (ui_) { |
| 84 *window_id = ui_->OnStarted( | 84 *window_id = ui_->OnStarted( |
| 85 base::Bind(&Core::ProcessStopRequestFromUI, base::Unretained(this))); | 85 base::Bind(&Core::ProcessStopRequestFromUI, base::Unretained(this))); |
| 86 } | 86 } |
| 87 } | 87 } |
| 88 | 88 |
| 89 void MediaStreamUIProxy::Core::ProcessAccessRequestResponse( | 89 void MediaStreamUIProxy::Core::ProcessAccessRequestResponse( |
| 90 const MediaStreamDevices& devices, | 90 const MediaStreamDevices& devices, |
| 91 content::MediaStreamRequestResult result, | 91 content::MediaStreamRequestResult result, |
| 92 scoped_ptr<MediaStreamUI> stream_ui) { | 92 scoped_ptr<MediaStreamUI> stream_ui) { |
| 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 93 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 94 | 94 |
| 95 ui_ = stream_ui.Pass(); | 95 ui_ = stream_ui.Pass(); |
| 96 BrowserThread::PostTask( | 96 BrowserThread::PostTask( |
| 97 BrowserThread::IO, FROM_HERE, | 97 BrowserThread::IO, FROM_HERE, |
| 98 base::Bind(&MediaStreamUIProxy::ProcessAccessRequestResponse, | 98 base::Bind(&MediaStreamUIProxy::ProcessAccessRequestResponse, |
| 99 proxy_, devices, result)); | 99 proxy_, devices, result)); |
| 100 } | 100 } |
| 101 | 101 |
| 102 void MediaStreamUIProxy::Core::ProcessStopRequestFromUI() { | 102 void MediaStreamUIProxy::Core::ProcessStopRequestFromUI() { |
| 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 103 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 104 | 104 |
| 105 BrowserThread::PostTask( | 105 BrowserThread::PostTask( |
| 106 BrowserThread::IO, FROM_HERE, | 106 BrowserThread::IO, FROM_HERE, |
| 107 base::Bind(&MediaStreamUIProxy::ProcessStopRequestFromUI, proxy_)); | 107 base::Bind(&MediaStreamUIProxy::ProcessStopRequestFromUI, proxy_)); |
| 108 } | 108 } |
| 109 | 109 |
| 110 // static | 110 // static |
| 111 scoped_ptr<MediaStreamUIProxy> MediaStreamUIProxy::Create() { | 111 scoped_ptr<MediaStreamUIProxy> MediaStreamUIProxy::Create() { |
| 112 return scoped_ptr<MediaStreamUIProxy>(new MediaStreamUIProxy(NULL)); | 112 return scoped_ptr<MediaStreamUIProxy>(new MediaStreamUIProxy(NULL)); |
| 113 } | 113 } |
| 114 | 114 |
| 115 // static | 115 // static |
| 116 scoped_ptr<MediaStreamUIProxy> MediaStreamUIProxy::CreateForTests( | 116 scoped_ptr<MediaStreamUIProxy> MediaStreamUIProxy::CreateForTests( |
| 117 RenderViewHostDelegate* render_delegate) { | 117 RenderViewHostDelegate* render_delegate) { |
| 118 return scoped_ptr<MediaStreamUIProxy>( | 118 return scoped_ptr<MediaStreamUIProxy>( |
| 119 new MediaStreamUIProxy(render_delegate)); | 119 new MediaStreamUIProxy(render_delegate)); |
| 120 } | 120 } |
| 121 | 121 |
| 122 MediaStreamUIProxy::MediaStreamUIProxy( | 122 MediaStreamUIProxy::MediaStreamUIProxy( |
| 123 RenderViewHostDelegate* test_render_delegate) | 123 RenderViewHostDelegate* test_render_delegate) |
| 124 : weak_factory_(this) { | 124 : weak_factory_(this) { |
| 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 125 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 126 core_.reset(new Core(weak_factory_.GetWeakPtr(), test_render_delegate)); | 126 core_.reset(new Core(weak_factory_.GetWeakPtr(), test_render_delegate)); |
| 127 } | 127 } |
| 128 | 128 |
| 129 MediaStreamUIProxy::~MediaStreamUIProxy() { | 129 MediaStreamUIProxy::~MediaStreamUIProxy() { |
| 130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 130 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 131 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, core_.release()); | 131 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, core_.release()); |
| 132 } | 132 } |
| 133 | 133 |
| 134 void MediaStreamUIProxy::RequestAccess( | 134 void MediaStreamUIProxy::RequestAccess( |
| 135 const MediaStreamRequest& request, | 135 const MediaStreamRequest& request, |
| 136 const ResponseCallback& response_callback) { | 136 const ResponseCallback& response_callback) { |
| 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 137 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 138 | 138 |
| 139 response_callback_ = response_callback; | 139 response_callback_ = response_callback; |
| 140 BrowserThread::PostTask( | 140 BrowserThread::PostTask( |
| 141 BrowserThread::UI, FROM_HERE, | 141 BrowserThread::UI, FROM_HERE, |
| 142 base::Bind(&Core::RequestAccess, base::Unretained(core_.get()), request)); | 142 base::Bind(&Core::RequestAccess, base::Unretained(core_.get()), request)); |
| 143 } | 143 } |
| 144 | 144 |
| 145 void MediaStreamUIProxy::OnStarted(const base::Closure& stop_callback, | 145 void MediaStreamUIProxy::OnStarted(const base::Closure& stop_callback, |
| 146 const WindowIdCallback& window_id_callback) { | 146 const WindowIdCallback& window_id_callback) { |
| 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 147 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 148 | 148 |
| 149 stop_callback_ = stop_callback; | 149 stop_callback_ = stop_callback; |
| 150 | 150 |
| 151 // Owned by the PostTaskAndReply callback. | 151 // Owned by the PostTaskAndReply callback. |
| 152 gfx::NativeViewId* window_id = new gfx::NativeViewId(0); | 152 gfx::NativeViewId* window_id = new gfx::NativeViewId(0); |
| 153 | 153 |
| 154 BrowserThread::PostTaskAndReply( | 154 BrowserThread::PostTaskAndReply( |
| 155 BrowserThread::UI, | 155 BrowserThread::UI, |
| 156 FROM_HERE, | 156 FROM_HERE, |
| 157 base::Bind(&Core::OnStarted, base::Unretained(core_.get()), window_id), | 157 base::Bind(&Core::OnStarted, base::Unretained(core_.get()), window_id), |
| 158 base::Bind(&MediaStreamUIProxy::OnWindowId, | 158 base::Bind(&MediaStreamUIProxy::OnWindowId, |
| 159 weak_factory_.GetWeakPtr(), | 159 weak_factory_.GetWeakPtr(), |
| 160 window_id_callback, | 160 window_id_callback, |
| 161 base::Owned(window_id))); | 161 base::Owned(window_id))); |
| 162 } | 162 } |
| 163 | 163 |
| 164 void MediaStreamUIProxy::OnWindowId(const WindowIdCallback& window_id_callback, | 164 void MediaStreamUIProxy::OnWindowId(const WindowIdCallback& window_id_callback, |
| 165 gfx::NativeViewId* window_id) { | 165 gfx::NativeViewId* window_id) { |
| 166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 166 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 167 if (!window_id_callback.is_null()) | 167 if (!window_id_callback.is_null()) |
| 168 window_id_callback.Run(*window_id); | 168 window_id_callback.Run(*window_id); |
| 169 } | 169 } |
| 170 | 170 |
| 171 void MediaStreamUIProxy::ProcessAccessRequestResponse( | 171 void MediaStreamUIProxy::ProcessAccessRequestResponse( |
| 172 const MediaStreamDevices& devices, | 172 const MediaStreamDevices& devices, |
| 173 content::MediaStreamRequestResult result) { | 173 content::MediaStreamRequestResult result) { |
| 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 174 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 175 DCHECK(!response_callback_.is_null()); | 175 DCHECK(!response_callback_.is_null()); |
| 176 | 176 |
| 177 ResponseCallback cb = response_callback_; | 177 ResponseCallback cb = response_callback_; |
| 178 response_callback_.Reset(); | 178 response_callback_.Reset(); |
| 179 cb.Run(devices, result); | 179 cb.Run(devices, result); |
| 180 } | 180 } |
| 181 | 181 |
| 182 void MediaStreamUIProxy::ProcessStopRequestFromUI() { | 182 void MediaStreamUIProxy::ProcessStopRequestFromUI() { |
| 183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 183 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 184 DCHECK(!stop_callback_.is_null()); | 184 DCHECK(!stop_callback_.is_null()); |
| 185 | 185 |
| 186 base::Closure cb = stop_callback_; | 186 base::Closure cb = stop_callback_; |
| 187 stop_callback_.Reset(); | 187 stop_callback_.Reset(); |
| 188 cb.Run(); | 188 cb.Run(); |
| 189 } | 189 } |
| 190 | 190 |
| 191 FakeMediaStreamUIProxy::FakeMediaStreamUIProxy() | 191 FakeMediaStreamUIProxy::FakeMediaStreamUIProxy() |
| 192 : MediaStreamUIProxy(NULL) { | 192 : MediaStreamUIProxy(NULL) { |
| 193 } | 193 } |
| 194 | 194 |
| 195 FakeMediaStreamUIProxy::~FakeMediaStreamUIProxy() {} | 195 FakeMediaStreamUIProxy::~FakeMediaStreamUIProxy() {} |
| 196 | 196 |
| 197 void FakeMediaStreamUIProxy::SetAvailableDevices( | 197 void FakeMediaStreamUIProxy::SetAvailableDevices( |
| 198 const MediaStreamDevices& devices) { | 198 const MediaStreamDevices& devices) { |
| 199 devices_ = devices; | 199 devices_ = devices; |
| 200 } | 200 } |
| 201 | 201 |
| 202 void FakeMediaStreamUIProxy::RequestAccess( | 202 void FakeMediaStreamUIProxy::RequestAccess( |
| 203 const MediaStreamRequest& request, | 203 const MediaStreamRequest& request, |
| 204 const ResponseCallback& response_callback) { | 204 const ResponseCallback& response_callback) { |
| 205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 205 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 206 | 206 |
| 207 response_callback_ = response_callback; | 207 response_callback_ = response_callback; |
| 208 | 208 |
| 209 MediaStreamDevices devices_to_use; | 209 MediaStreamDevices devices_to_use; |
| 210 bool accepted_audio = false; | 210 bool accepted_audio = false; |
| 211 bool accepted_video = false; | 211 bool accepted_video = false; |
| 212 | 212 |
| 213 // Use the first capture device of the same media type in the list for the | 213 // Use the first capture device of the same media type in the list for the |
| 214 // fake UI. | 214 // fake UI. |
| 215 for (MediaStreamDevices::const_iterator it = devices_.begin(); | 215 for (MediaStreamDevices::const_iterator it = devices_.begin(); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 245 devices_to_use.empty() ? | 245 devices_to_use.empty() ? |
| 246 MEDIA_DEVICE_NO_HARDWARE : | 246 MEDIA_DEVICE_NO_HARDWARE : |
| 247 MEDIA_DEVICE_OK)); | 247 MEDIA_DEVICE_OK)); |
| 248 } | 248 } |
| 249 | 249 |
| 250 void FakeMediaStreamUIProxy::OnStarted( | 250 void FakeMediaStreamUIProxy::OnStarted( |
| 251 const base::Closure& stop_callback, | 251 const base::Closure& stop_callback, |
| 252 const WindowIdCallback& window_id_callback) {} | 252 const WindowIdCallback& window_id_callback) {} |
| 253 | 253 |
| 254 } // namespace content | 254 } // namespace content |
| OLD | NEW |