MediaClient.h revision a6ac14c7
1/*
2 * Copyright 2015-2018, Dario Casalinuovo. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5
6#ifndef _MEDIA_CLIENT_H
7#define _MEDIA_CLIENT_H
8
9#include <Buffer.h>
10#include <MediaAddOn.h>
11#include <MediaClientDefs.h>
12#include <MediaDefs.h>
13#include <ObjectList.h>
14
15
16namespace BPrivate { namespace media {
17
18
19class BMediaClientNode;
20class BMediaConnection;
21class BMediaInput;
22class BMediaOutput;
23
24// Private stuff
25class InputReleaser;
26class OutputReleaser;
27
28
29// BMediaClient is a general purpose class allowing to create any kind
30// of media_node. It automatically manage the expected behavior under
31// different run modes, and allow to specify the different capabilities needed.
32// BMediaClient is not using any of the coding patterns you might be used to.
33// There are no events to care, and threading is managed internally using
34// the data processing specified by the BMediaGraph class.
35class BMediaClient {
36public:
37									BMediaClient(const char* name,
38										media_type type
39											= B_MEDIA_UNKNOWN_TYPE,
40										media_client_kinds
41											kind = B_MEDIA_PLAYER
42												& B_MEDIA_RECORDER);
43
44	virtual							~BMediaClient();
45
46			const media_client&		Client() const;
47
48			media_client_id			Id() const;
49			const char*				Name() const;
50	// Return the capabilities of this BMediaClient instance.
51			media_client_kinds		Kinds() const;
52			media_type				MediaType() const;
53
54			status_t				InitCheck() const;
55
56	// Called when the node is correctly registered to the media services.
57	virtual void					ClientRegistered();
58
59	// TODO: Should allow BControllable capabilities
60
61	// When those functions return, the BMediaConnection is added to the
62	// list and is visible to other nodes as not connected. Any input/output
63	// should be registered to a BMediaClient to become visible in the system.
64	virtual status_t				RegisterInput(BMediaInput* input);
65	virtual status_t				RegisterOutput(BMediaOutput* output);
66
67	// Bind internally two connections of the same BMediaClient, so that the
68	// input will be automatically forwarded to the output just after the
69	// ProcessFunc is called. The buffer is automatically recycled too.
70	// Beware that the binding operation is valid only for local connections
71	// which belong to this node, otherwise return B_ERROR.
72	virtual status_t				Bind(BMediaInput* input,
73										BMediaOutput* output);
74
75	virtual status_t				Unbind(BMediaInput* input,
76										BMediaOutput* output);
77
78	// If the user want a particular format for a connection it should
79	// use BMediaConnection::SetAcceptedFormat(), if it's not specified
80	// BMediaClient::Format() will be used, in case both aren't specified
81	// an error is returned. The first parameter should always belong to
82	// this node, the second will be a connection obtained from another
83	// BMediaClient. Unregistered connections will be registered automatically.
84	virtual status_t				Connect(BMediaConnection* ourConnection,
85										BMediaConnection* theirConnection);
86
87	virtual status_t				Connect(BMediaConnection* ourConnection,
88										const media_connection& theirConnection);
89
90	// Find a free input/output and try to connect to the media_client,
91	// return meaningful error otherwise.
92	virtual status_t				Connect(BMediaConnection* ourConnection,
93										const media_client& client);
94
95	// Disconnect any connection belonging to this object, to disconnect
96	// a single connection use BMediaConnection::Disconnect().
97	virtual status_t				Disconnect();
98
99			int32					CountInputs() const;
100			int32					CountOutputs() const;
101
102			BMediaInput*			InputAt(int32 index) const;
103			BMediaOutput*			OutputAt(int32 index) const;
104
105			BMediaInput*			FindInput(
106										const media_connection& input) const;
107			BMediaOutput*			FindOutput(
108										const media_connection& output) const;
109
110			bool					IsRunning() const;
111	// Called when the node is correctly registered to the media services.
112	//virtual void					ReadyToRun() const;
113
114	// NOTE: The following functions aren't provided to be inherited,
115	// always use the protected HandleSomething version. This is because
116	// otherwise you could break the connection mechanism and mine interoperability
117	// from remote nodes.
118			status_t				Start();
119			status_t				Stop();
120			status_t				Seek(bigtime_t mediaTime,
121										bigtime_t performanceTime);
122			status_t				Roll(bigtime_t start, bigtime_t stop,
123										bigtime_t seek);
124
125	// Return the current performance time handled by the client.
126			bigtime_t				CurrentTime() const;
127
128	// This is supplied to support using this class in a BMediaAddOn.
129	// Default version just return NULL.
130	virtual	BMediaAddOn*			AddOn(int32* id) const;
131
132protected:
133	virtual void					HandleStart(bigtime_t performanceTime);
134	virtual void					HandleStop(bigtime_t performanceTime);
135
136	virtual void					HandleSeek(bigtime_t mediaTime,
137										bigtime_t performanceTime);
138
139	virtual status_t				FormatSuggestion(media_type type,
140										int32 quality, media_format* format);
141
142private:
143			void					_Init();
144			void					_Deinit();
145
146			void					_AddInput(BMediaInput* input);
147			void					_AddOutput(BMediaOutput* output);
148
149			BMediaInput*			_FindInput(
150										const media_destination& dest) const;
151			BMediaOutput*			_FindOutput(
152										const media_source& source) const;
153
154			status_t				_ConnectInput(BMediaOutput* output,
155										const media_connection& input);
156			status_t				_ConnectOutput(BMediaInput* input,
157										const media_connection& output);
158
159			status_t				_DisconnectConnection(BMediaConnection* conn);
160			status_t				_ReleaseConnection(BMediaConnection* conn);
161
162			status_t				fInitErr;
163
164			media_client			fClient;
165
166			bool					fRunning;
167			BMediaClientNode*		fNode;
168
169			bigtime_t				fCurrentTime;
170
171			BObjectList<InputReleaser>	fInputs;
172			BObjectList<OutputReleaser>	fOutputs;
173
174			media_connection_id		fLastID;
175
176	virtual	void					_ReservedMediaClient0();
177	virtual	void					_ReservedMediaClient1();
178	virtual	void					_ReservedMediaClient2();
179	virtual	void					_ReservedMediaClient3();
180	virtual	void					_ReservedMediaClient4();
181	virtual	void					_ReservedMediaClient5();
182	virtual	void					_ReservedMediaClient6();
183	virtual	void					_ReservedMediaClient7();
184	virtual	void					_ReservedMediaClient8();
185	virtual	void					_ReservedMediaClient9();
186	virtual	void					_ReservedMediaClient10();
187			uint32					fPadding[64];
188
189	friend class BMediaClientNode;
190	friend class BMediaConnection;
191	friend class BMediaInput;
192	friend class BMediaOutput;
193};
194
195
196}
197
198}
199
200using namespace BPrivate::media;
201
202#endif
203