forked from bdlukaa/just_audio_windows
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP Send player messages from the UI thread
Fix for the following warning when AudioPlayer functions like setAudioSource are called: [ERROR:flutter/shell/common/shell.cc(1015)] The 'com.ryanheise.just_audio.events....' channel sent a message from native to Flutter on a non-platform thread. Platform channel messages must be sent on the platform thread. Failure to do so may result in data loss or crashes, and must be fixed in the plugin or application code creating that channel... WIP: - At some point Flutter will support sending messages from any thread. Tracking issue: flutter/flutter#93945 So this is mostly a stopgap solution. - Alternatively to the enable_shared_from_this pattern, posted messages could capture weak pointers to the event sink. This would behave subtly differently when the sink pointer changes between posting the message and executing the handler (i.e., upon any change, the handler would see an empty pointer. This is possibly more correct).
- Loading branch information
1 parent
1ffc510
commit a479139
Showing
4 changed files
with
127 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#pragma once | ||
|
||
#include <windows.h> | ||
|
||
#include <flutter/plugin_registrar_windows.h> | ||
|
||
#include <algorithm> | ||
#include <functional> | ||
#include <optional> | ||
#include <mutex> | ||
|
||
class JustAudioUiThreadHandler | ||
{ | ||
public: | ||
explicit JustAudioUiThreadHandler(flutter::PluginRegistrarWindows* registrar) | ||
: registrar_(registrar) { | ||
windowProcId_ = registrar_->RegisterTopLevelWindowProcDelegate( | ||
[this](HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { | ||
return HandleWindowMessage(hwnd, message, wparam, lparam); | ||
}); | ||
} | ||
|
||
~JustAudioUiThreadHandler() { | ||
registrar_->UnregisterTopLevelWindowProcDelegate(windowProcId_); | ||
} | ||
|
||
JustAudioUiThreadHandler(const JustAudioUiThreadHandler&) = delete; | ||
JustAudioUiThreadHandler& operator=(const JustAudioUiThreadHandler&) = delete; | ||
|
||
void Post(std::function<void()>&& func) | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
queuedFuncs_.emplace_back(std::move(func)); | ||
Notify(); | ||
} | ||
|
||
private: | ||
static const UINT kWmCallQueuedFunctions = WM_APP + 0x1d7; | ||
|
||
void Notify() { | ||
if (hwnd_ != 0) { | ||
PostMessage(hwnd_, kWmCallQueuedFunctions, 0, reinterpret_cast<LPARAM>(this)); | ||
} | ||
} | ||
|
||
std::optional<LRESULT>HandleWindowMessage( | ||
HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { | ||
if (hwnd_ == 0) { | ||
hwnd_ = hwnd; | ||
Notify(); // Make sure queued functions are processed | ||
} | ||
if (message == kWmCallQueuedFunctions && lparam == reinterpret_cast<LPARAM>(this)) { | ||
std::list<std::function<void()>> queuedFuncs; | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
std::swap(queuedFuncs_, queuedFuncs); | ||
} | ||
for (auto& func : queuedFuncs) { | ||
func(); | ||
} | ||
} | ||
return std::nullopt; | ||
} | ||
|
||
flutter::PluginRegistrarWindows* registrar_; | ||
int windowProcId_ = 0; | ||
HWND hwnd_ = 0; | ||
std::list<std::function<void()>> queuedFuncs_; | ||
std::mutex mutex_; | ||
}; |