MediaConnection.h revision dc4c49c3
1/*
2 * Copyright 2015-2018, Dario Casalinuovo. 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
16namespace BPrivate { namespace media {
17
18class 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.
29class BMediaConnection {
30public:
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	// If the connection is connected get the other endpoint,
43	// return media_connection::null otherwise.
44	media_connection				Endpoint();
45
46	// This allow to specify a format that will be used while
47	// connecting to another node.
48	void							SetAcceptedFormat(
49										const media_format& format);
50	const media_format&				AcceptedFormat() const;
51
52	// Represents the buffer size, depends on the format set or negotiated
53	// for this connection.
54	size_t							BufferSize() const;
55
56	// Disconnect this connection. When a connection is disconnected,
57	// it can be reused as brand new.
58	status_t						Disconnect();
59
60	// Once you are done with this connection you release it, it automatically
61	// remove the object from the BMediaClient and free all used resources.
62	// This will make the connection to disappear completely, so if you
63	// want to preserve it for future connections just Disconnect() it.
64	status_t						Release();
65
66protected:
67									BMediaConnection(
68										media_connection_kinds kinds);
69	virtual							~BMediaConnection();
70
71	// Those callbacks are shared between BMediaInput and BMediaOutput
72	virtual void					Connected(const media_format& format);
73	virtual void					Disconnected();
74
75	// Specify a latency range to allow the connection behave correctly.
76	// Ideally the minimum latency should be the algorithmic latency you expect
77	// from the node and will be used as starting point. The max latency is the
78	// maximum acceptable by you, over that point the node will adjust it's
79	// performance time to recover if a big delay happen.
80			void					SetLatencyRange(bigtime_t min,
81										bigtime_t max);
82
83			void					GetLatencyRange(bigtime_t* min,
84										bigtime_t* max) const;
85
86private:
87
88			void					_ConnectionRegistered(BMediaClient* owner,
89										media_connection_id id);
90
91	const media_source&				_Source() const;
92	const media_destination&		_Destination() const;
93
94	media_connection				fConnection;
95
96	BMediaClient*					fOwner;
97
98	// A connection might be binded so that it will automatically
99	// forward or receive the data from/to a local BMediaConnection,
100	// see BMediaClient::Bind.
101	BMediaConnection*				fBind;
102
103	BBufferGroup*					fBufferGroup;
104
105	bool							fConnected;
106
107	bigtime_t						fMinLatency;
108	bigtime_t						fMaxLatency;
109
110	virtual	void					_ReservedMediaConnection0();
111	virtual	void					_ReservedMediaConnection1();
112	virtual	void					_ReservedMediaConnection2();
113	virtual	void					_ReservedMediaConnection3();
114	virtual	void					_ReservedMediaConnection4();
115	virtual	void					_ReservedMediaConnection5();
116	virtual	void					_ReservedMediaConnection6();
117	virtual	void					_ReservedMediaConnection7();
118	virtual	void					_ReservedMediaConnection8();
119	virtual	void					_ReservedMediaConnection9();
120	virtual	void					_ReservedMediaConnection10();
121	uint32							fPadding[64];
122
123	friend class BMediaClient;
124	friend class BMediaClientNode;
125
126	friend class BMediaInput;
127	friend class BMediaOutput;
128};
129
130
131class BMediaInput : public virtual BMediaConnection {
132public:
133									BMediaInput();
134
135protected:
136	virtual							~BMediaInput();
137
138	// Callbacks
139	virtual status_t				FormatChanged(const media_format& format);
140
141	virtual void					HandleBuffer(BBuffer* buffer);
142
143private:
144	media_input						_MediaInput() const;
145
146	virtual	void					_ReservedMediaInput0();
147	virtual	void					_ReservedMediaInput1();
148	virtual	void					_ReservedMediaInput2();
149	virtual	void					_ReservedMediaInput3();
150	virtual	void					_ReservedMediaInput4();
151	virtual	void					_ReservedMediaInput5();
152	virtual	void					_ReservedMediaInput6();
153	virtual	void					_ReservedMediaInput7();
154	virtual	void					_ReservedMediaInput8();
155	virtual	void					_ReservedMediaInput9();
156	virtual	void					_ReservedMediaInput10();
157	uint32							fPadding[32];
158
159	friend class BMediaClientNode;
160};
161
162
163class BMediaOutput : public virtual BMediaConnection {
164public:
165									BMediaOutput();
166
167	void							SetEnabled(bool enabled);
168	bool							IsEnabled() const;
169
170protected:
171	virtual							~BMediaOutput();
172
173	// Callbacks
174	virtual status_t				PrepareToConnect(media_format* format);
175
176	virtual status_t				FormatProposal(media_format* format);
177	virtual status_t				FormatChangeRequested(media_format* format);
178
179	// When a connection is not binded with another, and you really don't want
180	// to use BMediaGraph it's your job to send the buffer to the connection
181	// you want. You might want to ovverride it so that you can track something,
182	// in this case be sure to call the base version. Be sure to know what
183	// you are doing.
184	virtual	status_t				SendBuffer(BBuffer* buffer);
185
186private:
187	media_output					_MediaOutput() const;
188
189	bool							fEnabled;
190	size_t							fFramesSent;
191
192	virtual	void					_ReservedMediaOutput0();
193	virtual	void					_ReservedMediaOutput1();
194	virtual	void					_ReservedMediaOutput2();
195	virtual	void					_ReservedMediaOutput3();
196	virtual	void					_ReservedMediaOutput4();
197	virtual	void					_ReservedMediaOutput5();
198	virtual	void					_ReservedMediaOutput6();
199	virtual	void					_ReservedMediaOutput7();
200	virtual	void					_ReservedMediaOutput8();
201	virtual	void					_ReservedMediaOutput9();
202	virtual	void					_ReservedMediaOutput10();
203	uint32							fPadding[32];
204
205	friend class BMediaClientNode;
206};
207
208
209}
210
211}
212
213using namespace BPrivate::media;
214
215#endif
216