1 /* 2 * Copyright 2015, Dario Casalinuovo. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 #include <MediaConnection.h> 7 8 #include "MediaClientNode.h" 9 10 #include "debug.h" 11 12 13 BMediaConnection::BMediaConnection(media_connection_kinds kinds) 14 : 15 fOwner(NULL), 16 fBind(NULL), 17 fBufferGroup(NULL), 18 fMinLatency(0), 19 fMaxLatency(0) 20 { 21 CALLED(); 22 23 fConnection.kinds = kinds; 24 fConnection.id = 0; 25 //fConnection.client = media_client::null; 26 } 27 28 29 BMediaConnection::~BMediaConnection() 30 { 31 CALLED(); 32 33 } 34 35 36 const media_connection& 37 BMediaConnection::Connection() const 38 { 39 return fConnection; 40 } 41 42 43 BMediaClient* 44 BMediaConnection::Client() const 45 { 46 return fOwner; 47 } 48 49 50 bool 51 BMediaConnection::HasBinding() const 52 { 53 CALLED(); 54 55 return fBind != NULL; 56 } 57 58 59 BMediaConnection* 60 BMediaConnection::Binding() const 61 { 62 CALLED(); 63 64 return fBind; 65 } 66 67 68 void 69 BMediaConnection::SetAcceptedFormat(const media_format& format) 70 { 71 CALLED(); 72 73 fConnection.format = format; 74 } 75 76 77 const media_format& 78 BMediaConnection::AcceptedFormat() const 79 { 80 CALLED(); 81 82 return fConnection.format; 83 } 84 85 86 bool 87 BMediaConnection::IsConnected() const 88 { 89 CALLED(); 90 91 return fConnected; 92 } 93 94 95 status_t 96 BMediaConnection::Disconnect() 97 { 98 CALLED(); 99 100 status_t ret = fOwner->_DisconnectConnection(this); 101 if (ret != B_OK) 102 return ret; 103 104 delete fBufferGroup; 105 fBufferGroup = NULL; 106 107 return ret; 108 } 109 110 111 status_t 112 BMediaConnection::Release() 113 { 114 CALLED(); 115 116 status_t ret = fOwner->_ReleaseConnection(this); 117 if (ret != B_OK) 118 return ret; 119 120 delete this; 121 return ret; 122 } 123 124 125 size_t 126 BMediaConnection::BufferSize() const 127 { 128 CALLED(); 129 130 switch (fConnection.format.type) { 131 case B_MEDIA_RAW_AUDIO: 132 return fConnection.format.u.raw_audio.buffer_size; 133 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; 137 138 default: 139 return 0; 140 } 141 } 142 143 144 void 145 BMediaConnection::Connected(const media_format& format) 146 { 147 fConnected = true; 148 } 149 150 151 void 152 BMediaConnection::Disconnected() 153 { 154 fConnected = false; 155 } 156 157 158 void 159 BMediaConnection::GetLatencyRange(bigtime_t* min, bigtime_t* max) const 160 { 161 CALLED(); 162 163 *min = fMinLatency; 164 *max = fMaxLatency; 165 } 166 167 168 void 169 BMediaConnection::SetLatencyRange(bigtime_t min, bigtime_t max) 170 { 171 CALLED(); 172 173 fMinLatency = min; 174 fMaxLatency = max; 175 } 176 177 178 void 179 BMediaConnection::_ConnectionRegistered(BMediaClient* owner, 180 media_connection_id id) 181 { 182 fOwner = owner; 183 fConnection.id = id; 184 fConnection.client = fOwner->Client(); 185 186 if (fConnection.IsOutput()) { 187 fConnection.source.port = fOwner->fNode->ControlPort(); 188 fConnection.source.id = fConnection.id; 189 190 fConnection.destination = media_destination::null; 191 } else { 192 fConnection.destination.port = fOwner->fNode->ControlPort(); 193 fConnection.destination.id = fConnection.id; 194 195 fConnection.source = media_source::null; 196 } 197 } 198 199 200 const media_source& 201 BMediaConnection::_Source() const 202 { 203 return fConnection._Source(); 204 } 205 206 207 const media_destination& 208 BMediaConnection::_Destination() const 209 { 210 return fConnection._Destination(); 211 } 212 213 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() {} 225 226 227 BMediaInput::BMediaInput() 228 : 229 BMediaConnection(B_MEDIA_INPUT) 230 { 231 } 232 233 234 BMediaInput::~BMediaInput() 235 { 236 CALLED(); 237 } 238 239 240 status_t 241 BMediaInput::FormatChanged(const media_format& format) 242 { 243 if (!format_is_compatible(format, AcceptedFormat())) 244 return B_MEDIA_BAD_FORMAT; 245 246 SetAcceptedFormat(format); 247 248 return B_OK; 249 } 250 251 252 void 253 BMediaInput::HandleBuffer(BBuffer* buffer) 254 { 255 CALLED(); 256 257 } 258 259 260 media_input 261 BMediaInput::_MediaInput() const 262 { 263 return Connection()._MediaInput(); 264 } 265 266 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() {} 278 279 280 BMediaOutput::BMediaOutput() 281 : 282 BMediaConnection(B_MEDIA_OUTPUT) 283 { 284 } 285 286 287 BMediaOutput::~BMediaOutput() 288 { 289 CALLED(); 290 } 291 292 293 bool 294 BMediaOutput::IsEnabled() const 295 { 296 CALLED(); 297 298 return fEnabled; 299 } 300 301 302 void 303 BMediaOutput::SetEnabled(bool enabled) 304 { 305 fEnabled = enabled; 306 } 307 308 309 status_t 310 BMediaOutput::PrepareToConnect(media_format* format) 311 { 312 if (!format_is_compatible(AcceptedFormat(), *format)) 313 return B_ERROR; 314 315 SetAcceptedFormat(*format); 316 317 return B_OK; 318 } 319 320 321 status_t 322 BMediaOutput::FormatProposal(media_format* format) 323 { 324 *format = AcceptedFormat(); 325 return B_OK; 326 } 327 328 329 status_t 330 BMediaOutput::FormatChangeRequested(media_format* format) 331 { 332 return B_ERROR; 333 } 334 335 336 status_t 337 BMediaOutput::SendBuffer(BBuffer* buffer) 338 { 339 CALLED(); 340 341 if (!IsConnected()) 342 return B_ERROR; 343 344 return fOwner->fNode->SendBuffer(buffer, this); 345 } 346 347 348 media_output 349 BMediaOutput::_MediaOutput() const 350 { 351 return Connection()._MediaOutput(); 352 } 353 354 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() {} 366