]> git.sesse.net Git - vlc/blob - modules/mux/mpjpeg.c
Improvements to preferences
[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 "\r\n--This Random String\r\n" \
33                   "Content-Type: image/jpeg\r\n\r\n"
34 /*****************************************************************************
35  * Module descriptor
36  *****************************************************************************/
37 static int  Open   ( vlc_object_t * );
38 static void Close  ( vlc_object_t * );
39
40 vlc_module_begin();
41     set_description( _("Multipart jpeg muxer") );
42     set_capability( "sout mux", 5 );
43     set_category( CAT_SOUT );
44     set_subcategory( SUBCAT_SOUT_MUX );
45     set_callbacks( Open, Close );
46     add_shortcut( "mpjpeg" );
47 vlc_module_end();
48
49 /*****************************************************************************
50  * Exported prototypes
51  *****************************************************************************/
52 static int Control  ( sout_mux_t *, int, va_list );
53 static int AddStream( sout_mux_t *, sout_input_t * );
54 static int DelStream( sout_mux_t *, sout_input_t * );
55 static int Mux      ( sout_mux_t * );
56
57 struct sout_mux_sys_t
58 {
59     block_t *p_separator;
60     vlc_bool_t b_send_headers;
61 };
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     sout_mux_sys_t  *p_sys;
70
71     msg_Dbg( p_mux, "Multipart jpeg muxer opened" );
72
73     p_mux->pf_control   = Control;
74     p_mux->pf_addstream = AddStream;
75     p_mux->pf_delstream = DelStream;
76     p_mux->pf_mux       = Mux;
77
78     p_sys = p_mux->p_sys = malloc( sizeof(sout_mux_sys_t) );
79     p_sys->b_send_headers = VLC_TRUE;
80     p_sys->p_separator = block_New( p_mux, sizeof(SEPARATOR) - 1 );
81     memcpy( p_sys->p_separator->p_buffer, SEPARATOR, sizeof(SEPARATOR) - 1 );
82
83     return VLC_SUCCESS;
84 }
85
86 /*****************************************************************************
87  * Close:
88  *****************************************************************************/
89
90 static void Close( vlc_object_t * p_this )
91 {
92     sout_mux_t *p_mux = (sout_mux_t*)p_this;
93     sout_mux_sys_t *p_sys = p_mux->p_sys;
94
95     msg_Dbg( p_mux, "Multipart jpeg muxer closed" );
96     block_Release( p_sys->p_separator );
97     free( p_sys );
98 }
99
100 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
101 {
102     vlc_bool_t *pb_bool;
103     char **ppsz;
104
105    switch( i_query )
106    {
107        case MUX_CAN_ADD_STREAM_WHILE_MUXING:
108            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
109            *pb_bool = VLC_TRUE;
110            return VLC_SUCCESS;
111
112        case MUX_GET_ADD_STREAM_WAIT:
113            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
114            *pb_bool = VLC_TRUE;
115            return VLC_SUCCESS;
116
117        case MUX_GET_MIME:
118            ppsz = (char**)va_arg( args, char ** );
119            *ppsz = strdup( "multipart/x-mixed-replace; boundary=This Random String" );
120            return VLC_SUCCESS;
121
122         default:
123             return VLC_EGENERIC;
124    }
125 }
126
127 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
128 {
129     if( p_mux->i_nb_inputs > 1 )
130     {
131         msg_Dbg( p_mux, "only 1 input allowed" );
132         return VLC_EGENERIC;
133     }
134
135     msg_Dbg( p_mux, "adding input" );
136     if( p_input->p_fmt->i_codec != VLC_FOURCC('M','J','P','G') )
137     {
138         return VLC_EGENERIC;
139     }
140
141     return VLC_SUCCESS;
142 }
143
144 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
145 {
146     msg_Dbg( p_mux, "removing input" );
147     return VLC_SUCCESS;
148 }
149
150 static int Mux( sout_mux_t *p_mux )
151 {
152     block_fifo_t *p_fifo;
153     sout_mux_sys_t *p_sys = p_mux->p_sys;
154     int i_count;
155
156     if( p_sys->b_send_headers )
157     {
158         block_t *p_header = block_New( p_mux, sizeof(SEPARATOR) - 3);
159         memcpy( p_header->p_buffer, &SEPARATOR[2], sizeof(SEPARATOR) - 3 );
160         p_header->i_flags |= BLOCK_FLAG_HEADER;
161         sout_AccessOutWrite( p_mux->p_access, p_header );
162         p_sys->b_send_headers = VLC_FALSE;
163     }
164
165     if( !p_mux->i_nb_inputs ) return VLC_SUCCESS;
166
167     p_fifo = p_mux->pp_inputs[0]->p_fifo;
168     i_count = p_fifo->i_depth;
169     while( i_count > 0 )
170     {
171         block_t *p_data = block_FifoGet( p_fifo );
172         sout_AccessOutWrite( p_mux->p_access,
173                              block_Duplicate( p_sys->p_separator ) );
174         sout_AccessOutWrite( p_mux->p_access, p_data );
175
176         i_count--;
177     }
178
179     return VLC_SUCCESS;
180 }