1 /*
2 * Tvheadend
3 * Copyright (C) 2010 Andreas Öman
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef SERVICE_H__
20 #define SERVICE_H__
21
22 #include "htsmsg.h"
23 #include "idnode.h"
24 #include "profile.h"
25 #include "descrambler.h"
26
27 extern const idclass_t service_class;
28 extern const idclass_t service_raw_class;
29
30 extern struct service_queue service_all;
31 extern struct service_queue service_raw_all;
32 extern struct service_queue service_raw_remove;
33
34 struct channel;
35 struct tvh_input;
36 struct mpegts_apids;
37
38 /**
39 * Source information
40 */
41 typedef struct source_info {
42 tvh_uuid_t si_adapter_uuid;
43 tvh_uuid_t si_network_uuid;
44 tvh_uuid_t si_mux_uuid;
45 char *si_adapter;
46 char *si_network;
47 char *si_network_type;
48 char *si_satpos;
49 char *si_mux;
50 char *si_provider;
51 char *si_service;
52 int si_type;
53 } source_info_t;
54
55 /**
56 * Stream, one media component for a service.
57 */
58 typedef struct elementary_stream {
59
60 TAILQ_ENTRY(elementary_stream) es_link;
61 TAILQ_ENTRY(elementary_stream) es_filt_link;
62 int es_position;
63 struct service *es_service;
64
65 streaming_component_type_t es_type;
66 int es_index;
67
68 uint16_t es_aspect_num;
69 uint16_t es_aspect_den;
70
71 char es_lang[4]; /* ISO 639 2B 3-letter language code */
72 uint8_t es_audio_type; /* Audio type */
73 uint8_t es_audio_version; /* Audio version/layer */
74
75 uint16_t es_composition_id;
76 uint16_t es_ancillary_id;
77
78 int16_t es_pid;
79 uint16_t es_parent_pid; /* For subtitle streams originating from
80 a teletext stream. this is the pid
81 of the teletext stream */
82 int8_t es_pid_opened; /* PID is opened */
83
84 int8_t es_cc; /* Last CC */
85
86 int es_peak_presentation_delay; /* Max seen diff. of DTS and PTS */
87
88 /* For service stream packet reassembly */
89
90 sbuf_t es_buf;
91
92 uint8_t es_incomplete;
93 uint8_t es_header_mode;
94 uint32_t es_header_offset;
95 uint32_t es_startcond;
96 uint32_t es_startcode;
97 uint32_t es_startcode_offset;
98 int es_parser_state;
99 int es_parser_ptr;
100 void *es_priv; /* Parser private data */
101
102 sbuf_t es_buf_a; // Audio packet reassembly
103
104 uint8_t *es_global_data;
105 int es_global_data_len;
106
107 struct th_pkt *es_curpkt;
108 struct streaming_message_queue es_backlog;
109 int64_t es_curpts;
110 int64_t es_curdts;
111 int64_t es_prevdts;
112 int64_t es_nextdts;
113 int es_frame_duration;
114 int es_width;
115 int es_height;
116
117 int es_meta_change;
118
119 /* CA ID's on this stream */
120 struct caid_list es_caids;
121
122 /* */
123
124 int es_delete_me; /* Temporary flag for deleting streams */
125
126 /* Error log limiters */
127
128 tvhlog_limit_t es_cc_log;
129 tvhlog_limit_t es_pes_log;
130
131 char *es_nicename;
132
133 /* Teletext subtitle */
134 char es_blank; // Last subtitle was blank
135
136 /* SI section processing (horrible hack) */
137 void *es_section;
138
139 /* Filter temporary variable */
140 uint32_t es_filter;
141
142 } elementary_stream_t;
143
144
145 typedef TAILQ_HEAD(service_instance_list, service_instance) service_instance_list_t;
146
147 /**
148 *
149 */
150 typedef struct service_instance {
151
152 TAILQ_ENTRY(service_instance) si_link;
153
154 int si_prio;
155
156 struct service *si_s; // A reference is held
157 int si_instance; // Discriminator when having multiple adapters, etc
158
159 int si_error; /* Set if subscription layer deem this cand
160 to be broken. We typically set this if we
161 have not seen any demuxed packets after
162 the grace period has expired.
163 The actual value is current time
164 */
165
166 time_t si_error_time;
167
168
169 int si_weight; // Highest weight that holds this cand
170
171 int si_mark; // For mark & sweep
172
173 char si_source[128];
174
175 } service_instance_t;
176
177
178 /**
179 *
180 */
181 service_instance_t *service_instance_add(service_instance_list_t *sil,
182 struct service *s,
183 int instance,
184 const char *source,
185 int prio,
186 int weight);
187
188 void service_instance_destroy
189 (service_instance_list_t *sil, service_instance_t *si);
190
191 void service_instance_list_clear(service_instance_list_t *sil);
192
193 /**
194 *
195 */
196 typedef struct service_lcn {
197 LIST_ENTRY(service_lcn) sl_link;
198 void *sl_bouquet;
199 uint64_t sl_lcn;
200 uint8_t sl_seen;
201 } service_lcn_t;
202
203
204 /**
205 *
206 */
207 #define SERVICE_AUTO_NORMAL 0
208 #define SERVICE_AUTO_OFF 1
209 #define SERVICE_AUTO_PAT_MISSING 2
210
211 #define SERVICE_PMT_AUTO 0xffff
212
213 /**
214 *
215 */
216 typedef struct service {
217 idnode_t s_id;
218
219 TAILQ_ENTRY(service) s_all_link;
220
221 enum {
222 /**
223 * Transport is idle.
224 */
225 SERVICE_IDLE,
226
227 /**
228 * Transport producing output
229 */
230 SERVICE_RUNNING,
231
232 /**
233 * Destroyed, but pointer is till valid.
234 * This would be the case if transport_destroy() did not actually free
235 * the transport because there are references held to it.
236 *
237 * Reference counts can be used so that code can hold a pointer to
238 * a transport without having the global lock.
239 *
240 * Note: No fields in the transport may be accessed without the
241 * global lock held. Thus, the global_lock must be reaquired and
242 * then s_status must be checked. If it is ZOMBIE the code must
243 * just drop the refcount and pretend that the transport never
244 * was there in the first place.
245 */
246 SERVICE_ZOMBIE,
247 } s_status;
248
249 /**
250 * Refcount, operated using atomic.h ops.
251 */
252 int s_refcount;
253
254 /**
255 * Service type, standard or raw (for mux or partial mux streaming)
256 */
257 enum {
258 STYPE_STD,
259 STYPE_RAW,
260 STYPE_RAW_REMOVED
261 } s_type;
262
263 /**
264 * Source type is used to determine if an output requesting
265 * MPEG-TS can shortcut all the parsing and remuxing.
266 */
267 enum {
268 S_MPEG_TS,
269 S_MPEG_PS,
270 S_OTHER,
271 } s_source_type;
272
273 /**
274 * Service type
275 */
276 enum {
277 ST_UNSET = -1,
278 ST_NONE = 0,
279 ST_OTHER,
280 ST_SDTV,
281 ST_HDTV,
282 ST_UHDTV,
283 ST_RADIO
284 } s_servicetype;
285
286 // TODO: should this really be here?
287
288 /**
289 * PID carrying the programs PCR.
290 * XXX: We don't support transports that does not carry
291 * the PCR in one of the content streams.
292 */
293 uint16_t s_pcr_pid;
294
295 /**
296 * PID for the PMT of this MPEG-TS stream.
297 */
298 uint16_t s_pmt_pid;
299
300 /**
301 * Set if transport is enabled (the default). If disabled it should
302 * not be considered when chasing for available transports during
303 * subscription scheduling.
304 */
305 int s_enabled;
306 int s_auto;
307 int s_prio;
308 int s_type_user;
309 int s_pts_shift; // in ms (may be negative)
310
311 LIST_ENTRY(service) s_active_link;
312
313 LIST_HEAD(, th_subscription) s_subscriptions;
314
315 int (*s_is_enabled)(struct service *t, int flags);
316
317 int (*s_enlist)(struct service *s, struct tvh_input *ti,
318 service_instance_list_t *sil, int flags, int weight);
319
320 int (*s_start_feed)(struct service *s, int instance, int weight, int flags);
321
322 void (*s_refresh_feed)(struct service *t);
323
324 void (*s_stop_feed)(struct service *t);
325
326 htsmsg_t *(*s_config_save)(struct service *t, char *filename, size_t fsize);
327
328 void (*s_setsourceinfo)(struct service *t, struct source_info *si);
329
330 int (*s_grace_period)(struct service *t);
331
332 void (*s_delete)(struct service *t, int delconf);
333
334 void (*s_unref)(struct service *t);
335
336 int (*s_satip_source)(struct service *t);
337
338 void (*s_memoryinfo)(struct service *t, int64_t *size);
339
340 int (*s_unseen)(struct service *t, const char *type, time_t before);
341
342 /**
343 * Channel info
344 */
345 int64_t (*s_channel_number) (struct service *);
346 const char *(*s_channel_name) (struct service *);
347 const char *(*s_channel_epgid) (struct service *);
348 htsmsg_t *(*s_channel_tags) (struct service *);
349 const char *(*s_provider_name) (struct service *);
350 const char *(*s_channel_icon) (struct service *);
351 void (*s_mapped) (struct service *);
352
353 /**
354 * Name usable for displaying to user
355 */
356 char *s_nicename;
357 int s_nicename_prefidx;
358
359 /**
360 * Teletext...
361 */
362 th_commercial_advice_t s_tt_commercial_advice;
363 time_t s_tt_clock; /* Network clock as determined by teletext decoder */
364
365 /**
366 * Channel mapping
367 */
368 idnode_list_head_t s_channels;
369
370 /**
371 * Service mapping, see service_mapper.c form details
372 */
373 int s_sm_onqueue;
374
375 /**
376 * Pending save.
377 *
378 * transport_request_save() will enqueue the transport here.
379 * We need to do this if we don't hold the global lock.
380 * This happens when we update PMT from within the TS stream itself.
381 * Then we hold the stream mutex, and thus, can not obtain the global lock
382 * as it would cause lock inversion.
383 */
384 int s_ps_onqueue;
385 TAILQ_ENTRY(service) s_ps_link;
386
387 /**
388 * Timer which is armed at transport start. Once it fires
389 * it will check if any packets has been parsed. If not the status
390 * will be set to TRANSPORT_STATUS_NO_INPUT
391 */
392 mtimer_t s_receive_timer;
393 /**
394 * Stream start time
395 */
396 int s_timeout;
397 int s_grace_delay;
398 int64_t s_start_time;
399
400
401 /*********************************************************
402 *
403 * Streaming part of transport
404 *
405 * All access to fields below this must be protected with
406 * s_stream_mutex held.
407 *
408 * Note: Code holding s_stream_mutex should _never_
409 * acquire global_lock while already holding s_stream_mutex.
410 *
411 */
412
413 /**
414 * Mutex to be held during streaming.
415 * This mutex also protects all elementary_stream_t instances for this
416 * transport.
417 */
418 pthread_mutex_t s_stream_mutex;
419
420 /**
421 *
422 */
423 int s_streaming_status;
424
425 // Progress
426 #define TSS_INPUT_HARDWARE 0x1
427 #define TSS_INPUT_SERVICE 0x2
428 #define TSS_MUX_PACKETS 0x4
429 #define TSS_PACKETS 0x8
430 #define TSS_NO_ACCESS 0x10
431 #define TSS_CA_CHECK 0x20
432
433
434 // Errors
435 #define TSS_GRACEPERIOD 0x10000
436 #define TSS_NO_DESCRAMBLER 0x20000
437 #define TSS_TIMEOUT 0x40000
438 #define TSS_TUNING 0x80000
439
440 #define TSS_ERRORS 0xffff0000
441
442
443 /**
444 *
445 */
446 int s_streaming_live;
447 int s_running;
448 int s_pending_restart;
449
450 // Live status
451 #define TSS_LIVE 0x01
452
453 /**
454 * For simple streaming sources (such as video4linux) keeping
455 * track of the video and audio stream is convenient.
456 */
457 #ifdef MOVE_TO_V4L
458 elementary_stream_t *s_video;
459 elementary_stream_t *s_audio;
460 #endif
461
462 /**
463 * Descrambling support
464 */
465
466 uint16_t s_dvb_forcecaid;
467 struct th_descrambler_list s_descramblers;
468 uint8_t s_scrambled_seen;
469 uint8_t s_scrambled_pass;
470 th_descrambler_runtime_t *s_descramble;
471 void *s_descrambler; /* last active descrambler */
472 descramble_info_t *s_descramble_info;
473
474 /**
475 * List of all and filtered components.
476 */
477 struct elementary_stream_queue s_components;
478 struct elementary_stream_queue s_filt_components;
479 int s_last_pid;
480 elementary_stream_t *s_last_es;
481
482 /**
483 * Delivery pad, this is were we finally deliver all streaming output
484 */
485 streaming_pad_t s_streaming_pad;
486
487 tvhlog_limit_t s_tei_log;
488
489 int64_t s_current_pts;
490
491 /*
492 * Local channel numbers per bouquet
493 */
494 LIST_HEAD(,service_lcn) s_lcns;
495
496 } service_t;
497
498
499
500
501
502 void service_init(void);
503 void service_done(void);
504
505 int service_start(service_t *t, int instance, int weight, int flags,
506 int timeout, int postpone);
507 void service_stop(service_t *t);
508
509 void service_build_filter(service_t *t);
510
511 service_t *service_create0(service_t *t, int service_type, const idclass_t *idc,
512 const char *uuid, int source_type, htsmsg_t *conf);
513
514 #define service_create(t, y, c, u, s, m)\
515 (struct t*)service_create0(calloc(1, sizeof(struct t), y, &t##_class, c, u, s, m)
516
517 void service_unref(service_t *t);
518
519 void service_ref(service_t *t);
520
service_find(const char * identifier)521 static inline service_t *service_find(const char *identifier)
522 { return idnode_find(identifier, &service_class, NULL); }
523 #define service_find_by_identifier service_find
524
525 service_instance_t *service_find_instance(struct service *s,
526 struct channel *ch,
527 struct tvh_input *source,
528 profile_chain_t *prch,
529 service_instance_list_t *sil,
530 int *error, int weight,
531 int flags, int timeout,
532 int postpone);
533
534 elementary_stream_t *service_stream_find_(service_t *t, int pid);
535
536 static inline elementary_stream_t *
service_stream_find(service_t * t,int pid)537 service_stream_find(service_t *t, int pid)
538 {
539 if (t->s_last_pid != (pid))
540 return service_stream_find_(t, pid);
541 else
542 return t->s_last_es;
543 }
544
545 elementary_stream_t *service_stream_create(service_t *t, int pid,
546 streaming_component_type_t type);
547
548 void service_settings_write(service_t *t);
549
550 const char *service_servicetype_txt(service_t *t);
551
552 int service_has_audio_or_video(service_t *t);
553 int service_is_sdtv(service_t *t);
554 int service_is_uhdtv(service_t *t);
555 int service_is_hdtv(service_t *t);
556 int service_is_radio(service_t *t);
557 int service_is_other(service_t *t);
558 #define service_is_tv(s) (service_is_hdtv(s) || service_is_sdtv(s) || service_is_uhdtv(s))
559
560 int service_is_encrypted ( service_t *t );
561
562 void service_set_enabled ( service_t *t, int enabled, int _auto );
563
564 void service_destroy(service_t *t, int delconf);
565
566 void service_remove_raw(service_t *);
567
568 void service_remove_subscriber(service_t *t, struct th_subscription *s,
569 int reason);
570
571
572 void service_send_streaming_status(service_t *t);
573
574 void service_set_streaming_status_flags_(service_t *t, int flag);
575
576 static inline void
service_set_streaming_status_flags(service_t * t,int flag)577 service_set_streaming_status_flags(service_t *t, int flag)
578 {
579 int n = t->s_streaming_status;
580 if ((n & flag) != flag)
581 service_set_streaming_status_flags_(t, n | flag);
582 }
583
584 static inline void
service_reset_streaming_status_flags(service_t * t,int flag)585 service_reset_streaming_status_flags(service_t *t, int flag)
586 {
587 int n = t->s_streaming_status;
588 if ((n & flag) != 0)
589 service_set_streaming_status_flags_(t, n & ~flag);
590 }
591
592
593 struct streaming_start;
594 struct streaming_start *service_build_stream_start(service_t *t);
595
596 void service_restart(service_t *t);
597
598 void service_stream_destroy(service_t *t, elementary_stream_t *st);
599
600 void service_request_save(service_t *t, int restart);
601
602 void service_source_info_free(source_info_t *si);
603
604 void service_source_info_copy(source_info_t *dst, const source_info_t *src);
605
606 void service_make_nicename(service_t *t);
607
608 const char *service_nicename(service_t *t);
609
610 const char *service_component_nicename(elementary_stream_t *st);
611
612 const char *service_adapter_nicename(service_t *t, char *buf, size_t len);
613
614 const char *service_tss2text(int flags);
615
service_tss_is_error(int flags)616 static inline int service_tss_is_error(int flags)
617 {
618 return flags & TSS_ERRORS ? 1 : 0;
619 }
620
621 void service_refresh_channel(service_t *t);
622
623 int tss2errcode(int tss);
624
625 htsmsg_t *servicetype_list (void);
626
627 void service_load ( service_t *s, htsmsg_t *c );
628
629 void service_save ( service_t *s, htsmsg_t *c );
630
631 void service_remove_unseen(const char *type, int days);
632
633 void sort_elementary_streams(service_t *t);
634
635 const char *service_get_channel_name (service_t *s);
636 const char *service_get_full_channel_name (service_t *s);
637 int64_t service_get_channel_number (service_t *s);
638 const char *service_get_channel_icon (service_t *s);
639 const char *service_get_channel_epgid (service_t *s);
640
641 void service_memoryinfo (service_t *s, int64_t *size);
642
643 void service_mapped (service_t *s);
644
645 #endif // SERVICE_H__
646