]> git.sesse.net Git - mlt/blob - src/modules/core/consumer_null.c
framework: remove global profile, rather share one mlt_profile across a service netwo...
[mlt] / src / modules / core / consumer_null.c
1 /*
2  * consumer_null.c -- a null consumer
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 // mlt Header files
22 #include <framework/mlt_consumer.h>
23 #include <framework/mlt_frame.h>
24
25 // System header files
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <pthread.h>
30
31 // Forward references.
32 static int consumer_start( mlt_consumer this );
33 static int consumer_stop( mlt_consumer this );
34 static int consumer_is_stopped( mlt_consumer this );
35 static void *consumer_thread( void *arg );
36 static void consumer_close( mlt_consumer this );
37
38 /** Initialise the dv consumer.
39 */
40
41 mlt_consumer consumer_null_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
42 {
43         // Allocate the consumer
44         mlt_consumer this = mlt_consumer_new( profile );
45
46         // If memory allocated and initialises without error
47         if ( this != NULL )
48         {
49                 // Assign close callback
50                 this->close = consumer_close;
51
52                 // Set up start/stop/terminated callbacks
53                 this->start = consumer_start;
54                 this->stop = consumer_stop;
55                 this->is_stopped = consumer_is_stopped;
56         }
57
58         // Return this
59         return this;
60 }
61
62 /** Start the consumer.
63 */
64
65 static int consumer_start( mlt_consumer this )
66 {
67         // Get the properties
68         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
69
70         // Check that we're not already running
71         if ( !mlt_properties_get_int( properties, "running" ) )
72         {
73                 // Allocate a thread
74                 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
75
76                 // Assign the thread to properties
77                 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
78
79                 // Set the running state
80                 mlt_properties_set_int( properties, "running", 1 );
81                 mlt_properties_set_int( properties, "joined", 0 );
82
83                 // Create the thread
84                 pthread_create( thread, NULL, consumer_thread, this );
85         }
86         return 0;
87 }
88
89 /** Stop the consumer.
90 */
91
92 static int consumer_stop( mlt_consumer this )
93 {
94         // Get the properties
95         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
96
97         // Check that we're running
98         if ( !mlt_properties_get_int( properties, "joined" ) )
99         {
100                 // Get the thread
101                 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
102
103                 // Stop the thread
104                 mlt_properties_set_int( properties, "running", 0 );
105                 mlt_properties_set_int( properties, "joined", 1 );
106
107                 // Wait for termination
108                 pthread_join( *thread, NULL );
109         }
110
111         return 0;
112 }
113
114 /** Determine if the consumer is stopped.
115 */
116
117 static int consumer_is_stopped( mlt_consumer this )
118 {
119         // Get the properties
120         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
121         return !mlt_properties_get_int( properties, "running" );
122 }
123
124 /** The main thread - the argument is simply the consumer.
125 */
126
127 static void *consumer_thread( void *arg )
128 {
129         // Map the argument to the object
130         mlt_consumer this = arg;
131
132         // Get the properties
133         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
134
135         // Convenience functionality
136         int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
137         int terminated = 0;
138
139         // Frame and size
140         mlt_frame frame = NULL;
141
142         // Loop while running
143         while( !terminated && mlt_properties_get_int( properties, "running" ) )
144         {
145                 // Get the frame
146                 frame = mlt_consumer_rt_frame( this );
147
148                 // Check for termination
149                 if ( terminate_on_pause && frame != NULL )
150                         terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
151
152                 // Check that we have a frame to work with
153                 if ( frame != NULL )
154                 {
155                         // Close the frame
156                         mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
157                         mlt_frame_close( frame );
158                 }
159         }
160
161         // Indicate that the consumer is stopped
162         mlt_properties_set_int( properties, "running", 0 );
163         mlt_consumer_stopped( this );
164
165         return NULL;
166 }
167
168 /** Close the consumer.
169 */
170
171 static void consumer_close( mlt_consumer this )
172 {
173         // Stop the consumer
174         mlt_consumer_stop( this );
175
176         // Close the parent
177         mlt_consumer_close( this );
178
179         // Free the memory
180         free( this );
181 }