root/trunk/tvheadend/src/tvhead.h @ 3083

Revision 3083, 14.7 KB (checked in by andoma, 15 months ago)

Figure out if we need to re-save a transport configuration instead of relying on if we've seen it once, it can not change

Line 
1/*
2 *  TV headend - structures
3 *  Copyright (C) 2007 Andreas �an
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 TV_HEAD_H
20#define TV_HEAD_H
21
22#include "config.h"
23
24#include <pthread.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <errno.h>
28#include <netinet/in.h>
29#include <sys/time.h>
30
31#include "queue.h"
32#include "avg.h"
33#include "hts_strtab.h"
34
35#include <libavcodec/avcodec.h>
36
37#include "redblack.h"
38
39extern pthread_mutex_t global_lock;
40extern pthread_mutex_t ffmpeg_lock;
41
42static inline void
43lock_assert0(pthread_mutex_t *l, const char *file, int line)
44{
45  if(pthread_mutex_trylock(l) == EBUSY)
46    return;
47
48  fprintf(stderr, "Mutex not held at %s:%d\n", file, line);
49  abort();
50}
51
52#define lock_assert(l) lock_assert0(l, __FILE__, __LINE__)
53
54
55/*
56 * Commercial status
57 */
58typedef enum {
59  COMMERCIAL_UNKNOWN,
60  COMMERCIAL_YES,
61  COMMERCIAL_NO,
62} th_commercial_advice_t;
63
64
65/*
66 * global timer
67 */
68
69
70typedef void (gti_callback_t)(void *opaque);
71
72typedef struct gtimer {
73  LIST_ENTRY(gtimer) gti_link;
74  gti_callback_t *gti_callback;
75  void *gti_opaque;
76  time_t gti_expire;
77} gtimer_t;
78
79void gtimer_arm(gtimer_t *gti, gti_callback_t *callback, void *opaque,
80                int delta);
81
82void gtimer_arm_abs(gtimer_t *gti, gti_callback_t *callback, void *opaque,
83                    time_t when);
84
85void gtimer_disarm(gtimer_t *gti);
86
87
88/*
89 * List / Queue header declarations
90 */
91LIST_HEAD(th_subscription_list, th_subscription);
92RB_HEAD(channel_tree, channel);
93TAILQ_HEAD(channel_queue, channel);
94LIST_HEAD(channel_list, channel);
95LIST_HEAD(event_list, event);
96RB_HEAD(event_tree, event);
97LIST_HEAD(dvr_entry_list, dvr_entry);
98TAILQ_HEAD(ref_update_queue, ref_update);
99LIST_HEAD(th_transport_list, th_transport);
100RB_HEAD(th_transport_tree, th_transport);
101TAILQ_HEAD(th_transport_queue, th_transport);
102LIST_HEAD(th_stream_list, th_stream);
103LIST_HEAD(th_muxer_list, th_muxer);
104LIST_HEAD(th_muxstream_list, th_muxstream);
105LIST_HEAD(th_descrambler_list, th_descrambler);
106TAILQ_HEAD(th_refpkt_queue, th_refpkt);
107TAILQ_HEAD(th_muxpkt_queue, th_muxpkt);
108LIST_HEAD(dvr_autorec_entry_list, dvr_autorec_entry);
109TAILQ_HEAD(th_pktref_queue, th_pktref);
110LIST_HEAD(streaming_target_list, streaming_target);
111
112
113typedef enum {
114  SCT_MPEG2VIDEO = 1,
115  SCT_MPEG2AUDIO,
116  SCT_H264,
117  SCT_AC3,
118  SCT_TELETEXT,
119  SCT_SUBTITLES,
120  SCT_CA,
121  SCT_PAT,
122  SCT_PMT,
123  SCT_AAC,
124} streaming_component_type_t;
125
126
127/**
128 * A streaming pad generates data.
129 * It has one or more streaming targets attached to it.
130 *
131 * We support two different streaming target types:
132 * One is callback driven and the other uses a queue + thread.
133 *
134 * Targets which already has a queueing intrastructure in place (such
135 * as HTSP) does not need any interim queues so it would be a waste. That
136 * is why we have the callback target.
137 *
138 */
139typedef struct streaming_pad {
140  struct streaming_target_list sp_targets;
141  int sp_ntargets;
142} streaming_pad_t;
143
144
145TAILQ_HEAD(streaming_message_queue, streaming_message);
146
147/**
148 * Streaming messages types
149 */
150typedef enum {
151  SMT_PACKET,       // sm_data is a th_pkt. Unref when destroying msg
152  SMT_START,        // sm_data is a htsmsg, see transport_build_stream_msg()
153  SMT_STOP,         // sm_data is a htsmsg
154  SMT_TRANSPORT_STATUS, // sm_code is TRANSPORT_STATUS_
155  SMT_EXIT,             // Used to signal exit to threads
156  SMT_NOSOURCE,
157} streaming_message_type_t;
158
159
160/**
161 * Streaming messages are sent from the pad to its receivers
162 */
163typedef struct streaming_message {
164  TAILQ_ENTRY(streaming_message) sm_link;
165  streaming_message_type_t sm_type;
166  union {
167    void *sm_data;
168    int sm_code;
169  };
170} streaming_message_t;
171
172/**
173 * A streaming target receives data.
174 */
175
176typedef void (st_callback_t)(void *opauqe, streaming_message_t *sm);
177
178typedef struct streaming_target {
179  LIST_ENTRY(streaming_target) st_link;
180  streaming_pad_t *st_pad;               /* Source we are linked to */
181
182  st_callback_t *st_cb;
183  void *st_opaque;
184} streaming_target_t;
185
186
187/**
188 *
189 */
190typedef struct streaming_queue {
191 
192  streaming_target_t sq_st;
193
194  pthread_mutex_t sq_mutex;              /* Protects sp_queue */
195  pthread_cond_t  sq_cond;               /* Condvar for signalling new
196                                            packets */
197 
198  struct streaming_message_queue sq_queue;
199
200} streaming_queue_t;
201
202
203
204/**
205 * Descrambler superclass
206 *
207 * Created/Destroyed on per-transport basis upon transport start/stop
208 */
209typedef struct th_descrambler {
210  LIST_ENTRY(th_descrambler) td_transport_link;
211
212  void (*td_table)(struct th_descrambler *d, struct th_transport *t,
213                   struct th_stream *st, uint8_t *section, int section_len);
214
215  int (*td_descramble)(struct th_descrambler *d, struct th_transport *t,
216                       struct th_stream *st, uint8_t *tsb);
217
218  void (*td_stop)(struct th_descrambler *d);
219
220} th_descrambler_t;
221
222
223
224/*
225 * Section callback, called when a PSI table is fully received
226 */
227typedef void (pid_section_callback_t)(struct th_transport *t,
228                                      struct th_stream *pi,
229                                      uint8_t *section, int section_len);
230
231/*
232 * Stream, one media component for a transport.
233 *
234 * XXX: This should be renamed to 'elementary_stream' or something
235 */
236typedef struct th_stream {
237
238  LIST_ENTRY(th_stream) st_link;
239 
240  streaming_component_type_t st_type;
241  int st_index;
242
243  char st_lang[4];           /* ISO 639 3-letter language code */
244
245  uint16_t st_pid;
246  uint8_t st_cc;             /* Last CC */
247  uint8_t st_cc_valid;       /* Is CC valid at all? */
248
249  avgstat_t st_cc_errors;
250  avgstat_t st_rate;
251
252  int st_demuxer_fd;
253  int st_peak_presentation_delay; /* Max seen diff. of DTS and PTS */
254
255  struct psi_section *st_section;
256  int st_section_docrc;           /* Set if we should verify CRC on tables */
257  pid_section_callback_t *st_got_section;
258  void *st_got_section_opaque;
259
260  /* PCR recovery */
261
262  int st_pcr_recovery_fails;
263  int64_t st_pcr_real_last;     /* realtime clock when we saw last PCR */
264  int64_t st_pcr_last;          /* PCR clock when we saw last PCR */
265  int64_t st_pcr_drift;
266
267  /* For transport stream packet reassembly */
268
269  uint8_t *st_buffer;
270  int st_buffer_ptr;
271  int st_buffer_size;
272  int st_buffer_errors;   /* Errors accumulated for this packet */
273  uint32_t st_startcond;
274  uint32_t st_startcode;
275  uint32_t st_startcode_offset;
276  int st_parser_state;
277  void *st_priv;          /* Parser private data */
278
279  struct th_pkt *st_curpkt;
280  int64_t st_curpts;
281  int64_t st_curdts;
282  int64_t st_prevdts;
283  int64_t st_nextdts;
284  int st_frame_duration;
285
286  /* DTS generator */
287
288  int64_t st_dts_epoch;  /* upper bits (auto generated) */
289  int64_t st_last_dts;
290
291  /* Codec */
292
293  struct AVCodecContext *st_ctx;
294  struct AVCodecParserContext *st_parser;
295
296  /* Temporary frame store for calculating PTS */
297
298  struct th_pktref_queue st_ptsq;
299  int st_ptsq_len;
300
301  /* Temporary frame store for calculating duration */
302
303  struct th_pktref_queue st_durationq;
304
305  /* ca id for this stream */
306
307  uint16_t st_caid;
308
309  /* Remuxing information */
310  AVRational st_tb;
311
312  int st_vbv_size;        /* Video buffer size (in bytes) */
313  int st_vbv_delay;       /* -1 if CBR */
314
315} th_stream_t;
316
317
318/**
319 *
320 */
321typedef enum {
322
323  /** No status known */
324  TRANSPORT_FEED_UNKNOWN,
325
326  /** No packets are received from source at all */
327  TRANSPORT_FEED_NO_INPUT,
328
329  /** No input is received from source destined for this transport */
330  TRANSPORT_FEED_NO_DEMUXED_INPUT,
331
332  /** Raw input seen but nothing has really been decoded */
333  TRANSPORT_FEED_RAW_INPUT,
334
335  /** No descrambler is able to decrypt the stream */
336  TRANSPORT_FEED_NO_DESCRAMBLER,
337
338  /** Potential descrambler is available, but access is denied */
339  TRANSPORT_FEED_NO_ACCESS,
340
341  /** Packet are being parsed. */
342  TRANSPORT_FEED_VALID_PACKETS,
343
344} transport_feed_status_t;
345
346
347/**
348 * A Transport (or in MPEG TS terms: a 'service')
349 */
350typedef struct th_transport {
351 
352  const char *tht_name;
353
354  LIST_ENTRY(th_transport) tht_hash_link;
355
356  enum {
357    TRANSPORT_DVB,
358    TRANSPORT_IPTV,
359    TRANSPORT_V4L,
360  } tht_type;
361
362  enum {
363    /**
364     * Transport is idle.
365     */
366    TRANSPORT_IDLE,
367
368    /**
369     * Transport producing output
370     */
371    TRANSPORT_RUNNING,
372
373    /**
374     * Destroyed, but pointer is till valid.
375     * This would be the case if transport_destroy() did not actually free
376     * the transport because there are references held to it.
377     *
378     * Reference counts can be used so that code can hold a pointer to
379     * a transport without having the global lock.
380     *
381     * Note: No fields in the transport may be accessed without the
382     * global lock held. Thus, the global_lock must be reaquired and
383     * then tht_status must be checked. If it is ZOMBIE the code must
384     * just drop the refcount and pretend that the transport never
385     * was there in the first place.
386     */
387    TRANSPORT_ZOMBIE,
388  } tht_status;
389
390  /**
391   * Refcount, operated using atomic.h ops.
392   */
393  int tht_refcount;
394
395  /**
396   *
397   */
398  int tht_flags;
399
400#define THT_DEBUG 0x1
401
402  /**
403   * Source type is used to determine if an output requesting
404   * MPEG-TS can shortcut all the parsing and remuxing.
405   */
406  enum {
407    THT_MPEG_TS,
408    THT_OTHER,
409  } tht_source_type;
410
411 
412  /**
413   * PID carrying the programs PCR.
414   * XXX: We don't support transports that does not carry
415   * the PCR in one of the content streams.
416   */
417  uint16_t tht_pcr_pid;
418
419  /**
420   * PID for the PMT of this MPEG-TS stream.
421   */
422  uint16_t tht_pmt_pid;
423
424  /**
425   * Set if transport is disabled. If disabled it should not be
426   * considered when chasing for available transports during
427   * subscription scheduling.
428   */
429  int tht_disabled;
430
431 
432  LIST_ENTRY(th_transport) tht_mux_link;
433
434  LIST_ENTRY(th_transport) tht_active_link;
435
436  LIST_HEAD(, th_subscription) tht_subscriptions;
437
438  int (*tht_start_feed)(struct th_transport *t, unsigned int weight,
439                        int status, int force_start);
440
441  void (*tht_stop_feed)(struct th_transport *t);
442
443  void (*tht_config_change)(struct th_transport *t);
444
445  const char *(*tht_networkname)(struct th_transport *t);
446
447  const char *(*tht_sourcename)(struct th_transport *t);
448
449  int (*tht_quality_index)(struct th_transport *t);
450
451
452  /*
453   * Per source type structs
454   */
455  struct th_dvb_mux_instance *tht_dvb_mux_instance;
456
457  /**
458   * Unique identifer (used for storing on disk, etc)
459   */
460  char *tht_identifier;
461
462  /**
463   * Service ID according to EN 300 468
464   */
465  uint16_t tht_dvb_service_id;
466
467  /**
468   * Service name (eg. DVB service name as specified by EN 300 468)
469   */
470  char *tht_svcname;
471
472  /**
473   * Provider name (eg. DVB provider name as specified by EN 300 468)
474   */
475  char *tht_provider;
476
477  enum {
478    /* Service types defined in EN 300 468 */
479
480    ST_SDTV       = 0x1,    /* SDTV (MPEG2) */
481    ST_RADIO      = 0x2,
482    ST_HDTV       = 0x11,   /* HDTV (MPEG2) */
483    ST_AC_SDTV    = 0x16,   /* Advanced codec SDTV */
484    ST_AC_HDTV    = 0x19,   /* Advanced codec HDTV */
485  } tht_servicetype;
486
487
488  /**
489   * Teletext...
490   */
491  th_commercial_advice_t tht_tt_commercial_advice;
492  int tht_tt_rundown_content_length;
493  time_t tht_tt_clock;   /* Network clock as determined by teletext decoder */
494 
495  /**
496   * Channel mapping
497   */
498  LIST_ENTRY(th_transport) tht_ch_link;
499  struct channel *tht_ch;
500  char *tht_chname;
501
502  /**
503   * Service probe, see serviceprobe.c for details
504   */
505  int tht_sp_onqueue;
506  TAILQ_ENTRY(th_transport) tht_sp_link;
507
508  /**
509   * Timer which is armed at transport start. Once it fires
510   * it will check if any packets has been parsed. If not the status
511   * will be set to TRANSPORT_STATUS_NO_INPUT
512   */
513  gtimer_t tht_receive_timer;
514
515  /*********************************************************
516   *
517   * Streaming part of transport
518   *
519   * All access to fields below this must be protected with
520   * tht_stream_mutex held.
521   *
522   * Note: Code holding tht_stream_mutex should _never_
523   * acquire global_lock while already holding tht_stream_mutex.
524   *
525   */
526
527  /**
528   * Mutex to be held during streaming.
529   * This mutex also protects all th_stream_t instances for this
530   * transport.
531   */
532  pthread_mutex_t tht_stream_mutex;
533
534  /**
535   * Last known data status (or error)
536   */                     
537  transport_feed_status_t tht_feed_status;
538
539  /**
540   * Set as soon as we get some kind of activity
541   */
542  transport_feed_status_t tht_input_status;
543
544
545  /**
546   * For simple streaming sources (such as video4linux) keeping
547   * track of the video and audio stream is convenient.
548   */
549  th_stream_t *tht_video;
550  th_stream_t *tht_audio;
551 
552  /**
553   * Average continuity errors
554   */
555  avgstat_t tht_cc_errors;
556
557  /**
558   * Average bitrate
559   */
560  avgstat_t tht_rate;
561
562  /**
563   * Descrambling support
564   */
565
566  struct th_descrambler_list tht_descramblers;
567  int tht_scrambled;
568  int tht_caid;
569
570  /**
571   * Used by parsing code to normalize timestamp to zero
572   */
573  int64_t tht_dts_start;
574
575  /**
576   * PCR drift compensation. This should really be per-packet.
577   */
578  int64_t  tht_pcr_drift;
579
580  /**
581   * List of all components.
582   */
583  struct th_stream_list tht_components;
584
585
586  /**
587   * Delivery pad, this is were we finally deliver all streaming output
588   */
589  streaming_pad_t tht_streaming_pad;
590
591
592} th_transport_t;
593
594
595
596
597const char *streaming_component_type2txt(streaming_component_type_t s);
598
599static inline unsigned int tvh_strhash(const char *s, unsigned int mod)
600{
601  unsigned int v = 5381;
602  while(*s)
603    v += (v << 5) + v + *s++;
604  return v % mod;
605}
606
607#define MIN(a,b) ((a) < (b) ? (a) : (b))
608#define MAX(a,b) ((a) > (b) ? (a) : (b))
609
610void tvh_str_set(char **strp, const char *src);
611void tvh_str_update(char **strp, const char *src);
612
613void tvhlog(int severity, const char *subsys, const char *fmt, ...);
614
615void tvhlog_spawn(int severity, const char *subsys, const char *fmt, ...);
616
617#define LOG_EMERG       0       /* system is unusable */
618#define LOG_ALERT       1       /* action must be taken immediately */
619#define LOG_CRIT        2       /* critical conditions */
620#define LOG_ERR         3       /* error conditions */
621#define LOG_WARNING     4       /* warning conditions */
622#define LOG_NOTICE      5       /* normal but significant condition */
623#define LOG_INFO        6       /* informational */
624#define LOG_DEBUG       7       /* debug-level messages */
625
626extern int log_debug;
627
628#define DEBUGLOG(subsys, fmt...) do { \
629 if(log_debug) \
630  tvhlog(LOG_DEBUG, subsys, fmt); \
631} while(0)
632
633
634static inline int64_t
635getclock_hires(void)
636{
637  int64_t now;
638  struct timeval tv;
639
640  gettimeofday(&tv, NULL);
641  now = (uint64_t)tv.tv_sec * 1000000ULL + (uint64_t)tv.tv_usec;
642  return now;
643}
644
645
646
647
648extern time_t dispatch_clock;
649extern struct th_transport_list all_transports;
650extern struct channel_tree channel_name_tree;
651
652#endif /* TV_HEAD_H */
Note: See TracBrowser for help on using the browser.