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

Side by Side Diff: content/browser/renderer_host/media/media_stream_manager.cc

Issue 2869733005: Convert some audio code to OnceCallback. (Closed)
Patch Set: Fix BindToCurrentLoop issues Created 3 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
OLDNEW
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
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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 MEDIA_DEVICE_ACCESS, controls, std::string()); 497 MEDIA_DEVICE_ACCESS, controls, std::string());
497 498
498 const std::string& label = AddRequest(request); 499 const std::string& label = AddRequest(request);
499 500
500 request->callback = callback; 501 request->callback = callback;
501 // Post a task and handle the request asynchronously. The reason is that the 502 // Post a task and handle the request asynchronously. The reason is that the
502 // requester won't have a label for the request until this function returns 503 // requester won't have a label for the request until this function returns
503 // and thus can not handle a response. Using base::Unretained is safe since 504 // and thus can not handle a response. Using base::Unretained is safe since
504 // MediaStreamManager is deleted on the UI thread, after the IO thread has 505 // MediaStreamManager is deleted on the UI thread, after the IO thread has
505 // been stopped. 506 // been stopped.
506 BrowserThread::PostTask( 507 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
507 BrowserThread::IO, FROM_HERE, 508 base::BindOnce(&MediaStreamManager::SetupRequest,
508 base::Bind(&MediaStreamManager::SetupRequest, 509 base::Unretained(this), label));
509 base::Unretained(this), label));
510 return label; 510 return label;
511 } 511 }
512 512
513 void MediaStreamManager::GenerateStream(MediaStreamRequester* requester, 513 void MediaStreamManager::GenerateStream(MediaStreamRequester* requester,
514 int render_process_id, 514 int render_process_id,
515 int render_frame_id, 515 int render_frame_id,
516 const std::string& salt, 516 const std::string& salt,
517 int page_request_id, 517 int page_request_id,
518 const StreamControls& controls, 518 const StreamControls& controls,
519 const url::Origin& security_origin, 519 const url::Origin& security_origin,
(...skipping 17 matching lines...) Expand all
537 FinalizeRequestFailed(label, request, MEDIA_DEVICE_INVALID_STATE); 537 FinalizeRequestFailed(label, request, MEDIA_DEVICE_INVALID_STATE);
538 } 538 }
539 return; 539 return;
540 } 540 }
541 541
542 // Post a task and handle the request asynchronously. The reason is that the 542 // Post a task and handle the request asynchronously. The reason is that the
543 // requester won't have a label for the request until this function returns 543 // requester won't have a label for the request until this function returns
544 // and thus can not handle a response. Using base::Unretained is safe since 544 // and thus can not handle a response. Using base::Unretained is safe since
545 // MediaStreamManager is deleted on the UI thread, after the IO thread has 545 // MediaStreamManager is deleted on the UI thread, after the IO thread has
546 // been stopped. 546 // been stopped.
547 BrowserThread::PostTask( 547 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
548 BrowserThread::IO, FROM_HERE, 548 base::BindOnce(&MediaStreamManager::SetupRequest,
549 base::Bind(&MediaStreamManager::SetupRequest, 549 base::Unretained(this), label));
550 base::Unretained(this), label));
551 } 550 }
552 551
553 void MediaStreamManager::CancelRequest(int render_process_id, 552 void MediaStreamManager::CancelRequest(int render_process_id,
554 int render_frame_id, 553 int render_frame_id,
555 int page_request_id) { 554 int page_request_id) {
556 DCHECK_CURRENTLY_ON(BrowserThread::IO); 555 DCHECK_CURRENTLY_ON(BrowserThread::IO);
557 for (const LabeledDeviceRequest& labeled_request : requests_) { 556 for (const LabeledDeviceRequest& labeled_request : requests_) {
558 DeviceRequest* const request = labeled_request.second; 557 DeviceRequest* const request = labeled_request.second;
559 if (request->requesting_process_id == render_process_id && 558 if (request->requesting_process_id == render_process_id &&
560 request->requesting_frame_id == render_frame_id && 559 request->requesting_frame_id == render_frame_id &&
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
733 page_request_id, security_origin, 732 page_request_id, security_origin,
734 false, // user gesture 733 false, // user gesture
735 MEDIA_OPEN_DEVICE_PEPPER_ONLY, controls, salt); 734 MEDIA_OPEN_DEVICE_PEPPER_ONLY, controls, salt);
736 735
737 const std::string& label = AddRequest(request); 736 const std::string& label = AddRequest(request);
738 // Post a task and handle the request asynchronously. The reason is that the 737 // Post a task and handle the request asynchronously. The reason is that the
739 // requester won't have a label for the request until this function returns 738 // requester won't have a label for the request until this function returns
740 // and thus can not handle a response. Using base::Unretained is safe since 739 // and thus can not handle a response. Using base::Unretained is safe since
741 // MediaStreamManager is deleted on the UI thread, after the IO thread has 740 // MediaStreamManager is deleted on the UI thread, after the IO thread has
742 // been stopped. 741 // been stopped.
743 BrowserThread::PostTask( 742 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
744 BrowserThread::IO, FROM_HERE, 743 base::BindOnce(&MediaStreamManager::SetupRequest,
745 base::Bind(&MediaStreamManager::SetupRequest, 744 base::Unretained(this), label));
746 base::Unretained(this), label));
747 } 745 }
748 746
749 bool MediaStreamManager::TranslateSourceIdToDeviceId( 747 bool MediaStreamManager::TranslateSourceIdToDeviceId(
750 MediaStreamType stream_type, 748 MediaStreamType stream_type,
751 const std::string& salt, 749 const std::string& salt,
752 const url::Origin& security_origin, 750 const url::Origin& security_origin,
753 const std::string& source_id, 751 const std::string& source_id,
754 std::string* device_id) const { 752 std::string* device_id) const {
755 DCHECK(stream_type == MEDIA_DEVICE_AUDIO_CAPTURE || 753 DCHECK(stream_type == MEDIA_DEVICE_AUDIO_CAPTURE ||
756 stream_type == MEDIA_DEVICE_VIDEO_CAPTURE); 754 stream_type == MEDIA_DEVICE_VIDEO_CAPTURE);
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
920 918
921 // Actual audio parameters are required only for MEDIA_TAB_AUDIO_CAPTURE. 919 // Actual audio parameters are required only for MEDIA_TAB_AUDIO_CAPTURE.
922 // TODO(guidou): MEDIA_TAB_AUDIO_CAPTURE should not be a special case. See 920 // TODO(guidou): MEDIA_TAB_AUDIO_CAPTURE should not be a special case. See
923 // crbug.com/584287. 921 // crbug.com/584287.
924 if (request->audio_type() == MEDIA_TAB_AUDIO_CAPTURE) { 922 if (request->audio_type() == MEDIA_TAB_AUDIO_CAPTURE) {
925 // Using base::Unretained is safe: |audio_system_| will post 923 // Using base::Unretained is safe: |audio_system_| will post
926 // PostRequestToUI() to IO thread, and MediaStreamManager is deleted on the 924 // PostRequestToUI() to IO thread, and MediaStreamManager is deleted on the
927 // UI thread, after the IO thread has been stopped. 925 // UI thread, after the IO thread has been stopped.
928 audio_system_->GetOutputStreamParameters( 926 audio_system_->GetOutputStreamParameters(
929 media::AudioDeviceDescription::kDefaultDeviceId, 927 media::AudioDeviceDescription::kDefaultDeviceId,
930 base::Bind(&MediaStreamManager::PostRequestToUI, base::Unretained(this), 928 base::BindOnce(&MediaStreamManager::PostRequestToUI,
931 label, request, enumeration)); 929 base::Unretained(this), label, request, enumeration));
932 } else { 930 } else {
933 PostRequestToUI(label, request, enumeration, media::AudioParameters()); 931 PostRequestToUI(label, request, enumeration, media::AudioParameters());
934 } 932 }
935 } 933 }
936 934
937 void MediaStreamManager::PostRequestToUI( 935 void MediaStreamManager::PostRequestToUI(
938 const std::string& label, 936 const std::string& label,
939 DeviceRequest* request, 937 DeviceRequest* request,
940 const MediaDeviceEnumeration& enumeration, 938 const MediaDeviceEnumeration& enumeration,
941 const media::AudioParameters& output_parameters) { 939 const media::AudioParameters& output_parameters) {
(...skipping 26 matching lines...) Expand all
968 966
969 fake_ui_->SetAvailableDevices(devices); 967 fake_ui_->SetAvailableDevices(devices);
970 968
971 request->ui_proxy = std::move(fake_ui_); 969 request->ui_proxy = std::move(fake_ui_);
972 } else { 970 } else {
973 request->ui_proxy = MediaStreamUIProxy::Create(); 971 request->ui_proxy = MediaStreamUIProxy::Create();
974 } 972 }
975 973
976 request->ui_proxy->RequestAccess( 974 request->ui_proxy->RequestAccess(
977 request->DetachUIRequest(), 975 request->DetachUIRequest(),
978 base::Bind(&MediaStreamManager::HandleAccessRequestResponse, 976 base::BindOnce(&MediaStreamManager::HandleAccessRequestResponse,
979 base::Unretained(this), label, output_parameters)); 977 base::Unretained(this), label, output_parameters));
980 } 978 }
981 979
982 void MediaStreamManager::SetupRequest(const std::string& label) { 980 void MediaStreamManager::SetupRequest(const std::string& label) {
983 DCHECK_CURRENTLY_ON(BrowserThread::IO); 981 DCHECK_CURRENTLY_ON(BrowserThread::IO);
984 DeviceRequest* request = FindRequest(label); 982 DeviceRequest* request = FindRequest(label);
985 if (!request) { 983 if (!request) {
986 DVLOG(1) << "SetupRequest label " << label << " doesn't exist!!"; 984 DVLOG(1) << "SetupRequest label " << label << " doesn't exist!!";
987 return; // This can happen if the request has been canceled. 985 return; // This can happen if the request has been canceled.
988 } 986 }
989 987
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
1229 } 1227 }
1230 1228
1231 void MediaStreamManager::InitializeMaybeAsync( 1229 void MediaStreamManager::InitializeMaybeAsync(
1232 std::unique_ptr<VideoCaptureProvider> video_capture_provider) { 1230 std::unique_ptr<VideoCaptureProvider> video_capture_provider) {
1233 // Some unit tests initialize the MSM in the IO thread and assume the 1231 // Some unit tests initialize the MSM in the IO thread and assume the
1234 // initialization is done synchronously. Other clients call this from a 1232 // initialization is done synchronously. Other clients call this from a
1235 // different thread and expect initialization to run asynchronously. 1233 // different thread and expect initialization to run asynchronously.
1236 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { 1234 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
1237 BrowserThread::PostTask( 1235 BrowserThread::PostTask(
1238 BrowserThread::IO, FROM_HERE, 1236 BrowserThread::IO, FROM_HERE,
1239 base::Bind(&MediaStreamManager::InitializeMaybeAsync, 1237 base::BindOnce(&MediaStreamManager::InitializeMaybeAsync,
1240 base::Unretained(this), 1238 base::Unretained(this),
1241 base::Passed(&video_capture_provider))); 1239 std::move(video_capture_provider)));
1242 return; 1240 return;
1243 } 1241 }
1244 1242
1245 // Store a pointer to |this| on the IO thread to avoid having to jump to the 1243 // Store a pointer to |this| on the IO thread to avoid having to jump to the
1246 // UI thread to fetch a pointer to the MSM. In particular on Android, it can 1244 // UI thread to fetch a pointer to the MSM. In particular on Android, it can
1247 // be problematic to post to a UI thread from arbitrary worker threads since 1245 // be problematic to post to a UI thread from arbitrary worker threads since
1248 // attaching to the VM is required and we may have to access the MSM from 1246 // attaching to the VM is required and we may have to access the MSM from
1249 // callback threads that we don't own and don't want to attach. 1247 // callback threads that we don't own and don't want to attach.
1250 g_media_stream_manager_tls_ptr.Pointer()->Set(this); 1248 g_media_stream_manager_tls_ptr.Pointer()->Set(this);
1251 1249
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
1411 void MediaStreamManager::UseFakeUIForTests( 1409 void MediaStreamManager::UseFakeUIForTests(
1412 std::unique_ptr<FakeMediaStreamUIProxy> fake_ui) { 1410 std::unique_ptr<FakeMediaStreamUIProxy> fake_ui) {
1413 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1411 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1414 use_fake_ui_ = true; 1412 use_fake_ui_ = true;
1415 fake_ui_ = std::move(fake_ui); 1413 fake_ui_ = std::move(fake_ui);
1416 } 1414 }
1417 1415
1418 void MediaStreamManager::RegisterNativeLogCallback( 1416 void MediaStreamManager::RegisterNativeLogCallback(
1419 int renderer_host_id, 1417 int renderer_host_id,
1420 const base::Callback<void(const std::string&)>& callback) { 1418 const base::Callback<void(const std::string&)>& callback) {
1421 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 1419 BrowserThread::PostTask(
1422 base::Bind(&MediaStreamManager::DoNativeLogCallbackRegistration, 1420 BrowserThread::IO, FROM_HERE,
1423 base::Unretained(this), renderer_host_id, callback)); 1421 base::BindOnce(&MediaStreamManager::DoNativeLogCallbackRegistration,
1422 base::Unretained(this), renderer_host_id, callback));
1424 } 1423 }
1425 1424
1426 void MediaStreamManager::UnregisterNativeLogCallback(int renderer_host_id) { 1425 void MediaStreamManager::UnregisterNativeLogCallback(int renderer_host_id) {
1427 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 1426 BrowserThread::PostTask(
1428 base::Bind(&MediaStreamManager::DoNativeLogCallbackUnregistration, 1427 BrowserThread::IO, FROM_HERE,
1429 base::Unretained(this), renderer_host_id)); 1428 base::BindOnce(&MediaStreamManager::DoNativeLogCallbackUnregistration,
1429 base::Unretained(this), renderer_host_id));
1430 } 1430 }
1431 1431
1432 void MediaStreamManager::AddLogMessageOnIOThread(const std::string& message) { 1432 void MediaStreamManager::AddLogMessageOnIOThread(const std::string& message) {
1433 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1433 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1434 for (const auto& callback : log_callbacks_) 1434 for (const auto& callback : log_callbacks_)
1435 callback.second.Run(message); 1435 callback.second.Run(message);
1436 } 1436 }
1437 1437
1438 void MediaStreamManager::HandleAccessRequestResponse( 1438 void MediaStreamManager::HandleAccessRequestResponse(
1439 const std::string& label, 1439 const std::string& label,
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
1764 return devices; 1764 return devices;
1765 } 1765 }
1766 1766
1767 void MediaStreamManager::OnStreamStarted(const std::string& label) { 1767 void MediaStreamManager::OnStreamStarted(const std::string& label) {
1768 DeviceRequest* const request = FindRequest(label); 1768 DeviceRequest* const request = FindRequest(label);
1769 if (!request) 1769 if (!request)
1770 return; 1770 return;
1771 1771
1772 if (request->ui_proxy) { 1772 if (request->ui_proxy) {
1773 request->ui_proxy->OnStarted( 1773 request->ui_proxy->OnStarted(
1774 base::Bind(&MediaStreamManager::StopMediaStreamFromBrowser, 1774 base::BindOnce(&MediaStreamManager::StopMediaStreamFromBrowser,
1775 base::Unretained(this), label), 1775 base::Unretained(this), label),
1776 base::Bind(&MediaStreamManager::OnMediaStreamUIWindowId, 1776 base::BindOnce(&MediaStreamManager::OnMediaStreamUIWindowId,
1777 base::Unretained(this), request->video_type(), 1777 base::Unretained(this), request->video_type(),
1778 request->devices)); 1778 request->devices));
1779 } 1779 }
1780 } 1780 }
1781 1781
1782 } // namespace content 1782 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698