]> git.sesse.net Git - vlc/blob - modules/demux/mjpeg.c
RTP: handle odd port numbers correctly
[vlc] / modules / demux / mjpeg.c
1 /*****************************************************************************
2  * mjpeg.c : demuxes mjpeg webcam http streams
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Henry Jen (slowhog) <henryjen@ztune.net>
8  *          Derk-Jan Hartman (thedj)
9  *          Sigmund Augdal (Dnumgis)
10  *          Laurent Aimar <fenrir@via.ecp.fr>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_demux.h>
38
39 #include <vlc_codecs.h>
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 static int  Open ( vlc_object_t * );
45 static void Close( vlc_object_t * );
46
47 #define FPS_TEXT N_("Frames per Second")
48 #define FPS_LONGTEXT N_("This is the desired frame rate when " \
49     "playing MJPEG from a file. Use 0 (this is the default value) for a " \
50     "live stream (from a camera).")
51
52 vlc_module_begin();
53     set_shortname( "MJPEG");
54     set_description( N_("M-JPEG camera demuxer") );
55     set_capability( "demux", 5 );
56     set_callbacks( Open, Close );
57     set_category( CAT_INPUT );
58     set_subcategory( SUBCAT_INPUT_DEMUX );
59     add_float( "mjpeg-fps", 0.0, NULL, FPS_TEXT, FPS_LONGTEXT, false );
60 vlc_module_end();
61
62 /*****************************************************************************
63  * Local prototypes
64  *****************************************************************************/
65 static int MimeDemux( demux_t * );
66 static int MjpgDemux( demux_t * );
67 static int Control( demux_t *, int i_query, va_list args );
68
69 struct demux_sys_t
70 {
71     es_format_t     fmt;
72     es_out_id_t     *p_es;
73
74     bool      b_still;
75     mtime_t         i_still_end;
76     mtime_t         i_still_length;
77
78     mtime_t         i_time;
79     mtime_t         i_frame_length;
80     char            *psz_separator;
81     int             i_frame_size_estimate;
82     const uint8_t   *p_peek;
83     int             i_data_peeked;
84 };
85
86 /*****************************************************************************
87  * Peek: Helper function to peek data with incremental size.
88  * \return false if peek no more data, true otherwise.
89  *****************************************************************************/
90 static bool Peek( demux_t *p_demux, bool b_first )
91 {
92     int i_data;
93     demux_sys_t *p_sys = p_demux->p_sys;
94
95     if( b_first )
96     {
97         p_sys->i_data_peeked = 0;
98     }
99     else if( p_sys->i_data_peeked == p_sys->i_frame_size_estimate )
100     {
101         p_sys->i_frame_size_estimate += 5120;
102     }
103     i_data = stream_Peek( p_demux->s, &p_sys->p_peek,
104                           p_sys->i_frame_size_estimate );
105     if( i_data == p_sys->i_data_peeked )
106     {
107         msg_Warn( p_demux, "no more data" );
108         return false;
109     }
110     p_sys->i_data_peeked = i_data;
111     if( i_data <= 0 )
112     {
113         msg_Warn( p_demux, "cannot peek data" );
114         return false;
115     }
116     return true;
117 }
118
119 /*****************************************************************************
120  * GetLine: Internal function used to dup a line of string from the buffer
121  *****************************************************************************/
122 static char* GetLine( demux_t *p_demux, int *p_pos )
123 {
124     demux_sys_t *p_sys = p_demux->p_sys;
125     const uint8_t *p_buf;
126     int         i_size;
127     int         i;
128     char        *p_line;
129
130     while( *p_pos > p_sys->i_data_peeked )
131     {
132         if( ! Peek( p_demux, false ) )
133         {
134             return NULL;
135         }
136     }
137     p_buf = p_sys->p_peek + *p_pos;
138     i_size = p_sys->i_data_peeked - *p_pos;
139     i = 0;
140     while( p_buf[i] != '\n' )
141     {
142         i++;
143         if( i == i_size )
144         {
145             if( ! Peek( p_demux, false ) )
146             {
147                 return NULL;
148             }
149         }
150         p_buf = p_sys->p_peek + *p_pos;
151         i_size = p_sys->i_data_peeked - *p_pos;
152     }
153     *p_pos += ( i + 1 );
154     if( i > 0 && '\r' == p_buf[i - 1] )
155     {
156         i--;
157     }
158     p_line = malloc( i + 1 );
159     if( NULL == p_line )
160     {
161         msg_Err( p_demux, "out of memory" );
162         return NULL;
163     }
164     strncpy ( p_line, (char*)p_buf, i );
165     p_line[i] = '\0';
166 //    msg_Dbg( p_demux, "i = %d, pos = %d, %s", i, *p_pos, p_line );
167     return p_line;
168 }
169
170 /*****************************************************************************
171  * CheckMimeHeader: Internal function used to verify and skip mime header
172  * \param p_header_size Return size of MIME header, 0 if no MIME header
173  * detected, minus value if error
174  * \return true if content type is image/jpeg, false otherwise
175  *****************************************************************************/
176 static bool CheckMimeHeader( demux_t *p_demux, int *p_header_size )
177 {
178     bool  b_jpeg = false;
179     int         i_pos;
180     char        *psz_line;
181     char        *p_ch;
182     demux_sys_t *p_sys = p_demux->p_sys;
183
184     if( !Peek( p_demux, true ) )
185     {
186         msg_Err( p_demux, "cannot peek" );
187         *p_header_size = -1;
188         return false;
189     }
190     if( p_sys->i_data_peeked < 3)
191     {
192         msg_Err( p_demux, "data shortage" );
193         *p_header_size = -2;
194         return false;
195     }
196     if( strncmp( (char *)p_sys->p_peek, "--", 2 ) )
197     {
198         *p_header_size = 0;
199         return false;
200     }
201     i_pos = 2;
202     psz_line = GetLine( p_demux, &i_pos );
203     if( NULL == psz_line )
204     {
205         msg_Err( p_demux, "no EOL" );
206         *p_header_size = -3;
207         return false;
208     }
209
210     /* Read the separator and remember it if not yet stored */
211     if( p_sys->psz_separator == NULL )
212     {
213         p_sys->psz_separator = psz_line;
214         msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
215                  p_sys->psz_separator );
216     }
217     else
218     {
219         if( strcmp( psz_line, p_sys->psz_separator ) )
220         {
221             msg_Warn( p_demux, "separator %s does not match %s", psz_line,
222                       p_sys->psz_separator );
223         }
224         free( psz_line );
225     }
226
227     psz_line = GetLine( p_demux, &i_pos );
228     while( psz_line && *psz_line )
229     {
230         if( !strncasecmp( psz_line, "Content-Type:", 13 ) )
231         {
232             p_ch = psz_line + 13;
233             while( *p_ch != '\0' && ( *p_ch == ' ' || *p_ch == '\t' ) ) p_ch++;
234             if( strncasecmp( p_ch, "image/jpeg", 10 ) )
235             {
236                 msg_Warn( p_demux, "%s, image/jpeg is expected", psz_line );
237                 b_jpeg = false;
238             }
239             else
240             {
241                 b_jpeg = true;
242             }
243         }
244         else
245         {
246             msg_Dbg( p_demux, "discard MIME header: %s", psz_line );
247         }
248         free( psz_line );
249         psz_line = GetLine( p_demux, &i_pos );
250     }
251
252     if( NULL == psz_line )
253     {
254         msg_Err( p_demux, "no EOL" );
255         *p_header_size = -3;
256         return false;
257     }
258
259     free( psz_line );
260
261     *p_header_size = i_pos;
262     return b_jpeg;
263 }
264
265 static int SendBlock( demux_t *p_demux, int i )
266 {
267     demux_sys_t *p_sys = p_demux->p_sys;
268     block_t     *p_block;
269
270     if( ( p_block = stream_Block( p_demux->s, i ) ) == NULL )
271     {
272         msg_Warn( p_demux, "cannot read data" );
273         return 0;
274     }
275
276     if( !p_sys->i_frame_length || !p_sys->i_time )
277     {
278         p_sys->i_time = p_block->i_dts = p_block->i_pts = mdate();
279     }
280     else
281     {
282         p_block->i_dts = p_block->i_pts = p_sys->i_time;
283         p_sys->i_time += p_sys->i_frame_length;
284     }
285
286     /* set PCR */
287     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
288     es_out_Send( p_demux->out, p_sys->p_es, p_block );
289
290     if( p_sys->b_still )
291     {
292         p_sys->i_still_end = mdate() + p_sys->i_still_length;
293     }
294
295     return 1;
296 }
297
298 /*****************************************************************************
299  * Open: check file and initializes structures
300  *****************************************************************************/
301 static int Open( vlc_object_t * p_this )
302 {
303     demux_t     *p_demux = (demux_t*)p_this;
304     demux_sys_t *p_sys;
305     int         i_size;
306     int         b_matched = false;
307     vlc_value_t val;
308
309     p_demux->pf_control = Control;
310     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
311     p_sys->p_es         = NULL;
312     p_sys->i_time       = 0;
313
314     p_sys->psz_separator = NULL;
315     p_sys->i_frame_size_estimate = 15 * 1024;
316
317     b_matched = CheckMimeHeader( p_demux, &i_size);
318     if( b_matched )
319     {
320         p_demux->pf_demux = MimeDemux;
321         stream_Read( p_demux->s, NULL, i_size );
322     }
323     else if( 0 == i_size )
324     {
325         /* 0xffd8 identify a JPEG SOI */
326         if( p_sys->p_peek[0] == 0xFF && p_sys->p_peek[1] == 0xD8 )
327         {
328             msg_Dbg( p_demux, "JPEG SOI marker detected" );
329             p_demux->pf_demux = MjpgDemux;
330         }
331         else
332         {
333             goto error;
334         }
335     }
336     else
337     {
338         goto error;
339     }
340
341
342     var_Create( p_demux, "mjpeg-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
343     var_Get( p_demux, "mjpeg-fps", &val );
344     p_sys->i_frame_length = 0;
345
346     /* Check for jpeg file extension */
347     p_sys->b_still = false;
348     p_sys->i_still_end = 0;
349     if( demux_IsPathExtension( p_demux, ".jpeg" ) ||
350         demux_IsPathExtension( p_demux, ".jpg" ) )
351     {
352         p_sys->b_still = true;
353         if( val.f_float)
354         {
355             p_sys->i_still_length = 1000000.0 / val.f_float;
356         }
357         else
358         {
359             /* Defaults to 1fps */
360             p_sys->i_still_length = 1000000;
361         }
362     }
363     else if ( val.f_float )
364     {
365         p_sys->i_frame_length = 1000000.0 / val.f_float;
366     }
367
368     es_format_Init( &p_sys->fmt, VIDEO_ES, 0 );
369     p_sys->fmt.i_codec = VLC_FOURCC('m','j','p','g');
370
371     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
372     return VLC_SUCCESS;
373
374 error:
375     free( p_sys );
376     return VLC_EGENERIC;
377 }
378
379 /*****************************************************************************
380  * Demux: read packet and send them to decoders
381  *****************************************************************************
382  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
383  *****************************************************************************/
384 static int MjpgDemux( demux_t *p_demux )
385 {
386     demux_sys_t *p_sys = p_demux->p_sys;
387     int i;
388
389     if( p_sys->b_still && p_sys->i_still_end && p_sys->i_still_end < mdate() )
390     {
391         /* Still frame, wait until the pause delay is gone */
392         p_sys->i_still_end = 0;
393     }
394     else if( p_sys->b_still && p_sys->i_still_end )
395     {
396         msleep( 400 );
397         return 1;
398     }
399
400     if( !Peek( p_demux, true ) )
401     {
402         msg_Warn( p_demux, "cannot peek data" );
403         return 0;
404     }
405     if( p_sys->i_data_peeked < 4 )
406     {
407         msg_Warn( p_demux, "data shortage" );
408         return 0;
409     }
410     i = 3;
411     while( !( 0xFF == p_sys->p_peek[i-1] && 0xD9 == p_sys->p_peek[i] ) )
412     {
413         i++;
414         if( i >= p_sys->i_data_peeked )
415         {
416             msg_Dbg( p_demux, "did not find JPEG EOI in %d bytes",
417                      p_sys->i_data_peeked );
418             if( !Peek( p_demux, false ) )
419             {
420                 msg_Warn( p_demux, "no more data is available at the moment" );
421                 return 0;
422             }
423         }
424     }
425     i++;
426
427     msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
428     return SendBlock( p_demux, i );
429 }
430
431 static int MimeDemux( demux_t *p_demux )
432 {
433     demux_sys_t *p_sys = p_demux->p_sys;
434     int         i_size, i;
435
436     bool  b_match = CheckMimeHeader( p_demux, &i_size );
437
438     if( i_size > 0 )
439     {
440         stream_Read( p_demux->s, NULL, i_size );
441     }
442     else if( i_size < 0 )
443     {
444         return 0;
445     }
446     else
447     {
448         // No MIME header, assume OK
449         b_match = true;
450     }
451
452     if( !Peek( p_demux, true ) )
453     {
454         msg_Warn( p_demux, "cannot peek data" );
455         return 0;
456     }
457
458     i = 0;
459     i_size = strlen( p_sys->psz_separator ) + 2;
460     if( p_sys->i_data_peeked < i_size )
461     {
462         msg_Warn( p_demux, "data shortage" );
463         return 0;
464     }
465
466     for( ;; )
467     {
468         while( !( p_sys->p_peek[i] == '-' && p_sys->p_peek[i+1] == '-' ) )
469         {
470             i++;
471             i_size++;
472             if( i_size >= p_sys->i_data_peeked )
473             {
474                 msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
475                          "data", p_sys->i_data_peeked );
476
477                 if( !Peek( p_demux, false ) )
478                 {
479                     msg_Warn( p_demux, "no more data is available at the "
480                               "moment" );
481                     return 0;
482                 }
483             }
484         }
485
486         if( !strncmp( p_sys->psz_separator, (char *)(p_sys->p_peek + i + 2),
487                       strlen( p_sys->psz_separator ) ) )
488         {
489             break;
490         }
491
492         i++;
493         i_size++;
494     }
495
496     if( !b_match )
497     {
498         msg_Err( p_demux, "discard non-JPEG part" );
499         stream_Read( p_demux->s, NULL, i );
500         return 0;
501     }
502
503     return SendBlock( p_demux, i );
504 }
505
506 /*****************************************************************************
507  * Close: frees unused data
508  *****************************************************************************/
509 static void Close ( vlc_object_t * p_this )
510 {
511     demux_t     *p_demux = (demux_t*)p_this;
512     demux_sys_t *p_sys  = p_demux->p_sys;
513
514     free( p_sys->psz_separator );
515     free( p_sys );
516 }
517
518 /*****************************************************************************
519  * Control:
520  *****************************************************************************/
521 static int Control( demux_t *p_demux, int i_query, va_list args )
522 {
523     return demux_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );
524 }