xref: /haiku/headers/private/media/experimental/SimpleMediaClient.h (revision 6dc7d854230121fa6287e1dc440a852322443354)
1 /*
2  * Copyright 2015-2018, Dario Casalinuovo. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #ifndef _MEDIA_SIMPLE_CLIENT_H
7 #define _MEDIA_SIMPLE_CLIENT_H
8 
9 #include <MediaClient.h>
10 #include <MediaConnection.h>
11 
12 
13 namespace BPrivate { namespace media {
14 
15 
16 class BSimpleMediaInput;
17 class BSimpleMediaOutput;
18 
19 class BSimpleMediaClient : public BMediaClient {
20 public:
21 	enum notification {
22 		B_WILL_START = 1,			// performance_time
23 		B_WILL_STOP,				// performance_time immediate
24 		B_WILL_SEEK,				// performance_time media_time
25 
26 		B_FORMAT_SUGGESTION,		// media_type type, int32 quality,
27 									// media_format* format
28 	};
29 
30 	typedef void					(*notify_hook)(void* cookie,
31 											notification what,
32 											...);
33 
34 									BSimpleMediaClient(const char* name,
35 										media_type type
36 											= B_MEDIA_UNKNOWN_TYPE,
37 										media_client_kinds
38 											kinds = B_MEDIA_PLAYER
39 												& B_MEDIA_RECORDER);
40 
41 	virtual							~BSimpleMediaClient();
42 
43 	// This is supplied to support generic connections not related
44 	// to a certain destination or source node, however for various ambiguity
45 	// reasons we want the BMediaConnection to be declared as input or output.
46 	// You can pass the object returned by this function to another
47 	// BMediaClient::Connect(), so that it will automatically connect to this node.
48 	virtual BSimpleMediaInput*		BeginInput();
49 	virtual BSimpleMediaOutput*		BeginOutput();
50 
51 			void					SetHook(notify_hook notifyHook = NULL,
52 										void* cookie = NULL);
53 
54 protected:
55 	virtual void					HandleStart(bigtime_t performanceTime);
56 	virtual void					HandleStop(bigtime_t performanceTime);
57 
58 	virtual void					HandleSeek(bigtime_t mediaTime,
59 										bigtime_t performanceTime);
60 
61 	virtual status_t				FormatSuggestion(media_type type,
62 										int32 quality, media_format* format);
63 
64 private:
65 			notify_hook				fNotifyHook;
66 			void*					fNotifyCookie;
67 
68 	virtual	void					_ReservedSimpleMediaClient0();
69 	virtual	void					_ReservedSimpleMediaClient1();
70 	virtual	void					_ReservedSimpleMediaClient2();
71 	virtual	void					_ReservedSimpleMediaClient3();
72 	virtual	void					_ReservedSimpleMediaClient4();
73 	virtual	void					_ReservedSimpleMediaClient5();
74 			uint32					fPadding[32];
75 };
76 
77 
78 class BSimpleMediaConnection : public virtual BMediaConnection {
79 public:
80 	enum notification {
81 		// Inputs
82 		B_INPUT_CONNECTED = 1,
83 		B_INPUT_DISCONNECTED,
84 
85 		B_FORMAT_CHANGED,
86 
87 		// Outputs
88 		B_OUTPUT_CONNECTED,
89 		B_OUTPUT_DISCONNECTED,
90 
91 		B_PREPARE_TO_CONNECT,	// media_format* format, media_source* source,
92 								// char* name
93 
94 		B_FORMAT_PROPOSAL,		// media_format* format
95 		B_ASK_FORMAT_CHANGE,
96 	};
97 
98 	// This function is called when it is the moment to handle a buffer.
99 	typedef void					(*process_hook)(
100 										BMediaConnection* connection,
101 										BBuffer* buffer);
102 
103 	// Used to notify or inquire the client about what to do when certain
104 	// events happen.
105 	typedef status_t				(*notify_hook)(
106 										BMediaConnection* connection,
107 										notification what,
108 										...);
109 
110 			// Use this to set your callbacks.
111 			void					SetHooks(process_hook processHook = NULL,
112 										notify_hook notifyHook = NULL,
113 										void* cookie = NULL);
114 
115 			void*					Cookie() const;
116 
117 	virtual size_t					BufferSize() const;
118 	virtual void					SetBufferSize(size_t bufferSize);
119 
120 			// This allow to specify a format that will be used while
121 			// connecting to another node.
122 			void					SetAcceptedFormat(
123 										const media_format& format);
124 			const media_format&		AcceptedFormat() const;
125 
126 protected:
127 									BSimpleMediaConnection(
128 										media_connection_kinds kinds);
129 	virtual							~BSimpleMediaConnection();
130 
131 			// TODO: move those to private and introduce protected methods
132 			process_hook			fProcessHook;
133 			notify_hook				fNotifyHook;
134 			void*					fBufferCookie;
135 
136 			size_t					fBufferSize;
137 
138 			media_format			fAcceptedFormat;
139 };
140 
141 
142 class BSimpleMediaInput : public BSimpleMediaConnection, public BMediaInput {
143 public:
144 									BSimpleMediaInput();
145 
146 protected:
147 	virtual							~BSimpleMediaInput();
148 
149 	virtual status_t				AcceptFormat(media_format* format);
150 
151 	virtual void					HandleBuffer(BBuffer* buffer);
152 
153 	virtual void					Connected(const media_format& format);
154 	virtual void					Disconnected();
155 };
156 
157 
158 class BSimpleMediaOutput : public BSimpleMediaConnection, public BMediaOutput {
159 public:
160 									BSimpleMediaOutput();
161 
162 protected:
163 	virtual							~BSimpleMediaOutput();
164 
165 	virtual status_t				PrepareToConnect(media_format* format);
166 	virtual status_t				FormatProposal(media_format* format);
167 
168 	virtual void					Connected(const media_format& format);
169 	virtual void					Disconnected();
170 };
171 
172 
173 }
174 
175 }
176 
177 using namespace BPrivate::media;
178 
179 #endif
180