1 /*
2  * consumer_sdl_preview.c -- A Simple DirectMedia Layer consumer
3  * Copyright (C) 2004-2021 Meltytech, LLC
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include <framework/mlt_consumer.h>
21 #include <framework/mlt_frame.h>
22 #include <framework/mlt_factory.h>
23 #include <framework/mlt_producer.h>
24 #include <framework/mlt_log.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <pthread.h>
28 #include <SDL.h>
29 #include <SDL_syswm.h>
30 #include <sys/time.h>
31 
32 extern pthread_mutex_t mlt_sdl_mutex;
33 
34 typedef struct consumer_sdl_s *consumer_sdl;
35 
36 struct consumer_sdl_s
37 {
38 	struct mlt_consumer_s parent;
39 	mlt_consumer active;
40 	int ignore_change;
41 	mlt_consumer play;
42 	mlt_consumer still;
43 	pthread_t thread;
44 	int joined;
45 	int running;
46 	int sdl_flags;
47 	double last_speed;
48 	mlt_position last_position;
49 
50 	pthread_cond_t refresh_cond;
51 	pthread_mutex_t refresh_mutex;
52 	int refresh_count;
53 };
54 
55 /** Forward references to static functions.
56 */
57 
58 static int consumer_start( mlt_consumer parent );
59 static int consumer_stop( mlt_consumer parent );
60 static int consumer_is_stopped( mlt_consumer parent );
61 static void consumer_purge( mlt_consumer parent );
62 static void consumer_close( mlt_consumer parent );
63 static void *consumer_thread( void * );
64 static void consumer_frame_show_cb( mlt_consumer sdl, mlt_consumer self, mlt_event_data );
65 static void consumer_sdl_event_cb( mlt_consumer sdl, mlt_consumer self, mlt_event_data );
66 static void consumer_refresh_cb(mlt_consumer sdl, mlt_consumer self, mlt_event_data );
67 
consumer_sdl_preview_init(mlt_profile profile,mlt_service_type type,const char * id,char * arg)68 mlt_consumer consumer_sdl_preview_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
69 {
70 	consumer_sdl self = calloc( 1, sizeof( struct consumer_sdl_s ) );
71 	if ( self != NULL && mlt_consumer_init( &self->parent, self, profile ) == 0 )
72 	{
73 		// Get the parent consumer object
74 		mlt_consumer parent = &self->parent;
75 
76 		// Get the properties
77 		mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
78 
79 		// Get the width and height
80 		int width = mlt_properties_get_int( properties, "width" );
81 		int height = mlt_properties_get_int( properties, "height" );
82 
83 		// Process actual param
84 		if ( arg == NULL || sscanf( arg, "%dx%d", &width, &height ) == 2 )
85 		{
86 			mlt_properties_set_int( properties, "width", width );
87 			mlt_properties_set_int( properties, "height", height );
88 		}
89 
90 		// Create child consumers
91 		self->play = mlt_factory_consumer( profile, "sdl", arg );
92 		self->still = mlt_factory_consumer( profile, "sdl_still", arg );
93 		mlt_properties_set( properties, "rescale", "nearest" );
94 		mlt_properties_set( properties, "deinterlace_method", "onefield" );
95 		mlt_properties_set_int( properties, "prefill", 1 );
96 		mlt_properties_set_int( properties, "top_field_first", -1 );
97 
98 		parent->close = consumer_close;
99 		parent->start = consumer_start;
100 		parent->stop = consumer_stop;
101 		parent->is_stopped = consumer_is_stopped;
102 		parent->purge = consumer_purge;
103 		self->joined = 1;
104 		mlt_events_listen( MLT_CONSUMER_PROPERTIES( self->play ), self, "consumer-frame-show", ( mlt_listener )consumer_frame_show_cb );
105 		mlt_events_listen( MLT_CONSUMER_PROPERTIES( self->still ), self, "consumer-frame-show", ( mlt_listener )consumer_frame_show_cb );
106 		mlt_events_listen( MLT_CONSUMER_PROPERTIES( self->play ), self, "consumer-sdl-event", ( mlt_listener )consumer_sdl_event_cb );
107 		mlt_events_listen( MLT_CONSUMER_PROPERTIES( self->still ), self, "consumer-sdl-event", ( mlt_listener )consumer_sdl_event_cb );
108 		pthread_cond_init( &self->refresh_cond, NULL );
109 		pthread_mutex_init( &self->refresh_mutex, NULL );
110 		mlt_events_listen( MLT_CONSUMER_PROPERTIES( parent ), self, "property-changed", ( mlt_listener )consumer_refresh_cb );
111 		mlt_events_register( properties, "consumer-sdl-paused" );
112 		return parent;
113 	}
114 	free( self );
115 	return NULL;
116 }
117 
consumer_frame_show_cb(mlt_consumer sdl,mlt_consumer parent,mlt_event_data event_data)118 void consumer_frame_show_cb(mlt_consumer sdl, mlt_consumer parent, mlt_event_data event_data )
119 {
120 	mlt_frame frame = mlt_event_data_to_frame(event_data);
121 	consumer_sdl self = parent->child;
122 	if (frame && self) {
123 		self->last_speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" );
124 		self->last_position = mlt_frame_get_position( frame );
125 		mlt_events_fire( MLT_CONSUMER_PROPERTIES( parent ), "consumer-frame-show", event_data );
126 	}
127 }
128 
consumer_sdl_event_cb(mlt_consumer sdl,mlt_consumer parent,mlt_event_data event_data)129 static void consumer_sdl_event_cb( mlt_consumer sdl, mlt_consumer parent, mlt_event_data event_data )
130 {
131 	mlt_events_fire( MLT_CONSUMER_PROPERTIES( parent ), "consumer-sdl-event", event_data );
132 }
133 
consumer_refresh_cb(mlt_consumer sdl,mlt_consumer parent,mlt_event_data event_data)134 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer parent, mlt_event_data event_data )
135 {
136 	const char *name = mlt_event_data_to_string(event_data);
137 	if ( name && !strcmp( name, "refresh" ) )
138 	{
139 		consumer_sdl self = parent->child;
140 		pthread_mutex_lock( &self->refresh_mutex );
141 		self->refresh_count = self->refresh_count <= 0 ? 1 : self->refresh_count + 1;
142 		pthread_cond_broadcast( &self->refresh_cond );
143 		pthread_mutex_unlock( &self->refresh_mutex );
144 	}
145 }
146 
consumer_start(mlt_consumer parent)147 static int consumer_start( mlt_consumer parent )
148 {
149 	consumer_sdl self = parent->child;
150 
151 	if ( !self->running )
152 	{
153 		// properties
154 		mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
155 		mlt_properties play = MLT_CONSUMER_PROPERTIES( self->play );
156 		mlt_properties still = MLT_CONSUMER_PROPERTIES( self->still );
157 
158 		char *window_id = mlt_properties_get( properties, "window_id" );
159 		char *audio_driver = mlt_properties_get( properties, "audio_driver" );
160 		char *video_driver = mlt_properties_get( properties, "video_driver" );
161 		char *audio_device = mlt_properties_get( properties, "audio_device" );
162 		char *output_display = mlt_properties_get( properties, "output_display" );
163 		int progressive = mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" );
164 
165 		consumer_stop( parent );
166 
167 		self->running = 1;
168 		self->joined = 0;
169 		self->last_speed = 1;
170 
171 		if ( output_display != NULL )
172 			setenv( "DISPLAY", output_display, 1 );
173 
174 		if ( window_id != NULL )
175 			setenv( "SDL_WINDOWID", window_id, 1 );
176 
177 		if ( video_driver != NULL )
178 			setenv( "SDL_VIDEODRIVER", video_driver, 1 );
179 
180 		if ( audio_driver != NULL )
181 			setenv( "SDL_AUDIODRIVER", audio_driver, 1 );
182 
183 		if ( audio_device != NULL )
184 			setenv( "AUDIODEV", audio_device, 1 );
185 
186 		pthread_mutex_lock( &mlt_sdl_mutex );
187 		int ret = SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
188 		pthread_mutex_unlock( &mlt_sdl_mutex );
189 		if ( ret < 0 )
190 		{
191 			fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
192 			return -1;
193 		}
194 
195 		SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
196 		SDL_EnableUNICODE( 1 );
197 
198 		// Pass properties down
199 		mlt_properties_set_data( play, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
200 		mlt_properties_set_data( still, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
201 		mlt_properties_set_data( play, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
202 		mlt_properties_set_data( still, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
203 
204 		mlt_properties_set_int( play, "progressive", progressive );
205 		mlt_properties_set_int( still, "progressive", progressive );
206 
207 		mlt_properties_pass_list( play, properties,
208 			"deinterlace_method,resize,rescale,width,height,aspect_ratio,display_ratio,preview_off,preview_format,window_background"
209 			",top_field_first,volume,real_time,buffer,prefill,audio_off,frequency,drop_max" );
210 		mlt_properties_pass_list( still, properties,
211 			"deinterlace_method,resize,rescale,width,height,aspect_ratio,display_ratio,preview_off,preview_format,window_background"
212 			",top_field_first");
213 
214 		mlt_properties_pass( play, properties, "play." );
215 		mlt_properties_pass( still, properties, "still." );
216 
217 		mlt_properties_set_data( play, "app_lock", mlt_properties_get_data( properties, "app_lock", NULL ), 0, NULL, NULL );
218 		mlt_properties_set_data( still, "app_lock", mlt_properties_get_data( properties, "app_lock", NULL ), 0, NULL, NULL );
219 		mlt_properties_set_data( play, "app_unlock", mlt_properties_get_data( properties, "app_unlock", NULL ), 0, NULL, NULL );
220 		mlt_properties_set_data( still, "app_unlock", mlt_properties_get_data( properties, "app_unlock", NULL ), 0, NULL, NULL );
221 
222 		mlt_properties_set_int( play, "put_mode", 1 );
223 		mlt_properties_set_int( still, "put_mode", 1 );
224 		mlt_properties_set_int( play, "terminate_on_pause", 1 );
225 
226 		// Start the still producer just to initialise the gui
227 		mlt_consumer_start( self->still );
228 		self->active = self->still;
229 
230 		// Inform child consumers that we control the sdl
231 		mlt_properties_set_int( play, "sdl_started", 1 );
232 		mlt_properties_set_int( still, "sdl_started", 1 );
233 
234 		pthread_create( &self->thread, NULL, consumer_thread, self );
235 	}
236 
237 	return 0;
238 }
239 
consumer_stop(mlt_consumer parent)240 static int consumer_stop( mlt_consumer parent )
241 {
242 	// Get the actual object
243 	consumer_sdl self = parent->child;
244 
245 	if ( self->joined == 0 )
246 	{
247 		mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
248 		int app_locked = mlt_properties_get_int( properties, "app_locked" );
249 		void ( *lock )( void ) = mlt_properties_get_data( properties, "app_lock", NULL );
250 		void ( *unlock )( void ) = mlt_properties_get_data( properties, "app_unlock", NULL );
251 
252 		if ( app_locked && unlock ) unlock( );
253 
254 		// Kill the thread and clean up
255 		self->running = 0;
256 
257 		pthread_mutex_lock( &self->refresh_mutex );
258 		pthread_cond_broadcast( &self->refresh_cond );
259 		pthread_mutex_unlock( &self->refresh_mutex );
260 #ifndef _WIN32
261 		if ( self->thread )
262 #endif
263 			pthread_join( self->thread, NULL );
264 		self->joined = 1;
265 
266 		if ( app_locked && lock ) lock( );
267 
268 		pthread_mutex_lock( &mlt_sdl_mutex );
269 		SDL_Quit( );
270 		pthread_mutex_unlock( &mlt_sdl_mutex );
271 	}
272 
273 	return 0;
274 }
275 
consumer_is_stopped(mlt_consumer parent)276 static int consumer_is_stopped( mlt_consumer parent )
277 {
278 	consumer_sdl self = parent->child;
279 	return !self->running;
280 }
281 
consumer_purge(mlt_consumer parent)282 void consumer_purge( mlt_consumer parent )
283 {
284 	consumer_sdl self = parent->child;
285 	if ( self->running )
286 		mlt_consumer_purge( self->play );
287 }
288 
consumer_thread(void * arg)289 static void *consumer_thread( void *arg )
290 {
291 	// Identify the arg
292 	consumer_sdl self = arg;
293 
294 	// Get the consumer
295 	mlt_consumer consumer = &self->parent;
296 
297 	// Get the properties
298 	mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
299 
300 	// internal initialization
301 	mlt_frame frame = NULL;
302 	int last_position = -1;
303 	int eos = 0;
304 	int eos_threshold = 20;
305 	if ( self->play )
306 		eos_threshold = eos_threshold + mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( self->play ), "buffer" );
307 
308 	// Determine if the application is dealing with the preview
309 	int preview_off = mlt_properties_get_int( properties, "preview_off" );
310 
311 	pthread_mutex_lock( &self->refresh_mutex );
312 	self->refresh_count = 0;
313 	pthread_mutex_unlock( &self->refresh_mutex );
314 
315 	// Loop until told not to
316 	while( self->running )
317 	{
318 		// Get a frame from the attached producer
319 		frame = mlt_consumer_get_frame( consumer );
320 
321 		// Ensure that we have a frame
322 		if ( self->running && frame != NULL )
323 		{
324 			// Get the speed of the frame
325 			double speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" );
326 
327 			// Lock during the operation
328 			mlt_service_lock( MLT_CONSUMER_SERVICE( consumer ) );
329 
330 			// Get refresh request for the current frame
331 			int refresh = mlt_properties_get_int( properties, "refresh" );
332 
333 			// Decrement refresh and clear changed
334 			mlt_events_block( properties, properties );
335 			mlt_properties_set_int( properties, "refresh", 0 );
336 			mlt_events_unblock( properties, properties );
337 
338 			// Unlock after the operation
339 			mlt_service_unlock( MLT_CONSUMER_SERVICE( consumer ) );
340 
341 			// Set the changed property on this frame for the benefit of still
342 			mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "refresh", refresh );
343 
344 			// Make sure the recipient knows that this frame isn't really rendered
345 			mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 0 );
346 
347 			// Optimisation to reduce latency
348 			if ( speed == 1.0 )
349 			{
350 				if ( last_position != -1 && last_position + 1 != mlt_frame_get_position( frame ) )
351 					mlt_consumer_purge( self->play );
352 				last_position = mlt_frame_get_position( frame );
353 			}
354 			else
355 			{
356 				//mlt_consumer_purge( self->play );
357 				last_position = -1;
358 			}
359 
360 			// If we aren't playing normally, then use the still
361 			if ( speed != 1 )
362 			{
363 				mlt_producer producer = MLT_PRODUCER( mlt_service_get_producer( MLT_CONSUMER_SERVICE( consumer ) ) );
364 				mlt_position duration = producer? mlt_producer_get_playtime( producer ) : -1;
365 				int pause = 0;
366 
367 #ifndef SKIP_WAIT_EOS
368 				if ( self->active == self->play )
369 				{
370 					// Do not interrupt the play consumer near the end
371 					if ( duration - self->last_position > eos_threshold )
372 					{
373 						// Get a new frame at the sought position
374 						mlt_frame_close( frame );
375 						if ( producer )
376 							mlt_producer_seek( producer, self->last_position );
377 						frame = mlt_consumer_get_frame( consumer );
378 						pause = 1;
379 					}
380 					else
381 					{
382 						// Send frame with speed 0 to stop it
383 						if ( frame && !mlt_consumer_is_stopped( self->play ) )
384 						{
385 							mlt_consumer_put_frame( self->play, frame );
386 							frame = NULL;
387 							eos = 1;
388 						}
389 
390 						// Check for end of stream
391 						if ( mlt_consumer_is_stopped( self->play ) )
392 						{
393 							// Stream has ended
394 							mlt_log_verbose( MLT_CONSUMER_SERVICE( consumer ), "END OF STREAM\n" );
395 							pause = 1;
396 							eos = 0; // reset eos indicator
397 						}
398 						else
399 						{
400 							// Prevent a tight busy loop
401 							struct timespec tm = { 0, 100000L }; // 100 usec
402 							nanosleep( &tm, NULL );
403 						}
404 					}
405 				}
406 #else
407 				pause = self->active == self->play;
408 #endif
409 				if ( pause )
410 				{
411 					// Start the still consumer
412 					if ( !mlt_consumer_is_stopped( self->play ) )
413 						mlt_consumer_stop( self->play );
414 					self->last_speed = speed;
415 					self->active = self->still;
416 					self->ignore_change = 0;
417 					mlt_consumer_start( self->still );
418 				}
419 				// Send the frame to the active child
420 				if ( frame && !eos )
421 				{
422 					mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "refresh", 1 );
423 					if ( self->active )
424 						mlt_consumer_put_frame( self->active, frame );
425 				}
426 				if ( pause && speed == 0.0 )
427 				{
428 					mlt_events_fire( properties, "consumer-sdl-paused", mlt_event_data_none() );
429 				}
430 			}
431 			// Allow a little grace time before switching consumers on speed changes
432 			else if ( self->ignore_change -- > 0 && self->active != NULL && !mlt_consumer_is_stopped( self->active ) )
433 			{
434 				mlt_consumer_put_frame( self->active, frame );
435 			}
436 			// Otherwise use the normal player
437 			else
438 			{
439 				if ( !mlt_consumer_is_stopped( self->still ) )
440 					mlt_consumer_stop( self->still );
441 				if ( mlt_consumer_is_stopped( self->play ) )
442 				{
443 					self->last_speed = speed;
444 					self->active = self->play;
445 					self->ignore_change = 0;
446 					mlt_consumer_start( self->play );
447 				}
448 				if ( self->play )
449 					mlt_consumer_put_frame( self->play, frame );
450 			}
451 
452 			// Copy the rectangle info from the active consumer
453 			if ( self->running && preview_off == 0 && self->active )
454 			{
455 				mlt_properties active = MLT_CONSUMER_PROPERTIES( self->active );
456 				mlt_service_lock( MLT_CONSUMER_SERVICE( consumer ) );
457 				mlt_properties_set_int( properties, "rect_x", mlt_properties_get_int( active, "rect_x" ) );
458 				mlt_properties_set_int( properties, "rect_y", mlt_properties_get_int( active, "rect_y" ) );
459 				mlt_properties_set_int( properties, "rect_w", mlt_properties_get_int( active, "rect_w" ) );
460 				mlt_properties_set_int( properties, "rect_h", mlt_properties_get_int( active, "rect_h" ) );
461 				mlt_service_unlock( MLT_CONSUMER_SERVICE( consumer ) );
462 			}
463 
464 			if ( self->active == self->still )
465 			{
466 				pthread_mutex_lock( &self->refresh_mutex );
467 				if ( self->running && speed == 0 && self->refresh_count <= 0 )
468 				{
469 					mlt_events_fire( properties, "consumer-sdl-paused", mlt_event_data_none() );
470 					pthread_cond_wait( &self->refresh_cond, &self->refresh_mutex );
471 				}
472 				self->refresh_count --;
473 				pthread_mutex_unlock( &self->refresh_mutex );
474 			}
475 		}
476 		else
477 		{
478 			if ( frame ) mlt_frame_close( frame );
479 			mlt_consumer_put_frame( self->active, NULL );
480 			self->running = 0;
481 		}
482 	}
483 
484 	if ( self->play ) mlt_consumer_stop( self->play );
485 	if ( self->still ) mlt_consumer_stop( self->still );
486 
487 	return NULL;
488 }
489 
490 /** Callback to allow override of the close method.
491 */
492 
consumer_close(mlt_consumer parent)493 static void consumer_close( mlt_consumer parent )
494 {
495 	// Get the actual object
496 	consumer_sdl self = parent->child;
497 
498 	// Stop the consumer
499 	mlt_consumer_stop( parent );
500 
501 	// Close the child consumers
502 	mlt_consumer_close( self->play );
503 	mlt_consumer_close( self->still );
504 
505 	// Now clean up the rest
506 	mlt_consumer_close( parent );
507 
508 	// Finally clean up self
509 	free( self );
510 }
511