]> git.sesse.net Git - mlt/blob - src/modules/core/consumer_multi.c
add multi consumer (non-functional)
[mlt] / src / modules / core / consumer_multi.c
1 /*
2  * Copyright (C) 2011 Ushodaya Enterprises Limited
3  * Author: Dan Dennedy <dan@dennedy.org>
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <framework/mlt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <pthread.h>
25
26 // Forward references
27 static int start( mlt_consumer consumer );
28 static int stop( mlt_consumer consumer );
29 static int is_stopped( mlt_consumer consumer );
30 static void *consumer_thread( void *arg );
31 static void consumer_close( mlt_consumer consumer );
32
33 /** Initialise the consumer.
34 */
35
36 mlt_consumer consumer_multi_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
37 {
38         mlt_consumer consumer = mlt_consumer_new( profile );
39
40         if ( consumer )
41         {
42                 // Assign callbacks
43                 consumer->close = consumer_close;
44                 consumer->start = start;
45                 consumer->stop = stop;
46                 consumer->is_stopped = is_stopped;
47         }
48
49         return consumer;
50 }
51
52 /** Start the consumer.
53 */
54
55 static int start( mlt_consumer consumer )
56 {
57         // Check that we're not already running
58         if ( is_stopped( consumer ) )
59         {
60                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
61                 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
62
63                 // Assign the thread to properties with automatic dealloc
64                 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
65
66                 // Set the running state
67                 mlt_properties_set_int( properties, "running", 1 );
68
69                 // Create the thread
70                 pthread_create( thread, NULL, consumer_thread, consumer );
71         }
72         return 0;
73 }
74
75 /** Stop the consumer.
76 */
77
78 static int stop( mlt_consumer consumer )
79 {
80         // Check that we're running
81         if ( !is_stopped( consumer ) )
82         {
83                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
84                 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
85
86                 // Stop the thread
87                 mlt_properties_set_int( properties, "running", 0 );
88
89                 // Wait for termination
90                 if ( thread )
91                         pthread_join( *thread, NULL );
92         }
93
94         return 0;
95 }
96
97 /** Determine if the consumer is stopped.
98 */
99
100 static int is_stopped( mlt_consumer consumer )
101 {
102         return !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "running" );
103 }
104
105 /** The main thread - the argument is simply the consumer.
106 */
107
108 static void *consumer_thread( void *arg )
109 {
110         mlt_consumer consumer = arg;
111         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
112         mlt_frame frame = NULL;
113
114         // Determine whether to stop at end-of-media
115         int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
116         int terminated = 0;
117
118         // Loop while running
119         while ( !terminated && mlt_properties_get_int( properties, "running" ) )
120         {
121                 // Get the next frame
122                 frame = mlt_consumer_rt_frame( consumer );
123
124                 // Check for termination
125                 if ( terminate_on_pause && frame )
126                         terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
127
128                 // Check that we have a frame to work with
129                 if ( frame )
130                 {
131                         // Close the frame
132                         mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
133                         mlt_frame_close( frame );
134                 }
135         }
136
137         // Indicate that the consumer is stopped
138         mlt_properties_set_int( properties, "running", 0 );
139         mlt_consumer_stopped( consumer );
140
141         return NULL;
142 }
143
144 /** Close the consumer.
145 */
146
147 static void consumer_close( mlt_consumer consumer )
148 {
149         mlt_consumer_stop( consumer );
150         // Close the parent
151         mlt_consumer_close( consumer );
152         free( consumer );
153 }