1 /*****************************************************************************
2 * sgimb.c: a meta demux to parse sgimb referrer files
3 *****************************************************************************
4 * Copyright (C) 2004 VLC authors and VideoLAN
7 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
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?????.
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
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)
51 * The width of the to be received stream. Only present if stream is not Live.
53 * The height of the to be received stream. Only present if stream is not Live.
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.
60 * rtsp://host.name.tld/demo/a_very_cool.mpg
62 * Sometimes present. QT will recognize this as a RTSP reference file, if present.
63 * sgiApplicationName=MediaBaseURL
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.
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
77 * sgiServerVersion=6.1.2
78 * Version of the server
80 * TCP port used for RTSP communication
82 * Start playing automatically
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.
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.
94 *****************************************************************************/
97 /*****************************************************************************
99 *****************************************************************************/
105 #include <vlc_common.h>
106 #include <vlc_demux.h>
107 #include "playlist.h"
109 /*****************************************************************************
111 *****************************************************************************/
112 #define MAX_LINE 1024
116 char *psz_uri; /* Stream= or sgiQTFileBegin rtsp link */
117 char *psz_server; /* sgiNameServerHost= */
118 char *psz_location; /* sgiMovieName= */
119 char *psz_name; /* sgiShowingName= */
120 char *psz_user; /* sgiUserAccount= */
121 char *psz_password; /* sgiUserPassword= */
122 char *psz_mcast_ip; /* sgiMulticastAddress= */
123 int i_mcast_port; /* sgiMulticastPort= */
124 int i_packet_size; /* sgiPacketSize= */
125 mtime_t i_duration; /* sgiDuration= */
126 int i_port; /* sgiRtspPort= */
127 int i_sid; /* sgiSid= */
128 bool b_concert; /* DeliveryService=cds */
129 bool b_rtsp_kasenna; /* kasenna style RTSP */
132 static int Demux ( demux_t *p_demux );
134 /*****************************************************************************
135 * Activate: initializes m3u demux structures
136 *****************************************************************************/
137 int Import_SGIMB( vlc_object_t * p_this )
139 demux_t *p_demux = (demux_t *)p_this;
140 const uint8_t *p_peek;
143 /* Lets check the content to see if this is a sgi mediabase file */
144 i_size = stream_Peek( p_demux->s, &p_peek, MAX_LINE );
145 i_size -= sizeof("sgiNameServerHost=") - 1;
148 unsigned int i_len = sizeof("sgiNameServerHost=") - 1;
149 while ( i_size && strncasecmp( (char *)p_peek, "sgiNameServerHost=", i_len ) )
154 if ( !strncasecmp( (char *)p_peek, "sgiNameServerHost=", i_len ) )
156 STANDARD_DEMUX_INIT_MSG( "using SGIMB playlist reader" );
157 p_demux->p_sys->psz_uri = NULL;
158 p_demux->p_sys->psz_server = NULL;
159 p_demux->p_sys->psz_location = NULL;
160 p_demux->p_sys->psz_name = NULL;
161 p_demux->p_sys->psz_user = NULL;
162 p_demux->p_sys->psz_password = NULL;
163 p_demux->p_sys->psz_mcast_ip = NULL;
164 p_demux->p_sys->i_mcast_port = 0;
165 p_demux->p_sys->i_packet_size = 0;
166 p_demux->p_sys->i_duration = 0;
167 p_demux->p_sys->i_port = 0;
168 p_demux->p_sys->i_sid = 0;
169 p_demux->p_sys->b_rtsp_kasenna = false;
170 p_demux->p_sys->b_concert = false;
178 /*****************************************************************************
179 * Deactivate: frees unused data
180 *****************************************************************************/
181 void Close_SGIMB( vlc_object_t *p_this )
183 demux_t *p_demux = (demux_t*)p_this;
184 demux_sys_t *p_sys = p_demux->p_sys;
185 free( p_sys->psz_uri );
186 free( p_sys->psz_server );
187 free( p_sys->psz_location );
188 free( p_sys->psz_name );
189 free( p_sys->psz_user );
190 free( p_sys->psz_password );
191 free( p_sys->psz_mcast_ip );
192 free( p_demux->p_sys );
196 static int ParseLine ( demux_t *p_demux, char *psz_line )
199 demux_sys_t *p_sys = p_demux->p_sys;
203 /* Remove unnecessary tabs or spaces at the beginning of line */
204 while( *psz_bol == ' ' || *psz_bol == '\t' ||
205 *psz_bol == '\n' || *psz_bol == '\r' )
210 if( !strncasecmp( psz_bol, "rtsp://", sizeof("rtsp://") - 1 ) )
212 /* We found the link, it was inside a sgiQTFileBegin */
213 free( p_sys->psz_uri );
214 p_sys->psz_uri = strdup( psz_bol );
216 else if( !strncasecmp( psz_bol, "Stream=\"", sizeof("Stream=\"") - 1 ) )
218 psz_bol += sizeof("Stream=\"") - 1;
219 char* psz_tmp = strrchr( psz_bol, '"' );
223 /* We cheat around xdma. for some reason xdma links work different then rtsp */
224 if( !strncasecmp( psz_bol, "xdma://", sizeof("xdma://") - 1 ) )
231 free( p_sys->psz_uri );
232 p_sys->psz_uri = strdup( psz_bol );
234 else if( !strncasecmp( psz_bol, "sgiNameServerHost=", sizeof("sgiNameServerHost=") - 1 ) )
236 psz_bol += sizeof("sgiNameServerHost=") - 1;
237 free( p_sys->psz_server );
238 p_sys->psz_server = strdup( psz_bol );
240 else if( !strncasecmp( psz_bol, "sgiMovieName=", sizeof("sgiMovieName=") - 1 ) )
242 psz_bol += sizeof("sgiMovieName=") - 1;
243 free( p_sys->psz_location );
244 p_sys->psz_location = strdup( psz_bol );
246 else if( !strncasecmp( psz_bol, "sgiUserAccount=", sizeof("sgiUserAccount=") - 1 ) )
248 psz_bol += sizeof("sgiUserAccount=") - 1;
249 free( p_sys->psz_user );
250 p_sys->psz_user = strdup( psz_bol );
252 else if( !strncasecmp( psz_bol, "sgiUserPassword=", sizeof("sgiUserPassword=") - 1 ) )
254 psz_bol += sizeof("sgiUserPassword=") - 1;
255 free( p_sys->psz_password );
256 p_sys->psz_password = strdup( psz_bol );
258 else if( !strncasecmp( psz_bol, "sgiShowingName=", sizeof("sgiShowingName=") - 1 ) )
260 psz_bol += sizeof("sgiShowingName=") - 1;
261 free( p_sys->psz_name );
262 p_sys->psz_name = strdup( psz_bol );
264 else if( !strncasecmp( psz_bol, "sgiFormatName=", sizeof("sgiFormatName=") - 1 ) )
266 psz_bol += sizeof("sgiFormatName=") - 1;
267 if( strcasestr( psz_bol, "MPEG-4") == NULL ) /*not mpeg4 found in string */
268 p_sys->b_rtsp_kasenna = true;
270 else if( !strncasecmp( psz_bol, "sgiMulticastAddress=", sizeof("sgiMulticastAddress=") - 1 ) )
272 psz_bol += sizeof("sgiMulticastAddress=") - 1;
273 free( p_sys->psz_mcast_ip );
274 p_sys->psz_mcast_ip = strdup( psz_bol );
276 else if( !strncasecmp( psz_bol, "sgiMulticastPort=", sizeof("sgiMulticastPort=") - 1 ) )
278 psz_bol += sizeof("sgiMulticastPort=") - 1;
279 p_sys->i_mcast_port = (int) strtol( psz_bol, NULL, 0 );
281 else if( !strncasecmp( psz_bol, "sgiPacketSize=", sizeof("sgiPacketSize=") - 1 ) )
283 psz_bol += sizeof("sgiPacketSize=") - 1;
284 p_sys->i_packet_size = (int) strtol( psz_bol, NULL, 0 );
286 else if( !strncasecmp( psz_bol, "sgiDuration=", sizeof("sgiDuration=") - 1 ) )
288 psz_bol += sizeof("sgiDuration=") - 1;
289 p_sys->i_duration = (mtime_t) strtol( psz_bol, NULL, 0 );
291 else if( !strncasecmp( psz_bol, "sgiRtspPort=", sizeof("sgiRtspPort=") - 1 ) )
293 psz_bol += sizeof("sgiRtspPort=") - 1;
294 p_sys->i_port = (int) strtol( psz_bol, NULL, 0 );
296 else if( !strncasecmp( psz_bol, "sgiSid=", sizeof("sgiSid=") - 1 ) )
298 psz_bol += sizeof("sgiSid=") - 1;
299 p_sys->i_sid = (int) strtol( psz_bol, NULL, 0 );
301 else if( !strncasecmp( psz_bol, "DeliveryService=cds", sizeof("DeliveryService=cds") - 1 ) )
303 p_sys->b_concert = true;
307 /* This line isn't really important */
313 /*****************************************************************************
314 * Demux: reads and demuxes data packets
315 *****************************************************************************
316 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
317 *****************************************************************************/
318 static int Demux ( demux_t *p_demux )
320 demux_sys_t *p_sys = p_demux->p_sys;
321 input_item_t *p_child = NULL;
324 input_item_t *p_current_input = GetCurrentItem(p_demux);
326 while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
328 ParseLine( p_demux, psz_line );
332 if( p_sys->psz_mcast_ip )
334 /* Definetly schedules multicast session */
335 /* We don't care if it's live or not */
336 free( p_sys->psz_uri );
337 if( asprintf( &p_sys->psz_uri, "udp://@" "%s:%i", p_sys->psz_mcast_ip, p_sys->i_mcast_port ) == -1 )
339 p_sys->psz_uri = NULL;
344 if( p_sys->psz_uri == NULL )
346 if( p_sys->psz_server && p_sys->psz_location )
348 if( asprintf( &p_sys->psz_uri, "rtsp://" "%s:%i%s",
349 p_sys->psz_server, p_sys->i_port > 0 ? p_sys->i_port : 554, p_sys->psz_location ) == -1 )
351 p_sys->psz_uri = NULL;
357 if( p_sys->b_concert )
359 /* It's definetly a simulcasted scheduled stream */
360 /* We don't care if it's live or not */
361 if( p_sys->psz_uri == NULL )
363 msg_Err( p_demux, "no URI was found" );
368 if( asprintf( &uri, "%s%%3FMeDiAbAsEshowingId=%d%%26MeDiAbAsEconcert"
369 "%%3FMeDiAbAsE", p_sys->psz_uri, p_sys->i_sid ) == -1 )
371 free( p_sys->psz_uri );
372 p_sys->psz_uri = uri;
375 p_child = input_item_NewWithType( p_sys->psz_uri,
376 p_sys->psz_name ? p_sys->psz_name : p_sys->psz_uri,
377 0, NULL, 0, p_sys->i_duration, ITEM_TYPE_NET );
381 msg_Err( p_demux, "A valid playlistitem could not be created" );
385 input_item_CopyOptions( p_current_input, p_child );
386 if( p_sys->i_packet_size && p_sys->psz_mcast_ip )
389 p_sys->i_packet_size += 1000;
390 if( asprintf( &psz_option, "mtu=%i", p_sys->i_packet_size ) != -1 )
392 input_item_AddOption( p_child, psz_option, VLC_INPUT_OPTION_TRUSTED );
396 if( !p_sys->psz_mcast_ip )
397 input_item_AddOption( p_child, "rtsp-caching=5000", VLC_INPUT_OPTION_TRUSTED );
398 if( !p_sys->psz_mcast_ip && p_sys->b_rtsp_kasenna )
399 input_item_AddOption( p_child, "rtsp-kasenna", VLC_INPUT_OPTION_TRUSTED );
401 input_item_PostSubItem( p_current_input, p_child );
402 vlc_gc_decref( p_child );
403 vlc_gc_decref(p_current_input);
404 return 0; /* Needed for correct operation of go back */