]> git.sesse.net Git - vlc/blob - modules/demux/sgimb.c
02ea755ef92660acaae4c56958f5c54249fa6956
[vlc] / modules / demux / sgimb.c
1 /*****************************************************************************
2  * sgimb.c: a meta demux to parse sgimb referrer files
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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 loose 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 <vlc_playlist.h>
105
106 /*****************************************************************************
107  * Module descriptor
108  *****************************************************************************/
109 static int  Activate  ( vlc_object_t * );
110 static void Deactivate( vlc_object_t * );
111
112 vlc_module_begin();
113     set_description( _("Kasenna MediaBase metademux") );
114     set_capability( "demux2", 170 );
115     set_callbacks( Activate, Deactivate );
116     add_shortcut( "sgimb" );
117 vlc_module_end();
118
119 /*****************************************************************************
120  * Local prototypes
121  *****************************************************************************/
122 #define MAX_LINE 1024
123
124 struct demux_sys_t
125 {
126     char        *psz_uri;       /* Stream= or sgiQTFileBegin rtsp link */
127     char        *psz_server;    /* sgiNameServerHost= */
128     char        *psz_location;  /* sgiMovieName= */
129     char        *psz_name;      /* sgiShowingName= */
130     char        *psz_user;      /* sgiUserAccount= */
131     char        *psz_password;  /* sgiUserPassword= */
132     char        *psz_mcast_ip;  /* sgiMulticastAddress= */
133     int         i_mcast_port;   /* sgiMulticastPort= */
134     int         i_packet_size;  /* sgiPacketSize= */
135     mtime_t     i_duration;     /* sgiDuration= */
136     int         i_port;         /* sgiRtspPort= */
137     int         i_sid;          /* sgiSid= */
138     vlc_bool_t  b_concert;      /* DeliveryService=cds */
139     vlc_bool_t  b_rtsp_kasenna; /* kasenna style RTSP */
140 };
141
142 static int Demux ( demux_t *p_demux );
143 static int Control( demux_t *p_demux, int i_query, va_list args );
144
145 /*****************************************************************************
146  * Activate: initializes m3u demux structures
147  *****************************************************************************/
148 static int Activate( vlc_object_t * p_this )
149 {
150     demux_t *p_demux = (demux_t *)p_this;
151     demux_sys_t *p_sys;
152     byte_t *p_peek;
153     int i_size;
154
155     /* Lets check the content to see if this is a sgi mediabase file */
156     i_size = stream_Peek( p_demux->s, &p_peek, MAX_LINE );
157     i_size -= sizeof("sgiNameServerHost=") - 1;
158     if ( i_size > 0 )
159     {
160         while ( i_size && strncasecmp( p_peek, "sgiNameServerHost=",
161                                        sizeof("sgiNameServerHost=") - 1 ) )
162         {
163             p_peek++;
164             i_size--;
165         }
166         if ( !strncasecmp( p_peek, "sgiNameServerHost=",
167                            sizeof("sgiNameServerHost=") -1 ) )
168         {
169             p_demux->pf_demux = Demux;
170             p_demux->pf_control = Control;
171
172             p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
173             p_sys->psz_uri = NULL;
174             p_sys->psz_server = NULL;
175             p_sys->psz_location = NULL;
176             p_sys->psz_name = NULL;
177             p_sys->psz_user = NULL;
178             p_sys->psz_password = NULL;
179             p_sys->psz_mcast_ip = NULL;
180             p_sys->i_mcast_port = 0;
181             p_sys->i_packet_size = 0;
182             p_sys->i_duration = 0;
183             p_sys->i_port = 0;
184             p_sys->i_sid = 0;
185             p_sys->b_rtsp_kasenna = VLC_FALSE;
186             p_sys->b_concert = VLC_FALSE;
187
188             return VLC_SUCCESS;
189         }
190     }
191     return VLC_EGENERIC;
192 }
193
194 /*****************************************************************************
195  * Deactivate: frees unused data
196  *****************************************************************************/
197 static void Deactivate( vlc_object_t *p_this )
198 {
199     demux_t *p_demux = (demux_t*)p_this;
200     demux_sys_t *p_sys = p_demux->p_sys;
201     if( p_sys->psz_uri )
202         free( p_sys->psz_uri );
203     if( p_sys->psz_server )
204         free( p_sys->psz_server );
205     if( p_sys->psz_location )
206         free( p_sys->psz_location );
207     if( p_sys->psz_name )
208         free( p_sys->psz_name );
209     if( p_sys->psz_user )
210         free( p_sys->psz_user );
211     if( p_sys->psz_password )
212         free( p_sys->psz_password );
213     if( p_sys->psz_mcast_ip )
214         free( p_sys->psz_mcast_ip );
215     free( p_demux->p_sys );
216     return;
217 }
218
219 static int ParseLine ( demux_t *p_demux, char *psz_line )
220 {
221     char        *psz_bol;
222     demux_sys_t *p_sys = p_demux->p_sys;
223
224     psz_bol = psz_line;
225
226     /* Remove unnecessary tabs or spaces at the beginning of line */
227     while( *psz_bol == ' ' || *psz_bol == '\t' ||
228            *psz_bol == '\n' || *psz_bol == '\r' )
229     {
230         psz_bol++;
231     }
232
233     if( !strncasecmp( psz_bol, "rtsp://", sizeof("rtsp://") - 1 ) )
234     {
235         /* We found the link, it was inside a sgiQTFileBegin */
236         p_sys->psz_uri = strdup( psz_bol );
237     }
238     else if( !strncasecmp( psz_bol, "Stream=\"", sizeof("Stream=\"") - 1 ) )
239     {
240         psz_bol += sizeof("Stream=\"") - 1;
241         if ( !psz_bol )
242             return 0;
243         strrchr( psz_bol, '"' )[0] = '\0';
244         /* We cheat around xdma. for some reason xdma links work different then rtsp */
245         if( !strncasecmp( psz_bol, "xdma://", sizeof("xdma://") - 1 ) )
246         {
247             psz_bol[0] = 'r';
248             psz_bol[1] = 't';
249             psz_bol[2] = 's';
250             psz_bol[3] = 'p';
251         }
252         p_sys->psz_uri = strdup( psz_bol );
253     }
254     else if( !strncasecmp( psz_bol, "sgiNameServerHost=", sizeof("sgiNameServerHost=") - 1 ) )
255     {
256         psz_bol += sizeof("sgiNameServerHost=") - 1;
257         p_sys->psz_server = strdup( psz_bol );
258     }
259     else if( !strncasecmp( psz_bol, "sgiMovieName=", sizeof("sgiMovieName=") - 1 ) )
260     {
261         psz_bol += sizeof("sgiMovieName=") - 1;
262         p_sys->psz_location = strdup( psz_bol );
263     }
264     else if( !strncasecmp( psz_bol, "sgiUserAccount=", sizeof("sgiUserAccount=") - 1 ) )
265     {
266         psz_bol += sizeof("sgiUserAccount=") - 1;
267         p_sys->psz_user = strdup( psz_bol );
268     }
269     else if( !strncasecmp( psz_bol, "sgiUserPassword=", sizeof("sgiUserPassword=") - 1 ) )
270     {
271         psz_bol += sizeof("sgiUserPassword=") - 1;
272         p_sys->psz_password = strdup( psz_bol );
273     }
274     else if( !strncasecmp( psz_bol, "sgiShowingName=", sizeof("sgiShowingName=") - 1 ) )
275     {
276         psz_bol += sizeof("sgiShowingName=") - 1;
277         p_sys->psz_name = strdup( psz_bol );
278     }
279     else if( !strncasecmp( psz_bol, "sgiFormatName=", sizeof("sgiFormatName=") - 1 ) )
280     {
281         psz_bol += sizeof("sgiFormatName=") - 1;
282         if( strcasestr( psz_bol, "MPEG-4") == NULL ) /*not mpeg4 found in string */
283             p_sys->b_rtsp_kasenna = VLC_TRUE;
284     }
285     else if( !strncasecmp( psz_bol, "sgiMulticastAddress=", sizeof("sgiMulticastAddress=") - 1 ) )
286     {
287         psz_bol += sizeof("sgiMulticastAddress=") - 1;
288         p_sys->psz_mcast_ip = strdup( psz_bol );
289     }
290     else if( !strncasecmp( psz_bol, "sgiMulticastPort=", sizeof("sgiMulticastPort=") - 1 ) )
291     {
292         psz_bol += sizeof("sgiMulticastPort=") - 1;
293         p_sys->i_mcast_port = (int) strtol( psz_bol, NULL, 0 );
294     }
295     else if( !strncasecmp( psz_bol, "sgiPacketSize=", sizeof("sgiPacketSize=") - 1 ) )
296     {
297         psz_bol += sizeof("sgiPacketSize=") - 1;
298         p_sys->i_packet_size = (int) strtol( psz_bol, NULL, 0 );
299     }
300     else if( !strncasecmp( psz_bol, "sgiDuration=", sizeof("sgiDuration=") - 1 ) )
301     {
302         psz_bol += sizeof("sgiDuration=") - 1;
303         p_sys->i_duration = (mtime_t) strtol( psz_bol, NULL, 0 );
304     }
305     else if( !strncasecmp( psz_bol, "sgiRtspPort=", sizeof("sgiRtspPort=") - 1 ) )
306     {
307         psz_bol += sizeof("sgiRtspPort=") - 1;
308         p_sys->i_port = (int) strtol( psz_bol, NULL, 0 );
309     }
310     else if( !strncasecmp( psz_bol, "sgiSid=", sizeof("sgiSid=") - 1 ) )
311     {
312         psz_bol += sizeof("sgiSid=") - 1;
313         p_sys->i_sid = (int) strtol( psz_bol, NULL, 0 );
314     }
315     else if( !strncasecmp( psz_bol, "DeliveryService=cds", sizeof("DeliveryService=cds") - 1 ) )
316     {
317         p_sys->b_concert = VLC_TRUE;
318     }
319     else
320     {
321         /* This line isn't really important */
322         return 0;
323     }
324     return VLC_SUCCESS;
325 }
326
327 /*****************************************************************************
328  * Demux: reads and demuxes data packets
329  *****************************************************************************
330  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
331  *****************************************************************************/
332 static int Demux ( demux_t *p_demux )
333 {
334     demux_sys_t     *p_sys = p_demux->p_sys;
335     playlist_t      *p_playlist;
336     playlist_item_t *p_item;
337     
338     char            *psz_line;
339     int             i_position;
340
341     p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
342                                                  FIND_ANYWHERE );
343     if( !p_playlist )
344     {
345         msg_Err( p_demux, "can't find playlist" );
346         return -1;
347     }
348
349     p_playlist->status.p_item->i_flags = PLAYLIST_DEL_FLAG;
350     i_position = -1;  /* FIXME p_playlist->i_index + 1; */
351
352     while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
353     {
354         ParseLine( p_demux, psz_line );
355         if( psz_line ) free( psz_line );
356     }
357
358     if( p_sys->psz_mcast_ip )
359     {
360         /* Definetly schedules multicast session */
361         /* We don't care if it's live or not */
362         char *temp;
363
364         asprintf( &temp, "udp://@" "%s:%i", p_sys->psz_mcast_ip, p_sys->i_mcast_port );
365         if( p_sys->psz_uri ) free( p_sys->psz_uri );
366         p_sys->psz_uri = strdup( temp );
367         free( temp );
368     }
369
370     if( p_sys->psz_uri == NULL )
371     {
372         if( p_sys->psz_server && p_sys->psz_location )
373         {
374             char *temp;
375             
376             asprintf( &temp, "rtsp://" "%s:%i%s",
377                      p_sys->psz_server, p_sys->i_port > 0 ? p_sys->i_port : 554, p_sys->psz_location );
378             
379             p_sys->psz_uri = strdup( temp );
380             free( temp );
381         }
382     }
383
384     if( p_sys->b_concert )
385     {
386         /* It's definetly a simulcasted scheduled stream */
387         /* We don't care if it's live or not */
388         char *temp;
389
390         if( p_sys->psz_uri == NULL )
391         {
392             msg_Err( p_demux, "no URI was found" );
393             return -1;
394         }
395         
396         asprintf( &temp, "%s%%3FMeDiAbAsEshowingId=%d%%26MeDiAbAsEconcert%%3FMeDiAbAsE",
397                 p_sys->psz_uri, p_sys->i_sid );
398
399         free( p_sys->psz_uri );
400         p_sys->psz_uri = strdup( temp );
401         free( temp );
402     }
403
404     p_item = playlist_ItemNew( p_playlist, p_sys->psz_uri,
405                       p_sys->psz_name ? p_sys->psz_name : p_sys->psz_uri );
406
407     if( !p_item || !p_item->input.psz_uri )
408     {
409         msg_Err( p_demux, "A valid playlistitem could not be created" );
410         return VLC_EGENERIC;
411     }
412
413     if( p_sys->i_packet_size && p_sys->psz_mcast_ip )
414     {
415         char *psz_option;
416         p_sys->i_packet_size += 1000;
417         asprintf( &psz_option, "mtu=%i", p_sys->i_packet_size );
418         playlist_ItemAddOption( p_item, psz_option );
419         free( psz_option );
420     }
421     if( !p_sys->psz_mcast_ip )
422     {
423         char *psz_option;
424         asprintf( &psz_option, "rtsp-caching=5000" );
425         playlist_ItemAddOption( p_item, psz_option );
426         free( psz_option );
427     }
428     if( !p_sys->psz_mcast_ip && p_sys->b_rtsp_kasenna )
429     {
430         char *psz_option;
431         asprintf( &psz_option, "rtsp-kasenna" );
432         playlist_ItemAddOption( p_item, psz_option );
433         free( psz_option );
434     }
435
436     playlist_ItemSetDuration( p_item, p_sys->i_duration );
437     playlist_AddItem( p_playlist, p_item, PLAYLIST_INSERT, i_position );
438
439     vlc_object_release( p_playlist );
440     return VLC_SUCCESS;
441 }
442
443 static int Control( demux_t *p_demux, int i_query, va_list args )
444 {
445     return VLC_EGENERIC;
446 }
447