-
Notifications
You must be signed in to change notification settings - Fork 1
/
instance.go
654 lines (530 loc) · 15.8 KB
/
instance.go
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
// license. Its contents can be found at:
// http://creativecommons.org/publicdomain/zero/1.0
package vlc
// #include "glue.h"
import "C"
import (
"unsafe"
)
// A single libvlc instance.
type Instance struct {
ptr *C.libvlc_instance_t
}
// New creates and initializes a new VLC instance with the given parameters.
// Returns nil and a possible error if no instance could be created.
func New(argv []string) (i *Instance, err error) {
cstr := make([]*C.char, len(argv))
for i := range cstr {
cstr[i] = C.CString(argv[i])
}
if c := C.libvlc_new(C.int(len(argv)), *(***C.char)(unsafe.Pointer(&cstr))); c != nil {
i = &Instance{c}
} else {
err = checkError()
}
for i := range cstr {
C.free(unsafe.Pointer(cstr[i]))
}
return
}
// Retain increments the reference count of the Instance.
// The initial reference count is 1 after vlc.New() returns.
func (this *Instance) Retain() (err error) {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
C.libvlc_retain(this.ptr)
return
}
// Release decreases the reference count of the instance and destroys it
// when it reaches zero.
func (this *Instance) Release() (err error) {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
C.libvlc_release(this.ptr)
return
}
// StartUI tries to start a user interface for the Instance.
// Specify an empty name to use the default.
func (this *Instance) StartUI(name string) (err error) {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
c := C.CString(name)
defer C.free(unsafe.Pointer(c))
if C.libvlc_add_intf(this.ptr, c) < 0 {
err = checkError()
}
return
}
// SetName sets the human-readable application name (e.g. "FooBar player 1.2.3")
// and user-agent name (e.g. "FooBar/1.2.3 Python/2.6.0"). LibVLC passes this as
// the user agent string when a protocol requires it.
func (this *Instance) SetName(appname, httpname string) (err error) {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
ca := C.CString(appname)
ch := C.CString(httpname)
C.libvlc_set_user_agent(this.ptr, ca, ch)
C.free(unsafe.Pointer(ca))
C.free(unsafe.Pointer(ch))
return
}
// Wait waits until an interface causes the instance to exit.
// You should start at least one interface first, using Instance.StartUI().
func (this *Instance) Wait() error {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
C.libvlc_wait(this.ptr)
return nil
}
// TODO(henrym): Log API has changed. Left here as a reminder to update to new libvlc log API
//
// LogVerbosity returns the VLC messaging verbosity level.
//func (this *Instance) LogVerbosity() uint {
// if this.ptr == nil {
// return 0
// }
// return uint(C.libvlc_get_log_verbosity(this.ptr))
//}
// SetLogVerbosity sets the VLC messaging verbosity level.
//func (this *Instance) SetLogVerbosity(v uint) {
// if this.ptr != nil {
// C.libvlc_set_log_verbosity(this.ptr, C.uint(v))
// }
//}
// OpenLog opens a VLC message log instance.
//func (this *Instance) OpenLog() (*Log, error) {
// if this.ptr == nil {
// return nil, os.EINVAL
// }
//
// if c := C.libvlc_log_open(this.ptr); c != nil {
// l := new(Log)
// l.fromC(c)
// return l, nil
// }
//
// return nil, checkError()
//}
// OpenMediaUri loads a media instance from the given uri.
func (this *Instance) OpenMediaUri(uri string) (*Media, error) {
if this.ptr == nil {
return nil, &VLCError{"Instance is nil"}
}
c := C.CString(uri)
defer C.free(unsafe.Pointer(c))
if m := C.libvlc_media_new_location(this.ptr, c); m != nil {
return &Media{m}, nil
}
return nil, checkError()
}
// OpenMediaFile loads a media instance from the given filesystem path.
func (this *Instance) OpenMediaFile(path string) (*Media, error) {
if this.ptr == nil {
return nil, &VLCError{"Instance is nil"}
}
c := C.CString(path)
defer C.free(unsafe.Pointer(c))
if m := C.libvlc_media_new_path(this.ptr, c); m != nil {
return &Media{m}, nil
}
return nil, checkError()
}
// OpenMediaFd creates a media instance for an open file descriptor.
// The file descriptor shall be open for reading (or reading and writing).
//
// Regular file descriptors, pipe read descriptors and character device
// descriptors (including TTYs) are supported on all platforms.
// Block device descriptors are supported where available.
// Directory descriptors are supported on systems that provide fdopendir().
// Sockets are supported on all platforms where they are file descriptors,
// i.e. all except Windows.
//
// Note: This library will /not/ automatically close the file descriptor
// under any circumstance. Nevertheless, a file descriptor can usually only be
// rendered once in a media player. To render it a second time, the file
// descriptor should probably be rewound to the beginning with lseek().
func (this *Instance) OpenMediaFd(fd int) (*Media, error) {
if this.ptr == nil {
return nil, &VLCError{"Instance is nil"}
}
if m := C.libvlc_media_new_fd(this.ptr, C.int(fd)); m != nil {
return &Media{m}, nil
}
return nil, checkError()
}
// OpenMediaNode creates a media instance as an empty node with a given name.
func (this *Instance) OpenMediaNode(name string) (*Media, error) {
if this.ptr == nil {
return nil, &VLCError{"Instance is nil"}
}
c := C.CString(name)
defer C.free(unsafe.Pointer(c))
if m := C.libvlc_media_new_as_node(this.ptr, c); m != nil {
return &Media{m}, nil
}
return nil, checkError()
}
// NewPlayer creates an empty media player object.
func (this *Instance) NewPlayer() (*Player, error) {
if this.ptr == nil {
return nil, &VLCError{"Instance is nil"}
}
if c := C.libvlc_media_player_new(this.ptr); c != nil {
return &Player{c}, nil
}
return nil, checkError()
}
// NewList creates and initializes a new media list.
func (this *Instance) NewList() (*MediaList, error) {
if this.ptr == nil {
return nil, &VLCError{"Instance is nil"}
}
if c := C.libvlc_media_list_new(this.ptr); c != nil {
return &MediaList{c}, nil
}
return nil, checkError()
}
// NewListPlayer creates an empty media list player object.
func (this *Instance) NewListPlayer() (*ListPlayer, error) {
if this.ptr == nil {
return nil, &VLCError{"Instance is nil"}
}
if c := C.libvlc_media_list_player_new(this.ptr); c != nil {
return &ListPlayer{c}, nil
}
return nil, checkError()
}
// NewLibrary creates an empty media library.
func (this *Instance) NewLibrary() (*Library, error) {
if this.ptr == nil {
return nil, &VLCError{"Instance is nil"}
}
if c := C.libvlc_media_library_new(this.ptr); c != nil {
return &Library{c}, nil
}
return nil, checkError()
}
// Discoverer creates a new discover media service by name.
func (this *Instance) Discoverer(name string) (*Discoverer, error) {
if this.ptr == nil {
return nil, &VLCError{"Instance is nil"}
}
s := C.CString(name)
defer C.free(unsafe.Pointer(s))
if c := C.libvlc_media_discoverer_new_from_name(this.ptr, s); c != nil {
return &Discoverer{c}, nil
}
return nil, checkError()
}
// VlmRelease releases the vlm instance associated with this instance.
func (this *Instance) VlmRelease() error {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
C.libvlc_vlm_release(this.ptr)
return checkError()
}
// VlmAddBroadcast adds a broadcast with given input.
//
// name: The name of the new broadcast.
// input: The input MRL.
// output: The output MRL (the parameter to the "sout" variable).
// options: Additional options.
// enabled: Enable the new broadcast?
// loop: Should this broadcast be played in loop?
//
func (this *Instance) VlmAddBroadcast(name, input, output string, options []string, enabled, loop bool) error {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
a := C.CString(name)
b := C.CString(input)
c := C.CString(output)
d := C.int(len(options))
e := make([]*C.char, len(options))
for i := range e {
e[i] = C.CString(options[i])
}
var f, g C.int
if enabled {
f = 1
}
if loop {
g = 1
}
C.libvlc_vlm_add_broadcast(this.ptr, a, b, c, d, *(***C.char)(unsafe.Pointer(&e)), f, g)
C.free(unsafe.Pointer(a))
C.free(unsafe.Pointer(b))
C.free(unsafe.Pointer(c))
for i := range e {
C.free(unsafe.Pointer(e[i]))
}
return checkError()
}
// VlmAddVOD adds a VOD with given input.
//
// name: The name of the new broadcast.
// input: The input MRL.
// options: Additional options.
// mux: The muxer of the vod media.
// enabled: Enable the new broadcast?
//
func (this *Instance) VlmAddVOD(name, input, output, mux string, options []string, enabled bool) error {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
a := C.CString(name)
b := C.CString(input)
c := C.int(len(options))
d := make([]*C.char, len(options))
for i := range d {
d[i] = C.CString(options[i])
}
var e C.int
if enabled {
e = 1
}
f := C.CString(mux)
C.libvlc_vlm_add_vod(this.ptr, a, b, c, *(***C.char)(unsafe.Pointer(&d)), e, f)
C.free(unsafe.Pointer(a))
C.free(unsafe.Pointer(b))
for i := range d {
C.free(unsafe.Pointer(d[i]))
}
C.free(unsafe.Pointer(f))
return checkError()
}
// VlmDelete deletes the given media (VOD or broadcast).
func (this *Instance) VlmDelete(name string) error {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
c := C.CString(name)
C.libvlc_vlm_del_media(this.ptr, c)
C.free(unsafe.Pointer(c))
return checkError()
}
// VlmSetEnabled enables or disables the given media (VOD or broadcast).
func (this *Instance) VlmSetEnabled(name string, toggle bool) error {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
c := C.CString(name)
if toggle {
C.libvlc_vlm_set_enabled(this.ptr, c, 1)
} else {
C.libvlc_vlm_set_enabled(this.ptr, c, 0)
}
C.free(unsafe.Pointer(c))
return checkError()
}
// VlmSetLoop enables or disables the given media's loop state (VOD or broadcast).
func (this *Instance) VlmSetLoop(name string, toggle bool) error {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
c := C.CString(name)
if toggle {
C.libvlc_vlm_set_loop(this.ptr, c, 1)
} else {
C.libvlc_vlm_set_loop(this.ptr, c, 0)
}
C.free(unsafe.Pointer(c))
return checkError()
}
// VlmSetOutput sets the output for the given media (VOD or broadcast).
func (this *Instance) VlmSetOutput(name, output string) error {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
a := C.CString(name)
b := C.CString(output)
C.libvlc_vlm_set_output(this.ptr, a, b)
C.free(unsafe.Pointer(a))
C.free(unsafe.Pointer(b))
return checkError()
}
// VlmSetInput sets the input MRL for the given media (VOD or broadcast).
// This will delete all existing inputs and add the specified one.
func (this *Instance) VlmSetInput(name, input string) error {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
a := C.CString(name)
b := C.CString(input)
C.libvlc_vlm_set_input(this.ptr, a, b)
C.free(unsafe.Pointer(a))
C.free(unsafe.Pointer(b))
return checkError()
}
// VlmAddInput adds an input MRL for the given media (VOD or broadcast).
func (this *Instance) VlmAddInput(name, input string) error {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
a := C.CString(name)
b := C.CString(input)
C.libvlc_vlm_add_input(this.ptr, a, b)
C.free(unsafe.Pointer(a))
C.free(unsafe.Pointer(b))
return checkError()
}
// VlmSetMux sets a media's VOD muxer.
func (this *Instance) VlmSetMux(name, mux string) error {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
a := C.CString(name)
b := C.CString(mux)
C.libvlc_vlm_set_mux(this.ptr, a, b)
C.free(unsafe.Pointer(a))
C.free(unsafe.Pointer(b))
return checkError()
}
// VlmChangeMedia edits the parameters of a media. This will delete all existing
// inputs and add the specified one.
//
// name: The name of the new broadcast.
// input: The input MRL.
// output: The output MRL (the parameter to the "sout" variable).
// options: Additional options.
// enabled: Enable the new broadcast?
// loop: Should this broadcast be played in loop?
//
func (this *Instance) VlmChangeMedia(name, input, output string, options []string, enabled, loop bool) error {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
a := C.CString(name)
b := C.CString(input)
c := C.CString(output)
d := C.int(len(options))
e := make([]*C.char, len(options))
for i := range e {
e[i] = C.CString(options[i])
}
var f, g C.int
if enabled {
f = 1
}
if loop {
g = 1
}
C.libvlc_vlm_change_media(this.ptr, a, b, c, d, *(***C.char)(unsafe.Pointer(&e)), f, g)
C.free(unsafe.Pointer(a))
C.free(unsafe.Pointer(b))
C.free(unsafe.Pointer(c))
for i := range e {
C.free(unsafe.Pointer(e[i]))
}
return checkError()
}
// VlmPlay plays the named broadcast.
func (this *Instance) VlmPlay(name string) error {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
a := C.CString(name)
C.libvlc_vlm_play_media(this.ptr, a)
C.free(unsafe.Pointer(a))
return checkError()
}
// VlmStop halts playback of the named broadcast.
func (this *Instance) VlmStop(name string) error {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
a := C.CString(name)
C.libvlc_vlm_stop_media(this.ptr, a)
C.free(unsafe.Pointer(a))
return checkError()
}
// VlmPause pauses playback of the named broadcast.
func (this *Instance) VlmPause(name string) error {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
a := C.CString(name)
C.libvlc_vlm_pause_media(this.ptr, a)
C.free(unsafe.Pointer(a))
return checkError()
}
// VlmSeek seeks in the named broadcast.
func (this *Instance) VlmSeek(name string, percentage float32) error {
if this.ptr == nil {
return &VLCError{"Instance is nil"}
}
a := C.CString(name)
C.libvlc_vlm_seek_media(this.ptr, a, C.float(percentage))
C.free(unsafe.Pointer(a))
return checkError()
}
// VlmMediaInfo returns information about the named media as a JSON string.
//
// Note: This function is mainly intended for debugging use,
func (this *Instance) VlmMediaInfo(name string) (s string, err error) {
if this.ptr == nil {
return "", &VLCError{"Instance is nil"}
}
a := C.CString(name)
if c := C.libvlc_vlm_show_media(this.ptr, a); c != nil {
s = C.GoString(c)
C.free(unsafe.Pointer(c))
} else {
err = checkError()
}
C.free(unsafe.Pointer(a))
return
}
// VlmPosition returns the instance position by name or instance id.
func (this *Instance) VlmPosition(name string, id int) (float32, error) {
if this.ptr == nil {
return 0, &VLCError{"Instance is nil"}
}
a := C.CString(name)
defer C.free(unsafe.Pointer(a))
return float32(C.libvlc_vlm_get_media_instance_position(this.ptr, a, C.int(id))), checkError()
}
// VlmTime returns the instance time by name or instance id.
func (this *Instance) VlmTime(name string, id int) (int, error) {
if this.ptr == nil {
return 0, &VLCError{"Instance is nil"}
}
a := C.CString(name)
defer C.free(unsafe.Pointer(a))
return int(C.libvlc_vlm_get_media_instance_time(this.ptr, a, C.int(id))), checkError()
}
// VlmLength returns the instance length by name or instance id.
func (this *Instance) VlmLength(name string, id int) (int, error) {
if this.ptr == nil {
return 0, &VLCError{"Instance is nil"}
}
a := C.CString(name)
defer C.free(unsafe.Pointer(a))
return int(C.libvlc_vlm_get_media_instance_length(this.ptr, a, C.int(id))), checkError()
}
// VlmRate returns the instance playback rate by name or instance id.
func (this *Instance) VlmRate(name string, id int) (int, error) {
if this.ptr == nil {
return 0, &VLCError{"Instance is nil"}
}
a := C.CString(name)
defer C.free(unsafe.Pointer(a))
return int(C.libvlc_vlm_get_media_instance_rate(this.ptr, a, C.int(id))), checkError()
}
// VlmEvents returns an event manager for a VLM instance.
func (this *Instance) VlmEvents() (*EventManager, error) {
if this.ptr == nil {
return nil, &VLCError{"Instance is nil"}
}
if c := C.libvlc_vlm_get_event_manager(this.ptr); c != nil {
return NewEventManager(c), nil
}
return nil, checkError()
}