diff options
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 218 |
1 files changed, 0 insertions, 218 deletions
@@ -1,218 +0,0 @@ -/* - * This file is part of the OpenKinect Project. http://www.openkinect.org - * - * Copyright (c) 2010 Brandyn White (bwhite@dappervision.com) - * Copyright (c) 2016 Luke Shumaker (lukeshu@sbcglobal.net) - * - * This code is licensed to you under the terms of the Apache License, version - * 2.0, or, at your option, the terms of the GNU General Public License, - * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, - * or the following URLs: - * http://www.apache.org/licenses/LICENSE-2.0 - * http://www.gnu.org/licenses/gpl-2.0.txt - * - * If you redistribute this file in source form, modified or unmodified, you - * may: - * 1) Leave this header intact and distribute it under the same terms, - * accompanying it with the APACHE20 and GPL20 files, or - * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or - * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file - * In all cases you must keep the copyright notice intact and include a copy - * of the CONTRIB file. - * - * Binary distributions must follow the binary distribution requirements of - * either License. - */ - -#include <stdio.h> - -#include <errno.h> -#include <error.h> -#include <pthread.h> -#include <signal.h> -#include <stdlib.h> -#include <unistd.h> -#include <sys/wait.h> - -#include "main.h" -#include "wg.h" - -volatile sig_atomic_t running = 1; -static struct wg wg; - -void * xrealloc(void *ptr, size_t size) -{ - void *ret = realloc(ptr, size); - if (ret == NULL) { - if (ptr==NULL) - error(1, errno, "Could not allocate memory"); - else - error(1, errno, "Could not re-allocate memory"); - } - return ret; -} - -static -void start_ffmpeg(int in, int out) { - switch (fork()) { - case -1: /* error */ - error(1, errno, "fork"); - case 0: /* child */ - dup2(in, 0); - dup2(out, 1); - for (int fd = 3; fd < 30; fd++) - close(fd); - execlp("ffmpeg", - "ffmpeg", - "-pix_fmt", "rgb24", "-s", "640x480", "-f", "rawvideo", "-i", "/dev/stdin", - "-f", "mpjpeg", "-", - NULL); - error(1, errno, "execlp: ffmpeg"); - default: /* parent */ - close(in); - close(out); - } -} - -struct thread_kinect_args { - int video_fd; - int depth_fd; - int accel_fd; -}; -static -void* start_kinect_inner(void *args_anon) { - pthread_setname_np(pthread_self(), "kinect"); - struct thread_kinect_args *args = args_anon; - thread_kinect(args->video_fd, args->depth_fd, args->accel_fd); - wg_sub(&wg, 1); - finish(0); - return NULL; -} -static -void start_kinect(int video_fd, int depth_fd, int accel_fd) { - - static struct thread_kinect_args args; - args.video_fd = video_fd; - args.depth_fd = depth_fd; - args.accel_fd = accel_fd; - - static pthread_t thread; - wg_add(&wg, 1); - pthread_create(&thread, NULL, start_kinect_inner, &args); -} - -struct thread_mpjpeg_args { - struct mpjpeg_stream *s; - int fd; - const char *boundary; -}; -static -void *start_mpjpeg_reader_inner(void *args_anon) { - pthread_setname_np(pthread_self(), "mpjpeg-reader"); - struct thread_mpjpeg_args *args = args_anon; - thread_mpjpeg_reader(args->s, args->fd, args->boundary); - wg_sub(&wg, 1); - return NULL; -} -static -void start_mpjpeg_reader(struct mpjpeg_stream *s, int fd, const char *boundary) { - struct thread_mpjpeg_args *args = xrealloc(NULL, sizeof(struct thread_mpjpeg_args)); - args->s = s; - args->fd = fd; - args->boundary = boundary; - wg_add(&wg, 1); - static pthread_t thread; - pthread_create(&thread, NULL, start_mpjpeg_reader_inner, args); -} - -struct thread_http_args { - struct wg *wg; - struct mpjpeg_stream *video; - struct mpjpeg_stream *depth; -}; -static -void *start_http_inner(void *args_anon) { - pthread_setname_np(pthread_self(), "http-listener"); - struct thread_http_args *args = args_anon; - thread_http_listener(args->wg, args->video, args->depth); - wg_sub(&wg, 1); - finish(0); - return NULL; -} -static -void start_http(struct wg *awg, - struct mpjpeg_stream *video, - struct mpjpeg_stream *depth) { - static struct thread_http_args args; - args.wg = awg; - args.video = video; - args.depth = depth; - - static pthread_t thread; - wg_add(&wg, 1); - pthread_create(&thread, NULL, start_http_inner, &args); -} - -static -void sigchld_handler(int sig UNUSED) -{ - int status; - while (waitpid(-1, &status, WNOHANG) > 0) {} -} - -void finish(int sig) -{ - if (sig != 0) - printf("Caught signal %d\n", sig); - running = 0; - close(httpsock); - kill(getpid(), SIGHUP); -} - -int main(int argc UNUSED, char **argv UNUSED) -{ - int kinect_video_fds[2]; - int kinect_depth_fds[2]; - int kinect_accel_fds[2]; - int mpjpeg_video_fds[2]; - int mpjpeg_depth_fds[2]; - struct mpjpeg_stream mpjpeg_video; - struct mpjpeg_stream mpjpeg_depth; - - struct sigaction act_chld; - sigemptyset (&act_chld.sa_mask); - act_chld.sa_flags = SA_RESTART; - act_chld.sa_handler = sigchld_handler; - if (sigaction(SIGCHLD, &act_chld, 0)) { - error(EXIT_FAILURE, errno, _("Could not set up SIGCHLD handler")); - } - - struct sigaction act_exit; - sigemptyset (&act_exit.sa_mask); - act_exit.sa_flags = 0; - act_exit.sa_handler = finish; - sigaction(SIGINT, &act_exit, 0); - sigaction(SIGQUIT, &act_exit, 0); - sigaction(SIGTERM, &act_exit, 0); - - if (pipe(kinect_video_fds) == -1) error(1, errno, "pipe"); - if (pipe(kinect_depth_fds) == -1) error(1, errno, "pipe"); - if (pipe(kinect_accel_fds) == -1) error(1, errno, "pipe"); - if (pipe(mpjpeg_video_fds) == -1) error(1, errno, "pipe"); - if (pipe(mpjpeg_depth_fds) == -1) error(1, errno, "pipe"); - init_mpjpeg_stream(&mpjpeg_video); - init_mpjpeg_stream(&mpjpeg_depth); - - wg_init(&wg); - start_kinect(kinect_video_fds[1], - kinect_depth_fds[1], - kinect_accel_fds[1]); - start_mpjpeg_reader(&mpjpeg_video, mpjpeg_video_fds[0], "ffserver"); - start_mpjpeg_reader(&mpjpeg_depth, mpjpeg_depth_fds[0], "ffserver"); - start_http(&wg, &mpjpeg_video, &mpjpeg_depth); - /* never call exit() after we've started ffmpeg */ - start_ffmpeg(kinect_video_fds[0], mpjpeg_video_fds[1]); - start_ffmpeg(kinect_depth_fds[0], mpjpeg_depth_fds[1]); - wg_wait(&wg); - return 0; -} |