]> sjero.net Git - linphone/blob - linphone/oRTP/src/tests/rtpmemtest.c
Merge branch 'master' of belledonne-communications.com:linphone-private
[linphone] / linphone / oRTP / src / tests / rtpmemtest.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 #else
36 //#include <time.h>
37 #endif
38
39 int runcond=1;
40
41 void stophandler(int signum)
42 {
43         runcond=0;
44 }
45
46 static char *help="usage: mrtprecv      file_prefix local_port number_of_streams \n"
47                 "Receives multiples rtp streams on local_port+2*k, k={0..number_of_streams}\n";
48
49 #define STREAMS_COUNT 1000
50
51 int rtp2disk(RtpSession *session,uint32_t ts, int fd)
52 {
53         unsigned char buffer[160];
54         int err,havemore=1;
55         while (havemore){
56                 err=rtp_session_recv_with_ts(session,buffer,160,ts,&havemore);
57                 if (err>0){
58                         rtp_session_set_data(session,(void*)1);
59                         /* to indicate that (for the application) the stream has started, so we can start
60                         recording on disk */
61                 }
62                 if (session->user_data != NULL) {
63                         size_t ret = write(fd,buffer,err);
64                         assert( ret == err );
65                 }
66         }
67         return 0;
68 }
69
70
71 int main(int argc, char *argv[])
72 {
73         RtpSession *session[STREAMS_COUNT];
74         int i;
75         int filefd[STREAMS_COUNT];
76         int port;
77         uint32_t user_ts=0;
78         int channels;
79         SessionSet *set;
80         char *filename;
81
82         argc=4;
83         argv[1]="/tmp/output";
84         argv[2]="8000";
85         argv[3]="100";
86
87         if (argc<4){
88                 printf("%s",help);
89                 return -1;
90         }
91
92         channels=atoi(argv[3]);
93         if (channels==0){
94                 printf("%s",help);
95                 return -1;
96         }
97
98         ortp_init();
99         ortp_scheduler_init();
100
101         port=atoi(argv[2]);
102         for (i=0;i<channels;i++){
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 #ifdef ORTP_INET6
107                 rtp_session_set_local_addr(session[i],"::",port);
108 #else
109                 rtp_session_set_local_addr(session[i],"0.0.0.0",port);
110 #endif
111                 rtp_session_set_payload_type(session[i],0);
112                 rtp_session_set_recv_buf_size(session[i],256);
113                 port+=2;
114         }
115
116         filename=ortp_malloc(strlen(argv[1])+8);
117         for (i=0;i<channels;i++){
118                 sprintf(filename,"%s%4.4d.dat",argv[1],i);
119                 filefd[i]=open(filename,O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
120                 if (filefd[i]<0) ortp_error("Could not open %s for writing: %s",filename,strerror(errno));
121         }
122         ortp_free(filename);
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[k]->setflags=%i\n",session[k]->setflags);
134                 }
135                 /* and then suspend the process by selecting() */
136                 session_set_select(set,NULL,NULL);
137                 for (k=0;k<channels;k++){
138                         if (session_set_is_set(set,session[k])){
139                                 rtp2disk(session[k],user_ts,filefd[k]);
140                         }
141                 }
142                 user_ts+=160;
143         }
144         for (i=0;i<channels;i++){
145                 close(filefd[i]);
146                 rtp_session_destroy(session[i]);
147         }
148         session_set_destroy(set);
149         ortp_global_stats_display();
150         ortp_exit();
151         return 0;
152 }