OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_manager.h" | 5 #include "content/browser/renderer_host/media/media_stream_manager.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 std::unique_ptr<MediaStreamRequest> ui_request_; | 373 std::unique_ptr<MediaStreamRequest> ui_request_; |
374 MediaStreamType audio_type_; | 374 MediaStreamType audio_type_; |
375 MediaStreamType video_type_; | 375 MediaStreamType video_type_; |
376 int target_process_id_; | 376 int target_process_id_; |
377 int target_frame_id_; | 377 int target_frame_id_; |
378 }; | 378 }; |
379 | 379 |
380 // static | 380 // static |
381 void MediaStreamManager::SendMessageToNativeLog(const std::string& message) { | 381 void MediaStreamManager::SendMessageToNativeLog(const std::string& message) { |
382 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 382 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
383 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 383 BrowserThread::PostTask( |
384 base::Bind(&MediaStreamManager::SendMessageToNativeLog, message)); | 384 BrowserThread::IO, FROM_HERE, |
| 385 base::BindOnce(&MediaStreamManager::SendMessageToNativeLog, message)); |
385 return; | 386 return; |
386 } | 387 } |
387 | 388 |
388 MediaStreamManager* msm = g_media_stream_manager_tls_ptr.Pointer()->Get(); | 389 MediaStreamManager* msm = g_media_stream_manager_tls_ptr.Pointer()->Get(); |
389 if (!msm) { | 390 if (!msm) { |
390 DLOG(ERROR) << "No MediaStreamManager on the IO thread. " << message; | 391 DLOG(ERROR) << "No MediaStreamManager on the IO thread. " << message; |
391 return; | 392 return; |
392 } | 393 } |
393 | 394 |
394 msm->AddLogMessageOnIOThread(message); | 395 msm->AddLogMessageOnIOThread(message); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 MEDIA_DEVICE_ACCESS, controls, std::string()); | 502 MEDIA_DEVICE_ACCESS, controls, std::string()); |
502 | 503 |
503 const std::string& label = AddRequest(request); | 504 const std::string& label = AddRequest(request); |
504 | 505 |
505 request->callback = callback; | 506 request->callback = callback; |
506 // Post a task and handle the request asynchronously. The reason is that the | 507 // Post a task and handle the request asynchronously. The reason is that the |
507 // requester won't have a label for the request until this function returns | 508 // requester won't have a label for the request until this function returns |
508 // and thus can not handle a response. Using base::Unretained is safe since | 509 // and thus can not handle a response. Using base::Unretained is safe since |
509 // MediaStreamManager is deleted on the UI thread, after the IO thread has | 510 // MediaStreamManager is deleted on the UI thread, after the IO thread has |
510 // been stopped. | 511 // been stopped. |
511 BrowserThread::PostTask( | 512 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
512 BrowserThread::IO, FROM_HERE, | 513 base::BindOnce(&MediaStreamManager::SetupRequest, |
513 base::Bind(&MediaStreamManager::SetupRequest, | 514 base::Unretained(this), label)); |
514 base::Unretained(this), label)); | |
515 return label; | 515 return label; |
516 } | 516 } |
517 | 517 |
518 void MediaStreamManager::GenerateStream(MediaStreamRequester* requester, | 518 void MediaStreamManager::GenerateStream(MediaStreamRequester* requester, |
519 int render_process_id, | 519 int render_process_id, |
520 int render_frame_id, | 520 int render_frame_id, |
521 const std::string& salt, | 521 const std::string& salt, |
522 int page_request_id, | 522 int page_request_id, |
523 const StreamControls& controls, | 523 const StreamControls& controls, |
524 const url::Origin& security_origin, | 524 const url::Origin& security_origin, |
(...skipping 17 matching lines...) Expand all Loading... |
542 FinalizeRequestFailed(label, request, MEDIA_DEVICE_INVALID_STATE); | 542 FinalizeRequestFailed(label, request, MEDIA_DEVICE_INVALID_STATE); |
543 } | 543 } |
544 return; | 544 return; |
545 } | 545 } |
546 | 546 |
547 // Post a task and handle the request asynchronously. The reason is that the | 547 // Post a task and handle the request asynchronously. The reason is that the |
548 // requester won't have a label for the request until this function returns | 548 // requester won't have a label for the request until this function returns |
549 // and thus can not handle a response. Using base::Unretained is safe since | 549 // and thus can not handle a response. Using base::Unretained is safe since |
550 // MediaStreamManager is deleted on the UI thread, after the IO thread has | 550 // MediaStreamManager is deleted on the UI thread, after the IO thread has |
551 // been stopped. | 551 // been stopped. |
552 BrowserThread::PostTask( | 552 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
553 BrowserThread::IO, FROM_HERE, | 553 base::BindOnce(&MediaStreamManager::SetupRequest, |
554 base::Bind(&MediaStreamManager::SetupRequest, | 554 base::Unretained(this), label)); |
555 base::Unretained(this), label)); | |
556 } | 555 } |
557 | 556 |
558 void MediaStreamManager::CancelRequest(int render_process_id, | 557 void MediaStreamManager::CancelRequest(int render_process_id, |
559 int render_frame_id, | 558 int render_frame_id, |
560 int page_request_id) { | 559 int page_request_id) { |
561 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 560 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
562 for (const LabeledDeviceRequest& labeled_request : requests_) { | 561 for (const LabeledDeviceRequest& labeled_request : requests_) { |
563 DeviceRequest* const request = labeled_request.second; | 562 DeviceRequest* const request = labeled_request.second; |
564 if (request->requesting_process_id == render_process_id && | 563 if (request->requesting_process_id == render_process_id && |
565 request->requesting_frame_id == render_frame_id && | 564 request->requesting_frame_id == render_frame_id && |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
738 page_request_id, security_origin, | 737 page_request_id, security_origin, |
739 false, // user gesture | 738 false, // user gesture |
740 MEDIA_OPEN_DEVICE_PEPPER_ONLY, controls, salt); | 739 MEDIA_OPEN_DEVICE_PEPPER_ONLY, controls, salt); |
741 | 740 |
742 const std::string& label = AddRequest(request); | 741 const std::string& label = AddRequest(request); |
743 // Post a task and handle the request asynchronously. The reason is that the | 742 // Post a task and handle the request asynchronously. The reason is that the |
744 // requester won't have a label for the request until this function returns | 743 // requester won't have a label for the request until this function returns |
745 // and thus can not handle a response. Using base::Unretained is safe since | 744 // and thus can not handle a response. Using base::Unretained is safe since |
746 // MediaStreamManager is deleted on the UI thread, after the IO thread has | 745 // MediaStreamManager is deleted on the UI thread, after the IO thread has |
747 // been stopped. | 746 // been stopped. |
748 BrowserThread::PostTask( | 747 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
749 BrowserThread::IO, FROM_HERE, | 748 base::BindOnce(&MediaStreamManager::SetupRequest, |
750 base::Bind(&MediaStreamManager::SetupRequest, | 749 base::Unretained(this), label)); |
751 base::Unretained(this), label)); | |
752 } | 750 } |
753 | 751 |
754 bool MediaStreamManager::TranslateSourceIdToDeviceId( | 752 bool MediaStreamManager::TranslateSourceIdToDeviceId( |
755 MediaStreamType stream_type, | 753 MediaStreamType stream_type, |
756 const std::string& salt, | 754 const std::string& salt, |
757 const url::Origin& security_origin, | 755 const url::Origin& security_origin, |
758 const std::string& source_id, | 756 const std::string& source_id, |
759 std::string* device_id) const { | 757 std::string* device_id) const { |
760 DCHECK(stream_type == MEDIA_DEVICE_AUDIO_CAPTURE || | 758 DCHECK(stream_type == MEDIA_DEVICE_AUDIO_CAPTURE || |
761 stream_type == MEDIA_DEVICE_VIDEO_CAPTURE); | 759 stream_type == MEDIA_DEVICE_VIDEO_CAPTURE); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
925 | 923 |
926 // Actual audio parameters are required only for MEDIA_TAB_AUDIO_CAPTURE. | 924 // Actual audio parameters are required only for MEDIA_TAB_AUDIO_CAPTURE. |
927 // TODO(guidou): MEDIA_TAB_AUDIO_CAPTURE should not be a special case. See | 925 // TODO(guidou): MEDIA_TAB_AUDIO_CAPTURE should not be a special case. See |
928 // crbug.com/584287. | 926 // crbug.com/584287. |
929 if (request->audio_type() == MEDIA_TAB_AUDIO_CAPTURE) { | 927 if (request->audio_type() == MEDIA_TAB_AUDIO_CAPTURE) { |
930 // Using base::Unretained is safe: |audio_system_| will post | 928 // Using base::Unretained is safe: |audio_system_| will post |
931 // PostRequestToUI() to IO thread, and MediaStreamManager is deleted on the | 929 // PostRequestToUI() to IO thread, and MediaStreamManager is deleted on the |
932 // UI thread, after the IO thread has been stopped. | 930 // UI thread, after the IO thread has been stopped. |
933 audio_system_->GetOutputStreamParameters( | 931 audio_system_->GetOutputStreamParameters( |
934 media::AudioDeviceDescription::kDefaultDeviceId, | 932 media::AudioDeviceDescription::kDefaultDeviceId, |
935 base::Bind(&MediaStreamManager::PostRequestToUI, base::Unretained(this), | 933 base::BindOnce(&MediaStreamManager::PostRequestToUI, |
936 label, request, enumeration)); | 934 base::Unretained(this), label, request, enumeration)); |
937 } else { | 935 } else { |
938 PostRequestToUI(label, request, enumeration, media::AudioParameters()); | 936 PostRequestToUI(label, request, enumeration, media::AudioParameters()); |
939 } | 937 } |
940 } | 938 } |
941 | 939 |
942 void MediaStreamManager::PostRequestToUI( | 940 void MediaStreamManager::PostRequestToUI( |
943 const std::string& label, | 941 const std::string& label, |
944 DeviceRequest* request, | 942 DeviceRequest* request, |
945 const MediaDeviceEnumeration& enumeration, | 943 const MediaDeviceEnumeration& enumeration, |
946 const media::AudioParameters& output_parameters) { | 944 const media::AudioParameters& output_parameters) { |
(...skipping 26 matching lines...) Expand all Loading... |
973 | 971 |
974 fake_ui_->SetAvailableDevices(devices); | 972 fake_ui_->SetAvailableDevices(devices); |
975 | 973 |
976 request->ui_proxy = std::move(fake_ui_); | 974 request->ui_proxy = std::move(fake_ui_); |
977 } else { | 975 } else { |
978 request->ui_proxy = MediaStreamUIProxy::Create(); | 976 request->ui_proxy = MediaStreamUIProxy::Create(); |
979 } | 977 } |
980 | 978 |
981 request->ui_proxy->RequestAccess( | 979 request->ui_proxy->RequestAccess( |
982 request->DetachUIRequest(), | 980 request->DetachUIRequest(), |
983 base::Bind(&MediaStreamManager::HandleAccessRequestResponse, | 981 base::BindOnce(&MediaStreamManager::HandleAccessRequestResponse, |
984 base::Unretained(this), label, output_parameters)); | 982 base::Unretained(this), label, output_parameters)); |
985 } | 983 } |
986 | 984 |
987 void MediaStreamManager::SetupRequest(const std::string& label) { | 985 void MediaStreamManager::SetupRequest(const std::string& label) { |
988 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 986 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
989 DeviceRequest* request = FindRequest(label); | 987 DeviceRequest* request = FindRequest(label); |
990 if (!request) { | 988 if (!request) { |
991 DVLOG(1) << "SetupRequest label " << label << " doesn't exist!!"; | 989 DVLOG(1) << "SetupRequest label " << label << " doesn't exist!!"; |
992 return; // This can happen if the request has been canceled. | 990 return; // This can happen if the request has been canceled. |
993 } | 991 } |
994 | 992 |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1234 } | 1232 } |
1235 | 1233 |
1236 void MediaStreamManager::InitializeMaybeAsync( | 1234 void MediaStreamManager::InitializeMaybeAsync( |
1237 std::unique_ptr<VideoCaptureProvider> video_capture_provider) { | 1235 std::unique_ptr<VideoCaptureProvider> video_capture_provider) { |
1238 // Some unit tests initialize the MSM in the IO thread and assume the | 1236 // Some unit tests initialize the MSM in the IO thread and assume the |
1239 // initialization is done synchronously. Other clients call this from a | 1237 // initialization is done synchronously. Other clients call this from a |
1240 // different thread and expect initialization to run asynchronously. | 1238 // different thread and expect initialization to run asynchronously. |
1241 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 1239 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
1242 BrowserThread::PostTask( | 1240 BrowserThread::PostTask( |
1243 BrowserThread::IO, FROM_HERE, | 1241 BrowserThread::IO, FROM_HERE, |
1244 base::Bind(&MediaStreamManager::InitializeMaybeAsync, | 1242 base::BindOnce(&MediaStreamManager::InitializeMaybeAsync, |
1245 base::Unretained(this), | 1243 base::Unretained(this), |
1246 base::Passed(&video_capture_provider))); | 1244 std::move(video_capture_provider))); |
1247 return; | 1245 return; |
1248 } | 1246 } |
1249 | 1247 |
1250 // Store a pointer to |this| on the IO thread to avoid having to jump to the | 1248 // Store a pointer to |this| on the IO thread to avoid having to jump to the |
1251 // UI thread to fetch a pointer to the MSM. In particular on Android, it can | 1249 // UI thread to fetch a pointer to the MSM. In particular on Android, it can |
1252 // be problematic to post to a UI thread from arbitrary worker threads since | 1250 // be problematic to post to a UI thread from arbitrary worker threads since |
1253 // attaching to the VM is required and we may have to access the MSM from | 1251 // attaching to the VM is required and we may have to access the MSM from |
1254 // callback threads that we don't own and don't want to attach. | 1252 // callback threads that we don't own and don't want to attach. |
1255 g_media_stream_manager_tls_ptr.Pointer()->Set(this); | 1253 g_media_stream_manager_tls_ptr.Pointer()->Set(this); |
1256 | 1254 |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1416 void MediaStreamManager::UseFakeUIForTests( | 1414 void MediaStreamManager::UseFakeUIForTests( |
1417 std::unique_ptr<FakeMediaStreamUIProxy> fake_ui) { | 1415 std::unique_ptr<FakeMediaStreamUIProxy> fake_ui) { |
1418 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1416 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1419 use_fake_ui_ = true; | 1417 use_fake_ui_ = true; |
1420 fake_ui_ = std::move(fake_ui); | 1418 fake_ui_ = std::move(fake_ui); |
1421 } | 1419 } |
1422 | 1420 |
1423 void MediaStreamManager::RegisterNativeLogCallback( | 1421 void MediaStreamManager::RegisterNativeLogCallback( |
1424 int renderer_host_id, | 1422 int renderer_host_id, |
1425 const base::Callback<void(const std::string&)>& callback) { | 1423 const base::Callback<void(const std::string&)>& callback) { |
1426 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 1424 BrowserThread::PostTask( |
1427 base::Bind(&MediaStreamManager::DoNativeLogCallbackRegistration, | 1425 BrowserThread::IO, FROM_HERE, |
1428 base::Unretained(this), renderer_host_id, callback)); | 1426 base::BindOnce(&MediaStreamManager::DoNativeLogCallbackRegistration, |
| 1427 base::Unretained(this), renderer_host_id, callback)); |
1429 } | 1428 } |
1430 | 1429 |
1431 void MediaStreamManager::UnregisterNativeLogCallback(int renderer_host_id) { | 1430 void MediaStreamManager::UnregisterNativeLogCallback(int renderer_host_id) { |
1432 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 1431 BrowserThread::PostTask( |
1433 base::Bind(&MediaStreamManager::DoNativeLogCallbackUnregistration, | 1432 BrowserThread::IO, FROM_HERE, |
1434 base::Unretained(this), renderer_host_id)); | 1433 base::BindOnce(&MediaStreamManager::DoNativeLogCallbackUnregistration, |
| 1434 base::Unretained(this), renderer_host_id)); |
1435 } | 1435 } |
1436 | 1436 |
1437 void MediaStreamManager::AddLogMessageOnIOThread(const std::string& message) { | 1437 void MediaStreamManager::AddLogMessageOnIOThread(const std::string& message) { |
1438 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1438 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1439 for (const auto& callback : log_callbacks_) | 1439 for (const auto& callback : log_callbacks_) |
1440 callback.second.Run(message); | 1440 callback.second.Run(message); |
1441 } | 1441 } |
1442 | 1442 |
1443 void MediaStreamManager::HandleAccessRequestResponse( | 1443 void MediaStreamManager::HandleAccessRequestResponse( |
1444 const std::string& label, | 1444 const std::string& label, |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1769 return devices; | 1769 return devices; |
1770 } | 1770 } |
1771 | 1771 |
1772 void MediaStreamManager::OnStreamStarted(const std::string& label) { | 1772 void MediaStreamManager::OnStreamStarted(const std::string& label) { |
1773 DeviceRequest* const request = FindRequest(label); | 1773 DeviceRequest* const request = FindRequest(label); |
1774 if (!request) | 1774 if (!request) |
1775 return; | 1775 return; |
1776 | 1776 |
1777 if (request->ui_proxy) { | 1777 if (request->ui_proxy) { |
1778 request->ui_proxy->OnStarted( | 1778 request->ui_proxy->OnStarted( |
1779 base::Bind(&MediaStreamManager::StopMediaStreamFromBrowser, | 1779 base::BindOnce(&MediaStreamManager::StopMediaStreamFromBrowser, |
1780 base::Unretained(this), label), | 1780 base::Unretained(this), label), |
1781 base::Bind(&MediaStreamManager::OnMediaStreamUIWindowId, | 1781 base::BindOnce(&MediaStreamManager::OnMediaStreamUIWindowId, |
1782 base::Unretained(this), request->video_type(), | 1782 base::Unretained(this), request->video_type(), |
1783 request->devices)); | 1783 request->devices)); |
1784 } | 1784 } |
1785 } | 1785 } |
1786 | 1786 |
1787 } // namespace content | 1787 } // namespace content |
OLD | NEW |