8000 fix: add task runner for linux. (#1821) · flutter-webrtc/flutter-webrtc@2489b79 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2489b79

Browse files
authored
fix: add task runner for linux. (#1821)
* fix: add task runner for linux. * update.
1 parent 04ccd08 commit 2489b79

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

linux/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ add_library(${PLUGIN_NAME} SHARED
2424
"flutter/core_implementations.cc"
2525
"flutter/standard_codec.cc"
2626
"flutter/plugin_registrar.cc"
27+
"task_runner_linux.cc"
2728
)
2829

2930
include_directories(

linux/flutter_webrtc_plugin.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "flutter_common.h"
44
#include "flutter_webrtc.h"
5+
#include "task_runner_linux.h"
56

67
const char* kChannelName = "FlutterWebRTC.Method";
78

@@ -37,15 +38,16 @@ class FlutterWebRTCPluginImpl : public FlutterWebRTCPlugin {
3738

3839
TextureRegistrar* textures() { return textures_; }
3940

40-
TaskRunner* task_runner() { return nullptr; }
41+
TaskRunner* task_runner() { return task_runner_.get(); }
4142

4243
private:
4344
// Creates a plugin that communicates on the given channel.
4445
FlutterWebRTCPluginImpl(PluginRegistrar* registrar,
4546
std::unique_ptr<MethodChannel> channel)
4647
: channel_(std::move(channel)),
4748
messenger_(registrar->messenger()),
48-
textures_(registrar->texture_registrar()) {
49+
textures_(registrar->texture_registrar()),
50+
task_runner_(std::make_unique<TaskRunnerLinux>()) {
4951
webrtc_ = std::make_unique<FlutterWebRTC>(this);
5052
}
5153

@@ -63,6 +65,7 @@ class FlutterWebRTCPluginImpl : public FlutterWebRTCPlugin {
6365
std::unique_ptr<FlutterWebRTC> webrtc_;
6466
BinaryMessenger* messenger_;
6567
TextureRegistrar* textures_;
68+
std::unique_ptr<TaskRunner> task_runner_;
6669
};
6770

6871
} // namespace flutter_webrtc_plugin

linux/task_runner_linux.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "task_runner_linux.h"
2+
3+
#include <glib.h>
4+
5+
namespace flutter_webrtc_plugin {
6+
7+
void TaskRunnerLinux::EnqueueTask(TaskClosure task) {
8+
{
9+
std::lock_guard<std::mutex> lock(tasks_mutex_);
10+
tasks_.push(std::move(task));
11+
}
12+
13+
GMainContext* context = g_main_context_default();
14+
if (context) {
15+
g_main_context_invoke(
16+
context,
17+
[](gpointer user_data) -> gboolean {
18+
TaskRunnerLinux* runner = static_cast<TaskRunnerLinux*>(user_data);
19+
std::lock_guard<std::mutex> lock(runner->tasks_mutex_);
20+
while (!runner->tasks_.empty()) {
21+
TaskClosure task = std::move(runner->tasks_.front());
22+
runner->tasks_.pop();
23+
task();
24+
}
25+
return G_SOURCE_REMOVE;
26+
},
27+
this);
28+
}
29+
}
30+
31+
} // namespace flutter_webrtc_plugin

linux/task_runner_linux.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef PACKAGES_FLUTTER_WEBRTC_LINUX_TASK_RUNNER_LINUX_H_
2+
#define PACKAGES_FLUTTER_WEBRTC_LINUX_TASK_RUNNER_LINUX_H_
3+
4+
#include <memory>
5+
#include <mutex>
6+
#include <queue>
7+
#include "task_runner.h"
8+
9+
namespace flutter_webrtc_plugin {
10+
11+
class TaskRunnerLinux : public TaskRunner {
12+
public:
13+
TaskRunnerLinux() = default;
14+
~TaskRunnerLinux() override = default;
15+
16+
// TaskRunner implementation.
17+
void EnqueueTask(TaskClosure task) override;
18+
19+
private:
20+
std::mutex tasks_mutex_;
21+
std::queue<TaskClosure> tasks_;
22+
};
23+
24+
} // namespace flutter_webrtc_plugin
25+
26+
#endif // PACKAGES_FLUTTER_WEBRTC_LINUX_TASK_RUNNER_LINUX_H_

0 commit comments

Comments
 (0)
0