Server customisation
[melted] / mlt++ / test / server.cpp
1 #include <iostream>
2 #include <string>
3 #include <sstream>
4 using namespace std;
5
6 #include <Mlt.h>
7 using namespace Mlt;
8
9 class Custom : public Miracle
10 {
11 private:
12 Event *event;
13
14 public:
15 Custom( char *name = "Custom", int port = 5290, char *config = NULL ) :
16 Miracle( name, port, config ),
17 event( NULL )
18 {
19 // Ensure that we receive the westley document before it's deserialised
20 set( "push-parser-off", 1 );
21 }
22
23 virtual ~Custom( )
24 {
25 delete event;
26 }
27
28 // Optional step - receive the westley document and do something with it
29 Response *received( char *command, char *document )
30 {
31 cerr << document << endl;
32 Producer producer( "westley-xml", document );
33 return push( command, &producer );
34 }
35
36 // Push handling - clear the playlist, append, seek to beginning and play
37 Response *push( char *command, Service *service )
38 {
39 Playlist playlist( ( mlt_playlist )( unit( 0 )->get_data( "playlist" ) ) );
40 Producer producer( *service );
41 if ( producer.is_valid( ) && playlist.is_valid( ) )
42 {
43 playlist.lock( );
44 playlist.clear( );
45 playlist.append( producer );
46 playlist.seek( 0 );
47 playlist.set_speed( 1 );
48 playlist.unlock( );
49 return new Response( 200, "OK" );
50 }
51 return new Response( 400, "Invalid" );
52 }
53
54 // Custom command execution
55 Response *execute( char *command )
56 {
57 Response *response = NULL;
58
59 if ( !strcmp( command, "debug" ) )
60 {
61 // Example of a custom command
62 response = new Response( 200, "Diagnostics output" );
63 for( int i = 0; unit( i ) != NULL; i ++ )
64 {
65 Properties *properties = unit( i );
66 stringstream output;
67 output << string( "Unit " ) << i << endl;
68 for ( int j = 0; j < properties->count( ); j ++ )
69 output << properties->get_name( j ) << " = " << properties->get( j ) << endl;
70 response->write( output.str( ).c_str( ) );
71 }
72 }
73 else
74 {
75 // Use the default command processing
76 response = Miracle::execute( command );
77 }
78
79 // If no event exists and the first unit has been added...
80 if ( event == NULL && unit( 0 ) != NULL )
81 {
82 // Set up the event handling
83 Consumer consumer( ( mlt_consumer )( unit( 0 )->get_data( "consumer" ) ) );
84 event = consumer.listen( "consumer-frame-render", this, ( mlt_listener )frame_render );
85
86 // In this custom case, we'll loop everything on the unit
87 Playlist playlist( ( mlt_playlist )( unit( 0 )->get_data( "playlist" ) ) );
88 playlist.set( "eof", "loop" );
89 }
90
91 return response;
92 }
93
94 // Callback for frame render notification
95 static void frame_render( mlt_consumer consumer, Custom *self, mlt_frame frame_ptr )
96 {
97 Frame frame( frame_ptr );
98 self->frame_render_event( frame );
99 }
100
101 // Do something to the frame here
102 void frame_render_event( Frame &frame )
103 {
104 }
105
106 };
107
108 int main( int argc, char **argv )
109 {
110 Custom server( "Server" );
111 server.start( );
112 server.execute( "uadd sdl" );
113 server.execute( "play u0" );
114 server.wait_for_shutdown( );
115 return 0;
116 }
117