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
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include "ffmpeg.h"
FFMPEG* ffmpegStart(const char* filename, uint32_t width, uint32_t height,
uint32_t fps)
{
int pipefd[2];
if (pipe(pipefd) < 0)
{
TraceLog(LOG_ERROR, "FFMPEG: Could not create a pipe: %s",
strerror(errno));
return NULL;
}
pid_t child = fork();
if (child < 0)
{
TraceLog(LOG_ERROR, "FFMPEG: could not fork a child: %s",
strerror(errno));
return NULL;
}
if (child == 0)
{
if (dup2(pipefd[READ_END], STDIN_FILENO) < 0)
{
TraceLog(
LOG_ERROR,
"FFMPEG CHILD: could not reopen read end of pipe as stdin: %s",
strerror(errno));
exit(1);
}
close(pipefd[WRITE_END]);
char resolution[64];
snprintf(resolution, sizeof(resolution), "%zux%zu", (size_t)width,
(size_t)height);
char framerate[64];
snprintf(framerate, sizeof(framerate), "%zu", (size_t)fps);
int ret = execlp("ffmpeg",
"ffmpeg",
"-loglevel", "verbose",
"-y",
"-f", "rawvideo",
"-pix_fmt", "rgba",
"-s", resolution,
"-r", framerate,
"-i", "-",
"-c:v", "libx264",
"-vb", "2500k",
"-c:a", "aac",
"-ab", "200k",
"-pix_fmt", "yuv420p",
filename,
NULL
);
if (ret < 0)
{
TraceLog(
LOG_ERROR,
"FFMPEG CHILD: could not run ffmpeg as a child process: %s",
strerror(errno));
exit(1);
}
assert(0 && "unreachable");
exit(1);
}
if (close(pipefd[READ_END]) < 0)
{
TraceLog(
LOG_WARNING,
"FFMPEG: could not close read end of the pipe on the parent's end:%s",
strerror(errno));
}
FFMPEG* ffmpeg = malloc(sizeof(FFMPEG));
assert(ffmpeg != NULL && "Buy MORE RAM lol!!");
ffmpeg->pid = child;
ffmpeg->pipe = pipefd[WRITE_END];
return ffmpeg;
}
int ffmpegSendFrame(FFMPEG* ffmpeg, void* data, uint32_t width,
uint32_t height)
{
for (uint32_t y = height; y > 0; --y)
{
if (write(ffmpeg->pipe, (uint32_t*)data + (y - 1) * width,
sizeof(uint32_t) * width) < 0)
{
TraceLog(
LOG_ERROR,
"FFMPEG: failed to write into ffmpeg pipe: %s",
strerror(errno));
return -1;
}
}
return 0;
}
int ffmpegEnd(FFMPEG* ffmpeg, bool cancel)
{
int pipe = ffmpeg->pipe;
pid_t pid = ffmpeg->pid;
free(ffmpeg);
if (close(pipe) < 0)
{
TraceLog(
LOG_WARNING,
"FFMPEG: could not close write end of the pipe on the parent's end:%s",
strerror(errno));
}
if (cancel)
{
kill(pid, SIGKILL);
}
while (true)
{
int wstatus = 0;
if (waitpid(pid, &wstatus, 0) < 0)
{
TraceLog(
LOG_ERROR,
"FFMPEG: could not wait for ffmpeg child process to finish:%s",
strerror(errno));
return -1;
}
if (WIFEXITED(wstatus))
{
int exit_status = WEXITSTATUS(wstatus);
if (exit_status != 0)
{
TraceLog(
LOG_ERROR,
"FFMPEG: ffmpeg exited with code %d",
exit_status);
return -1;
}
return 0;
}
if (WIFSIGNALED(wstatus))
{
TraceLog(
LOG_ERROR,
"FFMPEG: ffmpeg got terminated by %s",
strsignal(WTERMSIG(wstatus)));
return -1;
}
}
assert(0 && "unreachable");
return 0;
}
|