MediaConnection.h revision 7561d9ac
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
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.
28class BMediaConnection {
29public:
30	virtual							~BMediaConnection();
31
32	const media_connection&			Connection() const;
33	BMediaClient*					Client() const;
34
35	media_connection_id				Id() const;
36	const char*						Name() const;
37
38	bool							HasBinding() const;
39	BMediaConnection*				Binding() const;
40
41	bool							IsConnected() const;
42
43	// This allow to specify a format that will be used while
44	// connecting to another node. See BMediaClient::SetFormat.
45	void							SetAcceptedFormat(
46										const media_format& format);
47	const media_format&				AcceptedFormat() const;
48
49	// Represents the buffer size, depends on the format set or negotiated
50	// for this connection.
51	size_t							BufferSize() const;
52
53	// Represents the duration of one buffer depends on the format set or
54	// negotiated for this connection.
55	bigtime_t						BufferDuration() const;
56
57	// Disconnect this connection. When a connection is disconnected,
58	// it can be reused as brand new.
59	status_t						Disconnect();
60
61	// Once you are done with this connection you release it, it automatically
62	// remove the object from the BMediaClient and free all used resources.
63	// This will make the connection to disappear completely, so if you
64	// want to preserve it for future connections just Disconnect() it.
65	status_t						Release();
66
67protected:
68									BMediaConnection(
69										media_connection_kinds kinds);
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			void					ConnectionRegistered(BMediaClient* owner,
88										media_connection_id id);
89
90	const media_source&				Source() const;
91	const media_destination&		Destination() const;
92
93	media_connection				fConnection;
94
95	BMediaClient*					fOwner;
96
97	// A connection might be binded so that it will automatically
98	// forward or receive the data from/to a local BMediaConnection,
99	// see BMediaClient::Bind.
100	BMediaConnection*				fBind;
101
102	size_t							fBufferSize;
103	bigtime_t						fBufferDuration;
104
105	BBufferGroup*					fBufferGroup;
106
107	bool							fConnected;
108
109	bigtime_t						fMinLatency;
110	bigtime_t						fMaxLatency;
111
112	virtual	void					_ReservedMediaConnection0();
113	virtual	void					_ReservedMediaConnection1();
114	virtual	void					_ReservedMediaConnection2();
115	virtual	void					_ReservedMediaConnection3();
116	virtual	void					_ReservedMediaConnection4();
117	virtual	void					_ReservedMediaConnection5();
118	virtual	void					_ReservedMediaConnection6();
119	virtual	void					_ReservedMediaConnection7();
120	virtual	void					_ReservedMediaConnection8();
121	virtual	void					_ReservedMediaConnection9();
122	virtual	void					_ReservedMediaConnection10();
123	uint32							fPadding[64];
124
125	friend class BMediaClient;
126	friend class BMediaClientNode;
127
128	friend class BMediaInput;
129	friend class BMediaOutput;
130};
131
132
133class BMediaInput : public virtual BMediaConnection {
134public:
135									BMediaInput();
136
137protected:
138	// Callbacks
139	virtual status_t				FormatChanged(const media_format& format);
140
141	virtual void					BufferReceived(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	// Callbacks
172	virtual status_t				PrepareToConnect(media_format* format);
173
174	virtual status_t				FormatProposal(media_format* format);
175	virtual status_t				FormatChangeRequested(media_format* format);
176
177	// When a connection is not binded with another, and you really don't want
178	// to use BMediaGraph it's your job to send the buffer to the connection
179	// you want. You might want to ovverride it so that you can track something,
180	// in this case be sure to call the base version. Be sure to know what
181	// you are doing.
182	virtual	status_t				SendBuffer(BBuffer* buffer);
183
184private:
185	media_output					MediaOutput() const;
186
187	bool							fEnabled;
188	size_t							fFramesSent;
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