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_CLIENT_H
79b410c41SDario Casalinuovo#define _MEDIA_CLIENT_H
89b410c41SDario Casalinuovo
99b410c41SDario Casalinuovo#include <Buffer.h>
109b410c41SDario Casalinuovo#include <MediaAddOn.h>
11f506f305SDario Casalinuovo#include <MediaClientDefs.h>
129b410c41SDario Casalinuovo#include <MediaDefs.h>
1378fea9cdSBarrett#include <ObjectList.h>
149b410c41SDario Casalinuovo
159b410c41SDario Casalinuovo
169b410c41SDario Casalinuovonamespace BPrivate { namespace media {
179b410c41SDario Casalinuovo
189b410c41SDario Casalinuovo
197561d9acSDario Casalinuovoclass BMediaClientNode;
206d025521SDario Casalinuovoclass BMediaConnection;
216d025521SDario Casalinuovoclass BMediaInput;
226d025521SDario Casalinuovoclass BMediaOutput;
236d025521SDario Casalinuovo
240844ddf4SDario Casalinuovo// Private stuff
250844ddf4SDario Casalinuovoclass InputReleaser;
260844ddf4SDario Casalinuovoclass OutputReleaser;
270844ddf4SDario Casalinuovo
280844ddf4SDario Casalinuovo
299b410c41SDario Casalinuovo// BMediaClient is a general purpose class allowing to create any kind
309b410c41SDario Casalinuovo// of media_node. It automatically manage the expected behavior under
31c1137899SDario Casalinuovo// different run modes, and allow to specify the different capabilities needed.
32c1137899SDario Casalinuovo// BMediaClient is not using any of the coding patterns you might be used to.
33c1137899SDario Casalinuovo// There are no events to care, and threading is managed internally using
34c1137899SDario Casalinuovo// the data processing specified by the BMediaGraph class.
359b410c41SDario Casalinuovoclass BMediaClient {
369b410c41SDario Casalinuovopublic:
379b410c41SDario Casalinuovo									BMediaClient(const char* name,
389b410c41SDario Casalinuovo										media_type type
399b410c41SDario Casalinuovo											= B_MEDIA_UNKNOWN_TYPE,
40f1f09565SDario Casalinuovo										media_client_kinds
41f506f305SDario Casalinuovo											kind = B_MEDIA_PLAYER
429b410c41SDario Casalinuovo												& B_MEDIA_RECORDER);
439b410c41SDario Casalinuovo
449b410c41SDario Casalinuovo	virtual							~BMediaClient();
459b410c41SDario Casalinuovo
46f506f305SDario Casalinuovo			const media_client&		Client() const;
479b410c41SDario Casalinuovo
48f1f09565SDario Casalinuovo			media_client_id			Id() const;
49f1f09565SDario Casalinuovo			const char*				Name() const;
509b410c41SDario Casalinuovo	// Return the capabilities of this BMediaClient instance.
51f1f09565SDario Casalinuovo			media_client_kinds		Kinds() const;
52f506f305SDario Casalinuovo			media_type				MediaType() const;
53f506f305SDario Casalinuovo
54f506f305SDario Casalinuovo			status_t				InitCheck() const;
559b410c41SDario Casalinuovo
56a6ac14c7SBarrett	// Called when the node is correctly registered to the media services.
57a6ac14c7SBarrett	virtual void					ClientRegistered();
58a6ac14c7SBarrett
59ecb39585SDario Casalinuovo	// TODO: Should allow BControllable capabilities
60ecb39585SDario Casalinuovo
619b410c41SDario Casalinuovo	// When those functions return, the BMediaConnection is added to the
62c1137899SDario Casalinuovo	// list and is visible to other nodes as not connected. Any input/output
635854fc4aSDario Casalinuovo	// should be registered to a BMediaClient to become visible in the system.
64ecb39585SDario Casalinuovo	virtual status_t				RegisterInput(BMediaInput* input);
65ecb39585SDario Casalinuovo	virtual status_t				RegisterOutput(BMediaOutput* output);
669b410c41SDario Casalinuovo
679b410c41SDario Casalinuovo	// Bind internally two connections of the same BMediaClient, so that the
689b410c41SDario Casalinuovo	// input will be automatically forwarded to the output just after the
699b410c41SDario Casalinuovo	// ProcessFunc is called. The buffer is automatically recycled too.
709b410c41SDario Casalinuovo	// Beware that the binding operation is valid only for local connections
719b410c41SDario Casalinuovo	// which belong to this node, otherwise return B_ERROR.
726d025521SDario Casalinuovo	virtual status_t				Bind(BMediaInput* input,
736d025521SDario Casalinuovo										BMediaOutput* output);
749b410c41SDario Casalinuovo
756d025521SDario Casalinuovo	virtual status_t				Unbind(BMediaInput* input,
766d025521SDario Casalinuovo										BMediaOutput* output);
779b410c41SDario Casalinuovo
789b410c41SDario Casalinuovo	// If the user want a particular format for a connection it should
799b410c41SDario Casalinuovo	// use BMediaConnection::SetAcceptedFormat(), if it's not specified
809b410c41SDario Casalinuovo	// BMediaClient::Format() will be used, in case both aren't specified
810253ecbdSDario Casalinuovo	// an error is returned. The first parameter should always belong to
820253ecbdSDario Casalinuovo	// this node, the second will be a connection obtained from another
835854fc4aSDario Casalinuovo	// BMediaClient. Unregistered connections will be registered automatically.
849b410c41SDario Casalinuovo	virtual status_t				Connect(BMediaConnection* ourConnection,
859b410c41SDario Casalinuovo										BMediaConnection* theirConnection);
869b410c41SDario Casalinuovo
879b410c41SDario Casalinuovo	virtual status_t				Connect(BMediaConnection* ourConnection,
88f506f305SDario Casalinuovo										const media_connection& theirConnection);
899b410c41SDario Casalinuovo
90f506f305SDario Casalinuovo	// Find a free input/output and try to connect to the media_client,
91f506f305SDario Casalinuovo	// return meaningful error otherwise.
929b410c41SDario Casalinuovo	virtual status_t				Connect(BMediaConnection* ourConnection,
93f506f305SDario Casalinuovo										const media_client& client);
949b410c41SDario Casalinuovo
959b410c41SDario Casalinuovo	// Disconnect any connection belonging to this object, to disconnect
969b410c41SDario Casalinuovo	// a single connection use BMediaConnection::Disconnect().
979b410c41SDario Casalinuovo	virtual status_t				Disconnect();
989b410c41SDario Casalinuovo
999b410c41SDario Casalinuovo			int32					CountInputs() const;
1009b410c41SDario Casalinuovo			int32					CountOutputs() const;
101f506f305SDario Casalinuovo
1026d025521SDario Casalinuovo			BMediaInput*			InputAt(int32 index) const;
1036d025521SDario Casalinuovo			BMediaOutput*			OutputAt(int32 index) const;
1049b410c41SDario Casalinuovo
1056d025521SDario Casalinuovo			BMediaInput*			FindInput(
106902701e6SDario Casalinuovo										const media_connection& input) const;
1076d025521SDario Casalinuovo			BMediaOutput*			FindOutput(
108902701e6SDario Casalinuovo										const media_connection& output) const;
1099b410c41SDario Casalinuovo
110852b6295SBarrett			bool					IsStarted() const;
1119b410c41SDario Casalinuovo
112c1137899SDario Casalinuovo	// NOTE: The following functions aren't provided to be inherited,
113c1137899SDario Casalinuovo	// always use the protected HandleSomething version. This is because
114c1137899SDario Casalinuovo	// otherwise you could break the connection mechanism and mine interoperability
115c1137899SDario Casalinuovo	// from remote nodes.
116550e05afSDario Casalinuovo			status_t				Start();
117550e05afSDario Casalinuovo			status_t				Stop();
1189b410c41SDario Casalinuovo			status_t				Seek(bigtime_t mediaTime,
1199b410c41SDario Casalinuovo										bigtime_t performanceTime);
1209b410c41SDario Casalinuovo			status_t				Roll(bigtime_t start, bigtime_t stop,
1219b410c41SDario Casalinuovo										bigtime_t seek);
122550e05afSDario Casalinuovo
123e74e02acSBarrett	// Return the current performance time handled by the client.
1249e9655edSDario Casalinuovo			bigtime_t				CurrentTime() const;
1259b410c41SDario Casalinuovo
1269b410c41SDario Casalinuovo	// This is supplied to support using this class in a BMediaAddOn.
1279b410c41SDario Casalinuovo	// Default version just return NULL.
1289b410c41SDario Casalinuovo	virtual	BMediaAddOn*			AddOn(int32* id) const;
1299b410c41SDario Casalinuovo
1309b410c41SDario Casalinuovoprotected:
131ecb39585SDario Casalinuovo	virtual void					HandleStart(bigtime_t performanceTime);
132ecb39585SDario Casalinuovo	virtual void					HandleStop(bigtime_t performanceTime);
133ecb39585SDario Casalinuovo
134ecb39585SDario Casalinuovo	virtual void					HandleSeek(bigtime_t mediaTime,
135ecb39585SDario Casalinuovo										bigtime_t performanceTime);
136ecb39585SDario Casalinuovo
137d64dd7beSDario Casalinuovo	virtual status_t				FormatSuggestion(media_type type,
138ecb39585SDario Casalinuovo										int32 quality, media_format* format);
139ecb39585SDario Casalinuovo
1409b410c41SDario Casalinuovoprivate:
141d64dd7beSDario Casalinuovo			void					_Init();
142d64dd7beSDario Casalinuovo			void					_Deinit();
143c1137899SDario Casalinuovo
144d64dd7beSDario Casalinuovo			void					_AddInput(BMediaInput* input);
145d64dd7beSDario Casalinuovo			void					_AddOutput(BMediaOutput* output);
146d64dd7beSDario Casalinuovo
147d64dd7beSDario Casalinuovo			BMediaInput*			_FindInput(
148902701e6SDario Casalinuovo										const media_destination& dest) const;
149d64dd7beSDario Casalinuovo			BMediaOutput*			_FindOutput(
150902701e6SDario Casalinuovo										const media_source& source) const;
1519b410c41SDario Casalinuovo
1526d025521SDario Casalinuovo			status_t				_ConnectInput(BMediaOutput* output,
153f506f305SDario Casalinuovo										const media_connection& input);
1546d025521SDario Casalinuovo			status_t				_ConnectOutput(BMediaInput* input,
155f506f305SDario Casalinuovo										const media_connection& output);
1569b410c41SDario Casalinuovo
157991d9dbfSDario Casalinuovo			status_t				_DisconnectConnection(BMediaConnection* conn);
158991d9dbfSDario Casalinuovo			status_t				_ReleaseConnection(BMediaConnection* conn);
159991d9dbfSDario Casalinuovo
1609b410c41SDario Casalinuovo			status_t				fInitErr;
1619b410c41SDario Casalinuovo
162f506f305SDario Casalinuovo			media_client			fClient;
1639b410c41SDario Casalinuovo
1649b410c41SDario Casalinuovo			bool					fRunning;
1659b410c41SDario Casalinuovo			BMediaClientNode*		fNode;
1669b410c41SDario Casalinuovo
1679e9655edSDario Casalinuovo			bigtime_t				fCurrentTime;
1689b410c41SDario Casalinuovo
1690844ddf4SDario Casalinuovo			BObjectList<InputReleaser>	fInputs;
1700844ddf4SDario Casalinuovo			BObjectList<OutputReleaser>	fOutputs;
1719b410c41SDario Casalinuovo
172eebedf8fSDario Casalinuovo			media_connection_id		fLastID;
173f506f305SDario Casalinuovo
1749b410c41SDario Casalinuovo	virtual	void					_ReservedMediaClient0();
1759b410c41SDario Casalinuovo	virtual	void					_ReservedMediaClient1();
1769b410c41SDario Casalinuovo	virtual	void					_ReservedMediaClient2();
1779b410c41SDario Casalinuovo	virtual	void					_ReservedMediaClient3();
1789b410c41SDario Casalinuovo	virtual	void					_ReservedMediaClient4();
1799b410c41SDario Casalinuovo	virtual	void					_ReservedMediaClient5();
1809b410c41SDario Casalinuovo	virtual	void					_ReservedMediaClient6();
1819b410c41SDario Casalinuovo	virtual	void					_ReservedMediaClient7();
1829b410c41SDario Casalinuovo	virtual	void					_ReservedMediaClient8();
1839b410c41SDario Casalinuovo	virtual	void					_ReservedMediaClient9();
1849b410c41SDario Casalinuovo	virtual	void					_ReservedMediaClient10();
1859b410c41SDario Casalinuovo			uint32					fPadding[64];
1869b410c41SDario Casalinuovo
1879b410c41SDario Casalinuovo	friend class BMediaClientNode;
1889b410c41SDario Casalinuovo	friend class BMediaConnection;
1899ee6577eSDario Casalinuovo	friend class BMediaInput;
1909ee6577eSDario Casalinuovo	friend class BMediaOutput;
1919b410c41SDario Casalinuovo};
1929b410c41SDario Casalinuovo
1939b410c41SDario Casalinuovo
1949b410c41SDario Casalinuovo}
1959b410c41SDario Casalinuovo
1969b410c41SDario Casalinuovo}
1979b410c41SDario Casalinuovo
1989b410c41SDario Casalinuovousing namespace BPrivate::media;
1999b410c41SDario Casalinuovo
2009b410c41SDario Casalinuovo#endif
201