Revert inadvertent changes in commit fea346.
[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
33 #include <framework/mlt.h>
34 #include <mvcp/mvcp_notifier.h>
35 #include <mvcp/mvcp_status.h>
36
37 /* Application header files */
38 #include "melted_server.h"
39 #include "melted_log.h"
40 #include "melted_commands.h"
41 #include "melted_unit.h"
42
43 /** Our server context.
44 */
45
46 static melted_server server = NULL;
47
48 /** atexit shutdown handler for the server.
49 */
50
51 static void main_cleanup( )
52 {
53 melted_server_close( server );
54 }
55
56 /** Report usage and exit.
57 */
58
59 void usage( char *app )
60 {
61 fprintf( stderr, "Usage: %s [-test] [-port NNNN] [-c config-file]\n", app );
62 exit( 0 );
63 }
64
65 /** The main function.
66 */
67
68 int main( int argc, char **argv )
69 {
70 int error = 0;
71 int index = 0;
72 int background = 1;
73 struct timespec tm = { 1, 0 };
74 struct sched_param scp;
75 mvcp_status_t status;
76 struct {
77 int clip_index;
78 int is_logged;
79 } asrun[ MAX_UNITS ];
80 const char *config_file = "/etc/melted.conf";
81
82 // Use realtime scheduling if possible
83 memset( &scp, '\0', sizeof( scp ) );
84 scp.sched_priority = sched_get_priority_max( SCHED_FIFO ) - 1;
85 #ifndef __DARWIN__
86 sched_setscheduler( 0, SCHED_FIFO, &scp );
87 #endif
88
89 mlt_factory_init( NULL );
90
91 server = melted_server_init( argv[ 0 ] );
92
93 for ( index = 1; index < argc; index ++ )
94 {
95 if ( !strcmp( argv[ index ], "-port" ) )
96 melted_server_set_port( server, atoi( argv[ ++ index ] ) );
97 else if ( !strcmp( argv[ index ], "-proxy" ) )
98 melted_server_set_proxy( server, argv[ ++ index ] );
99 else if ( !strcmp( argv[ index ], "-test" ) )
100 background = 0;
101 else if ( !strcmp( argv[ index ], "-c" ) )
102 config_file = argv[ ++ index ];
103 else
104 usage( argv[ 0 ] );
105 }
106
107 /* Optionally detatch ourselves from the controlling tty */
108
109 if ( background )
110 {
111 if ( fork() )
112 return 0;
113 setsid();
114 melted_log_init( log_syslog, LOG_NOTICE );
115 }
116 else
117 {
118 melted_log_init( log_stderr, LOG_DEBUG );
119 }
120
121 atexit( main_cleanup );
122
123 /* Set the config script */
124 melted_server_set_config( server, config_file );
125
126 /* Execute the server */
127 error = melted_server_execute( server );
128
129 /* Initialize the as-run log tracking */
130 for ( index = 0; index < MAX_UNITS; index ++ )
131 asrun[ index ].clip_index = -1;
132
133 /* We need to wait until we're exited.. */
134 while ( !server->shutdown )
135 {
136 nanosleep( &tm, NULL );
137
138 /* As-run logging */
139 for ( index = 0; !error && index < MAX_UNITS; index ++ )
140 {
141 melted_unit unit = melted_get_unit( index );
142
143 if ( unit && melted_unit_get_status( unit, &status ) == 0 )
144 {
145 int length = status.length - 60;
146
147 /* Reset the logging if needed */
148 if ( status.clip_index != asrun[ index ].clip_index || status.position < length || status.status == unit_not_loaded )
149 {
150 asrun[ index ].clip_index = status.clip_index;
151 asrun[ index ].is_logged = 0;
152 }
153 /* Log as-run only once when near the end */
154 if ( ! asrun[ index ].is_logged && status.length > 0 && status.position > length )
155 {
156 melted_log( LOG_NOTICE, "AS-RUN U%d \"%s\" len %d pos %d", index, status.clip, status.length, status.position );
157 asrun[ index ].is_logged = 1;
158 }
159 }
160 }
161 }
162
163 return error;
164 }