forked from alexmercerind/dart_vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.cc
357 lines (299 loc) · 11.1 KB
/
api.cc
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* dart_vlc: A media playback library for Dart & Flutter. Based on libVLC &
* libVLC++.
*
* Hitesh Kumar Saini
* https://github.com/alexmercerind
*
* GNU Lesser General Public License v2.1
*/
#include "api.h"
#include "broadcast.h"
#include "chromecast.h"
#include "device.h"
#include "equalizer.h"
#include "player.h"
#include "record.h"
namespace DartObjects {
struct DeviceList {
// The device list that gets exposed to Dart.
DartDeviceList dart_object;
// Backing data
std::vector<DartDeviceList::Device> device_infos;
std::vector<Device> devices;
};
struct Equalizer {
// The equalizer that gets exposed to Dart.
DartEqualizer dart_object;
// Backing data
std::vector<float> bands;
std::vector<float> amps;
};
static void DestroyObject(void*, void* peer) { delete peer; }
} // namespace DartObjects
#ifdef __cplusplus
extern "C" {
#endif
void PlayerCreate(int32_t id, int32_t video_width, int32_t video_height,
int32_t commandLineArgumentsCount,
const char** commandLineArguments) {
std::vector<std::string> args{};
for (int32_t index = 0; index < commandLineArgumentsCount; index++)
args.emplace_back(commandLineArguments[index]);
Player* player = g_players->Get(id, args);
if (video_width > 0 && video_height > 0) {
player->SetVideoWidth(video_width);
player->SetVideoHeight(video_height);
}
player->OnPlay([=]() -> void { OnPlayPauseStop(id, player->state()); });
player->OnPause([=]() -> void { OnPlayPauseStop(id, player->state()); });
player->OnStop([=]() -> void {
OnPlayPauseStop(id, player->state());
OnPosition(id, player->state());
});
player->OnComplete([=]() -> void { OnComplete(id, player->state()); });
player->OnVolume([=](float) -> void { OnVolume(id, player->state()); });
player->OnRate([=](float) -> void { OnRate(id, player->state()); });
player->OnPosition([=](int32_t) -> void { OnPosition(id, player->state()); });
player->OnOpen([=](VLC::Media) -> void { OnOpen(id, player->state()); });
player->OnPlaylist([=]() -> void { OnOpen(id, player->state()); });
player->OnBuffering(
[=](float buffering) -> void { OnBuffering(id, buffering); });
player->OnVideoDimensions(
[=](int32_t video_width, int32_t video_height) -> void {
OnVideoDimensions(id, video_width, video_height);
});
player->onError(
[=](std::string error) -> void { OnError(id, error.c_str()); });
}
void PlayerDispose(int32_t id) { g_players->Dispose(id); }
void PlayerOpen(int32_t id, bool auto_start, const char** source,
int32_t source_size) {
std::vector<std::shared_ptr<Media>> medias{};
Player* player = g_players->Get(id);
for (int32_t index = 0; index < 4 * source_size; index += 4) {
std::shared_ptr<Media> media;
const char* type = source[index];
const char* resource = source[index + 1];
const char* start_time = source[index + 2];
const char* stop_time = source[index + 3];
if (strcmp(type, "MediaType.file") == 0)
media = Media::file(resource, false, 10000, start_time, stop_time);
else if (strcmp(type, "MediaType.network") == 0)
media = Media::network(resource, false, 10000, start_time, stop_time);
else
media = Media::directShow(resource);
medias.emplace_back(media);
}
player->Open(std::make_shared<Playlist>(medias), auto_start);
}
void PlayerPlay(int32_t id) {
Player* player = g_players->Get(id);
player->Play();
}
void PlayerPause(int32_t id) {
Player* player = g_players->Get(id);
player->Pause();
}
void PlayerPlayOrPause(int32_t id) {
Player* player = g_players->Get(id);
player->PlayOrPause();
}
void PlayerStop(int32_t id) {
Player* player = g_players->Get(id);
player->Stop();
}
void PlayerNext(int32_t id) {
Player* player = g_players->Get(id);
player->Next();
}
void PlayerBack(int32_t id) {
Player* player = g_players->Get(id);
player->Back();
}
void PlayerJump(int32_t id, int32_t index) {
Player* player = g_players->Get(id);
player->Jump(index);
}
void PlayerSeek(int32_t id, int32_t position) {
Player* player = g_players->Get(id);
player->Seek(position);
}
void PlayerSetVolume(int32_t id, float volume) {
Player* player = g_players->Get(id);
player->SetVolume(volume);
}
void PlayerSetRate(int32_t id, float rate) {
Player* player = g_players->Get(id);
player->SetRate(rate);
}
void PlayerSetUserAgent(int32_t id, const char* userAgent) {
Player* player = g_players->Get(id);
player->SetUserAgent(userAgent);
}
void PlayerSetDevice(int32_t id, const char* device_id,
const char* device_name) {
Player* player = g_players->Get(id);
Device device(device_id, device_name);
player->SetDevice(device);
}
void PlayerSetEqualizer(int32_t id, int32_t equalizer_id) {
Player* player = g_players->Get(id);
Equalizer* equalizer = g_equalizers->Get(equalizer_id);
player->SetEqualizer(*equalizer);
}
void PlayerSetPlaylistMode(int32_t id, const char* mode) {
Player* player = g_players->Get(id);
PlaylistMode playlistMode;
if (strcmp(mode, "PlaylistMode.repeat") == 0)
playlistMode = PlaylistMode::repeat;
else if (strcmp(mode, "PlaylistMode.loop") == 0)
playlistMode = PlaylistMode::loop;
else
playlistMode = PlaylistMode::single;
player->SetPlaylistMode(playlistMode);
}
void PlayerAdd(int32_t id, const char* type, const char* resource) {
Player* player = g_players->Get(id);
std::shared_ptr<Media> media;
if (strcmp(type, "MediaType.file") == 0)
media = Media::file(resource, false);
else if (strcmp(type, "MediaType.network") == 0)
media = Media::network(resource, false);
else
media = Media::directShow(resource);
player->Add(media);
}
void PlayerRemove(int32_t id, int32_t index) {
Player* player = g_players->Get(id);
player->Remove(index);
}
void PlayerInsert(int32_t id, int32_t index, const char* type,
const char* resource) {
Player* player = g_players->Get(id);
std::shared_ptr<Media> media;
if (strcmp(type, "MediaType.file") == 0)
media = Media::file(resource, false);
else if (strcmp(type, "MediaType.network") == 0)
media = Media::network(resource, false);
else
media = Media::directShow(resource);
player->Insert(index, media);
}
void PlayerMove(int32_t id, int32_t initial_index, int32_t final_index) {
Player* player = g_players->Get(id);
player->Move(initial_index, final_index);
}
void PlayerTakeSnapshot(int32_t id, const char* file_path, int32_t width,
int32_t height) {
Player* player = g_players->Get(id);
player->TakeSnapshot(file_path, width, height);
}
void MediaClearMap(void*, void* peer) {
delete reinterpret_cast<std::map<std::string, std::string>*>(peer);
}
void MediaClearVector(void*, void* peer) {
delete reinterpret_cast<std::vector<const char*>*>(peer);
}
const char** MediaParse(Dart_Handle object, const char* type,
const char* resource, int32_t timeout) {
std::shared_ptr<Media> media = Media::create(type, resource, true, timeout);
auto metas = new std::map<std::string, std::string>(media->metas());
auto values = new std::vector<const char*>();
Dart_NewFinalizableHandle_DL(
object, reinterpret_cast<void*>(metas), sizeof(metas),
static_cast<Dart_HandleFinalizer>(MediaClearMap));
Dart_NewFinalizableHandle_DL(
object, reinterpret_cast<void*>(values), sizeof(values),
static_cast<Dart_HandleFinalizer>(MediaClearVector));
for (const auto & [ key, value ] : *metas) {
values->emplace_back(value.c_str());
}
return values->data();
}
void BroadcastCreate(int32_t id, const char* type, const char* resource,
const char* access, const char* mux, const char* dst,
const char* vcodec, int32_t vb, const char* acodec,
int32_t ab) {
std::shared_ptr<Media> media = Media::create(type, resource);
std::unique_ptr<BroadcastConfiguration> configuration =
std::make_unique<BroadcastConfiguration>(access, mux, dst, vcodec, vb,
acodec, ab);
g_broadcasts->Get(id, std::move(media), std::move(configuration));
}
void BroadcastStart(int32_t id) {
Broadcast* broadcast = g_broadcasts->Get(id, nullptr, nullptr);
broadcast->Start();
}
void BroadcastDispose(int32_t id) { g_broadcasts->Dispose(id); }
void ChromecastCreate(int32_t id, const char* type, const char* resource,
const char* ip_address) {
std::shared_ptr<Media> media = Media::create(type, resource);
chromecasts->Get(id, std::move(media), ip_address);
}
void ChromecastStart(int32_t id) {
Chromecast* chromecast = chromecasts->Get(id, nullptr, "");
chromecast->Start();
}
void ChromecastDispose(int32_t id) { chromecasts->Dispose(id); }
void RecordCreate(int32_t id, const char* saving_file, const char* type,
const char* resource) {
std::shared_ptr<Media> media = Media::create(type, resource);
g_records->Get(id, media, saving_file);
}
void RecordStart(int32_t id) {
Record* record = g_records->Get(id, nullptr, "");
record->Start();
}
void RecordDispose(int32_t id) { g_records->Dispose(id); }
DartDeviceList* DevicesAll(Dart_Handle object) {
auto wrapper = new DartObjects::DeviceList();
wrapper->devices = Devices::All();
for (const auto& device : wrapper->devices) {
wrapper->device_infos.emplace_back(device.name().c_str(),
device.id().c_str());
}
wrapper->dart_object.size = wrapper->device_infos.size();
wrapper->dart_object.device_infos = wrapper->device_infos.data();
Dart_NewFinalizableHandle_DL(
object, wrapper, sizeof(*wrapper),
static_cast<Dart_HandleFinalizer>(DartObjects::DestroyObject));
return &wrapper->dart_object;
}
static DartEqualizer* EqualizerToDart(const Equalizer* equalizer, int32_t id,
Dart_Handle dart_handle) {
auto wrapper = new DartObjects::Equalizer();
for (const auto & [ band, amp ] : equalizer->band_amps()) {
wrapper->bands.emplace_back(band);
wrapper->amps.emplace_back(amp);
}
wrapper->dart_object.id = id;
wrapper->dart_object.pre_amp = equalizer->pre_amp();
wrapper->dart_object.bands = wrapper->bands.data();
wrapper->dart_object.amps = wrapper->amps.data();
wrapper->dart_object.size = wrapper->amps.size();
Dart_NewFinalizableHandle_DL(
dart_handle, wrapper, sizeof(*wrapper),
static_cast<Dart_HandleFinalizer>(DartObjects::DestroyObject));
return &wrapper->dart_object;
}
struct DartEqualizer* EqualizerCreateEmpty(Dart_Handle object) {
int32_t id = g_equalizers->CreateEmpty();
Equalizer* equalizer = g_equalizers->Get(id);
return EqualizerToDart(equalizer, id, object);
}
struct DartEqualizer* EqualizerCreateMode(Dart_Handle object, int32_t mode) {
int32_t id = g_equalizers->CreateMode(static_cast<EqualizerMode>(mode));
Equalizer* equalizer = g_equalizers->Get(id);
return EqualizerToDart(equalizer, id, object);
}
void EqualizerSetBandAmp(int32_t id, float band, float amp) {
g_equalizers->Get(id)->SetBandAmp(band, amp);
}
void EqualizerSetPreAmp(int32_t id, float amp) {
g_equalizers->Get(id)->SetPreAmp(amp);
}
#ifdef __cplusplus
}
#endif