SDL Preview provisional checkin
[melted] / src / modules / sdl / consumer_sdl_preview.c
1 /*
2 * consumer_sdl_preview.c -- A Simple DirectMedia Layer consumer
3 * Copyright (C) 2004-2005 Ushodaya Enterprises Limited
4 * Author: Charles Yates
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include "consumer_sdl.h"
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_factory.h>
24 #include <framework/mlt_producer.h>
25 #include <stdlib.h>
26 #include <pthread.h>
27 #include <SDL/SDL.h>
28 #include <SDL/SDL_syswm.h>
29
30 typedef struct consumer_sdl_s *consumer_sdl;
31
32 struct consumer_sdl_s
33 {
34 struct mlt_consumer_s parent;
35 mlt_consumer play;
36 mlt_consumer still;
37 pthread_t thread;
38 int joined;
39 int running;
40 int sdl_flags;
41 double last_speed;
42 };
43
44 /** Forward references to static functions.
45 */
46
47 static int consumer_start( mlt_consumer parent );
48 static int consumer_stop( mlt_consumer parent );
49 static int consumer_is_stopped( mlt_consumer parent );
50 static void consumer_close( mlt_consumer parent );
51 static void *consumer_thread( void * );
52 static void consumer_frame_show_cb( mlt_consumer sdl, mlt_consumer this, mlt_frame frame );
53
54 mlt_consumer consumer_sdl_preview_init( char *arg )
55 {
56 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
57 if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
58 {
59 // Get the parent consumer object
60 mlt_consumer parent = &this->parent;
61 this->play = mlt_factory_consumer( "sdl", arg );
62 this->still = mlt_factory_consumer( "sdl_still", arg );
63 mlt_properties_set( mlt_consumer_properties( parent ), "real_time", "0" );
64 parent->close = consumer_close;
65 parent->start = consumer_start;
66 parent->stop = consumer_stop;
67 parent->is_stopped = consumer_is_stopped;
68 this->joined = 1;
69 mlt_events_listen( mlt_consumer_properties( this->play ), this, "consumer-frame-show", ( mlt_listener )consumer_frame_show_cb );
70 mlt_events_listen( mlt_consumer_properties( this->still ), this, "consumer-frame-show", ( mlt_listener )consumer_frame_show_cb );
71 return parent;
72 }
73 free( this );
74 return NULL;
75 }
76
77 void consumer_frame_show_cb( mlt_consumer sdl, mlt_consumer parent, mlt_frame frame )
78 {
79 consumer_sdl this = parent->child;
80 this->last_speed = mlt_properties_get_double( mlt_frame_properties( frame ), "_speed" );
81 mlt_events_fire( mlt_consumer_properties( parent ), "consumer-frame-show", frame, NULL );
82 }
83
84 static int consumer_start( mlt_consumer parent )
85 {
86 consumer_sdl this = parent->child;
87
88 if ( !this->running )
89 {
90 pthread_attr_t thread_attributes;
91
92 consumer_stop( parent );
93
94 this->running = 1;
95 this->joined = 0;
96 this->last_speed = 1;
97
98 // Inherit the scheduling priority
99 pthread_attr_init( &thread_attributes );
100 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
101
102 pthread_create( &this->thread, &thread_attributes, consumer_thread, this );
103 }
104
105 return 0;
106 }
107
108 static int consumer_stop( mlt_consumer parent )
109 {
110 // Get the actual object
111 consumer_sdl this = parent->child;
112
113 if ( this->joined == 0 )
114 {
115 // Kill the thread and clean up
116 this->running = 0;
117
118 pthread_join( this->thread, NULL );
119 this->joined = 1;
120 }
121
122 return 0;
123 }
124
125 static int consumer_is_stopped( mlt_consumer parent )
126 {
127 consumer_sdl this = parent->child;
128 return !this->running;
129 }
130
131 static void *consumer_thread( void *arg )
132 {
133 // Identify the arg
134 consumer_sdl this = arg;
135
136 // Get the consumer
137 mlt_consumer consumer = &this->parent;
138
139 // internal intialization
140 int first = 1;
141 mlt_frame frame = NULL;
142
143 // properties
144 mlt_properties properties = mlt_consumer_properties( consumer );
145 mlt_properties play = mlt_consumer_properties( this->play );
146 mlt_properties still = mlt_consumer_properties( this->still );
147
148 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
149 {
150 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
151 return NULL;
152 }
153
154 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
155 SDL_EnableUNICODE( 1 );
156
157 // Inform child consumers that we control the sdl
158 mlt_properties_set_int( play, "sdl_started", 1 );
159 mlt_properties_set_int( still, "sdl_started", 1 );
160
161 // Pass properties down
162 mlt_properties_set_data( play, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
163 mlt_properties_set_data( still, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
164 mlt_properties_set_data( play, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
165 mlt_properties_set_data( still, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
166 mlt_properties_set( play, "rescale", mlt_properties_get( properties, "rescale" ) );
167 mlt_properties_set( still, "rescale", mlt_properties_get( properties, "rescale" ) );
168 mlt_properties_set( play, "width", mlt_properties_get( properties, "width" ) );
169 mlt_properties_set( still, "width", mlt_properties_get( properties, "width" ) );
170 mlt_properties_set( play, "height", mlt_properties_get( properties, "height" ) );
171 mlt_properties_set( still, "height", mlt_properties_get( properties, "height" ) );
172
173 mlt_properties_pass( play, mlt_consumer_properties( consumer ), "play." );
174 mlt_properties_pass( still, mlt_consumer_properties( consumer ), "still." );
175
176 // Loop until told not to
177 while( this->running )
178 {
179 // Get a frame from the attached producer
180 frame = mlt_consumer_get_frame( consumer );
181
182 // Ensure that we have a frame
183 if ( frame != NULL )
184 {
185 // Get the speed of the frame
186 double speed = mlt_properties_get_double( mlt_frame_properties( frame ), "_speed" );
187
188 // Make sure the recipient knows that this frame isn't really rendered
189 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 0 );
190
191 if ( !first && mlt_consumer_is_stopped( this->play ) && mlt_consumer_is_stopped( this->still ) )
192 {
193 this->running = 0;
194 mlt_frame_close( frame );
195 }
196 else if ( this->last_speed != 1 )
197 {
198 if ( !mlt_consumer_is_stopped( this->play ) )
199 mlt_consumer_stop( this->play );
200 if ( mlt_consumer_is_stopped( this->still ) )
201 {
202 this->last_speed = speed;
203 mlt_consumer_start( this->still );
204 }
205 mlt_consumer_put_frame( this->still, frame );
206 }
207 else
208 {
209 if ( !mlt_consumer_is_stopped( this->still ) )
210 mlt_consumer_stop( this->still );
211 if ( mlt_consumer_is_stopped( this->play ) )
212 {
213 this->last_speed = speed;
214 mlt_consumer_start( this->play );
215 }
216 mlt_consumer_put_frame( this->play, frame );
217 }
218 first = 0;
219 }
220 }
221
222 mlt_consumer_stop( this->play );
223 mlt_consumer_stop( this->still );
224
225 SDL_Quit( );
226
227 return NULL;
228 }
229
230 /** Callback to allow override of the close method.
231 */
232
233 static void consumer_close( mlt_consumer parent )
234 {
235 // Get the actual object
236 consumer_sdl this = parent->child;
237
238 // Stop the consumer
239 mlt_consumer_stop( parent );
240
241 // Now clean up the rest
242 mlt_consumer_close( parent );
243
244 // Close the child consumers
245 mlt_consumer_close( this->play );
246 mlt_consumer_close( this->still );
247
248 // Finally clean up this
249 free( this );
250 }