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
|
// SPDX-License-Identifier: GPL-2.0
/*
* Vidtv serves as a reference DVB driver and helps validate the existing APIs
* in the media subsystem. It can also aid developers working on userspace
* applications.
*
* This file contains the code for a 'channel' abstraction.
*
* When vidtv boots, it will create some hardcoded channels.
* Their services will be concatenated to populate the SDT.
* Their programs will be concatenated to populate the PAT
* Their events will be concatenated to populate the EIT
* For each program in the PAT, a PMT section will be created
* The PMT section for a channel will be assigned its streams.
* Every stream will have its corresponding encoder polled to produce TS packets
* These packets may be interleaved by the mux and then delivered to the bridge
*
*
* Copyright (C) 2020 Daniel W. S. Almeida
*/
#include <linux/dev_printk.h>
#include <linux/ratelimit.h>
#include <linux/slab.h>
#include <linux/types.h>
#include "vidtv_channel.h"
#include "vidtv_common.h"
#include "vidtv_encoder.h"
#include "vidtv_mux.h"
#include "vidtv_psi.h"
#include "vidtv_s302m.h"
static void vidtv_channel_encoder_destroy(struct vidtv_encoder *e)
{
struct vidtv_encoder *curr = e;
struct vidtv_encoder *tmp = NULL;
while (curr) {
/* forward the call to the derived type */
tmp = curr;
curr = curr->next;
tmp->destroy(tmp);
}
}
#define ENCODING_ISO8859_15 "\x0b"
struct vidtv_channel
*vidtv_channel_s302m_init(struct vidtv_channel *head, u16 transport_stream_id)
{
/*
* init an audio only channel with a s302m encoder
*/
const u16 s302m_service_id = 0x880;
const u16 s302m_program_num = 0x880;
const u16 s302m_program_pid = 0x101; /* packet id for PMT*/
const u16 s302m_es_pid = 0x111; /* packet id for the ES */
const __be32 s302m_fid = cpu_to_be32(VIDTV_S302M_FORMAT_IDENTIFIER);
char *name = ENCODING_ISO8859_15 "Beethoven";
char *provider = ENCODING_ISO8859_15 "LinuxTV.org";
char *iso_language_code = ENCODING_ISO8859_15 "eng";
char *event_name = ENCODING_ISO8859_15 "Beethoven Music";
char *event_text = ENCODING_ISO8859_15 "Beethoven's 5th Symphony";
const u16 s302m_beethoven_event_id = 1;
struct vidtv_channel *s302m = kzalloc(sizeof(*s302m), GFP_KERNEL);
struct vidtv_s302m_encoder_init_args encoder_args = {};
s302m->name = kstrdup(name, GFP_KERNEL);
s302m->service = vidtv_psi_sdt_service_init(NULL, s302m_service_id, false, true);
s302m->service->descriptor = (struct vidtv_psi_desc *)
vidtv_psi_service_desc_init(NULL,
DIGITAL_TELEVISION_SERVICE,
name,
provider);
s302m->transport_stream_id = transport_stream_id;
s302m->program = vidtv_psi_pat_program_init(NULL,
s302m_service_id,
s302m_program_pid);
s302m->program_num = s302m_program_num;
s302m->streams = vidtv_psi_pmt_stream_init(NULL,
STREAM_PRIVATE_DATA,
s302m_es_pid);
s302m->streams->descriptor = (struct vidtv_psi_desc *)
vidtv_psi_registration_desc_init(NULL,
s302m_fid,
NULL,
0);
encoder_args.es_pid = s302m_es_pid;
s302m->encoders = vidtv_s302m_encoder_init(encoder_args);
s302m->events = vidtv_psi_eit_event_init(NULL, s302m_beethoven_event_id);
s302m->events->descriptor = (struct vidtv_psi_desc *)
vidtv_psi_short_event_desc_init(NULL,
iso_language_code,
event_name,
event_text);
if (head) {
while (head->next)
head = head->next;
head->next = s302m;
}
return s302m;
}
static struct vidtv_psi_table_eit_event
*vidtv_channel_eit_event_cat_into_new(struct vidtv_mux *m)
{
/* Concatenate the events */
const struct vidtv_channel *cur_chnl = m->channels;
struct vidtv_psi_table_eit_event *curr = NULL;
struct vidtv_psi_table_eit_event *head = NULL;
struct vidtv_psi_table_eit_event *tail = NULL;
struct vidtv_psi_desc *desc = NULL;
u16 event_id;
if (!cur_chnl)
return NULL;
while (cur_chnl) {
curr = cur_chnl->events;
if (!curr)
dev_warn_ratelimited(m->dev,
"No events found for channel %s\n", cur_chnl->name);
while (curr) {
event_id = be16_to_cpu(curr->event_id);
tail = vidtv_psi_eit_event_init(tail, event_id);
desc = vidtv_psi_desc_clone(curr->descriptor);
vidtv_psi_desc_assign(&tail->descriptor, desc);
if (!head)
head = tail;
curr = curr->next;
}
cur_chnl = cur_chnl->next;
}
return head;
}
static struct vidtv_psi_table_sdt_service
*vidtv_channel_sdt_serv_cat_into_new(struct vidtv_mux *m)
{
/* Concatenate the services */
const struct vidtv_channel *cur_chnl = m->channels;
struct vidtv_psi_table_sdt_service *curr = NULL;
struct vidtv_psi_table_sdt_service *head = NULL;
struct vidtv_psi_table_sdt_service *tail = NULL;
struct vidtv_psi_desc *desc = NULL;
u16 service_id;
if (!cur_chnl)
return NULL;
while (cur_chnl) {
curr = cur_chnl->service;
if (!curr)
dev_warn_ratelimited(m->dev,
"No services found for channel %s\n", cur_chnl->name);
while (curr) {
service_id = be16_to_cpu(curr->service_id);
tail = vidtv_psi_sdt_service_init(tail,
service_id,
curr->EIT_schedule,
curr->EIT_present_following);
desc = vidtv_psi_desc_clone(curr->descriptor);
vidtv_psi_desc_assign(&tail->descriptor, desc);
if (!head)
head = tail;
curr = curr->next;
}
cur_chnl = cur_chnl->next;
}
return head;
}
static struct vidtv_psi_table_pat_program*
vidtv_channel_pat_prog_cat_into_new(struct vidtv_mux *m)
{
/* Concatenate the programs */
const struct vidtv_channel *cur_chnl = m->channels;
struct vidtv_psi_table_pat_program *curr = NULL;
struct vidtv_psi_table_pat_program *head = NULL;
struct vidtv_psi_table_pat_program *tail = NULL;
u16 serv_id;
u16 pid;
if (!cur_chnl)
return NULL;
while (cur_chnl) {
curr = cur_chnl->program;
if (!curr)
dev_warn_ratelimited(m->dev,
"No programs found for channel %s\n",
cur_chnl->name);
while (curr) {
serv_id = be16_to_cpu(curr->service_id);
pid = vidtv_psi_get_pat_program_pid(curr);
tail = vidtv_psi_pat_program_init(tail,
serv_id,
pid);
if (!head)
head = tail;
curr = curr->next;
}
cur_chnl = cur_chnl->next;
}
return head;
}
static void
vidtv_channel_pmt_match_sections(struct vidtv_channel *channels,
struct vidtv_psi_table_pmt **sections,
u32 nsections)
{
/*
* Match channels to their respective PMT sections, then assign the
* streams
*/
struct vidtv_psi_table_pmt *curr_section = NULL;
struct vidtv_channel *cur_chnl = channels;
struct vidtv_psi_table_pmt_stream *s = NULL;
struct vidtv_psi_table_pmt_stream *head = NULL;
struct vidtv_psi_table_pmt_stream *tail = NULL;
struct vidtv_psi_desc *desc = NULL;
u32 j;
u16 curr_id;
u16 e_pid; /* elementary stream pid */
while (cur_chnl) {
for (j = 0; j < nsections; ++j) {
curr_section = sections[j];
if (!curr_section)
continue;
curr_id = be16_to_cpu(curr_section->header.id);
/* we got a match */
if (curr_id == cur_chnl->program_num) {
s = cur_chnl->streams;
/* clone the streams for the PMT */
while (s) {
e_pid = vidtv_psi_pmt_stream_get_elem_pid(s);
tail = vidtv_psi_pmt_stream_init(tail,
s->type,
e_pid);
if (!head)
head = tail;
desc = vidtv_psi_desc_clone(s->descriptor);
vidtv_psi_desc_assign(&tail->descriptor, desc);
s = s->next;
}
vidtv_psi_pmt_stream_assign(curr_section, head);
break;
}
}
cur_chnl = cur_chnl->next;
}
}
static struct vidtv_psi_desc_service_list_entry
*vidtv_channel_build_service_list(struct vidtv_psi_table_sdt_service *s)
{
struct vidtv_psi_desc_service_list_entry *curr_e = NULL;
struct vidtv_psi_desc_service_list_entry *head_e = NULL;
struct vidtv_psi_desc_service_list_entry *prev_e = NULL;
struct vidtv_psi_desc *desc = s->descriptor;
struct vidtv_psi_desc_service *s_desc;
while (s) {
while (desc) {
if (s->descriptor->type != SERVICE_DESCRIPTOR)
goto next_desc;
s_desc = (struct vidtv_psi_desc_service *)desc;
curr_e = kzalloc(sizeof(*curr_e), GFP_KERNEL);
curr_e->service_id = s->service_id;
curr_e->service_type = s_desc->service_type;
if (!head_e)
head_e = curr_e;
if (prev_e)
prev_e->next = curr_e;
prev_e = curr_e;
next_desc:
desc = desc->next;
}
s = s->next;
}
return head_e;
}
static void vidtv_channel_destroy_service_list(struct vidtv_psi_desc_service_list_entry *e)
{
struct vidtv_psi_desc_service_list_entry *tmp;
while (e) {
tmp = e;
e = e->next;
kfree(tmp);
}
}
void vidtv_channel_si_init(struct vidtv_mux *m)
{
struct vidtv_psi_table_pat_program *programs = NULL;
struct vidtv_psi_table_sdt_service *services = NULL;
struct vidtv_psi_desc_service_list_entry *service_list = NULL;
struct vidtv_psi_table_eit_event *events = NULL;
m->si.pat = vidtv_psi_pat_table_init(m->transport_stream_id);
m->si.sdt = vidtv_psi_sdt_table_init(m->transport_stream_id);
programs = vidtv_channel_pat_prog_cat_into_new(m);
services = vidtv_channel_sdt_serv_cat_into_new(m);
events = vidtv_channel_eit_event_cat_into_new(m);
/* look for a service descriptor for every service */
service_list = vidtv_channel_build_service_list(services);
/* use these descriptors to build the NIT */
m->si.nit = vidtv_psi_nit_table_init(m->network_id,
m->transport_stream_id,
m->network_name,
service_list);
m->si.eit = vidtv_psi_eit_table_init(m->network_id, m->transport_stream_id);
/* assemble all programs and assign to PAT */
vidtv_psi_pat_program_assign(m->si.pat, programs);
/* assemble all services and assign to SDT */
vidtv_psi_sdt_service_assign(m->si.sdt, services);
/* assemble all events and assign to EIT */
vidtv_psi_eit_event_assign(m->si.eit, events);
m->si.pmt_secs = vidtv_psi_pmt_create_sec_for_each_pat_entry(m->si.pat, m->pcr_pid);
vidtv_channel_pmt_match_sections(m->channels,
m->si.pmt_secs,
m->si.pat->programs);
vidtv_channel_destroy_service_list(service_list);
}
void vidtv_channel_si_destroy(struct vidtv_mux *m)
{
u32 i;
u16 num_programs = m->si.pat->programs;
vidtv_psi_pat_table_destroy(m->si.pat);
for (i = 0; i < num_programs; ++i)
vidtv_psi_pmt_table_destroy(m->si.pmt_secs[i]);
kfree(m->si.pmt_secs);
vidtv_psi_sdt_table_destroy(m->si.sdt);
vidtv_psi_nit_table_destroy(m->si.nit);
vidtv_psi_eit_table_destroy(m->si.eit);
}
void vidtv_channels_init(struct vidtv_mux *m)
{
/* this is the place to add new 'channels' for vidtv */
m->channels = vidtv_channel_s302m_init(NULL, m->transport_stream_id);
}
void vidtv_channels_destroy(struct vidtv_mux *m)
{
struct vidtv_channel *curr = m->channels;
struct vidtv_channel *tmp = NULL;
while (curr) {
kfree(curr->name);
vidtv_psi_sdt_service_destroy(curr->service);
vidtv_psi_pat_program_destroy(curr->program);
vidtv_psi_pmt_stream_destroy(curr->streams);
vidtv_channel_encoder_destroy(curr->encoders);
vidtv_psi_eit_event_destroy(curr->events);
tmp = curr;
curr = curr->next;
kfree(tmp);
}
}
|