vfs: check userland buffers before reading them.
[haiku.git] / src / kits / media / experimental / MediaConnection.cpp
bloba16dfb7637a60a866e5ab67632428ef26915c1e1
1 /*
2 * Copyright 2015, Dario Casalinuovo. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
6 #include <MediaConnection.h>
8 #include "MediaClientNode.h"
10 #include "debug.h"
13 BMediaConnection::BMediaConnection(media_connection_kinds kinds)
15 fOwner(NULL),
16 fBind(NULL),
17 fBufferGroup(NULL),
18 fMinLatency(0),
19 fMaxLatency(0)
21 CALLED();
23 fConnection.kinds = kinds;
24 fConnection.id = -1;
25 //fConnection.client = media_client::null;
29 BMediaConnection::~BMediaConnection()
31 CALLED();
36 const media_connection&
37 BMediaConnection::Connection() const
39 return fConnection;
43 BMediaClient*
44 BMediaConnection::Client() const
46 return fOwner;
50 bool
51 BMediaConnection::HasBinding() const
53 CALLED();
55 return fBind != NULL;
59 BMediaConnection*
60 BMediaConnection::Binding() const
62 CALLED();
64 return fBind;
68 void
69 BMediaConnection::SetAcceptedFormat(const media_format& format)
71 CALLED();
73 fConnection.format = format;
77 const media_format&
78 BMediaConnection::AcceptedFormat() const
80 CALLED();
82 return fConnection.format;
86 bool
87 BMediaConnection::IsConnected() const
89 CALLED();
91 return fConnected;
95 status_t
96 BMediaConnection::Disconnect()
98 CALLED();
100 status_t ret = fOwner->_DisconnectConnection(this);
101 if (ret != B_OK)
102 return ret;
104 delete fBufferGroup;
105 fBufferGroup = NULL;
107 return ret;
111 status_t
112 BMediaConnection::Release()
114 CALLED();
116 status_t ret = fOwner->_ReleaseConnection(this);
117 if (ret != B_OK)
118 return ret;
120 delete this;
121 return ret;
125 size_t
126 BMediaConnection::BufferSize() const
128 CALLED();
130 switch (fConnection.format.type) {
131 case B_MEDIA_RAW_AUDIO:
132 return fConnection.format.u.raw_audio.buffer_size;
134 case B_MEDIA_RAW_VIDEO:
135 return fConnection.format.u.raw_video.display.bytes_per_row *
136 fConnection.format.u.raw_video.display.line_count;
138 default:
139 return 0;
144 void
145 BMediaConnection::Connected(const media_format& format)
147 fConnected = true;
151 void
152 BMediaConnection::Disconnected()
154 fConnected = false;
158 void
159 BMediaConnection::GetLatencyRange(bigtime_t* min, bigtime_t* max) const
161 CALLED();
163 *min = fMinLatency;
164 *max = fMaxLatency;
168 void
169 BMediaConnection::SetLatencyRange(bigtime_t min, bigtime_t max)
171 CALLED();
173 fMinLatency = min;
174 fMaxLatency = max;
178 void
179 BMediaConnection::_ConnectionRegistered(BMediaClient* owner,
180 media_connection_id id)
182 fOwner = owner;
183 fConnection.id = id;
184 fConnection.client = fOwner->Client();
186 if (fConnection.IsOutput()) {
187 fConnection.source.port = fOwner->fNode->ControlPort();
188 fConnection.source.id = fConnection.id;
190 fConnection.destination = media_destination::null;
191 } else {
192 fConnection.destination.port = fOwner->fNode->ControlPort();
193 fConnection.destination.id = fConnection.id;
195 fConnection.source = media_source::null;
200 const media_source&
201 BMediaConnection::_Source() const
203 return fConnection._Source();
207 const media_destination&
208 BMediaConnection::_Destination() const
210 return fConnection._Destination();
214 void BMediaConnection::_ReservedMediaConnection0() {}
215 void BMediaConnection::_ReservedMediaConnection1() {}
216 void BMediaConnection::_ReservedMediaConnection2() {}
217 void BMediaConnection::_ReservedMediaConnection3() {}
218 void BMediaConnection::_ReservedMediaConnection4() {}
219 void BMediaConnection::_ReservedMediaConnection5() {}
220 void BMediaConnection::_ReservedMediaConnection6() {}
221 void BMediaConnection::_ReservedMediaConnection7() {}
222 void BMediaConnection::_ReservedMediaConnection8() {}
223 void BMediaConnection::_ReservedMediaConnection9() {}
224 void BMediaConnection::_ReservedMediaConnection10() {}
227 BMediaInput::BMediaInput()
229 BMediaConnection(B_MEDIA_INPUT)
234 BMediaInput::~BMediaInput()
236 CALLED();
240 status_t
241 BMediaInput::FormatChanged(const media_format& format)
243 if (!format_is_compatible(format, AcceptedFormat()))
244 return B_MEDIA_BAD_FORMAT;
246 SetAcceptedFormat(format);
248 return B_OK;
252 void
253 BMediaInput::HandleBuffer(BBuffer* buffer)
255 CALLED();
260 media_input
261 BMediaInput::_MediaInput() const
263 return Connection()._MediaInput();
267 void BMediaInput::_ReservedMediaInput0() {}
268 void BMediaInput::_ReservedMediaInput1() {}
269 void BMediaInput::_ReservedMediaInput2() {}
270 void BMediaInput::_ReservedMediaInput3() {}
271 void BMediaInput::_ReservedMediaInput4() {}
272 void BMediaInput::_ReservedMediaInput5() {}
273 void BMediaInput::_ReservedMediaInput6() {}
274 void BMediaInput::_ReservedMediaInput7() {}
275 void BMediaInput::_ReservedMediaInput8() {}
276 void BMediaInput::_ReservedMediaInput9() {}
277 void BMediaInput::_ReservedMediaInput10() {}
280 BMediaOutput::BMediaOutput()
282 BMediaConnection(B_MEDIA_OUTPUT)
287 BMediaOutput::~BMediaOutput()
289 CALLED();
293 bool
294 BMediaOutput::IsEnabled() const
296 CALLED();
298 return fEnabled;
302 void
303 BMediaOutput::SetEnabled(bool enabled)
305 fEnabled = enabled;
309 status_t
310 BMediaOutput::PrepareToConnect(media_format* format)
312 if (!format_is_compatible(AcceptedFormat(), *format))
313 return B_ERROR;
315 SetAcceptedFormat(*format);
317 return B_OK;
321 status_t
322 BMediaOutput::FormatProposal(media_format* format)
324 *format = AcceptedFormat();
325 return B_OK;
329 status_t
330 BMediaOutput::FormatChangeRequested(media_format* format)
332 return B_ERROR;
336 status_t
337 BMediaOutput::SendBuffer(BBuffer* buffer)
339 CALLED();
341 if (!IsConnected())
342 return B_ERROR;
344 return fOwner->fNode->SendBuffer(buffer, this);
348 media_output
349 BMediaOutput::_MediaOutput() const
351 return Connection()._MediaOutput();
355 void BMediaOutput::_ReservedMediaOutput0() {}
356 void BMediaOutput::_ReservedMediaOutput1() {}
357 void BMediaOutput::_ReservedMediaOutput2() {}
358 void BMediaOutput::_ReservedMediaOutput3() {}
359 void BMediaOutput::_ReservedMediaOutput4() {}
360 void BMediaOutput::_ReservedMediaOutput5() {}
361 void BMediaOutput::_ReservedMediaOutput6() {}
362 void BMediaOutput::_ReservedMediaOutput7() {}
363 void BMediaOutput::_ReservedMediaOutput8() {}
364 void BMediaOutput::_ReservedMediaOutput9() {}
365 void BMediaOutput::_ReservedMediaOutput10() {}