1 /*****************************************************************************
2 * sgimb.c: a meta demux to parse sgimb referrer files
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
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.
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.
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 *****************************************************************************/
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 * Stream="xdma://host.name.tld/demo/a_very_cool.mpg"
34 * sgiMovieName=/demo/a_very_cool.mpg
36 * sgiFormatName=PARTNER_41_MPEG-4
38 * sgiDuration=378345000
41 * rtsp://host.name.tld/demo/a_very_cool.mpg
43 * sgiApplicationName=MediaBaseURL
45 * sgiServerVersion=6.1.2
48 * sgiUserAccount=pid=1724&time=1078527309&displayText=You%20are%20logged%20as%20guest&
51 *****************************************************************************/
54 /*****************************************************************************
56 *****************************************************************************/
57 #include <stdlib.h> /* malloc(), free() */
60 #include <vlc/input.h>
61 #include <vlc_playlist.h>
63 /*****************************************************************************
65 *****************************************************************************/
66 static int Activate ( vlc_object_t * );
67 static void Deactivate( vlc_object_t * );
70 set_description( _("Kasenna MediaBase metademux") );
71 set_capability( "demux2", 170 );
72 set_callbacks( Activate, Deactivate );
73 add_shortcut( "sgimb" );
76 /*****************************************************************************
78 *****************************************************************************/
83 char *psz_uri; /* Stream= or sgiQTFileBegin rtsp link */
84 char *psz_server; /* sgiNameServerHost= */
85 char *psz_location; /* sgiMovieName= */
86 char *psz_name; /* sgiShowingName= */
87 char *psz_user; /* sgiUserAccount= */
88 char *psz_password; /* sgiUserPassword= */
89 char *psz_mcast_ip; /* sgiMulticastAddress= */
90 int i_mcast_port; /* sgiMulticastPort= */
91 int i_packet_size; /* sgiPacketSize= */
92 mtime_t i_duration; /* sgiDuration= */
93 int i_port; /* sgiRtspPort= */
94 int i_sid; /* sgiSid= */
97 static int Demux ( demux_t *p_demux );
98 static int Control( demux_t *p_demux, int i_query, va_list args );
100 /*****************************************************************************
101 * Activate: initializes m3u demux structures
102 *****************************************************************************/
103 static int Activate( vlc_object_t * p_this )
105 demux_t *p_demux = (demux_t *)p_this;
110 /* Lets check the content to see if this is a sgi mediabase file */
111 i_size = stream_Peek( p_demux->s, &p_peek, MAX_LINE );
112 i_size -= sizeof("sgiNameServerHost=") - 1;
115 while ( i_size && strncasecmp( p_peek, "sgiNameServerHost=",
116 sizeof("sgiNameServerHost=") - 1 ) )
121 if ( !strncasecmp( p_peek, "sgiNameServerHost=",
122 sizeof("sgiNameServerHost=") -1 ) )
124 p_demux->pf_demux = Demux;
125 p_demux->pf_control = Control;
127 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
128 p_sys->psz_uri = NULL;
129 p_sys->psz_server = NULL;
130 p_sys->psz_location = NULL;
131 p_sys->psz_name = NULL;
132 p_sys->psz_user = NULL;
133 p_sys->psz_password = NULL;
134 p_sys->psz_mcast_ip = NULL;
135 p_sys->i_mcast_port = 0;
136 p_sys->i_packet_size = 0;
137 p_sys->i_duration = 0;
146 /*****************************************************************************
147 * Deactivate: frees unused data
148 *****************************************************************************/
149 static void Deactivate( vlc_object_t *p_this )
151 demux_t *p_demux = (demux_t*)p_this;
152 demux_sys_t *p_sys = p_demux->p_sys;
154 free( p_sys->psz_uri );
155 if( p_sys->psz_server )
156 free( p_sys->psz_server );
157 if( p_sys->psz_location )
158 free( p_sys->psz_location );
159 if( p_sys->psz_name )
160 free( p_sys->psz_name );
161 if( p_sys->psz_user )
162 free( p_sys->psz_user );
163 if( p_sys->psz_password )
164 free( p_sys->psz_password );
165 if( p_sys->psz_mcast_ip )
166 free( p_sys->psz_mcast_ip );
167 free( p_demux->p_sys );
171 static int ParseLine ( demux_t *p_demux, char *psz_line )
174 demux_sys_t *p_sys = p_demux->p_sys;
178 /* Remove unnecessary tabs or spaces at the beginning of line */
179 while( *psz_bol == ' ' || *psz_bol == '\t' ||
180 *psz_bol == '\n' || *psz_bol == '\r' )
185 if( !strncasecmp( psz_bol, "rtsp://", sizeof("rtsp://") - 1 ) )
187 /* We found the link, it was inside a sgiQTFileBegin */
188 p_sys->psz_uri = strdup( psz_bol );
190 else if( !strncasecmp( psz_bol, "Stream=\"", sizeof("Stream=\"") - 1 ) )
192 psz_bol += sizeof("Stream=\"") - 1;
195 strrchr( psz_bol, '"' )[0] = '\0';
196 /* We cheat around xdma. for some reason xdma links work different then rtsp */
197 if( !strncasecmp( psz_bol, "xdma://", sizeof("xdma://") - 1 ) )
204 p_sys->psz_uri = strdup( psz_bol );
206 else if( !strncasecmp( psz_bol, "sgiNameServerHost=", sizeof("sgiNameServerHost=") - 1 ) )
208 psz_bol += sizeof("sgiNameServerHost=") - 1;
209 p_sys->psz_server = strdup( psz_bol );
211 else if( !strncasecmp( psz_bol, "sgiMovieName=", sizeof("sgiMovieName=") - 1 ) )
213 psz_bol += sizeof("sgiMovieName=") - 1;
214 p_sys->psz_location = strdup( psz_bol );
216 else if( !strncasecmp( psz_bol, "sgiUserAccount=", sizeof("sgiUserAccount=") - 1 ) )
218 psz_bol += sizeof("sgiUserAccount=") - 1;
219 p_sys->psz_user = strdup( psz_bol );
221 else if( !strncasecmp( psz_bol, "sgiUserPassword=", sizeof("sgiUserPassword=") - 1 ) )
223 psz_bol += sizeof("sgiUserPassword=") - 1;
224 p_sys->psz_password = strdup( psz_bol );
226 else if( !strncasecmp( psz_bol, "sgiShowingName=", sizeof("sgiShowingName=") - 1 ) )
228 psz_bol += sizeof("sgiShowingName=") - 1;
229 p_sys->psz_name = strdup( psz_bol );
231 else if( !strncasecmp( psz_bol, "sgiMulticastAddress=", sizeof("sgiMulticastAddress=") - 1 ) )
233 psz_bol += sizeof("sgiMulticastAddress=") - 1;
234 p_sys->psz_mcast_ip = strdup( psz_bol );
236 else if( !strncasecmp( psz_bol, "sgiMulticastPort=", sizeof("sgiMulticastPort=") - 1 ) )
238 psz_bol += sizeof("sgiMulticastPort=") - 1;
239 p_sys->i_mcast_port = (int) strtol( psz_bol, NULL, 0 );
241 else if( !strncasecmp( psz_bol, "sgiPacketSize=", sizeof("sgiPacketSize=") - 1 ) )
243 psz_bol += sizeof("sgiPacketSize=") - 1;
244 p_sys->i_packet_size = (int) strtol( psz_bol, NULL, 0 );
246 else if( !strncasecmp( psz_bol, "sgiDuration=", sizeof("sgiDuration=") - 1 ) )
248 psz_bol += sizeof("sgiDuration=") - 1;
249 p_sys->i_duration = (mtime_t) strtol( psz_bol, NULL, 0 );
251 else if( !strncasecmp( psz_bol, "sgiRtspPort=", sizeof("sgiRtspPort=") - 1 ) )
253 psz_bol += sizeof("sgiRtspPort=") - 1;
254 p_sys->i_port = (int) strtol( psz_bol, NULL, 0 );
257 /* This line isn't really important */
263 /*****************************************************************************
264 * Demux: reads and demuxes data packets
265 *****************************************************************************
266 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
267 *****************************************************************************/
268 static int Demux ( demux_t *p_demux )
270 demux_sys_t *p_sys = p_demux->p_sys;
271 playlist_t *p_playlist;
272 playlist_item_t *p_item;
277 p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
281 msg_Err( p_demux, "can't find playlist" );
285 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
286 i_position = p_playlist->i_index + 1;
288 while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
290 ParseLine( p_demux, psz_line );
291 if( psz_line ) free( psz_line );
294 if( p_sys->psz_mcast_ip )
298 temp = (char *)malloc( sizeof("udp/ts2://@000.000.000.000:123456789" ) );
299 sprintf( temp, "udp/ts2://@" "%s:%i", p_sys->psz_mcast_ip, p_sys->i_mcast_port );
300 if( p_sys->psz_uri ) free( p_sys->psz_uri );
301 p_sys->psz_uri = strdup( temp );
304 else if( p_sys->psz_uri == NULL )
306 if( p_sys->psz_server && p_sys->psz_location )
310 temp = (char *)malloc( sizeof("rtsp/live://" ":" "123456789") +
311 strlen( p_sys->psz_server ) + strlen( p_sys->psz_location ) );
312 sprintf( temp, "rtsp/live://" "%s:%i%s",
313 p_sys->psz_server, p_sys->i_port > 0 ? p_sys->i_port : 554, p_sys->psz_location );
315 p_sys->psz_uri = strdup( temp );
320 p_item = playlist_ItemNew( p_playlist, p_sys->psz_uri,
321 p_sys->psz_name ? p_sys->psz_name : p_sys->psz_uri );
323 if( !p_item || !p_item->input.psz_uri )
325 msg_Err( p_demux, "A valid playlistitem could not be created" );
329 if( p_sys->i_packet_size && p_sys->psz_mcast_ip )
332 p_sys->i_packet_size += 1000;
333 asprintf( &psz_option, "mtu=%i", p_sys->i_packet_size );
334 playlist_ItemAddOption( p_item, psz_option );
338 playlist_ItemSetDuration( p_item, p_sys->i_duration );
339 playlist_AddItem( p_playlist, p_item, PLAYLIST_INSERT, i_position );
341 vlc_object_release( p_playlist );
345 static int Control( demux_t *p_demux, int i_query, va_list args )