@@ -103,15 +103,13 @@ namespace webrtc
103
103
// -----------------------------------------------------------------------------
104
104
DisplayOrientation::DisplayOrientation (DisplayOrientationListener* listener)
105
105
: listener_(listener) {
106
- std::mutex mutex;
107
- std::condition_variable condition_variable;
108
106
auto queue = UseWebrtcLib::delegateQueue ();
109
107
ZS_ASSERT (queue);
110
- queue-> postClosure ([ this , &condition_variable] () {
111
- // GetForCurrentView() only works on a thread associated with
112
- // a CoreWindow. If this doesn't work because we're running in
113
- // a background task then the orientation needs to come from the
114
- // foreground as a notification.
108
+ // GetForCurrentView () only works on a thread associated with
109
+ // a CoreWindow. If this doesn't work because we're running in
110
+ // a background task then the orientation needs to come from the
111
+ // foreground as a notification.
112
+ if (queue-> isCurrentThread ()) {
115
113
try {
116
114
display_info_ = DisplayInformation::GetForCurrentView ();
117
115
orientation_ = display_info_.CurrentOrientation ();
@@ -124,11 +122,28 @@ namespace webrtc
124
122
orientation_ = winrt::Windows::Graphics::Display::DisplayOrientations::Portrait;
125
123
RTC_LOG (LS_ERROR) << " DisplayOrientation could not be initialized." ;
126
124
}
127
- condition_variable.notify_one ();
128
- });
129
- {
130
- std::unique_lock<std::mutex> lock (mutex);
131
- condition_variable.wait (lock);
125
+ } else {
126
+ std::mutex mutex;
127
+ std::condition_variable condition_variable;
128
+ queue->postClosure ([this , &condition_variable]() {
129
+ try {
130
+ display_info_ = DisplayInformation::GetForCurrentView ();
131
+ orientation_ = display_info_.CurrentOrientation ();
132
+ orientation_changed_registration_token_ =
133
+ display_info_.OrientationChanged (
134
+ TypedEventHandler<DisplayInformation,
135
+ winrt::Windows::Foundation::IInspectable>(this , &DisplayOrientation::OnOrientationChanged));
136
+ } catch (...) {
137
+ display_info_ = nullptr ;
138
+ orientation_ = winrt::Windows::Graphics::Display::DisplayOrientations::Portrait;
139
+ RTC_LOG (LS_ERROR) << " DisplayOrientation could not be initialized." ;
140
+ }
141
+ condition_variable.notify_one ();
142
+ });
143
+ {
144
+ std::unique_lock<std::mutex> lock (mutex);
145
+ condition_variable.wait (lock);
146
+ }
132
147
}
133
148
}
134
149
@@ -523,24 +538,14 @@ namespace webrtc
523
538
winrt::agile_ref<winrt::Windows::Media::Capture::MediaCapture>
524
539
media_capture_agile = winrt::Windows::Media::Capture::MediaCapture ();
525
540
526
- std::mutex mutex;
527
- std::condition_variable condition_variable;
528
541
auto queue = UseWebrtcLib::delegateQueue ();
529
542
ZS_ASSERT (queue);
530
543
531
- Concurrency::task< void > initialize_async_task;
532
- queue-> postClosure ([ this , &initialize_async_task, media_capture_agile, &condition_variable]() {
544
+ if (queue-> isCurrentThread ()) {
545
+ Concurrency::task< void > initialize_async_task;
533
546
auto settings = MediaCaptureInitializationSettings ();
534
547
settings.VideoDeviceId (device_id_);
535
548
536
- // If Communications media category is configured, the
537
- // GetAvailableMediaStreamProperties will report only H264 frame format
538
- // for some devices (ex: Surface Pro 3). Since at the moment, WebRTC does
539
- // not support receiving H264 frames from capturer, the Communications
540
- // category is not configured.
541
-
542
- // settings.MediaCategory(
543
- // winrt::Windows::Media::Capture::MediaCategory::Communications);
544
549
initialize_async_task = Concurrency::create_task ([this , media_capture_agile, settings]() {
545
550
return media_capture_agile.get ().InitializeAsync (settings).get ();
546
551
}).then ([this , media_capture_agile](Concurrency::task<void > initTask) {
@@ -552,15 +557,43 @@ namespace webrtc
552
557
<< rtc::ToUtf8 (e.message ().c_str ());
553
558
}
554
559
});
555
- condition_variable.notify_one ();
556
- });
560
+ } else {
561
+ std::mutex mutex;
562
+ std::condition_variable condition_variable;
563
+ Concurrency::task<void > initialize_async_task;
564
+ queue->postClosure ([this , &initialize_async_task, media_capture_agile, &condition_variable]() {
565
+ auto settings = MediaCaptureInitializationSettings ();
566
+ settings.VideoDeviceId (device_id_);
567
+
568
+ // If Communications media category is configured, the
569
+ // GetAvailableMediaStreamProperties will report only H264 frame format
570
+ // for some devices (ex: Surface Pro 3). Since at the moment, WebRTC does
571
+ // not support receiving H264 frames from capturer, the Communications
572
+ // category is not configured.
573
+ // settings.MediaCategory(
574
+ // winrt::Windows::Media::Capture::MediaCategory::Communications);
575
+
576
+ initialize_async_task = Concurrency::create_task ([this , media_capture_agile, settings]() {
577
+ return media_capture_agile.get ().InitializeAsync (settings).get ();
578
+ }).then ([this , media_capture_agile](Concurrency::task<void > initTask) {
579
+ try {
580
+ initTask.get ();
581
+ } catch (winrt::hresult_error const & e) {
582
+ RTC_LOG (LS_ERROR)
583
+ << " Failed to initialize media capture device. "
584
+ << rtc::ToUtf8 (e.message ().c_str ());
585
+ }
586
+ });
587
+ condition_variable.notify_one ();
588
+ });
557
589
558
- {
559
- std::unique_lock<std::mutex> lock (mutex);
560
- condition_variable.wait (lock);
561
- }
590
+ {
591
+ std::unique_lock<std::mutex> lock (mutex);
592
+ condition_variable.wait (lock);
593
+ }
562
594
563
- initialize_async_task.wait ();
595
+ initialize_async_task.wait ();
596
+ }
564
597
565
598
// Cache the MediaCapture object so we don't recreate it later.
566
599
media_capture_ = media_capture_agile;
0 commit comments