1 /**
2  * MltConsumer.cpp - MLT Wrapper
3  * Copyright (C) 2004-2019 Meltytech, LLC
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include <stdlib.h>
21 #include <string.h>
22 #include "MltConsumer.h"
23 #include "MltEvent.h"
24 #include "MltProfile.h"
25 using namespace Mlt;
26 
Consumer()27 Consumer::Consumer( ) :
28 	instance( NULL )
29 {
30 	instance = mlt_factory_consumer( NULL, NULL, NULL );
31 }
32 
Consumer(Profile & profile)33 Consumer::Consumer( Profile& profile ) :
34 	instance( NULL )
35 {
36 	instance = mlt_factory_consumer( profile.get_profile(), NULL, NULL );
37 }
38 
Consumer(Profile & profile,const char * id,const char * arg)39 Consumer::Consumer( Profile& profile, const char *id, const char *arg ) :
40 	Consumer ( profile.get_profile(), id, arg )
41 {
42 }
43 
Consumer(mlt_profile profile,const char * id,const char * arg)44 Consumer::Consumer( mlt_profile profile, const char *id, const char *arg ) :
45 	instance( NULL )
46 {
47 	if ( id == NULL || arg != NULL )
48 	{
49 		instance = mlt_factory_consumer( profile, id, arg );
50 	}
51 	else
52 	{
53 		if ( strchr( id, ':' ) )
54 		{
55 			char *temp = strdup( id );
56 			char *arg = strchr( temp, ':' ) + 1;
57 			*( arg - 1 ) = '\0';
58 			instance = mlt_factory_consumer( profile, temp, arg );
59 			free( temp );
60 		}
61 		else
62 		{
63 			instance = mlt_factory_consumer( profile, id, NULL );
64 		}
65 	}
66 }
67 
Consumer(Service & consumer)68 Consumer::Consumer( Service &consumer ) :
69 	instance( NULL )
70 {
71 	if ( consumer.type( ) == mlt_service_consumer_type )
72 	{
73 		instance = ( mlt_consumer )consumer.get_service( );
74 		inc_ref( );
75 	}
76 }
77 
Consumer(Consumer & consumer)78 Consumer::Consumer( Consumer &consumer ) :
79 	Mlt::Service( consumer ),
80 	instance( consumer.get_consumer( ) )
81 {
82 	inc_ref( );
83 }
84 
Consumer(mlt_consumer consumer)85 Consumer::Consumer( mlt_consumer consumer ) :
86 	instance( consumer )
87 {
88 	inc_ref( );
89 }
90 
~Consumer()91 Consumer::~Consumer( )
92 {
93 	mlt_consumer_close( instance );
94 }
95 
get_consumer()96 mlt_consumer Consumer::get_consumer( )
97 {
98 	return instance;
99 }
100 
get_service()101 mlt_service Consumer::get_service( )
102 {
103 	return mlt_consumer_service( get_consumer( ) );
104 }
105 
connect(Service & service)106 int Consumer::connect( Service &service )
107 {
108 	return connect_producer( service );
109 }
110 
start()111 int Consumer::start( )
112 {
113 	return mlt_consumer_start( get_consumer( ) );
114 }
115 
purge()116 void Consumer::purge( )
117 {
118 	mlt_consumer_purge( get_consumer( ) );
119 }
120 
stop()121 int Consumer::stop( )
122 {
123 	return mlt_consumer_stop( get_consumer( ) );
124 }
125 
is_stopped()126 bool Consumer::is_stopped( )
127 {
128 	return mlt_consumer_is_stopped( get_consumer( ) ) != 0;
129 }
130 
run()131 int Consumer::run( )
132 {
133 	int ret = start( );
134 	if ( !is_stopped( ) )
135 	{
136 		Event *e = setup_wait_for( "consumer-stopped" );
137 		wait_for( e );
138 		delete e;
139 	}
140 	return ret;
141 }
142 
position()143 int Consumer::position( )
144 {
145 	return mlt_consumer_position( get_consumer() );
146 }
147