Skip to content
Navigation Menu
{{ message }}
forked from livekit/client-sdk-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlivekit_plugin.cpp
More file actions
287 lines (251 loc) · 9.72 KB
/
Copy pathlivekit_plugin.cpp
File metadata and controls
287 lines (251 loc) · 9.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2024 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "include/livekit_client/live_kit_plugin.h"
#include <flutter_linux/flutter_linux.h>
#include <gtk/gtk.h>
#include <sys/utsname.h>
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar.h>
#include <flutter/standard_method_codec.h>
#include <flutter_common.h>
#include <flutter_webrtc.h>
#include <flutter_webrtc/flutter_web_r_t_c_plugin.h>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <map>
#include <memory>
#include <sstream>
#include "audio_visualizer.h"
#include "task_runner_linux.h"
namespace livekit_client_plugin {
/// Centers the sorted bands by placing higher values in the middle.
std::vector<float> centerBands(const std::vector<float> &sortedBands) {
std::vector<float> centeredBands(sortedBands.size(), 0);
size_t leftIndex = sortedBands.size() / 2;
size_t rightIndex = leftIndex;
for (size_t index = 0; index < sortedBands.size(); ++index) {
if (index % 2 == 0) {
// Place value to the right
centeredBands[rightIndex] = sortedBands[index];
rightIndex += 1;
} else {
// Place value to the left
leftIndex -= 1;
centeredBands[leftIndex] = sortedBands[index];
}
}
return centeredBands;
}
class VisualizerSink : public libwebrtc::AudioTrackSink {
public:
VisualizerSink(BinaryMessenger *messenger, std::string event_channel_name,
libwebrtc::scoped_refptr<libwebrtc::RTCMediaTrack> media_track,
bool is_centered = false, int bar_count = 7)
: channel_(
std::make_unique<flutter::EventChannel<flutter::EncodableValue>>(
messenger, event_channel_name,
&flutter::StandardMethodCodec::GetInstance())),
media_track_(media_track), is_centered_(is_centered),
bar_count_(bar_count) {
task_runner_ = std::make_unique<livekit_client_plugin::TaskRunnerLinux>();
auto handler = std::make_unique<
flutter::StreamHandlerFunctions<flutter::EncodableValue>>(
[&](const flutter::EncodableValue *arguments,
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>
&&events)
-> std::unique_ptr<
flutter::StreamHandlerError<flutter::EncodableValue>> {
sink_ = std::move(events);
std::weak_ptr<flutter::EventSink<flutter::EncodableValue>> weak_sink =
sink_;
for (auto &event : event_queue_) {
PostEvent(event);
}
event_queue_.clear();
on_listen_called_ = true;
return nullptr;
},
[&](const flutter::EncodableValue *arguments)
-> std::unique_ptr<
flutter::StreamHandlerError<flutter::EncodableValue>> {
on_listen_called_ = false;
return nullptr;
});
channel_->SetStreamHandler(std::move(handler));
audio_visualizer_ =
std::make_unique<AudioVisualizer>(bar_count_, is_centered_);
((libwebrtc::RTCAudioTrack *)media_track_.get())->AddSink(this);
}
~VisualizerSink() override {}
public:
void OnData(const void *audio_data, int bits_per_sample, int sample_rate,
size_t number_of_channels, size_t number_of_frames) override {
if (!on_listen_called_) {
return;
}
std::vector<float> bands;
if (audio_visualizer_->Process((const int16_t *)audio_data,
(unsigned int)number_of_frames,
float(sample_rate), bands)) {
// Post the processed data to the event sink
EncodableList bands_list = EncodableList(bands.begin(), bands.end());
Success(EncodableValue(bands_list));
}
}
void Success(const flutter::EncodableValue &event, bool cache_event = true) {
if (on_listen_called_) {
PostEvent(event);
} else {
if (cache_event) {
event_queue_.push_back(event);
}
}
}
void PostEvent(const flutter::EncodableValue &event) {
if (task_runner_) {
std::weak_ptr<flutter::EventSink<EncodableValue>> weak_sink = sink_;
task_runner_->EnqueueTask([weak_sink, event]() {
auto sink = weak_sink.lock();
if (sink) {
sink->Success(event);
}
});
} else {
sink_->Success(event);
}
}
void RemoveSink() {
((libwebrtc::RTCAudioTrack *)media_track_.get())->RemoveSink(this);
}
private:
std::unique_ptr<AudioVisualizer> audio_visualizer_;
std::unique_ptr<livekit_client_plugin::TaskRunnerLinux> task_runner_;
std::unique_ptr<flutter::EventChannel<flutter::EncodableValue>> channel_;
std::shared_ptr<flutter::EventSink<flutter::EncodableValue>> sink_;
std::list<flutter::EncodableValue> event_queue_;
bool on_listen_called_ = false;
libwebrtc::scoped_refptr<libwebrtc::RTCMediaTrack> media_track_;
bool is_centered_ = false;
int bar_count_ = 7;
};
class LiveKitPlugin : public flutter::Plugin {
public:
static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar);
LiveKitPlugin(BinaryMessenger *messenger);
virtual ~LiveKitPlugin();
private:
// Called when a method is called on this plugin's channel from Dart.
void HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue> &method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
private:
flutter_webrtc_plugin::FlutterWebRTC *webrtc_instance_ = nullptr;
std::unordered_map<std::string, std::unique_ptr<VisualizerSink>> visualizers_;
BinaryMessenger *messenger_ = nullptr;
mutable std::mutex mutex_;
};
// static
void LiveKitPlugin::RegisterWithRegistrar(flutter::PluginRegistrar *registrar) {
auto channel =
std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
registrar->messenger(), "livekit_client",
&flutter::StandardMethodCodec::GetInstance());
auto plugin = std::make_unique<LiveKitPlugin>(registrar->messenger());
channel->SetMethodCallHandler(
[plugin_pointer = plugin.get()](const auto &call, auto result) {
plugin_pointer->HandleMethodCall(call, std::move(result));
});
registrar->AddPlugin(std::move(plugin));
}
LiveKitPlugin::LiveKitPlugin(BinaryMessenger *messenger)
: messenger_(messenger) {
webrtc_instance_ = flutter_webrtc_plugin_get_shared_instance();
}
LiveKitPlugin::~LiveKitPlugin() {}
void LiveKitPlugin::HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue> &method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
if (method_call.method_name().compare("startVisualizer") == 0) {
if (!method_call.arguments()) {
result->Error("Bad Arguments", "Null arguments received");
return;
}
flutter::EncodableMap params =
GetValue<flutter::EncodableMap>(*method_call.arguments());
std::string trackId = findString(params, "trackId");
std::string visualizerId = findString(params, "visualizerId");
int barCount = findInt(params, "barCount");
bool isCentered = findBoolean(params, "isCentered");
if (trackId.empty() || visualizerId.empty()) {
result->Error("Invalid Arguments",
"trackId and visualizerId are required");
return;
}
libwebrtc::scoped_refptr<libwebrtc::RTCMediaTrack> media_track =
webrtc_instance_->MediaTrackForId(trackId);
if (!media_track) {
result->Error("Track Not Found", "No media track found for the given ID");
return;
}
std::ostringstream oss;
oss << "io.livekit.audio.visualizer/eventChannel-" << trackId << "-"
<< visualizerId;
mutex_.lock();
visualizers_[visualizerId] = std::make_unique<VisualizerSink>(
messenger_, oss.str(), media_track, isCentered, barCount);
mutex_.unlock();
result->Success(flutter::EncodableValue(true));
} else if (method_call.method_name().compare("stopVisualizer") == 0) {
if (!method_call.arguments()) {
result->Error("Bad Arguments", "Null arguments received");
return;
}
flutter::EncodableMap args =
GetValue<flutter::EncodableMap>(*method_call.arguments());
std::string trackId = findString(args, "trackId");
std::string visualizerId = findString(args, "visualizerId");
if (trackId.empty() || visualizerId.empty()) {
result->Error("Invalid Arguments",
"trackId and visualizerId are required");
return;
}
libwebrtc::scoped_refptr<libwebrtc::RTCMediaTrack> media_track =
webrtc_instance_->MediaTrackForId(trackId);
if (!media_track) {
result->Error("Track Not Found", "No media track found for the given ID");
return;
}
mutex_.lock();
auto it = visualizers_.find(visualizerId);
if (it != visualizers_.end()) {
it->second->RemoveSink();
visualizers_.erase(it);
mutex_.unlock();
} else {
mutex_.unlock();
result->Error("Visualizer Not Found",
"No visualizer found for the given visualizerId");
return;
}
result->Success();
} else {
result->NotImplemented();
}
}
} // namespace livekit_client_plugin
void live_kit_plugin_register_with_registrar(FlPluginRegistrar *registrar) {
static auto *plugin_registrar = new flutter::PluginRegistrar(registrar);
livekit_client_plugin::LiveKitPlugin::RegisterWithRegistrar(plugin_registrar);
}
You can’t perform that action at this time.
