root/trunk/libffado/src/libstreaming/AmdtpStreamProcessor.cpp

Revision 554, 50.3 kB (checked in by ppalmers, 15 years ago)

Merge echoaudio branch into trunk.

This adds support for the Echo Audiofire devices to FFADO. Possibly also other devices working with the Apple Class Driver will work with this code. It is not fully complete yet, but the main rework is
done.

First of all the IAvDevice class/interface is renamed to FFADODevice, in order to separate the AV/C code from the FFADO API code. A device supported by FFADO implements a FFADODevice.

The BeBoB device has been split up into three groups:
- libavc/* : all code and commands that are specified by AV/C specs. Note that a lot of the code that used to be in BeBoB::AvDevice? now resides in AVC::Unit
- genericavc/* : a FFADODevice that uses AV/C descriptors & commands for discovery and config
- bebob/* : the bebob FFADODevice that inherits from GenericAVC::AvDevice? but that uses BridgeCo? commands for discovery

Everything has been moved as high as possible in the class hierarchy. If necessary, a subclass that uses device specific commands is introduced (e.g. BeBoB::Plug inherits from AVC::Plug and uses the
BridgeCo? extended plug info command to discover it's properties).

There are some other fixes along the way that have been done too.

Line 
1 /*
2  * Copyright (C) 2005-2007 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License version 2.1, as published by the Free Software Foundation;
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21  * MA 02110-1301 USA
22  */
23
24 #include "AmdtpStreamProcessor.h"
25 #include "Port.h"
26 #include "AmdtpPort.h"
27
28 #include "cycletimer.h"
29
30 #include <netinet/in.h>
31 #include <assert.h>
32
33 // in ticks
34 #define TRANSMIT_TRANSFER_DELAY 9000U
35 // the number of cycles to send a packet in advance of it's timestamp
36 #define TRANSMIT_ADVANCE_CYCLES 4U
37
38 namespace Streaming {
39
40 IMPL_DEBUG_MODULE( AmdtpTransmitStreamProcessor, AmdtpTransmitStreamProcessor, DEBUG_LEVEL_NORMAL );
41 IMPL_DEBUG_MODULE( AmdtpReceiveStreamProcessor, AmdtpReceiveStreamProcessor, DEBUG_LEVEL_NORMAL );
42
43
44 /* transmit */
45 AmdtpTransmitStreamProcessor::AmdtpTransmitStreamProcessor(int port, int framerate, int dimension)
46         : TransmitStreamProcessor(port, framerate), m_dimension(dimension)
47         , m_last_timestamp(0), m_dbc(0), m_ringbuffer_size_frames(0)
48 {
49
50 }
51
52 AmdtpTransmitStreamProcessor::~AmdtpTransmitStreamProcessor() {
53
54 }
55
56 /**
57  * @return
58  */
59 bool AmdtpTransmitStreamProcessor::init() {
60
61     debugOutput( DEBUG_LEVEL_VERBOSE, "Initializing (%p)...\n");
62     // call the parent init
63     // this has to be done before allocating the buffers,
64     // because this sets the buffersizes from the processormanager
65     if(!TransmitStreamProcessor::init()) {
66         debugFatal("Could not do base class init (%p)\n",this);
67         return false;
68     }
69
70     return true;
71 }
72
73 void AmdtpTransmitStreamProcessor::setVerboseLevel(int l) {
74     setDebugLevel(l);
75     TransmitStreamProcessor::setVerboseLevel(l);
76 }
77
78 enum raw1394_iso_disposition
79 AmdtpTransmitStreamProcessor::getPacket(unsigned char *data, unsigned int *length,
80                   unsigned char *tag, unsigned char *sy,
81                   int cycle, unsigned int dropped, unsigned int max_length) {
82    
83     struct iec61883_packet *packet = (struct iec61883_packet *) data;
84    
85     if (cycle<0) {
86         debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"Xmit handler for cycle %d, (running=%d, enabled=%d,%d)\n",
87             cycle, m_running, m_disabled, m_is_disabled);
88        
89         *tag = 0;
90         *sy = 0;
91         *length=0;
92         return RAW1394_ISO_OK;
93    
94     }
95    
96     debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"Xmit handler for cycle %d, (running=%d, enabled=%d,%d)\n",
97         cycle, m_running, m_disabled, m_is_disabled);
98    
99     m_last_cycle=cycle;
100
101 #ifdef DEBUG
102     if(dropped>0) {
103         debugWarning("Dropped %d packets on cycle %d\n",dropped, cycle);
104     }
105 #endif
106
107     // calculate & preset common values
108
109     /* Our node ID can change after a bus reset, so it is best to fetch
110      * our node ID for each packet. */
111     packet->sid = getNodeId() & 0x3f;
112
113     packet->dbs = m_dimension;
114     packet->fn = 0;
115     packet->qpc = 0;
116     packet->sph = 0;
117     packet->reserved = 0;
118     packet->dbc = m_dbc;
119     packet->eoh1 = 2;
120     packet->fmt = IEC61883_FMT_AMDTP;
121
122     *tag = IEC61883_TAG_WITH_CIP;
123     *sy = 0;
124
125     // determine if we want to send a packet or not
126     // note that we can't use getCycleTimer directly here,
127     // because packets are queued in advance. This means that
128     // we the packet we are constructing will be sent out
129     // on 'cycle', not 'now'.
130     unsigned int ctr=m_handler->getCycleTimer();
131     int now_cycles = (int)CYCLE_TIMER_GET_CYCLES(ctr);
132
133     // the difference between the cycle this
134     // packet is intended for and 'now'
135     int cycle_diff = diffCycles(cycle, now_cycles);
136
137 #ifdef DEBUG
138     if(m_running && (cycle_diff < 0)) {
139         debugWarning("Requesting packet for cycle %04d which is in the past (now=%04dcy)\n",
140             cycle, now_cycles);
141     }
142 #endif
143
144     // as long as the cycle parameter is not in sync with
145     // the current time, the stream is considered not
146     // to be 'running'
147     // NOTE: this works only at startup
148     if (!m_running && cycle_diff >= 0 && cycle >= 0) {
149             debugOutput(DEBUG_LEVEL_VERBOSE, "Xmit StreamProcessor %p started running at cycle %d\n",this, cycle);
150             m_running=true;
151     }
152
153     uint64_t ts_head;
154     signed int fc;
155     if (!m_disabled && m_is_disabled) { // this means that we are trying to enable
156         // check if we are on or past the enable point
157         int cycles_past_enable=diffCycles(cycle, m_cycle_to_enable_at);
158
159         if (cycles_past_enable >= 0) {
160             m_is_disabled=false;
161
162             debugOutput(DEBUG_LEVEL_VERBOSE,"Enabling StreamProcessor %p at %u\n", this, cycle);
163
164             // initialize the buffer head & tail
165             ffado_timestamp_t ts_head_tmp;
166             m_SyncSource->m_data_buffer->getBufferHeadTimestamp(&ts_head_tmp, &fc); // thread safe
167             ts_head=(uint64_t)ts_head_tmp;
168            
169             // the number of cycles the sync source lags (> 0)
170             // or leads (< 0)
171             int sync_lag_cycles=diffCycles(cycle, m_SyncSource->getLastCycle());
172
173             // account for the cycle lag between sync SP and this SP
174             // the last update of the sync source's timestamps was sync_lag_cycles
175             // cycles before the cycle we are calculating the timestamp for.
176             // if we were to use one-frame buffers, you would expect the
177             // frame that is sent on cycle CT to have a timestamp T1.
178             // ts_head however is for cycle CT-sync_lag_cycles, and lies
179             // therefore sync_lag_cycles * TICKS_PER_CYCLE earlier than
180             // T1.
181             ts_head = addTicks(ts_head, (sync_lag_cycles) * TICKS_PER_CYCLE);
182
183             ts_head = substractTicks(ts_head, TICKS_PER_CYCLE);
184
185             // account for the number of cycles we are too late to enable
186             ts_head = addTicks(ts_head, cycles_past_enable * TICKS_PER_CYCLE);
187
188             // account for one extra packet of frames
189             ts_head = substractTicks(ts_head,
190                         (uint32_t)((float)m_syt_interval * m_SyncSource->m_data_buffer->getRate()));
191
192             m_data_buffer->setBufferHeadTimestamp(ts_head);
193
194             #ifdef DEBUG
195             if ((unsigned int)m_data_buffer->getFrameCounter() != m_data_buffer->getBufferSize()) {
196                 debugWarning("m_data_buffer->getFrameCounter() != m_data_buffer->getBufferSize()\n");
197             }
198             #endif
199             debugOutput(DEBUG_LEVEL_VERBOSE,"XMIT TS SET: TS=%10llu, LAG=%03d, FC=%4d\n",
200                             ts_head, sync_lag_cycles, m_data_buffer->getFrameCounter());
201         } else {
202             debugOutput(DEBUG_LEVEL_VERY_VERBOSE,
203                         "will enable StreamProcessor %p at %u, now is %d\n",
204                         this, m_cycle_to_enable_at, cycle);
205         }
206     } else if (m_disabled && !m_is_disabled) {
207         // trying to disable
208         debugOutput(DEBUG_LEVEL_VERBOSE,"disabling StreamProcessor %p at %u\n",
209                     this, cycle);
210         m_is_disabled=true;
211     }
212
213     // the base timestamp is the one of the next sample in the buffer
214     ffado_timestamp_t ts_head_tmp;
215     m_data_buffer->getBufferHeadTimestamp(&ts_head_tmp, &fc); // thread safe
216     ts_head=(uint64_t)ts_head_tmp;
217
218     // we send a packet some cycles in advance, to avoid the
219     // following situation:
220     // suppose we are only a few ticks away from
221     // the moment to send this packet. therefore we decide
222     // not to send the packet, but send it in the next cycle.
223     // This means that the next time point will be 3072 ticks
224     // later, making that the timestamp will be expired when the
225     // packet is sent, unless TRANSFER_DELAY > 3072.
226     // this means that we need at least one cycle of extra buffering.
227     uint32_t ticks_to_advance = TICKS_PER_CYCLE * TRANSMIT_ADVANCE_CYCLES;
228
229     // if cycle lies cycle_diff cycles in the future, we should
230     // queue this packet cycle_diff * TICKS_PER_CYCLE earlier than
231     // we would if it were to be sent immediately.
232     ticks_to_advance += cycle_diff * TICKS_PER_CYCLE;
233
234     // determine the 'now' time in ticks
235     uint32_t cycle_timer=CYCLE_TIMER_TO_TICKS(ctr);
236
237     cycle_timer = addTicks(cycle_timer, ticks_to_advance);
238
239     // time until the packet is to be sent (if > 0: send packet)
240     int32_t until_next=diffTicks(ts_head, cycle_timer);
241
242     // if until_next < 0 we should send a filled packet
243     // otherwise we should send a NO-DATA packet
244     if((until_next<0) && (m_running)) {
245         // add the transmit transfer delay to construct the playout time (=SYT timestamp)
246         uint32_t ts_packet=addTicks(ts_head, TRANSMIT_TRANSFER_DELAY);
247
248         // if we are disabled, send a silent packet
249         // and advance the buffer head timestamp
250         if(m_is_disabled) {
251
252 //             transmitSilenceBlock((char *)(data+8), m_syt_interval, 0);
253 //             m_dbc += fillDataPacketHeader(packet, length, ts_packet);
254 //
255 //             debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "XMIT SYNC: CY=%04u TSH=%011llu TSP=%011lu\n",
256 //                 cycle, ts_head, ts_packet);
257 //
258 //             // update the base timestamp
259 //             uint32_t ts_step=(uint32_t)((float)(m_syt_interval)
260 //                              *m_SyncSource->m_data_buffer->getRate());
261 //
262 //             // the next buffer head timestamp
263 //             ts_head=addTicks(ts_head,ts_step);
264 //             m_data_buffer->setBufferHeadTimestamp(ts_head);
265 //
266             // no-data
267             debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "XMIT SYNC: CY=%04u NONE\n", cycle);
268             m_dbc += fillNoDataPacketHeader(packet, length);
269             // defer to make sure we get to be enabled asap
270             return RAW1394_ISO_DEFER;
271
272         } else { // enabled & packet due, read from the buffer
273             if (m_data_buffer->readFrames(m_syt_interval, (char *)(data + 8))) {
274                 m_dbc += fillDataPacketHeader(packet, length, ts_packet);
275
276                 // process all ports that should be handled on a per-packet base
277                 // this is MIDI for AMDTP (due to the need of DBC)
278                 if (!encodePacketPorts((quadlet_t *)(data+8), m_syt_interval, packet->dbc)) {
279                     debugWarning("Problem encoding Packet Ports\n");
280                 }
281
282                 debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "XMIT DATA: CY=%04u TSH=%011llu TSP=%011lu\n",
283                     cycle, ts_head, ts_packet);
284
285                 return RAW1394_ISO_OK;
286
287             } else if (now_cycles<cycle) {
288                 // we can still postpone the queueing of the packets
289                 // because the ISO transmit packet buffer is not empty yet
290                 return RAW1394_ISO_AGAIN;
291
292             } else { // there is no more data in the ringbuffer
293                 // compose a silent packet, we should always
294                 // send a valid packet
295                 transmitSilenceBlock((char *)(data+8), m_syt_interval, 0);
296                 m_dbc += fillDataPacketHeader(packet, length, ts_packet);
297
298                 debugWarning("Transmit buffer underrun (now %d, queue %d, target %d)\n",
299                         now_cycles, cycle, TICKS_TO_CYCLES(ts_packet));
300                 // signal underrun
301                 m_xruns++;
302                 // disable the processing, will be re-enabled when
303                 // the xrun is handled
304                 m_disabled=true;
305                 m_is_disabled=true;
306
307                 return RAW1394_ISO_DEFER;
308             }
309         }
310
311     } else { // no packet due, send no-data packet
312         debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "XMIT NONE: CY=%04u TSH=%011llu\n",
313                 cycle, ts_head);
314
315         m_dbc += fillNoDataPacketHeader(packet, length);
316         return RAW1394_ISO_DEFER;
317     }
318
319     // we shouldn't get here
320     return RAW1394_ISO_ERROR;
321
322 }
323
324 unsigned int AmdtpTransmitStreamProcessor::fillDataPacketHeader(
325         struct iec61883_packet *packet, unsigned int* length,
326         uint32_t ts) {
327
328     packet->fdf = m_fdf;
329
330     // convert the timestamp to SYT format
331     uint16_t timestamp_SYT = TICKS_TO_SYT(ts);
332     packet->syt = ntohs(timestamp_SYT);
333
334     *length = m_syt_interval*sizeof(quadlet_t)*m_dimension + 8;
335
336     return m_syt_interval;
337 }
338
339 unsigned int AmdtpTransmitStreamProcessor::fillNoDataPacketHeader(
340         struct iec61883_packet *packet, unsigned int* length) {
341
342     // no-data packets have syt=0xFFFF
343     // and have the usual amount of events as dummy data (?)
344     packet->fdf = IEC61883_FDF_NODATA;
345     packet->syt = 0xffff;
346
347     // FIXME: either make this a setting or choose
348     bool send_payload=true;
349     if(send_payload) {
350         // this means no-data packets with payload (DICE doesn't like that)
351         *length = 2*sizeof(quadlet_t) + m_syt_interval * m_dimension * sizeof(quadlet_t);
352         return m_syt_interval;
353     } else {
354         // dbc is not incremented
355         // this means no-data packets without payload
356         *length = 2*sizeof(quadlet_t);
357         return 0;
358     }
359 }
360
361 int AmdtpTransmitStreamProcessor::getMinimalSyncDelay() {
362     return 0;
363 }
364
365 bool AmdtpTransmitStreamProcessor::prefill() {
366
367     debugOutput( DEBUG_LEVEL_VERBOSE, "Prefill transmit buffers...\n");
368
369     if(!transferSilence(m_ringbuffer_size_frames)) {
370         debugFatal("Could not prefill transmit stream\n");
371         return false;
372     }
373
374     return true;
375 }
376
377 bool AmdtpTransmitStreamProcessor::reset() {
378
379     debugOutput( DEBUG_LEVEL_VERBOSE, "Resetting...\n");
380
381     // reset the statistics
382     m_PeriodStat.reset();
383     m_PacketStat.reset();
384     m_WakeupStat.reset();
385
386     // we have to make sure that the buffer HEAD timestamp
387     // lies in the future for every possible buffer fill case.
388     int offset=(int)(m_ringbuffer_size_frames*getTicksPerFrame());
389
390     m_data_buffer->setTickOffset(offset);
391
392     // reset all non-device specific stuff
393     // i.e. the iso stream and the associated ports
394     if(!TransmitStreamProcessor::reset()) {
395         debugFatal("Could not do base class reset\n");
396         return false;
397     }
398
399     // we should prefill the event buffer
400     if (!prefill()) {
401         debugFatal("Could not prefill buffers\n");
402         return false;
403     }
404
405     return true;
406 }
407
408 bool AmdtpTransmitStreamProcessor::prepare() {
409     m_PeriodStat.setName("XMT PERIOD");
410     m_PacketStat.setName("XMT PACKET");
411     m_WakeupStat.setName("XMT WAKEUP");
412
413     debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing (%p)...\n", this);
414
415     // prepare all non-device specific stuff
416     // i.e. the iso stream and the associated ports
417     if(!TransmitStreamProcessor::prepare()) {
418         debugFatal("Could not prepare base class\n");
419         return false;
420     }
421
422     switch (m_framerate) {
423     case 32000:
424         m_syt_interval = 8;
425         m_fdf = IEC61883_FDF_SFC_32KHZ;
426         break;
427     case 44100:
428         m_syt_interval = 8;
429         m_fdf = IEC61883_FDF_SFC_44K1HZ;
430         break;
431     default:
432     case 48000:
433         m_syt_interval = 8;
434         m_fdf = IEC61883_FDF_SFC_48KHZ;
435         break;
436     case 88200:
437         m_syt_interval = 16;
438         m_fdf = IEC61883_FDF_SFC_88K2HZ;
439         break;
440     case 96000:
441         m_syt_interval = 16;
442         m_fdf = IEC61883_FDF_SFC_96KHZ;
443         break;
444     case 176400:
445         m_syt_interval = 32;
446         m_fdf = IEC61883_FDF_SFC_176K4HZ;
447         break;
448     case 192000:
449         m_syt_interval = 32;
450         m_fdf = IEC61883_FDF_SFC_192KHZ;
451         break;
452     }
453
454     iec61883_cip_init (
455         &m_cip_status,
456         IEC61883_FMT_AMDTP,
457         m_fdf,
458         m_framerate,
459         m_dimension,
460         m_syt_interval);
461
462     // prepare the framerate estimate
463     float ticks_per_frame = (TICKS_PER_SECOND*1.0) / ((float)m_framerate);
464     m_ticks_per_frame=ticks_per_frame;
465
466     // initialize internal buffer
467     m_ringbuffer_size_frames=m_nb_buffers * m_period;
468
469     assert(m_data_buffer);
470     m_data_buffer->setBufferSize(m_ringbuffer_size_frames);
471     m_data_buffer->setEventSize(sizeof(quadlet_t));
472     m_data_buffer->setEventsPerFrame(m_dimension);
473
474     m_data_buffer->setUpdatePeriod(m_period);
475     m_data_buffer->setNominalRate(ticks_per_frame);
476
477     m_data_buffer->setWrapValue(128L*TICKS_PER_SECOND);
478
479     m_data_buffer->prepare();
480
481     // set the parameters of ports we can:
482     // we want the audio ports to be period buffered,
483     // and the midi ports to be packet buffered
484     for ( PortVectorIterator it = m_Ports.begin();
485           it != m_Ports.end();
486           ++it )
487     {
488         debugOutput(DEBUG_LEVEL_VERBOSE, "Setting up port %s\n",(*it)->getName().c_str());
489         if(!(*it)->setBufferSize(m_period)) {
490             debugFatal("Could not set buffer size to %d\n",m_period);
491             return false;
492         }
493
494
495         switch ((*it)->getPortType()) {
496             case Port::E_Audio:
497                 if(!(*it)->setSignalType(Port::E_PeriodSignalled)) {
498                     debugFatal("Could not set signal type to PeriodSignalling");
499                     return false;
500                 }
501                 debugWarning("---------------- ! Doing hardcoded test setup ! --------------\n");
502                 // buffertype and datatype are dependant on the API
503                 if(!(*it)->setBufferType(Port::E_PointerBuffer)) {
504                     debugFatal("Could not set buffer type");
505                     return false;
506                 }
507                 if(!(*it)->useExternalBuffer(true)) {
508                     debugFatal("Could not set external buffer usage");
509                     return false;
510                 }
511
512                 if(!(*it)->setDataType(Port::E_Float)) {
513                     debugFatal("Could not set data type");
514                     return false;
515                 }
516
517
518                 break;
519             case Port::E_Midi:
520                 if(!(*it)->setSignalType(Port::E_PacketSignalled)) {
521                     debugFatal("Could not set signal type to PeriodSignalling");
522                     return false;
523                 }
524
525                 // we use a timing unit of 10ns
526                 // this makes sure that for the max syt interval
527                 // we don't have rounding, and keeps the numbers low
528                 // we have 1 slot every 8 events
529                 // we have syt_interval events per packet
530                 // => syt_interval/8 slots per packet
531                 // packet rate is 8000pkt/sec => interval=125us
532                 // so the slot interval is (1/8000)/(syt_interval/8)
533                 // or: 1/(1000 * syt_interval) sec
534                 // which is 1e9/(1000*syt_interval) nsec
535                 // or 100000/syt_interval 'units'
536                 // the event interval is fixed to 320us = 32000 'units'
537                 if(!(*it)->useRateControl(true,(100000/m_syt_interval),32000, false)) {
538                     debugFatal("Could not set signal type to PeriodSignalling");
539                     return false;
540                 }
541
542                 // buffertype and datatype are dependant on the API
543                 debugWarning("---------------- ! Doing hardcoded test setup ! --------------\n");
544                 // buffertype and datatype are dependant on the API
545                 if(!(*it)->setBufferType(Port::E_RingBuffer)) {
546                     debugFatal("Could not set buffer type");
547                     return false;
548                 }
549                 if(!(*it)->setDataType(Port::E_MidiEvent)) {
550                     debugFatal("Could not set data type");
551                     return false;
552                 }
553                 break;
554             default:
555                 debugWarning("Unsupported port type specified\n");
556                 break;
557         }
558     }
559
560     // the API specific settings of the ports should already be set,
561     // as this is called from the processorManager->prepare()
562     // so we can init the ports
563     if(!initPorts()) {
564         debugFatal("Could not initialize ports!\n");
565         return false;
566     }
567
568     if(!preparePorts()) {
569         debugFatal("Could not initialize ports!\n");
570         return false;
571     }
572
573     debugOutput( DEBUG_LEVEL_VERBOSE, "Prepared for:\n");
574     debugOutput( DEBUG_LEVEL_VERBOSE, " Samplerate: %d, FDF: %d, DBS: %d, SYT: %d\n",
575              m_framerate,m_fdf,m_dimension,m_syt_interval);
576     debugOutput( DEBUG_LEVEL_VERBOSE, " PeriodSize: %d, NbBuffers: %d\n",
577              m_period,m_nb_buffers);
578     debugOutput( DEBUG_LEVEL_VERBOSE, " Port: %d, Channel: %d\n",
579              m_port,m_channel);
580
581     return true;
582
583 }
584
585 bool AmdtpTransmitStreamProcessor::prepareForStart() {
586
587     return true;
588 }
589
590 bool AmdtpTransmitStreamProcessor::prepareForStop() {
591     disable();
592     return true;
593 }
594
595 bool AmdtpTransmitStreamProcessor::prepareForEnable(uint64_t time_to_enable_at) {
596
597     debugOutput(DEBUG_LEVEL_VERBOSE,"Preparing to enable...\n");
598
599     // for the transmit SP, we have to initialize the
600     // buffer timestamp to something sane, because this timestamp
601     // is used when it is SyncSource
602
603     // the time we initialize to will determine the time at which
604     // the first sample in the buffer will be sent, so we should
605     // make it at least 'time_to_enable_at'
606
607     uint64_t now=m_handler->getCycleTimer();
608     unsigned int now_secs=CYCLE_TIMER_GET_SECS(now);
609
610     // check if a wraparound on the secs will happen between
611     // now and the time we start
612     int until_enable=(int)time_to_enable_at - (int)CYCLE_TIMER_GET_CYCLES(now);
613
614     if(until_enable>4000) {
615         // wraparound on CYCLE_TIMER_GET_CYCLES(now)
616         // this means that we are late starting up,
617         // and that the start lies in the previous second
618         if (now_secs==0) now_secs=127;
619         else now_secs--;
620     } else if (until_enable<-4000) {
621         // wraparound on time_to_enable_at
622         // this means that we are early and that the start
623         // point lies in the next second
624         now_secs++;
625         if (now_secs>=128) now_secs=0;
626     }
627
628     uint64_t ts_head= now_secs*TICKS_PER_SECOND;
629     ts_head+=time_to_enable_at*TICKS_PER_CYCLE;
630
631     // we also add the nb of cycles we transmit in advance
632     ts_head=addTicks(ts_head, TRANSMIT_ADVANCE_CYCLES*TICKS_PER_CYCLE);
633
634     m_data_buffer->setBufferTailTimestamp(ts_head);
635
636
637     if (!StreamProcessor::prepareForEnable(time_to_enable_at)) {
638         debugError("StreamProcessor::prepareForEnable failed\n");
639         return false;
640     }
641
642     return true;
643 }
644
645 bool AmdtpTransmitStreamProcessor::transferSilence(unsigned int nframes) {
646     bool retval;
647
648     char *dummybuffer=(char *)calloc(sizeof(quadlet_t),nframes*m_dimension);
649
650     transmitSilenceBlock(dummybuffer, nframes, 0);
651
652     // add the silence data to the ringbuffer
653     if(m_data_buffer->writeFrames(nframes, dummybuffer, 0)) {
654         retval=true;
655     } else {
656         debugWarning("Could not write to event buffer\n");
657         retval=false;
658     }
659
660     free(dummybuffer);
661
662     return retval;
663 }
664
665 bool AmdtpTransmitStreamProcessor::putFrames(unsigned int nbframes, int64_t ts) {
666     m_PeriodStat.mark(m_data_buffer->getBufferFill());
667
668     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "AmdtpTransmitStreamProcessor::putFrames(%d, %llu)\n", nbframes, ts);
669
670     // transfer the data
671     m_data_buffer->blockProcessWriteFrames(nbframes, ts);
672
673     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, " New timestamp: %llu\n", ts);
674
675     return true;
676 }
677 /*
678  * write received events to the stream ringbuffers.
679  */
680
681 bool AmdtpTransmitStreamProcessor::processWriteBlock(char *data,
682                        unsigned int nevents, unsigned int offset)
683 {
684     bool no_problem=true;
685
686     for ( PortVectorIterator it = m_PeriodPorts.begin();
687           it != m_PeriodPorts.end();
688           ++it )
689     {
690
691         if((*it)->isDisabled()) {continue;};
692
693         //FIXME: make this into a static_cast when not DEBUG?
694
695         AmdtpPortInfo *pinfo=dynamic_cast<AmdtpPortInfo *>(*it);
696         assert(pinfo); // this should not fail!!
697
698         switch(pinfo->getFormat()) {
699         case AmdtpPortInfo::E_MBLA:
700             if(encodePortToMBLAEvents(static_cast<AmdtpAudioPort *>(*it), (quadlet_t *)data, offset, nevents)) {
701                 debugWarning("Could not encode port %s to MBLA events",(*it)->getName().c_str());
702                 no_problem=false;
703             }
704             break;
705         case AmdtpPortInfo::E_SPDIF: // still unimplemented
706             break;
707         default: // ignore
708             break;
709         }
710     }
711     return no_problem;
712
713 }
714
715 int AmdtpTransmitStreamProcessor::transmitSilenceBlock(char *data,
716                        unsigned int nevents, unsigned int offset)
717 {
718     int problem=0;
719
720     for ( PortVectorIterator it = m_PeriodPorts.begin();
721           it != m_PeriodPorts.end();
722           ++it )
723     {
724
725         //FIXME: make this into a static_cast when not DEBUG?
726
727         AmdtpPortInfo *pinfo=dynamic_cast<AmdtpPortInfo *>(*it);
728         assert(pinfo); // this should not fail!!
729
730         switch(pinfo->getFormat()) {
731         case AmdtpPortInfo::E_MBLA:
732             if(encodeSilencePortToMBLAEvents(static_cast<AmdtpAudioPort *>(*it), (quadlet_t *)data, offset, nevents)) {
733                 debugWarning("Could not encode port %s to MBLA events",(*it)->getName().c_str());
734                 problem=1;
735             }
736             break;
737         case AmdtpPortInfo::E_SPDIF: // still unimplemented
738             break;
739         default: // ignore
740             break;
741         }
742     }
743     return problem;
744
745 }
746
747 /**
748  * @brief decode a packet for the packet-based ports
749  *
750  * @param data Packet data
751  * @param nevents number of events in data (including events of other ports & port types)
752  * @param dbc DataBlockCount value for this packet
753  * @return true if all successfull
754  */
755 bool AmdtpTransmitStreamProcessor::encodePacketPorts(quadlet_t *data, unsigned int nevents, unsigned int dbc)
756 {
757     bool ok=true;
758     quadlet_t byte;
759
760     quadlet_t *target_event=NULL;
761     unsigned int j;
762
763     for ( PortVectorIterator it = m_PacketPorts.begin();
764           it != m_PacketPorts.end();
765           ++it )
766     {
767
768 #ifdef DEBUG
769         AmdtpPortInfo *pinfo=dynamic_cast<AmdtpPortInfo *>(*it);
770         assert(pinfo); // this should not fail!!
771
772         // the only packet type of events for AMDTP is MIDI in mbla
773         assert(pinfo->getFormat()==AmdtpPortInfo::E_Midi);
774 #endif
775
776         AmdtpMidiPort *mp=static_cast<AmdtpMidiPort *>(*it);
777
778         // we encode this directly (no function call) due to the high frequency
779         /* idea:
780         spec says: current_midi_port=(dbc+j)%8;
781         => if we start at (dbc+stream->location-1)%8,
782         we'll start at the right event for the midi port.
783         => if we increment j with 8, we stay at the right event.
784         */
785         // FIXME: as we know in advance how big a packet is (syt_interval) we can
786         //        predict how much loops will be present here
787         // first prefill the buffer with NO_DATA's on all time muxed channels
788
789         for(j = (dbc & 0x07)+mp->getLocation(); j < nevents; j += 8) {
790            
791             quadlet_t tmpval;
792            
793             target_event=(quadlet_t *)(data + ((j * m_dimension) + mp->getPosition()));
794            
795             if(mp->canRead()) { // we can send a byte
796                 mp->readEvent(&byte);
797                 byte &= 0xFF;
798                 tmpval=htonl(
799                     IEC61883_AM824_SET_LABEL((byte)<<16,
800                                              IEC61883_AM824_LABEL_MIDI_1X));
801
802                 debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "MIDI port %s, pos=%d, loc=%d, dbc=%d, nevents=%d, dim=%d\n",
803                     mp->getName().c_str(), mp->getPosition(), mp->getLocation(), dbc, nevents, m_dimension);
804                 debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "base=%p, target=%p, value=%08X\n",
805                     data, target_event, tmpval);
806                    
807             } else {
808                 // can't send a byte, either because there is no byte,
809                 // or because this would exceed the maximum rate
810                 tmpval=htonl(
811                     IEC61883_AM824_SET_LABEL(0,IEC61883_AM824_LABEL_MIDI_NO_DATA));
812             }
813            
814             *target_event=tmpval;
815         }
816
817     }
818
819     return ok;
820 }
821
822
823 int AmdtpTransmitStreamProcessor::encodePortToMBLAEvents(AmdtpAudioPort *p, quadlet_t *data,
824                        unsigned int offset, unsigned int nevents)
825 {
826     unsigned int j=0;
827
828     quadlet_t *target_event;
829
830     target_event=(quadlet_t *)(data + p->getPosition());
831
832     switch(p->getDataType()) {
833         default:
834         case Port::E_Int24:
835             {
836                 quadlet_t *buffer=(quadlet_t *)(p->getBufferAddress());
837
838                 assert(nevents + offset <= p->getBufferSize());
839
840                 buffer+=offset;
841
842                 for(j = 0; j < nevents; j += 1) { // decode max nsamples
843                     *target_event = htonl((*(buffer) & 0x00FFFFFF) | 0x40000000);
844                     buffer++;
845                     target_event += m_dimension;
846                 }
847             }
848             break;
849         case Port::E_Float:
850             {
851                 const float multiplier = (float)(0x7FFFFF00);
852                 float *buffer=(float *)(p->getBufferAddress());
853
854                 assert(nevents + offset <= p->getBufferSize());
855
856                 buffer+=offset;
857
858                 for(j = 0; j < nevents; j += 1) { // decode max nsamples
859
860                     // don't care for overflow
861                     float v = *buffer * multiplier;  // v: -231 .. 231
862                     unsigned int tmp = ((int)v);
863                     *target_event = htonl((tmp >> 8) | 0x40000000);
864
865                     buffer++;
866                     target_event += m_dimension;
867                 }
868             }
869             break;
870     }
871
872     return 0;
873 }
874 int AmdtpTransmitStreamProcessor::encodeSilencePortToMBLAEvents(AmdtpAudioPort *p, quadlet_t *data,
875                        unsigned int offset, unsigned int nevents)
876 {
877     unsigned int j=0;
878
879     quadlet_t *target_event;
880
881     target_event=(quadlet_t *)(data + p->getPosition());
882
883     switch(p->getDataType()) {
884         default:
885         case Port::E_Int24:
886         case Port::E_Float:
887             {
888                 for(j = 0; j < nevents; j += 1) { // decode max nsamples
889                     *target_event = htonl(0x40000000);
890                     target_event += m_dimension;
891                 }
892             }
893             break;
894     }
895
896     return 0;
897 }
898
899 /* --------------------- RECEIVE ----------------------- */
900
901 AmdtpReceiveStreamProcessor::AmdtpReceiveStreamProcessor(int port, int framerate, int dimension)
902     : ReceiveStreamProcessor(port, framerate), m_dimension(dimension), m_last_timestamp(0), m_last_timestamp2(0) {
903
904 }
905
906 AmdtpReceiveStreamProcessor::~AmdtpReceiveStreamProcessor() {
907
908 }
909
910 bool AmdtpReceiveStreamProcessor::init() {
911
912     // call the parent init
913     // this has to be done before allocating the buffers,
914     // because this sets the buffersizes from the processormanager
915     if(!ReceiveStreamProcessor::init()) {
916         debugFatal("Could not do base class init (%d)\n",this);
917         return false;
918     }
919
920     return true;
921 }
922
923 enum raw1394_iso_disposition
924 AmdtpReceiveStreamProcessor::putPacket(unsigned char *data, unsigned int length,
925                   unsigned char channel, unsigned char tag, unsigned char sy,
926                   unsigned int cycle, unsigned int dropped) {
927
928     enum raw1394_iso_disposition retval=RAW1394_ISO_OK;
929     m_last_cycle=cycle;
930
931     struct iec61883_packet *packet = (struct iec61883_packet *) data;
932     assert(packet);
933
934 #ifdef DEBUG
935     if(dropped>0) {
936         debugWarning("Dropped %d packets on cycle %d\n",dropped, cycle);
937     }
938
939     debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"ch%2u: CY=%4u, SYT=%08X (%4ucy + %04uticks) (running=%d, disabled=%d,%d)\n",
940         channel, cycle,ntohs(packet->syt),
941         CYCLE_TIMER_GET_CYCLES(ntohs(packet->syt)), CYCLE_TIMER_GET_OFFSET(ntohs(packet->syt)),
942         m_running,m_disabled,m_is_disabled);
943
944     debugOutput(DEBUG_LEVEL_VERY_VERBOSE,
945         "RCV: CH = %d, FDF = %X. SYT = %6d, DBS = %3d, DBC = %3d, FMT = %3d, LEN = %4d\n",
946         channel, packet->fdf,
947         packet->syt,
948         packet->dbs,
949         packet->dbc,
950         packet->fmt,
951         length);
952
953 #endif
954     if (!m_disabled && m_is_disabled) { // this means that we are trying to enable
955         // check if we are on or past the enable point
956         int cycles_past_enable=diffCycles(cycle, m_cycle_to_enable_at);
957
958         if (cycles_past_enable >= 0) {
959             m_is_disabled=false;
960             debugOutput(DEBUG_LEVEL_VERBOSE,"Enabling StreamProcessor %p at %d (SYT=%04X)\n",
961                 this, cycle, ntohs(packet->syt));
962             // the previous timestamp is the one we need to start with
963             // because we're going to update the buffer again this loop
964             // using writeframes
965             m_data_buffer->setBufferTailTimestamp(m_last_timestamp);
966
967         } else {
968             debugOutput(DEBUG_LEVEL_VERY_VERBOSE,
969                 "will enable StreamProcessor %p at %u, now is %d\n",
970                     this, m_cycle_to_enable_at, cycle);
971         }
972     } else if (m_disabled && !m_is_disabled) {
973         // trying to disable
974         debugOutput(DEBUG_LEVEL_VERBOSE,"disabling StreamProcessor %p at %u\n", this, cycle);
975         m_is_disabled=true;
976     }
977
978     // check if this is a valid packet
979     if((packet->syt != 0xFFFF)
980        && (packet->fdf != 0xFF)
981        && (packet->fmt == 0x10)
982        && (packet->dbs>0)
983        && (length>=2*sizeof(quadlet_t))) {
984
985         unsigned int nevents=((length / sizeof (quadlet_t)) - 2)/packet->dbs;
986
987         //=> store the previous timestamp
988         m_last_timestamp2=m_last_timestamp;
989
990         //=> convert the SYT to a full timestamp in ticks
991         m_last_timestamp=sytRecvToFullTicks((uint32_t)ntohs(packet->syt),
992                                         cycle, m_handler->getCycleTimer());
993
994         debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "RECV: CY=%04u TS=%011llu\n",
995                 cycle, m_last_timestamp);
996
997         // we have to keep in mind that there are also
998         // some packets buffered by the ISO layer,
999         // at most x=m_handler->getWakeupInterval()
1000         // these contain at most x*syt_interval
1001         // frames, meaning that we might receive
1002         // this packet x*syt_interval*ticks_per_frame
1003         // later than expected (the real receive time)
1004         debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"STMP: %lluticks | buff=%d, syt_interval=%d, tpf=%f\n",
1005             m_last_timestamp, m_handler->getWakeupInterval(),m_syt_interval,getTicksPerFrame());
1006
1007         //=> signal that we're running (if we are)
1008         if(!m_running && nevents && m_last_timestamp2 && m_last_timestamp) {
1009             debugOutput(DEBUG_LEVEL_VERBOSE,"Receive StreamProcessor %p started running at %d\n", this, cycle);
1010             m_running=true;
1011         }
1012
1013         //=> don't process the stream samples when it is not enabled.
1014         if(m_is_disabled) {
1015
1016             // we keep track of the timestamp here
1017             // this makes sure that we will have a somewhat accurate
1018             // estimate as to when a period might be ready. i.e. it will not
1019             // be ready earlier than this timestamp + period time
1020
1021             // the next (possible) sample is not this one, but lies
1022             // SYT_INTERVAL * rate later
1023             uint64_t ts=addTicks(m_last_timestamp,
1024                                  (uint64_t)((float)m_syt_interval * getTicksPerFrame()));
1025
1026             // set the timestamp as if there will be a sample put into
1027             // the buffer by the next packet.
1028             m_data_buffer->setBufferTailTimestamp(ts);
1029
1030             return RAW1394_ISO_DEFER;
1031         }
1032
1033         #ifdef DEBUG_OFF
1034         if((cycle % 1000) == 0) {
1035             uint32_t syt = (uint32_t)ntohs(packet->syt);
1036             uint32_t now=m_handler->getCycleTimer();
1037             uint32_t now_ticks=CYCLE_TIMER_TO_TICKS(now);
1038
1039             uint32_t test_ts=sytRecvToFullTicks(syt, cycle, now);
1040
1041             debugOutput(DEBUG_LEVEL_VERBOSE, "R %04d: SYT=%08X,            CY=%02d OFF=%04d\n",
1042                 cycle, syt, CYCLE_TIMER_GET_CYCLES(syt), CYCLE_TIMER_GET_OFFSET(syt)
1043                 );
1044             debugOutput(DEBUG_LEVEL_VERBOSE, "R %04d: NOW=%011lu, SEC=%03u CY=%02u OFF=%04u\n",
1045                 cycle, now_ticks, CYCLE_TIMER_GET_SECS(now), CYCLE_TIMER_GET_CYCLES(now), CYCLE_TIMER_GET_OFFSET(now)
1046                 );
1047             debugOutput(DEBUG_LEVEL_VERBOSE, "R %04d: TSS=%011lu, SEC=%03u CY=%02u OFF=%04u\n",
1048                 cycle, test_ts, TICKS_TO_SECS(test_ts), TICKS_TO_CYCLES(test_ts), TICKS_TO_OFFSET(test_ts)
1049                 );
1050         }
1051         #endif
1052
1053         //=> process the packet
1054         // add the data payload to the ringbuffer
1055         if(m_data_buffer->writeFrames(nevents, (char *)(data+8), m_last_timestamp)) {
1056             retval=RAW1394_ISO_OK;
1057
1058             // process all ports that should be handled on a per-packet base
1059             // this is MIDI for AMDTP (due to the need of DBC)
1060             if (!decodePacketPorts((quadlet_t *)(data+8), nevents, packet->dbc)) {
1061                 debugWarning("Problem decoding Packet Ports\n");
1062                 retval=RAW1394_ISO_DEFER;
1063             }
1064
1065         } else {
1066
1067             debugWarning("Receive buffer overrun (cycle %d, FC=%d, PC=%d)\n",
1068                  cycle, m_data_buffer->getFrameCounter(), m_handler->getPacketCount());
1069
1070             m_xruns++;
1071
1072             // disable the processing, will be re-enabled when
1073             // the xrun is handled
1074             m_disabled=true;
1075             m_is_disabled=true;
1076
1077             retval=RAW1394_ISO_DEFER;
1078         }
1079     }
1080
1081     return retval;
1082 }
1083
1084 // returns the delay between the actual (real) time of a timestamp as received,
1085 // and the timestamp that is passed on for the same event. This is to cope with
1086 // ISO buffering
1087 int AmdtpReceiveStreamProcessor::getMinimalSyncDelay() {
1088     return ((int)(m_handler->getWakeupInterval() * m_syt_interval * getTicksPerFrame()));
1089 }
1090
1091 void AmdtpReceiveStreamProcessor::dumpInfo() {
1092     StreamProcessor::dumpInfo();
1093 }
1094
1095 void AmdtpReceiveStreamProcessor::setVerboseLevel(int l) {
1096     setDebugLevel(l);
1097     ReceiveStreamProcessor::setVerboseLevel(l);
1098 }
1099
1100 bool AmdtpReceiveStreamProcessor::reset() {
1101
1102     debugOutput( DEBUG_LEVEL_VERBOSE, "Resetting...\n");
1103
1104     m_PeriodStat.reset();
1105     m_PacketStat.reset();
1106     m_WakeupStat.reset();
1107
1108     m_data_buffer->setTickOffset(0);
1109
1110     // reset all non-device specific stuff
1111     // i.e. the iso stream and the associated ports
1112     if(!ReceiveStreamProcessor::reset()) {
1113             debugFatal("Could not do base class reset\n");
1114             return false;
1115     }
1116     return true;
1117 }
1118
1119 bool AmdtpReceiveStreamProcessor::prepare() {
1120
1121     m_PeriodStat.setName("RCV PERIOD");
1122     m_PacketStat.setName("RCV PACKET");
1123     m_WakeupStat.setName("RCV WAKEUP");
1124
1125     debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing (%p)...\n", this);
1126
1127     // prepare all non-device specific stuff
1128     // i.e. the iso stream and the associated ports
1129     if(!ReceiveStreamProcessor::prepare()) {
1130         debugFatal("Could not prepare base class\n");
1131         return false;
1132     }
1133
1134     switch (m_framerate) {
1135     case 32000:
1136         m_syt_interval = 8;
1137         break;
1138     case 44100:
1139         m_syt_interval = 8;
1140         break;
1141     default:
1142     case 48000:
1143         m_syt_interval = 8;
1144         break;
1145     case 88200:
1146         m_syt_interval = 16;
1147         break;
1148     case 96000:
1149         m_syt_interval = 16;
1150         break;
1151     case 176400:
1152         m_syt_interval = 32;
1153         break;
1154     case 192000:
1155         m_syt_interval = 32;
1156         break;
1157     }
1158
1159     // prepare the framerate estimate
1160     float ticks_per_frame = (TICKS_PER_SECOND*1.0) / ((float)m_framerate);
1161     m_ticks_per_frame=ticks_per_frame;
1162
1163     debugOutput(DEBUG_LEVEL_VERBOSE,"Initializing remote ticks/frame to %f\n",ticks_per_frame);
1164
1165     // initialize internal buffer
1166     unsigned int ringbuffer_size_frames=m_nb_buffers * m_period;
1167
1168     assert(m_data_buffer);
1169     m_data_buffer->setBufferSize(ringbuffer_size_frames);
1170     m_data_buffer->setEventSize(sizeof(quadlet_t));
1171     m_data_buffer->setEventsPerFrame(m_dimension);
1172
1173     // the buffer is written every syt_interval
1174     m_data_buffer->setUpdatePeriod(m_syt_interval);
1175     m_data_buffer->setNominalRate(ticks_per_frame);
1176
1177     m_data_buffer->setWrapValue(128L*TICKS_PER_SECOND);
1178
1179     m_data_buffer->prepare();
1180
1181     // set the parameters of ports we can:
1182     // we want the audio ports to be period buffered,
1183     // and the midi ports to be packet buffered
1184     for ( PortVectorIterator it = m_Ports.begin();
1185           it != m_Ports.end();
1186           ++it )
1187     {
1188         debugOutput(DEBUG_LEVEL_VERBOSE, "Setting up port %s\n",(*it)->getName().c_str());
1189         if(!(*it)->setBufferSize(m_period)) {
1190             debugFatal("Could not set buffer size to %d\n",m_period);
1191             return false;
1192         }
1193
1194         switch ((*it)->getPortType()) {
1195             case Port::E_Audio:
1196                 if(!(*it)->setSignalType(Port::E_PeriodSignalled)) {
1197                     debugFatal("Could not set signal type to PeriodSignalling");
1198                     return false;
1199                 }
1200                 // buffertype and datatype are dependant on the API
1201                 debugWarning("---------------- ! Doing hardcoded dummy setup ! --------------\n");
1202                 // buffertype and datatype are dependant on the API
1203                 if(!(*it)->setBufferType(Port::E_PointerBuffer)) {
1204                     debugFatal("Could not set buffer type");
1205                     return false;
1206                 }
1207                 if(!(*it)->useExternalBuffer(true)) {
1208                     debugFatal("Could not set external buffer usage");
1209                     return false;
1210                 }
1211                 if(!(*it)->setDataType(Port::E_Float)) {
1212                     debugFatal("Could not set data type");
1213                     return false;
1214                 }
1215                 break;
1216             case Port::E_Midi:
1217                 if(!(*it)->setSignalType(Port::E_PacketSignalled)) {
1218                     debugFatal("Could not set signal type to PacketSignalling");
1219                     return false;
1220                 }
1221                 // buffertype and datatype are dependant on the API
1222                 // buffertype and datatype are dependant on the API
1223                 debugWarning("---------------- ! Doing hardcoded test setup ! --------------\n");
1224                 // buffertype and datatype are dependant on the API
1225                 if(!(*it)->setBufferType(Port::E_RingBuffer)) {
1226                     debugFatal("Could not set buffer type");
1227                     return false;
1228                 }
1229                 if(!(*it)->setDataType(Port::E_MidiEvent)) {
1230                     debugFatal("Could not set data type");
1231                     return false;
1232                 }
1233                 break;
1234             default:
1235                 debugWarning("Unsupported port type specified\n");
1236                 break;
1237         }
1238     }
1239
1240     // the API specific settings of the ports should already be set,
1241     // as this is called from the processorManager->prepare()
1242     // so we can init the ports
1243     if(!initPorts()) {
1244         debugFatal("Could not initialize ports!\n");
1245         return false;
1246     }
1247
1248     if(!preparePorts()) {
1249         debugFatal("Could not initialize ports!\n");
1250         return false;
1251     }
1252
1253     debugOutput( DEBUG_LEVEL_VERBOSE, "Prepared for:\n");
1254     debugOutput( DEBUG_LEVEL_VERBOSE, " Samplerate: %d, DBS: %d, SYT: %d\n",
1255              m_framerate,m_dimension,m_syt_interval);
1256     debugOutput( DEBUG_LEVEL_VERBOSE, " PeriodSize: %d, NbBuffers: %d\n",
1257              m_period,m_nb_buffers);
1258     debugOutput( DEBUG_LEVEL_VERBOSE, " Port: %d, Channel: %d\n",
1259              m_port,m_channel);
1260
1261     return true;
1262
1263 }
1264
1265 bool AmdtpReceiveStreamProcessor::prepareForStart() {
1266     disable();
1267     return true;
1268 }
1269
1270 bool AmdtpReceiveStreamProcessor::prepareForStop() {
1271     disable();
1272     return true;
1273 }
1274
1275 bool AmdtpReceiveStreamProcessor::getFrames(unsigned int nbframes, int64_t ts) {
1276
1277     m_PeriodStat.mark(m_data_buffer->getBufferFill());
1278     uint64_t ts_head;
1279     signed int fc;
1280     int32_t lag_ticks;
1281     float lag_frames;
1282
1283     // in order to sync up multiple received streams, we should
1284     // use the ts parameter. It specifies the time of the block's
1285     // first sample.
1286    
1287     ffado_timestamp_t ts_head_tmp;
1288     m_data_buffer->getBufferHeadTimestamp(&ts_head_tmp, &fc);
1289     ts_head=(uint64_t)ts_head_tmp;
1290     lag_ticks=diffTicks(ts, ts_head);
1291     float rate=m_data_buffer->getRate();
1292    
1293     assert(rate!=0.0);
1294    
1295     lag_frames=(((float)lag_ticks)/rate);
1296    
1297     if (lag_frames>=1.0) {
1298         // the stream leads
1299         debugOutput( DEBUG_LEVEL_VERBOSE, "stream (%p): lags  with %6d ticks = %10.5f frames (rate=%10.5f)\n",this,lag_ticks,lag_frames,rate);
1300        
1301         if (lag_frames>=10.0) {
1302             debugOutput( DEBUG_LEVEL_VERBOSE, "  %lld, %llu, %d\n", ts, ts_head, fc);
1303         }
1304        
1305         // ditch the excess frames
1306         char dummy[m_data_buffer->getBytesPerFrame()]; // one frame of garbage
1307         int frames_to_ditch=(int)(lag_frames);
1308         debugOutput( DEBUG_LEVEL_VERBOSE, "stream (%p): ditching %d frames (@ ts=%lld)\n",this,frames_to_ditch,ts);
1309        
1310         while (frames_to_ditch--) {
1311 //             m_data_buffer->readFrames(1, dummy);
1312         }
1313        
1314     } else if (lag_frames<=-1.0) {
1315         // the stream leads
1316         debugOutput( DEBUG_LEVEL_VERBOSE, "stream (%p): leads with %6d ticks = %10.5f frames (rate=%10.5f)\n",this,lag_ticks,lag_frames,rate);
1317        
1318         if (lag_frames<=-10.0) {
1319             debugOutput( DEBUG_LEVEL_VERBOSE, "  %lld, %llu, %d\n", ts, ts_head, fc);
1320         }
1321        
1322         // add some padding frames
1323         int frames_to_add=(int)lag_frames;
1324         debugOutput( DEBUG_LEVEL_VERBOSE, "stream (%p): adding %d frames (@ ts=%lld)\n",this,-frames_to_add,ts);
1325        
1326         while (frames_to_add++) {
1327 //             m_data_buffer->writeDummyFrame();
1328         }
1329     }
1330    
1331     // ask the buffer to process nbframes of frames
1332     // using it's registered client's processReadBlock(),
1333     // which should be ours
1334     m_data_buffer->blockProcessReadFrames(nbframes);
1335
1336     return true;
1337 }
1338
1339 /**
1340  * \brief write received events to the stream ringbuffers.
1341  */
1342 bool AmdtpReceiveStreamProcessor::processReadBlock(char *data,
1343                        unsigned int nevents, unsigned int offset)
1344 {
1345     debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "(%p)->processReadBlock(%u, %u)\n",this,nevents,offset);
1346
1347     bool no_problem=true;
1348
1349     for ( PortVectorIterator it = m_PeriodPorts.begin();
1350           it != m_PeriodPorts.end();
1351           ++it )
1352     {
1353
1354         if((*it)->isDisabled()) {continue;};
1355
1356         //FIXME: make this into a static_cast when not DEBUG?
1357
1358         AmdtpPortInfo *pinfo=dynamic_cast<AmdtpPortInfo *>(*it);
1359         assert(pinfo); // this should not fail!!
1360
1361         switch(pinfo->getFormat()) {
1362         case AmdtpPortInfo::E_MBLA:
1363             if(decodeMBLAEventsToPort(static_cast<AmdtpAudioPort *>(*it), (quadlet_t *)data, offset, nevents)) {
1364                 debugWarning("Could not decode packet MBLA to port %s",(*it)->getName().c_str());
1365                 no_problem=false;
1366             }
1367             break;
1368         case AmdtpPortInfo::E_SPDIF: // still unimplemented
1369             break;
1370     /* for this processor, midi is a packet based port
1371         case AmdtpPortInfo::E_Midi:
1372             break;*/
1373         default: // ignore
1374             break;
1375         }
1376     }
1377     return no_problem;
1378
1379 }
1380
1381 /**
1382  * @brief decode a packet for the packet-based ports
1383  *
1384  * @param data Packet data
1385  * @param nevents number of events in data (including events of other ports & port types)
1386  * @param dbc DataBlockCount value for this packet
1387  * @return true if all successfull
1388  */
1389 bool AmdtpReceiveStreamProcessor::decodePacketPorts(quadlet_t *data, unsigned int nevents, unsigned int dbc)
1390 {
1391     bool ok=true;
1392
1393     quadlet_t *target_event=NULL;
1394     unsigned int j;
1395
1396     for ( PortVectorIterator it = m_PacketPorts.begin();
1397           it != m_PacketPorts.end();
1398           ++it )
1399     {
1400
1401 #ifdef DEBUG
1402         AmdtpPortInfo *pinfo=dynamic_cast<AmdtpPortInfo *>(*it);
1403         assert(pinfo); // this should not fail!!
1404
1405         // the only packet type of events for AMDTP is MIDI in mbla
1406         assert(pinfo->getFormat()==AmdtpPortInfo::E_Midi);
1407 #endif
1408         AmdtpMidiPort *mp=static_cast<AmdtpMidiPort *>(*it);
1409
1410         // we decode this directly (no function call) due to the high frequency
1411         /* idea:
1412         spec says: current_midi_port=(dbc+j)%8;
1413         => if we start at (dbc+stream->location-1)%8,
1414         we'll start at the right event for the midi port.
1415         => if we increment j with 8, we stay at the right event.
1416         */
1417         // FIXME: as we know in advance how big a packet is (syt_interval) we can
1418         //        predict how much loops will be present here
1419         for(j = (dbc & 0x07)+mp->getLocation(); j < nevents; j += 8) {
1420             target_event=(quadlet_t *)(data + ((j * m_dimension) + mp->getPosition()));
1421             quadlet_t sample_int=ntohl(*target_event);
1422             // FIXME: this assumes that 2X and 3X speed isn't used,
1423             // because only the 1X slot is put into the ringbuffer
1424             if(IEC61883_AM824_GET_LABEL(sample_int) != IEC61883_AM824_LABEL_MIDI_NO_DATA) {
1425                 sample_int=(sample_int >> 16) & 0x000000FF;
1426                 if(!mp->writeEvent(&sample_int)) {
1427                     debugWarning("Packet port events lost\n");
1428                     ok=false;
1429                 }
1430             }
1431         }
1432
1433     }
1434
1435     return ok;
1436 }
1437
1438 int AmdtpReceiveStreamProcessor::decodeMBLAEventsToPort(AmdtpAudioPort *p, quadlet_t *data,
1439                        unsigned int offset, unsigned int nevents)
1440 {
1441     unsigned int j=0;
1442
1443 //     printf("****************\n");
1444 //     hexDumpQuadlets(data,m_dimension*4);
1445 //     printf("****************\n");
1446
1447     quadlet_t *target_event;
1448
1449     target_event=(quadlet_t *)(data + p->getPosition());
1450
1451     switch(p->getDataType()) {
1452         default:
1453         case Port::E_Int24:
1454             {
1455                 quadlet_t *buffer=(quadlet_t *)(p->getBufferAddress());
1456
1457                 assert(nevents + offset <= p->getBufferSize());
1458
1459                 buffer+=offset;
1460
1461                 for(j = 0; j < nevents; j += 1) { // decode max nsamples
1462                     *(buffer)=(ntohl((*target_event) ) & 0x00FFFFFF);
1463                     buffer++;
1464                     target_event+=m_dimension;
1465                 }
1466             }
1467             break;
1468         case Port::E_Float:
1469             {
1470                 const float multiplier = 1.0f / (float)(0x7FFFFF);
1471                 float *buffer=(float *)(p->getBufferAddress());
1472
1473                 assert(nevents + offset <= p->getBufferSize());
1474
1475                 buffer+=offset;
1476
1477                 for(j = 0; j < nevents; j += 1) { // decode max nsamples
1478
1479                     unsigned int v = ntohl(*target_event) & 0x00FFFFFF;
1480                     // sign-extend highest bit of 24-bit int
1481                     int tmp = (int)(v << 8) / 256;
1482
1483                     *buffer = tmp * multiplier;
1484
1485                     buffer++;
1486                     target_event+=m_dimension;
1487                 }
1488             }
1489             break;
1490     }
1491
1492     return 0;
1493 }
1494
1495 } // end of namespace Streaming
Note: See TracBrowser for help on using the browser.