Skip to content

Robin/20190206 queue thread check #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 64 additions & 31 deletions windows/wrapper/impl_webrtc_VideoCapturer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,13 @@ namespace webrtc
//-----------------------------------------------------------------------------
DisplayOrientation::DisplayOrientation(DisplayOrientationListener* listener)
: listener_(listener) {
std::mutex mutex;
std::condition_variable condition_variable;
auto queue = UseWebrtcLib::delegateQueue();
ZS_ASSERT(queue);
queue->postClosure([this, &condition_variable]() {
// GetForCurrentView() only works on a thread associated with
// a CoreWindow. If this doesn't work because we're running in
// a background task then the orientation needs to come from the
// foreground as a notification.
// GetForCurrentView() only works on a thread associated with
// a CoreWindow. If this doesn't work because we're running in
// a background task then the orientation needs to come from the
// foreground as a notification.
if (queue->isCurrentThread()) {
try {
display_info_ = DisplayInformation::GetForCurrentView();
orientation_ = display_info_.CurrentOrientation();
Expand All @@ -124,11 +122,28 @@ namespace webrtc
orientation_ = winrt::Windows::Graphics::Display::DisplayOrientations::Portrait;
RTC_LOG(LS_ERROR) << "DisplayOrientation could not be initialized.";
}
condition_variable.notify_one();
});
{
std::unique_lock<std::mutex> lock(mutex);
condition_variable.wait(lock);
} else {
std::mutex mutex;
std::condition_variable condition_variable;
queue->postClosure([this, &condition_variable]() {
try {
display_info_ = DisplayInformation::GetForCurrentView();
orientation_ = display_info_.CurrentOrientation();
orientation_changed_registration_token_ =
display_info_.OrientationChanged(
TypedEventHandler<DisplayInformation,
winrt::Windows::Foundation::IInspectable>(this, &DisplayOrientation::OnOrientationChanged));
} catch (...) {
display_info_ = nullptr;
orientation_ = winrt::Windows::Graphics::Display::DisplayOrientations::Portrait;
RTC_LOG(LS_ERROR) << "DisplayOrientation could not be initialized.";
}
condition_variable.notify_one();
});
{
std::unique_lock<std::mutex> lock(mutex);
condition_variable.wait(lock);
}
}
}

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

std::mutex mutex;
std::condition_variable condition_variable;
auto queue = UseWebrtcLib::delegateQueue();
ZS_ASSERT(queue);

Concurrency::task<void> initialize_async_task;
queue->postClosure([this, &initialize_async_task, media_capture_agile, &condition_variable]() {
if (queue->isCurrentThread()) {
Concurrency::task<void> initialize_async_task;
auto settings = MediaCaptureInitializationSettings();
settings.VideoDeviceId(device_id_);

// If Communications media category is configured, the
// GetAvailableMediaStreamProperties will report only H264 frame format
// for some devices (ex: Surface Pro 3). Since at the moment, WebRTC does
// not support receiving H264 frames from capturer, the Communications
// category is not configured.

// settings.MediaCategory(
// winrt::Windows::Media::Capture::MediaCategory::Communications);
initialize_async_task = Concurrency::create_task([this, media_capture_agile, settings]() {
return media_capture_agile.get().InitializeAsync(settings).get();
}).then([this, media_capture_agile](Concurrency::task<void> initTask) {
Expand All @@ -552,15 +557,43 @@ namespace webrtc
<< rtc::ToUtf8(e.message().c_str());
}
});
condition_variable.notify_one();
});
} else {
std::mutex mutex;
std::condition_variable condition_variable;
Concurrency::task<void> initialize_async_task;
queue->postClosure([this, &initialize_async_task, media_capture_agile, &condition_variable]() {
auto settings = MediaCaptureInitializationSettings();
settings.VideoDeviceId(device_id_);

// If Communications media category is configured, the
// GetAvailableMediaStreamProperties will report only H264 frame format
// for some devices (ex: Surface Pro 3). Since at the moment, WebRTC does
// not support receiving H264 frames from capturer, the Communications
// category is not configured.
// settings.MediaCategory(
// winrt::Windows::Media::Capture::MediaCategory::Communications);

initialize_async_task = Concurrency::create_task([this, media_capture_agile, settings]() {
return media_capture_agile.get().InitializeAsync(settings).get();
}).then([this, media_capture_agile](Concurrency::task<void> initTask) {
try {
initTask.get();
} catch (winrt::hresult_error const& e) {
RTC_LOG(LS_ERROR)
<< "Failed to initialize media capture device. "
<< rtc::ToUtf8(e.message().c_str());
}
});
condition_variable.notify_one();
});

{
std::unique_lock<std::mutex> lock(mutex);
condition_variable.wait(lock);
}
{
std::unique_lock<std::mutex> lock(mutex);
condition_variable.wait(lock);
}

initialize_async_task.wait();
initialize_async_task.wait();
}

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