forked from RC-MODULE/utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts-save-1.c
111 lines (92 loc) · 1.96 KB
/
ts-save-1.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
#include <stdio.h>
#include <unistd.h>
#include "mdemux.h"
#include "common.h"
void send_buffer (struct mdemux_buffer* buffer, void *userdata);
struct mdemux_callback g_cb1 = {
.logger = console_logger,
.alloc_space = mem_alloc,
.free_space = mem_free,
.send_buffer = send_buffer,
.time = system_time
};
void send_buffer(struct mdemux_buffer* buffer, void *userdata)
{
int ret;
int fd = (int)userdata;
int wsize = 0;
while(wsize < buffer->fsize) {
ret = write(fd, buffer->buf + wsize, buffer->fsize-wsize);
if(ret<0) {
int e = errno;
if(e == EINTR)
continue;
fprintf(stderr,"Got error %d while sending buffer\n", e);
exit(-1);
}
if(ret == 0) {
fprintf(stderr,"Got 0 result while sending buffer\n");
exit(-1);
}
wsize += ret;
}
free(buffer->buf);
}
int open_file(int pid)
{
char fname[51];
int ret;
snprintf(fname, 50, "ts-save-1.%d.out", pid);
ret = open(fname,O_WRONLY|O_CREAT|O_TRUNC, 0644);
if(ret <0) {
fprintf(stderr,"Unable to open file: %s\n", fname);
exit(-1);
}
return ret;
}
int main(int argc, char **argv)
{
int ret;
struct mdemux filter[1];
struct mdemux_poller poller;
int pid0;
int fd0;
if(argc != 2) {
fprintf(stderr,"usage: %s PID\n", argv[0]);
exit(-1);
}
dbglevel_set(1);
pid0 = atoi(argv[1]);
fprintf(stderr, "pid %d\n", pid0);
//fd0 = 1; //stdout
fd0 = open_file(pid0);
mdemux_init(&filter[0], (void*)fd0);
filter[0].s.pes_type = 0;
filter[0].s.adapter_id = 0;
filter[0].s.demux_id = 1;
filter[0].s.ftype = MDEMUX_TS;
filter[0].c = &g_cb1;
ret = mdemux_setpid(&filter[0], pid0);
if(ret < 0) {
fprintf(stderr,"Unable to set pid %d\n", pid0);
exit(-1);
}
poller.timeout = 1000;
poller.filters = filter;
poller.filter_count = 1;
poller.stat = 0;
while(1) {
ret = mdemux_loop(&poller);
if(ret == -EAGAIN) {
sleep(1);
continue;
}
if(ret < 0) {
fprintf(stderr,"mdemux_loop returns error %d\n", ret);
break;
}
}
mdemux_close(&filter[0]);
close(fd0);
return 0;
}