ad903bf56f2c38be3f3974f26dc66d041005d83a
[melted] / src / melted / melted.c
1 /*
2 * melted.c -- MLT Video TCP Server
3 *
4 * Copyright (C) 2002-2009 Ushodaya Enterprises Limited
5 * Authors:
6 * Dan Dennedy <dan@dennedy.org>
7 * Charles Yates <charles.yates@pandora.be>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24 /* System header files */
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 #include <time.h>
31 #include <sched.h>
32 #include <signal.h>
33
34 #include <framework/mlt.h>
35 #include <mvcp/mvcp_notifier.h>
36 #include <mvcp/mvcp_status.h>
37
38 /* Application header files */
39 #include "melted_server.h"
40 #include "melted_log.h"
41 #include "melted_commands.h"
42 #include "melted_unit.h"
43
44 /** Our server context.
45 */
46
47 static melted_server server = NULL;
48
49 /** atexit shutdown handler for the server.
50 */
51
52 static void main_cleanup( )
53 {
54 melted_server_close( server );
55 }
56
57 /** Report usage and exit.
58 */
59
60 void usage( char *app )
61 {
62 fprintf( stderr, "Usage: %s [-test] [-port NNNN] [-c config-file]\n", app );
63 exit( 0 );
64 }
65
66 /** The main function.
67 */
68
69 int main( int argc, char **argv )
70 {
71 int error = 0;
72 int index = 0;
73 int background = 1;
74 struct timespec tm = { 1, 0 };
75 struct sched_param scp;
76 mvcp_status_t status;
77 struct {
78 int clip_index;
79 int is_logged;
80 } asrun[ MAX_UNITS ];
81 const char *config_file = "/etc/melted.conf";
82
83 // Use realtime scheduling if possible
84 memset( &scp, '\0', sizeof( scp ) );
85 scp.sched_priority = sched_get_priority_max( SCHED_FIFO ) - 1;
86 #ifndef __DARWIN__
87 sched_setscheduler( 0, SCHED_FIFO, &scp );
88 #endif
89
90 mlt_factory_init( NULL );
91
92 server = melted_server_init( argv[ 0 ] );
93
94 for ( index = 1; index < argc; index ++ )
95 {
96 if ( !strcmp( argv[ index ], "-port" ) )
97 melted_server_set_port( server, atoi( argv[ ++ index ] ) );
98 else if ( !strcmp( argv[ index ], "-proxy" ) )
99 melted_server_set_proxy( server, argv[ ++ index ] );
100 else if ( !strcmp( argv[ index ], "-test" ) )
101 background = 0;
102 else if ( !strcmp( argv[ index ], "-c" ) )
103 config_file = argv[ ++ index ];
104 else
105 usage( argv[ 0 ] );
106 }
107
108 /* Optionally detatch ourselves from the controlling tty */
109
110 if ( background )
111 {
112 if ( fork() )
113 return 0;
114 setsid();
115 melted_log_init( log_syslog, LOG_NOTICE );
116 }
117 else
118 {
119 melted_log_init( log_stderr, LOG_DEBUG );
120 }
121
122 atexit( main_cleanup );
123
124 /* Set the config script */
125 melted_server_set_config( server, config_file );
126
127 /* Execute the server */
128 error = melted_server_execute( server );
129
130 /* Initialize the as-run log tracking */
131 for ( index = 0; index < MAX_UNITS; index ++ )
132 asrun[ index ].clip_index = -1;
133
134 /* We need to wait until we're exited.. */
135 while ( !server->shutdown )
136 {
137 nanosleep( &tm, NULL );
138
139 /* As-run logging */
140 for ( index = 0; !error && index < MAX_UNITS; index ++ )
141 {
142 melted_unit unit = melted_get_unit( index );
143
144 if ( unit && melted_unit_get_status( unit, &status ) == 0 )
145 {
146 int length = status.length - 60;
147
148 /* Reset the logging if needed */
149 if ( status.clip_index != asrun[ index ].clip_index || status.position < length || status.status == unit_not_loaded )
150 {
151 asrun[ index ].clip_index = status.clip_index;
152 asrun[ index ].is_logged = 0;
153 }
154 /* Log as-run only once when near the end */
155 if ( ! asrun[ index ].is_logged && status.length > 0 && status.position > length )
156 {
157 melted_log( LOG_NOTICE, "AS-RUN U%d \"%s\" len %d pos %d", index, status.clip, status.length, status.position );
158 asrun[ index ].is_logged = 1;
159 }
160 }
161 }
162 }
163
164 return error;
165 }