]> git.sesse.net Git - vlc/blob - modules/stream_out/select.c
Atmo: remove misleading comment about vlc_object_find()
[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 SOUT_CFG_PREFIX "sout-select-"
54
55 vlc_module_begin ()
56     set_shortname(N_("Select"))
57     set_description(N_("Select individual es to enable or disable from stream"))
58     set_capability("sout stream", 50 )
59     add_integer(SOUT_CFG_PREFIX "port", 5001, PORT_TEXT, PORT_LONGTEXT, true)
60     add_integer(SOUT_CFG_PREFIX "disable", -1, DISABLE_TEXT, DISABLE_LONGTEXT, false)
61     add_shortcut("select")
62     set_callbacks(Open, Close)
63 vlc_module_end ()
64
65 /*****************************************************************************
66  * Local prototypes
67  *****************************************************************************/
68 static sout_stream_id_t *Add   (sout_stream_t *, es_format_t *);
69 static int               Del   (sout_stream_t *, sout_stream_id_t *);
70 static int               Send  (sout_stream_t *, sout_stream_id_t *, block_t *);
71
72 static void* Command(void *);
73
74 struct sout_stream_id_t
75 {
76     sout_stream_id_t *id;
77     es_format_t fmt;
78     bool b_error;
79     bool b_enabled;
80 };
81
82 struct sout_stream_sys_t
83 {
84     sout_stream_id_t **pp_es;
85     int i_es_num;
86
87     vlc_mutex_t es_lock;
88     vlc_thread_t thread;
89
90     int i_fd;
91     int i_id_disable;
92 };
93
94 static const char *const ppsz_sout_options[] = {
95     "disable", "port", NULL
96 };
97
98 /*****************************************************************************
99  * Open:
100  *****************************************************************************/
101 static int Open(vlc_object_t *p_this)
102 {
103     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
104     sout_stream_sys_t *p_sys;
105
106     p_sys = malloc(sizeof(sout_stream_sys_t));
107     if (!p_sys)
108         return VLC_ENOMEM;
109
110     if (!p_stream->p_next)
111     {
112         msg_Err( p_stream, "cannot create chain" );
113         free( p_sys );
114         return VLC_EGENERIC;
115     }
116
117     config_ChainParse(p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
118                       p_stream->p_cfg);
119
120     int port = var_GetInteger(p_stream, SOUT_CFG_PREFIX "port");
121     p_sys->i_fd = net_ListenUDP1(VLC_OBJECT(p_stream), NULL, port);
122     if (p_sys->i_fd < 0)
123     {
124         free( p_sys );
125         return VLC_EGENERIC;
126     }
127     p_sys->i_id_disable = var_GetInteger(p_stream, SOUT_CFG_PREFIX "disable");
128
129     p_sys->pp_es = NULL;
130     p_sys->i_es_num = 0;
131
132     p_stream->pf_add    = Add;
133     p_stream->pf_del    = Del;
134     p_stream->pf_send   = Send;
135
136     p_stream->p_sys     = p_sys;
137
138     vlc_mutex_init(&p_sys->es_lock);
139
140     if(vlc_clone(&p_sys->thread, Command, p_stream, VLC_THREAD_PRIORITY_LOW))
141     {
142         vlc_mutex_destroy(&p_sys->es_lock);
143         free(p_sys);
144         return VLC_EGENERIC;
145     }
146
147     /* update p_sout->i_out_pace_nocontrol */
148     p_stream->p_sout->i_out_pace_nocontrol++;
149
150     return VLC_SUCCESS;
151 }
152
153 /*****************************************************************************
154  * Close:
155  *****************************************************************************/
156 static void Close (vlc_object_t * p_this)
157 {
158     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
159     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
160
161     /* Stop the thread */
162     vlc_cancel(p_sys->thread);
163     vlc_join(p_sys->thread, NULL);
164
165     /* Free the ressources */
166     net_Close( p_sys->i_fd );
167     vlc_mutex_destroy(&p_sys->es_lock);
168
169     p_stream->p_sout->i_out_pace_nocontrol--;
170
171     free(p_sys);
172 }
173
174 /****************************************************************************
175  * Command Thread:
176  ****************************************************************************/
177 static void* Command(void *p_this)
178 {
179     sout_stream_t *p_stream = (sout_stream_t *)p_this;
180     sout_stream_sys_t *p_sys = p_stream->p_sys;
181
182     while (vlc_object_alive(p_stream))
183     {
184         char psz_buffer[20];
185
186         int i_len = recv(p_sys->i_fd, psz_buffer, sizeof(psz_buffer)-1, 0);
187         if (i_len < 4)
188             continue;
189
190         psz_buffer[i_len] = '\0';
191         msg_Info( p_stream, "command: %s", psz_buffer );
192
193         if (strncmp(psz_buffer, "show", 4) == 0)
194         {
195             vlc_mutex_lock(&p_sys->es_lock);
196             mutex_cleanup_push(&p_sys->es_lock);
197             for (int i = 0; i < p_sys->i_es_num; i++)
198             {
199                 i_len = snprintf(psz_buffer, sizeof(psz_buffer), "%.4s : %d",
200                                  (char *)&p_sys->pp_es[i]->fmt.i_codec,
201                                  p_sys->pp_es[i]->fmt.i_id);
202                 psz_buffer[i_len] = '\0';
203                 msg_Info(p_stream, psz_buffer);
204             }
205             vlc_cleanup_pop();
206         }
207         else
208         {
209             bool b_apply = false;
210             bool b_select = false;
211             int i_pid = 0x1FFF;
212
213             if (strncmp(psz_buffer, "enable", 6) == 0)
214             {
215                 i_pid = atol(psz_buffer+7);
216                 b_select = true;
217                 b_apply = true;
218             }
219             else if (strncmp(psz_buffer, "disable", 7) == 0)
220             {
221                 i_pid = atol(psz_buffer+8);
222                 b_apply = true;
223             }
224
225             if (b_apply)
226             {
227                 vlc_mutex_lock(&p_sys->es_lock);
228                 mutex_cleanup_push(&p_sys->es_lock);
229                 for (int i = 0; i < p_sys->i_es_num; i++)
230                 {
231                     msg_Info(p_stream, "elementary stream pid %d",
232                              p_sys->pp_es[i]->fmt.i_id);
233                     if (p_sys->pp_es[i]->fmt.i_id == i_pid)
234                     {
235                         p_sys->pp_es[i]->b_enabled = b_select;
236                         msg_Info(p_stream, "%s: %d", b_select ? "enable" : "disable", i_pid);
237                     }
238                 }
239                 vlc_cleanup_pop();
240             }
241         }
242     }
243
244     return NULL;
245 }
246
247 static sout_stream_id_t *Add(sout_stream_t *p_stream, es_format_t *p_fmt)
248 {
249     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
250     sout_stream_id_t *p_es = malloc(sizeof(sout_stream_id_t));
251     if( !p_es )
252         return NULL;
253
254     p_es->fmt = *p_fmt;
255     p_es->id = NULL;
256     p_es->b_error = false;
257     p_es->b_enabled = (p_es->fmt.i_id == p_sys->i_id_disable) ? false : true;
258
259     vlc_mutex_lock(&p_sys->es_lock);
260     TAB_APPEND(p_sys->i_es_num, p_sys->pp_es, p_es);
261     vlc_mutex_unlock(&p_sys->es_lock);
262
263     return p_es;
264 }
265
266 static int Del(sout_stream_t *p_stream, sout_stream_id_t *p_es)
267 {
268     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
269     sout_stream_id_t *id = p_es->id;
270
271     vlc_mutex_lock(&p_sys->es_lock);
272     TAB_REMOVE(p_sys->i_es_num, p_sys->pp_es, p_es);
273     vlc_mutex_unlock(&p_sys->es_lock);
274
275     free(p_es);
276
277     if (id != NULL)
278         return p_stream->p_next->pf_del(p_stream->p_next, id);
279     else
280         return VLC_SUCCESS;
281 }
282
283 static int Send(sout_stream_t *p_stream, sout_stream_id_t *p_es,
284                 block_t *p_buffer)
285 {
286     if (p_es->id == NULL && !p_es->b_error)
287     {
288         p_es->id = p_stream->p_next->pf_add(p_stream->p_next, &p_es->fmt);
289         if (p_es->id == NULL)
290         {
291             p_es->b_error = true;
292             msg_Err(p_stream, "couldn't create chain for id %d",
293                     p_es->fmt.i_id);
294         }
295     }
296
297     if (!p_es->b_error && p_es->b_enabled)
298         p_stream->p_next->pf_send(p_stream->p_next, p_es->id, p_buffer);
299     else
300         block_ChainRelease(p_buffer);
301
302     return VLC_SUCCESS;
303 }