]> git.sesse.net Git - vlc/blob - modules/mux/mpjpeg.c
- Heavily simplify the MPJPEG mux (closes #1188); please test.
[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 /*****************************************************************************
34  * Module descriptor
35  *****************************************************************************/
36 static int  Open   ( vlc_object_t * );
37 static void Close  ( vlc_object_t * );
38
39 #define SOUT_CFG_PREFIX "sout-mpjpeg-"
40
41 vlc_module_begin();
42     set_shortname( "MPJPEG" );
43     set_description( _("Multipart JPEG muxer") );
44     set_capability( "sout mux", 5 );
45     add_obsolete_string( SOUT_CFG_PREFIX "separator" );
46     set_category( CAT_SOUT );
47     set_subcategory( SUBCAT_SOUT_MUX );
48     set_callbacks( Open, Close );
49     add_shortcut( "mpjpeg" );
50 vlc_module_end();
51
52 /*****************************************************************************
53  * Exported prototypes
54  *****************************************************************************/
55 static int Control  ( sout_mux_t *, int, va_list );
56 static int AddStream( sout_mux_t *, sout_input_t * );
57 static int DelStream( sout_mux_t *, sout_input_t * );
58 static int Mux      ( sout_mux_t * );
59
60 /* This pseudo-random sequence is unlikely to ever happen */
61 #define BOUNDARY "7b3cc56e5f51db803f790dad720ed50a"
62
63 /*****************************************************************************
64  * Open:
65  *****************************************************************************/
66 static int Open( vlc_object_t *p_this )
67 {
68     sout_mux_t *p_mux = (sout_mux_t*)p_this;
69
70     msg_Dbg( p_mux, "Multipart jpeg muxer opened" );
71
72     p_mux->pf_control   = Control;
73     p_mux->pf_addstream = AddStream;
74     p_mux->pf_delstream = DelStream;
75     p_mux->pf_mux       = Mux;
76     p_mux->p_sys        = NULL;
77
78     return VLC_SUCCESS;
79 }
80
81 /*****************************************************************************
82  * Close:
83  *****************************************************************************/
84
85 static void Close( vlc_object_t * p_this )
86 {
87     /* TODO: send the ending boundary ("\r\n--"BOUNDARY"--\r\n"),
88      * but is the access_output still useable?? */
89     msg_Dbg( p_this, "Multipart jpeg muxer closed" );
90 }
91
92 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
93 {
94     vlc_bool_t *pb_bool;
95     char **ppsz;
96
97    switch( i_query )
98    {
99        case MUX_CAN_ADD_STREAM_WHILE_MUXING:
100            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
101            *pb_bool = VLC_TRUE;
102            return VLC_SUCCESS;
103
104        case MUX_GET_ADD_STREAM_WAIT:
105            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
106            *pb_bool = VLC_TRUE;
107            return VLC_SUCCESS;
108
109        case MUX_GET_MIME:
110            ppsz = (char**)va_arg( args, char ** );
111            *ppsz = strdup( "multipart/x-mixed-replace; boundary="BOUNDARY );
112            return VLC_SUCCESS;
113
114         default:
115             return VLC_EGENERIC;
116    }
117 }
118
119 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
120 {
121     if( p_mux->i_nb_inputs > 1 )
122     {
123         msg_Dbg( p_mux, "only 1 input allowed" );
124         return VLC_EGENERIC;
125     }
126
127     msg_Dbg( p_mux, "adding input" );
128     if( p_input->p_fmt->i_codec != VLC_FOURCC('M','J','P','G') &&
129         p_input->p_fmt->i_codec != VLC_FOURCC('m','j','p','g') &&
130         p_input->p_fmt->i_codec != VLC_FOURCC('j','p','e','g') &&
131         p_input->p_fmt->i_codec != VLC_FOURCC('J','P','E','G') &&
132         p_input->p_fmt->i_codec != VLC_FOURCC('J','F','I','F') &&
133         p_input->p_fmt->i_codec != VLC_FOURCC('J','P','G','L') &&
134         p_input->p_fmt->i_codec != VLC_FOURCC('m','j','p','a') )
135     {
136         return VLC_EGENERIC;
137     }
138
139     return VLC_SUCCESS;
140 }
141
142 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
143 {
144     msg_Dbg( p_mux, "removing input" );
145     return VLC_SUCCESS;
146 }
147
148 static int Mux( sout_mux_t *p_mux )
149 {
150     block_fifo_t *p_fifo;
151
152     if( !p_mux->i_nb_inputs ) return VLC_SUCCESS;
153
154     p_fifo = p_mux->pp_inputs[0]->p_fifo;
155
156     while( block_FifoCount( p_fifo ) > 0 )
157     {
158         static const char psz_hfmt[] = "\r\n"
159             "--"BOUNDARY"\r\n"
160             "Content-Type: image/jpeg\r\n"
161             "Content-Length: %u\r\n"
162             "\r\n";
163         block_t *p_data = block_FifoGet( p_fifo );
164         block_t *p_header = block_New( p_mux, sizeof( psz_hfmt ) + 20 );
165
166         if( p_header == NULL ) /* uho! */
167         {
168             block_Release( p_data );
169             continue;
170         }
171
172         p_header->i_buffer =
173             snprintf( (char *)p_header->p_buffer, p_header->i_buffer,
174                       psz_hfmt, p_data->i_buffer );
175         p_header->i_flags |= BLOCK_FLAG_HEADER;
176         sout_AccessOutWrite( p_mux->p_access, p_header );
177         sout_AccessOutWrite( p_mux->p_access, p_data );
178     }
179
180     return VLC_SUCCESS;
181 }