]> git.sesse.net Git - vlc/blob - modules/mux/mpjpeg.c
* modules/mux/mpjpeg.c: allow removing/adding a track on the fly.
[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 "\n--This Random String\n" \
33                   "Content-Type: image/jpeg\n\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_callbacks( Open, Close );
44     add_shortcut( "mpjpeg" );
45 vlc_module_end();
46
47 /*****************************************************************************
48  * Exported prototypes
49  *****************************************************************************/
50 static int  Capability(sout_mux_t *, int, void *, void * );
51 static int  AddStream( sout_mux_t *, sout_input_t * );
52 static int  DelStream( sout_mux_t *, sout_input_t * );
53 static int  Mux      ( sout_mux_t * );
54
55 struct sout_mux_sys_t
56 {
57     block_t *p_separator;
58     vlc_bool_t b_send_headers;
59 };
60
61 /*****************************************************************************
62  * Open:
63  *****************************************************************************/
64 static int Open( vlc_object_t *p_this )
65 {
66     sout_mux_t *p_mux = (sout_mux_t*)p_this;
67     sout_mux_sys_t  *p_sys;
68
69     msg_Dbg( p_mux, "Multipart jpeg muxer opened" );
70
71     p_mux->pf_capacity  = Capability;
72     p_mux->pf_addstream = AddStream;
73     p_mux->pf_delstream = DelStream;
74     p_mux->pf_mux       = Mux;
75
76     p_sys = p_mux->p_sys = malloc( sizeof(sout_mux_sys_t) );
77     p_sys->b_send_headers = VLC_TRUE;
78     p_sys->p_separator = block_New( p_mux, sizeof(SEPARATOR) - 1 );
79     memcpy( p_sys->p_separator->p_buffer, SEPARATOR, sizeof(SEPARATOR) - 1 );
80
81     return VLC_SUCCESS;
82 }
83
84 /*****************************************************************************
85  * Close:
86  *****************************************************************************/
87
88 static void Close( vlc_object_t * p_this )
89 {
90     sout_mux_t *p_mux = (sout_mux_t*)p_this;
91     sout_mux_sys_t *p_sys = p_mux->p_sys;
92
93     msg_Dbg( p_mux, "Multipart jpeg muxer closed" );
94     block_Release( p_sys->p_separator );
95     free( p_sys );
96 }
97
98 static int Capability( sout_mux_t *p_mux, int i_query,
99                        void *p_args, void *p_answer )
100 {
101     switch( i_query )
102     {
103         case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
104             *(vlc_bool_t*)p_answer = VLC_TRUE;
105             return SOUT_MUX_CAP_ERR_OK;
106         default:
107             return SOUT_MUX_CAP_ERR_UNIMPLEMENTED;
108     }
109 }
110
111 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
112 {
113     if( p_mux->i_nb_inputs > 1 )
114     {
115         msg_Dbg( p_mux, "only 1 input allowed" );
116         return VLC_EGENERIC;
117     }
118
119     msg_Dbg( p_mux, "adding input" );
120     if( p_input->p_fmt->i_codec != VLC_FOURCC('M','J','P','G') )
121     {
122         return VLC_EGENERIC;
123     }
124
125     return VLC_SUCCESS;
126 }
127
128 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
129 {
130     msg_Dbg( p_mux, "removing input" );
131     return VLC_SUCCESS;
132 }
133
134 static int Mux( sout_mux_t *p_mux )
135 {
136     block_fifo_t *p_fifo;
137     sout_mux_sys_t *p_sys = p_mux->p_sys;
138     int i_count;
139
140     if( p_sys->b_send_headers )
141     {
142         block_t *p_header = block_New( p_mux, sizeof(SEPARATOR) - 2);
143         memcpy( p_header->p_buffer, &SEPARATOR[1], sizeof(SEPARATOR) - 2 );
144         p_header->i_flags |= BLOCK_FLAG_HEADER;
145         sout_AccessOutWrite( p_mux->p_access, p_header );
146         p_sys->b_send_headers = VLC_FALSE;
147     }
148
149     if( !p_mux->i_nb_inputs ) return VLC_SUCCESS;
150
151     p_fifo = p_mux->pp_inputs[0]->p_fifo;
152     i_count = p_fifo->i_depth;
153     while( i_count > 0 )
154     {
155         block_t *p_data = block_FifoGet( p_fifo );
156         sout_AccessOutWrite( p_mux->p_access,
157                              block_Duplicate( p_sys->p_separator ) );
158         sout_AccessOutWrite( p_mux->p_access, p_data );
159
160         i_count--;
161     }
162
163     return VLC_SUCCESS;
164 }