]> sjero.net Git - linphone/blob - linphone/oRTP/src/tests/tevmrtprecv.c
023bec79851c3289f0a8a25b3e8696c74c50fc1f
[linphone] / linphone / oRTP / src / tests / tevmrtprecv.c
1  /*
2   The oRTP LinPhone RTP library intends to provide basics for a RTP 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 /* this program shows how to receive streams in paralel using the SessionSet api 
21         and two threads only. */
22
23 #include <ortp/ortp.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <assert.h>
27
28 #ifndef _WIN32
29 #include <signal.h>
30 #include <sys/types.h>
31 #include <sys/time.h>
32 #include <unistd.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35
36 #else
37 //#include <time.h>
38 #endif
39
40 #include <ortp/telephonyevents.h>
41
42 int runcond=1;
43
44 void stophandler(int signum)
45 {
46         runcond=0;
47 }
48
49 static int dtmf_tab[16]={'0','1','2','3','4','5','6','7','8','9','*','#','A','B','C','D'};
50
51 static int *p_channel_id;
52
53 int dtmf_count=0;
54
55 static char *help="usage: tevmrtprecv   file_prefix local_port number_of_streams \n"
56                 "Receives multiples rtp streams with telephone events on local_port+2*k, k={0..number_of_streams}\n";
57
58 #define STREAMS_COUNT 1000
59
60
61 void recv_tev_cb(RtpSession *session,int type,long user_data)
62 {
63         //printf("Receiving telephony event:%i\n",type);
64         if (type<16) printf("This is dtmf %c on channel %d\n",dtmf_tab[type],*(int *)user_data);
65         dtmf_count++;
66 }
67
68 int rtp2disk(RtpSession *session,uint32_t ts, int fd)
69 {
70         unsigned char buffer[160];
71         int err,havemore=1;
72         while (havemore){
73                 err=rtp_session_recv_with_ts(session,buffer,160,ts,&havemore);
74                 if (err>0){
75                         rtp_session_set_data(session,(void*)1);
76                         /* to indicate that (for the application) the stream has started, so we can start
77                         recording on disk */
78                 }
79                 if (session->user_data != NULL) {
80                         size_t ret = write(fd,buffer,err);
81                         assert( ret == err );
82                 }
83         }
84         return 0;
85 }
86
87
88 int main(int argc, char *argv[])
89 {
90         RtpSession *session[STREAMS_COUNT];
91         int i;
92         int filefd[STREAMS_COUNT];
93         int port;
94         uint32_t user_ts=0;
95         int channels;
96         SessionSet *set;
97         char *filename;
98
99         if (argc<4){
100                 printf("%s",help);
101                 return -1;
102         }
103         
104         channels=atoi(argv[3]);
105         if (channels==0){
106                 printf("%s",help);
107                 return -1;
108         }
109         
110         ortp_init();
111         ortp_scheduler_init();
112         
113         /* set the telephony event payload type to 96 in the av profile.*/
114         rtp_profile_set_payload(&av_profile,96,&payload_type_telephone_event);
115
116         port=atoi(argv[2]);
117         p_channel_id = (int *)ortp_malloc(channels*sizeof(int));
118         for (i=0;i<channels;i++){
119                 session[i]=rtp_session_new(RTP_SESSION_RECVONLY);       
120                 rtp_session_set_scheduling_mode(session[i],1);
121                 rtp_session_set_blocking_mode(session[i],0);
122
123                 rtp_session_set_local_addr(session[i],"0.0.0.0",port);
124                 rtp_session_set_recv_payload_type(session[i],0);
125                 rtp_session_set_recv_buf_size(session[i],256);
126
127                 p_channel_id[i] = i;
128                 /* register for telephony events */
129                 rtp_session_signal_connect(session[i],"telephone-event",(RtpCallback)recv_tev_cb,(long)&p_channel_id[i]);
130
131                 port+=2;
132         }
133                 
134         filename=ortp_malloc(strlen(argv[1])+8);
135         for (i=0;i<channels;i++){
136                 sprintf(filename,"%s%4.4d.dat",argv[1],i);
137                 #ifndef _WIN32
138                 filefd[i]=open(filename,O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
139                 #else
140                 filefd[i]=open(filename,_O_BINARY | O_WRONLY | O_CREAT | O_TRUNC);
141                 #endif
142                 if (filefd[i]<0) ortp_error("Could not open %s for writing: %s",filename,strerror(errno));
143         }
144         signal(SIGINT,stophandler);
145         /* create a set */
146         set=session_set_new();
147         while(runcond)
148         {
149                 int k;
150                 
151                 for (k=0;k<channels;k++){
152                         /* add the session to the set */
153                         session_set_set(set,session[k]);
154                         
155                 }
156                 /* and then suspend the process by selecting() */
157                 session_set_select(set,NULL,NULL);
158                 for (k=0;k<channels;k++){
159                         if (session_set_is_set(set,session[k])){
160                                 rtp2disk(session[k],user_ts,filefd[k]);
161                         }
162                 }
163                 user_ts+=160;
164         }
165         for (i=0;i<channels;i++){
166                 close(filefd[i]);
167                 rtp_session_destroy(session[i]);
168         }
169         session_set_destroy(set);
170         ortp_free(p_channel_id);
171         ortp_free(filename);
172         ortp_exit();
173         ortp_global_stats_display();
174         return 0;
175 }