forked from meetecho/janus-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
210 lines (196 loc) · 5.09 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*! \file utils.c
* \author Lorenzo Miniero <[email protected]>
* \copyright GNU General Public License v3
* \brief Utilities and helpers
* \details Implementations of a few methods that may be of use here
* and there in the code.
*
* \ingroup core
* \ref core
*/
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include "utils.h"
#include "debug.h"
gint64 janus_get_monotonic_time(void) {
struct timespec ts;
clock_gettime (CLOCK_MONOTONIC, &ts);
return (ts.tv_sec*G_GINT64_CONSTANT(1000000)) + (ts.tv_nsec/G_GINT64_CONSTANT(1000));
}
gboolean janus_is_true(const char *value) {
return value && (!strcasecmp(value, "yes") || !strcasecmp(value, "true") || !strcasecmp(value, "1"));
}
void janus_flags_reset(janus_flags *flags) {
if(flags != NULL)
*flags = 0;
}
void janus_flags_set(janus_flags *flags, uint32_t flag) {
if(flags != NULL) {
*flags |= flag;
}
}
void janus_flags_clear(janus_flags *flags, uint32_t flag) {
if(flags != NULL) {
*flags &= ~(flag);
}
}
gboolean janus_flags_is_set(janus_flags *flags, uint32_t flag) {
if(flags != NULL) {
uint32_t bit = *flags & flag;
return (bit != 0);
}
return FALSE;
}
/* Easy way to replace multiple occurrences of a string with another: ALWAYS creates a NEW string */
char *janus_string_replace(char *message, const char *old_string, const char *new_string)
{
if(!message || !old_string || !new_string)
return NULL;
if(!strstr(message, old_string)) { /* Nothing to be done (old_string is not there) */
return message;
}
if(!strcmp(old_string, new_string)) { /* Nothing to be done (old_string=new_string) */
return message;
}
if(strlen(old_string) == strlen(new_string)) { /* Just overwrite */
char *outgoing = message;
char *pos = strstr(outgoing, old_string), *tmp = NULL;
int i = 0;
while(pos) {
i++;
memcpy(pos, new_string, strlen(new_string));
pos += strlen(old_string);
tmp = strstr(pos, old_string);
pos = tmp;
}
return outgoing;
} else { /* We need to resize */
char *outgoing = strdup(message);
free(message);
if(outgoing == NULL) {
return NULL;
}
int diff = strlen(new_string) - strlen(old_string);
/* Count occurrences */
int counter = 0;
char *pos = strstr(outgoing, old_string), *tmp = NULL;
while(pos) {
counter++;
pos += strlen(old_string);
tmp = strstr(pos, old_string);
pos = tmp;
}
uint16_t old_stringlen = strlen(outgoing)+1, new_stringlen = old_stringlen + diff*counter;
if(diff > 0) { /* Resize now */
tmp = realloc(outgoing, new_stringlen);
if(!tmp) {
free(outgoing);
return NULL;
}
outgoing = tmp;
}
/* Replace string */
pos = strstr(outgoing, old_string);
while(pos) {
if(diff > 0) { /* Move to the right (new_string is larger than old_string) */
uint16_t len = strlen(pos)+1;
memmove(pos + diff, pos, len);
memcpy(pos, new_string, strlen(new_string));
pos += strlen(new_string);
tmp = strstr(pos, old_string);
} else { /* Move to the left (new_string is smaller than old_string) */
uint16_t len = strlen(pos - diff)+1;
memmove(pos, pos - diff, len);
memcpy(pos, new_string, strlen(new_string));
pos += strlen(old_string);
tmp = strstr(pos, old_string);
}
pos = tmp;
}
if(diff < 0) { /* We skipped the resize previously (shrinking memory) */
tmp = realloc(outgoing, new_stringlen);
if(!tmp) {
free(outgoing);
return NULL;
}
outgoing = tmp;
}
outgoing[strlen(outgoing)] = '\0';
return outgoing;
}
}
int janus_mkdir(const char *dir, mode_t mode) {
char tmp[256];
char *p = NULL;
size_t len;
int res = 0;
g_snprintf(tmp, sizeof(tmp), "%s", dir);
len = strlen(tmp);
if(tmp[len - 1] == '/')
tmp[len - 1] = 0;
for(p = tmp + 1; *p; p++) {
if(*p == '/') {
*p = 0;
res = mkdir(tmp, mode);
if(res != 0 && errno != EEXIST) {
JANUS_LOG(LOG_ERR, "Error creating folder %s\n", tmp);
return res;
}
*p = '/';
}
}
res = mkdir(tmp, mode);
if(res != 0 && errno != EEXIST)
return res;
return 0;
}
int janus_get_opus_pt(const char *sdp) {
if(!sdp)
return -1;
if(!strstr(sdp, "m=audio") || !strstr(sdp, "opus/48000")) /* FIXME Should be case insensitive */
return -1;
const char *line = strstr(sdp, "m=audio");
while(line) {
char *next = strchr(line, '\n');
if(next) {
*next = '\0';
if(strstr(line, "a=rtpmap") && strstr(line, "opus/48000")) {
/* Gotcha! */
int pt = 0;
if(sscanf(line, "a=rtpmap:%d opus/48000/2", &pt) == 1) {
*next = '\n';
return pt;
}
}
*next = '\n';
}
line = next ? (next+1) : NULL;
}
return -1;
}
int janus_get_vp8_pt(const char *sdp) {
if(!sdp)
return -1;
if(!strstr(sdp, "m=video") || !strstr(sdp, "VP8/90000")) /* FIXME Should be case insensitive */
return -1;
const char *line = strstr(sdp, "m=video");
while(line) {
char *next = strchr(line, '\n');
if(next) {
*next = '\0';
if(strstr(line, "a=rtpmap") && strstr(line, "VP8/90000")) {
/* Gotcha! */
int pt = 0;
if(sscanf(line, "a=rtpmap:%d VP8/90000", &pt) == 1) {
*next = '\n';
return pt;
}
}
*next = '\n';
}
line = next ? (next+1) : NULL;
}
return -1;
}