]> sjero.net Git - linphone/blob - linphone/oRTP/src/tests/rtprecv.c
Merge branch 'master' of belledonne-communications.com:linphone-private
[linphone] / linphone / oRTP / src / tests / rtprecv.c
1  /*
2   The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) stack..
3   Copyright (C) 2001  Simon MORLAT simon.morlat@linphone.org
4
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9
10   This library 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 GNU
13   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20
21 #include <ortp/ortp.h>
22 #include <signal.h>
23 #include <stdlib.h>
24 #ifndef _WIN32
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <sys/types.h>
28 #endif
29
30 int cond=1;
31
32 void stop_handler(int signum)
33 {
34         cond=0;
35 }
36
37 void ssrc_cb(RtpSession *session)
38 {
39         printf("hey, the ssrc has changed !\n");
40 }
41
42 static char *help="usage: rtprecv  filename loc_port [--format format] [--soundcard] [--noadapt] [--with-jitter <milliseconds>]\n";
43
44 #define MULAW 0
45 #define ALAW 1
46
47 #if defined(__hpux) && HAVE_SYS_AUDIO_H
48
49 #include <sys/audio.h>
50
51 int sound_init(int format)
52 {
53         int fd;
54         fd=open("/dev/audio",O_WRONLY);
55         if (fd<0){
56                 perror("Can't open /dev/audio");
57                 return -1;
58         }
59         ioctl(fd,AUDIO_RESET,0);
60         ioctl(fd,AUDIO_SET_SAMPLE_RATE,8000);
61         ioctl(fd,AUDIO_SET_CHANNELS,1);
62         if (format==MULAW)
63                 ioctl(fd,AUDIO_SET_DATA_FORMAT,AUDIO_FORMAT_ULAW);
64         else ioctl(fd,AUDIO_SET_DATA_FORMAT,AUDIO_FORMAT_ALAW);
65         return fd;      
66 }
67 #else
68 int sound_init(int format)
69 {
70         return -1;
71 }
72 #endif
73
74 int main(int argc, char*argv[])
75 {
76         RtpSession *session;
77         unsigned char buffer[160];
78         int err;
79         uint32_t ts=0;
80         int stream_received=0;
81         FILE *outfile;
82         int local_port;
83         int have_more;
84         int i;
85         int format=0;
86         int soundcard=0;
87         int sound_fd=0;
88         int jittcomp=40;
89         bool_t adapt=TRUE;
90         
91         /* init the lib */
92         if (argc<3){
93                 printf("%s",help);
94                 return -1;
95         }
96         local_port=atoi(argv[2]);
97         if (local_port<=0) {
98                 printf("%s",help);
99                 return -1;
100         }
101         for (i=3;i<argc;i++)
102         {
103                 if (strcmp(argv[i],"--noadapt")==0) adapt=FALSE;
104                 if (strcmp(argv[i],"--format")==0){
105                         i++;
106                         if (i<argc){
107                                 if (strcmp(argv[i],"mulaw")==0){
108                                         format=MULAW;
109                                 }else
110                                 if (strcmp(argv[i],"alaw")==0){
111                                         format=ALAW;
112                                 }else{
113                                         printf("Unsupported format %s\n",argv[i]);
114                                         return -1;
115                                 }
116                         }
117                 }
118                 else if (strcmp(argv[i],"--soundcard")==0){
119                         soundcard=1;
120                 }
121                 else if (strcmp(argv[i],"--with-jitter")==0){
122                         i++;
123                         if (i<argc){
124                                 jittcomp=atoi(argv[i]);
125                                 printf("Using a jitter buffer of %i milliseconds.\n",jittcomp);
126                         }
127                 }
128         }
129         
130         outfile=fopen(argv[1],"wb");
131         if (outfile==NULL) {
132                 perror("Cannot open file for writing");
133                 return -1;
134         }
135         
136         
137         if (soundcard){
138                 sound_fd=sound_init(format);
139         }
140         
141         ortp_init();
142         ortp_scheduler_init();
143         ortp_set_log_level_mask(ORTP_DEBUG|ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR);
144         signal(SIGINT,stop_handler);
145         session=rtp_session_new(RTP_SESSION_RECVONLY);  
146         rtp_session_set_scheduling_mode(session,1);
147         rtp_session_set_blocking_mode(session,1);
148         rtp_session_set_local_addr(session,"0.0.0.0",atoi(argv[2]));
149         rtp_session_set_connected_mode(session,TRUE);
150         rtp_session_set_symmetric_rtp(session,TRUE);
151         rtp_session_enable_adaptive_jitter_compensation(session,adapt);
152         rtp_session_set_jitter_compensation(session,jittcomp);
153         rtp_session_set_payload_type(session,0);
154         rtp_session_signal_connect(session,"ssrc_changed",(RtpCallback)ssrc_cb,0);
155         rtp_session_signal_connect(session,"ssrc_changed",(RtpCallback)rtp_session_reset,0);
156         
157         while(cond)
158         {
159                 have_more=1;
160                 while (have_more){
161                         err=rtp_session_recv_with_ts(session,buffer,160,ts,&have_more);
162                         if (err>0) stream_received=1;
163                         /* this is to avoid to write to disk some silence before the first RTP packet is returned*/     
164                         if ((stream_received) && (err>0)) {
165                                 size_t ret = fwrite(buffer,1,err,outfile);
166                                 if (sound_fd>0)
167                                         ret = write(sound_fd,buffer,err);
168                         }
169                 }
170                 ts+=160;
171                 //ortp_message("Receiving packet.");
172         }
173         
174         rtp_session_destroy(session);
175         ortp_exit();
176         
177         ortp_global_stats_display();
178         
179         return 0;
180 }