]> git.sesse.net Git - mlt/blob - src/modules/core/consumer_null.c
ef1f95403ea0d1d0c94e85498ba646c2dfe8a0af
[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 // Local header files
22 #include "consumer_null.h"
23
24 // mlt Header files
25 #include <framework/mlt_frame.h>
26
27 // System header files
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <pthread.h>
32
33 // Forward references.
34 static int consumer_start( mlt_consumer this );
35 static int consumer_stop( mlt_consumer this );
36 static int consumer_is_stopped( mlt_consumer this );
37 static void *consumer_thread( void *arg );
38 static void consumer_close( mlt_consumer this );
39
40 /** Initialise the dv consumer.
41 */
42
43 mlt_consumer consumer_null_init( char *arg )
44 {
45         // Allocate the consumer
46         mlt_consumer this = mlt_consumer_new( );
47
48         // If memory allocated and initialises without error
49         if ( this != NULL )
50         {
51                 // Assign close callback
52                 this->close = consumer_close;
53
54                 // Set up start/stop/terminated callbacks
55                 this->start = consumer_start;
56                 this->stop = consumer_stop;
57                 this->is_stopped = consumer_is_stopped;
58         }
59
60         // Return this
61         return this;
62 }
63
64 /** Start the consumer.
65 */
66
67 static int consumer_start( mlt_consumer this )
68 {
69         // Get the properties
70         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
71
72         // Check that we're not already running
73         if ( !mlt_properties_get_int( properties, "running" ) )
74         {
75                 // Allocate a thread
76                 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
77
78                 // Assign the thread to properties
79                 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
80
81                 // Set the running state
82                 mlt_properties_set_int( properties, "running", 1 );
83                 mlt_properties_set_int( properties, "joined", 0 );
84
85                 // Create the thread
86                 pthread_create( thread, NULL, consumer_thread, this );
87         }
88         return 0;
89 }
90
91 /** Stop the consumer.
92 */
93
94 static int consumer_stop( mlt_consumer this )
95 {
96         // Get the properties
97         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
98
99         // Check that we're running
100         if ( !mlt_properties_get_int( properties, "joined" ) )
101         {
102                 // Get the thread
103                 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
104
105                 // Stop the thread
106                 mlt_properties_set_int( properties, "running", 0 );
107                 mlt_properties_set_int( properties, "joined", 1 );
108
109                 // Wait for termination
110                 pthread_join( *thread, NULL );
111         }
112
113         return 0;
114 }
115
116 /** Determine if the consumer is stopped.
117 */
118
119 static int consumer_is_stopped( mlt_consumer this )
120 {
121         // Get the properties
122         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
123         return !mlt_properties_get_int( properties, "running" );
124 }
125
126 /** The main thread - the argument is simply the consumer.
127 */
128
129 static void *consumer_thread( void *arg )
130 {
131         // Map the argument to the object
132         mlt_consumer this = arg;
133
134         // Get the properties
135         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
136
137         // Convenience functionality
138         int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
139         int terminated = 0;
140
141         // Frame and size
142         mlt_frame frame = NULL;
143
144         // Loop while running
145         while( !terminated && mlt_properties_get_int( properties, "running" ) )
146         {
147                 // Get the frame
148                 frame = mlt_consumer_rt_frame( this );
149
150                 // Check for termination
151                 if ( terminate_on_pause && frame != NULL )
152                         terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
153
154                 // Check that we have a frame to work with
155                 if ( frame != NULL )
156                 {
157                         // Close the frame
158                         mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
159                         mlt_frame_close( frame );
160                 }
161         }
162
163         // Indicate that the consumer is stopped
164         mlt_properties_set_int( properties, "running", 0 );
165         mlt_consumer_stopped( this );
166
167         return NULL;
168 }
169
170 /** Close the consumer.
171 */
172
173 static void consumer_close( mlt_consumer this )
174 {
175         // Stop the consumer
176         mlt_consumer_stop( this );
177
178         // Close the parent
179         mlt_consumer_close( this );
180
181         // Free the memory
182         free( this );
183 }