]> git.sesse.net Git - vlc/blob - modules/mux/mpjpeg.c
* Allow all MJPEG fourcc
[vlc] / modules / mux / mpjpeg.c
1 /*****************************************************************************
2  * mpjpeg.c: mime multipart jpeg  muxer module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: dummy.c 7047 2004-03-11 17:37:50Z fenrir $
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/sout.h>
31
32 #define SEPARATOR_TEXT N_( "Multipart separator string" )
33 #define SEPARATOR_LONGTEXT N_( "Multipart strings like MPJPEG use a " \
34                                "separator string betwen content pieces. "\
35                                "You can select this string. Default is "\
36                                "--myboundary" )
37
38
39 #define CONTENT_TYPE "Content-Type: image/jpeg"
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 static int  Open   ( vlc_object_t * );
44 static void Close  ( vlc_object_t * );
45
46 #define SOUT_CFG_PREFIX "sout-mpjpeg-"
47
48 vlc_module_begin();
49     set_description( _("Multipart jpeg muxer") );
50     set_capability( "sout mux", 5 );
51     add_string( SOUT_CFG_PREFIX "separator", "--myboundary", NULL,
52                               SEPARATOR_TEXT, SEPARATOR_LONGTEXT, VLC_TRUE );
53     set_category( CAT_SOUT );
54     set_subcategory( SUBCAT_SOUT_MUX );
55     set_callbacks( Open, Close );
56     add_shortcut( "mpjpeg" );
57 vlc_module_end();
58
59 /*****************************************************************************
60  * Exported prototypes
61  *****************************************************************************/
62 static const char *ppsz_sout_options[] = { "separator", NULL };
63
64 static int Control  ( sout_mux_t *, int, va_list );
65 static int AddStream( sout_mux_t *, sout_input_t * );
66 static int DelStream( sout_mux_t *, sout_input_t * );
67 static int Mux      ( sout_mux_t * );
68
69 struct sout_mux_sys_t
70 {
71     block_t *p_separator;
72     vlc_bool_t b_send_headers;
73 };
74
75 /*****************************************************************************
76  * Open:
77  *****************************************************************************/
78 static int Open( vlc_object_t *p_this )
79 {
80     int i_size;
81     sout_mux_t *p_mux = (sout_mux_t*)p_this;
82     sout_mux_sys_t  *p_sys;
83     char *psz_separator_block, *psz_separator;
84
85     msg_Dbg( p_mux, "Multipart jpeg muxer opened" );
86
87     p_sys = p_mux->p_sys = malloc( sizeof(sout_mux_sys_t) );
88     p_sys->b_send_headers = VLC_TRUE;
89
90     psz_separator = var_CreateGetString( p_mux, SOUT_CFG_PREFIX "separator" );
91     i_size = strlen( psz_separator ) + 2 + 2 + 2 + strlen( CONTENT_TYPE );
92     psz_separator_block = (char*)malloc( i_size );
93     sprintf( psz_separator_block, "\r\n%s\r\n%s\r\n", psz_separator,
94                                   CONTENT_TYPE );
95     p_sys->p_separator = block_New( p_mux, i_size );
96     memcpy( p_sys->p_separator->p_buffer, psz_separator_block , i_size );
97
98     if( psz_separator_block ) free( psz_separator_block );
99
100     p_mux->pf_control   = Control;
101     p_mux->pf_addstream = AddStream;
102     p_mux->pf_delstream = DelStream;
103     p_mux->pf_mux       = Mux;
104
105     return VLC_SUCCESS;
106 }
107
108 /*****************************************************************************
109  * Close:
110  *****************************************************************************/
111
112 static void Close( vlc_object_t * p_this )
113 {
114     sout_mux_t *p_mux = (sout_mux_t*)p_this;
115     sout_mux_sys_t *p_sys = p_mux->p_sys;
116
117     msg_Dbg( p_mux, "Multipart jpeg muxer closed" );
118     block_Release( p_sys->p_separator );
119     free( p_sys );
120 }
121
122 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
123 {
124     vlc_bool_t *pb_bool;
125     char **ppsz;
126
127    switch( i_query )
128    {
129        case MUX_CAN_ADD_STREAM_WHILE_MUXING:
130            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
131            *pb_bool = VLC_TRUE;
132            return VLC_SUCCESS;
133
134        case MUX_GET_ADD_STREAM_WAIT:
135            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
136            *pb_bool = VLC_TRUE;
137            return VLC_SUCCESS;
138
139        case MUX_GET_MIME:
140            ppsz = (char**)va_arg( args, char ** );
141            *ppsz = strdup( "multipart/x-mixed-replace; boundary=This Random String" );
142            return VLC_SUCCESS;
143
144         default:
145             return VLC_EGENERIC;
146    }
147 }
148
149 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
150 {
151     if( p_mux->i_nb_inputs > 1 )
152     {
153         msg_Dbg( p_mux, "only 1 input allowed" );
154         return VLC_EGENERIC;
155     }
156
157     msg_Dbg( p_mux, "adding input" );
158     if( p_input->p_fmt->i_codec != VLC_FOURCC('M','J','P','G') &&
159         p_input->p_fmt->i_codec != VLC_FOURCC('m','j','p','g') &&
160         p_input->p_fmt->i_codec != VLC_FOURCC('j','p','e','g') &&
161         p_input->p_fmt->i_codec != VLC_FOURCC('J','P','E','G') &&
162         p_input->p_fmt->i_codec != VLC_FOURCC('J','F','I','F') &&
163         p_input->p_fmt->i_codec != VLC_FOURCC('J','P','G','L') &&
164         p_input->p_fmt->i_codec != VLC_FOURCC('m','j','p','a') )
165     {
166         return VLC_EGENERIC;
167     }
168
169     return VLC_SUCCESS;
170 }
171
172 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
173 {
174     msg_Dbg( p_mux, "removing input" );
175     return VLC_SUCCESS;
176 }
177
178 static int Mux( sout_mux_t *p_mux )
179 {
180     block_fifo_t *p_fifo;
181     sout_mux_sys_t *p_sys = p_mux->p_sys;
182     int i_count;
183     /* Content-Length:.......\r\n */
184     char psz_content_length[25];
185
186     if( p_sys->b_send_headers )
187     {
188         block_t *p_header;
189         char *psz_separator = var_CreateGetString( p_mux,
190                                              SOUT_CFG_PREFIX "separator" );
191         char *psz_separator_block = (char *)malloc( strlen( psz_separator ) +
192                                               2 + strlen( CONTENT_TYPE ) );
193
194         sprintf( psz_separator_block, "%s\r\n%s\r\n", psz_separator,
195                                       CONTENT_TYPE );
196
197         p_header = block_New( p_mux, strlen( psz_separator_block ) );
198         memcpy( p_header->p_buffer, psz_separator_block ,
199                                     strlen( psz_separator_block ) );
200         p_header->i_flags |= BLOCK_FLAG_HEADER;
201         sout_AccessOutWrite( p_mux->p_access, p_header );
202         p_sys->b_send_headers = VLC_FALSE;
203         if( psz_separator_block ) free( psz_separator_block );
204     }
205
206     if( !p_mux->i_nb_inputs ) return VLC_SUCCESS;
207
208     p_fifo = p_mux->pp_inputs[0]->p_fifo;
209     i_count = p_fifo->i_depth;
210     while( i_count > 0 )
211     {
212         block_t *p_length = block_New( p_mux, 25 );
213         block_t *p_data = block_FifoGet( p_fifo );
214         sout_AccessOutWrite( p_mux->p_access,
215                              block_Duplicate( p_sys->p_separator ) );
216         memset( psz_content_length, 0, 25 );
217         snprintf( psz_content_length, 25, "Content-Length: %i\r\n\r\n",
218                                           p_data->i_buffer );
219         memcpy( p_length->p_buffer, psz_content_length, 25 );
220         sout_AccessOutWrite( p_mux->p_access, p_length );
221         sout_AccessOutWrite( p_mux->p_access, p_data );
222
223         i_count--;
224     }
225
226     return VLC_SUCCESS;
227 }