1 /*
2  * consumer_xml.c -- a libxml2 serialiser of mlt service networks
3  * Copyright (C) 2003-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 "common.h"
21 
22 #include <framework/mlt.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <locale.h>
28 #include <libxml/tree.h>
29 #include <pthread.h>
30 
31 #define ID_SIZE 128
32 #define TIME_PROPERTY "_consumer_xml"
33 
34 #define _x (const xmlChar*)
35 #define _s (const char*)
36 
37 // This maintains counters for adding ids to elements
38 struct serialise_context_s
39 {
40 	mlt_properties id_map;
41 	int producer_count;
42 	int multitrack_count;
43 	int playlist_count;
44 	int tractor_count;
45 	int filter_count;
46 	int transition_count;
47 	int chain_count;
48 	int link_count;
49 	int pass;
50 	mlt_properties hide_map;
51 	char *root;
52 	char *store;
53 	int no_meta;
54 	mlt_profile profile;
55 	mlt_time_format time_format;
56 };
57 typedef struct serialise_context_s* serialise_context;
58 
59 /** Forward references to static functions.
60 */
61 
62 static int consumer_start( mlt_consumer parent );
63 static int consumer_stop( mlt_consumer parent );
64 static int consumer_is_stopped( mlt_consumer consumer );
65 static void consumer_close( mlt_consumer parent );
66 static void *consumer_thread( void *arg );
67 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node );
68 
69 typedef enum
70 {
71 	xml_existing,
72 	xml_producer,
73 	xml_multitrack,
74 	xml_playlist,
75 	xml_tractor,
76 	xml_filter,
77 	xml_transition,
78 	xml_chain,
79 	xml_link,
80 }
81 xml_type;
82 
83 /** Create or retrieve an id associated to this service.
84 */
85 
xml_get_id(serialise_context context,mlt_service service,xml_type type)86 static char *xml_get_id( serialise_context context, mlt_service service, xml_type type )
87 {
88 	char *id = NULL;
89 	int i = 0;
90 	mlt_properties map = context->id_map;
91 
92 	// Search the map for the service
93 	for ( i = 0; i < mlt_properties_count( map ); i ++ )
94 		if ( mlt_properties_get_data_at( map, i, NULL ) == service )
95 			break;
96 
97 	// If the service is not in the map, and the type indicates a new id is needed...
98 	if ( i >= mlt_properties_count( map ) && type != xml_existing )
99 	{
100 		// Attempt to reuse existing id
101 		id = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "id" );
102 
103 		// If no id, or the id is used in the map (for another service), then
104 		// create a new one.
105 		if ( id == NULL || mlt_properties_get_data( map, id, NULL ) != NULL )
106 		{
107 			char temp[ ID_SIZE ];
108 			do
109 			{
110 				switch( type )
111 				{
112 					case xml_producer:
113 						sprintf( temp, "producer%d", context->producer_count ++ );
114 						break;
115 					case xml_multitrack:
116 						sprintf( temp, "multitrack%d", context->multitrack_count ++ );
117 						break;
118 					case xml_playlist:
119 						sprintf( temp, "playlist%d", context->playlist_count ++ );
120 						break;
121 					case xml_tractor:
122 						sprintf( temp, "tractor%d", context->tractor_count ++ );
123 						break;
124 					case xml_filter:
125 						sprintf( temp, "filter%d", context->filter_count ++ );
126 						break;
127 					case xml_transition:
128 						sprintf( temp, "transition%d", context->transition_count ++ );
129 						break;
130 					case xml_chain:
131 						sprintf( temp, "chain%d", context->chain_count ++ );
132 						break;
133 					case xml_link:
134 						sprintf( temp, "link%d", context->link_count ++ );
135 						break;
136 					case xml_existing:
137 						// Never gets here
138 						break;
139 				}
140 			}
141 			while( mlt_properties_get_data( map, temp, NULL ) != NULL );
142 
143 			// Set the data at the generated name
144 			mlt_properties_set_data( map, temp, service, 0, NULL, NULL );
145 
146 			// Get the pointer to the name (i is the end of the list)
147 			id = mlt_properties_get_name( map, i );
148 		}
149 		else
150 		{
151 			// Store the existing id in the map
152 			mlt_properties_set_data( map, id, service, 0, NULL, NULL );
153 		}
154 	}
155 	else if ( type == xml_existing )
156 	{
157 		id = mlt_properties_get_name( map, i );
158 	}
159 
160 	return id;
161 }
162 
163 /** This is what will be called by the factory - anything can be passed in
164 	via the argument, but keep it simple.
165 */
166 
consumer_xml_init(mlt_profile profile,mlt_service_type type,const char * id,char * arg)167 mlt_consumer consumer_xml_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
168 {
169 	// Create the consumer object
170 	mlt_consumer consumer = calloc( 1, sizeof( struct mlt_consumer_s ) );
171 
172 	// If no malloc'd and consumer init ok
173 	if ( consumer != NULL && mlt_consumer_init( consumer, NULL, profile ) == 0 )
174 	{
175 		// Allow thread to be started/stopped
176 		consumer->start = consumer_start;
177 		consumer->stop = consumer_stop;
178 		consumer->is_stopped = consumer_is_stopped;
179 
180 		// Assign close callback
181 		consumer->close = consumer_close;
182 
183 		mlt_properties_set( MLT_CONSUMER_PROPERTIES( consumer ), "resource", arg );
184 		mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "real_time", 0 );
185 		mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "prefill", 1 );
186 		mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "terminate_on_pause", 1 );
187 
188 		// Return the consumer produced
189 		return consumer;
190 	}
191 
192 	// malloc or consumer init failed
193 	free( consumer );
194 
195 	// Indicate failure
196 	return NULL;
197 }
198 
serialise_properties(serialise_context context,mlt_properties properties,xmlNode * node)199 static void serialise_properties( serialise_context context, mlt_properties properties, xmlNode *node )
200 {
201 	int i;
202 	xmlNode *p;
203 
204 	// Enumerate the properties
205 	for ( i = 0; i < mlt_properties_count( properties ); i++ )
206 	{
207 		char *name = mlt_properties_get_name( properties, i );
208 		if ( name != NULL &&
209 			 name[ 0 ] != '_' &&
210 			 mlt_properties_get_value( properties, i ) != NULL &&
211 			 ( !context->no_meta || strncmp( name, "meta.", 5 ) ) &&
212 			 strcmp( name, "mlt" ) &&
213 			 strcmp( name, "mlt_type" ) &&
214 			 strcmp( name, "in" ) &&
215 			 strcmp( name, "out" ) &&
216 			 strcmp( name, "id" ) &&
217 			 strcmp( name, "title" ) &&
218 			 strcmp( name, "root" ) &&
219 			 strcmp( name, "width" ) &&
220 			 strcmp( name, "height" ) )
221 		{
222 			char *value = mlt_properties_get_value_tf( properties, i, context->time_format );
223 			if ( value )
224 			{
225 				int rootlen = strlen( context->root );
226 				const char *value_orig = value;
227 				size_t prefix_size = mlt_xml_prefix_size( properties, name, value );
228 
229 				// Strip off prefix.
230 				if ( prefix_size )
231 					value += prefix_size;
232 
233 				// Ignore trailing slash on root.
234 				if ( rootlen && ( context->root[rootlen - 1] == '/' || context->root[rootlen - 1] == '\\') )
235 					--rootlen;
236 
237 				// convert absolute path to relative
238 				if ( rootlen && !strncmp( value, context->root, rootlen ) &&
239 					( value[rootlen] == '/' || value[rootlen] == '\\' ) )
240 				{
241 					if ( prefix_size )
242 					{
243 						char *s = calloc( 1, strlen( value_orig ) - rootlen + 1 );
244 						strncat( s, value_orig, prefix_size );
245 						strcat( s, value + rootlen + 1 );
246 						p = xmlNewTextChild( node, NULL, _x("property"), _x(s) );
247 						free( s );
248 					} else {
249 						p = xmlNewTextChild( node, NULL, _x("property"), _x(value_orig + rootlen + 1) );
250 					}
251 				}
252 				else
253 					p = xmlNewTextChild( node, NULL, _x("property"), _x(value_orig) );
254 				xmlNewProp( p, _x("name"), _x(name) );
255 			}
256 		}
257 	}
258 }
259 
serialise_store_properties(serialise_context context,mlt_properties properties,xmlNode * node,const char * store)260 static void serialise_store_properties( serialise_context context, mlt_properties properties, xmlNode *node, const char *store )
261 {
262 	int i;
263 	xmlNode *p;
264 
265 	// Enumerate the properties
266 	for ( i = 0; store != NULL && i < mlt_properties_count( properties ); i++ )
267 	{
268 		char *name = mlt_properties_get_name( properties, i );
269 		if ( !strncmp( name, store, strlen( store ) ) )
270 		{
271 			char *value = mlt_properties_get_value_tf( properties, i, context->time_format );
272 			if ( value )
273 			{
274 				int rootlen = strlen( context->root );
275 				// convert absolute path to relative
276 				if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
277 					p = xmlNewTextChild( node, NULL, _x("property"), _x(value + rootlen + 1) );
278 				else
279 					p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
280 				xmlNewProp( p, _x("name"), _x(name) );
281 			}
282 		}
283 	}
284 }
285 
serialise_service_filters(serialise_context context,mlt_service service,xmlNode * node)286 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
287 {
288 	int i;
289 	xmlNode *p;
290 	mlt_filter filter = NULL;
291 
292 	// Enumerate the filters
293 	for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
294 	{
295 		mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
296 		if ( mlt_properties_get_int( properties, "_loader" ) == 0 )
297 		{
298 			// Get a new id - if already allocated, do nothing
299 			char *id = xml_get_id( context, MLT_FILTER_SERVICE( filter ), xml_filter );
300 			if ( id != NULL )
301 			{
302 				p = xmlNewChild( node, NULL, _x("filter"), NULL );
303 				xmlNewProp( p, _x("id"), _x(id) );
304 				if ( mlt_properties_get( properties, "title" ) )
305 					xmlNewProp( p, _x("title"), _x(mlt_properties_get( properties, "title" )) );
306 				if ( mlt_properties_get_position( properties, "in" ) )
307 					xmlNewProp( p, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
308 				if ( mlt_properties_get_position( properties, "out" ) )
309 					xmlNewProp( p, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
310 				serialise_properties( context, properties, p );
311 				serialise_service_filters( context, MLT_FILTER_SERVICE( filter ), p );
312 			}
313 		}
314 	}
315 }
316 
serialise_producer(serialise_context context,mlt_service service,xmlNode * node)317 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
318 {
319 	xmlNode *child = node;
320 	mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
321 
322 	if ( context->pass == 0 )
323 	{
324 		mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
325 		// Get a new id - if already allocated, do nothing
326 		char *id = xml_get_id( context, parent, xml_producer );
327 		if ( id == NULL )
328 			return;
329 
330 		child = xmlNewChild( node, NULL, _x("producer"), NULL );
331 
332 		// Set the id
333 		xmlNewProp( child, _x("id"), _x(id) );
334 		if ( mlt_properties_get( properties, "title" ) )
335 			xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
336 		xmlNewProp( child, _x("in"), _x(mlt_properties_get_time( properties, "in", context->time_format )) );
337 		xmlNewProp( child, _x("out"), _x(mlt_properties_get_time( properties, "out", context->time_format )) );
338 
339 		// If the xml producer fails to load a producer, it creates a text producer that says INVALID
340 		// and sets the xml_mlt_service property to the original service.
341 		const char *xml_mlt_service = mlt_properties_get(properties, "_xml_mlt_service");
342 		if (xml_mlt_service) {
343 			// We should not serialize this as a text producer but using the original mlt_service.
344 			mlt_properties_set(properties, "mlt_service", xml_mlt_service);
345 		}
346 
347 		serialise_properties( context, properties, child );
348 		serialise_service_filters( context, service, child );
349 
350 		// Add producer to the map
351 		mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
352 	}
353 	else
354 	{
355 		char *id = xml_get_id( context, parent, xml_existing );
356 		mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
357 		xmlNewProp( node, _x("parent"), _x(id) );
358 		xmlNewProp( node, _x("in"), _x(mlt_properties_get_time( properties, "in", context->time_format )) );
359 		xmlNewProp( node, _x("out"), _x(mlt_properties_get_time( properties, "out", context->time_format )) );
360 	}
361 }
362 
363 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
364 
serialise_multitrack(serialise_context context,mlt_service service,xmlNode * node)365 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
366 {
367 	int i;
368 
369 	if ( context->pass == 0 )
370 	{
371 		// Iterate over the tracks to collect the producers
372 		for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
373 		{
374 			mlt_producer producer = mlt_producer_cut_parent( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) );
375 			serialise_service( context, MLT_SERVICE( producer ), node );
376 		}
377 	}
378 	else
379 	{
380 		// Get a new id - if already allocated, do nothing
381 		char *id = xml_get_id( context, service, xml_multitrack );
382 		if ( id == NULL )
383 			return;
384 
385 		// Serialise the tracks
386 		for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
387 		{
388 			xmlNode *track = xmlNewChild( node, NULL, _x("track"), NULL );
389 			int hide = 0;
390 			mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
391 			mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
392 
393 			mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( producer ) );
394 
395 			char *id = xml_get_id( context, MLT_SERVICE( parent ), xml_existing );
396 			xmlNewProp( track, _x("producer"), _x(id) );
397 			if ( mlt_producer_is_cut( producer ) )
398 			{
399 				xmlNewProp( track, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
400 				xmlNewProp( track, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
401 				serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, context->store );
402 				serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "xml_" );
403 				if ( !context->no_meta )
404 					serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "meta." );
405 				serialise_service_filters( context, MLT_PRODUCER_SERVICE( producer ), track );
406 			}
407 
408 			hide = mlt_properties_get_int( context->hide_map, id );
409 			if ( hide )
410 				xmlNewProp( track, _x("hide"), _x( hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) ) );
411 		}
412 		serialise_service_filters( context, service, node );
413 	}
414 }
415 
serialise_playlist(serialise_context context,mlt_service service,xmlNode * node)416 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
417 {
418 	int i;
419 	xmlNode *child = node;
420 	mlt_playlist_clip_info info;
421 	mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
422 
423 	if ( context->pass == 0 )
424 	{
425 		// Get a new id - if already allocated, do nothing
426 		char *id = xml_get_id( context, service, xml_playlist );
427 		if ( id == NULL )
428 			return;
429 
430 		// Iterate over the playlist entries to collect the producers
431 		for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
432 		{
433 			if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
434 			{
435 				if ( info.producer != NULL )
436 				{
437 					mlt_producer producer = mlt_producer_cut_parent( info.producer );
438 					char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
439 					char *resource_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "resource" );
440 					if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
441 						serialise_playlist( context, MLT_SERVICE( producer ), node );
442 					else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
443 						serialise_service( context, MLT_SERVICE( producer ), node );
444 				}
445 			}
446 		}
447 
448 		child = xmlNewChild( node, NULL, _x("playlist"), NULL );
449 
450 		// Set the id
451 		xmlNewProp( child, _x("id"), _x(id) );
452 		if ( mlt_properties_get( properties, "title" ) )
453 			xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
454 
455 		// Store application specific properties
456 		serialise_store_properties( context, properties, child, context->store );
457 		serialise_store_properties( context, properties, child, "xml_" );
458 		if ( !context->no_meta )
459 			serialise_store_properties( context, properties, child, "meta." );
460 
461 		// Add producer to the map
462 		mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
463 
464 		// Iterate over the playlist entries
465 		for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
466 		{
467 			if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
468 			{
469 				mlt_producer producer = mlt_producer_cut_parent( info.producer );
470 				mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
471 				char *service_s = mlt_properties_get( producer_props, "mlt_service" );
472 				if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
473 				{
474 					xmlNode *entry = xmlNewChild( child, NULL, _x("blank"), NULL );
475 					mlt_properties_set_data( producer_props, "_profile", context->profile, 0, NULL, NULL );
476 					mlt_properties_set_position( producer_props, TIME_PROPERTY, info.frame_count );
477 					xmlNewProp( entry, _x("length"), _x( mlt_properties_get_time( producer_props, TIME_PROPERTY, context->time_format ) ) );
478 				}
479 				else
480 				{
481 					char temp[ 20 ];
482 					xmlNode *entry = xmlNewChild( child, NULL, _x("entry"), NULL );
483 					id = xml_get_id( context, MLT_SERVICE( producer ), xml_existing );
484 					xmlNewProp( entry, _x("producer"), _x(id) );
485 					mlt_properties_set_position( producer_props, TIME_PROPERTY, info.frame_in );
486 					xmlNewProp( entry, _x("in"), _x( mlt_properties_get_time( producer_props, TIME_PROPERTY, context->time_format ) ) );
487 					mlt_properties_set_position( producer_props, TIME_PROPERTY, info.frame_out );
488 					xmlNewProp( entry, _x("out"), _x( mlt_properties_get_time( producer_props, TIME_PROPERTY, context->time_format ) ) );
489 					if ( info.repeat > 1 )
490 					{
491 						sprintf( temp, "%d", info.repeat );
492 						xmlNewProp( entry, _x("repeat"), _x(temp) );
493 					}
494 					if ( mlt_producer_is_cut( info.cut ) )
495 					{
496 						serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, context->store );
497 						serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "xml_" );
498 						if ( !context->no_meta )
499 							serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "meta." );
500 						serialise_service_filters( context, MLT_PRODUCER_SERVICE( info.cut ), entry );
501 					}
502 				}
503 			}
504 		}
505 
506 		serialise_service_filters( context, service, child );
507 	}
508 	else if ( xmlStrcmp( node->name, _x("tractor") ) != 0 )
509 	{
510 		char *id = xml_get_id( context, service, xml_existing );
511 		xmlNewProp( node, _x("producer"), _x(id) );
512 	}
513 }
514 
serialise_tractor(serialise_context context,mlt_service service,xmlNode * node)515 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
516 {
517 	xmlNode *child = node;
518 	mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
519 
520 	if ( context->pass == 0 )
521 	{
522 		// Recurse on connected producer
523 		serialise_service( context, mlt_service_producer( service ), node );
524 	}
525 	else
526 	{
527 		// Get a new id - if already allocated, do nothing
528 		char *id = xml_get_id( context, service, xml_tractor );
529 		if ( id == NULL )
530 			return;
531 
532 		child = xmlNewChild( node, NULL, _x("tractor"), NULL );
533 
534 		// Set the id
535 		xmlNewProp( child, _x("id"), _x(id) );
536 		if ( mlt_properties_get( properties, "title" ) )
537 			xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
538 		if ( mlt_properties_get_position( properties, "in" ) >= 0 )
539 			xmlNewProp( child, _x("in"), _x(mlt_properties_get_time( properties, "in", context->time_format )) );
540 		if ( mlt_properties_get_position( properties, "out" ) >= 0 )
541 			xmlNewProp( child, _x("out"), _x(mlt_properties_get_time( properties, "out", context->time_format )) );
542 
543 		// Store application specific properties
544 		serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, context->store );
545 		serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "xml_" );
546 		if ( !context->no_meta )
547 			serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "meta." );
548 
549 		// Recurse on connected producer
550 		serialise_service( context, mlt_service_producer( service ), child );
551 		serialise_service_filters( context, service, child );
552 	}
553 }
554 
serialise_filter(serialise_context context,mlt_service service,xmlNode * node)555 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
556 {
557 	xmlNode *child = node;
558 	mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
559 
560 	// Recurse on connected producer
561 	serialise_service( context, mlt_service_producer( service ), node );
562 
563 	if ( context->pass == 1 )
564 	{
565 		// Get a new id - if already allocated, do nothing
566 		char *id = xml_get_id( context, service, xml_filter );
567 		if ( id == NULL )
568 			return;
569 
570 		child = xmlNewChild( node, NULL, _x("filter"), NULL );
571 
572 		// Set the id
573 		xmlNewProp( child, _x("id"), _x(id) );
574 		if ( mlt_properties_get( properties, "title" ) )
575 			xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
576 		if ( mlt_properties_get_position( properties, "in" ) )
577 			xmlNewProp( child, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
578 		if ( mlt_properties_get_position( properties, "out" ) )
579 			xmlNewProp( child, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
580 
581 		serialise_properties( context, properties, child );
582 		serialise_service_filters( context, service, child );
583 	}
584 }
585 
serialise_transition(serialise_context context,mlt_service service,xmlNode * node)586 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
587 {
588 	xmlNode *child = node;
589 	mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
590 
591 	// Recurse on connected producer
592 	serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
593 
594 	if ( context->pass == 1 )
595 	{
596 		// Get a new id - if already allocated, do nothing
597 		char *id = xml_get_id( context, service, xml_transition );
598 		if ( id == NULL )
599 			return;
600 
601 		child = xmlNewChild( node, NULL, _x("transition"), NULL );
602 
603 		// Set the id
604 		xmlNewProp( child, _x("id"), _x(id) );
605 		if ( mlt_properties_get( properties, "title" ) )
606 			xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
607 		if ( mlt_properties_get_position( properties, "in" ) )
608 			xmlNewProp( child, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
609 		if ( mlt_properties_get_position( properties, "out" ) )
610 			xmlNewProp( child, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
611 
612 		serialise_properties( context, properties, child );
613 		serialise_service_filters( context, service, child );
614 	}
615 }
616 
serialise_link(serialise_context context,mlt_service service,xmlNode * node)617 static void serialise_link( serialise_context context, mlt_service service, xmlNode *node )
618 {
619 	xmlNode *child = node;
620 	mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
621 
622 	if ( context->pass == 0 )
623 	{
624 		// Get a new id - if already allocated, do nothing
625 		char *id = xml_get_id( context, service, xml_link );
626 		if ( id == NULL )
627 			return;
628 
629 		child = xmlNewChild( node, NULL, _x("link"), NULL );
630 
631 		// Set the id
632 		xmlNewProp( child, _x("id"), _x(id) );
633 		if ( mlt_properties_get( properties, "title" ) )
634 			xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
635 		if ( mlt_properties_get_position( properties, "in" ) )
636 			xmlNewProp( child, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
637 		if ( mlt_properties_get_position( properties, "out" ) )
638 			xmlNewProp( child, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
639 
640 		serialise_properties( context, properties, child );
641 		serialise_service_filters( context, service, child );
642 	}
643 }
644 
serialise_chain(serialise_context context,mlt_service service,xmlNode * node)645 static void serialise_chain( serialise_context context, mlt_service service, xmlNode *node )
646 {
647 	int i = 0;
648 	xmlNode *child = node;
649 	mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
650 
651 	if ( context->pass == 0 )
652 	{
653 		// Get a new id - if already allocated, do nothing
654 		char *id = xml_get_id( context, service, xml_chain );
655 		if ( id == NULL )
656 			return;
657 
658 		child = xmlNewChild( node, NULL, _x("chain"), NULL );
659 
660 		// Set the id
661 		xmlNewProp( child, _x("id"), _x(id) );
662 		if ( mlt_properties_get( properties, "title" ) )
663 			xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
664 		if ( mlt_properties_get_position( properties, "in" ) )
665 			xmlNewProp( child, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
666 		if ( mlt_properties_get_position( properties, "out" ) )
667 			xmlNewProp( child, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
668 
669 		serialise_properties( context, properties, child );
670 
671 		// Serialize links
672 		for ( i = 0; i < mlt_chain_link_count( MLT_CHAIN( service ) ); i++ )
673 		{
674 			mlt_link link = mlt_chain_link( MLT_CHAIN( service ), i );
675 			if ( link )
676 			{
677 				serialise_link( context, MLT_LINK_SERVICE(link), child );
678 			}
679 		}
680 
681 		serialise_service_filters( context, service, child );
682 	}
683 }
684 
serialise_service(serialise_context context,mlt_service service,xmlNode * node)685 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
686 {
687 	// Iterate over consumer/producer connections
688 	while ( service != NULL )
689 	{
690 		mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
691 		char *mlt_type = mlt_properties_get( properties, "mlt_type" );
692 
693 		// Tell about the producer
694 		if ( strcmp( mlt_type, "producer" ) == 0 )
695 		{
696 			char *mlt_service = mlt_properties_get( properties, "mlt_service" );
697 			if ( mlt_properties_get( properties, "xml" ) == NULL && ( mlt_service != NULL && !strcmp( mlt_service, "tractor" ) ) )
698 			{
699 				context->pass = 0;
700 				serialise_tractor( context, service, node );
701 				context->pass = 1;
702 				serialise_tractor( context, service, node );
703 				context->pass = 0;
704 				break;
705 			}
706 			else
707 			{
708 				serialise_producer( context, service, node );
709 			}
710 			if ( mlt_properties_get( properties, "xml" ) != NULL )
711 				break;
712 		}
713 
714 		// Tell about the framework container producers
715 		else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
716 		{
717 			char *resource = mlt_properties_get( properties, "resource" );
718 
719 			// Recurse on multitrack's tracks
720 			if ( resource && strcmp( resource, "<multitrack>" ) == 0 )
721 			{
722 				serialise_multitrack( context, service, node );
723 				break;
724 			}
725 
726 			// Recurse on playlist's clips
727 			else if ( resource && strcmp( resource, "<playlist>" ) == 0 )
728 			{
729 				serialise_playlist( context, service, node );
730 			}
731 
732 			// Recurse on tractor's producer
733 			else if ( resource && strcmp( resource, "<tractor>" ) == 0 )
734 			{
735 				context->pass = 0;
736 				serialise_tractor( context, service, node );
737 				context->pass = 1;
738 				serialise_tractor( context, service, node );
739 				context->pass = 0;
740 				break;
741 			}
742 
743 			// Treat it as a normal chain
744 			else if ( mlt_properties_get_int( properties, "_original_type" ) == mlt_service_chain_type )
745 			{
746 				serialise_chain( context, service, node );
747 				mlt_properties_set( properties, "mlt_type", "chain" );
748 				if ( mlt_properties_get( properties, "xml" ) != NULL )
749 					break;
750 			}
751 
752 			// Treat it as a normal producer
753 			else
754 			{
755 				serialise_producer( context, service, node );
756 				if ( mlt_properties_get( properties, "xml" ) != NULL )
757 					break;
758 			}
759 		}
760 
761 		// Tell about a chain
762 		else if ( strcmp( mlt_type, "chain" ) == 0 )
763 		{
764 			serialise_chain( context, service, node );
765 			break;
766 		}
767 
768 		// Tell about a filter
769 		else if ( strcmp( mlt_type, "filter" ) == 0 )
770 		{
771 			serialise_filter( context, service, node );
772 			break;
773 		}
774 
775 		// Tell about a transition
776 		else if ( strcmp( mlt_type, "transition" ) == 0 )
777 		{
778 			serialise_transition( context, service, node );
779 			break;
780 		}
781 
782 		// Get the next connected service
783 		service = mlt_service_producer( service );
784 	}
785 }
786 
serialise_other(mlt_properties properties,struct serialise_context_s * context,xmlNodePtr root)787 static void serialise_other( mlt_properties properties, struct serialise_context_s *context, xmlNodePtr root )
788 {
789 	int i;
790 	for ( i = 0; i < mlt_properties_count( properties ); i++ )
791 	{
792 		const char* name = mlt_properties_get_name( properties, i );
793 		if ( strlen(name) > 10 && !strncmp( name, "xml_retain", 10 ) )
794 		{
795 			mlt_service service = mlt_properties_get_data_at( properties, i, NULL );
796 			if ( service )
797 			{
798 				mlt_properties_set_int( MLT_SERVICE_PROPERTIES( service ), "xml_retain", 1 );
799 				serialise_service( context, service, root );
800 			}
801 		}
802 	}
803 }
804 
xml_make_doc(mlt_consumer consumer,mlt_service service)805 xmlDocPtr xml_make_doc( mlt_consumer consumer, mlt_service service )
806 {
807 	mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
808 	xmlDocPtr doc = xmlNewDoc( _x("1.0") );
809 	xmlNodePtr root = xmlNewNode( NULL, _x("mlt") );
810 	struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
811 	mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( consumer ) );
812 	char tmpstr[ 32 ];
813 
814 	xmlDocSetRootElement( doc, root );
815 
816 	// Indicate the numeric locale
817 	if ( mlt_properties_get_lcnumeric( properties ) )
818 		xmlNewProp( root, _x("LC_NUMERIC"), _x( mlt_properties_get_lcnumeric( properties ) ) );
819 	else
820 #ifdef _WIN32
821 	{
822 		char* lcnumeric = getlocale();
823 		mlt_properties_set( properties, "_xml_lcnumeric_in", lcnumeric );
824 		free( lcnumeric );
825 		mlt_properties_to_utf8( properties, "_xml_lcnumeric_in", "_xml_lcnumeric_out" );
826 		lcnumeric = mlt_properties_get( properties, "_xml_lcnumeric_out" );
827 		xmlNewProp( root, _x("LC_NUMERIC"), _x( lcnumeric ) );
828 	}
829 #else
830 		xmlNewProp( root, _x("LC_NUMERIC"), _x( setlocale( LC_NUMERIC, NULL ) ) );
831 #endif
832 
833 	// Indicate the version
834 	xmlNewProp( root, _x("version"), _x( mlt_version_get_string() ) );
835 
836 	// If we have root, then deal with it now
837 	if ( mlt_properties_get( properties, "root" ) != NULL )
838 	{
839 		if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "no_root" ) )
840 			xmlNewProp( root, _x("root"), _x(mlt_properties_get( properties, "root" )) );
841 		context->root = strdup( mlt_properties_get( properties, "root" ) );
842 	}
843 	else
844 	{
845 		context->root = strdup( "" );
846 	}
847 
848 	// Assign the additional 'storage' pattern for properties
849 	context->store = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "store" );
850 	context->no_meta = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "no_meta" );
851 	const char *time_format = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "time_format" );
852 	if ( time_format && ( !strcmp( time_format, "smpte" ) || !strcmp( time_format, "SMPTE" )
853 			|| !strcmp( time_format, "timecode" ) || !strcmp( time_format, "smpte_df" ) ) )
854 		context->time_format = mlt_time_smpte_df;
855 	else if ( time_format && ( !strcmp( time_format, "smpte_ndf" ) ) )
856 		context->time_format = mlt_time_smpte_ndf;
857 	else if ( time_format && ( !strcmp( time_format, "clock" ) || !strcmp( time_format, "CLOCK" ) ) )
858 		context->time_format = mlt_time_clock;
859 
860 	// Assign a title property
861 	if ( mlt_properties_get( properties, "title" ) != NULL )
862 		xmlNewProp( root, _x("title"), _x(mlt_properties_get( properties, "title" )) );
863 
864 	// Add a profile child element
865 	if ( profile )
866 	{
867 		if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "no_profile" ) )
868 		{
869 			xmlNodePtr profile_node = xmlNewChild( root, NULL, _x("profile"), NULL );
870 			if ( profile->description )
871 				xmlNewProp( profile_node, _x("description"), _x(profile->description) );
872 			sprintf( tmpstr, "%d", profile->width );
873 			xmlNewProp( profile_node, _x("width"), _x(tmpstr) );
874 			sprintf( tmpstr, "%d", profile->height );
875 			xmlNewProp( profile_node, _x("height"), _x(tmpstr) );
876 			sprintf( tmpstr, "%d", profile->progressive );
877 			xmlNewProp( profile_node, _x("progressive"), _x(tmpstr) );
878 			sprintf( tmpstr, "%d", profile->sample_aspect_num );
879 			xmlNewProp( profile_node, _x("sample_aspect_num"), _x(tmpstr) );
880 			sprintf( tmpstr, "%d", profile->sample_aspect_den );
881 			xmlNewProp( profile_node, _x("sample_aspect_den"), _x(tmpstr) );
882 			sprintf( tmpstr, "%d", profile->display_aspect_num );
883 			xmlNewProp( profile_node, _x("display_aspect_num"), _x(tmpstr) );
884 			sprintf( tmpstr, "%d", profile->display_aspect_den );
885 			xmlNewProp( profile_node, _x("display_aspect_den"), _x(tmpstr) );
886 			sprintf( tmpstr, "%d", profile->frame_rate_num );
887 			xmlNewProp( profile_node, _x("frame_rate_num"), _x(tmpstr) );
888 			sprintf( tmpstr, "%d", profile->frame_rate_den );
889 			xmlNewProp( profile_node, _x("frame_rate_den"), _x(tmpstr) );
890 			sprintf( tmpstr, "%d", profile->colorspace );
891 			xmlNewProp( profile_node, _x("colorspace"), _x(tmpstr) );
892 		}
893 		context->profile = profile;
894 	}
895 
896 	// Construct the context maps
897 	context->id_map = mlt_properties_new();
898 	context->hide_map = mlt_properties_new();
899 
900 	// Ensure producer is a framework producer
901 	mlt_properties_set_int( properties, "_original_type", mlt_service_identify( service ) );
902 	mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "mlt_type", "mlt_producer" );
903 
904 	// In pass one, we serialise the end producers and playlists,
905 	// adding them to a map keyed by address.
906 	serialise_other( MLT_SERVICE_PROPERTIES( service ), context, root );
907 	serialise_service( context, service, root );
908 
909 	// In pass two, we serialise the tractor and reference the
910 	// producers and playlists
911 	context->pass++;
912 	serialise_other( MLT_SERVICE_PROPERTIES( service ), context, root );
913 	serialise_service( context, service, root );
914 
915 	// Cleanup resource
916 	mlt_properties_close( context->id_map );
917 	mlt_properties_close( context->hide_map );
918 	free( context->root );
919 	free( context );
920 
921 	return doc;
922 }
923 
924 
output_xml(mlt_consumer consumer)925 static void output_xml( mlt_consumer consumer )
926 {
927 	// Get the producer service
928 	mlt_service service = mlt_service_producer( MLT_CONSUMER_SERVICE( consumer ) );
929 	mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
930 	char *resource =  mlt_properties_get( properties, "resource" );
931 	xmlDocPtr doc = NULL;
932 
933 	if ( !service ) return;
934 
935 	// Set the title if provided
936 	if ( mlt_properties_get( properties, "title" ) )
937 		mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", mlt_properties_get( properties, "title" ) );
938 
939 	// Check for a root on the consumer properties and pass to service
940 	if ( mlt_properties_get( properties, "root" ) )
941 		mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", mlt_properties_get( properties, "root" ) );
942 
943 	// Specify roots in other cases...
944 	if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
945 	{
946 		// Get the current working directory
947 		char *cwd = getcwd( NULL, 0 );
948 		mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", cwd );
949 		free( cwd );
950 	}
951 
952 	// Make the document
953 	doc = xml_make_doc( consumer, service );
954 
955 	// Handle the output
956 	if ( resource == NULL || !strcmp( resource, "" ) )
957 	{
958 		xmlDocFormatDump( stdout, doc, 1 );
959 	}
960 	else if ( strchr( resource, '.' ) == NULL )
961 	{
962 		xmlChar *buffer = NULL;
963 		int length = 0;
964 		xmlDocDumpMemoryEnc( doc, &buffer, &length, "utf-8" );
965 		mlt_properties_set( properties, resource, _s(buffer) );
966 #ifdef _WIN32
967 		xmlFreeFunc xmlFree = NULL;
968 		xmlMemGet( &xmlFree, NULL, NULL, NULL);
969 #endif
970 		xmlFree( buffer );
971 	}
972 	else
973 	{
974 		xmlSaveFormatFileEnc( resource, doc, "utf-8", 1 );
975 	}
976 
977 	// Close the document
978 	xmlFreeDoc( doc );
979 }
consumer_start(mlt_consumer consumer)980 static int consumer_start( mlt_consumer consumer )
981 {
982 	mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
983 
984 	if ( mlt_properties_get_int( properties, "all" ) )
985 	{
986 		// Check that we're not already running
987 		if ( !mlt_properties_get_int( properties, "running" ) )
988 		{
989 			// Allocate a thread
990 			pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
991 
992 			// Assign the thread to properties
993 			mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
994 
995 			// Set the running state
996 			mlt_properties_set_int( properties, "running", 1 );
997 			mlt_properties_set_int( properties, "joined", 0 );
998 
999 			// Create the thread
1000 			pthread_create( thread, NULL, consumer_thread, consumer );
1001 		}
1002 	}
1003 	else
1004 	{
1005 		output_xml( consumer );
1006 		mlt_consumer_stop( consumer );
1007 		mlt_consumer_stopped( consumer );
1008 	}
1009 	return 0;
1010 }
1011 
consumer_is_stopped(mlt_consumer consumer)1012 static int consumer_is_stopped( mlt_consumer consumer )
1013 {
1014 	mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
1015 	return !mlt_properties_get_int( properties, "running" );
1016 }
1017 
consumer_stop(mlt_consumer consumer)1018 static int consumer_stop( mlt_consumer consumer )
1019 {
1020 	// Get the properties
1021 	mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
1022 
1023 	// Check that we're running
1024 	if ( !mlt_properties_get_int( properties, "joined" ) )
1025 	{
1026 		// Get the thread
1027 		pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
1028 
1029 		// Stop the thread
1030 		mlt_properties_set_int( properties, "running", 0 );
1031 		mlt_properties_set_int( properties, "joined", 1 );
1032 
1033 		// Wait for termination
1034 		if ( thread )
1035 			pthread_join( *thread, NULL );
1036 	}
1037 
1038 	return 0;
1039 }
1040 
consumer_thread(void * arg)1041 static void *consumer_thread( void *arg )
1042 {
1043 	// Map the argument to the object
1044 	mlt_consumer consumer = arg;
1045 
1046 	// Get the properties
1047 	mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
1048 
1049 	// Convenience functionality
1050 	int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
1051 	int terminated = 0;
1052 
1053 	// Frame and size
1054 	mlt_frame frame = NULL;
1055 
1056 	int video_off = mlt_properties_get_int( properties, "video_off" );
1057 	int audio_off = mlt_properties_get_int( properties, "audio_off" );
1058 
1059 	// Loop while running
1060 	while( !terminated && mlt_properties_get_int( properties, "running" ) )
1061 	{
1062 		// Get the frame
1063 		frame = mlt_consumer_rt_frame( consumer );
1064 
1065 		// Check for termination
1066 		if ( terminate_on_pause && frame != NULL )
1067 			terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
1068 
1069 		// Check that we have a frame to work with
1070 		if ( frame )
1071 		{
1072 			int width = 0, height = 0;
1073 			int frequency = mlt_properties_get_int( properties, "frequency" );
1074 			int channels = mlt_properties_get_int( properties, "channels" );
1075 			float fps = mlt_profile_fps( mlt_service_profile( MLT_CONSUMER_SERVICE( consumer ) ) );
1076 			int samples = mlt_audio_calculate_frame_samples( fps, frequency, mlt_frame_get_position( frame ) );
1077 			mlt_image_format iformat = mlt_image_yuv422;
1078 			mlt_audio_format aformat = mlt_audio_s16;
1079 			uint8_t *buffer;
1080 
1081 			if ( !video_off )
1082 				mlt_frame_get_image( frame, &buffer, &iformat, &width, &height, 0 );
1083 			if ( !audio_off )
1084 				mlt_frame_get_audio( frame, (void**) &buffer, &aformat, &frequency, &channels, &samples );
1085 
1086 			// Close the frame
1087 			mlt_events_fire( properties, "consumer-frame-show", mlt_event_data_from_frame(frame) );
1088 			mlt_frame_close( frame );
1089 		}
1090 	}
1091 	output_xml( consumer );
1092 
1093 	// Indicate that the consumer is stopped
1094 	mlt_properties_set_int( properties, "running", 0 );
1095 	mlt_consumer_stopped( consumer );
1096 
1097 	return NULL;
1098 }
1099 
consumer_close(mlt_consumer consumer)1100 static void consumer_close( mlt_consumer consumer )
1101 {
1102 	// Stop the consumer
1103 	mlt_consumer_stop( consumer );
1104 
1105 	// Close the parent
1106 	mlt_consumer_close( consumer );
1107 
1108 	// Free the memory
1109 	free( consumer );
1110 }
1111