1 /*
2  *  tvheadend, Stream Profile
3  *  Copyright (C) 2014 Jaroslav Kysela
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 __TVH_PROFILE_H__
20 #define __TVH_PROFILE_H__
21 
22 #include "tvheadend.h"
23 #include "idnode.h"
24 #include "muxer.h"
25 
26 typedef enum {
27   PROFILE_SPRIO_NOTSET = 0,
28   PROFILE_SPRIO_IMPORTANT,
29   PROFILE_SPRIO_HIGH,
30   PROFILE_SPRIO_NORMAL,
31   PROFILE_SPRIO_LOW,
32   PROFILE_SPRIO_UNIMPORTANT,
33   PROFILE_SPRIO_DVR_IMPORTANT,
34   PROFILE_SPRIO_DVR_HIGH,
35   PROFILE_SPRIO_DVR_NORMAL,
36   PROFILE_SPRIO_DVR_LOW,
37   PROFILE_SPRIO_DVR_UNIMPORTANT
38 } profile_sprio_t;
39 
40 typedef enum {
41   PROFILE_SVF_NONE = 0,
42   PROFILE_SVF_SD,
43   PROFILE_SVF_HD,
44   PROFILE_SVF_UHD
45 } profile_svfilter_t;
46 
47 struct profile;
48 struct muxer;
49 struct streaming_target;
50 struct streaming_start;
51 
52 extern const idclass_t profile_class;
53 extern const idclass_t profile_mpegts_pass_class;
54 extern const idclass_t profile_matroska_class;
55 
56 TAILQ_HEAD(profile_entry_queue, profile);
57 
58 extern struct profile_entry_queue profiles;
59 
60 typedef struct profile *(*profile_builder_t)(void);
61 
62 typedef struct profile_build {
63   LIST_ENTRY(profile_build) link;
64   const idclass_t *clazz;
65   profile_builder_t build;
66 } profile_build_t;
67 
68 typedef LIST_HEAD(, profile_build) profile_builders_queue;
69 
70 extern profile_builders_queue profile_builders;
71 
72 typedef struct profile_sharer {
73   streaming_target_t        prsh_input;
74   LIST_HEAD(,profile_chain) prsh_chains;
75   struct profile_chain     *prsh_master;
76   struct streaming_start   *prsh_start_msg;
77   struct streaming_target  *prsh_tsfix;
78 #if ENABLE_LIBAV
79   struct streaming_target  *prsh_transcoder;
80 #endif
81 } profile_sharer_t;
82 
83 typedef struct profile_chain {
84   LIST_ENTRY(profile_chain) prch_link;
85   int                       prch_linked;
86 
87   struct profile_sharer    *prch_sharer;
88   LIST_ENTRY(profile_chain) prch_sharer_link;
89 
90   struct profile           *prch_pro;
91   void                     *prch_id;
92 
93   int64_t                   prch_ts_delta;
94 
95   int                       prch_flags;
96   int                       prch_stop;
97   int                       prch_start_pending;
98   int                       prch_sq_used;
99   struct streaming_queue    prch_sq;
100   struct streaming_target  *prch_post_share;
101   struct streaming_target  *prch_st;
102   struct muxer             *prch_muxer;
103   struct streaming_target  *prch_gh;
104   struct streaming_target  *prch_tsfix;
105 #if ENABLE_TIMESHIFT
106   struct streaming_target  *prch_timeshift;
107 #endif
108   struct streaming_target   prch_input;
109   struct streaming_target  *prch_share;
110 
111   int (*prch_can_share)(struct profile_chain *prch,
112                         struct profile_chain *joiner);
113 } profile_chain_t;
114 
115 typedef struct profile {
116   idnode_t pro_id;
117   TAILQ_ENTRY(profile) pro_link;
118 
119   int pro_refcount;
120 
121   LIST_HEAD(,dvr_config) pro_dvr_configs;
122   idnode_list_head_t pro_accesses;
123 
124   int pro_sflags;
125   int pro_enabled;
126   int pro_shield;
127   char *pro_name;
128   char *pro_comment;
129   int pro_prio;
130   int pro_fprio;
131   int pro_timeout;
132   int pro_restart;
133   int pro_contaccess;
134   int pro_ca_timeout;
135   int pro_swservice;
136   int pro_svfilter;
137 
138   void (*pro_free)(struct profile *pro);
139   void (*pro_conf_changed)(struct profile *pro);
140 
141   int (*pro_work)(profile_chain_t *prch, struct streaming_target *dst,
142                   uint32_t timeshift_period, int flags);
143   int (*pro_reopen)(profile_chain_t *prch,
144                     muxer_config_t *m_cfg, int flags);
145   int (*pro_open)(profile_chain_t *prch,
146                   muxer_config_t *m_cfg, int flags, size_t qsize);
147 } profile_t;
148 
149 void profile_register(const idclass_t *clazz, profile_builder_t builder);
150 
151 profile_t *profile_create
152   (const char *uuid, htsmsg_t *conf, int save);
153 
profile_grab(profile_t * pro)154 static inline void profile_grab( profile_t *pro )
155   { pro->pro_refcount++; }
156 
157 void profile_release_( profile_t *pro );
profile_release(profile_t * pro)158 static inline void profile_release( profile_t *pro )
159   {
160     assert(pro->pro_refcount > 0);
161     if (--pro->pro_refcount == 0) profile_release_(pro);
162   }
163 
164 int
165 profile_chain_work(profile_chain_t *prch, struct streaming_target *dst,
166                    uint32_t timeshift_period, int flags);
167 int
168 profile_chain_reopen(profile_chain_t *prch,
169                      muxer_config_t *m_cfg, int flags);
170 int
171 profile_chain_open(profile_chain_t *prch,
172                    muxer_config_t *m_cfg, int flags, size_t qsize);
173 void profile_chain_init(profile_chain_t *prch, profile_t *pro, void *id, int queue);
174 int  profile_chain_raw_open(profile_chain_t *prch, void *id, size_t qsize, int muxer);
175 void profile_chain_close(profile_chain_t *prch);
176 int  profile_chain_weight(profile_chain_t *prch, int custom);
177 
profile_find_by_uuid(const char * uuid)178 static inline profile_t *profile_find_by_uuid(const char *uuid)
179   {  return (profile_t*)idnode_find(uuid, &profile_class, NULL); }
180 profile_t *profile_find_by_name(const char *name, const char *alt);
181 profile_t *profile_find_by_list(htsmsg_t *uuids, const char *name,
182                                 const char *alt, int sflags);
183 int profile_verify(profile_t *pro, int sflags);
184 
185 htsmsg_t * profile_class_get_list(void *o, const char *lang);
186 
187 char *profile_validate_name(const char *name);
188 
189 const char *profile_get_name(profile_t *pro);
190 
191 void profile_get_htsp_list(htsmsg_t *array, htsmsg_t *filter);
192 
193 void profile_init(void);
194 void profile_done(void);
195 
196 #endif /* __TVH_PROFILE_H__ */
197