-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrtsp_client.c
415 lines (363 loc) · 10.5 KB
/
rtsp_client.c
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*****************************************************************************
* rtsp_client.c: RTSP Client
*
* Copyright (C) 2004 Shiro Ninomiya <[email protected]>
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************
*
* RaopX:
*
* Morten Hersson <[email protected]>
* Tormod Omholt-Jensen <[email protected]>
* - Minor code changes to make source compile on Os X
*****************************************************************************/
#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "aexcl_lib.h"
#include "rtsp_client.h"
#define MAX_NUM_KD 20
typedef struct rtspcl_data_t {
int fd;
char url[128];
int cseq;
key_data_t *kd;
key_data_t *exthds;
char *session;
char *transport;
u_int16_t server_port;
struct in_addr host_addr;
struct in_addr local_addr;
const char *useragent;
} rtspcl_data_t;
static int exec_request(rtspcl_data_t *rtspcld, char *cmd, char *content_type,
char *content, int get_response, key_data_t *hds, key_data_t **kd);
rtspcl_t *rtspcl_open()
{
rtspcl_data_t *rtspcld;
rtspcld=malloc(sizeof(rtspcl_data_t));
memset(rtspcld, 0, sizeof(rtspcl_data_t));
rtspcld->useragent="RTSPClient";
return (rtspcl_t *)rtspcld;
}
int rtspcl_close(rtspcl_t *p)
{
rtspcl_data_t *rtspcld;
if(!p) return -1;
rtspcld=(rtspcl_data_t *)p;
rtspcl_disconnect(p);
free(rtspcld);
p=NULL;
rtspcl_remove_all_exthds(p);
return 0;
}
u_int16_t rtspcl_get_server_port(rtspcl_t *p)
{
rtspcl_data_t *rtspcld;
if(!p) return -1;
rtspcld=(rtspcl_data_t *)p;
return rtspcld->server_port;
}
int rtspcl_set_useragent(rtspcl_t *p, const char *name)
{
rtspcl_data_t *rtspcld;
if(!p) return -1;
rtspcld=(rtspcl_data_t *)p;
rtspcld->useragent=name;
return 0;
}
int rtspcl_add_exthds(rtspcl_t *p, char *key, char *data)
{
int i=0;
rtspcl_data_t *rtspcld;
if(!p) return -1;
rtspcld=(rtspcl_data_t *)p;
if(!rtspcld->exthds){
if(realloc_memory((void*)&rtspcld->exthds, 17*sizeof(key_data_t),__func__)) return -1;
}else{
i=0;
while(rtspcld->exthds[i].key) {
if(rtspcld->exthds[i].key[0]==0xff) break;
i++;
}
if(i && i%16==0 && rtspcld->exthds[i].key[0]!=0xff){
if(realloc_memory((void*)&rtspcld->exthds,(16*((i%16)+1)+1)*sizeof(key_data_t),__func__))
return -1;
memset(rtspcld->exthds+16*(i/16),0,17*sizeof(key_data_t));
}
}
if(realloc_memory((void*)&rtspcld->exthds[i].key,strlen(key),__func__)) return -1;
strcpy((char*)rtspcld->exthds[i].key,key);
if(realloc_memory((void*)&rtspcld->exthds[i].data,strlen(data),__func__)) return -1;
strcpy((char*)rtspcld->exthds[i].data,data);
rtspcld->exthds[i+1].key=NULL;
return 0;
}
int rtspcl_mark_del_exthds(rtspcl_t *p, char *key)
{
int i=0;
rtspcl_data_t *rtspcld;
if(!p) return -1;
rtspcld=(rtspcl_data_t *)p;
if(!rtspcld->exthds) return -1;
while(rtspcld->exthds[i].key) {
if(!strcmp((char*)key,(char*)rtspcld->exthds[i].key)){
rtspcld->exthds[i].key[0]=0xff;
return 0;
}
i++;
}
return -1;
}
int rtspcl_remove_all_exthds(rtspcl_t *p)
{
int i=0;
rtspcl_data_t *rtspcld;
if(!p) return -1;
rtspcld=(rtspcl_data_t *)p;
while(rtspcld->exthds && rtspcld->exthds[i].key) {
free(rtspcld->exthds[i].key);
free(rtspcld->exthds[i].data);
}
free(rtspcld->exthds);
rtspcld->exthds=NULL;
return 0;
}
int rtspcl_connect(rtspcl_t *p, char *host, u_int16_t destport, char *sid)
{
u_int16_t myport=0;
struct sockaddr_in name;
socklen_t namelen=sizeof(name);
rtspcl_data_t *rtspcld;
if(!p) return -1;
rtspcld=(rtspcl_data_t *)p;
if((rtspcld->fd=open_tcp_socket(NULL, &myport))==-1) return -1;
if(get_tcp_connect_by_host(rtspcld->fd, host, destport)) return -1;
getsockname(rtspcld->fd, (struct sockaddr*)&name, &namelen);
memcpy(&rtspcld->local_addr,&name.sin_addr,sizeof(struct in_addr));
sprintf(rtspcld->url,"rtsp://%s/%s",inet_ntoa(name.sin_addr),sid);
getpeername(rtspcld->fd, (struct sockaddr*)&name, &namelen);
memcpy(&rtspcld->host_addr,&name.sin_addr,sizeof(struct in_addr));
return 0;
}
char* rtspcl_local_ip(rtspcl_t *p)
{
rtspcl_data_t *rtspcld;
if(!p) return NULL;
rtspcld=(rtspcl_data_t *)p;
return inet_ntoa(rtspcld->local_addr);
}
int rtspcl_disconnect(rtspcl_t *p)
{
rtspcl_data_t *rtspcld;
if(!p) return -1;
rtspcld=(rtspcl_data_t *)p;
if(rtspcld->fd>0) close(rtspcld->fd);
rtspcld->fd=0;
return 0;
}
int rtspcl_annouce_sdp(rtspcl_t *p, char *sdp)
{
rtspcl_data_t *rtspcld;
if(!p) return -1;
rtspcld=(rtspcl_data_t *)p;
return exec_request(rtspcld, "ANNOUNCE", "application/sdp", sdp, 1, NULL, &rtspcld->kd);
}
int rtspcl_setup(rtspcl_t *p, key_data_t **kd)
{
key_data_t *rkd=NULL;
key_data_t hds[2];
const char delimiters[] = ";";
char *buf=NULL;
char *token,*pc;
int rval=-1;
rtspcl_data_t *rtspcld;
if(!p) return -1;
rtspcld=(rtspcl_data_t *)p;
hds[0].key=(u_int8_t*)"Transport";
hds[0].data=(u_int8_t*)"RTP/AVP/TCP;unicast;interleaved=0-1;mode=record";
hds[1].key=NULL;
if(exec_request(rtspcld, "SETUP", NULL, NULL, 1, hds, &rkd)) return -1;
if(!(rtspcld->session=kd_lookup(rkd, "Session"))){
ERRMSG("%s: no session in responce\n",__func__);
goto erexit;
}
if(!(rtspcld->transport=kd_lookup(rkd, "Transport"))){
ERRMSG("%s: no transport in responce\n",__func__);
goto erexit;
}
if(realloc_memory((void*)&buf,strlen(rtspcld->transport)+1,__func__)) goto erexit;
strcpy(buf,rtspcld->transport);
token=strtok(buf,delimiters);
rtspcld->server_port=0;
while(token){
if((pc=strstr(token,"="))){
*pc=0;
if(!strcmp(token,"server_port")){
rtspcld->server_port=atoi(pc+1);
break;
}
}
token=strtok(NULL,delimiters);
}
if(rtspcld->server_port==0){
ERRMSG("%s: no server_port in responce\n",__func__);
goto erexit;
}
rval=0;
erexit:
if(buf) free(buf);
if(rval) {
free_kd(rkd);
rkd=NULL;
}
*kd=rkd;
return rval;
}
int rtspcl_record(rtspcl_t *p)
{
key_data_t hds[3];
rtspcl_data_t *rtspcld;
if(!p) return -1;
rtspcld=(rtspcl_data_t *)p;
if(!rtspcld->session){
ERRMSG("%s: no session in progress\n",__func__);
return -1;
}
hds[0].key=(u_int8_t*)"Range";
hds[0].data=(u_int8_t*)"npt=0-";
hds[1].key=(u_int8_t*)"RTP-Info";
hds[1].data=(u_int8_t*)"seq=0;rtptime=0";
hds[2].key=NULL;
return exec_request(rtspcld,"RECORD",NULL,NULL,1,hds,&rtspcld->kd);
}
int rtspcl_set_parameter(rtspcl_t *p, char *para)
{
rtspcl_data_t *rtspcld;
if(!p) return -1;
rtspcld=(rtspcl_data_t *)p;
return exec_request(rtspcld, "SET_PARAMETER", "text/parameters", para, 1, NULL, &rtspcld->kd);
}
int rtspcl_flush(rtspcl_t *p)
{
key_data_t hds[2];
rtspcl_data_t *rtspcld;
if(!p) return -1;
rtspcld=(rtspcl_data_t *)p;
hds[0].key=(u_int8_t*)"RTP-Info";
hds[0].data=(u_int8_t*)"seq=0;rtptime=0";
hds[1].key=NULL;
return exec_request(rtspcld, "FLUSH", NULL, NULL, 1, hds, &rtspcld->kd);
}
int rtspcl_teardown(rtspcl_t *p)
{
rtspcl_data_t *rtspcld;
if(!p) return -1;
rtspcld=(rtspcl_data_t *)p;
return exec_request(rtspcld, "TEARDOWN", NULL, NULL, 0, NULL, &rtspcld->kd);
}
/*
* send RTSP request, and get responce if it's needed
* if this gets a success, *kd is allocated or reallocated (if *kd is not NULL)
*/
static int exec_request(rtspcl_data_t *rtspcld, char *cmd, char *content_type,
char *content, int get_response, key_data_t *hds, key_data_t **kd)
{
char line[1024];
char req[1024];
char reql[128];
const char delimiters[] = " ";
char *token,*dp;
int i,j,dsize,rval;
int timeout=5000; // msec unit
if(!rtspcld) return -1;
sprintf(req, "%s %s RTSP/1.0\r\nCSeq: %d\r\n",cmd,rtspcld->url,++rtspcld->cseq );
if( rtspcld->session != NULL ){
sprintf(reql,"Session: %s\r\n", rtspcld->session );
strncat(req,reql,sizeof(req));
}
i=0;
while( hds && hds[i].key != NULL ){
sprintf(reql,"%s: %s\r\n", hds[i].key, hds[i].data);
strncat(req,reql,sizeof(req));
i++;
}
if( content_type && content) {
sprintf(reql, "Content-Type: %s\r\nContent-Length: %d\r\n",
content_type, (int)strlen(content));
strncat(req,reql,sizeof(req));
}
sprintf(reql, "User-Agent: %s\r\n", rtspcld->useragent );
strncat(req,reql,sizeof(req));
i=0;
while(rtspcld->exthds && rtspcld->exthds[i].key){
if(rtspcld->exthds[i].key[0]==0xff) {i++;continue;}
sprintf(reql,"%s: %s\r\n", rtspcld->exthds[i].key, rtspcld->exthds[i].data);
strncat(req,reql,sizeof(req));
i++;
}
strncat(req,"\r\n",sizeof(req));
if( content_type && content)
strncat(req,content,sizeof(req));
rval=write(rtspcld->fd,req,strlen(req));
//DBGMSG("%s: write %d: %d \n",__func__, strlen(req),rval);
if( !get_response ) return 0;
if(read_line(rtspcld->fd,line,sizeof(line),timeout,0)<=0) {
ERRMSG("%s: request failed\n",__func__);
return -1;
}
token = strtok(line, delimiters);
token = strtok(NULL, delimiters);
if(token==NULL || strcmp(token,"200")){
ERRMSG("%s: request failed, error %s\n",__func__,token);
return -1;
}
i=0;
while(read_line(rtspcld->fd,line,sizeof(line),timeout,0)>0){
//DBGMSG("%s\n",line);
timeout=1000; // once it started, it shouldn't take a long time
if(i%16==0){
if(realloc_memory((void*)kd,(16*(i/16+1)+1)*sizeof(key_data_t),__func__)) return -1;
memset(*kd+16*(i/16),0,17*sizeof(key_data_t));
}
if(i && line[0]==' '){
for(j=0;j<strlen(line);j++) if(line[j]!=' ') break;
dsize+=strlen(line+j);
if(realloc_memory((void*)&(*kd)[i].data,dsize,__func__)) return -1;
strcat((char*)(*kd)[i].data,line+j);
continue;
}
dp=strstr(line,":");
if(!dp){
ERRMSG("%s: Request failed, bad header\n",__func__);
free_kd(*kd);
*kd=NULL;
return -1;
}
*dp=0;
if(realloc_memory((void*)&(*kd)[i].key,strlen(line)+1,__func__)) return -1;
strcpy((char*)(*kd)[i].key,line);
dsize=strlen(dp+1)+1;
if(realloc_memory((void*)&(*kd)[i].data,dsize,__func__)) return -1;
strcpy((char*)(*kd)[i].data,dp+1);
i++;
}
(*kd)[i].key=NULL;
return 0;
}