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
BSimpleMediaClient(const char * name,media_type type,media_client_kinds kinds)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
~BSimpleMediaClient()22 BSimpleMediaClient::~BSimpleMediaClient()
23 {
24 CALLED();
25 }
26
27
28 BSimpleMediaInput*
BeginInput()29 BSimpleMediaClient::BeginInput()
30 {
31 CALLED();
32
33 BSimpleMediaInput* input = new BSimpleMediaInput();
34 RegisterInput(input);
35 return input;
36 }
37
38
39 BSimpleMediaOutput*
BeginOutput()40 BSimpleMediaClient::BeginOutput()
41 {
42 CALLED();
43
44 BSimpleMediaOutput* output = new BSimpleMediaOutput();
45 RegisterOutput(output);
46 return output;
47 }
48
49
50 void
SetHook(notify_hook notifyHook,void * cookie)51 BSimpleMediaClient::SetHook(notify_hook notifyHook, void* cookie)
52 {
53 CALLED();
54
55 fNotifyHook = notifyHook;
56 fNotifyCookie = cookie;
57 }
58
59
60 void
HandleStart(bigtime_t performanceTime)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
HandleStop(bigtime_t performanceTime)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
HandleSeek(bigtime_t mediaTime,bigtime_t performanceTime)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
FormatSuggestion(media_type type,int32 quality,media_format * format)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
_ReservedSimpleMediaClient0()108 void BSimpleMediaClient::_ReservedSimpleMediaClient0() {}
_ReservedSimpleMediaClient1()109 void BSimpleMediaClient::_ReservedSimpleMediaClient1() {}
_ReservedSimpleMediaClient2()110 void BSimpleMediaClient::_ReservedSimpleMediaClient2() {}
_ReservedSimpleMediaClient3()111 void BSimpleMediaClient::_ReservedSimpleMediaClient3() {}
_ReservedSimpleMediaClient4()112 void BSimpleMediaClient::_ReservedSimpleMediaClient4() {}
_ReservedSimpleMediaClient5()113 void BSimpleMediaClient::_ReservedSimpleMediaClient5() {}
114
115
BSimpleMediaConnection(media_connection_kinds kinds)116 BSimpleMediaConnection::BSimpleMediaConnection(media_connection_kinds kinds)
117 :
118 BMediaConnection(kinds),
119 fProcessHook(NULL),
120 fNotifyHook(NULL),
121 fBufferCookie(NULL)
122 {
123 }
124
125
~BSimpleMediaConnection()126 BSimpleMediaConnection::~BSimpleMediaConnection()
127 {
128 CALLED();
129 }
130
131
132 void
SetHooks(process_hook processHook,notify_hook notifyHook,void * cookie)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*
Cookie() const145 BSimpleMediaConnection::Cookie() const
146 {
147 CALLED();
148
149 return fBufferCookie;
150 }
151
152
153 size_t
BufferSize() const154 BSimpleMediaConnection::BufferSize() const
155 {
156 return fBufferSize;
157 }
158
159
160 void
SetBufferSize(size_t bufferSize)161 BSimpleMediaConnection::SetBufferSize(size_t bufferSize)
162 {
163 fBufferSize = bufferSize;
164 }
165
166
167 void
SetAcceptedFormat(const media_format & format)168 BSimpleMediaConnection::SetAcceptedFormat(const media_format& format)
169 {
170 CALLED();
171
172 fAcceptedFormat = format;
173 }
174
175
176 const media_format&
AcceptedFormat() const177 BSimpleMediaConnection::AcceptedFormat() const
178 {
179 CALLED();
180
181 return fAcceptedFormat;
182 }
183
184
BSimpleMediaInput()185 BSimpleMediaInput::BSimpleMediaInput()
186 :
187 BMediaConnection(B_MEDIA_INPUT),
188 BSimpleMediaConnection(B_MEDIA_INPUT),
189 BMediaInput()
190 {
191 }
192
193
~BSimpleMediaInput()194 BSimpleMediaInput::~BSimpleMediaInput()
195 {
196 CALLED();
197 }
198
199
200 status_t
AcceptFormat(media_format * format)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
Connected(const media_format & format)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
Disconnected()229 BSimpleMediaInput::Disconnected()
230 {
231 if (fNotifyHook != NULL)
232 (*fNotifyHook)(this, BSimpleMediaConnection::B_INPUT_DISCONNECTED);
233
234 BMediaInput::Disconnected();
235 }
236
237
238 void
HandleBuffer(BBuffer * buffer)239 BSimpleMediaInput::HandleBuffer(BBuffer* buffer)
240 {
241 CALLED();
242
243 if (fProcessHook != NULL)
244 (*fProcessHook)(this, buffer);
245 }
246
247
BSimpleMediaOutput()248 BSimpleMediaOutput::BSimpleMediaOutput()
249 :
250 BMediaConnection(B_MEDIA_OUTPUT),
251 BSimpleMediaConnection(B_MEDIA_OUTPUT),
252 BMediaOutput()
253 {
254 }
255
256
~BSimpleMediaOutput()257 BSimpleMediaOutput::~BSimpleMediaOutput()
258 {
259 CALLED();
260 }
261
262
263 status_t
PrepareToConnect(media_format * format)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
FormatProposal(media_format * format)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
Connected(const media_format & format)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
Disconnected()301 BSimpleMediaOutput::Disconnected()
302 {
303 if (fNotifyHook != NULL)
304 (*fNotifyHook)(this, BSimpleMediaConnection::B_OUTPUT_DISCONNECTED);
305
306 BMediaOutput::Disconnected();
307 }
308