-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrtprecv.c
241 lines (213 loc) · 5.35 KB
/
rtprecv.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
/*
The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) stack..
Copyright (C) 2001 Simon MORLAT [email protected]
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <ortp/ortp.h>
#include <signal.h>
#include <stdlib.h>
#ifndef _WIN32
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#endif
#define SBUS
#ifdef SBUS
#include <ortp/port.h>
#include <ortp/payloadtype.h>
#include <time.h>
#define SBUS_FRAME_SIZE 25
#define SBUS_FIND_RETRY 10
#define SBUS_HZ 10000 /* 10KHz = 100us */
PayloadType payload_type_sbus={
PAYLOAD_OTHER, /*type */
SBUS_HZ,
0,
NULL,
0,
0,
"SBUS",
0,
0
};
ORTP_VAR_PUBLIC PayloadType payload_type_sbus;
struct timespec tsnew, tsold;
#endif /* SBUS */
int cond=1;
void stop_handler(int signum)
{
cond=0;
}
void ssrc_cb(RtpSession *session)
{
printf("hey, the ssrc has changed !\n");
}
static char *help="usage: rtprecv filename loc_port [--format format] [--soundcard] [--noadapt] [--with-jitter <milliseconds>]\n";
#define MULAW 0
#define ALAW 1
#if defined(__hpux) && HAVE_SYS_AUDIO_H
#include <sys/audio.h>
int sound_init(int format)
{
int fd;
fd=open("/dev/audio",O_WRONLY);
if (fd<0){
perror("Can't open /dev/audio");
return -1;
}
ioctl(fd,AUDIO_RESET,0);
ioctl(fd,AUDIO_SET_SAMPLE_RATE,8000);
ioctl(fd,AUDIO_SET_CHANNELS,1);
if (format==MULAW)
ioctl(fd,AUDIO_SET_DATA_FORMAT,AUDIO_FORMAT_ULAW);
else ioctl(fd,AUDIO_SET_DATA_FORMAT,AUDIO_FORMAT_ALAW);
return fd;
}
#else
int sound_init(int format)
{
return -1;
}
#endif
int main(int argc, char*argv[])
{
RtpSession *session;
#ifndef SBUS
unsigned char buffer[160];
#else
unsigned char buffer[SBUS_FRAME_SIZE];
#endif
int err;
uint32_t ts=0;
int stream_received=0;
FILE *outfile;
int local_port;
int have_more;
int i;
int format=0;
int soundcard=0;
int sound_fd=0;
int jittcomp=40;
bool_t adapt=TRUE;
#ifdef SBUS
RtpProfile prof;
#endif
/* init the lib */
if (argc<3){
printf("%s",help);
return -1;
}
local_port=atoi(argv[2]);
if (local_port<=0) {
printf("%s",help);
return -1;
}
for (i=3;i<argc;i++)
{
if (strcmp(argv[i],"--noadapt")==0) adapt=FALSE;
if (strcmp(argv[i],"--format")==0){
i++;
if (i<argc){
if (strcmp(argv[i],"mulaw")==0){
format=MULAW;
}else
if (strcmp(argv[i],"alaw")==0){
format=ALAW;
}else{
printf("Unsupported format %s\n",argv[i]);
return -1;
}
}
}
else if (strcmp(argv[i],"--soundcard")==0){
soundcard=1;
}
else if (strcmp(argv[i],"--with-jitter")==0){
i++;
if (i<argc){
jittcomp=atoi(argv[i]);
printf("Using a jitter buffer of %i milliseconds.\n",jittcomp);
}
}
}
outfile=fopen(argv[1],"wb");
if (outfile==NULL) {
perror("Cannot open file for writing");
return -1;
}
#ifdef SBUS
setvbuf(outfile, NULL, _IONBF, 0);
#endif
if (soundcard){
sound_fd=sound_init(format);
}
ortp_init();
ortp_scheduler_init();
ortp_set_log_level_mask(ORTP_DEBUG|ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR);
signal(SIGINT,stop_handler);
session=rtp_session_new(RTP_SESSION_RECVONLY);
rtp_session_set_scheduling_mode(session,1);
rtp_session_set_blocking_mode(session,1);
rtp_session_set_local_addr(session,"0.0.0.0",atoi(argv[2]),-1);
rtp_session_set_connected_mode(session,TRUE);
rtp_session_set_symmetric_rtp(session,TRUE);
rtp_session_enable_adaptive_jitter_compensation(session,adapt);
rtp_session_set_jitter_compensation(session,jittcomp);
#ifndef SBUS
rtp_session_set_payload_type(session,0);
#else
rtp_profile_clear_all(&prof);
//rtp_profile_set_name(&prof, "SBUS");
rtp_profile_set_payload(&prof, 71, &payload_type_sbus);
rtp_session_set_profile(session, &prof);
rtp_session_set_payload_type(session, 71);
#endif
rtp_session_signal_connect(session,"ssrc_changed",(RtpCallback)ssrc_cb,0);
rtp_session_signal_connect(session,"ssrc_changed",(RtpCallback)rtp_session_reset,0);
while(cond)
{
have_more=1;
while (have_more){
#ifndef SBUS
err=rtp_session_recv_with_ts(session,buffer,160,ts,&have_more);
#else
err=rtp_session_recv_with_ts(session,buffer,SBUS_FRAME_SIZE,ts,&have_more);
#endif
if (err>0) stream_received=1;
/* this is to avoid to write to disk some silence before the first RTP packet is returned*/
if ((stream_received) && (err>0)) {
#ifdef SBUS
clock_gettime(CLOCK_REALTIME, &tsnew);
printf("%09ld\n", tsnew.tv_nsec);
#endif
size_t ret = fwrite(buffer,1,err,outfile);
if (sound_fd>0){
ret = write(sound_fd,buffer,err);
if (ret==-1){
fprintf(stderr,"write to sound card failed (%s)",strerror(errno));
}
}
}
}
#ifndef SBUS
ts+=160;
#else
ts+=70;
#endif
//ortp_message("Receiving packet.");
}
rtp_session_destroy(session);
ortp_exit();
ortp_global_stats_display();
return 0;
}