MediaConnection.h revision e74e02ac
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	virtual size_t					BufferSize() const = 0;
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
75private:
76
77			void					_ConnectionRegistered(BMediaClient* owner,
78										media_connection_id id);
79
80	const media_source&				_Source() const;
81	const media_destination&		_Destination() const;
82
83	media_connection				fConnection;
84
85	BMediaClient*					fOwner;
86
87	// A connection might be binded so that it will automatically
88	// forward or receive the data from/to a local BMediaConnection,
89	// see BMediaClient::Bind.
90	BMediaConnection*				fBind;
91
92	BBufferGroup*					fBufferGroup;
93
94	bool							fConnected;
95
96	virtual	void					_ReservedMediaConnection0();
97	virtual	void					_ReservedMediaConnection1();
98	virtual	void					_ReservedMediaConnection2();
99	virtual	void					_ReservedMediaConnection3();
100	virtual	void					_ReservedMediaConnection4();
101	virtual	void					_ReservedMediaConnection5();
102	virtual	void					_ReservedMediaConnection6();
103	virtual	void					_ReservedMediaConnection7();
104	virtual	void					_ReservedMediaConnection8();
105	virtual	void					_ReservedMediaConnection9();
106	virtual	void					_ReservedMediaConnection10();
107	uint32							fPadding[64];
108
109	friend class BMediaClient;
110	friend class BMediaClientNode;
111
112	friend class BMediaInput;
113	friend class BMediaOutput;
114};
115
116
117class BMediaInput : public virtual BMediaConnection {
118protected:
119									BMediaInput();
120	virtual							~BMediaInput();
121
122	// Callbacks
123	virtual status_t				FormatChanged(const media_format& format);
124
125	virtual void					HandleBuffer(BBuffer* buffer);
126
127private:
128	media_input						_MediaInput() const;
129
130	virtual	void					_ReservedMediaInput0();
131	virtual	void					_ReservedMediaInput1();
132	virtual	void					_ReservedMediaInput2();
133	virtual	void					_ReservedMediaInput3();
134	virtual	void					_ReservedMediaInput4();
135	virtual	void					_ReservedMediaInput5();
136	virtual	void					_ReservedMediaInput6();
137	virtual	void					_ReservedMediaInput7();
138	virtual	void					_ReservedMediaInput8();
139	virtual	void					_ReservedMediaInput9();
140	virtual	void					_ReservedMediaInput10();
141	uint32							fPadding[32];
142
143	friend class BMediaClientNode;
144};
145
146
147class BMediaOutput : public virtual BMediaConnection {
148protected:
149									BMediaOutput();
150	virtual							~BMediaOutput();
151
152	// Callbacks
153	virtual status_t				PrepareToConnect(media_format* format);
154
155	virtual status_t				FormatProposal(media_format* format);
156
157	// TODO: Do we really want this?
158	virtual status_t				FormatChangeRequested(media_format* format);
159
160	// When a connection is not binded with another, and you really don't want
161	// to use BMediaGraph it's your job to send the buffer to the connection
162	// you want. You might want to ovverride it so that you can track something,
163	// in this case be sure to call the base version. Be sure to know what
164	// you are doing.
165	virtual	status_t				SendBuffer(BBuffer* buffer);
166
167private:
168	media_output					_MediaOutput() const;
169
170	// TODO: possibly unneeded.
171	void							_SetEnabled(bool enabled);
172	bool							_IsEnabled() const;
173
174	bool							fEnabled;
175	size_t							fFramesSent;
176
177	virtual	void					_ReservedMediaOutput0();
178	virtual	void					_ReservedMediaOutput1();
179	virtual	void					_ReservedMediaOutput2();
180	virtual	void					_ReservedMediaOutput3();
181	virtual	void					_ReservedMediaOutput4();
182	virtual	void					_ReservedMediaOutput5();
183	virtual	void					_ReservedMediaOutput6();
184	virtual	void					_ReservedMediaOutput7();
185	virtual	void					_ReservedMediaOutput8();
186	virtual	void					_ReservedMediaOutput9();
187	virtual	void					_ReservedMediaOutput10();
188	uint32							fPadding[32];
189
190	friend class BMediaClientNode;
191};
192
193
194}
195
196}
197
198using namespace BPrivate::media;
199
200#endif
201