Do not use highest RT priority by default.
[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 [-prio NNNN|max] [-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 mvcp_status_t status;
75 struct {
76 int clip_index;
77 int is_logged;
78 } asrun[ MAX_UNITS ];
79 const char *config_file = "/etc/melted.conf";
80
81 #ifndef __DARWIN__
82 for ( index = 1; index < argc; index ++ )
83 {
84 if ( !strcmp( argv[ index ], "-prio" ) )
85 {
86 struct sched_param scp;
87 char* prio = argv[ ++ index ];
88
89 memset( &scp, 0, sizeof( scp ) );
90
91 if( !strcmp( prio, "max" ) )
92 scp.sched_priority = sched_get_priority_max( SCHED_FIFO ) - 1;
93 else
94 scp.sched_priority = atoi(prio);
95
96 sched_setscheduler( 0, SCHED_FIFO, &scp );
97 }
98 }
99 #endif
100
101 mlt_factory_init( NULL );
102
103 server = melted_server_init( argv[ 0 ] );
104
105 for ( index = 1; index < argc; index ++ )
106 {
107 if ( !strcmp( argv[ index ], "-port" ) )
108 melted_server_set_port( server, atoi( argv[ ++ index ] ) );
109 else if ( !strcmp( argv[ index ], "-proxy" ) )
110 melted_server_set_proxy( server, argv[ ++ index ] );
111 else if ( !strcmp( argv[ index ], "-test" ) )
112 background = 0;
113 else if ( !strcmp( argv[ index ], "-c" ) )
114 config_file = argv[ ++ index ];
115 else if ( !strcmp( argv[ index ], "-prio" ) )
116 index++;
117 else
118 usage( argv[ 0 ] );
119 }
120
121 /* Optionally detatch ourselves from the controlling tty */
122
123 if ( background )
124 {
125 if ( fork() )
126 return 0;
127 setsid();
128 melted_log_init( log_syslog, LOG_NOTICE );
129 }
130 else
131 {
132 mlt_log_set_level( MLT_LOG_VERBOSE );
133 melted_log_init( log_stderr, LOG_DEBUG );
134 }
135
136 atexit( main_cleanup );
137
138 /* Set the config script */
139 melted_server_set_config( server, config_file );
140
141 /* Execute the server */
142 error = melted_server_execute( server );
143
144 /* Initialize the as-run log tracking */
145 for ( index = 0; index < MAX_UNITS; index ++ )
146 asrun[ index ].clip_index = -1;
147
148 /* We need to wait until we're exited.. */
149 while ( !server->shutdown )
150 {
151 nanosleep( &tm, NULL );
152
153 /* As-run logging */
154 for ( index = 0; !error && index < MAX_UNITS; index ++ )
155 {
156 melted_unit unit = melted_get_unit( index );
157
158 if ( unit && melted_unit_get_status( unit, &status ) == 0 )
159 {
160 int length = status.length - 60;
161
162 /* Reset the logging if needed */
163 if ( status.clip_index != asrun[ index ].clip_index || status.position < length || status.status == unit_not_loaded )
164 {
165 asrun[ index ].clip_index = status.clip_index;
166 asrun[ index ].is_logged = 0;
167 }
168 /* Log as-run only once when near the end */
169 if ( ! asrun[ index ].is_logged && status.length > 0 && status.position > length )
170 {
171 melted_log( LOG_NOTICE, "AS-RUN U%d \"%s\" len %d pos %d", index, status.clip, status.length, status.position );
172 asrun[ index ].is_logged = 1;
173 }
174 }
175 }
176 }
177
178 return error;
179 }