100l, last patch broke window resizing with xv and xvmc.
[mplayer/glamo.git] / stream / stream_rtsp.c
blob90a0ca311a2ef67a69cf86d2d79ab7bce2b356f0
1 /*
2 * Copyright (C) 2006 Benjamin Zores
3 * based on previous Real RTSP support from Roberto Togni and xine team.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <ctype.h>
27 #include "config.h"
28 #ifndef HAVE_WINSOCK2
29 #include <netinet/in.h>
30 #include <sys/socket.h>
31 #include <arpa/inet.h>
32 #define closesocket close
33 #else
34 #include <winsock2.h>
35 #include <ws2tcpip.h>
36 #endif
37 #include <errno.h>
39 #include "stream.h"
40 #include "tcp.h"
41 #include "librtsp/rtsp.h"
42 #include "librtsp/rtsp_session.h"
44 #define RTSP_DEFAULT_PORT 554
46 extern int network_bandwidth;
48 static int
49 rtsp_streaming_read (int fd, char *buffer,
50 int size, streaming_ctrl_t *stream_ctrl)
52 return rtsp_session_read (stream_ctrl->data, buffer, size);
55 static int
56 rtsp_streaming_start (stream_t *stream)
58 int fd;
59 rtsp_session_t *rtsp;
60 char *mrl;
61 char *file;
62 int port;
63 int redirected, temp;
65 if (!stream)
66 return -1;
68 /* counter so we don't get caught in infinite redirections */
69 temp = 5;
71 do {
72 redirected = 0;
74 fd = connect2Server (stream->streaming_ctrl->url->hostname,
75 port = (stream->streaming_ctrl->url->port ?
76 stream->streaming_ctrl->url->port :
77 RTSP_DEFAULT_PORT), 1);
79 if (fd < 0 && !stream->streaming_ctrl->url->port)
80 fd = connect2Server (stream->streaming_ctrl->url->hostname,
81 port = 7070, 1);
83 if (fd < 0)
84 return -1;
86 file = stream->streaming_ctrl->url->file;
87 if (file[0] == '/')
88 file++;
90 mrl = malloc (strlen (stream->streaming_ctrl->url->hostname)
91 + strlen (file) + 16);
93 sprintf (mrl, "rtsp://%s:%i/%s",
94 stream->streaming_ctrl->url->hostname, port, file);
96 rtsp = rtsp_session_start (fd, &mrl, file,
97 stream->streaming_ctrl->url->hostname,
98 port, &redirected,
99 stream->streaming_ctrl->bandwidth,
100 stream->streaming_ctrl->url->username,
101 stream->streaming_ctrl->url->password);
103 if (redirected == 1)
105 url_free (stream->streaming_ctrl->url);
106 stream->streaming_ctrl->url = url_new (mrl);
107 closesocket (fd);
110 free (mrl);
111 temp--;
112 } while ((redirected != 0) && (temp > 0));
114 if (!rtsp)
115 return -1;
117 stream->fd = fd;
118 stream->streaming_ctrl->data = rtsp;
120 stream->streaming_ctrl->streaming_read = rtsp_streaming_read;
121 stream->streaming_ctrl->streaming_seek = NULL;
122 stream->streaming_ctrl->prebuffer_size = 128*1024; // 640 KBytes
123 stream->streaming_ctrl->buffering = 1;
124 stream->streaming_ctrl->status = streaming_playing_e;
126 return 0;
129 static void
130 rtsp_streaming_close (struct stream_st *s)
132 rtsp_session_t *rtsp = NULL;
134 rtsp = (rtsp_session_t *) s->streaming_ctrl->data;
135 if (rtsp)
136 rtsp_session_end (rtsp);
139 static int
140 rtsp_streaming_open (stream_t *stream, int mode, void *opts, int *file_format)
142 URL_t *url;
143 extern int index_mode;
145 mp_msg (MSGT_OPEN, MSGL_V, "STREAM_RTSP, URL: %s\n", stream->url);
146 stream->streaming_ctrl = streaming_ctrl_new ();
147 if (!stream->streaming_ctrl)
148 return STREAM_ERROR;
150 stream->streaming_ctrl->bandwidth = network_bandwidth;
151 url = url_new (stream->url);
152 stream->streaming_ctrl->url = check4proxies (url);
154 stream->fd = -1;
155 index_mode = -1; /* prevent most RTSP streams from locking due to -idx */
156 if (rtsp_streaming_start (stream) < 0)
158 streaming_ctrl_free (stream->streaming_ctrl);
159 stream->streaming_ctrl = NULL;
160 return STREAM_UNSUPORTED;
163 fixup_network_stream_cache (stream);
164 stream->type = STREAMTYPE_STREAM;
165 stream->close = rtsp_streaming_close;
167 return STREAM_OK;
170 stream_info_t stream_info_rtsp = {
171 "RTSP streaming",
172 "rtsp",
173 "Benjamin Zores, Roberto Togni",
174 "ported from xine",
175 rtsp_streaming_open,
176 {"rtsp", NULL},
177 NULL,
178 0 /* Urls are an option string */