xref: /haiku/src/kits/media/experimental/SimpleMediaClient.cpp (revision 12dba4e70f831d6d27a7f769cc9dab19c19a155d)
1 /*
2  * Copyright 2015, Dario Casalinuovo. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <SimpleMediaClient.h>
7 
8 #include <MediaDebug.h>
9 
10 
11 BSimpleMediaClient::BSimpleMediaClient(const char* name,
12 	media_type type, media_client_kinds kinds)
13 	:
14 	BMediaClient(name, type, kinds),
15 	fNotifyHook(NULL),
16 	fNotifyCookie(NULL)
17 {
18 	CALLED();
19 }
20 
21 
22 BSimpleMediaClient::~BSimpleMediaClient()
23 {
24 	CALLED();
25 }
26 
27 
28 BSimpleMediaInput*
29 BSimpleMediaClient::BeginInput()
30 {
31 	CALLED();
32 
33 	BSimpleMediaInput* input = new BSimpleMediaInput();
34 	RegisterInput(input);
35 	return input;
36 }
37 
38 
39 BSimpleMediaOutput*
40 BSimpleMediaClient::BeginOutput()
41 {
42 	CALLED();
43 
44 	BSimpleMediaOutput* output = new BSimpleMediaOutput();
45 	RegisterOutput(output);
46 	return output;
47 }
48 
49 
50 void
51 BSimpleMediaClient::SetHook(notify_hook notifyHook, void* cookie)
52 {
53 	CALLED();
54 
55 	fNotifyHook = notifyHook;
56 	fNotifyCookie = cookie;
57 }
58 
59 
60 void
61 BSimpleMediaClient::HandleStart(bigtime_t performanceTime)
62 {
63 	if (fNotifyHook != NULL) {
64 		(*fNotifyHook)(BSimpleMediaClient::fNotifyCookie,
65 			BSimpleMediaClient::B_WILL_START,
66 			performanceTime);
67 	}
68 }
69 
70 
71 void
72 BSimpleMediaClient::HandleStop(bigtime_t performanceTime)
73 {
74 	if (fNotifyHook != NULL) {
75 		(*fNotifyHook)(BSimpleMediaClient::fNotifyCookie,
76 			BSimpleMediaClient::B_WILL_STOP,
77 			performanceTime);
78 	}
79 }
80 
81 
82 void
83 BSimpleMediaClient::HandleSeek(bigtime_t mediaTime, bigtime_t performanceTime)
84 {
85 	if (fNotifyHook != NULL) {
86 		(*fNotifyHook)(BSimpleMediaClient::fNotifyCookie,
87 			BSimpleMediaClient::B_WILL_SEEK,
88 			performanceTime, mediaTime);
89 	}
90 }
91 
92 
93 status_t
94 BSimpleMediaClient::FormatSuggestion(media_type type, int32 quality,
95 	media_format* format)
96 {
97 	if (fNotifyHook != NULL) {
98 		status_t result = B_ERROR;
99 		(*fNotifyHook)(BSimpleMediaClient::fNotifyCookie,
100 			BSimpleMediaClient::B_FORMAT_SUGGESTION,
101 			type, quality, format, &result);
102 		return result;
103 	}
104 	return B_ERROR;
105 }
106 
107 
108 void BSimpleMediaClient::_ReservedSimpleMediaClient0() {}
109 void BSimpleMediaClient::_ReservedSimpleMediaClient1() {}
110 void BSimpleMediaClient::_ReservedSimpleMediaClient2() {}
111 void BSimpleMediaClient::_ReservedSimpleMediaClient3() {}
112 void BSimpleMediaClient::_ReservedSimpleMediaClient4() {}
113 void BSimpleMediaClient::_ReservedSimpleMediaClient5() {}
114 
115 
116 BSimpleMediaConnection::BSimpleMediaConnection(media_connection_kinds kinds)
117 	:
118 	BMediaConnection(kinds),
119 	fProcessHook(NULL),
120 	fNotifyHook(NULL),
121 	fBufferCookie(NULL)
122 {
123 }
124 
125 
126 BSimpleMediaConnection::~BSimpleMediaConnection()
127 {
128 	CALLED();
129 }
130 
131 
132 void
133 BSimpleMediaConnection::SetHooks(process_hook processHook,
134 	notify_hook notifyHook, void* cookie)
135 {
136 	CALLED();
137 
138 	fProcessHook = processHook;
139 	fNotifyHook = notifyHook;
140 	fBufferCookie = cookie;
141 }
142 
143 
144 void*
145 BSimpleMediaConnection::Cookie() const
146 {
147 	CALLED();
148 
149 	return fBufferCookie;
150 }
151 
152 
153 size_t
154 BSimpleMediaConnection::BufferSize() const
155 {
156 	return fBufferSize;
157 }
158 
159 
160 void
161 BSimpleMediaConnection::SetBufferSize(size_t bufferSize)
162 {
163 	fBufferSize = bufferSize;
164 }
165 
166 
167 void
168 BSimpleMediaConnection::SetAcceptedFormat(const media_format& format)
169 {
170 	CALLED();
171 
172 	fAcceptedFormat = format;
173 }
174 
175 
176 const media_format&
177 BSimpleMediaConnection::AcceptedFormat() const
178 {
179 	CALLED();
180 
181 	return fAcceptedFormat;
182 }
183 
184 
185 BSimpleMediaInput::BSimpleMediaInput()
186 	:
187 	BMediaConnection(B_MEDIA_INPUT),
188 	BSimpleMediaConnection(B_MEDIA_INPUT),
189 	BMediaInput()
190 {
191 }
192 
193 
194 BSimpleMediaInput::~BSimpleMediaInput()
195 {
196 	CALLED();
197 }
198 
199 
200 status_t
201 BSimpleMediaInput::AcceptFormat(media_format* format)
202 {
203 	CALLED();
204 
205 	// TODO: Add hooks
206 
207 	if (format_is_compatible(*format, AcceptedFormat()))
208 		return B_OK;
209 
210 	*format = AcceptedFormat();
211 
212 	return B_MEDIA_BAD_FORMAT;
213 }
214 
215 
216 void
217 BSimpleMediaInput::Connected(const media_format& format)
218 {
219 	if (fNotifyHook != NULL)
220 		(*fNotifyHook)(this, BSimpleMediaConnection::B_INPUT_CONNECTED);
221 
222 	SetAcceptedFormat(format);
223 
224 	BMediaInput::Connected(format);
225 }
226 
227 
228 void
229 BSimpleMediaInput::Disconnected()
230 {
231 	if (fNotifyHook != NULL)
232 		(*fNotifyHook)(this, BSimpleMediaConnection::B_INPUT_DISCONNECTED);
233 
234 	BMediaInput::Disconnected();
235 }
236 
237 
238 void
239 BSimpleMediaInput::HandleBuffer(BBuffer* buffer)
240 {
241 	CALLED();
242 
243 	if (fProcessHook != NULL)
244 		(*fProcessHook)(this, buffer);
245 }
246 
247 
248 BSimpleMediaOutput::BSimpleMediaOutput()
249 	:
250 	BMediaConnection(B_MEDIA_OUTPUT),
251 	BSimpleMediaConnection(B_MEDIA_OUTPUT),
252 	BMediaOutput()
253 {
254 }
255 
256 
257 BSimpleMediaOutput::~BSimpleMediaOutput()
258 {
259 	CALLED();
260 }
261 
262 
263 status_t
264 BSimpleMediaOutput::PrepareToConnect(media_format* format)
265 {
266 	// TODO: Add hooks
267 
268 	if (!format_is_compatible(AcceptedFormat(), *format))
269 		return B_ERROR;
270 
271 	return B_OK;
272 }
273 
274 
275 status_t
276 BSimpleMediaOutput::FormatProposal(media_format* format)
277 {
278 	if (fNotifyHook != NULL) {
279 		return (*fNotifyHook)(this,
280 			BSimpleMediaConnection::B_FORMAT_PROPOSAL, format);
281 	} else
282 		*format = AcceptedFormat();
283 
284 	return B_OK;
285 }
286 
287 
288 void
289 BSimpleMediaOutput::Connected(const media_format& format)
290 {
291 	if (fNotifyHook != NULL)
292 		(*fNotifyHook)(this, BSimpleMediaConnection::B_OUTPUT_CONNECTED);
293 
294 	SetAcceptedFormat(format);
295 
296 	BMediaOutput::Connected(format);
297 }
298 
299 
300 void
301 BSimpleMediaOutput::Disconnected()
302 {
303 	if (fNotifyHook != NULL)
304 		(*fNotifyHook)(this, BSimpleMediaConnection::B_OUTPUT_DISCONNECTED);
305 
306 	BMediaOutput::Disconnected();
307 }
308