2 * producer_consumer.c -- produce as a consumer of an encapsulated producer
3 * Copyright (C) 2008 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
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.
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.
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
21 #include <framework/mlt.h>
30 mlt_producer producer
;
31 mlt_consumer consumer
;
34 typedef struct context_s
*context
;
37 static int get_image( mlt_frame frame
, uint8_t **image
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
39 context cx
= mlt_frame_pop_service( frame
);
40 mlt_frame nested_frame
= mlt_frame_pop_service( frame
);
42 *width
= cx
->profile
->width
;
43 *height
= cx
->profile
->height
;
45 int result
= mlt_frame_get_image( nested_frame
, image
, format
, width
, height
, writable
);
48 int size
= *width
* *height
* ( *format
== mlt_image_yuv422 ?
2 : *format
== mlt_image_rgb24 ?
3 : *format
== mlt_image_rgb24a ?
4 : ( 3 / 2 ) );
49 uint8_t *new_image
= mlt_pool_alloc( size
);
52 mlt_properties properties
= mlt_frame_properties( frame
);
53 mlt_properties_set_data( properties
, "image", new_image
, size
, mlt_pool_release
, NULL
);
54 memcpy( new_image
, *image
, size
);
55 mlt_frame_close( nested_frame
);
58 // mlt_properties_debug( properties, "frame", stderr );
59 // mlt_properties_debug( mlt_frame_properties( nested_frame ), "nested_frame", stderr );
64 static int get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
66 mlt_frame nested_frame
= mlt_frame_pop_audio( frame
);
67 int result
= mlt_frame_get_audio( nested_frame
, buffer
, format
, frequency
, channels
, samples
);
68 int size
= *channels
* *samples
* sizeof( int16_t );
69 int16_t *new_buffer
= mlt_pool_alloc( size
);
70 mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame
), "audio", new_buffer
, size
, mlt_pool_release
, NULL
);
71 memcpy( new_buffer
, *buffer
, size
);
73 mlt_frame_close( nested_frame
);
77 static int get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
)
79 mlt_properties properties
= MLT_PRODUCER_PROPERTIES(this);
80 context cx
= mlt_properties_get_data( properties
, "context", NULL
);
84 // Allocate and initialize our context
85 cx
= mlt_pool_alloc( sizeof( struct context_s
) );
86 mlt_properties_set_data( properties
, "context", cx
, 0, mlt_pool_release
, NULL
);
88 cx
->profile
= mlt_profile_init( mlt_properties_get( properties
, "profile" ) );
90 // For now, we must conform the nested network's frame rate to the parent network's
92 mlt_profile profile
= mlt_service_profile( MLT_PRODUCER_SERVICE( this ) );
93 cx
->profile
->frame_rate_num
= profile
->frame_rate_num
;
94 cx
->profile
->frame_rate_den
= profile
->frame_rate_den
;
96 // We will encapsulate a consumer
97 cx
->consumer
= mlt_consumer_new( cx
->profile
);
98 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( cx
->consumer
), "real_time", 0 );
100 // Encapsulate a real producer for the resource
101 cx
->producer
= mlt_factory_producer( cx
->profile
, mlt_environment( "MLT_PRODUCER" ),
102 mlt_properties_get( properties
, "resource" ) );
103 mlt_properties_pass_list( properties
, MLT_PRODUCER_PROPERTIES( cx
->producer
),
104 "in, out, length, resource" );
106 // Since we control the seeking, prevent it from seeking on its own
107 mlt_producer_set_speed( cx
->producer
, 0 );
109 // Connect it all together
110 mlt_consumer_connect( cx
->consumer
, MLT_PRODUCER_SERVICE( cx
->producer
) );
111 mlt_consumer_start( cx
->consumer
);
115 *frame
= mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
118 // Our "in" needs to be the same, keep it so
119 mlt_properties_pass_list( MLT_PRODUCER_PROPERTIES( cx
->producer
), properties
, "in" );
121 // Seek the producer to the correct place
122 // Calculate our positions
123 double actual_position
= mlt_producer_get_speed( this ) * (double)mlt_producer_position( this );
124 mlt_position need_first
= floor( actual_position
);
125 mlt_producer_seek( cx
->producer
, need_first
);
127 // Get the nested frame
128 mlt_frame nested_frame
= mlt_consumer_rt_frame( cx
->consumer
);
130 // Stack the producer and our methods on the nested frame
131 mlt_frame_push_service( *frame
, nested_frame
);
132 mlt_frame_push_service( *frame
, cx
);
133 mlt_frame_push_get_image( *frame
, get_image
);
134 mlt_frame_push_audio( *frame
, nested_frame
);
135 mlt_frame_push_audio( *frame
, get_audio
);
137 // Give the returned frame temporal identity
138 mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
140 // Put additional references on the frame so both get_image and get_audio
141 // methods can close it.
142 mlt_properties_inc_ref( MLT_FRAME_PROPERTIES( nested_frame
) );
144 // Inform the normalizers about our video properties
145 mlt_properties frame_props
= MLT_FRAME_PROPERTIES( *frame
);
146 mlt_properties_set_double( frame_props
, "aspect_ratio", mlt_profile_sar( cx
->profile
) );
147 mlt_properties_set_double( frame_props
, "width", cx
->profile
->width
);
148 mlt_properties_set_double( frame_props
, "height", cx
->profile
->height
);
151 // Calculate the next timecode
152 mlt_producer_prepare_next( this );
157 static void producer_close( mlt_producer
this )
159 context cx
= mlt_properties_get_data( MLT_PRODUCER_PROPERTIES( this ), "context", NULL
);
161 // Shut down all the encapsulated services
164 mlt_consumer_stop( cx
->consumer
);
165 mlt_consumer_close( cx
->consumer
);
166 mlt_producer_close( cx
->producer
);
171 mlt_producer_close( this );
175 mlt_producer
producer_consumer_init( mlt_profile profile
, mlt_service_type type
, const char *id
, char *arg
)
177 mlt_producer
this = mlt_producer_new( );
179 // Encapsulate the real producer
180 mlt_producer real_producer
= mlt_factory_producer( profile
, mlt_environment( "MLT_PRODUCER" ), arg
);
182 if ( this && real_producer
)
184 // Override some producer methods
185 this->close
= ( mlt_destructor
)producer_close
;
186 this->get_frame
= get_frame
;
188 // Get the properties of this producer
189 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
190 mlt_properties_set( properties
, "resource", arg
);
192 mlt_producer_close( real_producer
);
197 mlt_producer_close( this );
199 mlt_producer_close( real_producer
);