Debian Codegraph @

prboom2/src/i_capture.c

raw
1 /* Emacs style mode select -*- C -*-
2 *-----------------------------------------------------------------------------
3 *
4 *
5 * PrBoom: a Doom port merged with LxDoom and LSDLDoom
6 * based on BOOM, a modified and improved DOOM engine
7 *
8 * Copyright (C) 2011 by
9 * Nicholai Main
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 * 02111-1307, USA.
25 *
26 * DESCRIPTION:
27 *
28 *---------------------------------------------------------------------
29 */
30
31 #include "SDL.h"
32 #include "SDL_thread.h"
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include "i_sound.h"
37 #include "i_video.h"
38 #include "lprintf.h"
39 #include "m_file.h"
40 #include "i_system.h"
41 #include "i_capture.h"
42
43 #include "dsda/configuration.h"
44
45 int capturing_video = 0;
46 static const char *vid_fname;
47
48 typedef struct
49 { // information on a running pipe
50 char command[PATH_MAX];
51 FILE *f_stdin;
52 FILE *f_stdout;
53 FILE *f_stderr;
54 SDL_Thread *outthread;
55 const char *stdoutdumpname;
56 SDL_Thread *errthread;
57 const char *stderrdumpname;
58 void *user;
59 } pipeinfo_t;
60
61 static pipeinfo_t soundpipe;
62 static pipeinfo_t videopipe;
63 static pipeinfo_t muxpipe;
64
65 int cap_fps;
66 int cap_frac;
67 int cap_wipescreen;
68
69 // parses a command with simple printf-style replacements.
70
71 // %w video width (px)
72 // %h video height (px)
73 // %s sound rate (hz)
74 // %f filename passed to -viddump
75 // %% single percent sign
76 // TODO: add aspect ratio information
77 //
78 // Modified to work with SDL2 resizeable window and fullscreen desktop - DTIED
79 static int parsecommand (char *out, const char *in, int len)
80 {
81 int i;
82
83 while (*in && len > 1)
84 {
85 if (*in == '%')
86 {
87 I_UpdateRenderSize(); // Handle potential resolution scaling - DTIED
88 switch (in[1])
89 {
90 case 'w':
91 i = snprintf (out, len, "%u", renderW);
92 break;
93 case 'h':
94 i = snprintf (out, len, "%u", renderH);
95 break;
96 case 's':
97 i = snprintf (out, len, "%u", snd_samplerate);
98 break;
99 case 'f':
100 i = snprintf (out, len, "%s", vid_fname);
101 break;
102 case 'r':
103 i = snprintf (out, len, "%u", cap_fps);
104 break;
105 case '%':
106 i = snprintf (out, len, "%%");
107 break;
108 default:
109 return 0;
110 }
111 out += i;
112 len -= i;
113 in += 2;
114 }
115 else
116 {
117 *out++ = *in++;
118 len--;
119 }
120 }
121 if (*in || len < 1)
122 { // out of space
123 return 0;
124 }
125 *out = 0;
126 return 1;
127 }
128
129
130
131
132 // popen3() implementation -
133 // starts a child process
134
135 // user is a pointer to implementation defined extra data
136 static int my_popen3 (pipeinfo_t *p); // 1 on success
137 // close waits on process
138 static void my_pclose3 (pipeinfo_t *p);
139
140
141 #ifdef _WIN32
142 // direct winapi implementation
143 #ifndef WIN32_LEAN_AND_MEAN
144 #define WIN32_LEAN_AND_MEAN 1
145 #endif
146 #include <windows.h>
147 #include <io.h>
148
149 typedef struct
150 {
151 HANDLE proc;
152 HANDLE thread;
153 } puser_t;
154
155 // extra pointer is used to hold process id to wait on to close
156 // NB: stdin is opened as "wb", stdout, stderr as "r"
157 static int my_popen3 (pipeinfo_t *p)
158 {
159 FILE *fin = NULL;
160 FILE *fout = NULL;
161 FILE *ferr = NULL;
162 HANDLE child_hin = INVALID_HANDLE_VALUE;
163 HANDLE child_hout = INVALID_HANDLE_VALUE;
164 HANDLE child_herr = INVALID_HANDLE_VALUE;
165 HANDLE parent_hin = INVALID_HANDLE_VALUE;
166 HANDLE parent_hout = INVALID_HANDLE_VALUE;
167 HANDLE parent_herr = INVALID_HANDLE_VALUE;
168
169
170 puser_t *puser = NULL;
171
172
173 PROCESS_INFORMATION piProcInfo;
174 STARTUPINFO siStartInfo;
175 SECURITY_ATTRIBUTES sa;
176
177 puser = Z_Malloc (sizeof (puser_t));
178 if (!puser)
179 return 0;
180
181 puser->proc = INVALID_HANDLE_VALUE;
182 puser->thread = INVALID_HANDLE_VALUE;
183
184
185 // make the pipes
186
187 sa.nLength = sizeof (sa);
188 sa.bInheritHandle = 1;
189 sa.lpSecurityDescriptor = NULL;
190 if (!CreatePipe (&child_hin, &parent_hin, &sa, 0))
191 goto fail;
192 if (!CreatePipe (&parent_hout, &child_hout, &sa, 0))
193 goto fail;
194 if (!CreatePipe (&parent_herr, &child_herr, &sa, 0))
195 goto fail;
196
197
198 // very important
199 if (!SetHandleInformation (parent_hin, HANDLE_FLAG_INHERIT, 0))
200 goto fail;
201 if (!SetHandleInformation (parent_hout, HANDLE_FLAG_INHERIT, 0))
202 goto fail;
203 if (!SetHandleInformation (parent_herr, HANDLE_FLAG_INHERIT, 0))
204 goto fail;
205
206
207
208
209 // start the child process
210
211 ZeroMemory (&siStartInfo, sizeof (STARTUPINFO));
212 siStartInfo.cb = sizeof (STARTUPINFO);
213 siStartInfo.hStdInput = child_hin;
214 siStartInfo.hStdOutput = child_hout;
215 siStartInfo.hStdError = child_herr;
216 siStartInfo.dwFlags = STARTF_USESTDHANDLES;
217
218 if (!CreateProcess(NULL,// application name
219 (LPTSTR)p->command,// command line
220 NULL, // process security attributes
221 NULL, // primary thread security attributes
222 TRUE, // handles are inherited
223 DETACHED_PROCESS, // creation flags
224 NULL, // use parent's environment
225 NULL, // use parent's current directory
226 &siStartInfo, // STARTUPINFO pointer
227 &piProcInfo)) // receives PROCESS_INFORMATION
228 {
229 goto fail;
230 }
231
232
233
234 puser->proc = piProcInfo.hProcess;
235 puser->thread = piProcInfo.hThread;
236
237
238 if (NULL == (fin = _fdopen (_open_osfhandle ((intptr_t) parent_hin, 0), "wb")))
239 goto fail;
240 if (NULL == (fout = _fdopen (_open_osfhandle ((intptr_t) parent_hout, 0), "r")))
241 goto fail;
242 if (NULL == (ferr = _fdopen (_open_osfhandle ((intptr_t) parent_herr, 0), "r")))
243 goto fail;
244 // after fdopen(osf()), we don't need to keep track of parent handles anymore
245 // fclose on the FILE struct will automatically free them
246
247
248 p->user = puser;
249 p->f_stdin = fin;
250 p->f_stdout = fout;
251 p->f_stderr = ferr;
252
253 CloseHandle (child_hin);
254 CloseHandle (child_hout);
255 CloseHandle (child_herr);
256
257 return 1;
258
259 fail:
260 if (fin)
261 fclose (fin);
262 if (fout)
263 fclose (fout);
264 if (ferr)
265 fclose (ferr);
266
267 if (puser->proc)
268 CloseHandle (puser->proc);
269 if (puser->thread)
270 CloseHandle (puser->thread);
271
272 if (child_hin != INVALID_HANDLE_VALUE)
273 CloseHandle (child_hin);
274 if (child_hout != INVALID_HANDLE_VALUE)
275 CloseHandle (child_hout);
276 if (child_herr != INVALID_HANDLE_VALUE)
277 CloseHandle (child_herr);
278 if (parent_hin != INVALID_HANDLE_VALUE)
279 CloseHandle (parent_hin);
280 if (parent_hout != INVALID_HANDLE_VALUE)
281 CloseHandle (parent_hout);
282 if (parent_herr != INVALID_HANDLE_VALUE)
283 CloseHandle (parent_herr);
284
285 Z_Free (puser);
286
287 return 0;
288
289
290 }
291
292 static void my_pclose3 (pipeinfo_t *p)
293 {
294 puser_t *puser = (puser_t *) p->user;
295
296 if (!p->f_stdin || !p->f_stdout || !p->f_stderr || !puser)
297 return;
298
299 fclose (p->f_stdin);
300 //fclose (p->f_stdout); // these are closed elsewhere
301 //fclose (p->f_stderr);
302
303 WaitForSingleObject (puser->proc, INFINITE);
304
305 CloseHandle (puser->proc);
306 CloseHandle (puser->thread);
307 Z_Free (puser);
308 }
309
310 #else // _WIN32
311 // posix implementation
312 // not tested
313 #include <unistd.h>
314 #include <sys/types.h>
315 #include <sys/wait.h>
316
317
318 typedef struct
319 {
320 int pid;
321 } puser_t;
322
323
324 static int my_popen3 (pipeinfo_t *p)
325 {
326 FILE *fin = NULL;
327 FILE *fout = NULL;
328 FILE *ferr = NULL;
329 int child_hin = -1;
330 int child_hout = -1;
331 int child_herr = -1;
332 int parent_hin = -1;
333 int parent_hout = -1;
334 int parent_herr = -1;
335
336 int scratch[2];
337
338 int pid;
339
340 puser_t *puser = NULL;
341
342 puser = Z_Malloc (sizeof (puser_t));
343 if (!puser)
344 return 0;
345
346
347
348 // make the pipes
349 if (pipe (scratch))
350 goto fail;
351 child_hin = scratch[0];
352 parent_hin = scratch[1];
353 if (pipe (scratch))
354 goto fail;
355 parent_hout = scratch[0];
356 child_hout = scratch[1];
357 if (pipe (scratch))
358 goto fail;
359 parent_herr = scratch[0];
360 child_herr = scratch[1];
361
362 pid = fork ();
363
364 if (pid == -1)
365 goto fail;
366 if (pid == 0)
367 {
368 dup2 (child_hin, STDIN_FILENO);
369 dup2 (child_hout, STDOUT_FILENO);
370 dup2 (child_herr, STDERR_FILENO);
371
372 close (parent_hin);
373 close (parent_hout);
374 close (parent_herr);
375
376 // does this work? otherwise we have to parse cmd into an **argv style array
377 execl ("/bin/sh", "sh", "-c", p->command, NULL);
378 // exit forked process if command failed
379 _exit (0);
380 }
381
382 if (NULL == (fin = fdopen (parent_hin, "wb")))
383 goto fail;
384 if (NULL == (fout = fdopen (parent_hout, "r")))
385 goto fail;
386 if (NULL == (ferr = fdopen (parent_herr, "r")))
387 goto fail;
388
389 close (child_hin);
390 close (child_hout);
391 close (child_herr);
392
393 puser->pid = pid;
394
395 p->user = puser;
396 p->f_stdin = fin;
397 p->f_stdout = fout;
398 p->f_stderr = ferr;
399 return 1;
400
401 fail:
402 if (fin)
403 fclose (fin);
404 if (fout)
405 fclose (fout);
406 if (ferr)
407 fclose (ferr);
408
409 close (parent_hin);
410 close (parent_hout);
411 close (parent_herr);
412 close (child_hin);
413 close (child_hout);
414 close (child_herr);
415
416 Z_Free (puser);
417 return 0;
418
419 }
420
421
422 static void my_pclose3 (pipeinfo_t *p)
423 {
424 puser_t *puser = (puser_t *) p->user;
425
426 int s;
427
428 if (!p->f_stdin || !p->f_stdout || !p->f_stderr || !puser)
429 return;
430
431 fclose (p->f_stdin);
432 //fclose (p->f_stdout); // these are closed elsewhere
433 //fclose (p->f_stderr);
434
435 waitpid (puser->pid, &s, 0);
436
437 Z_Free (puser);
438 }
439
440
441 #endif // _WIN32
442
443 typedef struct
444 {
445 FILE *fin;
446 const char *fn;
447 } threaddata_t;
448
449
450 static int threadstdoutproc (void *data)
451 { // simple thread proc dumps stdout
452 // not terribly fast
453 int c;
454
455 pipeinfo_t *p = (pipeinfo_t *) data;
456
457 FILE *f = M_OpenFile(p->stdoutdumpname, "w");
458
459 if (!f || !p->f_stdout)
460 return 0;
461
462 while ((c = fgetc (p->f_stdout)) != EOF)
463 fputc (c, f);
464
465 fclose (f);
466 fclose (p->f_stdout);
467 return 1;
468 }
469
470 static int threadstderrproc (void *data)
471 { // simple thread proc dumps stderr
472 // not terribly fast
473 int c;
474
475 pipeinfo_t *p = (pipeinfo_t *) data;
476
477 FILE *f = M_OpenFile(p->stderrdumpname, "w");
478
479 if (!f || !p->f_stderr)
480 return 0;
481
482 while ((c = fgetc (p->f_stderr)) != EOF)
483 fputc (c, f);
484
485 fclose (f);
486 fclose (p->f_stderr);
487 return 1;
488 }
489
490
491 // init and open sound, video pipes
492 // fn is filename passed from command line, typically final output file
493 void I_CapturePrep (const char *fn)
494 {
495 const char* cap_soundcommand;
496 const char* cap_videocommand;
497 const char* cap_muxcommand;
498
499 cap_soundcommand = dsda_StringConfig(dsda_config_cap_soundcommand);
500 cap_videocommand = dsda_StringConfig(dsda_config_cap_videocommand);
501 cap_muxcommand = dsda_StringConfig(dsda_config_cap_muxcommand);
502 cap_wipescreen = dsda_IntConfig(dsda_config_cap_wipescreen);
503 cap_fps = dsda_IntConfig(dsda_config_cap_fps);
504
505 vid_fname = fn;
506
507 if (!parsecommand (soundpipe.command, cap_soundcommand, sizeof(soundpipe.command)))
508 {
509 lprintf (LO_ERROR, "I_CapturePrep: malformed command %s\n", cap_soundcommand);
510 capturing_video = 0;
511 return;
512 }
513 if (!parsecommand (videopipe.command, cap_videocommand, sizeof(videopipe.command)))
514 {
515 lprintf (LO_ERROR, "I_CapturePrep: malformed command %s\n", cap_videocommand);
516 capturing_video = 0;
517 return;
518 }
519 if (!parsecommand (muxpipe.command, cap_muxcommand, sizeof(muxpipe.command)))
520 {
521 lprintf (LO_ERROR, "I_CapturePrep: malformed command %s\n", cap_muxcommand);
522 capturing_video = 0;
523 return;
524 }
525
526 lprintf (LO_INFO, "I_CapturePrep: opening pipe \"%s\"\n", soundpipe.command);
527 if (!my_popen3 (&soundpipe))
528 {
529 lprintf (LO_ERROR, "I_CapturePrep: sound pipe failed\n");
530 capturing_video = 0;
531 return;
532 }
533 lprintf (LO_INFO, "I_CapturePrep: opening pipe \"%s\"\n", videopipe.command);
534 if (!my_popen3 (&videopipe))
535 {
536 lprintf (LO_ERROR, "I_CapturePrep: video pipe failed\n");
537 my_pclose3 (&soundpipe);
538 capturing_video = 0;
539 return;
540 }
541 I_SetSoundCap ();
542 lprintf (LO_INFO, "I_CapturePrep: video capture started\n");
543 capturing_video = 1;
544
545 // start reader threads
546 soundpipe.stdoutdumpname = "sound_stdout.txt";
547 soundpipe.stderrdumpname = "sound_stderr.txt";
548 soundpipe.outthread = SDL_CreateThread (threadstdoutproc, "soundpipe.outthread", &soundpipe);
549 soundpipe.errthread = SDL_CreateThread (threadstderrproc, "soundpipe.errthread", &soundpipe);
550 videopipe.stdoutdumpname = "video_stdout.txt";
551 videopipe.stderrdumpname = "video_stderr.txt";
552 videopipe.outthread = SDL_CreateThread (threadstdoutproc, "videopipe.outthread", &videopipe);
553 videopipe.errthread = SDL_CreateThread (threadstderrproc, "videopipe.errthread", &videopipe);
554
555 I_AtExit (I_CaptureFinish, true, "I_CaptureFinish", exit_priority_normal);
556 }
557
558
559
560 // capture a single frame of video (and corresponding audio length)
561 // and send it to pipes
562 // Modified to work with SDL2 resizeable window and fullscreen desktop - DTIED
563 void I_CaptureFrame (void)
564 {
565 unsigned char *snd;
566 unsigned char *vid;
567 static int partsof35 = 0; // correct for sync when samplerate % 35 != 0
568 int nsampreq;
569
570 if (!capturing_video)
571 return;
572
573 nsampreq = snd_samplerate / cap_fps;
574 partsof35 += snd_samplerate % cap_fps;
575 if (partsof35 >= cap_fps)
576 {
577 partsof35 -= cap_fps;
578 nsampreq++;
579 }
580
581 snd = I_GrabSound (nsampreq);
582 if (snd)
583 {
584 if (fwrite (snd, nsampreq * 4, 1, soundpipe.f_stdin) != 1)
585 lprintf(LO_WARN, "I_CaptureFrame: error writing soundpipe.\n");
586 //Z_Free (snd); // static buffer
587 }
588 vid = I_GrabScreen ();
589 if (vid)
590 {
591 if (fwrite (vid, renderW * renderH * 3, 1, videopipe.f_stdin) != 1)
592 lprintf(LO_WARN, "I_CaptureFrame: error writing videopipe.\n");
593 //Z_Free (vid); // static buffer
594 }
595
596 }
597
598
599 // close pipes, call muxcommand, finalize
600 void I_CaptureFinish (void)
601 {
602 int s;
603
604 if (!capturing_video)
605 return;
606 capturing_video = 0;
607
608 // on linux, we have to close videopipe first, because it has a copy of the write
609 // end of soundpipe_stdin (so that stream will never see EOF).
610 // is there a better way to do this?
611
612 // (on windows, it doesn't matter what order we do it in)
613 my_pclose3 (&videopipe);
614 SDL_WaitThread (videopipe.outthread, &s);
615 SDL_WaitThread (videopipe.errthread, &s);
616
617 my_pclose3 (&soundpipe);
618 SDL_WaitThread (soundpipe.outthread, &s);
619 SDL_WaitThread (soundpipe.errthread, &s);
620
621 // muxing and temp file cleanup
622
623 lprintf (LO_INFO, "I_CaptureFinish: opening pipe \"%s\"\n", muxpipe.command);
624
625 if (!my_popen3 (&muxpipe))
626 {
627 lprintf (LO_ERROR, "I_CaptureFinish: finalize pipe failed\n");
628 return;
629 }
630
631 muxpipe.stdoutdumpname = "mux_stdout.txt";
632 muxpipe.stderrdumpname = "mux_stderr.txt";
633 muxpipe.outthread = SDL_CreateThread (threadstdoutproc, "muxpipe.outthread", &muxpipe);
634 muxpipe.errthread = SDL_CreateThread (threadstderrproc, "muxpipe.errthread", &muxpipe);
635
636 my_pclose3 (&muxpipe);
637 SDL_WaitThread (muxpipe.outthread, &s);
638 SDL_WaitThread (muxpipe.errthread, &s);
639
640
641 // unlink any files user wants gone
642 if (dsda_IntConfig(dsda_config_cap_remove_tempfiles))
643 {
644 const char* cap_tempfile1;
645 const char* cap_tempfile2;
646
647 cap_tempfile1 = dsda_StringConfig(dsda_config_cap_tempfile1);
648 cap_tempfile2 = dsda_StringConfig(dsda_config_cap_tempfile2);
649
650 M_remove (cap_tempfile1);
651 M_remove (cap_tempfile2);
652 }
653 }