]> sjero.net Git - linphone/blob - linphone/oRTP/src/tests/mrtprecv.c
Merge branch 'master' of belledonne-communications.com:linphone-private
[linphone] / linphone / oRTP / src / tests / mrtprecv.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 <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <fcntl.h>
29
30 #ifndef _WIN32
31 #include <unistd.h>
32
33 #include <sys/types.h>
34 #include <sys/time.h>
35 #include <sys/stat.h>
36 #endif
37
38 int runcond=1;
39
40 void stophandler(int signum)
41 {
42         runcond=0;
43 }
44
45 static char *help="usage: mrtprecv      file_prefix local_port number_of_streams \n"
46                 "Receives multiples rtp streams on local_port+2*k, k={0..number_of_streams}\n";
47
48 #define STREAMS_COUNT 1000
49
50 /* malloc'd in order to detect buffer overflows with efence */
51 static uint8_t* recvbuf=0;
52
53 int rtp2disk(RtpSession *session,uint32_t ts, int fd)
54 {
55         int err,havemore=1;
56         while (havemore){
57                 err=rtp_session_recv_with_ts(session,recvbuf,160,ts,&havemore);
58                 if (havemore) printf("warning: havemore=%i!\n",havemore);
59                 if (err>0){
60                         rtp_session_set_data(session,(void*)1);
61                         /* to indicate that (for the application) the stream has started, so we can start
62                         recording on disk */
63                 }
64                 if (session->user_data != NULL) {
65                         size_t ret = write(fd,recvbuf,err);
66                         assert( ret == err );
67                 }
68         }
69         return 0;
70 }
71
72
73 int main(int argc, char *argv[])
74 {
75         RtpSession *session[STREAMS_COUNT];
76         int i;
77         int filefd[STREAMS_COUNT];
78         int port;
79         uint32_t user_ts=0;
80         int channels;
81         SessionSet *set;
82         char *filename;
83         
84         if (argc<4){
85                 printf("%s",help);
86                 return -1;
87         }
88         
89         channels=atoi(argv[3]);
90         if (channels==0){
91                 printf("%s",help);
92                 return -1;
93         }
94         
95         ortp_init();
96         ortp_scheduler_init();
97         
98         port=atoi(argv[2]);
99         recvbuf=ortp_malloc(160);
100         
101         for (i=0;i<channels;i++){
102
103                 session[i]=rtp_session_new(RTP_SESSION_RECVONLY);       
104                 rtp_session_set_scheduling_mode(session[i],1);
105                 rtp_session_set_blocking_mode(session[i],0);
106                 rtp_session_set_local_addr(session[i],"0.0.0.0",port);
107                 rtp_session_set_payload_type(session[i],0);
108                 rtp_session_enable_adaptive_jitter_compensation(session[i], TRUE);
109                 rtp_session_set_recv_buf_size(session[i],256);
110                 port+=2;
111         }
112                 
113         filename=ortp_malloc(strlen(argv[1])+15);
114         for (i=0;i<channels;i++){
115                 sprintf(filename,"%s%4.4d.dat",argv[1],i);
116                 #ifndef _WIN32
117                 filefd[i]=open(filename,O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
118                 #else
119                 filefd[i]=open(filename,_O_BINARY | O_WRONLY | O_CREAT | O_TRUNC);
120                 #endif
121                 if (filefd[i]<0) ortp_error("Could not open %s for writing: %s",filename,strerror(errno));
122         }
123         signal(SIGINT,stophandler);
124         /* create a set */
125         set=session_set_new();
126         while(runcond)
127         {
128                 int k;
129                 
130                 for (k=0;k<channels;k++){
131                         /* add the session to the set */
132                         session_set_set(set,session[k]);
133                         //printf("session_set_set %d\n", k);
134                 }
135                 /* and then suspend the process by selecting() */
136                 k=session_set_select(set,NULL,NULL);
137                 if (k==0) printf("warning: session_set_select() is returning 0...\n");
138                 for (k=0;k<channels;k++){
139                         if (session_set_is_set(set,session[k])){
140                                 rtp2disk(session[k],user_ts,filefd[k]);
141                                 //printf("session_set_is_set %d\n", k);
142                         } else {
143                                 //printf("warning: session %i is not set !\n",k);
144                         }
145                 }
146                 user_ts+=160;
147         }
148         printf("Exiting\n");
149         for (i=0;i<channels;i++){
150                 close(filefd[i]);
151                 rtp_session_destroy(session[i]);
152         }
153         session_set_destroy(set);
154         ortp_free(filename);
155         ortp_exit();
156         ortp_global_stats_display();
157         ortp_free(recvbuf);
158         return 0;
159 }