webrtc-sdk / libwebrtc

A C++ wrapper for binary release, mainly used for flutter-webrtc desktop (windows, linux, embedded).
MIT License
378 stars 77 forks source link

How to listen for window closing #65

Closed nikohpng closed 5 months ago

nikohpng commented 1 year ago

Description

I want to get closing event of other process when I share my application screen. So, I can do someting.

Present Situation

I have listened process to get closing event, such like this

auto existing_thread = std::thread([this, source, desktop_capturer]() {
    unsigned long long source_pid =
        std::strtoull(source->id().std_string().c_str(), 0, 10);
    DWORD pid;
    GetWindowThreadProcessId((HWND)source_pid, &pid);
    HANDLE source_hdl =
        ::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION | SYNCHRONIZE, FALSE, (DWORD)pid);
    if (source_hdl == NULL) {
      std::cout << "Can't open process: " << source->id().std_string() 
                << ", cause is: " << GetLastError() << std::endl;
      return;
    }
    auto status = ::WaitForSingleObject((HANDLE)source_hdl, INFINITE);
    switch (status) {
      case WAIT_OBJECT_0: 
      case WAIT_TIMEOUT:  
        std::cout << "hello: close" << std::endl;
        OnStop(desktop_capturer);
        break;
      case WAIT_ABANDONED:
        std::cout << "abandoned" << std::endl;
      default:
        std::cout
            << "hello: set_client_confirm_event: WaitForSingleObject failed: "
            << GetLastError() << std::endl;
        break;
    }

     CloseHandle(source_hdl);
  });
  existing_thread.detach();

But, this code can't get closing event from chrome or wechat, unless we kill chrome in taskbar instead of closing the window.

Question

I think that this feature is necessary.

Thanks!

cloudwebrtc commented 1 year ago

maybe you can try to register a DesktopCapturerObserver to the libwebrtc::RTCDesktopCapturer. https://github.com/webrtc-sdk/libwebrtc/blob/fb0940cd9f3a65db115108774d5934fcdf5e4005/include/rtc_desktop_capturer.h#L37-L46

nikohpng commented 1 year ago

I have registed.

But I want to get OnStop event when chrome or wechat closed.

Now, OnStop only called by stop.

then I use WaitForSingleObject to get closing event of chrome or wechat

last, I can't get closing event

cloudwebrtc commented 1 year ago

Can you receive OnError when the window is closed or loses focus?

nikohpng commented 1 year ago
cloudwebrtc commented 1 year ago

I have no experience with accessing the window state when capturing a window, so still need to investigate :(