]> git.sesse.net Git - vlc/blob - modules/stream_out/setid.c
New stream_out module setid to change IDs of streams
[vlc] / modules / stream_out / setid.c
1 /*****************************************************************************
2  * setid.c: set ID on a stream
3  *****************************************************************************
4  * Copyright (C) 2009 VideoLAN and AUTHORS
5  *
6  * Authors: Christophe Massiot <massiot@via.ecp.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_sout.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 #define ID_TEXT N_("ID")
42 #define ID_LONGTEXT N_( \
43     "Specify an identifier integer for this elementary stream" )
44
45 #define NEWID_TEXT N_("New ID")
46 #define NEWID_LONGTEXT N_( \
47     "Specify an new identifier integer for this elementary stream" )
48
49 static int  Open    ( vlc_object_t * );
50 static void Close   ( vlc_object_t * );
51
52 #define SOUT_CFG_PREFIX "sout-setid-"
53
54 vlc_module_begin()
55     set_shortname( _("setid"))
56     set_description( _("Automatically add/delete input streams"))
57     set_capability( "sout stream", 50 )
58     add_shortcut( "setid" )
59     set_callbacks( Open, Close )
60     add_integer( SOUT_CFG_PREFIX "id", 0, ID_TEXT, ID_LONGTEXT,
61                  false )
62     add_integer( SOUT_CFG_PREFIX "new-id", 0, NEWID_TEXT, NEWID_LONGTEXT,
63                  false )
64 vlc_module_end()
65
66
67 /*****************************************************************************
68  * Local prototypes
69  *****************************************************************************/
70 static const char *ppsz_sout_options[] = {
71     "id", "new-id", NULL
72 };
73
74 static sout_stream_id_t *Add   ( sout_stream_t *, es_format_t * );
75 static int               Del   ( sout_stream_t *, sout_stream_id_t * );
76 static int               Send  ( sout_stream_t *, sout_stream_id_t *, block_t * );
77
78 struct sout_stream_sys_t
79 {
80     sout_stream_t   *p_out;
81     int i_id, i_new_id;
82 };
83
84 /*****************************************************************************
85  * Open:
86  *****************************************************************************/
87 static int Open( vlc_object_t *p_this )
88 {
89     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
90     sout_stream_sys_t *p_sys;
91     vlc_value_t       val;
92
93     p_sys          = malloc( sizeof( sout_stream_sys_t ) );
94
95     if( !p_stream->p_next )
96     {
97         msg_Err( p_stream, "cannot create chain" );
98         free( p_sys );
99         return VLC_EGENERIC;
100     }
101
102     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
103                    p_stream->p_cfg );
104
105     var_Get( p_stream, SOUT_CFG_PREFIX "id", &val );
106     p_sys->i_id = val.i_int;
107     var_Get( p_stream, SOUT_CFG_PREFIX "new-id", &val );
108     p_sys->i_new_id = val.i_int;
109
110     p_stream->pf_add    = Add;
111     p_stream->pf_del    = Del;
112     p_stream->pf_send   = Send;
113
114     p_stream->p_sys     = p_sys;
115
116     return VLC_SUCCESS;
117 }
118
119 /*****************************************************************************
120  * Close:
121  *****************************************************************************/
122 static void Close( vlc_object_t * p_this )
123 {
124     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
125     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
126
127     free( p_sys );
128 }
129
130 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
131 {
132     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
133
134     if ( p_fmt->i_id == p_sys->i_id )
135     {
136         msg_Dbg( p_stream, "turning ID %d to %d",
137                  p_sys->i_id, p_sys->i_new_id );
138         p_fmt->i_id = p_sys->i_new_id;
139     }
140
141
142     return p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
143 }
144
145 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
146 {
147     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
148
149     return p_sys->p_out->pf_del( p_sys->p_out, id );
150 }
151
152 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
153                  block_t *p_buffer )
154 {
155     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
156
157     return p_sys->p_out->pf_send( p_sys->p_out, id, p_buffer );
158 }