]> git.sesse.net Git - vlc/blob - modules/demux/playlist/sgimb.c
* Move sgimb demux to the playlist demux, and port it to the new playlist API
[vlc] / modules / demux / playlist / sgimb.c
1 /*****************************************************************************
2  * sgimb.c: a meta demux to parse sgimb referrer files
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <hartman at videolan dot 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  * This is a metademux for the Kasenna MediaBase metafile format.
26  * Kasenna MediaBase first returns this file when you are trying to access
27  * their MPEG streams (MIME: application/x-sgimb). Very few applications
28  * understand this format and the format is not really documented on the net.
29  * Following a typical MediaBase file. Notice the sgi prefix of all the elements.
30  * This stems from the fact that the MediaBase servers were first introduced by SGI?????.
31  *
32  * sgiNameServerHost=host.name.tld
33  *     Obvious: the host hosting this stream
34  * Stream="xdma://host.name.tld/demo/a_very_cool.mpg"
35  *     Not always present. xdma can be read as RTSP.
36  * sgiMovieName=/demo/a_very_cool.mpg
37  *     The path to the asset
38  * sgiAuxState=1|2
39  *     AuxState=2 is always Video On Demand (so not Scheduled)
40  *     Not present with Live streams
41  * sgiLiveFeed=True|False
42  *     Denounces if the stream is live or from assets (Canned?)
43  *     Live appears as a little sattelite dish in the web interface of Kasenna
44  * sgiFormatName=PARTNER_41_MPEG-4
45  *     The type of stream. One of:
46  *       PARTNER_41_MPEG-4 (RTSP MPEG-4 fully compliant)
47  *       MPEG1-Audio       (MP3 Audio streams in MPEG TS)
48  *       MPEG-1            (MPEG 1 A/V in MPEG TS)
49  *       MPEG-2            (MPEG 2 A/V in MPEG TS)
50  * sgiWidth=720
51  *     The width of the to be received stream. Only present if stream is not Live.
52  * sgiHeight=576
53  *     The height of the to be received stream. Only present if stream is not Live.
54  * sgiBitrate=1630208
55  *     The bitrate of the to be received stream. Only present if stream is not Live.
56  * sgiDuration=378345000
57  *     The duration of the to be received stream. Only present if stream is not Live.
58  * sgiQTFileBegin
59  * rtsptext
60  * rtsp://host.name.tld/demo/a_very_cool.mpg
61  * sgiQTFileEnd
62  *     Sometimes present. QT will recognize this as a RTSP reference file, if present.
63  * sgiApplicationName=MediaBaseURL
64  *     Beats me !! :)
65  * sgiElapsedTime=0
66  *     Time passed since the asset was started (resets for repeating non live assets?)
67  * sgiMulticastAddress=233.81.233.15
68  *     The multicast IP used for the Multicast feed.
69  *     Also defines if a stream is multicast or not. (blue dot in kasenna web interface)
70  * sgiMulticastPort=1234
71  *     The multicast port for the same Multicast feed.
72  * sgiPacketSize=16384
73  *     The packetsize of the UDP frames that Kasenna sends. They should have used a default
74  *     that is a multiple of 188 (TS frame size). Most networks don't support more than 1500 anyways.
75  *     Also, when you lose a frame of this size, imagecorruption is more likely then with smaller
76  *     frames.
77  * sgiServerVersion=6.1.2
78  *     Version of the server
79  * sgiRtspPort=554
80  *     TCP port used for RTSP communication
81  * AutoStart=True
82  *     Start playing automatically
83  * DeliveryService=cds
84  *     Simulcasted (scheduled unicast) content. (Green dot in Kasenna web interface) 
85  * sgiShowingName=A nice name that everyone likes
86  *     A human readible descriptive title for this stream.
87  * sgiSid=2311
88  *     Looks like this is the ID of the scheduled asset?
89  * sgiUserAccount=pid=1724&time=1078527309&displayText=You%20are%20logged%20as%20guest&
90  *     User Authentication. Above is a default guest entry. Not required for RTSP communication.
91  * sgiUserPassword=
92  *     Password :)
93  *
94  *****************************************************************************/
95
96
97 /*****************************************************************************
98  * Preamble
99  *****************************************************************************/
100 #include <stdlib.h>                                      /* malloc(), free() */
101
102 #include <vlc/vlc.h>
103 #include <vlc/input.h>
104 #include "playlist.h"
105
106 /*****************************************************************************
107  * Local prototypes
108  *****************************************************************************/
109 #define MAX_LINE 1024
110
111 struct demux_sys_t
112 {
113     char        *psz_uri;       /* Stream= or sgiQTFileBegin rtsp link */
114     char        *psz_server;    /* sgiNameServerHost= */
115     char        *psz_location;  /* sgiMovieName= */
116     char        *psz_name;      /* sgiShowingName= */
117     char        *psz_user;      /* sgiUserAccount= */
118     char        *psz_password;  /* sgiUserPassword= */
119     char        *psz_mcast_ip;  /* sgiMulticastAddress= */
120     int         i_mcast_port;   /* sgiMulticastPort= */
121     int         i_packet_size;  /* sgiPacketSize= */
122     mtime_t     i_duration;     /* sgiDuration= */
123     int         i_port;         /* sgiRtspPort= */
124     int         i_sid;          /* sgiSid= */
125     vlc_bool_t  b_concert;      /* DeliveryService=cds */
126     vlc_bool_t  b_rtsp_kasenna; /* kasenna style RTSP */
127 };
128
129 static int Demux ( demux_t *p_demux );
130 static int Control( demux_t *p_demux, int i_query, va_list args );
131
132 /*****************************************************************************
133  * Activate: initializes m3u demux structures
134  *****************************************************************************/
135 int E_(Import_SGIMB)( vlc_object_t * p_this )
136 {
137     demux_t *p_demux = (demux_t *)p_this;
138     demux_sys_t *p_sys;
139     byte_t *p_peek;
140     int i_size;
141
142     /* Lets check the content to see if this is a sgi mediabase file */
143     i_size = stream_Peek( p_demux->s, &p_peek, MAX_LINE );
144     i_size -= sizeof("sgiNameServerHost=") - 1;
145     if ( i_size > 0 )
146     {
147         unsigned int i_len = sizeof("sgiNameServerHost=") - 1;
148         while ( i_size && strncasecmp( (char *)p_peek, "sgiNameServerHost=", i_len ) )
149         {
150             p_peek++;
151             i_size--;
152         }
153         if ( !strncasecmp( (char *)p_peek, "sgiNameServerHost=", i_len ) )
154         {
155             p_demux->pf_demux = Demux;
156             p_demux->pf_control = Control;
157
158             p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
159             p_sys->psz_uri = NULL;
160             p_sys->psz_server = NULL;
161             p_sys->psz_location = NULL;
162             p_sys->psz_name = NULL;
163             p_sys->psz_user = NULL;
164             p_sys->psz_password = NULL;
165             p_sys->psz_mcast_ip = NULL;
166             p_sys->i_mcast_port = 0;
167             p_sys->i_packet_size = 0;
168             p_sys->i_duration = 0;
169             p_sys->i_port = 0;
170             p_sys->i_sid = 0;
171             p_sys->b_rtsp_kasenna = VLC_FALSE;
172             p_sys->b_concert = VLC_FALSE;
173             
174             msg_Dbg( p_demux, "using sgimb playlist import");
175
176             return VLC_SUCCESS;
177         }
178     }
179     return VLC_EGENERIC;
180 }
181
182 /*****************************************************************************
183  * Deactivate: frees unused data
184  *****************************************************************************/
185 void E_(Close_SGIMB)( vlc_object_t *p_this )
186 {
187     demux_t *p_demux = (demux_t*)p_this;
188     demux_sys_t *p_sys = p_demux->p_sys;
189     if( p_sys->psz_uri )
190         free( p_sys->psz_uri );
191     if( p_sys->psz_server )
192         free( p_sys->psz_server );
193     if( p_sys->psz_location )
194         free( p_sys->psz_location );
195     if( p_sys->psz_name )
196         free( p_sys->psz_name );
197     if( p_sys->psz_user )
198         free( p_sys->psz_user );
199     if( p_sys->psz_password )
200         free( p_sys->psz_password );
201     if( p_sys->psz_mcast_ip )
202         free( p_sys->psz_mcast_ip );
203     free( p_demux->p_sys );
204     return;
205 }
206
207 static int ParseLine ( demux_t *p_demux, char *psz_line )
208 {
209     char        *psz_bol;
210     demux_sys_t *p_sys = p_demux->p_sys;
211
212     psz_bol = psz_line;
213
214     /* Remove unnecessary tabs or spaces at the beginning of line */
215     while( *psz_bol == ' ' || *psz_bol == '\t' ||
216            *psz_bol == '\n' || *psz_bol == '\r' )
217     {
218         psz_bol++;
219     }
220
221     if( !strncasecmp( psz_bol, "rtsp://", sizeof("rtsp://") - 1 ) )
222     {
223         /* We found the link, it was inside a sgiQTFileBegin */
224         p_sys->psz_uri = strdup( psz_bol );
225     }
226     else if( !strncasecmp( psz_bol, "Stream=\"", sizeof("Stream=\"") - 1 ) )
227     {
228         psz_bol += sizeof("Stream=\"") - 1;
229         if ( !psz_bol )
230             return 0;
231         strrchr( psz_bol, '"' )[0] = '\0';
232         /* We cheat around xdma. for some reason xdma links work different then rtsp */
233         if( !strncasecmp( psz_bol, "xdma://", sizeof("xdma://") - 1 ) )
234         {
235             psz_bol[0] = 'r';
236             psz_bol[1] = 't';
237             psz_bol[2] = 's';
238             psz_bol[3] = 'p';
239         }
240         p_sys->psz_uri = strdup( psz_bol );
241     }
242     else if( !strncasecmp( psz_bol, "sgiNameServerHost=", sizeof("sgiNameServerHost=") - 1 ) )
243     {
244         psz_bol += sizeof("sgiNameServerHost=") - 1;
245         p_sys->psz_server = strdup( psz_bol );
246     }
247     else if( !strncasecmp( psz_bol, "sgiMovieName=", sizeof("sgiMovieName=") - 1 ) )
248     {
249         psz_bol += sizeof("sgiMovieName=") - 1;
250         p_sys->psz_location = strdup( psz_bol );
251     }
252     else if( !strncasecmp( psz_bol, "sgiUserAccount=", sizeof("sgiUserAccount=") - 1 ) )
253     {
254         psz_bol += sizeof("sgiUserAccount=") - 1;
255         p_sys->psz_user = strdup( psz_bol );
256     }
257     else if( !strncasecmp( psz_bol, "sgiUserPassword=", sizeof("sgiUserPassword=") - 1 ) )
258     {
259         psz_bol += sizeof("sgiUserPassword=") - 1;
260         p_sys->psz_password = strdup( psz_bol );
261     }
262     else if( !strncasecmp( psz_bol, "sgiShowingName=", sizeof("sgiShowingName=") - 1 ) )
263     {
264         psz_bol += sizeof("sgiShowingName=") - 1;
265         p_sys->psz_name = strdup( psz_bol );
266     }
267     else if( !strncasecmp( psz_bol, "sgiFormatName=", sizeof("sgiFormatName=") - 1 ) )
268     {
269         psz_bol += sizeof("sgiFormatName=") - 1;
270         if( strcasestr( psz_bol, "MPEG-4") == NULL ) /*not mpeg4 found in string */
271             p_sys->b_rtsp_kasenna = VLC_TRUE;
272     }
273     else if( !strncasecmp( psz_bol, "sgiMulticastAddress=", sizeof("sgiMulticastAddress=") - 1 ) )
274     {
275         psz_bol += sizeof("sgiMulticastAddress=") - 1;
276         p_sys->psz_mcast_ip = strdup( psz_bol );
277     }
278     else if( !strncasecmp( psz_bol, "sgiMulticastPort=", sizeof("sgiMulticastPort=") - 1 ) )
279     {
280         psz_bol += sizeof("sgiMulticastPort=") - 1;
281         p_sys->i_mcast_port = (int) strtol( psz_bol, NULL, 0 );
282     }
283     else if( !strncasecmp( psz_bol, "sgiPacketSize=", sizeof("sgiPacketSize=") - 1 ) )
284     {
285         psz_bol += sizeof("sgiPacketSize=") - 1;
286         p_sys->i_packet_size = (int) strtol( psz_bol, NULL, 0 );
287     }
288     else if( !strncasecmp( psz_bol, "sgiDuration=", sizeof("sgiDuration=") - 1 ) )
289     {
290         psz_bol += sizeof("sgiDuration=") - 1;
291         p_sys->i_duration = (mtime_t) strtol( psz_bol, NULL, 0 );
292     }
293     else if( !strncasecmp( psz_bol, "sgiRtspPort=", sizeof("sgiRtspPort=") - 1 ) )
294     {
295         psz_bol += sizeof("sgiRtspPort=") - 1;
296         p_sys->i_port = (int) strtol( psz_bol, NULL, 0 );
297     }
298     else if( !strncasecmp( psz_bol, "sgiSid=", sizeof("sgiSid=") - 1 ) )
299     {
300         psz_bol += sizeof("sgiSid=") - 1;
301         p_sys->i_sid = (int) strtol( psz_bol, NULL, 0 );
302     }
303     else if( !strncasecmp( psz_bol, "DeliveryService=cds", sizeof("DeliveryService=cds") - 1 ) )
304     {
305         p_sys->b_concert = VLC_TRUE;
306     }
307     else
308     {
309         /* This line isn't really important */
310         return 0;
311     }
312     return VLC_SUCCESS;
313 }
314
315 /*****************************************************************************
316  * Demux: reads and demuxes data packets
317  *****************************************************************************
318  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
319  *****************************************************************************/
320 static int Demux ( demux_t *p_demux )
321 {
322     demux_sys_t     *p_sys = p_demux->p_sys;
323     input_item_t    *p_child = NULL;
324     char            *psz_line;
325
326     INIT_PLAYLIST_STUFF;
327
328     while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
329     {
330         ParseLine( p_demux, psz_line );
331         if( psz_line ) free( psz_line );
332     }
333
334     if( p_sys->psz_mcast_ip )
335     {
336         /* Definetly schedules multicast session */
337         /* We don't care if it's live or not */
338         char *temp;
339
340         asprintf( &temp, "udp://@" "%s:%i", p_sys->psz_mcast_ip, p_sys->i_mcast_port );
341         if( p_sys->psz_uri ) free( p_sys->psz_uri );
342         p_sys->psz_uri = strdup( temp );
343         free( temp );
344     }
345
346     if( p_sys->psz_uri == NULL )
347     {
348         if( p_sys->psz_server && p_sys->psz_location )
349         {
350             char *temp;
351
352             asprintf( &temp, "rtsp://" "%s:%i%s",
353                      p_sys->psz_server, p_sys->i_port > 0 ? p_sys->i_port : 554, p_sys->psz_location );
354
355             p_sys->psz_uri = strdup( temp );
356             free( temp );
357         }
358     }
359
360     if( p_sys->b_concert )
361     {
362         /* It's definetly a simulcasted scheduled stream */
363         /* We don't care if it's live or not */
364         char *temp;
365
366         if( p_sys->psz_uri == NULL )
367         {
368             msg_Err( p_demux, "no URI was found" );
369             return -1;
370         }
371
372         asprintf( &temp, "%s%%3FMeDiAbAsEshowingId=%d%%26MeDiAbAsEconcert%%3FMeDiAbAsE",
373                 p_sys->psz_uri, p_sys->i_sid );
374
375         free( p_sys->psz_uri );
376         p_sys->psz_uri = strdup( temp );
377         free( temp );
378     }
379
380     p_child = input_ItemNewWithType( (vlc_object_t *)p_playlist, p_sys->psz_uri,
381                       p_sys->psz_name ? p_sys->psz_name : p_sys->psz_uri,
382                       0, NULL, p_sys->i_duration, ITEM_TYPE_NET );
383     
384     if( !p_child )
385     {
386         msg_Err( p_demux, "A valid playlistitem could not be created" );
387         return VLC_EGENERIC;
388     }
389
390     vlc_input_item_CopyOptions( p_current->p_input, p_child );
391     if( p_sys->i_packet_size && p_sys->psz_mcast_ip )
392     {
393         char *psz_option;
394         p_sys->i_packet_size += 1000;
395         asprintf( &psz_option, "mtu=%i", p_sys->i_packet_size );
396         vlc_input_item_AddOption( p_child, psz_option );
397         free( psz_option );
398     }
399     if( !p_sys->psz_mcast_ip )
400     {
401         char *psz_option;
402         asprintf( &psz_option, "rtsp-caching=5000" );
403         vlc_input_item_AddOption( p_child, psz_option );
404         free( psz_option );
405     }
406     if( !p_sys->psz_mcast_ip && p_sys->b_rtsp_kasenna )
407     {
408         char *psz_option;
409         asprintf( &psz_option, "rtsp-kasenna" );
410         vlc_input_item_AddOption( p_child, psz_option );
411         free( psz_option );
412     }
413
414     playlist_AddWhereverNeeded( p_playlist, p_child, p_current,
415                                  p_item_in_category, (i_parent_id > 0 )? VLC_TRUE : VLC_FALSE,
416                                  PLAYLIST_APPEND );
417     HANDLE_PLAY_AND_RELEASE
418     return VLC_SUCCESS;
419 }
420
421 static int Control( demux_t *p_demux, int i_query, va_list args )
422 {
423     return VLC_EGENERIC;
424 }
425