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 #ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_MAIN_UTIL_UTILITY_H_
12 #define WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_MAIN_UTIL_UTILITY_H_
13 
14 #include <stdlib.h>
15 #include <stdio.h>
16 
17 #if defined(__cplusplus)
18 extern "C" {
19 #endif
20 
21 #define OPEN_FILE_WB(filePtr, fullPath)                         \
22   do                                                            \
23   {                                                             \
24     if(fullPath != NULL)                                        \
25     {                                                           \
26       filePtr = fopen(fullPath, "wb");                          \
27       if(filePtr == NULL)                                       \
28       {                                                         \
29         printf("could not open %s to write to.", fullPath);     \
30         return -1;                                              \
31       }                                                         \
32     }                                                           \
33     else                                                        \
34     {                                                           \
35       filePtr = NULL;                                           \
36     }                                                           \
37   }while(0)
38 
39 #define OPEN_FILE_AB(filePtr, fullPath)                         \
40   do                                                            \
41   {                                                             \
42     if(fullPath != NULL)                                        \
43     {                                                           \
44       filePtr = fopen(fullPath, "ab");                          \
45       if(filePtr == NULL)                                       \
46       {                                                         \
47         printf("could not open %s to write to.", fullPath);     \
48         return -1;                                              \
49       }                                                         \
50     }                                                           \
51     else                                                        \
52     {                                                           \
53       filePtr = NULL;                                           \
54     }                                                           \
55   }while(0)
56 
57 #define OPEN_FILE_RB(filePtr, fullPath)                         \
58   do                                                            \
59   {                                                             \
60     if(fullPath != NULL)                                        \
61     {                                                           \
62       filePtr = fopen(fullPath, "rb");                          \
63       if(filePtr == NULL)                                       \
64       {                                                         \
65         printf("could not open %s to read from.", fullPath);    \
66         return -1;                                              \
67       }                                                         \
68     }                                                           \
69     else                                                        \
70     {                                                           \
71       filePtr = NULL;                                           \
72     }                                                           \
73   }while(0)
74 
75 #define WRITE_FILE_D(bufferPtr, len, filePtr)           \
76   do                                                    \
77   {                                                     \
78     if(filePtr != NULL)                                 \
79     {                                                   \
80       double dummy[1000];                               \
81       int cntr;                                         \
82       for(cntr = 0; cntr < (len); cntr++)               \
83       {                                                 \
84         dummy[cntr] = (double)bufferPtr[cntr];          \
85       }                                                 \
86       fwrite(dummy, sizeof(double), len, filePtr);      \
87       fflush(filePtr);                                  \
88     }                                                   \
89   } while(0)
90 
91   typedef struct {
92     unsigned int whenPackGeneratedMs;
93     unsigned int whenPrevPackLeftMs;
94     unsigned int sendTimeMs ;          /* milisecond */
95     unsigned int arrival_time;         /* samples */
96     unsigned int sample_count;         /* samples, also used as "send time stamp" */
97     unsigned int rtp_number;
98   } BottleNeckModel;
99 
100   void get_arrival_time(
101       int              current_framesamples,   /* samples */
102       size_t           packet_size,            /* bytes */
103       int              bottleneck,             /* excluding headers; bits/s */
104       BottleNeckModel* BN_data,
105       short            senderSampFreqHz,
106       short            receiverSampFreqHz);
107 
108   /* function for reading audio data from PCM file */
109   int readframe(
110       short* data,
111       FILE*  inp,
112       int    length);
113 
114   short readSwitch(
115       int   argc,
116       char* argv[],
117       char* strID);
118 
119   double readParamDouble(
120       int    argc,
121       char*  argv[],
122       char*  strID,
123       double defaultVal);
124 
125   int readParamInt(
126       int   argc,
127       char* argv[],
128       char* strID,
129       int   defaultVal);
130 
131   int readParamString(
132       int   argc,
133       char* argv[],
134       char* strID,
135       char* stringParam,
136       int   maxSize);
137 
138 #if defined(__cplusplus)
139 }
140 #endif
141 
142 
143 
144 #endif
145