19b410c41SDario Casalinuovo/*
2e8bf0f3eSBarrett * Copyright 2015-2018, Dario Casalinuovo. All rights reserved.
39b410c41SDario Casalinuovo * Distributed under the terms of the MIT License.
49b410c41SDario Casalinuovo */
59b410c41SDario Casalinuovo
69b410c41SDario Casalinuovo#ifndef _MEDIA_CONNECTION_H
79b410c41SDario Casalinuovo#define _MEDIA_CONNECTION_H
89b410c41SDario Casalinuovo
99b410c41SDario Casalinuovo#include <BufferGroup.h>
109b410c41SDario Casalinuovo#include <MediaDefs.h>
119b410c41SDario Casalinuovo
12f506f305SDario Casalinuovo#include <MediaClient.h>
13f506f305SDario Casalinuovo#include <MediaClientDefs.h>
14f506f305SDario Casalinuovo
159b410c41SDario Casalinuovo
169b410c41SDario Casalinuovonamespace BPrivate { namespace media {
179b410c41SDario Casalinuovo
187561d9acSDario Casalinuovoclass BMediaClientNode;
197561d9acSDario Casalinuovo
209b410c41SDario Casalinuovo
219b410c41SDario Casalinuovo// The BMediaConnection class is the swiss knife of BMediaClient.
229b410c41SDario Casalinuovo// It represents a connection between two nodes and allow to create complex
239b410c41SDario Casalinuovo// nodes without dealing with the unneeded complexity. Two local connections,
249b410c41SDario Casalinuovo// can be binded, this means that when you will receive a buffer A as input,
25c1137899SDario Casalinuovo// the BufferReceived function will be called so that you can process the BBuffer,
269b410c41SDario Casalinuovo// and once the function returns the output will be automatically forwarded
27c1137899SDario Casalinuovo// to the connection B SendBuffer method.
285854fc4aSDario Casalinuovo// It's not possible to mix a BMediaInput with a BMediaOutput in the same class.
299b410c41SDario Casalinuovoclass BMediaConnection {
309b410c41SDario Casalinuovopublic:
31f506f305SDario Casalinuovo	const media_connection&			Connection() const;
32f1f09565SDario Casalinuovo	BMediaClient*					Client() const;
33f1f09565SDario Casalinuovo
34f506f305SDario Casalinuovo	media_connection_id				Id() const;
35f506f305SDario Casalinuovo	const char*						Name() const;
36f506f305SDario Casalinuovo
379b410c41SDario Casalinuovo	bool							HasBinding() const;
389b410c41SDario Casalinuovo	BMediaConnection*				Binding() const;
399b410c41SDario Casalinuovo
4075b90c42SDario Casalinuovo	bool							IsConnected() const;
4175b90c42SDario Casalinuovo
42dc4c49c3SBarrett	// If the connection is connected get the other endpoint,
43dc4c49c3SBarrett	// return media_connection::null otherwise.
44dc4c49c3SBarrett	media_connection				Endpoint();
469ab4c384SBarrett	// Represents the buffer size, implement it to return the buffer size
479ab4c384SBarrett	// you decided for this connection.
489ab4c384SBarrett	// TODO: Do we want this (and ChainSize) moved on the output side?
499ab4c384SBarrett	// Or perhaps provide an implementation based on the buffer group
509ab4c384SBarrett	// for the consumer?
519ab4c384SBarrett	// Problem is: the consumer has not easy access to the buffer group,
529ab4c384SBarrett	// so we way want to add a special messaging between clients after
539ab4c384SBarrett	// connection, so that inputs know the buffer size and chain size.
549cf18a39SBarrett	virtual size_t					BufferSize() const = 0;
559ab4c384SBarrett	// Implement it to specify the size of your chain of buffers.
569ab4c384SBarrett	//virtual int32					ChainSize() const = 0;
579b410c41SDario Casalinuovo
5805e29ea1SDario Casalinuovo	// Disconnect this connection. When a connection is disconnected,
5905e29ea1SDario Casalinuovo	// it can be reused as brand new.
609b410c41SDario Casalinuovo	status_t						Disconnect();
619b410c41SDario Casalinuovo
629b410c41SDario Casalinuovo	// Once you are done with this connection you release it, it automatically
639b410c41SDario Casalinuovo	// remove the object from the BMediaClient and free all used resources.
649b410c41SDario Casalinuovo	// This will make the connection to disappear completely, so if you
6505e29ea1SDario Casalinuovo	// want to preserve it for future connections just Disconnect() it.
669b410c41SDario Casalinuovo	status_t						Release();
679b410c41SDario Casalinuovo
689b410c41SDario Casalinuovoprotected:
69f1f09565SDario Casalinuovo									BMediaConnection(
701c15261fSBarrett										media_connection_kinds kinds,
711c15261fSBarrett										const char* name = NULL);
720844ddf4SDario Casalinuovo	virtual							~BMediaConnection();
739b410c41SDario Casalinuovo
759ee6577eSDario Casalinuovo	// Those callbacks are shared between BMediaInput and BMediaOutput
769ee6577eSDario Casalinuovo	virtual void					Connected(const media_format& format);
779ee6577eSDario Casalinuovo	virtual void					Disconnected();
789b410c41SDario Casalinuovo
79d64dd7beSDario Casalinuovo			void					_ConnectionRegistered(BMediaClient* owner,
80ecb39585SDario Casalinuovo										media_connection_id id);
81ecb39585SDario Casalinuovo
82d64dd7beSDario Casalinuovo	const media_source&				_Source() const;
83d64dd7beSDario Casalinuovo	const media_destination&		_Destination() const;
849b410c41SDario Casalinuovo
85f506f305SDario Casalinuovo	media_connection				fConnection;
869b410c41SDario Casalinuovo
879b410c41SDario Casalinuovo	BMediaClient*					fOwner;
889b410c41SDario Casalinuovo
899b410c41SDario Casalinuovo	// A connection might be binded so that it will automatically
909b410c41SDario Casalinuovo	// forward or receive the data from/to a local BMediaConnection,
919b410c41SDario Casalinuovo	// see BMediaClient::Bind.
929b410c41SDario Casalinuovo	BMediaConnection*				fBind;
939b410c41SDario Casalinuovo
94f506f305SDario Casalinuovo	bool							fConnected;
95f506f305SDario Casalinuovo
969b410c41SDario Casalinuovo	virtual	void					_ReservedMediaConnection0();
979b410c41SDario Casalinuovo	virtual	void					_ReservedMediaConnection1();
989b410c41SDario Casalinuovo	virtual	void					_ReservedMediaConnection2();
999b410c41SDario Casalinuovo	virtual	void					_ReservedMediaConnection3();
1009b410c41SDario Casalinuovo	virtual	void					_ReservedMediaConnection4();
1019b410c41SDario Casalinuovo	virtual	void					_ReservedMediaConnection5();
1029b410c41SDario Casalinuovo	virtual	void					_ReservedMediaConnection6();
1039b410c41SDario Casalinuovo	virtual	void					_ReservedMediaConnection7();
1049b410c41SDario Casalinuovo	virtual	void					_ReservedMediaConnection8();
1059b410c41SDario Casalinuovo	virtual	void					_ReservedMediaConnection9();
1069b410c41SDario Casalinuovo	virtual	void					_ReservedMediaConnection10();
1079b410c41SDario Casalinuovo	uint32							fPadding[64];
1089b410c41SDario Casalinuovo
1099b410c41SDario Casalinuovo	friend class BMediaClient;
1109b410c41SDario Casalinuovo	friend class BMediaClientNode;
1119ee6577eSDario Casalinuovo
1129ee6577eSDario Casalinuovo	friend class BMediaInput;
1139ee6577eSDario Casalinuovo	friend class BMediaOutput;
1149b410c41SDario Casalinuovo};
1159b410c41SDario Casalinuovo
1169b410c41SDario Casalinuovo
117d1742cc3SDario Casalinuovoclass BMediaInput : public virtual BMediaConnection {
1189ee6577eSDario Casalinuovoprotected:
1191c15261fSBarrett									BMediaInput(const char* name = NULL);
1200844ddf4SDario Casalinuovo	virtual							~BMediaInput();
1210844ddf4SDario Casalinuovo
1229ee6577eSDario Casalinuovo	// Callbacks
1236dc7d854SBarrett	virtual status_t				AcceptFormat(media_format* format) = 0;
1255854fc4aSDario Casalinuovo	virtual void					HandleBuffer(BBuffer* buffer);
1269ee6577eSDario Casalinuovo
127444d8ec0SBarrett	virtual void					Connected(const media_format& format);
128444d8ec0SBarrett	virtual void					Disconnected();
1306d025521SDario Casalinuovoprivate:
1316d025521SDario Casalinuovo
132209b520fSDario Casalinuovo	virtual	void					_ReservedMediaInput0();
133209b520fSDario Casalinuovo	virtual	void					_ReservedMediaInput1();
134209b520fSDario Casalinuovo	virtual	void					_ReservedMediaInput2();
135209b520fSDario Casalinuovo	virtual	void					_ReservedMediaInput3();
136209b520fSDario Casalinuovo	virtual	void					_ReservedMediaInput4();
137209b520fSDario Casalinuovo	virtual	void					_ReservedMediaInput5();
138209b520fSDario Casalinuovo	virtual	void					_ReservedMediaInput6();
139209b520fSDario Casalinuovo	virtual	void					_ReservedMediaInput7();
140209b520fSDario Casalinuovo	virtual	void					_ReservedMediaInput8();
141209b520fSDario Casalinuovo	virtual	void					_ReservedMediaInput9();
142209b520fSDario Casalinuovo	virtual	void					_ReservedMediaInput10();
143209b520fSDario Casalinuovo	uint32							fPadding[32];
144209b520fSDario Casalinuovo
1456d025521SDario Casalinuovo	friend class BMediaClientNode;
1466d025521SDario Casalinuovo};
1476d025521SDario Casalinuovo
1486d025521SDario Casalinuovo
149d1742cc3SDario Casalinuovoclass BMediaOutput : public virtual BMediaConnection {
1509ee6577eSDario Casalinuovoprotected:
1511c15261fSBarrett									BMediaOutput(const char* name = NULL);
1520844ddf4SDario Casalinuovo	virtual							~BMediaOutput();
1530844ddf4SDario Casalinuovo
1549ee6577eSDario Casalinuovo	// Callbacks
1556dc7d854SBarrett	virtual status_t				PrepareToConnect(media_format* format) = 0;
1566dc7d854SBarrett	virtual status_t				FormatProposal(media_format* format) = 0;
158c1137899SDario Casalinuovo	// When a connection is not binded with another, and you really don't want
159c1137899SDario Casalinuovo	// to use BMediaGraph it's your job to send the buffer to the connection
160c1137899SDario Casalinuovo	// you want. You might want to ovverride it so that you can track something,
161c1137899SDario Casalinuovo	// in this case be sure to call the base version. Be sure to know what
162c1137899SDario Casalinuovo	// you are doing.
1639ee6577eSDario Casalinuovo	virtual	status_t				SendBuffer(BBuffer* buffer);
1649ee6577eSDario Casalinuovo
165444d8ec0SBarrett	virtual void					Connected(const media_format& format);
166ff56eda5SBarrett	virtual void					Disconnected();
1686d025521SDario Casalinuovoprivate:
1696d025521SDario Casalinuovo
170befa252fSBarrett	// TODO: possibly unneeded.
171befa252fSBarrett	void							_SetEnabled(bool enabled);
172befa252fSBarrett	bool							_IsEnabled() const;
1742fdc256dSDario Casalinuovo	bool							fEnabled;
1752c9fa0f2SDario Casalinuovo	size_t							fFramesSent;
1769ee6577eSDario Casalinuovo
177ff56eda5SBarrett	BBufferGroup*					fBufferGroup;
179209b520fSDario Casalinuovo	virtual	void					_ReservedMediaOutput0();
180209b520fSDario Casalinuovo	virtual	void					_ReservedMediaOutput1();
181209b520fSDario Casalinuovo	virtual	void					_ReservedMediaOutput2();
182209b520fSDario Casalinuovo	virtual	void					_ReservedMediaOutput3();
183209b520fSDario Casalinuovo	virtual	void					_ReservedMediaOutput4();
184209b520fSDario Casalinuovo	virtual	void					_ReservedMediaOutput5();
185209b520fSDario Casalinuovo	virtual	void					_ReservedMediaOutput6();
186209b520fSDario Casalinuovo	virtual	void					_ReservedMediaOutput7();
187209b520fSDario Casalinuovo	virtual	void					_ReservedMediaOutput8();
188209b520fSDario Casalinuovo	virtual	void					_ReservedMediaOutput9();
189209b520fSDario Casalinuovo	virtual	void					_ReservedMediaOutput10();
190209b520fSDario Casalinuovo	uint32							fPadding[32];
191209b520fSDario Casalinuovo
1926d025521SDario Casalinuovo	friend class BMediaClientNode;
1936d025521SDario Casalinuovo};
1946d025521SDario Casalinuovo
1956d025521SDario Casalinuovo
1969b410c41SDario Casalinuovo}
1979b410c41SDario Casalinuovo
1989b410c41SDario Casalinuovo}
1999b410c41SDario Casalinuovo
2009b410c41SDario Casalinuovousing namespace BPrivate::media;
2019b410c41SDario Casalinuovo
2029b410c41SDario Casalinuovo#endif