]> git.sesse.net Git - vlc/blob - modules/mux/mpjpeg.c
Remove stdlib.h
[vlc] / modules / mux / mpjpeg.c
1 /*****************************************************************************
2  * mpjpeg.c: mime multipart jpeg  muxer module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001, 2002, 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #include <vlc/vlc.h>
29 #include <vlc_sout.h>
30 #include <vlc_block.h>
31 #include <vlc_codecs.h>
32
33 #define SEPARATOR_TEXT N_( "Multipart separator string" )
34 #define SEPARATOR_LONGTEXT N_( "Multipart strings like MPJPEG use a " \
35                                "specific string to separate its content " \
36                                "pieces. You can select this string. " \
37                                "Default is --myboundary" )
38
39
40 #define CONTENT_TYPE "Content-Type: image/jpeg"
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 static int  Open   ( vlc_object_t * );
45 static void Close  ( vlc_object_t * );
46
47 #define SOUT_CFG_PREFIX "sout-mpjpeg-"
48
49 vlc_module_begin();
50     set_shortname( "MPJPEG" );
51     set_description( _("Multipart JPEG muxer") );
52     set_capability( "sout mux", 5 );
53     add_string( SOUT_CFG_PREFIX "separator", "--myboundary", NULL,
54                               SEPARATOR_TEXT, SEPARATOR_LONGTEXT, VLC_TRUE );
55     set_category( CAT_SOUT );
56     set_subcategory( SUBCAT_SOUT_MUX );
57     set_callbacks( Open, Close );
58     add_shortcut( "mpjpeg" );
59 vlc_module_end();
60
61 /*****************************************************************************
62  * Exported prototypes
63  *****************************************************************************/
64 static const char *ppsz_sout_options[] = { "separator", NULL };
65
66 static int Control  ( sout_mux_t *, int, va_list );
67 static int AddStream( sout_mux_t *, sout_input_t * );
68 static int DelStream( sout_mux_t *, sout_input_t * );
69 static int Mux      ( sout_mux_t * );
70
71 struct sout_mux_sys_t
72 {
73     block_t *p_separator;
74     vlc_bool_t b_send_headers;
75 };
76
77 /*****************************************************************************
78  * Open:
79  *****************************************************************************/
80 static int Open( vlc_object_t *p_this )
81 {
82     int i_size;
83     sout_mux_t *p_mux = (sout_mux_t*)p_this;
84     sout_mux_sys_t  *p_sys;
85     char *psz_separator_block, *psz_separator;
86
87     msg_Dbg( p_mux, "Multipart jpeg muxer opened" );
88     config_ChainParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
89
90     p_sys = p_mux->p_sys = malloc( sizeof(sout_mux_sys_t) );
91     p_sys->b_send_headers = VLC_TRUE;
92
93     psz_separator = var_GetString( p_mux, SOUT_CFG_PREFIX "separator" );
94     i_size = strlen( psz_separator ) + 2 + 2 + 2 + strlen( CONTENT_TYPE );
95     psz_separator_block = (char*)malloc( i_size );
96     sprintf( psz_separator_block, "\r\n%s\r\n%s\r\n", psz_separator,
97                                   CONTENT_TYPE );
98     p_sys->p_separator = block_New( p_mux, i_size );
99     memcpy( p_sys->p_separator->p_buffer, psz_separator_block , i_size );
100
101     if( psz_separator_block ) free( psz_separator_block );
102
103     p_mux->pf_control   = Control;
104     p_mux->pf_addstream = AddStream;
105     p_mux->pf_delstream = DelStream;
106     p_mux->pf_mux       = Mux;
107
108     return VLC_SUCCESS;
109 }
110
111 /*****************************************************************************
112  * Close:
113  *****************************************************************************/
114
115 static void Close( vlc_object_t * p_this )
116 {
117     sout_mux_t *p_mux = (sout_mux_t*)p_this;
118     sout_mux_sys_t *p_sys = p_mux->p_sys;
119
120     msg_Dbg( p_mux, "Multipart jpeg muxer closed" );
121     block_Release( p_sys->p_separator );
122     free( p_sys );
123 }
124
125 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
126 {
127     vlc_bool_t *pb_bool;
128     char **ppsz;
129
130    switch( i_query )
131    {
132        case MUX_CAN_ADD_STREAM_WHILE_MUXING:
133            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
134            *pb_bool = VLC_TRUE;
135            return VLC_SUCCESS;
136
137        case MUX_GET_ADD_STREAM_WAIT:
138            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
139            *pb_bool = VLC_TRUE;
140            return VLC_SUCCESS;
141
142        case MUX_GET_MIME:
143            ppsz = (char**)va_arg( args, char ** );
144            *ppsz = strdup( "multipart/x-mixed-replace; boundary=This Random String" );
145            return VLC_SUCCESS;
146
147         default:
148             return VLC_EGENERIC;
149    }
150 }
151
152 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
153 {
154     if( p_mux->i_nb_inputs > 1 )
155     {
156         msg_Dbg( p_mux, "only 1 input allowed" );
157         return VLC_EGENERIC;
158     }
159
160     msg_Dbg( p_mux, "adding input" );
161     if( p_input->p_fmt->i_codec != VLC_FOURCC('M','J','P','G') &&
162         p_input->p_fmt->i_codec != VLC_FOURCC('m','j','p','g') &&
163         p_input->p_fmt->i_codec != VLC_FOURCC('j','p','e','g') &&
164         p_input->p_fmt->i_codec != VLC_FOURCC('J','P','E','G') &&
165         p_input->p_fmt->i_codec != VLC_FOURCC('J','F','I','F') &&
166         p_input->p_fmt->i_codec != VLC_FOURCC('J','P','G','L') &&
167         p_input->p_fmt->i_codec != VLC_FOURCC('m','j','p','a') )
168     {
169         return VLC_EGENERIC;
170     }
171
172     return VLC_SUCCESS;
173 }
174
175 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
176 {
177     msg_Dbg( p_mux, "removing input" );
178     return VLC_SUCCESS;
179 }
180
181 static int Mux( sout_mux_t *p_mux )
182 {
183     block_fifo_t *p_fifo;
184     sout_mux_sys_t *p_sys = p_mux->p_sys;
185     int i_count;
186     /* Content-Length:.......\r\n */
187     char psz_content_length[25];
188
189     if( p_sys->b_send_headers )
190     {
191         block_t *p_header;
192         char *psz_separator = var_CreateGetString( p_mux,
193                                              SOUT_CFG_PREFIX "separator" );
194         char *psz_separator_block = (char *)malloc( strlen( psz_separator ) +
195                                               2 + strlen( CONTENT_TYPE ) );
196
197         sprintf( psz_separator_block, "%s\r\n%s\r\n", psz_separator,
198                                       CONTENT_TYPE );
199
200         p_header = block_New( p_mux, strlen( psz_separator_block ) );
201         memcpy( p_header->p_buffer, psz_separator_block ,
202                                     strlen( psz_separator_block ) );
203         p_header->i_flags |= BLOCK_FLAG_HEADER;
204         sout_AccessOutWrite( p_mux->p_access, p_header );
205         p_sys->b_send_headers = VLC_FALSE;
206         if( psz_separator_block ) free( psz_separator_block );
207     }
208
209     if( !p_mux->i_nb_inputs ) return VLC_SUCCESS;
210
211     p_fifo = p_mux->pp_inputs[0]->p_fifo;
212     i_count = p_fifo->i_depth;
213     while( i_count > 0 )
214     {
215         block_t *p_length = block_New( p_mux, 25 );
216         block_t *p_data = block_FifoGet( p_fifo );
217         sout_AccessOutWrite( p_mux->p_access,
218                              block_Duplicate( p_sys->p_separator ) );
219         memset( psz_content_length, 0, 25 );
220         snprintf( psz_content_length, 25, "Content-Length: %i\r\n\r\n",
221                                           p_data->i_buffer );
222         memcpy( p_length->p_buffer, psz_content_length, 25 );
223         sout_AccessOutWrite( p_mux->p_access, p_length );
224         sout_AccessOutWrite( p_mux->p_access, p_data );
225
226         i_count--;
227     }
228
229     return VLC_SUCCESS;
230 }