1 /*****************************************************************************
2 * select.c: Select individual es to enable or disable from stream
3 *****************************************************************************
4 * Copyright (C) 2006-2011 the VideoLAN team
7 * Authors: Jean-Paul Saman <jpsaman@videolan.org>
8 * Based upon autodel.c written by: Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_block.h>
37 #include <vlc_network.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Open ( vlc_object_t * );
43 static void Close ( vlc_object_t * );
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>)." )
49 #define DISABLE_TEXT N_("Disable ES id")
50 #define DISABLE_LONGTEXT N_( \
51 "Disable ES id at startup." )
53 #define SOUT_CFG_PREFIX "sout-select-"
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)
65 /*****************************************************************************
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 *);
72 static void* Command(vlc_object_t *);
74 struct sout_stream_id_t
82 struct sout_stream_sys_t
84 sout_stream_id_t **pp_es;
92 static const char *const ppsz_sout_options[] = {
93 "disable", "port", NULL
96 /*****************************************************************************
98 *****************************************************************************/
99 static int Open(vlc_object_t *p_this)
101 sout_stream_t *p_stream = (sout_stream_t*)p_this;
102 sout_stream_sys_t *p_sys;
104 p_sys = malloc(sizeof(sout_stream_sys_t));
108 if (!p_stream->p_next)
110 msg_Err( p_stream, "cannot create chain" );
115 config_ChainParse(p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
118 int port = var_CreateGetInteger(p_stream, SOUT_CFG_PREFIX "port");
119 p_sys->i_fd = net_ListenUDP1(VLC_OBJECT(p_stream), NULL, port);
125 p_sys->i_id_disable = var_CreateGetInteger(p_stream, SOUT_CFG_PREFIX "disable");
130 p_stream->pf_add = Add;
131 p_stream->pf_del = Del;
132 p_stream->pf_send = Send;
134 p_stream->p_sys = p_sys;
136 vlc_mutex_init(&p_sys->es_lock);
138 if (vlc_thread_create(p_stream, Command, VLC_THREAD_PRIORITY_LOW))
140 vlc_mutex_destroy(&p_sys->es_lock);
145 /* update p_sout->i_out_pace_nocontrol */
146 p_stream->p_sout->i_out_pace_nocontrol++;
151 /*****************************************************************************
153 *****************************************************************************/
154 static void Close (vlc_object_t * p_this)
156 sout_stream_t *p_stream = (sout_stream_t*)p_this;
157 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
159 net_Close( p_sys->i_fd );
162 vlc_thread_join(p_stream);
164 vlc_mutex_destroy(&p_sys->es_lock);
166 p_stream->p_sout->i_out_pace_nocontrol--;
171 /****************************************************************************
173 ****************************************************************************/
174 static void* Command(vlc_object_t *p_this)
176 sout_stream_t *p_stream = (sout_stream_t *)p_this;
177 sout_stream_sys_t *p_sys = p_stream->p_sys;
179 int canc = vlc_savecancel();
181 while (vlc_object_alive(p_stream))
188 int i_len = recv(p_sys->i_fd, psz_buffer, sizeof(psz_buffer)-1, 0);
192 psz_buffer[i_len] = '\0';
193 msg_Info( p_stream, "command: %s", psz_buffer );
195 if (strncmp(psz_buffer, "show", 4) == 0)
197 vlc_mutex_lock(&p_sys->es_lock);
198 for (int i = 0; i < p_sys->i_es_num; i++)
200 i_len = snprintf(psz_buffer, sizeof(psz_buffer), "%.4s : %d",
201 (char *)&p_sys->pp_es[i]->fmt.i_codec,
202 p_sys->pp_es[i]->fmt.i_id);
203 psz_buffer[i_len] = '\0';
204 msg_Info(p_stream, psz_buffer);
206 vlc_mutex_unlock(&p_sys->es_lock);
210 bool b_apply = false;
211 bool b_select = false;
214 if (strncmp(psz_buffer, "enable", 6) == 0)
216 i_pid = atol(psz_buffer+7);
220 else if (strncmp(psz_buffer, "disable", 7) == 0)
222 i_pid = atol(psz_buffer+8);
228 vlc_mutex_lock(&p_sys->es_lock);
229 for (int i = 0; i < p_sys->i_es_num; i++)
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)
235 p_sys->pp_es[i]->b_enabled = b_select;
236 msg_Info(p_stream, "%s: %d", b_select ? "enable" : "disable", i_pid);
239 vlc_mutex_unlock(&p_sys->es_lock);
244 vlc_restorecancel(canc);
248 static sout_stream_id_t *Add(sout_stream_t *p_stream, es_format_t *p_fmt)
250 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
251 sout_stream_id_t *p_es = malloc(sizeof(sout_stream_id_t));
257 p_es->b_error = false;
258 p_es->b_enabled = (p_es->fmt.i_id == p_sys->i_id_disable) ? false : true;
260 vlc_mutex_lock(&p_sys->es_lock);
261 TAB_APPEND(p_sys->i_es_num, p_sys->pp_es, p_es);
262 vlc_mutex_unlock(&p_sys->es_lock);
267 static int Del(sout_stream_t *p_stream, sout_stream_id_t *p_es)
269 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
270 sout_stream_id_t *id = p_es->id;
272 vlc_mutex_lock(&p_sys->es_lock);
273 TAB_REMOVE(p_sys->i_es_num, p_sys->pp_es, p_es);
274 vlc_mutex_unlock(&p_sys->es_lock);
279 return p_stream->p_next->pf_del(p_stream->p_next, id);
284 static int Send(sout_stream_t *p_stream, sout_stream_id_t *p_es,
287 if (p_es->id == NULL && p_es->b_error != true)
289 p_es->id = p_stream->p_next->pf_add(p_stream->p_next, &p_es->fmt);
290 if (p_es->id == NULL)
292 p_es->b_error = true;
293 msg_Err(p_stream, "couldn't create chain for id %d",
298 if ((p_es->b_error != true) && p_es->b_enabled)
299 p_stream->p_next->pf_send(p_stream->p_next, p_es->id, p_buffer);
301 block_ChainRelease(p_buffer);