-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVServer.c
618 lines (522 loc) · 15.6 KB
/
AVServer.c
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
/*
FUSE fioc: FUSE ioctl example
Copyright (C) 2008 SUSE Linux Products GmbH
Copyright (C) 2008 Tejun Heo <teheo@suse.de>
This program can be distributed under the terms of the GNU GPL.
See the file COPYING.
*/
/** @file
* @tableofcontents
*
* This example illustrates how to write a FUSE file system that can
* process (a restricted set of) ioctls. It can be tested with the
* ioctl_client.c program.
*
* Compile with:
*
* arm-linux-gnueabi-gcc -Wall -D_FILE_OFFSET_BITS=64 -I./include AVServer.c -L../lib/.libs -lfuse3 -o AVServer
*
* Compile with:
*
* ./AVServer -f -o auto_unmount /tmp/AVServer
*
* ## Source code ##
* \include AVServer.c
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <version.h>
#include <AVServer.h>
#define AUDIO_DEV "audio0"
#define VIDEO_DEV "video0"
#define DVR_DEV "dvr"
#define PAINEL_DEV "panel"
#define ENCODER_DEV "encoder"
#define VIDEO_SET_FRAME_RATE _IO('o', 99)
static unsigned dvb_hisi_open_mask;
#define MAX_ENCODERS 3
static struct encoder_ops *encoders[MAX_ENCODERS];
enum {
DVB_NONE,
DVB_ROOT,
DVB_FILE,
DVB_AUDIO_DEV,
DVB_VIDEO_DEV,
DVB_DVR_DEV,
DVB_PAINEL_DEV,
DVB_ENCODER_DEV,
};
static int dvb_hisi_file_type(const char *path) {
if (strEquals((char*)path, "/", false))
return DVB_ROOT;
else if (strEquals((char*)path, "/" AUDIO_DEV, false))
return DVB_AUDIO_DEV;
else if (strEquals((char*)path, "/" VIDEO_DEV, false))
return DVB_VIDEO_DEV;
else if (strEquals((char*)path, "/" DVR_DEV, true))
return DVB_DVR_DEV;
else if (strEquals((char*)path, "/" PAINEL_DEV, false))
return DVB_PAINEL_DEV;
else if (strEquals((char*)path, "/" ENCODER_DEV, true))
return DVB_ENCODER_DEV;
return DVB_NONE;
}
static void *dvb_hisi_init(struct fuse_conn_info *conn, struct fuse_config *cfg) {
(void) conn;
(void) cfg;
struct class_ops *player = player_get_ops();
if (player && player->create())
return player;
return NULL;
}
static void dvb_hisi_destroy(void *private_data) {
struct class_ops *player = (struct class_ops *)private_data;
if (player)
player->destroy();
}
static int dvb_hisi_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi) {
(void) fi;
stbuf->st_uid = getuid();
stbuf->st_gid = getgid();
stbuf->st_atime = stbuf->st_mtime = time(NULL);
switch (dvb_hisi_file_type(path)) {
case DVB_ROOT:
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
break;
case DVB_FILE:
case DVB_AUDIO_DEV:
case DVB_VIDEO_DEV:
case DVB_DVR_DEV:
case DVB_PAINEL_DEV:
case DVB_ENCODER_DEV:
stbuf->st_mode = S_IFREG | 0660;
stbuf->st_size = 0;
stbuf->st_nlink = 1;
break;
case DVB_NONE:
return -ENOENT;
}
return 0;
}
static int dvb_hisi_open(const char *path, struct fuse_file_info *fi) {
int type = dvb_hisi_file_type(path);
struct fuse_context *cxt = fuse_get_context();
struct class_ops *player = (struct class_ops *)cxt->private_data;
if (type == DVB_NONE) {
return -ENOENT;
} else if (type == DVB_ENCODER_DEV) {
if ((fi->flags & O_ACCMODE) != O_RDONLY) {
fi->fh = atoi(&path[(strlen(path)-2)]);
if (!fi->fh)
fi->fh = atoi(&path[(strlen(path)-1)]);
if (fi->fh >= MAX_ENCODERS)
return -ENOENT;
else if (encoders[fi->fh])
return -EBUSY;
encoders[fi->fh] = get_encoder();
if (!encoders[fi->fh]->create(encoders[fi->fh], fi->fh)) {
free(encoders[fi->fh]);
encoders[fi->fh] = NULL;
return -EBUSY;
}
} else {
return -EBUSY;
}
} else if ((fi->flags & O_ACCMODE) != O_RDONLY) {
/* Lock for only 1 access. */
switch (type) {
case DVB_AUDIO_DEV:
case DVB_VIDEO_DEV:
case DVB_DVR_DEV:
case DVB_PAINEL_DEV:
if (dvb_hisi_open_mask & (1 << type))
return -EBUSY;
dvb_hisi_open_mask |= (1 << type);
/* fsel files are nonseekable somewhat pipe-like files which
* gets filled up periodically by producer thread and consumed
* on read. Tell FUSE as such.
*/
if (type == DVB_DVR_DEV) {
char filename[32];
snprintf(filename, sizeof(filename), "/dev/dvb/adapter%c/dvr0", path[(strlen(path)-1)]);
fi->fh = open(filename, O_WRONLY);
player->set_dvr(true);
}
break;
default:
break;
}
}
fi->direct_io = 1;
fi->keep_cache = 0;
fi->flush = 0;
fi->nonseekable= 1;
return 0;
}
static int dvb_hisi_release(const char *path, struct fuse_file_info *fi) {
int type = dvb_hisi_file_type(path);
struct fuse_context *cxt = fuse_get_context();
struct class_ops *player = (struct class_ops *)cxt->private_data;
if ((fi->flags & O_ACCMODE) != O_RDONLY) {
/* Release for access. */
switch (type) {
case DVB_AUDIO_DEV:
case DVB_VIDEO_DEV:
case DVB_DVR_DEV:
case DVB_PAINEL_DEV:
dvb_hisi_open_mask &= ~(1 << type);
if (type == DVB_AUDIO_DEV && player)
player->stop(DEV_AUDIO);
else if (type == DVB_VIDEO_DEV && player)
player->stop(DEV_VIDEO);
else if (type == DVB_DVR_DEV) {
if (fi->fh >= 0)
close(fi->fh);
player->set_dvr(false);
}
break;
case DVB_ENCODER_DEV:
encoders[fi->fh]->destroy(encoders[fi->fh]);
free(encoders[fi->fh]);
encoders[fi->fh] = NULL;
break;
default:
break;
}
}
return 0;
}
static int dvb_hisi_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
(void)offset;
int ret = -EINVAL;
int type = dvb_hisi_file_type(path);
struct fuse_context *cxt = fuse_get_context();
struct class_ops *player = (struct class_ops *)cxt->private_data;
if (!player)
return -EINVAL;
switch (type) {
case DVB_ENCODER_DEV:
ret = encoders[fi->fh]->read(encoders[fi->fh], buf, size);
break;
default:
break;
}
return ret;
}
static int dvb_hisi_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
(void) offset;
int ret = -EINVAL;
int type = dvb_hisi_file_type(path);
struct fuse_context *cxt = fuse_get_context();
struct class_ops *player = (struct class_ops *)cxt->private_data;
if (!player)
return -EINVAL;
switch (type) {
case DVB_AUDIO_DEV:
ret = player->write(DEV_AUDIO, buf, size);
break;
case DVB_VIDEO_DEV:
ret = player->write(DEV_VIDEO, buf, size);
break;
case DVB_DVR_DEV:
ret = player->write(DEV_DVR, buf, size);
if (ret > 0 && fi->fh >= 0)
write(fi->fh, buf, ret);
break;
case DVB_PAINEL_DEV:
ret = player->write(DEV_PAINEL, buf, size);
break;
case DVB_ENCODER_DEV:
ret = encoders[fi->fh]->write(encoders[fi->fh], buf, size);
break;
}
return ret;
}
static int dvb_hisi_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi, enum fuse_readdir_flags flags) {
(void) offset;
(void) fi;
(void) flags;
if (dvb_hisi_file_type(path) != DVB_ROOT)
return -ENOENT;
filler(buf, ".", NULL, 0, 0);
filler(buf, "..", NULL, 0, 0);
filler(buf, AUDIO_DEV, NULL, 0, 0);
filler(buf, VIDEO_DEV, NULL, 0, 0);
filler(buf, DVR_DEV, NULL, 0, 0);
filler(buf, PAINEL_DEV, NULL, 0, 0);
return 0;
}
static int dvb_hisi_ioctl(const char *path, unsigned int cmd, void *arg, struct fuse_file_info *fi, unsigned int flags, void *data) {
(void) fi;
(void) flags;
int type = dvb_hisi_file_type(path);
struct fuse_context *cxt = fuse_get_context();
struct class_ops *player = (struct class_ops *)cxt->private_data;
switch (type) {
case DVB_AUDIO_DEV:
case DVB_VIDEO_DEV:
if (!player)
return -EINVAL;
if (!(dvb_hisi_open_mask & (1 << type))) {
switch (cmd) {
case AUDIO_GET_STATUS:
case VIDEO_GET_STATUS:
case VIDEO_GET_EVENT:
case VIDEO_GET_SIZE:
break;
default:
return -EPERM;
break;
}
}
break;
case DVB_ENCODER_DEV:
switch (cmd) {
case IOCTL_BROADCOM_SET_VPID:
printf("%s: IOCTL_BROADCOM_SET_VPID\n", __FUNCTION__);
encoders[fi->fh]->set_pid(encoders[fi->fh], DEV_VIDEO, (int)(intptr_t)arg);
break;
case IOCTL_BROADCOM_SET_APID:
printf("%s: IOCTL_BROADCOM_SET_APID\n", __FUNCTION__);
encoders[fi->fh]->set_pid(encoders[fi->fh], DEV_AUDIO, (int)(intptr_t)arg);
break;
case IOCTL_BROADCOM_SET_PMTPID:
printf("%s: IOCTL_BROADCOM_SET_PMTPID\n", __FUNCTION__);
encoders[fi->fh]->set_pid(encoders[fi->fh], -1, (int)(intptr_t)arg);
break;
case IOCTL_BROADCOM_START_TRANSCODING:
printf("%s: IOCTL_BROADCOM_START_TRANSCODING\n", __FUNCTION__);
return encoders[fi->fh]->play(encoders[fi->fh]) - 1;
break;
case IOCTL_BROADCOM_STOP_TRANSCODING:
printf("%s: IOCTL_BROADCOM_STOP_TRANSCODING\n", __FUNCTION__);
return encoders[fi->fh]->stop(encoders[fi->fh]) - 1;
break;
}
return 0;
break;
default:
return -EINVAL;
break;
}
switch (cmd) {
/* AUDIO IOCTL
*/
case AUDIO_STOP:
printf("%s: AUDIO_STOP\n", __FUNCTION__);
return player->stop(DEV_AUDIO) - 1;
break;
case AUDIO_PLAY:
printf("%s: AUDIO_PLAY\n", __FUNCTION__);
if (arg)
player->set_pid(DEV_AUDIO, (int)(intptr_t)arg);
return player->play(DEV_AUDIO) - 1;
break;
case AUDIO_PAUSE:
printf("%s: AUDIO_PAUSE\n", __FUNCTION__);
return player->pause(DEV_AUDIO) - 1;
break;
case AUDIO_CONTINUE:
printf("%s: AUDIO_CONTINUE\n", __FUNCTION__);
return player->resume(DEV_AUDIO) - 1;
break;
case AUDIO_SELECT_SOURCE:
printf("%s: AUDIO_SELECT_SOURCE\n", __FUNCTION__);
return player->set_mode((int)(intptr_t)arg) - 1;
break;
case AUDIO_SET_MUTE:
printf("%s: AUDIO_SET_MUTE\n", __FUNCTION__);
return player->mute((int)(intptr_t)arg) - 1;
break;
case AUDIO_SET_AV_SYNC:
printf("%s: AUDIO_SET_AV_SYNC\n", __FUNCTION__);
return player->sync((int)(intptr_t)arg) - 1;
break;
case AUDIO_SET_BYPASS_MODE:
printf("%s: AUDIO_SET_BYPASS_MODE\n", __FUNCTION__);
return player->set_type(DEV_AUDIO, (int)(intptr_t)arg) - 1;
break;
case AUDIO_CHANNEL_SELECT:
printf("%s: AUDIO_CHANNEL_SELECT\n", __FUNCTION__);
return player->channel((int)(intptr_t)arg) - 1;
break;
case AUDIO_GET_STATUS:
printf("%s: AUDIO_GET_STATUS\n", __FUNCTION__);
return player->status(DEV_AUDIO, (audio_status_t *)data) - 1;
break;
case AUDIO_GET_CAPABILITIES:
printf("%s: AUDIO_GET_CAPABILITIES\n", __FUNCTION__);
*(unsigned int *)data = AUDIO_CAP_LPCM | AUDIO_CAP_DTS | AUDIO_CAP_AC3 | AUDIO_CAP_MP1 | AUDIO_CAP_MP2;
break;
case AUDIO_CLEAR_BUFFER:
printf("%s: AUDIO_CLEAR_BUFFER\n", __FUNCTION__);
return player->clear(DEV_AUDIO) - 1;
break;
case AUDIO_SET_ID:
printf("%s: AUDIO_SET_ID\n", __FUNCTION__);
break;
case AUDIO_SET_MIXER:
printf("%s: AUDIO_SET_MIXER\n", __FUNCTION__);
return player->mixer((audio_mixer_t *)data) - 1;
break;
case AUDIO_SET_STREAMTYPE:
printf("%s: AUDIO_SET_STREAMTYPE\n", __FUNCTION__);
break;
case AUDIO_GET_PTS:
return player->get_pts(DEV_AUDIO, (long long *)data) - 1;
break;
/* VIDEO IOCTL
*/
case VIDEO_STOP:
printf("%s: VIDEO_STOP\n", __FUNCTION__);
return player->stop(DEV_VIDEO) - 1;
break;
case VIDEO_PLAY:
printf("%s: VIDEO_PLAY\n", __FUNCTION__);
if (arg)
player->set_pid(DEV_VIDEO, (int)(intptr_t)arg);
return player->play(DEV_VIDEO) - 1;
break;
case VIDEO_FREEZE:
printf("%s: VIDEO_FREEZE\n", __FUNCTION__);
return player->pause(DEV_VIDEO) - 1;
break;
case VIDEO_CONTINUE:
printf("%s: VIDEO_CONTINUE\n", __FUNCTION__);
return player->resume(DEV_VIDEO) - 1;
break;
case VIDEO_SELECT_SOURCE:
printf("%s: VIDEO_SELECT_SOURCE\n", __FUNCTION__);
return player->set_mode((int)(intptr_t)arg) - 1;
break;
case VIDEO_SET_BLANK:
printf("%s: VIDEO_SET_BLANK\n", __FUNCTION__);
return player->set_blank((int)(intptr_t)arg) - 1;
break;
case VIDEO_GET_STATUS:
printf("%s: VIDEO_GET_STATUS\n", __FUNCTION__);
return player->status(DEV_VIDEO, (struct video_status *)data) - 1;
break;
case VIDEO_GET_EVENT:
printf("%s: VIDEO_GET_EVENT\n", __FUNCTION__);
return player->get_event((struct video_event *)data) - 1;
break;
case VIDEO_GET_SIZE:
printf("%s: VIDEO_GET_SIZE\n", __FUNCTION__);
return player->get_vsize((video_size_t *)data) - 1;
break;
case VIDEO_SET_DISPLAY_FORMAT:
printf("%s: VIDEO_SET_DISPLAY_FORMAT\n", __FUNCTION__);
return player->set_disp_format((int)(intptr_t)arg) - 1;
break;
case VIDEO_SET_FORMAT:
printf("%s: VIDEO_SET_FORMAT\n", __FUNCTION__);
return player->set_format((int)(intptr_t)arg) - 1;
break;
case VIDEO_STILLPICTURE:
printf("%s: VIDEO_STILLPICTURE\n", __FUNCTION__);
break;
case VIDEO_FAST_FORWARD:
printf("%s: VIDEO_FAST_FORWARD - %d\n", __FUNCTION__, (int)(intptr_t)arg);
return player->set_fastfoward((int)(intptr_t)arg) - 1;
break;
case VIDEO_SLOWMOTION:
printf("%s: VIDEO_SLOWMOTION - %d\n", __FUNCTION__, (int)(intptr_t)arg);
return player->set_slowmotion((int)(intptr_t)arg) - 1;
break;
case VIDEO_GET_CAPABILITIES:
printf("%s: VIDEO_GET_CAPABILITIES\n", __FUNCTION__);
*(unsigned int *)data = VIDEO_CAP_MPEG1 | VIDEO_CAP_MPEG2 | VIDEO_CAP_SYS | VIDEO_CAP_PROG;
break;
case VIDEO_CLEAR_BUFFER:
printf("%s: VIDEO_CLEAR_BUFFER\n", __FUNCTION__);
return player->clear(DEV_VIDEO) - 1;
break;
case VIDEO_SET_STREAMTYPE:
printf("%s: VIDEO_SET_STREAMTYPE\n", __FUNCTION__);
return player->set_type(DEV_VIDEO, (int)(intptr_t)arg) - 1;
break;
case VIDEO_GET_FRAME_RATE:
printf("%s: VIDEO_GET_FRAME_RATE\n", __FUNCTION__);
return player->get_framerate((unsigned int *)data) - 1;
break;
case VIDEO_SET_FRAME_RATE:
printf("%s: VIDEO_SET_FRAME_RATE\n", __FUNCTION__);
return player->set_framerate((int)(intptr_t)arg) - 1;
break;
case VIDEO_GET_PTS:
return player->get_pts(DEV_VIDEO, (long long *)data) - 1;
break;
default:
printf("[INFO] %s: UNKNOWN %u\n", __FUNCTION__, cmd);
return -EINVAL;
break;
}
return 0;
}
static int dvb_hisi_poll(const char *path, struct fuse_file_info *fi, struct fuse_pollhandle *ph, unsigned *reventsp) {
(void) fi;
int type = dvb_hisi_file_type(path);
struct fuse_context *cxt = fuse_get_context();
struct class_ops *player = (struct class_ops *)cxt->private_data;
if (!player) {
if (ph)
fuse_pollhandle_destroy(ph);
*reventsp = -EINVAL;
return 0;
}
else
*reventsp = 0;
switch (type) {
case DVB_AUDIO_DEV:
return player->poll(DEV_AUDIO, ph, reventsp, false);
break;
case DVB_VIDEO_DEV:
return player->poll(DEV_VIDEO, ph, reventsp, dvb_hisi_open_mask & (1 << type));
break;
case DVB_DVR_DEV:
return player->poll(DEV_DVR, ph, reventsp, dvb_hisi_open_mask & (1 << type));
break;
case DVB_ENCODER_DEV:
return encoders[fi->fh]->poll(encoders[fi->fh], ph, reventsp, false);
break;
default:
if (ph)
fuse_pollhandle_destroy(ph);
*reventsp = -EINVAL;
break;
}
return 0;
}
static struct fuse_operations dvb_hisi_oper = {
.init = dvb_hisi_init,
.destroy = dvb_hisi_destroy,
.getattr = dvb_hisi_getattr,
.readdir = dvb_hisi_readdir,
.open = dvb_hisi_open,
.release = dvb_hisi_release,
.read = dvb_hisi_read,
.write = dvb_hisi_write,
.ioctl = dvb_hisi_ioctl,
.poll = dvb_hisi_poll,
};
int main(int argc, char *argv[]) {
printf("_____________________________________\n");
printf("# _ _ _ _ _ _ _ _ #\n");
printf("# / \\ / \\ / \\ / \\ / \\ / \\ / \\ / \\ #\n");
printf("# ( A | V | S | e | r | v | e | r ) #\n");
printf("# \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ #\n");
printf("# #\n");
printf("# Created by: #\n");
printf("# leandrotsampa #\n");
printf("# Contact: #\n");
printf("# leandrotsampa@yahoo.com.br #\n");
printf("# Current Version: #\n");
printf("# %s #\n", AVSERVER_VERSION);
printf("\e[4m#___________________________________#\e[24m\n\n");
return fuse_main(argc, argv, &dvb_hisi_oper, NULL);
}