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