Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ add_library(${PLUGIN_NAME} SHARED
"flutter/core_implementations.cc"
"flutter/standard_codec.cc"
"flutter/plugin_registrar.cc"
"task_runner_linux.cc"
)

include_directories(
Expand Down
7 changes: 5 additions & 2 deletions linux/flutter_webrtc_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "flutter_common.h"
#include "flutter_webrtc.h"
#include "task_runner_linux.h"

const char* kChannelName = "FlutterWebRTC.Method";

Expand Down Expand Up @@ -37,15 +38,16 @@ class FlutterWebRTCPluginImpl : public FlutterWebRTCPlugin {

TextureRegistrar* textures() { return textures_; }

TaskRunner* task_runner() { return nullptr; }
TaskRunner* task_runner() { return task_runner_.get(); }

private:
// Creates a plugin that communicates on the given channel.
FlutterWebRTCPluginImpl(PluginRegistrar* registrar,
std::unique_ptr<MethodChannel> channel)
: channel_(std::move(channel)),
messenger_(registrar->messenger()),
textures_(registrar->texture_registrar()) {
textures_(registrar->texture_registrar()),
task_runner_(std::make_unique<TaskRunnerLinux>()) {
webrtc_ = std::make_unique<FlutterWebRTC>(this);
}

Expand All @@ -63,6 +65,7 @@ class FlutterWebRTCPluginImpl : public FlutterWebRTCPlugin {
std::unique_ptr<FlutterWebRTC> webrtc_;
BinaryMessenger* messenger_;
TextureRegistrar* textures_;
std::unique_ptr<TaskRunner> task_runner_;
};

} // namespace flutter_webrtc_plugin
Expand Down
31 changes: 31 additions & 0 deletions linux/task_runner_linux.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "task_runner_linux.h"

#include <glib.h>

namespace flutter_webrtc_plugin {

void TaskRunnerLinux::EnqueueTask(TaskClosure task) {
{
std::lock_guard<std::mutex> lock(tasks_mutex_);
tasks_.push(std::move(task));
}

GMainContext* context = g_main_context_default();
if (context) {
g_main_context_invoke(
context,
[](gpointer user_data) -> gboolean {
TaskRunnerLinux* runner = static_cast<TaskRunnerLinux*>(user_data);
std::lock_guard<std::mutex> lock(runner->tasks_mutex_);
while (!runner->tasks_.empty()) {
TaskClosure task = std::move(runner->tasks_.front());
runner->tasks_.pop();
task();
}
return G_SOURCE_REMOVE;
},
this);
}
}

} // namespace flutter_webrtc_plugin
26 changes: 26 additions & 0 deletions linux/task_runner_linux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef PACKAGES_FLUTTER_WEBRTC_LINUX_TASK_RUNNER_LINUX_H_
#define PACKAGES_FLUTTER_WEBRTC_LINUX_TASK_RUNNER_LINUX_H_

#include <memory>
#include <mutex>
#include <queue>
#include "task_runner.h"

namespace flutter_webrtc_plugin {

class TaskRunnerLinux : public TaskRunner {
public:
TaskRunnerLinux() = default;
~TaskRunnerLinux() override = default;

// TaskRunner implementation.
void EnqueueTask(TaskClosure task) override;

private:
std::mutex tasks_mutex_;
std::queue<TaskClosure> tasks_;
};

} // namespace flutter_webrtc_plugin

#endif // PACKAGES_FLUTTER_WEBRTC_LINUX_TASK_RUNNER_LINUX_H_