]> sjero.net Git - linphone/blob - linphone/oRTP/src/tests/win_receiver/RTPReceiver.cpp
Merge branch 'master' of belledonne-communications.com:linphone-private
[linphone] / linphone / oRTP / src / tests / win_receiver / RTPReceiver.cpp
1 #include <ortp/ortp.h>
2 #include <signal.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <fcntl.h>
6 #include <string.h>
7
8 #define STREAMS_COUNT 1000
9
10 BOOL                    m_bExit                         = FALSE;
11
12 static char *help="usage: mrtprecv      file_prefix local_port number_of_streams \n"
13                 "Receives multiples rtp streams on local_port+2*k, k={0..number_of_streams}\n";
14
15
16 void ProductVersion()
17 {
18         char    strBuffer[255];
19
20         printf("====================================\n");
21         printf("Author  : Simon Morlat             =\n");
22         printf("Porting : Yann STEPHAN             =\n");
23         printf("====================================\n");       
24         
25         memset(&strBuffer, 0x0, sizeof(strBuffer));
26
27         sprintf((char *) &strBuffer, "= RTPReceiver V1.0   - Date : %s - %s\n", __DATE__, __TIME__);
28         printf(strBuffer);      
29
30         printf("====================================\n");       
31 }       
32
33 BOOL ctrlHandlerFunction(DWORD fdwCtrlType) 
34
35         switch (fdwCtrlType) 
36         { 
37                 // Handle the CTRL+C signal. 
38                 // CTRL+CLOSE: confirm that the user wants to exit. 
39                 case CTRL_C_EVENT: 
40                 case CTRL_CLOSE_EVENT: 
41                 case CTRL_BREAK_EVENT: 
42                 case CTRL_LOGOFF_EVENT: 
43                 case CTRL_SHUTDOWN_EVENT: 
44                         m_bExit = TRUE;
45                         return TRUE; 
46
47                 default: 
48                         return FALSE; 
49         } 
50
51
52 int rtp2disk(RtpSession *session,uint32_t ts, FILE * fd)
53 {
54         char buffer[160];
55         int err,havemore=1;
56         
57         while (havemore)
58         {
59                 err=rtp_session_recv_with_ts(session,buffer,160,ts,&havemore);
60                 
61                 if (havemore) 
62                         printf("==> Warning: havemore=1!\n");
63                 
64                 if (err>0)
65                 {
66                         rtp_session_set_data(session,(void*)1);
67                         /* to indicate that (for the application) the stream has started, so we can start
68                         recording on disk */
69                 }
70
71                 if  (session->user_data != NULL)
72                 {
73                         fwrite(&buffer,1,160, fd);
74                 }
75         }
76         return 0;
77 }
78
79 int GetSystemInformation()
80 {
81         SYSTEM_INFO     SystemInfo;
82
83         GetSystemInfo(&SystemInfo);
84
85         return SystemInfo.dwNumberOfProcessors;
86 }
87
88 int __cdecl main(int argc, char *argv[])
89 {
90         RtpSession      *       session[STREAMS_COUNT];
91         FILE            *       filefd[STREAMS_COUNT];
92         SessionSet      *       set;
93
94         uint32_t                        user_ts                         = 0;
95
96         int                     port                            = 0;
97         int                     channels                        = 0;
98         int                     i                                       = 0;
99         int                     nCPUCount                       = 0;
100         int                     nSchedulerCPU           = 2;
101
102         char                    strFilename[MAX_PATH];
103         
104         ProductVersion();
105
106         if (argc<4)
107         {
108                 printf(help);
109                 return -1;
110         }
111         
112         channels=atoi(argv[3]);
113         if (channels==0){
114                 printf(help);
115                 return -1;
116         }
117         
118         // Now it's time to use the power of multiple CPUs
119         nCPUCount = GetSystemInformation();
120
121         printf("==> # of CPU detected : %d\n", nCPUCount);
122
123         ortp_init();
124         ortp_scheduler_init();
125         
126         if (nCPUCount > 1)
127         {
128                 if (nCPUCount > 2)
129                 {
130                         nSchedulerCPU   = 3;
131                 }
132
133 /*              if (ortp_bind_scheduler_to_cpu(nSchedulerCPU) != -1)
134                 {
135                         printf("==> Scheduler has been binded to CPU %d\n", nSchedulerCPU);
136                 }
137                 else
138                 {
139                         printf("==> Scheduler still binded to CPU 1\n");
140                 }
141 */
142         }
143
144         port=atoi(argv[2]);
145
146         for (i=0;i<channels;i++)
147         {
148                 session[i]=rtp_session_new(RTP_SESSION_RECVONLY);       
149                 rtp_session_set_scheduling_mode(session[i],1);
150                 rtp_session_set_blocking_mode(session[i],0);
151                 rtp_session_set_local_addr(session[i],"0.0.0.0",port);
152                 rtp_session_set_send_payload_type(session[i],0);
153                 rtp_session_enable_adaptive_jitter_compensation(session[i], TRUE);
154                 rtp_session_set_recv_buf_size(session[i],256);
155                 port+=2;
156         }
157                 
158         memset(strFilename, 0x0, sizeof(strFilename));
159
160         for (i=0;i<channels;i++)
161         {
162                 sprintf(strFilename,"%s%4.4d.dat",argv[1],i);
163
164                 filefd[i]=fopen(strFilename, "wb");
165
166                 if (filefd[i]<0) 
167                 {
168                         printf("Could not open %s for writing: %s",strFilename,strerror(errno));
169                 }
170         }
171
172         // =============== INSTALL THE CONTROL HANDLER ===============
173         if (SetConsoleCtrlHandler( (PHANDLER_ROUTINE) ctrlHandlerFunction, TRUE) == 0)
174         {
175                 printf("==> Cannot handle the CTRL-C...\n");
176         }
177
178         /* create a set */
179         set=session_set_new();
180         printf("==> RTP Receiver started\n");
181
182         while(m_bExit == FALSE)
183         {
184                 int k;
185                 
186                 for (k=0;k<channels;k++){
187                         /* add the session to the set */
188                         session_set_set(set,session[k]);
189                         //printf("session_set_set %d\n", k);
190                 }
191                 /* and then suspend the process by selecting() */
192                 k=session_set_select(set,NULL,NULL);
193                 //printf("session_set_select\n");
194                 if (k==0)
195                 {
196                         printf("==> Warning: session_set_select() is returning 0...\n");
197                 }
198
199                 for (k=0;k<channels;k++){
200                         if (session_set_is_set(set,session[k]))
201                         {
202                                 rtp2disk(session[k],user_ts,filefd[k]);
203                                 //printf("==> Session_set_is_set %d\n", k);
204                         } 
205                         else
206                         {
207                                 //printf("warning: session %i is not set !\n",k);
208                         }
209                 }
210                 user_ts+=160;
211         }
212
213         printf("==> Exiting\n");
214
215         for (i=0;i<channels;i++)
216         {
217                 fclose(filefd[i]);
218                 rtp_session_destroy(session[i]);
219         }
220         session_set_destroy(set);
221
222         ortp_exit();
223
224         ortp_global_stats_display();
225
226         printf("Waiting for exit : ");
227
228         for (i = 0; i < 4*5; i++)
229         {
230                 printf(".");
231                 Sleep(250);
232         }
233
234         return 0;
235 }
236