8d6247d40d1ca2c76ee1f33586bb0c104d3444a2
[melted] / mlt / src / framework / mlt_tractor.c
1 /*
2 * mlt_tractor.c -- tractor service class
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
23 #include "mlt_tractor.h"
24 #include "mlt_frame.h"
25 #include "mlt_multitrack.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 /** Private structure.
31 */
32
33 struct mlt_tractor_s
34 {
35 struct mlt_producer_s parent;
36 mlt_service producer;
37 };
38
39 /** Forward references to static methods.
40 */
41
42 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int track );
43
44 /** Constructor for the tractor.
45
46 TODO: thread this service...
47 */
48
49 mlt_tractor mlt_tractor_init( )
50 {
51 mlt_tractor this = calloc( sizeof( struct mlt_tractor_s ), 1 );
52 if ( this != NULL )
53 {
54 mlt_producer producer = &this->parent;
55 if ( mlt_producer_init( producer, this ) == 0 )
56 {
57 producer->get_frame = producer_get_frame;
58 }
59 else
60 {
61 free( this );
62 this = NULL;
63 }
64 }
65 return this;
66 }
67
68 /** Get the service object associated to the tractor.
69 */
70
71 mlt_service mlt_tractor_service( mlt_tractor this )
72 {
73 return mlt_producer_service( &this->parent );
74 }
75
76 /** Get the producer object associated to the tractor.
77 */
78
79 mlt_producer mlt_tractor_producer( mlt_tractor this )
80 {
81 return &this->parent;
82 }
83
84 /** Get the properties object associated to the tractor.
85 */
86
87 mlt_properties mlt_tractor_properties( mlt_tractor this )
88 {
89 return mlt_producer_properties( &this->parent );
90 }
91
92 /** Connect the tractor.
93 */
94
95 int mlt_tractor_connect( mlt_tractor this, mlt_service producer )
96 {
97 int ret = mlt_service_connect_producer( mlt_tractor_service( this ), producer, 0 );
98
99 // This is the producer we're going to connect to
100 if ( ret == 0 )
101 this->producer = producer;
102
103 return ret;
104 }
105
106 /** Get the next frame.
107
108 TODO: This should be reading a pump being populated by the thread...
109 */
110
111 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int track )
112 {
113 mlt_tractor this = parent->child;
114
115 // We only respond to the first track requests
116 if ( track == 0 && this->producer != NULL )
117 {
118 int i = 0;
119 int looking = 1;
120 int done = 0;
121 mlt_frame temp = NULL;
122
123 // Get the properties of the parent producer
124 mlt_properties properties = mlt_producer_properties( parent );
125
126 // Try to obtain the multitrack associated to the tractor
127 mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
128
129 // If we don't have one, we're in trouble...
130 if ( multitrack != NULL )
131 {
132 mlt_producer target = mlt_multitrack_producer( multitrack );
133 mlt_producer_seek( target, mlt_producer_frame( parent ) );
134 mlt_producer_set_speed( target, mlt_producer_get_speed( parent ) );
135 mlt_producer_set_in_and_out( parent, mlt_producer_get_in( target ), mlt_producer_get_out( target ) );
136 }
137 else
138 {
139 fprintf( stderr, "tractor without a multitrack!!\n" );
140 }
141
142 // Loop through each of the tracks we're harvesting
143 for ( i = 0; !done; i ++ )
144 {
145 // Get a frame from the producer
146 mlt_service_get_frame( this->producer, &temp, i );
147
148 // Check for last track
149 done = mlt_properties_get_int( mlt_frame_properties( temp ), "last_track" );
150
151 // Handle the frame
152 if ( done && looking )
153 {
154 // Use this as output if we don't have one already
155 *frame = temp;
156 }
157 else if ( ( !mlt_frame_is_test_card( temp ) || !mlt_frame_is_test_audio( temp ) ) &&
158 mlt_producer_frame( parent ) == mlt_properties_get_position( mlt_frame_properties( temp ), "position" ) )
159 {
160 *frame = temp;
161 looking = 0;
162 }
163 else if ( ( !mlt_frame_is_test_card( temp ) || !mlt_frame_is_test_audio( temp ) ) && looking )
164 {
165 // This is the one we want and we can stop looking
166 *frame = temp;
167 looking = 0;
168 }
169 else
170 {
171 // We discard all other frames
172 mlt_frame_close( temp );
173 }
174 }
175
176 // Prepare the next frame
177 mlt_producer_prepare_next( parent );
178
179 // Indicate our found status
180 return 0;
181 }
182 else
183 {
184 // Generate a test card
185 *frame = mlt_frame_init( );
186 return 0;
187 }
188 }
189
190 /** Close the tractor.
191 */
192
193 void mlt_tractor_close( mlt_tractor this )
194 {
195 mlt_producer_close( &this->parent );
196 free( this );
197 }
198