1 /*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <math.h>
15 #include "utility.h"
16
17 /* function for reading audio data from PCM file */
18 int
readframe(short * data,FILE * inp,int length)19 readframe(
20 short* data,
21 FILE* inp,
22 int length)
23 {
24 short k, rlen, status = 0;
25 unsigned char* ptrUChar;
26 ptrUChar = (unsigned char*)data;
27
28 rlen = (short)fread(data, sizeof(short), length, inp);
29 if (rlen < length) {
30 for (k = rlen; k < length; k++)
31 data[k] = 0;
32 status = 1;
33 }
34
35 // Assuming that our PCM files are written in Intel machines
36 for(k = 0; k < length; k++)
37 {
38 data[k] = (short)ptrUChar[k<<1] | ((((short)ptrUChar[(k<<1) + 1]) << 8) & 0xFF00);
39 }
40
41 return status;
42 }
43
44 short
readSwitch(int argc,char * argv[],char * strID)45 readSwitch(
46 int argc,
47 char* argv[],
48 char* strID)
49 {
50 short n;
51 for(n = 0; n < argc; n++)
52 {
53 if(strcmp(argv[n], strID) == 0)
54 {
55 return 1;
56 }
57 }
58 return 0;
59 }
60
61 double
readParamDouble(int argc,char * argv[],char * strID,double defaultVal)62 readParamDouble(
63 int argc,
64 char* argv[],
65 char* strID,
66 double defaultVal)
67 {
68 double returnVal = defaultVal;
69 short n;
70 for(n = 0; n < argc; n++)
71 {
72 if(strcmp(argv[n], strID) == 0)
73 {
74 n++;
75 if(n < argc)
76 {
77 returnVal = atof(argv[n]);
78 }
79 break;
80 }
81 }
82 return returnVal;
83 }
84
85 int
readParamInt(int argc,char * argv[],char * strID,int defaultVal)86 readParamInt(
87 int argc,
88 char* argv[],
89 char* strID,
90 int defaultVal)
91 {
92 int returnVal = defaultVal;
93 short n;
94 for(n = 0; n < argc; n++)
95 {
96 if(strcmp(argv[n], strID) == 0)
97 {
98 n++;
99 if(n < argc)
100 {
101 returnVal = atoi(argv[n]);
102 }
103 break;
104 }
105 }
106 return returnVal;
107 }
108
109 int
readParamString(int argc,char * argv[],char * strID,char * stringParam,int maxSize)110 readParamString(
111 int argc,
112 char* argv[],
113 char* strID,
114 char* stringParam,
115 int maxSize)
116 {
117 int paramLenght = 0;
118 short n;
119 for(n = 0; n < argc; n++)
120 {
121 if(strcmp(argv[n], strID) == 0)
122 {
123 n++;
124 if(n < argc)
125 {
126 strncpy(stringParam, argv[n], maxSize);
127 paramLenght = (int)strlen(argv[n]);
128 }
129 break;
130 }
131 }
132 return paramLenght;
133 }
134
135 void
get_arrival_time(int current_framesamples,size_t packet_size,int bottleneck,BottleNeckModel * BN_data,short senderSampFreqHz,short receiverSampFreqHz)136 get_arrival_time(
137 int current_framesamples, /* samples */
138 size_t packet_size, /* bytes */
139 int bottleneck, /* excluding headers; bits/s */
140 BottleNeckModel* BN_data,
141 short senderSampFreqHz,
142 short receiverSampFreqHz)
143 {
144 unsigned int travelTimeMs;
145 const int headerSizeByte = 35;
146
147 int headerRate;
148
149 BN_data->whenPackGeneratedMs += (current_framesamples / (senderSampFreqHz / 1000));
150
151 headerRate = headerSizeByte * 8 * senderSampFreqHz / current_framesamples; /* bits/s */
152
153 /* everything in samples */
154 BN_data->sample_count = BN_data->sample_count + current_framesamples;
155
156 //travelTimeMs = ((packet_size + HeaderSize) * 8 * sampFreqHz) /
157 // (bottleneck + HeaderRate)
158 travelTimeMs = (unsigned int)floor((double)((packet_size + headerSizeByte) * 8 * 1000)
159 / (double)(bottleneck + headerRate) + 0.5);
160
161 if(BN_data->whenPrevPackLeftMs > BN_data->whenPackGeneratedMs)
162 {
163 BN_data->whenPrevPackLeftMs += travelTimeMs;
164 }
165 else
166 {
167 BN_data->whenPrevPackLeftMs = BN_data->whenPackGeneratedMs +
168 travelTimeMs;
169 }
170
171 BN_data->arrival_time = (BN_data->whenPrevPackLeftMs *
172 (receiverSampFreqHz / 1000));
173
174 // if (BN_data->arrival_time < BN_data->sample_count)
175 // BN_data->arrival_time = BN_data->sample_count;
176
177 BN_data->rtp_number++;
178 }
179