]> git.sesse.net Git - vlc/blob - modules/stream_out/vod.c
Don't leak a HICON
[vlc] / modules / stream_out / vod.c
1 /*****************************************************************************
2  * vod.c: rtsp VoD server module
3  *****************************************************************************
4  * Copyright (C) 2003-2006, 2010 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *          Pierre Ynard
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_input.h>
37 #include <vlc_sout.h>
38 #include <vlc_block.h>
39
40 #include <vlc_vod.h>
41 #include <vlc_url.h>
42 #include <vlc_network.h>
43
44 #include <assert.h>
45
46 #include "rtp.h"
47
48 /*****************************************************************************
49  * Exported prototypes
50  *****************************************************************************/
51
52 typedef struct media_es_t media_es_t;
53
54 struct media_es_t
55 {
56     int es_id;
57     rtp_format_t rtp_fmt;
58     rtsp_stream_id_t *rtsp_id;
59 };
60
61 struct vod_media_t
62 {
63     /* VoD server */
64     vod_t *p_vod;
65
66     /* RTSP server */
67     rtsp_stream_t *rtsp;
68
69     /* ES list */
70     int        i_es;
71     media_es_t **es;
72     const char *psz_mux;
73
74     /* Infos */
75     mtime_t i_length;
76 };
77
78 struct vod_sys_t
79 {
80     char *psz_rtsp_url;
81
82     /* */
83     vlc_thread_t thread;
84     block_fifo_t *p_fifo_cmd;
85 };
86
87 /* rtsp delayed command (to avoid deadlock between vlm/httpd) */
88 typedef enum
89 {
90     RTSP_CMD_TYPE_STOP,
91     RTSP_CMD_TYPE_ADD,
92     RTSP_CMD_TYPE_DEL,
93 } rtsp_cmd_type_t;
94
95 /* */
96 typedef struct
97 {
98     int i_type;
99     vod_media_t *p_media;
100     char *psz_arg;
101 } rtsp_cmd_t;
102
103 static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
104 static void         MediaDel( vod_t *, vod_media_t * );
105 static void         MediaAskDel ( vod_t *, vod_media_t * );
106
107 static void* CommandThread( void *obj );
108 static void  CommandPush( vod_t *, rtsp_cmd_type_t, vod_media_t *,
109                           const char *psz_arg );
110
111 /*****************************************************************************
112  * Open: Starts the RTSP server module
113  *****************************************************************************/
114 int OpenVoD( vlc_object_t *p_this )
115 {
116     vod_t *p_vod = (vod_t *)p_this;
117     vod_sys_t *p_sys = NULL;
118     char *psz_url;
119
120     p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
121     if( !p_sys ) goto error;
122
123     psz_url = var_InheritString( p_vod, "rtsp-host" );
124
125     if( psz_url == NULL )
126         p_sys->psz_rtsp_url = strdup( "/" );
127     else
128     if( !( strlen( psz_url ) > 0 && psz_url[strlen( psz_url ) - 1] == '/' ) )
129     {
130          if( asprintf( &p_sys->psz_rtsp_url, "%s/", psz_url ) == -1 )
131          {
132              p_sys->psz_rtsp_url = NULL;
133              free( psz_url );
134              goto error;
135          }
136          free( psz_url );
137     }
138     else
139         p_sys->psz_rtsp_url = psz_url;
140
141     p_vod->pf_media_new = MediaNew;
142     p_vod->pf_media_del = MediaAskDel;
143
144     p_sys->p_fifo_cmd = block_FifoNew();
145     if( vlc_clone( &p_sys->thread, CommandThread, p_vod, VLC_THREAD_PRIORITY_LOW ) )
146     {
147         msg_Err( p_vod, "cannot spawn rtsp vod thread" );
148         block_FifoRelease( p_sys->p_fifo_cmd );
149         goto error;
150     }
151
152     return VLC_SUCCESS;
153
154 error:
155     if( p_sys )
156     {
157         free( p_sys->psz_rtsp_url );
158         free( p_sys );
159     }
160
161     return VLC_EGENERIC;
162 }
163
164 /*****************************************************************************
165  * Close:
166  *****************************************************************************/
167 void CloseVoD( vlc_object_t * p_this )
168 {
169     vod_t *p_vod = (vod_t *)p_this;
170     vod_sys_t *p_sys = p_vod->p_sys;
171
172     /* Stop command thread */
173     vlc_cancel( p_sys->thread );
174     vlc_join( p_sys->thread, NULL );
175
176     while( block_FifoCount( p_sys->p_fifo_cmd ) > 0 )
177     {
178         rtsp_cmd_t cmd;
179         block_t *p_block_cmd = block_FifoGet( p_sys->p_fifo_cmd );
180         memcpy( &cmd, p_block_cmd->p_buffer, sizeof(cmd) );
181         block_Release( p_block_cmd );
182         if ( cmd.i_type == RTSP_CMD_TYPE_DEL )
183             MediaDel(p_vod, cmd.p_media);
184         free( cmd.psz_arg );
185     }
186     block_FifoRelease( p_sys->p_fifo_cmd );
187
188     free( p_sys->psz_rtsp_url );
189     free( p_sys );
190 }
191
192 /*****************************************************************************
193  * Media handling
194  *****************************************************************************/
195 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
196                               input_item_t *p_item )
197 {
198     vod_media_t *p_media = calloc( 1, sizeof(vod_media_t) );
199     if( !p_media )
200         return NULL;
201
202     p_media->p_vod = p_vod;
203     p_media->rtsp = NULL;
204     TAB_INIT( p_media->i_es, p_media->es );
205     p_media->psz_mux = NULL;
206     p_media->i_length = input_item_GetDuration( p_item );
207
208     vlc_mutex_lock( &p_item->lock );
209     msg_Dbg( p_vod, "media '%s' has %i declared ES", psz_name, p_item->i_es );
210     for( int i = 0; i < p_item->i_es; i++ )
211     {
212         es_format_t *p_fmt = p_item->es[i];
213
214         switch( p_fmt->i_codec )
215         {
216             case VLC_FOURCC( 'm', 'p', '2', 't' ):
217                 p_media->psz_mux = "ts";
218                 break;
219             case VLC_FOURCC( 'm', 'p', '2', 'p' ):
220                 p_media->psz_mux = "ps";
221                 break;
222         }
223         assert(p_media->psz_mux == NULL || p_item->i_es == 1);
224
225         media_es_t *p_es = calloc( 1, sizeof(media_es_t) );
226         if( !p_es )
227             continue;
228
229         p_es->es_id = p_fmt->i_id;
230         p_es->rtsp_id = NULL;
231
232         if (rtp_get_fmt(VLC_OBJECT(p_vod), p_fmt, p_media->psz_mux,
233                         &p_es->rtp_fmt) != VLC_SUCCESS)
234         {
235             free(p_es);
236             continue;
237         }
238
239         TAB_APPEND( p_media->i_es, p_media->es, p_es );
240         msg_Dbg(p_vod, "  - added ES %u %s (%4.4s)",
241                 p_es->rtp_fmt.payload_type, p_es->rtp_fmt.ptname,
242                 (char *)&p_fmt->i_codec);
243     }
244     vlc_mutex_unlock( &p_item->lock );
245
246     if (p_media->i_es == 0)
247     {
248         msg_Err(p_vod, "no ES was added to the media, aborting");
249         goto error;
250     }
251
252     msg_Dbg(p_vod, "adding media '%s'", psz_name);
253
254     CommandPush( p_vod, RTSP_CMD_TYPE_ADD, p_media, psz_name );
255     return p_media;
256
257 error:
258     MediaDel(p_vod, p_media);
259     return NULL;
260 }
261
262 static void MediaSetup( vod_t *p_vod, vod_media_t *p_media,
263                         const char *psz_name )
264 {
265     vod_sys_t *p_sys = p_vod->p_sys;
266     char *psz_url;
267
268     if( asprintf( &psz_url, "%s%s", p_sys->psz_rtsp_url, psz_name ) < 0 )
269         return;
270
271     vlc_url_t url;
272     vlc_UrlParse( &url, psz_url, 0 );
273     free( psz_url );
274
275     p_media->rtsp = RtspSetup(VLC_OBJECT(p_vod), p_media, &url);
276
277     vlc_UrlClean( &url );
278
279     if (p_media->rtsp == NULL)
280         return;
281
282     for (int i = 0; i < p_media->i_es; i++)
283     {
284         media_es_t *p_es = p_media->es[i];
285         p_es->rtsp_id = RtspAddId(p_media->rtsp, NULL, 0,
286                                   p_es->rtp_fmt.clock_rate, -1);
287     }
288 }
289
290 static void MediaAskDel ( vod_t *p_vod, vod_media_t *p_media )
291 {
292     msg_Dbg( p_vod, "deleting media" );
293     CommandPush( p_vod, RTSP_CMD_TYPE_DEL, p_media, NULL );
294 }
295
296 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
297 {
298     (void) p_vod;
299
300     if (p_media->rtsp != NULL)
301     {
302         for (int i = 0; i < p_media->i_es; i++)
303         {
304             media_es_t *p_es = p_media->es[i];
305             if (p_es->rtsp_id != NULL)
306                 RtspDelId(p_media->rtsp, p_es->rtsp_id);
307         }
308         RtspUnsetup(p_media->rtsp);
309     }
310
311     while( p_media->i_es )
312     {
313         media_es_t *p_es = p_media->es[0];
314         TAB_REMOVE( p_media->i_es, p_media->es, p_es );
315         free( p_es->rtp_fmt.fmtp );
316         free( p_es );
317     }
318
319     TAB_CLEAN( p_media->i_es, p_media->es );
320     free( p_media );
321 }
322
323 static void CommandPush( vod_t *p_vod, rtsp_cmd_type_t i_type,
324                          vod_media_t *p_media, const char *psz_arg )
325 {
326     rtsp_cmd_t cmd;
327     block_t *p_cmd;
328
329     cmd.i_type = i_type;
330     cmd.p_media = p_media;
331     if( psz_arg )
332         cmd.psz_arg = strdup(psz_arg);
333     else
334         cmd.psz_arg = NULL;
335
336     p_cmd = block_New( p_vod, sizeof(rtsp_cmd_t) );
337     memcpy( p_cmd->p_buffer, &cmd, sizeof(cmd) );
338
339     block_FifoPut( p_vod->p_sys->p_fifo_cmd, p_cmd );
340 }
341
342 static void* CommandThread( void *obj )
343 {
344     vod_t *p_vod = (vod_t*)obj;
345     vod_sys_t *p_sys = p_vod->p_sys;
346
347     for( ;; )
348     {
349         block_t *p_block_cmd = block_FifoGet( p_sys->p_fifo_cmd );
350         rtsp_cmd_t cmd;
351
352         if( !p_block_cmd )
353             break;
354
355         int canc = vlc_savecancel ();
356         memcpy( &cmd, p_block_cmd->p_buffer, sizeof(cmd) );
357         block_Release( p_block_cmd );
358
359         /* */
360         switch( cmd.i_type )
361         {
362         case RTSP_CMD_TYPE_ADD:
363             MediaSetup(p_vod, cmd.p_media, cmd.psz_arg);
364             break;
365         case RTSP_CMD_TYPE_DEL:
366             MediaDel(p_vod, cmd.p_media);
367             break;
368         case RTSP_CMD_TYPE_STOP:
369             vod_MediaControl( p_vod, cmd.p_media, cmd.psz_arg, VOD_MEDIA_STOP );
370             break;
371
372         default:
373             break;
374         }
375
376         free( cmd.psz_arg );
377         vlc_restorecancel (canc);
378     }
379
380     return NULL;
381 }
382
383 /*****************************************************************************
384  * SDPGenerateVoD
385  * FIXME: needs to be merged more?
386  *****************************************************************************/
387 char *SDPGenerateVoD( const vod_media_t *p_media, const char *rtsp_url )
388 {
389     char *psz_sdp;
390
391     assert(rtsp_url != NULL);
392     /* Check against URL format rtsp://[<ipv6>]:<port>/<path> */
393     bool ipv6 = strlen( rtsp_url ) > 7 && rtsp_url[7] == '[';
394
395     /* Dummy destination address for RTSP */
396     struct sockaddr_storage dst;
397     socklen_t dstlen = ipv6 ? sizeof( struct sockaddr_in6 )
398                             : sizeof( struct sockaddr_in );
399     memset (&dst, 0, dstlen);
400     dst.ss_family = ipv6 ? AF_INET6 : AF_INET;
401 #ifdef HAVE_SA_LEN
402     dst.ss_len = dstlen;
403 #endif
404
405     psz_sdp = vlc_sdp_Start( VLC_OBJECT( p_media->p_vod ), "sout-rtp-",
406                              NULL, 0, (struct sockaddr *)&dst, dstlen );
407     if( psz_sdp == NULL )
408         return NULL;
409
410     if( p_media->i_length > 0 )
411     {
412         lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
413         sdp_AddAttribute( &psz_sdp, "range"," npt=0-%lld.%03u", d.quot,
414                           (unsigned)d.rem );
415     }
416
417     sdp_AddAttribute ( &psz_sdp, "control", "%s", rtsp_url );
418
419     /* No locking needed, the ES table can't be modified now */
420     for( int i = 0; i < p_media->i_es; i++ )
421     {
422         media_es_t *p_es = p_media->es[i];
423         rtp_format_t *rtp_fmt = &p_es->rtp_fmt;
424         const char *mime_major; /* major MIME type */
425
426         switch( rtp_fmt->cat )
427         {
428             case VIDEO_ES:
429                 mime_major = "video";
430                 break;
431             case AUDIO_ES:
432                 mime_major = "audio";
433                 break;
434             case SPU_ES:
435                 mime_major = "text";
436                 break;
437             default:
438                 continue;
439         }
440
441         sdp_AddMedia( &psz_sdp, mime_major, "RTP/AVP", 0,
442                       rtp_fmt->payload_type, false, 0,
443                       rtp_fmt->ptname, rtp_fmt->clock_rate, rtp_fmt->channels,
444                       rtp_fmt->fmtp );
445
446         char *track_url = RtspAppendTrackPath( p_es->rtsp_id, rtsp_url );
447         if( track_url != NULL )
448         {
449             sdp_AddAttribute ( &psz_sdp, "control", "%s", track_url );
450             free( track_url );
451         }
452     }
453
454     return psz_sdp;
455 }
456
457 int vod_check_range(vod_media_t *p_media, const char *psz_session,
458                     int64_t start, int64_t end)
459 {
460     (void) psz_session;
461
462     if (p_media->i_length > 0 && (start > p_media->i_length
463                                   || end > p_media->i_length))
464         return VLC_EGENERIC;
465
466     return VLC_SUCCESS;
467 }
468
469 /* TODO: add support in the VLM for queueing proper PLAY requests with
470  * start and end times, fetch whether the input is seekable... and then
471  * clean this up */
472 void vod_play(vod_media_t *p_media, const char *psz_session,
473               int64_t *start, int64_t end)
474 {
475     if (vod_check_range(p_media, psz_session, *start, end) != VLC_SUCCESS)
476         return;
477
478     /* We're passing the #vod{} sout chain here */
479     vod_MediaControl(p_media->p_vod, p_media, psz_session,
480                      VOD_MEDIA_PLAY, "vod", start);
481 }
482
483 void vod_pause(vod_media_t *p_media, const char *psz_session, int64_t *npt)
484 {
485     vod_MediaControl(p_media->p_vod, p_media, psz_session,
486                      VOD_MEDIA_PAUSE, npt);
487 }
488
489 void vod_stop(vod_media_t *p_media, const char *psz_session)
490 {
491     CommandPush(p_media->p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session);
492 }
493
494
495 const char *vod_get_mux(const vod_media_t *p_media)
496 {
497     return p_media->psz_mux;
498 }
499
500
501 /* Match an RTP id to a VoD media ES and RTSP track to initialize it
502  * with the data that was already set up */
503 int vod_init_id(vod_media_t *p_media, const char *psz_session, int es_id,
504                 sout_stream_id_t *sout_id, rtp_format_t *rtp_fmt,
505                 uint32_t *ssrc, uint16_t *seq_init)
506 {
507     media_es_t *p_es;
508
509     if (p_media->psz_mux != NULL)
510     {
511         assert(p_media->i_es == 1);
512         p_es = p_media->es[0];
513     }
514     else
515     {
516         p_es = NULL;
517         /* No locking needed, the ES table can't be modified now */
518         for (int i = 0; i < p_media->i_es; i++)
519         {
520             if (p_media->es[i]->es_id == es_id)
521             {
522                 p_es = p_media->es[i];
523                 break;
524             }
525         }
526         if (p_es == NULL)
527             return VLC_EGENERIC;
528     }
529
530     memcpy(rtp_fmt, &p_es->rtp_fmt, sizeof(*rtp_fmt));
531     if (p_es->rtp_fmt.fmtp != NULL)
532         rtp_fmt->fmtp = strdup(p_es->rtp_fmt.fmtp);
533
534     return RtspTrackAttach(p_media->rtsp, psz_session, p_es->rtsp_id,
535                            sout_id, ssrc, seq_init);
536 }
537
538 /* Remove references to the RTP id from its RTSP track */
539 void vod_detach_id(vod_media_t *p_media, const char *psz_session,
540                    sout_stream_id_t *sout_id)
541 {
542     RtspTrackDetach(p_media->rtsp, psz_session, sout_id);
543 }
544