ae456b584e10a3936121ce37f49ca3f6df4692b9
[melted] / 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 program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * 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 pthread_attr_t thread_attributes;
78
79 // Assign the thread to properties
80 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
81
82 // Set the running state
83 mlt_properties_set_int( properties, "running", 1 );
84
85 // Inherit the scheduling priority
86 pthread_attr_init( &thread_attributes );
87 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
88
89 // Create the thread
90 pthread_create( thread, &thread_attributes, consumer_thread, this );
91 }
92 return 0;
93 }
94
95 /** Stop the consumer.
96 */
97
98 static int consumer_stop( mlt_consumer this )
99 {
100 // Get the properties
101 mlt_properties properties = mlt_consumer_properties( this );
102
103 // Check that we're running
104 if ( mlt_properties_get_int( properties, "running" ) )
105 {
106 // Get the thread
107 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
108
109 // Stop the thread
110 mlt_properties_set_int( properties, "running", 0 );
111
112 // Wait for termination
113 pthread_join( *thread, NULL );
114 }
115
116 return 0;
117 }
118
119 /** Determine if the consumer is stopped.
120 */
121
122 static int consumer_is_stopped( mlt_consumer this )
123 {
124 // Get the properties
125 mlt_properties properties = mlt_consumer_properties( this );
126 return !mlt_properties_get_int( properties, "running" );
127 }
128
129 /** The main thread - the argument is simply the consumer.
130 */
131
132 static void *consumer_thread( void *arg )
133 {
134 // Map the argument to the object
135 mlt_consumer this = arg;
136
137 // Get the properties
138 mlt_properties properties = mlt_consumer_properties( this );
139
140 // Frame and size
141 mlt_frame frame = NULL;
142
143 // Loop while running
144 while( mlt_properties_get_int( properties, "running" ) )
145 {
146 // Get the frame
147 frame = mlt_consumer_rt_frame( this );
148
149 // Check that we have a frame to work with
150 if ( frame != NULL )
151 {
152 // Close the frame
153 mlt_frame_close( frame );
154 }
155 }
156
157 return NULL;
158 }
159
160 /** Close the consumer.
161 */
162
163 static void consumer_close( mlt_consumer this )
164 {
165 // Stop the consumer
166 mlt_consumer_stop( this );
167
168 // Close the parent
169 mlt_consumer_close( this );
170
171 // Free the memory
172 free( this );
173 }