MediaConnection.h revision 9ab4c384
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, implement it to return the buffer size
53	// you decided for this connection.
54	// TODO: Do we want this (and ChainSize) moved on the output side?
55	// Or perhaps provide an implementation based on the buffer group
56	// for the consumer?
57	// Problem is: the consumer has not easy access to the buffer group,
58	// so we way want to add a special messaging between clients after
59	// connection, so that inputs know the buffer size and chain size.
60	virtual size_t					BufferSize() const = 0;
61	// Implement it to specify the size of your chain of buffers.
62	//virtual int32					ChainSize() const = 0;
63
64	// Disconnect this connection. When a connection is disconnected,
65	// it can be reused as brand new.
66	status_t						Disconnect();
67
68	// Once you are done with this connection you release it, it automatically
69	// remove the object from the BMediaClient and free all used resources.
70	// This will make the connection to disappear completely, so if you
71	// want to preserve it for future connections just Disconnect() it.
72	status_t						Release();
73
74protected:
75									BMediaConnection(
76										media_connection_kinds kinds);
77	virtual							~BMediaConnection();
78
79private:
80	// Those callbacks are shared between BMediaInput and BMediaOutput
81	virtual void					Connected(const media_format& format);
82	virtual void					Disconnected();
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	bool							fConnected;
100
101	virtual	void					_ReservedMediaConnection0();
102	virtual	void					_ReservedMediaConnection1();
103	virtual	void					_ReservedMediaConnection2();
104	virtual	void					_ReservedMediaConnection3();
105	virtual	void					_ReservedMediaConnection4();
106	virtual	void					_ReservedMediaConnection5();
107	virtual	void					_ReservedMediaConnection6();
108	virtual	void					_ReservedMediaConnection7();
109	virtual	void					_ReservedMediaConnection8();
110	virtual	void					_ReservedMediaConnection9();
111	virtual	void					_ReservedMediaConnection10();
112	uint32							fPadding[64];
113
114	friend class BMediaClient;
115	friend class BMediaClientNode;
116
117	friend class BMediaInput;
118	friend class BMediaOutput;
119};
120
121
122class BMediaInput : public virtual BMediaConnection {
123protected:
124									BMediaInput();
125	virtual							~BMediaInput();
126
127	// Callbacks
128	virtual status_t				FormatChanged(const media_format& format);
129
130	virtual void					HandleBuffer(BBuffer* buffer);
131
132	virtual void					Connected(const media_format& format);
133	virtual void					Disconnected();
134
135private:
136	media_input						_MediaInput() const;
137
138	virtual	void					_ReservedMediaInput0();
139	virtual	void					_ReservedMediaInput1();
140	virtual	void					_ReservedMediaInput2();
141	virtual	void					_ReservedMediaInput3();
142	virtual	void					_ReservedMediaInput4();
143	virtual	void					_ReservedMediaInput5();
144	virtual	void					_ReservedMediaInput6();
145	virtual	void					_ReservedMediaInput7();
146	virtual	void					_ReservedMediaInput8();
147	virtual	void					_ReservedMediaInput9();
148	virtual	void					_ReservedMediaInput10();
149	uint32							fPadding[32];
150
151	friend class BMediaClientNode;
152};
153
154
155class BMediaOutput : public virtual BMediaConnection {
156protected:
157									BMediaOutput();
158	virtual							~BMediaOutput();
159
160	// Callbacks
161	virtual status_t				PrepareToConnect(media_format* format);
162
163	virtual status_t				FormatProposal(media_format* format);
164
165	// TODO: Do we really want this?
166	virtual status_t				FormatChangeRequested(media_format* format);
167
168	// When a connection is not binded with another, and you really don't want
169	// to use BMediaGraph it's your job to send the buffer to the connection
170	// you want. You might want to ovverride it so that you can track something,
171	// in this case be sure to call the base version. Be sure to know what
172	// you are doing.
173	virtual	status_t				SendBuffer(BBuffer* buffer);
174
175	virtual void					Connected(const media_format& format);
176	virtual void					Disconnected();
177
178private:
179	media_output					_MediaOutput() const;
180
181	// TODO: possibly unneeded.
182	void							_SetEnabled(bool enabled);
183	bool							_IsEnabled() const;
184
185	bool							fEnabled;
186	size_t							fFramesSent;
187
188	BBufferGroup*					fBufferGroup;
189
190	virtual	void					_ReservedMediaOutput0();
191	virtual	void					_ReservedMediaOutput1();
192	virtual	void					_ReservedMediaOutput2();
193	virtual	void					_ReservedMediaOutput3();
194	virtual	void					_ReservedMediaOutput4();
195	virtual	void					_ReservedMediaOutput5();
196	virtual	void					_ReservedMediaOutput6();
197	virtual	void					_ReservedMediaOutput7();
198	virtual	void					_ReservedMediaOutput8();
199	virtual	void					_ReservedMediaOutput9();
200	virtual	void					_ReservedMediaOutput10();
201	uint32							fPadding[32];
202
203	friend class BMediaClientNode;
204};
205
206
207}
208
209}
210
211using namespace BPrivate::media;
212
213#endif
214