-
Notifications
You must be signed in to change notification settings - Fork 0
/
demuxer.cpp
304 lines (252 loc) · 6.28 KB
/
demuxer.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <poll.h>
#include <errno.h>
#include "demuxer.h"
bool neutrinoNT_PesFilter(int fd, DMX_CHANNEL_TYPE type, uint16_t pid, const dmx_input_t input)
{
if (fd <= 0)
return false;
if ((pid >= 0x0002 && pid <= 0x000f) || pid >= 0x1fff)
return false;
struct dmx_pes_filter_params pes;
memset(&pes, 0, sizeof(struct dmx_pes_filter_params));
pes.pid = pid;
pes.input = input;
pes.output = DMX_OUT_DECODER;
pes.flags = 0;
switch (type)
{
case DMX_VIDEO_CHANNEL:
pes.pes_type = DMX_PES_VIDEO;
break;
case DMX_AUDIO_CHANNEL:
pes.pes_type = DMX_PES_AUDIO;
break;
case DMX_PCR_ONLY_CHANNEL:
pes.pes_type = DMX_PES_PCR;
break;
case DMX_PES_CHANNEL:
pes.output = DMX_OUT_TAP; /* to memory */
pes.pes_type = DMX_PES_OTHER;
break;
case DMX_TP_CHANNEL:
pes.output = DMX_OUT_TSDEMUX_TAP; /* to demux */ /* Output multiplexed into a new TS */
pes.pes_type = DMX_PES_OTHER;
break;
case DMX_PIP_CHANNEL:
pes.pes_type = DMX_PES_VIDEO1; //for pip channel we need only video
break;
default:
return false;
}
if (ioctl(fd, DMX_SET_PES_FILTER, &pes) < 0)
{
perror("DMX_SET_PES_FILTER");
return false;
}
return true;
}
static const char* aDMXCHANNELTYPE[] = {
"",
"DMX_VIDEO_CHANNEL",
"DMX_AUDIO_CHANNEL",
"DMX_PES_CHANNEL",
"DMX_PSI_CHANNEL",
"DMX_PIP_CHANNEL",
"DMX_TP_CHANNEL",
"DMX_PCR_ONLY_CHANNEL"
};
bool neutrinoNT_SectionFilter(int fd, uint16_t pid, MonoArray* filter, MonoArray* mask, int len, int timeout, MonoArray* mode)
{
if (fd < 0)
return false;
unsigned char* _mode = NULL;
unsigned char* _filter = mono_array_addr(filter, unsigned char, 0);
unsigned char* _mask = mono_array_addr(mask, unsigned char, 0);
if (mode != NULL)
_mode = mono_array_addr(mode, unsigned char, 0);
struct dmx_sct_filter_params sct;
memset(&sct, 0, sizeof(struct dmx_sct_filter_params));
if (len > DMX_FILTER_SIZE)
{
len = DMX_FILTER_SIZE;
}
/* Pid */
sct.pid = pid;
/* filter */
memcpy(sct.filter.filter, _filter, len);
/* mask */
memcpy(sct.filter.mask, _mask, len);
/* mode */
if (mode)
memcpy(sct.filter.mode, _mode, len);
/* flag */
sct.flags = DMX_IMMEDIATE_START | DMX_CHECK_CRC;
/* timeout */
int to = 0;
switch (sct.filter.filter[0])
{
case 0x00: /* program_association_section */
to = 2000;
break;
case 0x01: /* conditional_access_section */
to = 6000;
break;
case 0x02: /* program_map_section */
to = 1500;
break;
case 0x03: /* transport_stream_description_section */
to = 10000;
break;
/* 0x04 - 0x3F: reserved */
case 0x40: /* network_information_section - actual_network */
to = 10000;
break;
case 0x41: /* network_information_section - other_network */
to = 15000;
break;
case 0x42: /* service_description_section - actual_transport_stream */
to = 10000;
break;
/* 0x43 - 0x45: reserved for future use */
case 0x46: /* service_description_section - other_transport_stream */
to = 10000;
break;
/* 0x47 - 0x49: reserved for future use */
case 0x4A: /* bouquet_association_section */
to = 11000;
break;
/* 0x4B - 0x4D: reserved for future use */
case 0x4E: /* event_information_section - actual_transport_stream, present/following */
to = 2000;
break;
case 0x4F: /* event_information_section - other_transport_stream, present/following */
to = 10000;
break;
case 0x70: /* time_date_section */ /* UTC */
sct.flags &= (~DMX_CHECK_CRC); /* section has no CRC */
sct.flags |= DMX_ONESHOT;
//sct.pid = 0x0014;
to = 30000;
break;
case 0x71: /* running_status_section */
sct.flags &= (~DMX_CHECK_CRC); /* section has no CRC */
to = 0;
break;
case 0x72: /* stuffing_section */
sct.flags &= (~DMX_CHECK_CRC); /* section has no CRC */
to = 0;
break;
case 0x73: /* time_offset_section */ /* UTC */
sct.flags |= DMX_ONESHOT;
//sct.pid = 0x0014;
to = 30000;
break;
/* 0x74 - 0x7D: reserved for future use */
case 0x7E: /* discontinuity_information_section */
sct.flags &= (~DMX_CHECK_CRC); /* section has no CRC */
to = 0;
break;
case 0x7F: /* selection_information_section */
to = 0;
break;
/* 0x80 - 0x8F: ca_message_section */
/* 0x90 - 0xFE: user defined */
/* 0xFF: reserved */
default:
//return -1;
break;
}
if (timeout == 0 && mode == NULL)
sct.timeout = to;
//printf("Pid=0x%x Len=%d Timeout=%d\n", pid, len, sct.timeout);
/* Set Demux Section Filter() */
if (ioctl(fd, DMX_SET_FILTER, &sct) < 0)
{
perror("DMX_SET_FILTER");
return false;
}
return true;
}
int neutrinoNT_Read(int fd, MonoArray** array, DMX_CHANNEL_TYPE type, int len, int timeout)
{
int rc;
struct pollfd ufds;
ufds.fd = fd;
ufds.events = POLLIN;
ufds.revents = 0;
if (fd < 0)
return -1;
if (type == DMX_PSI_CHANNEL && timeout <= 0)
timeout = 60 * 1000;
if (timeout > 0)
{
retry:
rc = poll(&ufds, 1, timeout);
if (!rc)
{
return 0; // timeout
}
else if (rc < 0)
{
if (errno == EINTR)
goto retry;
/* we consciously ignore EINTR, since it does not happen in practice */
return -1;
}
if (ufds.revents & POLLHUP) /* we get POLLHUP if e.g. a too big DMX_BUFFER_SIZE was set */
{
return -1;
}
if (!(ufds.revents & POLLIN)) /* we requested POLLIN but did not get it? */
{
return 0;
}
}
uint8_t* buffer = mono_array_addr(*array, uint8_t, 0);
rc = read(fd, buffer, len);
if (rc < 0)
perror("Read");
return rc;
}
int neutrinoNT_DMX_SET_SOURCE(int fd, dmx_source_t number)
{
return ioctl(fd, DMX_SET_SOURCE, &number);
}
int neutrinoNT_DMX_SET_BUFFER_SIZE(int fd, int bufferSize)
{
return ioctl(fd, DMX_SET_BUFFER_SIZE, bufferSize);
}
int neutrinoNT_DMX_START(int fd)
{
return ioctl(fd, DMX_START);
}
int neutrinoNT_DMX_STOP(int fd)
{
return ioctl(fd, DMX_STOP);
}
int neutrinoNT_DMX_ADD_PID(int fd, uint16_t pid)
{
return ioctl(fd, DMX_ADD_PID, pid);
}
int neutrinoNT_DMX_REMOVE_PID(int fd, uint16_t pid)
{
return ioctl(fd, DMX_REMOVE_PID, pid);
}
int neutrinoNT_DMX_SET_PES_FILTER(int fd, struct dmx_pes_filter_params* params)
{
return ioctl(fd, DMX_SET_PES_FILTER, params);
}
int neutrinoNT_DMX_SET_FILTER(int fd, struct dmx_sct_filter_params* params)
{
return ioctl(fd, DMX_SET_FILTER, params);
}