xref: /haiku/headers/private/media/experimental/MediaConnection.h (revision 3995592cdf304335132305e27c40cbb0b1ac46e3)
1 /*
2  * Copyright 2015, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #ifndef _MEDIA_CONNECTION_H
7 #define _MEDIA_CONNECTION_H
8 
9 #include <BufferGroup.h>
10 #include <MediaDefs.h>
11 
12 #include <MediaClient.h>
13 #include <MediaClientDefs.h>
14 
15 
16 namespace BPrivate { namespace media {
17 
18 class BMediaClientNode;
19 
20 
21 // The BMediaConnection class is the swiss knife of BMediaClient.
22 // It represents a connection between two nodes and allow to create complex
23 // nodes without dealing with the unneeded complexity. Two local connections,
24 // can be binded, this means that when you will receive a buffer A as input,
25 // the BufferReceived function will be called so that you can process the BBuffer,
26 // and once the function returns the output will be automatically forwarded
27 // to the connection B SendBuffer method.
28 // It's not possible to mix a BMediaInput with a BMediaOutput in the same class.
29 class BMediaConnection {
30 public:
31 	const media_connection&			Connection() const;
32 	BMediaClient*					Client() const;
33 
34 	media_connection_id				Id() const;
35 	const char*						Name() const;
36 
37 	bool							HasBinding() const;
38 	BMediaConnection*				Binding() const;
39 
40 	bool							IsConnected() const;
41 
42 	// This allow to specify a format that will be used while
43 	// connecting to another node.
44 	void							SetAcceptedFormat(
45 										const media_format& format);
46 	const media_format&				AcceptedFormat() const;
47 
48 	// Represents the buffer size, depends on the format set or negotiated
49 	// for this connection.
50 	size_t							BufferSize() const;
51 
52 	// Disconnect this connection. When a connection is disconnected,
53 	// it can be reused as brand new.
54 	status_t						Disconnect();
55 
56 	// Once you are done with this connection you release it, it automatically
57 	// remove the object from the BMediaClient and free all used resources.
58 	// This will make the connection to disappear completely, so if you
59 	// want to preserve it for future connections just Disconnect() it.
60 	status_t						Release();
61 
62 protected:
63 									BMediaConnection(
64 										media_connection_kinds kinds);
65 	virtual							~BMediaConnection();
66 
67 	// Those callbacks are shared between BMediaInput and BMediaOutput
68 	virtual void					Connected(const media_format& format);
69 	virtual void					Disconnected();
70 
71 	// Specify a latency range to allow the connection behave correctly.
72 	// Ideally the minimum latency should be the algorithmic latency you expect
73 	// from the node and will be used as starting point. The max latency is the
74 	// maximum acceptable by you, over that point the node will adjust it's
75 	// performance time to recover if a big delay happen.
76 			void					SetLatencyRange(bigtime_t min,
77 										bigtime_t max);
78 
79 			void					GetLatencyRange(bigtime_t* min,
80 										bigtime_t* max) const;
81 
82 private:
83 
84 			void					_ConnectionRegistered(BMediaClient* owner,
85 										media_connection_id id);
86 
87 	const media_source&				_Source() const;
88 	const media_destination&		_Destination() const;
89 
90 	media_connection				fConnection;
91 
92 	BMediaClient*					fOwner;
93 
94 	// A connection might be binded so that it will automatically
95 	// forward or receive the data from/to a local BMediaConnection,
96 	// see BMediaClient::Bind.
97 	BMediaConnection*				fBind;
98 
99 	BBufferGroup*					fBufferGroup;
100 
101 	bool							fConnected;
102 
103 	bigtime_t						fMinLatency;
104 	bigtime_t						fMaxLatency;
105 
106 	virtual	void					_ReservedMediaConnection0();
107 	virtual	void					_ReservedMediaConnection1();
108 	virtual	void					_ReservedMediaConnection2();
109 	virtual	void					_ReservedMediaConnection3();
110 	virtual	void					_ReservedMediaConnection4();
111 	virtual	void					_ReservedMediaConnection5();
112 	virtual	void					_ReservedMediaConnection6();
113 	virtual	void					_ReservedMediaConnection7();
114 	virtual	void					_ReservedMediaConnection8();
115 	virtual	void					_ReservedMediaConnection9();
116 	virtual	void					_ReservedMediaConnection10();
117 	uint32							fPadding[64];
118 
119 	friend class BMediaClient;
120 	friend class BMediaClientNode;
121 
122 	friend class BMediaInput;
123 	friend class BMediaOutput;
124 };
125 
126 
127 class BMediaInput : public virtual BMediaConnection {
128 public:
129 									BMediaInput();
130 
131 protected:
132 	virtual							~BMediaInput();
133 
134 	// Callbacks
135 	virtual status_t				FormatChanged(const media_format& format);
136 
137 	virtual void					HandleBuffer(BBuffer* buffer);
138 
139 private:
140 	media_input						_MediaInput() const;
141 
142 	virtual	void					_ReservedMediaInput0();
143 	virtual	void					_ReservedMediaInput1();
144 	virtual	void					_ReservedMediaInput2();
145 	virtual	void					_ReservedMediaInput3();
146 	virtual	void					_ReservedMediaInput4();
147 	virtual	void					_ReservedMediaInput5();
148 	virtual	void					_ReservedMediaInput6();
149 	virtual	void					_ReservedMediaInput7();
150 	virtual	void					_ReservedMediaInput8();
151 	virtual	void					_ReservedMediaInput9();
152 	virtual	void					_ReservedMediaInput10();
153 	uint32							fPadding[32];
154 
155 	friend class BMediaClientNode;
156 };
157 
158 
159 class BMediaOutput : public virtual BMediaConnection {
160 public:
161 									BMediaOutput();
162 
163 	void							SetEnabled(bool enabled);
164 	bool							IsEnabled() const;
165 
166 protected:
167 	virtual							~BMediaOutput();
168 
169 	// Callbacks
170 	virtual status_t				PrepareToConnect(media_format* format);
171 
172 	virtual status_t				FormatProposal(media_format* format);
173 	virtual status_t				FormatChangeRequested(media_format* format);
174 
175 	// When a connection is not binded with another, and you really don't want
176 	// to use BMediaGraph it's your job to send the buffer to the connection
177 	// you want. You might want to ovverride it so that you can track something,
178 	// in this case be sure to call the base version. Be sure to know what
179 	// you are doing.
180 	virtual	status_t				SendBuffer(BBuffer* buffer);
181 
182 private:
183 	media_output					_MediaOutput() const;
184 
185 	bool							fEnabled;
186 	size_t							fFramesSent;
187 
188 	virtual	void					_ReservedMediaOutput0();
189 	virtual	void					_ReservedMediaOutput1();
190 	virtual	void					_ReservedMediaOutput2();
191 	virtual	void					_ReservedMediaOutput3();
192 	virtual	void					_ReservedMediaOutput4();
193 	virtual	void					_ReservedMediaOutput5();
194 	virtual	void					_ReservedMediaOutput6();
195 	virtual	void					_ReservedMediaOutput7();
196 	virtual	void					_ReservedMediaOutput8();
197 	virtual	void					_ReservedMediaOutput9();
198 	virtual	void					_ReservedMediaOutput10();
199 	uint32							fPadding[32];
200 
201 	friend class BMediaClientNode;
202 };
203 
204 
205 }
206 
207 }
208 
209 using namespace BPrivate::media;
210 
211 #endif
212