Skip to content

Commit 6e91f6f

Browse files
author
Sergej Jovanovic
authored
Merge pull request #14 from webrtc-uwp/Robin/20190206-queue-thread-check
Robin/20190206 queue thread check
2 parents 974fdea + 741b6d2 commit 6e91f6f

File tree

1 file changed

+64
-31
lines changed

1 file changed

+64
-31
lines changed

windows/wrapper/impl_webrtc_VideoCapturer.cpp

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,13 @@ namespace webrtc
103103
//-----------------------------------------------------------------------------
104104
DisplayOrientation::DisplayOrientation(DisplayOrientationListener* listener)
105105
: listener_(listener) {
106-
std::mutex mutex;
107-
std::condition_variable condition_variable;
108106
auto queue = UseWebrtcLib::delegateQueue();
109107
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()) {
115113
try {
116114
display_info_ = DisplayInformation::GetForCurrentView();
117115
orientation_ = display_info_.CurrentOrientation();
@@ -124,11 +122,28 @@ namespace webrtc
124122
orientation_ = winrt::Windows::Graphics::Display::DisplayOrientations::Portrait;
125123
RTC_LOG(LS_ERROR) << "DisplayOrientation could not be initialized.";
126124
}
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+
}
132147
}
133148
}
134149

@@ -523,24 +538,14 @@ namespace webrtc
523538
winrt::agile_ref<winrt::Windows::Media::Capture::MediaCapture>
524539
media_capture_agile = winrt::Windows::Media::Capture::MediaCapture();
525540

526-
std::mutex mutex;
527-
std::condition_variable condition_variable;
528541
auto queue = UseWebrtcLib::delegateQueue();
529542
ZS_ASSERT(queue);
530543

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;
533546
auto settings = MediaCaptureInitializationSettings();
534547
settings.VideoDeviceId(device_id_);
535548

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);
544549
initialize_async_task = Concurrency::create_task([this, media_capture_agile, settings]() {
545550
return media_capture_agile.get().InitializeAsync(settings).get();
546551
}).then([this, media_capture_agile](Concurrency::task<void> initTask) {
@@ -552,15 +557,43 @@ namespace webrtc
552557
<< rtc::ToUtf8(e.message().c_str());
553558
}
554559
});
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+
});
557589

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+
}
562594

563-
initialize_async_task.wait();
595+
initialize_async_task.wait();
596+
}
564597

565598
// Cache the MediaCapture object so we don't recreate it later.
566599
media_capture_ = media_capture_agile;

0 commit comments

Comments
 (0)