1 /*****************************************************************************
2 * mpjpeg.c: mime multipart jpeg muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2006 VLC authors and VideoLAN
6 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
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 Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
34 #include <vlc_block.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t
* );
40 static void Close ( vlc_object_t
* );
42 #define SOUT_CFG_PREFIX "sout-mpjpeg-"
45 set_shortname( "MPJPEG" )
46 set_description( N_("Multipart JPEG muxer") )
47 set_capability( "sout mux", 5 )
48 set_category( CAT_SOUT
)
49 set_subcategory( SUBCAT_SOUT_MUX
)
50 set_callbacks( Open
, Close
)
51 add_shortcut( "mpjpeg" )
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Control ( sout_mux_t
*, int, va_list );
58 static int AddStream( sout_mux_t
*, sout_input_t
* );
59 static void DelStream( sout_mux_t
*, sout_input_t
* );
60 static int Mux ( sout_mux_t
* );
62 /* This pseudo-random sequence is unlikely to ever happen */
63 /* This should be the same as in src/network/httpd.c */
64 #define BOUNDARY "7b3cc56e5f51db803f790dad720ed50a"
66 /*****************************************************************************
68 *****************************************************************************/
69 static int Open( vlc_object_t
*p_this
)
71 sout_mux_t
*p_mux
= (sout_mux_t
*)p_this
;
73 msg_Dbg( p_mux
, "Multipart jpeg muxer opened" );
75 p_mux
->pf_control
= Control
;
76 p_mux
->pf_addstream
= AddStream
;
77 p_mux
->pf_delstream
= DelStream
;
84 /*****************************************************************************
86 *****************************************************************************/
88 static void Close( vlc_object_t
* p_this
)
90 /* TODO: send the ending boundary ("\r\n--"BOUNDARY"--\r\n"),
91 * but is the access_output still useable?? */
92 msg_Dbg( p_this
, "Multipart jpeg muxer closed" );
95 static int Control( sout_mux_t
*p_mux
, int i_query
, va_list args
)
103 case MUX_CAN_ADD_STREAM_WHILE_MUXING
:
104 pb_bool
= va_arg( args
, bool * );
109 ppsz
= va_arg( args
, char ** );
110 *ppsz
= strdup( "multipart/x-mixed-replace; boundary="BOUNDARY
);
118 static int AddStream( sout_mux_t
*p_mux
, sout_input_t
*p_input
)
120 if( p_mux
->i_nb_inputs
> 1 )
122 msg_Dbg( p_mux
, "only 1 input allowed" );
126 msg_Dbg( p_mux
, "adding input" );
127 if( p_input
->p_fmt
->i_codec
!= VLC_CODEC_MJPG
)
133 static void DelStream( sout_mux_t
*p_mux
, sout_input_t
*p_input
)
136 msg_Dbg( p_mux
, "removing input" );
139 static int Mux( sout_mux_t
*p_mux
)
141 block_fifo_t
*p_fifo
;
143 if( !p_mux
->i_nb_inputs
) return VLC_SUCCESS
;
145 p_fifo
= p_mux
->pp_inputs
[0]->p_fifo
;
147 while( block_FifoCount( p_fifo
) > 0 )
149 static const char psz_hfmt
[] = "\r\n"
151 "Content-Type: image/jpeg\r\n"
152 "Content-Length: %zu\r\n"
154 block_t
*p_data
= block_FifoGet( p_fifo
);
155 block_t
*p_header
= block_Alloc( sizeof( psz_hfmt
) + 20 );
157 if( p_header
== NULL
) /* uho! */
159 block_Release( p_data
);
164 snprintf( (char *)p_header
->p_buffer
, p_header
->i_buffer
,
165 psz_hfmt
, p_data
->i_buffer
);
166 p_header
->i_flags
|= BLOCK_FLAG_HEADER
;
167 sout_AccessOutWrite( p_mux
->p_access
, p_header
);
168 sout_AccessOutWrite( p_mux
->p_access
, p_data
);