]> git.sesse.net Git - vlc/blob - modules/stream_out/select.c
stream_output/select.c: explicitly select an ES
[vlc] / modules / stream_out / select.c
1 /*****************************************************************************
2  * select.c: Select individual es to enable or disable from stream
3  *****************************************************************************
4  * Copyright (C) 2006-2011 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Paul Saman <jpsaman@videolan.org>
8  * Based upon autodel.c written by: Christophe Massiot <massiot@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_sout.h>
36 #include <vlc_block.h>
37 #include <vlc_network.h>
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 static int  Open    ( vlc_object_t * );
43 static void Close   ( vlc_object_t * );
44
45 #define PORT_TEXT N_("Command UDP port")
46 #define PORT_LONGTEXT N_( \
47     "UDP port to listen to for commands (show | enable <pid> | disable <pid>)." )
48
49 #define DISABLE_TEXT N_("Disable ES id")
50 #define DISABLE_LONGTEXT N_( \
51     "Disable ES id at startup." )
52
53 #define ENABLE_TEXT N_("Enable ES id")
54 #define ENABLE_LONGTEXT N_( \
55     "Only enable ES id at startup." )
56
57 #define SOUT_CFG_PREFIX "sout-select-"
58
59 vlc_module_begin ()
60     set_shortname(N_("Select"))
61     set_description(N_("Select individual es to enable or disable from stream"))
62     set_capability("sout stream", 50 )
63     add_integer(SOUT_CFG_PREFIX "port", 5001, PORT_TEXT, PORT_LONGTEXT, true)
64     add_integer(SOUT_CFG_PREFIX "disable", -1, DISABLE_TEXT, DISABLE_LONGTEXT, false)
65     add_integer(SOUT_CFG_PREFIX "enable", -1, ENABLE_TEXT, ENABLE_LONGTEXT, false)
66     add_shortcut("select")
67     set_callbacks(Open, Close)
68 vlc_module_end ()
69
70 /*****************************************************************************
71  * Local prototypes
72  *****************************************************************************/
73 static sout_stream_id_t *Add   (sout_stream_t *, es_format_t *);
74 static int               Del   (sout_stream_t *, sout_stream_id_t *);
75 static int               Send  (sout_stream_t *, sout_stream_id_t *, block_t *);
76
77 static void* Command(void *);
78
79 struct sout_stream_id_t
80 {
81     sout_stream_id_t *id;
82     es_format_t fmt;
83     bool b_error;
84     bool b_enabled;
85 };
86
87 struct sout_stream_sys_t
88 {
89     sout_stream_id_t **pp_es;
90     int i_es_num;
91
92     vlc_mutex_t es_lock;
93     vlc_thread_t thread;
94
95     int i_fd;
96     int i_id_disable;
97     int i_id_enable;
98 };
99
100 static const char *const ppsz_sout_options[] = {
101     "enable", "disable", "port", NULL
102 };
103
104 /*****************************************************************************
105  * Open:
106  *****************************************************************************/
107 static int Open(vlc_object_t *p_this)
108 {
109     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
110     sout_stream_sys_t *p_sys;
111
112     p_sys = malloc(sizeof(sout_stream_sys_t));
113     if (!p_sys)
114         return VLC_ENOMEM;
115
116     if (!p_stream->p_next)
117     {
118         msg_Err( p_stream, "cannot create chain" );
119         free( p_sys );
120         return VLC_EGENERIC;
121     }
122
123     config_ChainParse(p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
124                       p_stream->p_cfg);
125
126     int port = var_GetInteger(p_stream, SOUT_CFG_PREFIX "port");
127     p_sys->i_fd = net_ListenUDP1(VLC_OBJECT(p_stream), NULL, port);
128     if (p_sys->i_fd < 0)
129     {
130         free( p_sys );
131         return VLC_EGENERIC;
132     }
133     p_sys->i_id_disable = var_GetInteger(p_stream, SOUT_CFG_PREFIX "disable");
134     p_sys->i_id_enable = var_GetInteger(p_stream, SOUT_CFG_PREFIX "enable");
135
136     p_sys->pp_es = NULL;
137     p_sys->i_es_num = 0;
138
139     p_stream->pf_add    = Add;
140     p_stream->pf_del    = Del;
141     p_stream->pf_send   = Send;
142
143     p_stream->p_sys     = p_sys;
144
145     vlc_mutex_init(&p_sys->es_lock);
146
147     if(vlc_clone(&p_sys->thread, Command, p_stream, VLC_THREAD_PRIORITY_LOW))
148     {
149         vlc_mutex_destroy(&p_sys->es_lock);
150         free(p_sys);
151         return VLC_EGENERIC;
152     }
153
154     /* update p_sout->i_out_pace_nocontrol */
155     p_stream->p_sout->i_out_pace_nocontrol++;
156
157     return VLC_SUCCESS;
158 }
159
160 /*****************************************************************************
161  * Close:
162  *****************************************************************************/
163 static void Close (vlc_object_t * p_this)
164 {
165     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
166     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
167
168     /* Stop the thread */
169     vlc_cancel(p_sys->thread);
170     vlc_join(p_sys->thread, NULL);
171
172     /* Free the ressources */
173     net_Close( p_sys->i_fd );
174     vlc_mutex_destroy(&p_sys->es_lock);
175
176     p_stream->p_sout->i_out_pace_nocontrol--;
177
178     free(p_sys);
179 }
180
181 /****************************************************************************
182  * Command Thread:
183  ****************************************************************************/
184 static void* Command(void *p_this)
185 {
186     sout_stream_t *p_stream = (sout_stream_t *)p_this;
187     sout_stream_sys_t *p_sys = p_stream->p_sys;
188
189     while (vlc_object_alive(p_stream))
190     {
191         char psz_buffer[20];
192
193         int i_len = recv(p_sys->i_fd, psz_buffer, sizeof(psz_buffer)-1, 0);
194         if (i_len < 4)
195             continue;
196
197         psz_buffer[i_len] = '\0';
198         msg_Info( p_stream, "command: %s", psz_buffer );
199
200         if (strncmp(psz_buffer, "show", 4) == 0)
201         {
202             vlc_mutex_lock(&p_sys->es_lock);
203             mutex_cleanup_push(&p_sys->es_lock);
204             for (int i = 0; i < p_sys->i_es_num; i++)
205             {
206                 i_len = snprintf(psz_buffer, sizeof(psz_buffer), "%.4s : %d",
207                                  (char *)&p_sys->pp_es[i]->fmt.i_codec,
208                                  p_sys->pp_es[i]->fmt.i_id);
209                 psz_buffer[i_len] = '\0';
210                 msg_Info(p_stream, psz_buffer);
211             }
212             vlc_cleanup_pop();
213         }
214         else
215         {
216             bool b_apply = false;
217             bool b_select = false;
218             int i_pid = 0x1FFF;
219
220             if (strncmp(psz_buffer, "enable", 6) == 0)
221             {
222                 i_pid = atol(psz_buffer+7);
223                 b_select = true;
224                 b_apply = true;
225             }
226             else if (strncmp(psz_buffer, "disable", 7) == 0)
227             {
228                 i_pid = atol(psz_buffer+8);
229                 b_apply = true;
230             }
231
232             if (b_apply)
233             {
234                 vlc_mutex_lock(&p_sys->es_lock);
235                 mutex_cleanup_push(&p_sys->es_lock);
236                 for (int i = 0; i < p_sys->i_es_num; i++)
237                 {
238                     msg_Info(p_stream, "elementary stream pid %d",
239                              p_sys->pp_es[i]->fmt.i_id);
240                     if (p_sys->pp_es[i]->fmt.i_id == i_pid)
241                     {
242                         p_sys->pp_es[i]->b_enabled = b_select;
243                         msg_Info(p_stream, "%s: %d", b_select ? "enable" : "disable", i_pid);
244                     }
245                 }
246                 vlc_cleanup_pop();
247             }
248         }
249     }
250
251     return NULL;
252 }
253
254 static sout_stream_id_t *Add(sout_stream_t *p_stream, es_format_t *p_fmt)
255 {
256     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
257     sout_stream_id_t *p_es = malloc(sizeof(sout_stream_id_t));
258     if( !p_es )
259         return NULL;
260
261     p_es->fmt = *p_fmt;
262     p_es->id = NULL;
263     p_es->b_error = false;
264     if (p_sys->i_id_disable >= -1)
265         p_es->b_enabled = (p_es->fmt.i_id == p_sys->i_id_disable) ? false : true;
266     else if (p_sys->i_id_enable >= -1)
267         p_es->b_enabled = (p_es->fmt.i_id == p_sys->i_id_enable) ? true: false;
268
269     vlc_mutex_lock(&p_sys->es_lock);
270     TAB_APPEND(p_sys->i_es_num, p_sys->pp_es, p_es);
271     vlc_mutex_unlock(&p_sys->es_lock);
272
273     return p_es;
274 }
275
276 static int Del(sout_stream_t *p_stream, sout_stream_id_t *p_es)
277 {
278     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
279     sout_stream_id_t *id = p_es->id;
280
281     vlc_mutex_lock(&p_sys->es_lock);
282     TAB_REMOVE(p_sys->i_es_num, p_sys->pp_es, p_es);
283     vlc_mutex_unlock(&p_sys->es_lock);
284
285     free(p_es);
286
287     if (id != NULL)
288         return p_stream->p_next->pf_del(p_stream->p_next, id);
289     else
290         return VLC_SUCCESS;
291 }
292
293 static int Send(sout_stream_t *p_stream, sout_stream_id_t *p_es,
294                 block_t *p_buffer)
295 {
296     if (p_es->id == NULL && !p_es->b_error)
297     {
298         p_es->id = p_stream->p_next->pf_add(p_stream->p_next, &p_es->fmt);
299         if (p_es->id == NULL)
300         {
301             p_es->b_error = true;
302             msg_Err(p_stream, "couldn't create chain for id %d",
303                     p_es->fmt.i_id);
304         }
305     }
306
307     if (!p_es->b_error && p_es->b_enabled)
308         p_stream->p_next->pf_send(p_stream->p_next, p_es->id, p_buffer);
309     else
310         block_ChainRelease(p_buffer);
311
312     return VLC_SUCCESS;
313 }