-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.cpp
More file actions
365 lines (300 loc) · 9.64 KB
/
main.cpp
File metadata and controls
365 lines (300 loc) · 9.64 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "main.h"
using namespace android;
int startRecording(char *config) {
ProcessState::self()->startThreadPool();
signal(SIGINT, sigIntHandler);
parseConfig(config);
setupInput();
adjustRotation();
printf("rotateView %d verticalInput %d rotation %d\n", rotateView, inputHeight > inputWidth ? 1 : 0, rotation);
fflush(stdout);
createOutputDir();
if (videoEncoder >= 0) {
if (useGl) {
output = new GLMediaRecorderOutput();
} else {
output = new CPUMediaRecorderOutput();
}
} else {
#ifdef SCR_FFMPEG
output = new FFmpegOutput();
#else
stop(199, "encoder not supported");
#endif
}
output->setupOutput();
shellSetState("RECORDING");
startTime = getTimeMs();
while (mrRunning && !finished) {
if (restrictFrameRate) {
waitForNextFrame();
}
frameCount++;
output->renderFrame();
}
int recordingTime = getTimeMs() - startTime;
float fps = -1.0f;
if (recordingTime > 0) {
fps = 1000.0f * frameCount / recordingTime;
}
printf("fps %f\n", fps);
fflush(stdout);
if (testMode) {
if (errorCode != 0) {
fps = 0.0f;
}
//TODO: use params instead of argv
//fprintf(stderr, "%ld, %4sx%s, %6s, %s, %2s, %4.1f\n", (long int)time(NULL), argv[1], argv[2], argv[3], argv[4], argv[5], fps);
fflush(stderr);
}
if (!stopping) {
stop(0, "finished");
}
fixFilePermissions();
ALOGV("main thread completed");
return errorCode;
}
void parseConfig(const char* config) {
if ((outputName = strchr(config, '/')) == NULL) {
stop(196, true, "no output name");
}
char mode[8];
char colorFormat[8];
int vertical;
int scanned = sscanf(config, "%d %c %d %d %d %d %d %7s %7s %d %d %d %d %d",
&rotation, &audioSource, &reqWidth, &reqHeight, &paddingWidth, &paddingHeight, &frameRate, mode,
colorFormat, &videoBitrate, &audioSamplingRate, &audioChannels, &videoEncoder, &vertical);
if (scanned != 14) {
stop(195, true, "params parse error");
}
if (frameRate == -1) {
restrictFrameRate = false;
frameRate = FRAME_RATE;
} else if (frameRate <= 0 || frameRate > 100) {
frameRate = FRAME_RATE;
}
initializeTransformation(mode);
if (colorFormat[0] == 'B') { //BGRA
useBGRA = true;
}
if (videoBitrate == 0) {
videoBitrate = 10000000;
}
if (audioSamplingRate == 0) {
audioSamplingRate = 16000;
}
allowVerticalFrames = (vertical > 0);
if (videoEncoder < 0) {
useOes = false;
}
ALOGI("SETTINGS rotation: %d, audioSource: %c, sampling: %d, channels: %d, resolution: %d x %d, padding: %d x %d, frameRate: %d, mode: %s, colorFix: %d, videoEncoder: %d, verticalFrames: %d",
rotation, audioSource, audioSamplingRate, audioChannels, reqWidth, reqHeight, paddingWidth, paddingHeight, frameRate, useGl ? "GPU" : "CPU", useBGRA, videoEncoder, allowVerticalFrames);
}
void initializeTransformation(char *transformation) {
if (strcmp(transformation, "CPU") == 0) {
useGl = false;
} else if (strcmp(transformation, "GPU") == 0) {
useGl = true;
} else if (strcmp(transformation, "OES") == 0) {
#if SCR_SDK_VERSION >= 18
useOes = true;
#endif
} else if (strcmp(transformation, "YUV_SP") == 0) {
useGl = false;
useYUV_SP = true;
} else if (strcmp(transformation, "YUV_P") == 0) {
useGl = false;
useYUV_P = true;
} else if (strcmp(transformation, "FB_CPU") == 0) {
useGl = false;
useFb = true;
} else if (strcmp(transformation, "FB_GPU") == 0) {
useGl = true;
useFb = true;
} else {
stop(196, "incorrect transformation");
}
}
void sigIntHandler(int param __unused) {
finished = true;
}
void stop(int error, const char* message) {
stop(error, true, message);
}
void stop(int error, bool fromMainThread, const char* message) {
if (error != 0) {
shellSetError(error);
}
//fprintf(stderr, "%d - stop requested from thread %s\n", error, getThreadName());
//fflush(stderr);
if (error == 0) {
ALOGV("%s - stopping\n", message);
} else {
ALOGE("%d - stopping\n", error);
}
if (stopping) {
if (errorCode == 0 && error != 0) {
errorCode = error;
}
ALOGV("Already stopping");
return;
}
stopping = true;
errorCode = error;
if (output != NULL) {
output->closeOutput(fromMainThread);
}
closeInput();
if (error == 201) {
debugWriteError();
}
if (fromMainThread) {
fixFilePermissions();
ALOGV("exiting main thread");
exit(errorCode);
}
}
// usually this shouldn't be needed but on some Huawei devices the output directory is not present
void createOutputDir() {
char path[1024];
struct stat st;
strncpy(path, outputName, 1024);
char *dir;
dir = dirname(path);
if (stat(dir, &st) != 0) {
ALOGE("Can't access output dir %s: %s", dir, strerror(errno));
if (mkdir(dir, 0777) < 0) {
ALOGE("Error creating output dir %s: %s", dir, strerror(errno));
}
}
}
void fixFilePermissions() {
// on SD Card this will be ignored and in other locations this will give read access for everyone (Gallery etc.)
if (chmod(outputName, 0664) < 0) {
ALOGW("can't change file mode %s (%s)", outputName, strerror(errno));
}
}
const char* getThreadName() {
pthread_t threadId = pthread_self();
if (pthread_equal(threadId, mainThread)) {
return "main";
}
if (pthread_equal(threadId, stoppingThread)) {
return "stopping";
}
return "other";
}
void waitForNextFrame() {
int64_t now = getTimeMs();
int64_t sleepTime = startTime + (frameCount * 1000l / frameRate) - now;
if (sleepTime > 0) {
usleep(sleepTime * 1000);
}
}
int64_t getTimeMs() {
timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return now.tv_sec * 1000l + now.tv_nsec / 1000000l;
}
bool fixOutputName() {
// replace /mnt/shell/emulated with /storage/emulated
// workaround for storage mapping bug on i_style_7_5
const char *prefixFrom = "/mnt/shell/emulated/";
const char *prefixTo = "/storage/emulated/";
char *newName = new char[strlen(outputName) - strlen(prefixFrom) + strlen(prefixTo) + 1];
if (strncmp(outputName, prefixFrom, strlen(prefixFrom)) != 0)
return false;
sprintf(newName, "%s%s", prefixTo, outputName + strlen("/mnt/shell/emulated/"));
outputName = newName;
ALOGW("Output changed to %s", outputName);
return true;
}
void logPathPermissions(const char *path) {
struct stat s;
if (lstat(path, &s) == 0) {
if (S_ISLNK(s.st_mode)) {
char linkPath[1024];
ssize_t linkSize = readlink(path, linkPath, 1024);
if (linkSize < 0) {
ALOGE("%03o %4ld %4ld %s => broken link", s.st_mode & 0777, s.st_uid, s.st_gid, path);
} else {
linkPath[linkSize] = '\0';
ALOGE("%03o %4ld %4ld %s => %s", s.st_mode & 0777, s.st_uid, s.st_gid, path, linkPath);
logPathPermissions(linkPath);
}
} else {
ALOGE("%03o %4ld %4ld %s", s.st_mode & 0777, s.st_uid, s.st_gid, path);
}
} else {
ALOGE("Can't lstat %s %s", path, strerror(errno));
}
}
void checkWritePermission(const char *path) {
logPathPermissions(path);
char testPath[1024];
sprintf(testPath, "%s/scr_test.txt", path);
int fd = open(testPath, O_RDWR | O_CREAT, 0744);
if (fd >= 0) {
ALOGE("Write success %s", testPath);
close(fd);
unlink(testPath);
} else {
ALOGE("Write FAILURE %s", testPath);
}
}
void checkChildrenWritePermission(const char *path) {
ALOGE("___________________________________________________________");
logPathPermissions(path);
DIR *d;
struct dirent *de;
d = opendir(path);
if (d == 0) {
ALOGE("Can't list %s %s", path, strerror(errno));
return;
}
char childPath[1024];
while ((de = readdir(d)) != 0) {
if (de->d_name[0] == '.')
continue;
sprintf(childPath, "%s/%s", path, de->d_name);
checkWritePermission(childPath);
}
closedir(d);
}
void logFile(const char *path) {
ALOGE("___________________________________________________________");
ALOGE("%s", path);
FILE *file;
file = fopen(path, "r");
if (file) {
char *line = NULL;
size_t len = 0;
while ((line = fgetln(file, &len)) != NULL) {
line[len - 1] = '\0'; // we expect \n at the end of file so this should be ok
ALOGE("%s", line);
}
fclose(file);
} else {
ALOGE("Error opening file: %s", strerror(errno));
}
ALOGE("___________________________________________________________");
}
void debugWriteError() {
ALOGE("Error writting to %s", outputName);
char path[1024];
strncpy(path, outputName, 1024);
char *dir = path;
while (true) {
dir = dirname(dir);
if (strlen(dir) <= 1)
break;
checkWritePermission(dir);
}
ALOGE("___________________________________________________________");
checkWritePermission("/sdcard");
checkChildrenWritePermission("/mnt/media_rw");
checkChildrenWritePermission("/mnt/shell/emulated");
checkChildrenWritePermission("/storage");
checkChildrenWritePermission("/storage/emulated");
logFile("/proc/self/mounts");
}