92a95fe18ce70f35a26c5ac7431a76f60a2a00e8
[melted] / src / framework / mlt_consumer.c
1 /*
2 * mlt_consumer.c -- abstraction for all consumer services
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 #include "config.h"
22 #include "mlt_consumer.h"
23 #include "mlt_factory.h"
24 #include "mlt_producer.h"
25 #include "mlt_frame.h"
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 /** Public final methods
31 */
32
33 int mlt_consumer_init( mlt_consumer this, void *child )
34 {
35 int error = 0;
36 memset( this, 0, sizeof( struct mlt_consumer_s ) );
37 this->child = child;
38 error = mlt_service_init( &this->parent, this );
39 if ( error == 0 )
40 {
41 // Get the properties from the service
42 mlt_properties properties = mlt_service_properties( &this->parent );
43
44 // Get the normalisation preference
45 char *normalisation = getenv( "MLT_NORMALISATION" );
46
47 // Deal with normalisation
48 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
49 {
50 mlt_properties_set( properties, "normalisation", "PAL" );
51 mlt_properties_set_double( properties, "fps", 25.0 );
52 mlt_properties_set_int( properties, "width", 720 );
53 mlt_properties_set_int( properties, "height", 576 );
54 mlt_properties_set_int( properties, "progressive", 0 );
55 }
56 else
57 {
58 mlt_properties_set( properties, "normalisation", "NTSC" );
59 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
60 mlt_properties_set_int( properties, "width", 720 );
61 mlt_properties_set_int( properties, "height", 480 );
62 mlt_properties_set_int( properties, "progressive", 0 );
63 }
64 mlt_properties_set_double( properties, "aspect_ratio", 4.0 / 3.0 );
65
66 // Default rescaler for all consumers
67 mlt_properties_set( properties, "rescale", "bilinear" );
68 }
69 return error;
70 }
71
72 /** Get the parent service object.
73 */
74
75 mlt_service mlt_consumer_service( mlt_consumer this )
76 {
77 return &this->parent;
78 }
79
80 /** Get the consumer properties.
81 */
82
83 mlt_properties mlt_consumer_properties( mlt_consumer this )
84 {
85 return mlt_service_properties( &this->parent );
86 }
87
88 /** Connect the consumer to the producer.
89 */
90
91 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
92 {
93 return mlt_service_connect_producer( &this->parent, producer, 0 );
94 }
95
96 /** Start the consumer.
97 */
98
99 int mlt_consumer_start( mlt_consumer this )
100 {
101 // Get the properies
102 mlt_properties properties = mlt_consumer_properties( this );
103
104 // Determine if there's a test card producer
105 char *test_card = mlt_properties_get( properties, "test_card" );
106
107 // Deal with it now.
108 if ( test_card != NULL )
109 {
110 // Create a test card producer
111 // TODO: do we want to use fezzik here?
112 mlt_producer producer = mlt_factory_producer( "fezzik", test_card );
113
114 // Do we have a producer
115 if ( producer != NULL )
116 {
117 // Set the test card on the consumer
118 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
119 }
120 }
121
122 // Start the service
123 if ( this->start != NULL )
124 return this->start( this );
125
126 return 0;
127 }
128
129 /** Protected method :-/ for consumer to get frames from connected service
130 */
131
132 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
133 {
134 // Frame to return
135 mlt_frame frame = NULL;
136
137 // Get the service assoicated to the consumer
138 mlt_service service = mlt_consumer_service( this );
139
140 // Get the frame
141 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
142 {
143 // Get the consumer properties
144 mlt_properties properties = mlt_consumer_properties( this );
145
146 // Get the frame properties
147 mlt_properties frame_properties = mlt_frame_properties( frame );
148
149 // Attach the test frame producer to it.
150 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
151 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
152
153 // Attach the rescale property
154 if ( mlt_properties_get( properties, "rescale" ) != NULL )
155 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
156
157 // TODO: Aspect ratio and other jiggery pokery
158 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
159
160 }
161
162 // Return the frame
163 return frame;
164 }
165
166 /** Stop the consumer.
167 */
168
169 int mlt_consumer_stop( mlt_consumer this )
170 {
171 // Get the properies
172 mlt_properties properties = mlt_consumer_properties( this );
173
174 // Stop the consumer
175 if ( this->stop != NULL )
176 return this->stop( this );
177
178 // Kill the test card
179 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
180
181 return 0;
182 }
183
184 /** Determine if the consumer is stopped.
185 */
186
187 int mlt_consumer_is_stopped( mlt_consumer this )
188 {
189 // Stop the consumer
190 if ( this->is_stopped != NULL )
191 return this->is_stopped( this );
192
193 return 0;
194 }
195
196 /** Close the consumer.
197 */
198
199 void mlt_consumer_close( mlt_consumer this )
200 {
201 // Get the childs close function
202 void ( *consumer_close )( ) = this->close;
203
204 // Make sure it only gets called once
205 this->close = NULL;
206
207 // Call the childs close if available
208 if ( consumer_close != NULL )
209 consumer_close( this );
210 else
211 mlt_service_close( &this->parent );
212 }
213