117 | | debugWarning("Transmit buffer underrun (cycle %d, FC=%d, PC=%d)\n", |
---|
118 | | cycle, m_framecounter, m_handler->getPacketCount()); |
---|
119 | | |
---|
120 | | // signal underrun |
---|
121 | | m_xruns++; |
---|
122 | | |
---|
123 | | retval=RAW1394_ISO_DEFER; |
---|
124 | | *length=0; |
---|
125 | | |
---|
126 | | } else { |
---|
127 | | retval=RAW1394_ISO_OK; |
---|
128 | | *length = read_size + 8; |
---|
129 | | |
---|
130 | | // process all ports that should be handled on a per-packet base |
---|
131 | | // this is MIDI for AMDTP (due to the need of DBC) |
---|
132 | | if (!encodePacketPorts((quadlet_t *)(data+8), nevents, packet->dbc)) { |
---|
133 | | debugWarning("Problem encoding Packet Ports\n"); |
---|
134 | | } |
---|
135 | | } |
---|
136 | | |
---|
137 | | *tag = IEC61883_TAG_WITH_CIP; |
---|
138 | | *sy = 0; |
---|
139 | | |
---|
140 | | // update the frame counter |
---|
141 | | m_framecounter+=nevents; |
---|
| 135 | /* there is no more data in the ringbuffer */ |
---|
| 136 | |
---|
| 137 | /* If there are already more than on period |
---|
| 138 | * of frames transfered to the XMIT buffer, there is no xrun. |
---|
| 139 | * |
---|
| 140 | */ |
---|
| 141 | if(m_framecounter>m_period) { |
---|
| 142 | // we stop processing this untill the next period boundary |
---|
| 143 | // that's when new data is ready |
---|
| 144 | |
---|
| 145 | //connection->pfd->events=0; |
---|
| 146 | |
---|
| 147 | // reset the cip to the old value |
---|
| 148 | memcpy(&m_cip_status,&old_cip,sizeof(struct iec61883_cip)); |
---|
| 149 | |
---|
| 150 | // retry this packed |
---|
| 151 | retval=RAW1394_ISO_AGAIN; |
---|
| 152 | nevents=0; |
---|
| 153 | debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "again!\n"); |
---|
| 154 | |
---|
| 155 | } else { |
---|
| 156 | debugWarning("Transmit buffer underrun (cycle %d, FC=%d, PC=%d)\n", |
---|
| 157 | cycle, m_framecounter, m_handler->getPacketCount()); |
---|
| 158 | |
---|
| 159 | // signal underrun |
---|
| 160 | m_xruns++; |
---|
| 161 | |
---|
| 162 | retval=RAW1394_ISO_DEFER; |
---|
| 163 | *length=0; |
---|
| 164 | nevents=0; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | } else { |
---|
| 168 | retval=RAW1394_ISO_OK; |
---|
| 169 | *length = read_size + 8; |
---|
| 170 | |
---|
| 171 | // process all ports that should be handled on a per-packet base |
---|
| 172 | // this is MIDI for AMDTP (due to the need of DBC) |
---|
| 173 | if (!encodePacketPorts((quadlet_t *)(data+8), nevents, packet->dbc)) { |
---|
| 174 | debugWarning("Problem encoding Packet Ports\n"); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | if (packet->syt != 0xFFFF) { |
---|
| 178 | unsigned int m_last_timestamp=ntohs(packet->syt); |
---|
| 179 | // reconstruct the top part of the timestamp using the current cycle number |
---|
| 180 | m_last_timestamp |= ((cycle << 12) & 0x01FF0000); |
---|
| 181 | |
---|
| 182 | debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"Sent packet with SYT for cycle %2d: %5u ticks (%2u cycles + %04u ticks)\n", |
---|
| 183 | cycle, |
---|
| 184 | CYCLE_COUNTER_TO_TICKS(m_last_timestamp), |
---|
| 185 | CYCLE_COUNTER_GET_CYCLES(m_last_timestamp), |
---|
| 186 | CYCLE_COUNTER_GET_TICKS(m_last_timestamp) |
---|
| 187 | ); |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | *tag = IEC61883_TAG_WITH_CIP; |
---|
| 192 | *sy = 0; |
---|
| 193 | |
---|
| 194 | // update the frame counter |
---|
| 195 | m_framecounter+=nevents; |
---|
182 | | debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing...\n"); |
---|
183 | | |
---|
184 | | // prepare all non-device specific stuff |
---|
185 | | // i.e. the iso stream and the associated ports |
---|
186 | | if(!TransmitStreamProcessor::prepare()) { |
---|
187 | | debugFatal("Could not prepare base class\n"); |
---|
188 | | return false; |
---|
189 | | } |
---|
190 | | |
---|
191 | | switch (m_framerate) { |
---|
192 | | case 32000: |
---|
193 | | m_syt_interval = 8; |
---|
194 | | m_fdf = IEC61883_FDF_SFC_32KHZ; |
---|
195 | | break; |
---|
196 | | case 44100: |
---|
197 | | m_syt_interval = 8; |
---|
198 | | m_fdf = IEC61883_FDF_SFC_44K1HZ; |
---|
199 | | break; |
---|
200 | | default: |
---|
201 | | case 48000: |
---|
202 | | m_syt_interval = 8; |
---|
203 | | m_fdf = IEC61883_FDF_SFC_48KHZ; |
---|
204 | | break; |
---|
205 | | case 88200: |
---|
206 | | m_syt_interval = 16; |
---|
207 | | m_fdf = IEC61883_FDF_SFC_88K2HZ; |
---|
208 | | break; |
---|
209 | | case 96000: |
---|
210 | | m_syt_interval = 16; |
---|
211 | | m_fdf = IEC61883_FDF_SFC_96KHZ; |
---|
212 | | break; |
---|
213 | | case 176400: |
---|
214 | | m_syt_interval = 32; |
---|
215 | | m_fdf = IEC61883_FDF_SFC_176K4HZ; |
---|
216 | | break; |
---|
217 | | case 192000: |
---|
218 | | m_syt_interval = 32; |
---|
219 | | m_fdf = IEC61883_FDF_SFC_192KHZ; |
---|
220 | | break; |
---|
221 | | } |
---|
222 | | |
---|
223 | | iec61883_cip_init ( |
---|
224 | | &m_cip_status, |
---|
225 | | IEC61883_FMT_AMDTP, |
---|
226 | | m_fdf, |
---|
227 | | m_framerate, |
---|
228 | | m_dimension, |
---|
229 | | m_syt_interval); |
---|
230 | | |
---|
231 | | // allocate the event buffer |
---|
232 | | if( !(m_event_buffer=freebob_ringbuffer_create( |
---|
233 | | (m_dimension * m_nb_buffers * m_period) * sizeof(quadlet_t)))) { |
---|
234 | | debugFatal("Could not allocate memory event ringbuffer"); |
---|
| 273 | debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing...\n"); |
---|
| 274 | |
---|
| 275 | // prepare all non-device specific stuff |
---|
| 276 | // i.e. the iso stream and the associated ports |
---|
| 277 | if(!TransmitStreamProcessor::prepare()) { |
---|
| 278 | debugFatal("Could not prepare base class\n"); |
---|
| 279 | return false; |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | switch (m_framerate) { |
---|
| 283 | case 32000: |
---|
| 284 | m_syt_interval = 8; |
---|
| 285 | m_fdf = IEC61883_FDF_SFC_32KHZ; |
---|
| 286 | break; |
---|
| 287 | case 44100: |
---|
| 288 | m_syt_interval = 8; |
---|
| 289 | m_fdf = IEC61883_FDF_SFC_44K1HZ; |
---|
| 290 | break; |
---|
| 291 | default: |
---|
| 292 | case 48000: |
---|
| 293 | m_syt_interval = 8; |
---|
| 294 | m_fdf = IEC61883_FDF_SFC_48KHZ; |
---|
| 295 | break; |
---|
| 296 | case 88200: |
---|
| 297 | m_syt_interval = 16; |
---|
| 298 | m_fdf = IEC61883_FDF_SFC_88K2HZ; |
---|
| 299 | break; |
---|
| 300 | case 96000: |
---|
| 301 | m_syt_interval = 16; |
---|
| 302 | m_fdf = IEC61883_FDF_SFC_96KHZ; |
---|
| 303 | break; |
---|
| 304 | case 176400: |
---|
| 305 | m_syt_interval = 32; |
---|
| 306 | m_fdf = IEC61883_FDF_SFC_176K4HZ; |
---|
| 307 | break; |
---|
| 308 | case 192000: |
---|
| 309 | m_syt_interval = 32; |
---|
| 310 | m_fdf = IEC61883_FDF_SFC_192KHZ; |
---|
| 311 | break; |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | iec61883_cip_init ( |
---|
| 315 | &m_cip_status, |
---|
| 316 | IEC61883_FMT_AMDTP, |
---|
| 317 | m_fdf, |
---|
| 318 | m_framerate, |
---|
| 319 | m_dimension, |
---|
| 320 | m_syt_interval); |
---|
| 321 | |
---|
| 322 | // allocate the event buffer |
---|
| 323 | unsigned int ringbuffer_size_frames=m_nb_buffers * m_period; |
---|
| 324 | |
---|
| 325 | // add the processing delay |
---|
| 326 | ringbuffer_size_frames+=(m_framerate * RECEIVE_PROCESSING_DELAY)/TICKS_PER_SECOND; |
---|
| 327 | |
---|
| 328 | if( !(m_event_buffer=freebob_ringbuffer_create( |
---|
| 329 | (m_dimension * ringbuffer_size_frames) * sizeof(quadlet_t)))) { |
---|
| 330 | debugFatal("Could not allocate memory event ringbuffer"); |
---|
245 | | } |
---|
246 | | |
---|
247 | | // set the parameters of ports we can: |
---|
248 | | // we want the audio ports to be period buffered, |
---|
249 | | // and the midi ports to be packet buffered |
---|
250 | | for ( PortVectorIterator it = m_Ports.begin(); |
---|
251 | | it != m_Ports.end(); |
---|
252 | | ++it ) |
---|
253 | | { |
---|
254 | | debugOutput(DEBUG_LEVEL_VERBOSE, "Setting up port %s\n",(*it)->getName().c_str()); |
---|
255 | | if(!(*it)->setBufferSize(m_period)) { |
---|
256 | | debugFatal("Could not set buffer size to %d\n",m_period); |
---|
257 | | return false; |
---|
258 | | } |
---|
259 | | |
---|
260 | | |
---|
261 | | switch ((*it)->getPortType()) { |
---|
262 | | case Port::E_Audio: |
---|
263 | | if(!(*it)->setSignalType(Port::E_PeriodSignalled)) { |
---|
264 | | debugFatal("Could not set signal type to PeriodSignalling"); |
---|
265 | | return false; |
---|
266 | | } |
---|
267 | | debugWarning("---------------- ! Doing hardcoded test setup ! --------------\n"); |
---|
268 | | // buffertype and datatype are dependant on the API |
---|
269 | | if(!(*it)->setBufferType(Port::E_PointerBuffer)) { |
---|
270 | | debugFatal("Could not set buffer type"); |
---|
271 | | return false; |
---|
272 | | } |
---|
273 | | if(!(*it)->useExternalBuffer(true)) { |
---|
274 | | debugFatal("Could not set external buffer usage"); |
---|
275 | | return false; |
---|
276 | | } |
---|
277 | | |
---|
278 | | if(!(*it)->setDataType(Port::E_Float)) { |
---|
279 | | debugFatal("Could not set data type"); |
---|
280 | | return false; |
---|
281 | | } |
---|
282 | | |
---|
283 | | |
---|
284 | | break; |
---|
285 | | case Port::E_Midi: |
---|
286 | | if(!(*it)->setSignalType(Port::E_PacketSignalled)) { |
---|
287 | | debugFatal("Could not set signal type to PeriodSignalling"); |
---|
288 | | return false; |
---|
289 | | } |
---|
290 | | |
---|
291 | | // we use a timing unit of 10ns |
---|
292 | | // this makes sure that for the max syt interval |
---|
293 | | // we don't have rounding, and keeps the numbers low |
---|
294 | | // we have 1 slot every 8 events |
---|
295 | | // we have syt_interval events per packet |
---|
296 | | // => syt_interval/8 slots per packet |
---|
297 | | // packet rate is 8000pkt/sec => interval=125us |
---|
298 | | // so the slot interval is (1/8000)/(syt_interval/8) |
---|
299 | | // or: 1/(1000 * syt_interval) sec |
---|
300 | | // which is 1e9/(1000*syt_interval) nsec |
---|
301 | | // or 100000/syt_interval 'units' |
---|
302 | | // the event interval is fixed to 320us = 32000 'units' |
---|
303 | | if(!(*it)->useRateControl(true,(100000/m_syt_interval),32000, false)) { |
---|
304 | | debugFatal("Could not set signal type to PeriodSignalling"); |
---|
305 | | return false; |
---|
306 | | } |
---|
307 | | |
---|
308 | | // buffertype and datatype are dependant on the API |
---|
309 | | debugWarning("---------------- ! Doing hardcoded test setup ! --------------\n"); |
---|
310 | | // buffertype and datatype are dependant on the API |
---|
311 | | if(!(*it)->setBufferType(Port::E_RingBuffer)) { |
---|
312 | | debugFatal("Could not set buffer type"); |
---|
313 | | return false; |
---|
314 | | } |
---|
315 | | if(!(*it)->setDataType(Port::E_MidiEvent)) { |
---|
316 | | debugFatal("Could not set data type"); |
---|
317 | | return false; |
---|
318 | | } |
---|
319 | | break; |
---|
320 | | default: |
---|
321 | | debugWarning("Unsupported port type specified\n"); |
---|
322 | | break; |
---|
323 | | } |
---|
324 | | } |
---|
325 | | |
---|
326 | | // the API specific settings of the ports should already be set, |
---|
327 | | // as this is called from the processorManager->prepare() |
---|
328 | | // so we can init the ports |
---|
329 | | if(!initPorts()) { |
---|
330 | | debugFatal("Could not initialize ports!\n"); |
---|
331 | | return false; |
---|
332 | | } |
---|
333 | | |
---|
334 | | if(!preparePorts()) { |
---|
335 | | debugFatal("Could not initialize ports!\n"); |
---|
336 | | return false; |
---|
337 | | } |
---|
338 | | |
---|
339 | | // we should prefill the event buffer |
---|
340 | | int i=m_nb_buffers; |
---|
341 | | while(i--) { |
---|
342 | | if(!transferSilence()) { |
---|
343 | | debugFatal("Could not prefill transmit stream\n"); |
---|
344 | | return false; |
---|
345 | | } |
---|
346 | | } |
---|
347 | | |
---|
348 | | debugOutput( DEBUG_LEVEL_VERBOSE, "Prepared for:\n"); |
---|
349 | | debugOutput( DEBUG_LEVEL_VERBOSE, " Samplerate: %d, FDF: %d, DBS: %d, SYT: %d\n", |
---|
350 | | m_framerate,m_fdf,m_dimension,m_syt_interval); |
---|
351 | | debugOutput( DEBUG_LEVEL_VERBOSE, " PeriodSize: %d, NbBuffers: %d\n", |
---|
352 | | m_period,m_nb_buffers); |
---|
353 | | debugOutput( DEBUG_LEVEL_VERBOSE, " Port: %d, Channel: %d\n", |
---|
354 | | m_port,m_channel); |
---|
355 | | |
---|
356 | | return true; |
---|
357 | | |
---|
358 | | } |
---|
359 | | |
---|
360 | | bool AmdtpTransmitStreamProcessor::transferSilence() { |
---|
361 | | /* a naive implementation would look like this: */ |
---|
362 | | |
---|
363 | | unsigned int write_size=m_period*sizeof(quadlet_t)*m_dimension; |
---|
364 | | char *dummybuffer=(char *)calloc(sizeof(quadlet_t),m_period*m_dimension); |
---|
365 | | transmitSilenceBlock(dummybuffer, m_period, 0); |
---|
366 | | |
---|
367 | | if (freebob_ringbuffer_write(m_event_buffer,(char *)(dummybuffer),write_size) < write_size) { |
---|
368 | | debugWarning("Could not write to event buffer\n"); |
---|
369 | | } |
---|
370 | | return true; |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | // set the parameters of ports we can: |
---|
| 344 | // we want the audio ports to be period buffered, |
---|
| 345 | // and the midi ports to be packet buffered |
---|
| 346 | for ( PortVectorIterator it = m_Ports.begin(); |
---|
| 347 | it != m_Ports.end(); |
---|
| 348 | ++it ) |
---|
| 349 | { |
---|
| 350 | debugOutput(DEBUG_LEVEL_VERBOSE, "Setting up port %s\n",(*it)->getName().c_str()); |
---|
| 351 | if(!(*it)->setBufferSize(m_period)) { |
---|
| 352 | debugFatal("Could not set buffer size to %d\n",m_period); |
---|
| 353 | return false; |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | |
---|
| 357 | switch ((*it)->getPortType()) { |
---|
| 358 | case Port::E_Audio: |
---|
| 359 | if(!(*it)->setSignalType(Port::E_PeriodSignalled)) { |
---|
| 360 | debugFatal("Could not set signal type to PeriodSignalling"); |
---|
| 361 | return false; |
---|
| 362 | } |
---|
| 363 | debugWarning("---------------- ! Doing hardcoded test setup ! --------------\n"); |
---|
| 364 | // buffertype and datatype are dependant on the API |
---|
| 365 | if(!(*it)->setBufferType(Port::E_PointerBuffer)) { |
---|
| 366 | debugFatal("Could not set buffer type"); |
---|
| 367 | return false; |
---|
| 368 | } |
---|
| 369 | if(!(*it)->useExternalBuffer(true)) { |
---|
| 370 | debugFatal("Could not set external buffer usage"); |
---|
| 371 | return false; |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | if(!(*it)->setDataType(Port::E_Float)) { |
---|
| 375 | debugFatal("Could not set data type"); |
---|
| 376 | return false; |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | |
---|
| 380 | break; |
---|
| 381 | case Port::E_Midi: |
---|
| 382 | if(!(*it)->setSignalType(Port::E_PacketSignalled)) { |
---|
| 383 | debugFatal("Could not set signal type to PeriodSignalling"); |
---|
| 384 | return false; |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | // we use a timing unit of 10ns |
---|
| 388 | // this makes sure that for the max syt interval |
---|
| 389 | // we don't have rounding, and keeps the numbers low |
---|
| 390 | // we have 1 slot every 8 events |
---|
| 391 | // we have syt_interval events per packet |
---|
| 392 | // => syt_interval/8 slots per packet |
---|
| 393 | // packet rate is 8000pkt/sec => interval=125us |
---|
| 394 | // so the slot interval is (1/8000)/(syt_interval/8) |
---|
| 395 | // or: 1/(1000 * syt_interval) sec |
---|
| 396 | // which is 1e9/(1000*syt_interval) nsec |
---|
| 397 | // or 100000/syt_interval 'units' |
---|
| 398 | // the event interval is fixed to 320us = 32000 'units' |
---|
| 399 | if(!(*it)->useRateControl(true,(100000/m_syt_interval),32000, false)) { |
---|
| 400 | debugFatal("Could not set signal type to PeriodSignalling"); |
---|
| 401 | return false; |
---|
| 402 | } |
---|
| 403 | |
---|
| 404 | // buffertype and datatype are dependant on the API |
---|
| 405 | debugWarning("---------------- ! Doing hardcoded test setup ! --------------\n"); |
---|
| 406 | // buffertype and datatype are dependant on the API |
---|
| 407 | if(!(*it)->setBufferType(Port::E_RingBuffer)) { |
---|
| 408 | debugFatal("Could not set buffer type"); |
---|
| 409 | return false; |
---|
| 410 | } |
---|
| 411 | if(!(*it)->setDataType(Port::E_MidiEvent)) { |
---|
| 412 | debugFatal("Could not set data type"); |
---|
| 413 | return false; |
---|
| 414 | } |
---|
| 415 | break; |
---|
| 416 | default: |
---|
| 417 | debugWarning("Unsupported port type specified\n"); |
---|
| 418 | break; |
---|
| 419 | } |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | // the API specific settings of the ports should already be set, |
---|
| 423 | // as this is called from the processorManager->prepare() |
---|
| 424 | // so we can init the ports |
---|
| 425 | if(!initPorts()) { |
---|
| 426 | debugFatal("Could not initialize ports!\n"); |
---|
| 427 | return false; |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | if(!preparePorts()) { |
---|
| 431 | debugFatal("Could not initialize ports!\n"); |
---|
| 432 | return false; |
---|
| 433 | } |
---|
| 434 | |
---|
| 435 | // we should prefill the event buffer |
---|
| 436 | if (!prefill()) { |
---|
| 437 | debugFatal("Could not prefill buffers\n"); |
---|
| 438 | return false; |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | debugOutput( DEBUG_LEVEL_VERBOSE, "Prepared for:\n"); |
---|
| 442 | debugOutput( DEBUG_LEVEL_VERBOSE, " Samplerate: %d, FDF: %d, DBS: %d, SYT: %d\n", |
---|
| 443 | m_framerate,m_fdf,m_dimension,m_syt_interval); |
---|
| 444 | debugOutput( DEBUG_LEVEL_VERBOSE, " PeriodSize: %d, NbBuffers: %d\n", |
---|
| 445 | m_period,m_nb_buffers); |
---|
| 446 | debugOutput( DEBUG_LEVEL_VERBOSE, " Port: %d, Channel: %d\n", |
---|
| 447 | m_port,m_channel); |
---|
| 448 | |
---|
| 449 | return true; |
---|
| 450 | |
---|
| 451 | } |
---|
| 452 | |
---|
| 453 | bool AmdtpTransmitStreamProcessor::transferSilence(unsigned int size) { |
---|
| 454 | /* a naive implementation would look like this: */ |
---|
| 455 | |
---|
| 456 | unsigned int write_size=size*sizeof(quadlet_t)*m_dimension; |
---|
| 457 | char *dummybuffer=(char *)calloc(sizeof(quadlet_t),size*m_dimension); |
---|
| 458 | transmitSilenceBlock(dummybuffer, size, 0); |
---|
| 459 | |
---|
| 460 | if (freebob_ringbuffer_write(m_event_buffer,(char *)(dummybuffer),write_size) < write_size) { |
---|
| 461 | debugWarning("Could not write to event buffer\n"); |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | free(dummybuffer); |
---|
| 465 | |
---|
| 466 | return true; |
---|
392 | | int xrun; |
---|
393 | | unsigned int offset=0; |
---|
394 | | |
---|
395 | | freebob_ringbuffer_data_t vec[2]; |
---|
396 | | // we received one period of frames |
---|
397 | | // this is period_size*dimension of events |
---|
398 | | int events2write=m_period*m_dimension; |
---|
399 | | int bytes2write=events2write*sizeof(quadlet_t); |
---|
400 | | |
---|
401 | | /* write events2write bytes to the ringbuffer |
---|
402 | | * first see if it can be done in one read. |
---|
403 | | * if so, ok. |
---|
404 | | * otherwise write up to a multiple of clusters directly to the buffer |
---|
405 | | * then do the buffer wrap around using ringbuffer_write |
---|
406 | | * then write the remaining data directly to the buffer in a third pass |
---|
407 | | * Make sure that we cannot end up on a non-cluster aligned position! |
---|
408 | | */ |
---|
409 | | int cluster_size=m_dimension*sizeof(quadlet_t); |
---|
410 | | |
---|
411 | | while(bytes2write>0) { |
---|
412 | | int byteswritten=0; |
---|
413 | | |
---|
414 | | unsigned int frameswritten=(m_period*cluster_size-bytes2write)/cluster_size; |
---|
415 | | offset=frameswritten; |
---|
416 | | |
---|
417 | | freebob_ringbuffer_get_write_vector(m_event_buffer, vec); |
---|
418 | | |
---|
419 | | if(vec[0].len==0) { // this indicates a full event buffer |
---|
420 | | debugError("XMT: Event buffer overrun in processor %p\n",this); |
---|
421 | | break; |
---|
422 | | } |
---|
423 | | |
---|
424 | | /* if we don't take care we will get stuck in an infinite loop |
---|
425 | | * because we align to a cluster boundary later |
---|
426 | | * the remaining nb of bytes in one write operation can be |
---|
427 | | * smaller than one cluster |
---|
428 | | * this can happen because the ringbuffer size is always a power of 2 |
---|
429 | | */ |
---|
430 | | if(vec[0].len<cluster_size) { |
---|
431 | | |
---|
432 | | // encode to the temporary buffer |
---|
433 | | xrun = transmitBlock(m_cluster_buffer, 1, offset); |
---|
434 | | |
---|
435 | | if(xrun<0) { |
---|
436 | | // xrun detected |
---|
437 | | debugError("XMT: Frame buffer underrun in processor %p\n",this); |
---|
438 | | break; |
---|
439 | | } |
---|
440 | | |
---|
441 | | // use the ringbuffer function to write one cluster |
---|
442 | | // the write function handles the wrap around. |
---|
443 | | freebob_ringbuffer_write(m_event_buffer, |
---|
444 | | m_cluster_buffer, |
---|
445 | | cluster_size); |
---|
446 | | |
---|
447 | | // we advanced one cluster_size |
---|
448 | | bytes2write-=cluster_size; |
---|
449 | | |
---|
450 | | } else { // |
---|
451 | | |
---|
452 | | if(bytes2write>vec[0].len) { |
---|
453 | | // align to a cluster boundary |
---|
454 | | byteswritten=vec[0].len-(vec[0].len%cluster_size); |
---|
455 | | } else { |
---|
456 | | byteswritten=bytes2write; |
---|
457 | | } |
---|
458 | | |
---|
459 | | xrun = transmitBlock(vec[0].buf, |
---|
460 | | byteswritten/cluster_size, |
---|
461 | | offset); |
---|
462 | | |
---|
463 | | if(xrun<0) { |
---|
464 | | // xrun detected |
---|
465 | | debugError("XMT: Frame buffer underrun in processor %p\n",this); |
---|
466 | | break; |
---|
467 | | } |
---|
468 | | |
---|
469 | | freebob_ringbuffer_write_advance(m_event_buffer, byteswritten); |
---|
470 | | bytes2write -= byteswritten; |
---|
471 | | } |
---|
472 | | |
---|
473 | | // the bytes2write should always be cluster aligned |
---|
474 | | assert(bytes2write%cluster_size==0); |
---|
475 | | |
---|
476 | | } |
---|
477 | | |
---|
478 | | return true; |
---|
| 488 | int xrun; |
---|
| 489 | unsigned int offset=0; |
---|
| 490 | |
---|
| 491 | freebob_ringbuffer_data_t vec[2]; |
---|
| 492 | // we received one period of frames |
---|
| 493 | // this is period_size*dimension of events |
---|
| 494 | int events2write=m_period*m_dimension; |
---|
| 495 | int bytes2write=events2write*sizeof(quadlet_t); |
---|
| 496 | |
---|
| 497 | /* write events2write bytes to the ringbuffer |
---|
| 498 | * first see if it can be done in one read. |
---|
| 499 | * if so, ok. |
---|
| 500 | * otherwise write up to a multiple of clusters directly to the buffer |
---|
| 501 | * then do the buffer wrap around using ringbuffer_write |
---|
| 502 | * then write the remaining data directly to the buffer in a third pass |
---|
| 503 | * Make sure that we cannot end up on a non-cluster aligned position! |
---|
| 504 | */ |
---|
| 505 | int cluster_size=m_dimension*sizeof(quadlet_t); |
---|
| 506 | |
---|
| 507 | while(bytes2write>0) { |
---|
| 508 | int byteswritten=0; |
---|
| 509 | |
---|
| 510 | unsigned int frameswritten=(m_period*cluster_size-bytes2write)/cluster_size; |
---|
| 511 | offset=frameswritten; |
---|
| 512 | |
---|
| 513 | freebob_ringbuffer_get_write_vector(m_event_buffer, vec); |
---|
| 514 | |
---|
| 515 | if(vec[0].len==0) { // this indicates a full event buffer |
---|
| 516 | debugError("XMT: Event buffer overrun in processor %p\n",this); |
---|
| 517 | break; |
---|
| 518 | } |
---|
| 519 | |
---|
| 520 | /* if we don't take care we will get stuck in an infinite loop |
---|
| 521 | * because we align to a cluster boundary later |
---|
| 522 | * the remaining nb of bytes in one write operation can be |
---|
| 523 | * smaller than one cluster |
---|
| 524 | * this can happen because the ringbuffer size is always a power of 2 |
---|
| 525 | */ |
---|
| 526 | if(vec[0].len<cluster_size) { |
---|
| 527 | |
---|
| 528 | // encode to the temporary buffer |
---|
| 529 | xrun = transmitBlock(m_cluster_buffer, 1, offset); |
---|
| 530 | |
---|
| 531 | if(xrun<0) { |
---|
| 532 | // xrun detected |
---|
| 533 | debugError("XMT: Frame buffer underrun in processor %p\n",this); |
---|
| 534 | break; |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | // use the ringbuffer function to write one cluster |
---|
| 538 | // the write function handles the wrap around. |
---|
| 539 | freebob_ringbuffer_write(m_event_buffer, |
---|
| 540 | m_cluster_buffer, |
---|
| 541 | cluster_size); |
---|
| 542 | |
---|
| 543 | // we advanced one cluster_size |
---|
| 544 | bytes2write-=cluster_size; |
---|
| 545 | |
---|
| 546 | } else { // |
---|
| 547 | |
---|
| 548 | if(bytes2write>vec[0].len) { |
---|
| 549 | // align to a cluster boundary |
---|
| 550 | byteswritten=vec[0].len-(vec[0].len%cluster_size); |
---|
| 551 | } else { |
---|
| 552 | byteswritten=bytes2write; |
---|
| 553 | } |
---|
| 554 | |
---|
| 555 | xrun = transmitBlock(vec[0].buf, |
---|
| 556 | byteswritten/cluster_size, |
---|
| 557 | offset); |
---|
| 558 | |
---|
| 559 | if(xrun<0) { |
---|
| 560 | // xrun detected |
---|
| 561 | debugError("XMT: Frame buffer underrun in processor %p\n",this); |
---|
| 562 | break; |
---|
| 563 | } |
---|
| 564 | |
---|
| 565 | freebob_ringbuffer_write_advance(m_event_buffer, byteswritten); |
---|
| 566 | bytes2write -= byteswritten; |
---|
| 567 | } |
---|
| 568 | |
---|
| 569 | // the bytes2write should always be cluster aligned |
---|
| 570 | assert(bytes2write%cluster_size==0); |
---|
| 571 | |
---|
| 572 | } |
---|
| 573 | |
---|
| 574 | return true; |
---|
578 | | |
---|
579 | | AmdtpMidiPort *mp=static_cast<AmdtpMidiPort *>(*it); |
---|
580 | | |
---|
581 | | // we encode this directly (no function call) due to the high frequency |
---|
582 | | /* idea: |
---|
583 | | spec says: current_midi_port=(dbc+j)%8; |
---|
584 | | => if we start at (dbc+stream->location-1)%8 [due to location_min=1], |
---|
585 | | we'll start at the right event for the midi port. |
---|
586 | | => if we increment j with 8, we stay at the right event. |
---|
587 | | */ |
---|
588 | | // FIXME: as we know in advance how big a packet is (syt_interval) we can |
---|
589 | | // predict how much loops will be present here |
---|
590 | | // first prefill the buffer with NO_DATA's on all time muxed channels |
---|
591 | | |
---|
592 | | for(j = (dbc & 0x07)+mp->getLocation()-1; j < nevents; j += 8) { |
---|
593 | | |
---|
594 | | target_event=(quadlet_t *)(data + ((j * m_dimension) + mp->getPosition())); |
---|
595 | | |
---|
596 | | if(mp->canRead()) { // we can send a byte |
---|
597 | | mp->readEvent(&byte); |
---|
598 | | *target_event=htonl( |
---|
599 | | IEC61883_AM824_SET_LABEL((byte)<<16, |
---|
600 | | IEC61883_AM824_LABEL_MIDI_1X)); |
---|
601 | | } else { |
---|
602 | | // can't send a byte, either because there is no byte, |
---|
603 | | // or because this would exceed the maximum rate |
---|
604 | | *target_event=htonl( |
---|
605 | | IEC61883_AM824_SET_LABEL(0,IEC61883_AM824_LABEL_MIDI_NO_DATA)); |
---|
606 | | } |
---|
607 | | } |
---|
608 | | |
---|
609 | | } |
---|
610 | | |
---|
611 | | return ok; |
---|
| 674 | |
---|
| 675 | AmdtpMidiPort *mp=static_cast<AmdtpMidiPort *>(*it); |
---|
| 676 | |
---|
| 677 | // we encode this directly (no function call) due to the high frequency |
---|
| 678 | /* idea: |
---|
| 679 | spec says: current_midi_port=(dbc+j)%8; |
---|
| 680 | => if we start at (dbc+stream->location-1)%8 [due to location_min=1], |
---|
| 681 | we'll start at the right event for the midi port. |
---|
| 682 | => if we increment j with 8, we stay at the right event. |
---|
| 683 | */ |
---|
| 684 | // FIXME: as we know in advance how big a packet is (syt_interval) we can |
---|
| 685 | // predict how much loops will be present here |
---|
| 686 | // first prefill the buffer with NO_DATA's on all time muxed channels |
---|
| 687 | |
---|
| 688 | for(j = (dbc & 0x07)+mp->getLocation()-1; j < nevents; j += 8) { |
---|
| 689 | |
---|
| 690 | target_event=(quadlet_t *)(data + ((j * m_dimension) + mp->getPosition())); |
---|
| 691 | |
---|
| 692 | if(mp->canRead()) { // we can send a byte |
---|
| 693 | mp->readEvent(&byte); |
---|
| 694 | *target_event=htonl( |
---|
| 695 | IEC61883_AM824_SET_LABEL((byte)<<16, |
---|
| 696 | IEC61883_AM824_LABEL_MIDI_1X)); |
---|
| 697 | } else { |
---|
| 698 | // can't send a byte, either because there is no byte, |
---|
| 699 | // or because this would exceed the maximum rate |
---|
| 700 | *target_event=htonl( |
---|
| 701 | IEC61883_AM824_SET_LABEL(0,IEC61883_AM824_LABEL_MIDI_NO_DATA)); |
---|
| 702 | } |
---|
| 703 | } |
---|
| 704 | |
---|
| 705 | } |
---|
| 706 | |
---|
| 707 | return ok; |
---|
618 | | unsigned int j=0; |
---|
619 | | |
---|
620 | | quadlet_t *target_event; |
---|
621 | | |
---|
622 | | target_event=(quadlet_t *)(data + p->getPosition()); |
---|
623 | | |
---|
624 | | switch(p->getDataType()) { |
---|
625 | | default: |
---|
626 | | case Port::E_Int24: |
---|
627 | | { |
---|
628 | | quadlet_t *buffer=(quadlet_t *)(p->getBufferAddress()); |
---|
629 | | |
---|
630 | | assert(nevents + offset <= p->getBufferSize()); |
---|
631 | | |
---|
632 | | buffer+=offset; |
---|
633 | | |
---|
634 | | for(j = 0; j < nevents; j += 1) { // decode max nsamples |
---|
635 | | *target_event = htonl((*(buffer) & 0x00FFFFFF) | 0x40000000); |
---|
636 | | buffer++; |
---|
637 | | target_event += m_dimension; |
---|
638 | | } |
---|
639 | | } |
---|
640 | | break; |
---|
641 | | case Port::E_Float: |
---|
642 | | { |
---|
643 | | const float multiplier = (float)(0x7FFFFF00); |
---|
644 | | float *buffer=(float *)(p->getBufferAddress()); |
---|
645 | | |
---|
646 | | assert(nevents + offset <= p->getBufferSize()); |
---|
647 | | |
---|
648 | | buffer+=offset; |
---|
649 | | |
---|
650 | | for(j = 0; j < nevents; j += 1) { // decode max nsamples |
---|
651 | | |
---|
652 | | // don't care for overflow |
---|
653 | | float v = *buffer * multiplier; // v: -231 .. 231 |
---|
654 | | unsigned int tmp = ((int)v); |
---|
655 | | *target_event = htonl((tmp >> 8) | 0x40000000); |
---|
656 | | |
---|
657 | | buffer++; |
---|
658 | | target_event += m_dimension; |
---|
659 | | } |
---|
660 | | } |
---|
661 | | break; |
---|
662 | | } |
---|
663 | | |
---|
664 | | return 0; |
---|
| 714 | unsigned int j=0; |
---|
| 715 | |
---|
| 716 | quadlet_t *target_event; |
---|
| 717 | |
---|
| 718 | target_event=(quadlet_t *)(data + p->getPosition()); |
---|
| 719 | |
---|
| 720 | switch(p->getDataType()) { |
---|
| 721 | default: |
---|
| 722 | case Port::E_Int24: |
---|
| 723 | { |
---|
| 724 | quadlet_t *buffer=(quadlet_t *)(p->getBufferAddress()); |
---|
| 725 | |
---|
| 726 | assert(nevents + offset <= p->getBufferSize()); |
---|
| 727 | |
---|
| 728 | buffer+=offset; |
---|
| 729 | |
---|
| 730 | for(j = 0; j < nevents; j += 1) { // decode max nsamples |
---|
| 731 | *target_event = htonl((*(buffer) & 0x00FFFFFF) | 0x40000000); |
---|
| 732 | buffer++; |
---|
| 733 | target_event += m_dimension; |
---|
| 734 | } |
---|
| 735 | } |
---|
| 736 | break; |
---|
| 737 | case Port::E_Float: |
---|
| 738 | { |
---|
| 739 | const float multiplier = (float)(0x7FFFFF00); |
---|
| 740 | float *buffer=(float *)(p->getBufferAddress()); |
---|
| 741 | |
---|
| 742 | assert(nevents + offset <= p->getBufferSize()); |
---|
| 743 | |
---|
| 744 | buffer+=offset; |
---|
| 745 | |
---|
| 746 | for(j = 0; j < nevents; j += 1) { // decode max nsamples |
---|
| 747 | |
---|
| 748 | // don't care for overflow |
---|
| 749 | float v = *buffer * multiplier; // v: -231 .. 231 |
---|
| 750 | unsigned int tmp = ((int)v); |
---|
| 751 | *target_event = htonl((tmp >> 8) | 0x40000000); |
---|
| 752 | |
---|
| 753 | buffer++; |
---|
| 754 | target_event += m_dimension; |
---|
| 755 | } |
---|
| 756 | } |
---|
| 757 | break; |
---|
| 758 | } |
---|
| 759 | |
---|
| 760 | return 0; |
---|
706 | | // call the parent init |
---|
707 | | // this has to be done before allocating the buffers, |
---|
708 | | // because this sets the buffersizes from the processormanager |
---|
709 | | if(!ReceiveStreamProcessor::init()) { |
---|
710 | | debugFatal("Could not do base class init (%d)\n",this); |
---|
711 | | return false; |
---|
712 | | } |
---|
713 | | |
---|
714 | | return true; |
---|
715 | | } |
---|
716 | | |
---|
717 | | int AmdtpReceiveStreamProcessor::putPacket(unsigned char *data, unsigned int length, |
---|
718 | | unsigned char channel, unsigned char tag, unsigned char sy, |
---|
719 | | unsigned int cycle, unsigned int dropped) { |
---|
720 | | |
---|
721 | | enum raw1394_iso_disposition retval=RAW1394_ISO_OK; |
---|
722 | | |
---|
723 | | struct iec61883_packet *packet = (struct iec61883_packet *) data; |
---|
724 | | assert(packet); |
---|
725 | | |
---|
726 | | // how are we going to get this right??? |
---|
727 | | m_running=true; |
---|
728 | | |
---|
729 | | if((packet->fmt == 0x10) && (packet->fdf != 0xFF) && (packet->dbs>0) && (length>=2*sizeof(quadlet_t))) { |
---|
730 | | unsigned int nevents=((length / sizeof (quadlet_t)) - 2)/packet->dbs; |
---|
731 | | |
---|
732 | | // signal that we're running |
---|
733 | | // if(nevents) m_running=true; |
---|
734 | | |
---|
735 | | // don't process the stream when it is not enabled. |
---|
736 | | if(m_disabled) { |
---|
737 | | return (int)RAW1394_ISO_OK; |
---|
738 | | } |
---|
739 | | |
---|
740 | | unsigned int write_size=nevents*sizeof(quadlet_t)*m_dimension; |
---|
741 | | // add the data payload to the ringbuffer |
---|
742 | | |
---|
743 | | if (freebob_ringbuffer_write(m_event_buffer,(char *)(data+8),write_size) < write_size) |
---|
744 | | { |
---|
745 | | debugWarning("Receive buffer overrun (cycle %d, FC=%d, PC=%d)\n", |
---|
746 | | cycle, m_framecounter, m_handler->getPacketCount()); |
---|
747 | | m_xruns++; |
---|
748 | | |
---|
749 | | retval=RAW1394_ISO_DEFER; |
---|
750 | | } else { |
---|
751 | | retval=RAW1394_ISO_OK; |
---|
752 | | // process all ports that should be handled on a per-packet base |
---|
753 | | // this is MIDI for AMDTP (due to the need of DBC) |
---|
754 | | if (!decodePacketPorts((quadlet_t *)(data+8), nevents, packet->dbc)) { |
---|
755 | | debugWarning("Problem decoding Packet Ports\n"); |
---|
756 | | retval=RAW1394_ISO_DEFER; |
---|
757 | | } |
---|
758 | | } |
---|
759 | | |
---|
760 | | debugOutput(DEBUG_LEVEL_VERY_VERBOSE, |
---|
761 | | "RCV: CH = %d, FDF = %X. SYT = %6d, DBS = %3d, DBC = %3d, FMT = %3d, LEN = %4d (%2d)\n", |
---|
762 | | channel, packet->fdf, |
---|
763 | | packet->syt, |
---|
764 | | packet->dbs, |
---|
765 | | packet->dbc, |
---|
766 | | packet->fmt, |
---|
767 | | length, |
---|
768 | | ((length / sizeof (quadlet_t)) - 2)/packet->dbs); |
---|
769 | | |
---|
770 | | // update the frame counter |
---|
771 | | m_framecounter+=nevents; |
---|
772 | | if(m_framecounter>m_period) { |
---|
773 | | retval=RAW1394_ISO_DEFER; |
---|
774 | | } |
---|
775 | | |
---|
776 | | } else { |
---|
777 | | // discard packet |
---|
778 | | // can be important for sync though |
---|
779 | | } |
---|
780 | | |
---|
| 802 | // call the parent init |
---|
| 803 | // this has to be done before allocating the buffers, |
---|
| 804 | // because this sets the buffersizes from the processormanager |
---|
| 805 | if(!ReceiveStreamProcessor::init()) { |
---|
| 806 | debugFatal("Could not do base class init (%d)\n",this); |
---|
| 807 | return false; |
---|
| 808 | } |
---|
| 809 | |
---|
| 810 | return true; |
---|
| 811 | } |
---|
| 812 | |
---|
| 813 | enum raw1394_iso_disposition |
---|
| 814 | AmdtpReceiveStreamProcessor::putPacket(unsigned char *data, unsigned int length, |
---|
| 815 | unsigned char channel, unsigned char tag, unsigned char sy, |
---|
| 816 | unsigned int cycle, unsigned int dropped) { |
---|
| 817 | |
---|
| 818 | enum raw1394_iso_disposition retval=RAW1394_ISO_OK; |
---|
| 819 | |
---|
| 820 | struct iec61883_packet *packet = (struct iec61883_packet *) data; |
---|
| 821 | assert(packet); |
---|
| 822 | |
---|
| 823 | // how are we going to get this right??? |
---|
| 824 | // m_running=true; |
---|
| 825 | |
---|
| 826 | if((packet->fmt == 0x10) && (packet->fdf != 0xFF) && (packet->dbs>0) && (length>=2*sizeof(quadlet_t))) { |
---|
| 827 | unsigned int nevents=((length / sizeof (quadlet_t)) - 2)/packet->dbs; |
---|
| 828 | |
---|
| 829 | // signal that we're running |
---|
| 830 | if(nevents) m_running=true; |
---|
| 831 | |
---|
| 832 | // don't process the stream when it is not enabled. |
---|
| 833 | if(m_disabled) { |
---|
| 834 | return RAW1394_ISO_OK; |
---|
| 835 | } |
---|
| 836 | debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "put packet...\n"); |
---|
| 837 | |
---|
| 838 | unsigned int write_size=nevents*sizeof(quadlet_t)*m_dimension; |
---|
| 839 | // add the data payload to the ringbuffer |
---|
| 840 | |
---|
| 841 | if (freebob_ringbuffer_write(m_event_buffer,(char *)(data+8),write_size) < write_size) |
---|
| 842 | { |
---|
| 843 | debugWarning("Receive buffer overrun (cycle %d, FC=%d, PC=%d)\n", |
---|
| 844 | cycle, m_framecounter, m_handler->getPacketCount()); |
---|
| 845 | m_xruns++; |
---|
| 846 | |
---|
| 847 | retval=RAW1394_ISO_DEFER; |
---|
| 848 | } else { |
---|
| 849 | retval=RAW1394_ISO_OK; |
---|
| 850 | // process all ports that should be handled on a per-packet base |
---|
| 851 | // this is MIDI for AMDTP (due to the need of DBC) |
---|
| 852 | if (!decodePacketPorts((quadlet_t *)(data+8), nevents, packet->dbc)) { |
---|
| 853 | debugWarning("Problem decoding Packet Ports\n"); |
---|
| 854 | retval=RAW1394_ISO_DEFER; |
---|
| 855 | } |
---|
| 856 | |
---|
| 857 | // do the time stamp processing |
---|
| 858 | // put the last time stamp a variable |
---|
| 859 | // this will allow us to determine the |
---|
| 860 | // actual presentation time later |
---|
| 861 | if (packet->syt != 0xFFFF) { |
---|
| 862 | |
---|
| 863 | m_last_timestamp=ntohs(packet->syt); |
---|
| 864 | // reconstruct the top part of the timestamp using the current cycle number |
---|
| 865 | // m_last_timestamp |= ((cycle << 12) & 0x01FF0000); |
---|
| 866 | unsigned int syt_cycles=CYCLE_COUNTER_GET_CYCLES(m_last_timestamp); |
---|
| 867 | int new_cycles=cycle+(syt_cycles-(cycle & 0xF)); |
---|
| 868 | // m_last_timestamp &= 0xFFF; // keep only the offset |
---|
| 869 | // m_last_timestamp |= ((new_cycles << 12) & 0x01FFF000); // add the right cycle info |
---|
| 870 | |
---|
| 871 | debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"R-SYT for cycle (%2d %2d)=>%2d: %5uT (%04uC + %04uT) %04X %04X %d\n", |
---|
| 872 | cycle,cycle & 0xF,new_cycles, |
---|
| 873 | CYCLE_COUNTER_TO_TICKS(m_last_timestamp), |
---|
| 874 | CYCLE_COUNTER_GET_CYCLES(m_last_timestamp), |
---|
| 875 | CYCLE_COUNTER_GET_TICKS(m_last_timestamp), |
---|
| 876 | ntohs(packet->syt),m_last_timestamp&0xFFFF, dropped |
---|
| 877 | ); |
---|
| 878 | } |
---|
| 879 | } |
---|
| 880 | |
---|
| 881 | debugOutput(DEBUG_LEVEL_VERY_VERBOSE, |
---|
| 882 | "RCV: CH = %d, FDF = %X. SYT = %6d, DBS = %3d, DBC = %3d, FMT = %3d, LEN = %4d (%2d)\n", |
---|
| 883 | channel, packet->fdf, |
---|
| 884 | packet->syt, |
---|
| 885 | packet->dbs, |
---|
| 886 | packet->dbc, |
---|
| 887 | packet->fmt, |
---|
| 888 | length, |
---|
| 889 | ((length / sizeof (quadlet_t)) - 2)/packet->dbs); |
---|
| 890 | |
---|
| 891 | // update the frame counter |
---|
| 892 | m_framecounter+=nevents; |
---|
| 893 | if(m_framecounter>m_period) { |
---|
| 894 | retval=RAW1394_ISO_DEFER; |
---|
| 895 | debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"defer!\n"); |
---|
| 896 | } |
---|
| 897 | |
---|
| 898 | } else { |
---|
| 899 | // discard packet |
---|
| 900 | // can be important for sync though |
---|
| 901 | } |
---|
| 902 | |
---|
782 | | |
---|
783 | | return (int)retval; |
---|
| 904 | |
---|
| 905 | return retval; |
---|
| 906 | } |
---|
| 907 | |
---|
| 908 | // this uses SYT to determine if one period is ready |
---|
| 909 | bool AmdtpReceiveStreamProcessor::isOnePeriodReady() { |
---|
| 910 | #if 0 // this code is not ready yet |
---|
| 911 | |
---|
| 912 | // one sample will take a number off cycle counter ticks: |
---|
| 913 | // The number of ticks per second is 24576000 |
---|
| 914 | // The number of samples per second is Fs |
---|
| 915 | // therefore the number of ticks per sample is 24576000 / Fs |
---|
| 916 | // NOTE: this will be rounded!! |
---|
| 917 | float ticks_per_sample=24576000.0/m_framerate; |
---|
| 918 | |
---|
| 919 | // we are allowed to add some constant |
---|
| 920 | // processing delay to the transfer delay |
---|
| 921 | // being the period size and some fixed delay |
---|
| 922 | unsigned int processing_delay=ticks_per_sample*(m_period)+RECEIVE_PROCESSING_DELAY; |
---|
| 923 | |
---|
| 924 | |
---|
| 925 | // the number of events in the buffer is |
---|
| 926 | // m_framecounter |
---|
| 927 | |
---|
| 928 | // we have the timestamp of the last event block: |
---|
| 929 | // m_last_timestamp |
---|
| 930 | |
---|
| 931 | // the time at which the beginning of the buffer should be |
---|
| 932 | // presented to the audio side is: |
---|
| 933 | // m_last_timestamp - (m_framecounter-m_syt_interval)*ticks_per_sample |
---|
| 934 | |
---|
| 935 | // however we have to make sure that we can transfer at least one period |
---|
| 936 | // therefore we first check if this is ok |
---|
| 937 | |
---|
| 938 | if(m_framecounter > (int)m_period) { |
---|
| 939 | // we make this signed, because this can be < 0 |
---|
| 940 | unsigned int m_last_timestamp_ticks = CYCLE_COUNTER_TO_TICKS(m_last_timestamp); |
---|
| 941 | |
---|
| 942 | // add the processing delay |
---|
| 943 | int ideal_presentation_time = m_last_timestamp_ticks + processing_delay; |
---|
| 944 | unsigned int buffer_content_ticks=(int)((m_framecounter-m_syt_interval)*ticks_per_sample); |
---|
| 945 | |
---|
| 946 | // if the ideal_presentation_time is smaller than buffer_content_ticks, wraparound has occurred |
---|
| 947 | // for the cycle part of m_last_timestamp. Therefore add one second worth of ticks |
---|
| 948 | // to the cycle counter, as this is the wraparound point. |
---|
| 949 | if (ideal_presentation_time < buffer_content_ticks) ideal_presentation_time += 24576000; |
---|
| 950 | // we can now safely substract these, it will always be > 0 |
---|
| 951 | ideal_presentation_time -= buffer_content_ticks; |
---|
| 952 | |
---|
| 953 | // FIXME: if we are sure, make ideal_presentation_time an unsigned int |
---|
| 954 | // assert(ideal_presentation_time>=0); |
---|
| 955 | |
---|
| 956 | |
---|
| 957 | /* if(ideal_presentation_time) { |
---|
| 958 | debugOutput(DEBUG_LEVEL_VERBOSE, "Presentation time < 0 : %d\n", ideal_presentation_time); |
---|
| 959 | }*/ |
---|
| 960 | |
---|
| 961 | unsigned int current_time=m_handler->getCycleCounter() & 0x1FFFFFF; |
---|
| 962 | unsigned int current_time_ticks = CYCLE_COUNTER_TO_TICKS(current_time); |
---|
| 963 | |
---|
| 964 | // if the last signalled period lies in the future, we know we had wraparound of the clock |
---|
| 965 | // so add one second |
---|
| 966 | // if (current_time_ticks < m_previous_signal_ticks) current_time_ticks += 24576000; |
---|
| 967 | debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"Periods: %d\n",m_PeriodStat.m_count); |
---|
| 968 | debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"Timestamp : %10u ticks (%3u secs + %4u cycles + %04u ticks)\n", |
---|
| 969 | m_last_timestamp_ticks, |
---|
| 970 | CYCLE_COUNTER_GET_SECS(m_last_timestamp), |
---|
| 971 | CYCLE_COUNTER_GET_CYCLES(m_last_timestamp), |
---|
| 972 | CYCLE_COUNTER_GET_TICKS(m_last_timestamp) |
---|
| 973 | ); |
---|
| 974 | debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"P-TIME : %10d ticks (%3u secs + %4u cycles + %04u ticks)\n", |
---|
| 975 | ideal_presentation_time, |
---|
| 976 | ideal_presentation_time/24576000, |
---|
| 977 | (ideal_presentation_time/3072) % 8000, |
---|
| 978 | ideal_presentation_time%3072 |
---|
| 979 | ); |
---|
| 980 | debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"Now : %10u ticks (%3u secs + %4u cycles + %04u ticks)\n", |
---|
| 981 | current_time_ticks, |
---|
| 982 | CYCLE_COUNTER_GET_SECS(current_time), |
---|
| 983 | CYCLE_COUNTER_GET_CYCLES(current_time), |
---|
| 984 | CYCLE_COUNTER_GET_TICKS(current_time) |
---|
| 985 | ); |
---|
| 986 | |
---|
| 987 | int tmp=ideal_presentation_time-current_time_ticks; |
---|
| 988 | |
---|
| 989 | // if current_time_ticks wraps around while ahead of the presentation time, we have |
---|
| 990 | // a problem. |
---|
| 991 | // we know however that we have to wait for at max one buffer + some transmit delay |
---|
| 992 | // therefore we clip this value at 0.5 seconds |
---|
| 993 | if (tmp > 24576000/2) tmp-=24576000; |
---|
| 994 | |
---|
| 995 | if(tmp<0) { |
---|
| 996 | debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"SYT passed (%d ticks too late)\n",-tmp); |
---|
| 997 | if (-tmp>1000000) debugWarning("SYT VERY LATE: %d!\n",-tmp); |
---|
| 998 | return true; |
---|
| 999 | } else { |
---|
| 1000 | debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"Too early wait %d ticks\n",tmp); |
---|
| 1001 | return false; |
---|
| 1002 | } |
---|
| 1003 | } else return false; |
---|
| 1004 | #endif |
---|
| 1005 | if(m_framecounter > (int)m_period) { |
---|
| 1006 | return true; |
---|
| 1007 | } |
---|
| 1008 | |
---|