]> git.sesse.net Git - vlc/blob - modules/mux/mpjpeg.c
mux: remove return value from sout_mux_t.pf_delstream
[vlc] / modules / mux / mpjpeg.c
1 /*****************************************************************************
2  * mpjpeg.c: mime multipart jpeg  muxer module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001, 2002, 2006 VLC authors and VideoLAN
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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_sout.h>
35 #include <vlc_block.h>
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 static int  Open   ( vlc_object_t * );
41 static void Close  ( vlc_object_t * );
42
43 #define SOUT_CFG_PREFIX "sout-mpjpeg-"
44
45 vlc_module_begin ()
46     set_shortname( "MPJPEG" )
47     set_description( N_("Multipart JPEG muxer") )
48     set_capability( "sout mux", 5 )
49     set_category( CAT_SOUT )
50     set_subcategory( SUBCAT_SOUT_MUX )
51     set_callbacks( Open, Close )
52     add_shortcut( "mpjpeg" )
53 vlc_module_end ()
54
55 /*****************************************************************************
56  * Exported prototypes
57  *****************************************************************************/
58 static int Control  ( sout_mux_t *, int, va_list );
59 static int AddStream( sout_mux_t *, sout_input_t * );
60 static void DelStream( sout_mux_t *, sout_input_t * );
61 static int Mux      ( sout_mux_t * );
62
63 /* This pseudo-random sequence is unlikely to ever happen */
64 /* This should be the same as in src/network/httpd.c */
65 #define BOUNDARY "7b3cc56e5f51db803f790dad720ed50a"
66
67 /*****************************************************************************
68  * Open:
69  *****************************************************************************/
70 static int Open( vlc_object_t *p_this )
71 {
72     sout_mux_t *p_mux = (sout_mux_t*)p_this;
73
74     msg_Dbg( p_mux, "Multipart jpeg muxer opened" );
75
76     p_mux->pf_control   = Control;
77     p_mux->pf_addstream = AddStream;
78     p_mux->pf_delstream = DelStream;
79     p_mux->pf_mux       = Mux;
80     p_mux->p_sys        = NULL;
81
82     return VLC_SUCCESS;
83 }
84
85 /*****************************************************************************
86  * Close:
87  *****************************************************************************/
88
89 static void Close( vlc_object_t * p_this )
90 {
91     /* TODO: send the ending boundary ("\r\n--"BOUNDARY"--\r\n"),
92      * but is the access_output still useable?? */
93     msg_Dbg( p_this, "Multipart jpeg muxer closed" );
94 }
95
96 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
97 {
98     VLC_UNUSED(p_mux);
99     bool *pb_bool;
100     char **ppsz;
101
102     switch( i_query )
103     {
104         case MUX_CAN_ADD_STREAM_WHILE_MUXING:
105             pb_bool = (bool*)va_arg( args, bool * );
106             *pb_bool = true;
107             return VLC_SUCCESS;
108
109         case MUX_GET_ADD_STREAM_WAIT:
110             pb_bool = (bool*)va_arg( args, bool * );
111             *pb_bool = true;
112             return VLC_SUCCESS;
113
114         case MUX_GET_MIME:
115             ppsz = (char**)va_arg( args, char ** );
116             *ppsz = strdup( "multipart/x-mixed-replace; boundary="BOUNDARY );
117             return VLC_SUCCESS;
118
119         default:
120             return VLC_EGENERIC;
121     }
122 }
123
124 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
125 {
126     if( p_mux->i_nb_inputs > 1 )
127     {
128         msg_Dbg( p_mux, "only 1 input allowed" );
129         return VLC_EGENERIC;
130     }
131
132     msg_Dbg( p_mux, "adding input" );
133     if( p_input->p_fmt->i_codec != VLC_CODEC_MJPG )
134         return VLC_EGENERIC;
135
136     return VLC_SUCCESS;
137 }
138
139 static void DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
140 {
141     VLC_UNUSED(p_input);
142     msg_Dbg( p_mux, "removing input" );
143 }
144
145 static int Mux( sout_mux_t *p_mux )
146 {
147     block_fifo_t *p_fifo;
148
149     if( !p_mux->i_nb_inputs ) return VLC_SUCCESS;
150
151     p_fifo = p_mux->pp_inputs[0]->p_fifo;
152
153     while( block_FifoCount( p_fifo ) > 0 )
154     {
155         static const char psz_hfmt[] = "\r\n"
156             "--"BOUNDARY"\r\n"
157             "Content-Type: image/jpeg\r\n"
158             "Content-Length: %zu\r\n"
159             "\r\n";
160         block_t *p_data = block_FifoGet( p_fifo );
161         block_t *p_header = block_Alloc( sizeof( psz_hfmt ) + 20 );
162
163         if( p_header == NULL ) /* uho! */
164         {
165             block_Release( p_data );
166             continue;
167         }
168
169         p_header->i_buffer =
170             snprintf( (char *)p_header->p_buffer, p_header->i_buffer,
171                       psz_hfmt, p_data->i_buffer );
172         p_header->i_flags |= BLOCK_FLAG_HEADER;
173         sout_AccessOutWrite( p_mux->p_access, p_header );
174         sout_AccessOutWrite( p_mux->p_access, p_data );
175     }
176
177     return VLC_SUCCESS;
178 }