File: rtsp.c

package info (click to toggle)
gmerlin-avdecoder 2.0.0~svn6298~dfsg0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,112 kB
  • sloc: ansic: 94,580; sh: 705; makefile: 705; awk: 43; sed: 16
file content (350 lines) | stat: -rw-r--r-- 7,662 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*****************************************************************
 * gmerlin-avdecoder - a general purpose multimedia decoding library
 *
 * Copyright (c) 2001 - 2012 Members of the Gmerlin project
 * gmerlin-general@lists.sourceforge.net
 * http://gmerlin.sourceforge.net
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/

#include <avdec_private.h>

#include <rtsp.h>
#include <sdp.h>

#include <http.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>



/* Real specific stuff */

#include <rmff.h>

#define LOG_DOMAIN "rtsp"

struct bgav_rtsp_s
  {
  int fd;
  int cseq;
  
  char * session;
  char * url;

  bgav_http_header_t * res;
  bgav_http_header_t * req;
  
  bgav_sdp_t sdp;

  const bgav_options_t * opt;
  
  };


#define DUMP_REQUESTS

static int rtsp_send_request(bgav_rtsp_t * rtsp,
                             const char * command,
                             const char * what,
                             int * got_redirected)
  {
  const char * var;
  int status;
  char * line = NULL;
  char * request;
  int i;
  
  /* We must send the whole request at once so that
     RTSP aware NATs/firewalls recognize our requests and
     open the UDP ports for us
  */
  
  rtsp->cseq++;

  

  request = bgav_sprintf("%s %s RTSP/1.0\r\n", command, what);
  
  for(i = 0; i < rtsp->req->num_lines; i++)
    {
    request = gavl_strcat(request, rtsp->req->lines[i]);
    request = gavl_strcat(request, "\r\n");
    }

  if(rtsp->session)
    {
    line = bgav_sprintf("Session: %s\r\n", rtsp->session);
    request = gavl_strcat(request, line);
    free(line);
    }
  
  line = bgav_sprintf("CSeq: %u\r\n", rtsp->cseq);
  request = gavl_strcat(request, line);
  free(line);

  request = gavl_strcat(request, "\r\n");

#ifdef DUMP_REQUESTS
  bgav_dprintf("Sending request:\n%s", request);
#endif  
  
  if(!bgav_tcp_send(rtsp->opt, rtsp->fd, (uint8_t*)request, strlen(request)))
    {
    free(request);
    goto fail;
    }
  free(request);
  
  bgav_http_header_reset(rtsp->req);
  
  /* Read answers */
  bgav_http_header_reset(rtsp->res);
  if(!bgav_http_header_revc(rtsp->opt, rtsp->res, rtsp->fd))
    return 0;
  
  /* Handle redirection */
  
  if(strstr(rtsp->res->lines[0], "REDIRECT"))
    {
    free(rtsp->url);
    rtsp->url =
      gavl_strdup(bgav_http_header_get_var(rtsp->res,"Location"));
    if(got_redirected)
      *got_redirected = 1;
#if 1
    /* Redirection means new session */
    if(rtsp->session)
      {
      free(rtsp->session);
      rtsp->session = NULL;
      }
#endif
    return 1;
    }

  /* Get the server status */
  status = bgav_http_header_status_code(rtsp->res);

#ifdef DUMP_REQUESTS
  bgav_dprintf("Got answer %d:\n", status);
  bgav_http_header_dump(rtsp->res);
#endif  

  if(status != 200)
    {
    gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN,
             "%s", bgav_http_header_status_line(rtsp->res));
    goto fail;
    }
  var = bgav_http_header_get_var(rtsp->res, "Session");
  if(var && !(rtsp->session)) 
    rtsp->session = gavl_strdup(var);
  return 1;
  
  fail:
  return 0;
  }

void bgav_rtsp_schedule_field(bgav_rtsp_t * rtsp, const char * field)
  {
  bgav_http_header_add_line(rtsp->req, field);
  }

const char * bgav_rtsp_get_answer(bgav_rtsp_t * rtsp, const char * name)
  {
  return bgav_http_header_get_var(rtsp->res, name);
  }

int bgav_rtsp_request_describe(bgav_rtsp_t *rtsp, int * got_redirected)
  {
  int content_length;
  const char * var;
  char * buf = NULL;

  /* Send the "DESCRIBE" request */
  
  if(!rtsp_send_request(rtsp, "DESCRIBE", rtsp->url, got_redirected))
    goto fail;

  if(got_redirected && *got_redirected)
    {
    return 1;
    }
  var = bgav_http_header_get_var(rtsp->res, "Content-Length");
  if(!var)
    goto fail;
  
  content_length = atoi(var);
  
  buf = malloc(content_length+1);
  
  if(bgav_read_data_fd(rtsp->opt, rtsp->fd, (uint8_t*)buf,
                       content_length, rtsp->opt->read_timeout) <
     content_length)
    {
    gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN,
             "Reading session description failed");
    goto fail;
    }

  buf[content_length] = '\0';
  
  if(!bgav_sdp_parse(rtsp->opt, buf, &rtsp->sdp))
    goto fail;

  //  bgav_sdp_dump(&rtsp->sdp);
  
  
  free(buf);

  return 1;
  
  fail:

  if(buf)
    free(buf);
  return 0;
  }

int bgav_rtsp_request_setup(bgav_rtsp_t *r, const char *what)
  {
  return rtsp_send_request(r,"SETUP",what, NULL);
  }

int bgav_rtsp_request_setparameter(bgav_rtsp_t * r)
  {
  return rtsp_send_request(r,"SET_PARAMETER",r->url, NULL);
  }

int bgav_rtsp_request_play(bgav_rtsp_t * r)
  {
  return rtsp_send_request(r,"PLAY",r->url, NULL);
  }

/*
 *  Open connection to the server, get options and handle redirections
 *
 *  Return FALSE if error, TRUE on success.
 *  If we got redirectred, TRUE is returned and got_redirected
 *  is set to 1. The new URL is copied to the rtsp structure
 */

static int do_connect(bgav_rtsp_t * rtsp,
                      int * got_redirected, int get_options)
  {
  int port = -1;
  char * host = NULL;
  char * protocol = NULL;
  int ret = 0;
  if(!bgav_url_split(rtsp->url,
                     &protocol,
                     NULL, /* User */
                     NULL, /* Pass */
                     &host,
                     &port, NULL))
    goto done;
  
  if(strcmp(protocol, "rtsp"))
    goto done;

  if(port == -1)
    port = 554;

  //  rtsp->cseq = 1;
  rtsp->fd = bgav_tcp_connect(rtsp->opt, host, port);
  if(rtsp->fd < 0)
    goto done;

  if(get_options)
    {
    if(!rtsp_send_request(rtsp, "OPTIONS", rtsp->url, got_redirected))
      goto done;
    }
  
  ret = 1;

  done:

  if(!ret && (rtsp->fd >= 0))
    closesocket(rtsp->fd);
  
  if(host)
    free(host);
  if(protocol)
    free(protocol);
  return ret;
  }

bgav_rtsp_t * bgav_rtsp_create(const bgav_options_t * opt)
  {
  bgav_rtsp_t * ret = NULL;
  ret = calloc(1, sizeof(*ret));
  ret->opt = opt;
  ret->res = bgav_http_header_create();
  ret->req = bgav_http_header_create();
  ret->fd = -1;
  return ret;
  }

int bgav_rtsp_open(bgav_rtsp_t * rtsp, const char * url,
                   int * got_redirected)
  {
  if(url)
    rtsp->url = gavl_strdup(url);
  return do_connect(rtsp, got_redirected, 1);
  }

int bgav_rtsp_reopen(bgav_rtsp_t * rtsp)
  {
  int got_redirected = 0;
  if(rtsp->fd >= 0)
    closesocket(rtsp->fd);
  return do_connect(rtsp, &got_redirected, 0);
  }

bgav_sdp_t * bgav_rtsp_get_sdp(bgav_rtsp_t * r)
  {
  return &r->sdp;
  }

void bgav_rtsp_close(bgav_rtsp_t * r, int teardown)
  {
  if(teardown && (r->fd >= 0))
    rtsp_send_request(r,"TEARDOWN",r->url, NULL);
  
  bgav_http_header_destroy(r->res);
  bgav_http_header_destroy(r->req);
  bgav_sdp_free(&r->sdp);
  if(r->url) free(r->url);
  if(r->session) free(r->session);

  if(r->fd > 0)
    closesocket(r->fd);
  
  free(r);
  }

int bgav_rtsp_get_fd(bgav_rtsp_t * r)
  {
  return r->fd;
  }

const char * bgav_rtsp_get_url(bgav_rtsp_t * r)
  {
  return r->url;
  }