root/branches/streaming-rework/src/libstreaming/AmdtpStreamProcessor.cpp

Revision 386, 62.3 kB (checked in by pieterpalmers, 16 years ago)

- moved files around to the place they belong
- fixed all compile warnings

Line 
1 /* $Id$ */
2
3 /*
4  *   FreeBob Streaming API
5  *   FreeBob = Firewire (pro-)audio for linux
6  *
7  *   http://freebob.sf.net
8  *
9  *   Copyright (C) 2005,2006 Pieter Palmers <pieterpalmers@users.sourceforge.net>
10  *
11  *   This program is free software {} you can redistribute it and/or modify
12  *   it under the terms of the GNU General Public License as published by
13  *   the Free Software Foundation {} either version 2 of the License, or
14  *   (at your option) any later version.
15  *
16  *   This program is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY {} without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with this program {} if not, write to the Free Software
23  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  *
26  *
27  */
28
29 #include "AmdtpStreamProcessor.h"
30 #include "Port.h"
31 #include "AmdtpPort.h"
32
33 #include "cycletimer.h"
34
35 #include <netinet/in.h>
36 #include <assert.h>
37
38 #define RECEIVE_DLL_INTEGRATION_COEFFICIENT 0.015
39
40 #define RECEIVE_PROCESSING_DELAY (10000)
41
42 // in ticks
43 #define TRANSMIT_TRANSFER_DELAY 9000U
44 // the number of cycles to send a packet in advance of it's timestamp
45 #define TRANSMIT_ADVANCE_CYCLES 1U
46
47 namespace FreebobStreaming {
48
49 IMPL_DEBUG_MODULE( AmdtpTransmitStreamProcessor, AmdtpTransmitStreamProcessor, DEBUG_LEVEL_NORMAL );
50 IMPL_DEBUG_MODULE( AmdtpReceiveStreamProcessor, AmdtpReceiveStreamProcessor, DEBUG_LEVEL_NORMAL );
51
52
53 /* transmit */
54 AmdtpTransmitStreamProcessor::AmdtpTransmitStreamProcessor(int port, int framerate, int dimension)
55         : TransmitStreamProcessor(port, framerate), m_dimension(dimension)
56         , m_last_timestamp(0), m_dbc(0), m_ringbuffer_size_frames(0)
57         {
58
59
60 }
61
62 AmdtpTransmitStreamProcessor::~AmdtpTransmitStreamProcessor() {
63         freebob_ringbuffer_free(m_event_buffer);
64         free(m_cluster_buffer);
65 }
66
67 /**
68  * @return
69  */
70 bool AmdtpTransmitStreamProcessor::init() {
71
72         debugOutput( DEBUG_LEVEL_VERBOSE, "Initializing (%p)...\n");
73         // call the parent init
74         // this has to be done before allocating the buffers,
75         // because this sets the buffersizes from the processormanager
76         if(!TransmitStreamProcessor::init()) {
77                 debugFatal("Could not do base class init (%p)\n",this);
78                 return false;
79         }
80        
81         return true;
82 }
83
84 void AmdtpTransmitStreamProcessor::setVerboseLevel(int l) {
85         setDebugLevel(l);
86         TransmitStreamProcessor::setVerboseLevel(l);
87 }
88
89 enum raw1394_iso_disposition
90 AmdtpTransmitStreamProcessor::getPacket(unsigned char *data, unsigned int *length,
91                       unsigned char *tag, unsigned char *sy,
92                       int cycle, unsigned int dropped, unsigned int max_length) {
93    
94     struct iec61883_packet *packet = (struct iec61883_packet *) data;
95     unsigned int nevents=0;
96    
97     debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"Xmit handler for cycle %d\n",cycle);
98    
99 #ifdef DEBUG
100     if(dropped>0) {
101         debugWarning("Dropped %d packets on cycle %d\n",dropped, cycle);
102     }
103 #endif
104    
105     /* Our node ID can change after a bus reset, so it is best to fetch
106      * our node ID for each packet. */
107     packet->sid = getNodeId() & 0x3f;
108
109     packet->dbs = m_dimension;
110     packet->fn = 0;
111     packet->qpc = 0;
112     packet->sph = 0;
113     packet->reserved = 0;
114     packet->dbc = m_dbc;
115     packet->eoh1 = 2;
116     packet->fmt = IEC61883_FMT_AMDTP;
117
118     // recalculate the buffer head timestamp
119     float ticks_per_frame=m_SyncSource->getTicksPerFrame();
120    
121     // the base timestamp is the one of the last sample in the buffer
122     uint64_t ts_tail;
123     uint64_t fc;
124    
125     getBufferTailTimestamp(&ts_tail, &fc); // thread safe
126    
127     int64_t timestamp = ts_tail;
128    
129     // meaning that the first sample in the buffer lies m_framecounter * rate
130     // earlier. This gives the next equation:
131     //   timestamp = m_last_timestamp - m_framecounter * rate
132     timestamp -= (int64_t)((float)fc * ticks_per_frame);
133    
134     // FIXME: test
135     // substract the receive transfer delay
136     timestamp -= RECEIVE_PROCESSING_DELAY;
137    
138     // this happens if m_buffer_tail_timestamp wraps around while there are
139     // not much frames in the buffer. We should add the wraparound value of the ticks
140     // counter
141     if (timestamp < 0) {
142         timestamp += TICKS_PER_SECOND * 128L;
143     }
144     // this happens when the last timestamp is near wrapping, and
145     // m_framecounter is low.
146     // this means: m_last_timestamp is near wrapping and have just had
147     // a getPackets() from the client side. the projected next_period
148     // boundary lies beyond the wrap value.
149     // the action is to wrap the value.
150     else if (timestamp >= TICKS_PER_SECOND * 128L) {
151         timestamp -= TICKS_PER_SECOND * 128L;
152     }
153    
154     // determine if we want to send a packet or not
155     // note that we can't use getCycleTimer directly here,
156     // because packets are queued in advance. This means that
157     // we the packet we are constructing will be sent out
158     // on 'cycle', not 'now'.
159     unsigned int ctr=m_handler->getCycleTimer();
160     int now_cycles = (int)CYCLE_TIMER_GET_CYCLES(ctr);
161    
162     // the difference between 'now' and the cycle this
163     // packet is intended for
164     int cycle_diff = cycle - now_cycles;
165    
166     // detect wraparounds
167     if(cycle_diff < -(int)(CYCLES_PER_SECOND/2)) {
168         cycle_diff += CYCLES_PER_SECOND;
169     } else if(cycle_diff > (int)(CYCLES_PER_SECOND/2)) {
170         cycle_diff -= CYCLES_PER_SECOND;
171     }
172
173     // as long as the cycle parameter is not in sync with
174     // the current time, the stream is considered not
175     // to be 'running'
176     if(cycle_diff < 0 || cycle == -1) {
177         m_running=false;
178     } else {
179         m_running=true;
180     }
181    
182 #ifdef DEBUG
183     if(cycle_diff < 0) {
184         debugWarning("Requesting packet for cycle %04d which is in the past (now=%04dcy)\n",
185             cycle, now_cycles);
186     }
187 #endif
188     // if cycle lies cycle_diff cycles in the future, we should
189     // queue this packet cycle_diff * TICKS_PER_CYCLE earlier than
190     // we would if it were to be sent immediately.
191    
192     // determine the 'now' time in ticks
193     uint64_t cycle_timer=CYCLE_TIMER_TO_TICKS(ctr);
194    
195     // time until the packet is to be sent (if > 0: send packet)
196     int64_t until_next=timestamp-(int64_t)cycle_timer;
197    
198     int64_t utn2=until_next; // debug!!
199    
200     // we send a packet some cycles in advance, to avoid the
201     // following situation:
202     // suppose we are only a few ticks away from
203     // the moment to send this packet. therefore we decide
204     // not to send the packet, but send it in the next cycle.
205     // This means that the next time point will be 3072 ticks
206     // later, making that the timestamp will be expired when the
207     // packet is sent, unless TRANSFER_DELAY > 3072.
208     // this means that we need at least one cycle of extra buffering.
209     until_next -= TICKS_PER_CYCLE * TRANSMIT_ADVANCE_CYCLES;
210    
211     // we have to queue it cycle_diff * TICKS_PER_CYCLE earlier
212     until_next -= cycle_diff * TICKS_PER_CYCLE;
213
214     // the maximal difference we can allow (64secs)
215     const int64_t max=TICKS_PER_SECOND*64L;
216
217 #ifdef DEBUG
218     if(!m_disabled) {
219         debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "=> TS=%11llu, CTR=%11llu, FC=%5d\n",
220             timestamp, cycle_timer, fc
221             );
222         debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "    UTN=%11lld, UTN2=%11lld\n",
223             until_next, utn2
224             );
225         debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "    CY_NOW=%04d, CY_TARGET=%04d, CY_DIFF=%04lld\n",
226             now_cycles, cycle, cycle_diff
227             );
228     }
229 #endif
230
231     if(until_next > max) {
232         // this means that cycle_timer has wrapped, but
233         // timestamp has not. we should unwrap cycle_timer
234         // by adding TICKS_PER_SECOND*128L, meaning that we should substract
235         // this value from until_next           
236         until_next -= TICKS_PER_SECOND*128L;
237     } else if (until_next < -max) {
238         // this means that timestamp has wrapped, but
239         // cycle_timer has not. we should unwrap timestamp
240         // by adding TICKS_PER_SECOND*128L, meaning that we should add
241         // this value from until_next
242         until_next += TICKS_PER_SECOND*128L;
243     }
244
245 #ifdef DEBUG
246     if(!m_disabled) {
247         debugOutput(DEBUG_LEVEL_VERY_VERBOSE, " > TS=%11llu, CTR=%11llu, FC=%5d\n",
248             timestamp, cycle_timer, fc
249             );
250         debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "    UTN=%11lld, UTN2=%11lld\n",
251             until_next, utn2
252             );
253     }
254 #endif
255
256     // don't process the stream when it is not enabled, not running
257     // or when the next sample is not due yet.
258    
259     // we do have to generate (semi) valid packets
260     // that means that we'll send NODATA packets.
261     // we don't add payload because DICE devices don't like that.
262     if((until_next>0) || m_disabled || !m_running) {
263         // no-data packets have syt=0xFFFF
264         // and have the usual amount of events as dummy data (?)
265         packet->fdf = IEC61883_FDF_NODATA;
266         packet->syt = 0xffff;
267        
268         // the dbc is incremented even with no data packets
269         m_dbc += m_syt_interval;
270
271         // this means no-data packets with payload (DICE doesn't like that)
272         *length = 2*sizeof(quadlet_t) + m_syt_interval * m_dimension * sizeof(quadlet_t);
273        
274         // this means no-data packets without payload
275         //*length = 2*sizeof(quadlet_t);
276        
277         *tag = IEC61883_TAG_WITH_CIP;
278         *sy = 0;
279        
280         if(m_disabled) {
281             // indicate that we are now in a disabled state.
282             m_is_disabled=true;
283         } else {
284             // indicate that we are now in an enabled state.
285             m_is_disabled=false;
286         }
287        
288         return RAW1394_ISO_DEFER;
289     }
290    
291     // indicate that we are now in an enabled state.
292     m_is_disabled=false;
293            
294     // construct the packet
295     nevents = m_syt_interval;
296     m_dbc += m_syt_interval;
297    
298     *tag = IEC61883_TAG_WITH_CIP;
299     *sy = 0;
300      
301     enum raw1394_iso_disposition retval;
302
303     unsigned int read_size=nevents*sizeof(quadlet_t)*m_dimension;
304
305     if ((freebob_ringbuffer_read(m_event_buffer,(char *)(data+8),read_size)) <
306                             read_size)
307     {
308         /* there is no more data in the ringbuffer */
309
310         debugWarning("Transmit buffer underrun (cycle %d, FC=%d, PC=%d)\n",
311                  cycle, getFrameCounter(), m_handler->getPacketCount());
312
313         nevents=0;
314
315         // TODO: we have to be a little smarter here
316         //       because we have some slack on the device side (TRANSFER_DELAY)
317         //       we can allow some skipped packets
318         // signal underrun
319         m_xruns++;
320
321         // compose a no-data packet, we should always
322         // send a valid packet
323         packet->fdf = IEC61883_FDF_NODATA;
324         packet->syt = 0xffff;
325
326         // this means no-data packets with payload (DICE doesn't like that)
327         *length = 2*sizeof(quadlet_t) + m_syt_interval * m_dimension * sizeof(quadlet_t);
328
329         // this means no-data packets without payload
330         //*length = 2*sizeof(quadlet_t);
331
332         retval=RAW1394_ISO_DEFER;
333     } else {
334         *length = read_size + 8;
335
336         // process all ports that should be handled on a per-packet base
337         // this is MIDI for AMDTP (due to the need of DBC)
338         if (!encodePacketPorts((quadlet_t *)(data+8), nevents, packet->dbc)) {
339             debugWarning("Problem encoding Packet Ports\n");
340         }
341
342         packet->fdf = m_fdf;
343
344         // convert the timestamp to SYT format
345         uint64_t ts=timestamp + TRANSMIT_TRANSFER_DELAY;
346        
347         // check if it wrapped
348         if (ts >= TICKS_PER_SECOND * 128L) {
349             ts -= TICKS_PER_SECOND * 128L;
350         }
351        
352         unsigned int timestamp_SYT = TICKS_TO_SYT(ts);
353         packet->syt = ntohs(timestamp_SYT);
354
355         retval=RAW1394_ISO_OK;
356     }
357    
358     // calculate the new buffer head timestamp. this is
359     // the previous buffer head timestamp plus
360     // the number of frames sent * ticks_per_frame
361     timestamp += (int64_t)((float)nevents * ticks_per_frame );
362    
363     // check if it wrapped
364     if (timestamp >= TICKS_PER_SECOND * 128L) {
365         timestamp -= TICKS_PER_SECOND * 128L;
366     }
367    
368     // update the frame counter such that it reflects the new value
369     // also update the buffer head timestamp
370     // done in the SP base class
371     if (!StreamProcessor::getFrames(nevents, timestamp)) {
372         debugError("Could not do StreamProcessor::getFrames(%d, %llu)\n",nevents, timestamp);
373         retval=RAW1394_ISO_ERROR;
374     }
375
376     return retval;
377
378 }
379
380 int64_t AmdtpTransmitStreamProcessor::getTimeUntilNextPeriodUsecs() {
381     debugFatal("IMPLEMENT ME!");
382     return 0;
383 }
384
385 uint64_t AmdtpTransmitStreamProcessor::getTimeAtPeriodUsecs() {
386     // then we should convert this into usecs
387     // FIXME: we assume that the TimeSource of the IsoHandler is
388     //        in usecs.
389     return m_handler->mapToTimeSource(getTimeAtPeriod());
390 }
391
392 uint64_t AmdtpTransmitStreamProcessor::getTimeAtPeriod() {
393     debugFatal("IMPLEMENT ME!");
394    
395     return 0;
396 }
397
398 bool AmdtpTransmitStreamProcessor::prefill() {
399     if(!transferSilence(m_ringbuffer_size_frames)) {
400         debugFatal("Could not prefill transmit stream\n");
401         return false;
402     }
403
404     // when the buffer is prefilled, we should
405     // also initialize the base timestamp
406     // this base timestamp is the timestamp of the
407     // last buffer transfer.
408     uint64_t ts;
409     uint64_t fc;
410     m_SyncSource->getBufferHeadTimestamp(&ts, &fc); // thread safe
411
412     // update the frame counter such that it reflects the buffer content,
413     // and also update the buffer tail timestamp
414     // done in the SP base class
415     if (!StreamProcessor::putFrames(m_ringbuffer_size_frames, ts)) {
416         debugError("Could not do StreamProcessor::putFrames(%d, %llu)\n",
417             m_ringbuffer_size_frames, ts);
418         return false;
419     }
420
421     return true;
422    
423 }
424
425 bool AmdtpTransmitStreamProcessor::reset() {
426
427     debugOutput( DEBUG_LEVEL_VERBOSE, "Resetting...\n");
428
429     // reset the event buffer, discard all content
430     freebob_ringbuffer_reset(m_event_buffer);
431    
432     // reset the statistics
433     m_PeriodStat.reset();
434     m_PacketStat.reset();
435     m_WakeupStat.reset();
436    
437     // reset all non-device specific stuff
438     // i.e. the iso stream and the associated ports
439     if(!TransmitStreamProcessor::reset()) {
440         debugFatal("Could not do base class reset\n");
441         return false;
442     }
443    
444     // we should prefill the event buffer
445     if (!prefill()) {
446         debugFatal("Could not prefill buffers\n");
447         return false;   
448     }
449    
450     return true;
451 }
452
453 bool AmdtpTransmitStreamProcessor::prepare() {
454     m_PeriodStat.setName("XMT PERIOD");
455     m_PacketStat.setName("XMT PACKET");
456     m_WakeupStat.setName("XMT WAKEUP");
457
458     debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing...\n");
459    
460     // prepare all non-device specific stuff
461     // i.e. the iso stream and the associated ports
462     if(!TransmitStreamProcessor::prepare()) {
463         debugFatal("Could not prepare base class\n");
464         return false;
465     }
466    
467     switch (m_framerate) {
468     case 32000:
469         m_syt_interval = 8;
470         m_fdf = IEC61883_FDF_SFC_32KHZ;
471         break;
472     case 44100:
473         m_syt_interval = 8;
474         m_fdf = IEC61883_FDF_SFC_44K1HZ;
475         break;
476     default:
477     case 48000:
478         m_syt_interval = 8;
479         m_fdf = IEC61883_FDF_SFC_48KHZ;
480         break;
481     case 88200:
482         m_syt_interval = 16;
483         m_fdf = IEC61883_FDF_SFC_88K2HZ;
484         break;
485     case 96000:
486         m_syt_interval = 16;
487         m_fdf = IEC61883_FDF_SFC_96KHZ;
488         break;
489     case 176400:
490         m_syt_interval = 32;
491         m_fdf = IEC61883_FDF_SFC_176K4HZ;
492         break;
493     case 192000:
494         m_syt_interval = 32;
495         m_fdf = IEC61883_FDF_SFC_192KHZ;
496         break;
497     }
498    
499     iec61883_cip_init (
500         &m_cip_status,
501         IEC61883_FMT_AMDTP,
502         m_fdf,
503         m_framerate,
504         m_dimension,
505         m_syt_interval);
506
507     // allocate the event buffer
508     m_ringbuffer_size_frames=m_nb_buffers * m_period;
509    
510     // prepare the framerate estimate
511     m_ticks_per_frame = (TICKS_PER_SECOND*1.0) / ((float)m_framerate);
512    
513     // add the receive processing delay
514 //     m_ringbuffer_size_frames+=(uint)(RECEIVE_PROCESSING_DELAY/m_ticks_per_frame);
515    
516     if( !(m_event_buffer=freebob_ringbuffer_create(
517             (m_dimension * m_ringbuffer_size_frames) * sizeof(quadlet_t)))) {
518         debugFatal("Could not allocate memory event ringbuffer");
519         return false;
520     }
521
522     // allocate the temporary cluster buffer
523     if( !(m_cluster_buffer=(char *)calloc(m_dimension,sizeof(quadlet_t)))) {
524         debugFatal("Could not allocate temporary cluster buffer");
525         freebob_ringbuffer_free(m_event_buffer);
526         return false;
527     }
528
529     // set the parameters of ports we can:
530     // we want the audio ports to be period buffered,
531     // and the midi ports to be packet buffered
532     for ( PortVectorIterator it = m_Ports.begin();
533           it != m_Ports.end();
534           ++it )
535     {
536         debugOutput(DEBUG_LEVEL_VERBOSE, "Setting up port %s\n",(*it)->getName().c_str());
537         if(!(*it)->setBufferSize(m_period)) {
538             debugFatal("Could not set buffer size to %d\n",m_period);
539             return false;
540         }
541        
542        
543         switch ((*it)->getPortType()) {
544             case Port::E_Audio:
545                 if(!(*it)->setSignalType(Port::E_PeriodSignalled)) {
546                     debugFatal("Could not set signal type to PeriodSignalling");
547                     return false;
548                 }
549                 debugWarning("---------------- ! Doing hardcoded test setup ! --------------\n");
550                 // buffertype and datatype are dependant on the API
551                 if(!(*it)->setBufferType(Port::E_PointerBuffer)) {
552                     debugFatal("Could not set buffer type");
553                     return false;
554                 }
555                 if(!(*it)->useExternalBuffer(true)) {
556                     debugFatal("Could not set external buffer usage");
557                     return false;
558                 }
559                
560                 if(!(*it)->setDataType(Port::E_Float)) {
561                     debugFatal("Could not set data type");
562                     return false;
563                 }
564                
565                
566                 break;
567             case Port::E_Midi:
568                 if(!(*it)->setSignalType(Port::E_PacketSignalled)) {
569                     debugFatal("Could not set signal type to PeriodSignalling");
570                     return false;
571                 }
572                
573                 // we use a timing unit of 10ns
574                 // this makes sure that for the max syt interval
575                 // we don't have rounding, and keeps the numbers low
576                 // we have 1 slot every 8 events
577                 // we have syt_interval events per packet
578                 // => syt_interval/8 slots per packet
579                 // packet rate is 8000pkt/sec => interval=125us
580                 // so the slot interval is (1/8000)/(syt_interval/8)
581                 // or: 1/(1000 * syt_interval) sec
582                 // which is 1e9/(1000*syt_interval) nsec
583                 // or 100000/syt_interval 'units'
584                 // the event interval is fixed to 320us = 32000 'units'
585                 if(!(*it)->useRateControl(true,(100000/m_syt_interval),32000, false)) {
586                     debugFatal("Could not set signal type to PeriodSignalling");
587                     return false;
588                 }
589                
590                 // buffertype and datatype are dependant on the API
591                 debugWarning("---------------- ! Doing hardcoded test setup ! --------------\n");
592                 // buffertype and datatype are dependant on the API
593                 if(!(*it)->setBufferType(Port::E_RingBuffer)) {
594                     debugFatal("Could not set buffer type");
595                     return false;
596                 }
597                 if(!(*it)->setDataType(Port::E_MidiEvent)) {
598                     debugFatal("Could not set data type");
599                     return false;
600                 }
601                 break;
602             default:
603                 debugWarning("Unsupported port type specified\n");
604                 break;
605         }
606     }
607
608     // the API specific settings of the ports should already be set,
609     // as this is called from the processorManager->prepare()
610     // so we can init the ports
611     if(!initPorts()) {
612         debugFatal("Could not initialize ports!\n");
613         return false;
614     }
615
616     if(!preparePorts()) {
617         debugFatal("Could not initialize ports!\n");
618         return false;
619     }
620    
621     // prefilling is done in ...()
622     // because at that point the streams are running,
623     // while here they are not.
624    
625     // prefill the event buffer
626     if (!prefill()) {
627         debugFatal("Could not prefill buffers\n");
628         return false;   
629     }
630    
631     debugOutput( DEBUG_LEVEL_VERBOSE, "Prepared for:\n");
632     debugOutput( DEBUG_LEVEL_VERBOSE, " Samplerate: %d, FDF: %d, DBS: %d, SYT: %d\n",
633              m_framerate,m_fdf,m_dimension,m_syt_interval);
634     debugOutput( DEBUG_LEVEL_VERBOSE, " PeriodSize: %d, NbBuffers: %d\n",
635              m_period,m_nb_buffers);
636     debugOutput( DEBUG_LEVEL_VERBOSE, " Port: %d, Channel: %d\n",
637              m_port,m_channel);
638
639     return true;
640
641 }
642
643 bool AmdtpTransmitStreamProcessor::prepareForStart() {
644
645     return true;
646 }
647
648 bool AmdtpTransmitStreamProcessor::prepareForStop() {
649     disable();
650     return true;
651 }
652
653 bool AmdtpTransmitStreamProcessor::prepareForEnable() {
654     uint64_t ts;
655     uint64_t fc;
656    
657     debugOutput(DEBUG_LEVEL_VERBOSE,"Preparing to enable...\n");
658    
659     m_SyncSource->getBufferHeadTimestamp(&ts, &fc); // thread safe
660    
661     // recalculate the buffer head timestamp
662     float ticks_per_frame=m_SyncSource->getTicksPerFrame();
663    
664     // set buffer head timestamp
665     // this makes that the next sample to be sent out
666     // has the same timestamp as the last one received
667     // plus one frame
668     ts += (uint64_t)ticks_per_frame;
669     setBufferHeadTimestamp(ts);
670     int64_t timestamp = ts;
671
672     // since we have a full buffer, we know that the buffer tail lies
673     // m_ringbuffer_size_frames * rate earlier
674     timestamp += (int64_t)((float)m_ringbuffer_size_frames * ticks_per_frame);
675    
676     // this happens when the last timestamp is near wrapping, and
677     // m_framecounter is low.
678     // this means: m_last_timestamp is near wrapping and have just had
679     // a getPackets() from the client side. the projected next_period
680     // boundary lies beyond the wrap value.
681     // the action is to wrap the value.
682     if (timestamp >= TICKS_PER_SECOND * 128L) {
683         timestamp -= TICKS_PER_SECOND * 128L;
684     }
685
686     StreamProcessor::setBufferTailTimestamp(timestamp);
687    
688     debugOutput(DEBUG_LEVEL_VERBOSE,"TS=%10lld, TSTMP=%10llu, %f\n",
689                     ts, timestamp, ticks_per_frame);
690    
691     if (!StreamProcessor::prepareForEnable()) {
692         debugError("StreamProcessor::prepareForEnable failed\n");
693         return false;
694     }
695
696     return true;
697 }
698
699 bool AmdtpTransmitStreamProcessor::transferSilence(unsigned int size) {
700     /* a naive implementation would look like this: */
701    
702     unsigned int write_size=size*sizeof(quadlet_t)*m_dimension;
703     char *dummybuffer=(char *)calloc(sizeof(quadlet_t),size*m_dimension);
704     transmitSilenceBlock(dummybuffer, size, 0);
705
706     if (freebob_ringbuffer_write(m_event_buffer,(char *)(dummybuffer),write_size) < write_size) {
707         debugWarning("Could not write to event buffer\n");
708     }
709    
710     free(dummybuffer);
711    
712     return true;
713 }
714
715 bool AmdtpTransmitStreamProcessor::canClientTransferFrames(unsigned int nbframes) {
716     // there has to be enough space to put the frames in
717     return m_ringbuffer_size_frames - getFrameCounter() > nbframes;
718 }
719
720 bool AmdtpTransmitStreamProcessor::putFrames(unsigned int nbframes, int64_t ts) {
721     m_PeriodStat.mark(freebob_ringbuffer_read_space(m_event_buffer)/(4*m_dimension));
722
723     debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Transferring period...\n");
724     int xrun;
725     unsigned int offset=0;
726    
727     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "AmdtpTransmitStreamProcessor::putFrames(%d, %llu)\n",nbframes, ts);
728    
729     freebob_ringbuffer_data_t vec[2];
730     // we received one period of frames
731     // this is period_size*dimension of events
732     unsigned int events2write=nbframes*m_dimension;
733     unsigned int bytes2write=events2write*sizeof(quadlet_t);
734
735     /* write events2write bytes to the ringbuffer
736     *  first see if it can be done in one read.
737     *  if so, ok.
738     *  otherwise write up to a multiple of clusters directly to the buffer
739     *  then do the buffer wrap around using ringbuffer_write
740     *  then write the remaining data directly to the buffer in a third pass
741     *  Make sure that we cannot end up on a non-cluster aligned position!
742     */
743     unsigned int cluster_size=m_dimension*sizeof(quadlet_t);
744
745     while(bytes2write>0) {
746         int byteswritten=0;
747        
748         unsigned int frameswritten=(nbframes*cluster_size-bytes2write)/cluster_size;
749         offset=frameswritten;
750        
751         freebob_ringbuffer_get_write_vector(m_event_buffer, vec);
752            
753         if(vec[0].len==0) { // this indicates a full event buffer
754             debugError("XMT: Event buffer overrun in processor %p\n",this);
755             break;
756         }
757            
758         /* if we don't take care we will get stuck in an infinite loop
759         * because we align to a cluster boundary later
760         * the remaining nb of bytes in one write operation can be
761         * smaller than one cluster
762         * this can happen because the ringbuffer size is always a power of 2
763         */
764         if(vec[0].len<cluster_size) {
765            
766             // encode to the temporary buffer
767             xrun = transmitBlock(m_cluster_buffer, 1, offset);
768            
769             if(xrun<0) {
770                 // xrun detected
771                 debugError("XMT: Frame buffer underrun in processor %p\n",this);
772                 break;
773             }
774                
775             // use the ringbuffer function to write one cluster
776             // the write function handles the wrap around.
777             freebob_ringbuffer_write(m_event_buffer,
778                          m_cluster_buffer,
779                          cluster_size);
780                
781             // we advanced one cluster_size
782             bytes2write-=cluster_size;
783                
784         } else { //
785            
786             if(bytes2write>vec[0].len) {
787                 // align to a cluster boundary
788                 byteswritten=vec[0].len-(vec[0].len%cluster_size);
789             } else {
790                 byteswritten=bytes2write;
791             }
792                
793             xrun = transmitBlock(vec[0].buf,
794                          byteswritten/cluster_size,
795                          offset);
796            
797             if(xrun<0) {
798                     // xrun detected
799                 debugError("XMT: Frame buffer underrun in processor %p\n",this);
800                 break; // FIXME: return false ?
801             }
802
803             freebob_ringbuffer_write_advance(m_event_buffer, byteswritten);
804             bytes2write -= byteswritten;
805         }
806
807         // the bytes2write should always be cluster aligned
808         assert(bytes2write%cluster_size==0);
809
810     }
811    
812     // recalculate the buffer tail timestamp
813     float ticks_per_frame=m_SyncSource->getTicksPerFrame();
814    
815     // this makes that the last sample to be sent out on ISO
816     // has the same timestamp as the last one transfered
817     // to the client
818     // plus one frame
819     ts += (uint64_t)ticks_per_frame;
820     int64_t timestamp = ts;
821    
822     // however we have to preserve causality, meaning that we have to make
823     // sure that the worst-case buffer head timestamp still lies in the future.
824     // this worst case timestamp occurs when the xmit buffer is completely full.
825     // therefore we add m_ringbuffer_size_frames * ticks_per_frame to the timestamp.
826     // this will make sure that the buffer head timestamp lies in the future.
827     // the netto effect of this is that the system works as if the buffer processing
828     // by the client doesn't take time.
829    
830     timestamp += (int64_t)((float)m_ringbuffer_size_frames * ticks_per_frame);
831    
832     // wrap the timestamp if nescessary
833     if (timestamp >= TICKS_PER_SECOND * 128L) {
834         timestamp -= TICKS_PER_SECOND * 128L;
835     }
836
837     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "StreamProcessor::putFrames(%d, %llu)\n",nbframes, timestamp);
838
839     // update the frame counter such that it reflects the new value,
840     // and also update the buffer tail timestamp
841     // done in the SP base class
842     if (!StreamProcessor::putFrames(nbframes, timestamp)) {
843         debugError("Could not do StreamProcessor::putFrames(%d, %llu)\n",nbframes, timestamp);
844         return false;
845     }
846
847     return true;
848 }
849 /*
850  * write received events to the stream ringbuffers.
851  */
852
853 int AmdtpTransmitStreamProcessor::transmitBlock(char *data,
854                        unsigned int nevents, unsigned int offset)
855 {
856     int problem=0;
857
858     for ( PortVectorIterator it = m_PeriodPorts.begin();
859           it != m_PeriodPorts.end();
860           ++it )
861     {
862
863         if((*it)->isDisabled()) {continue;};
864        
865         //FIXME: make this into a static_cast when not DEBUG?
866
867         AmdtpPortInfo *pinfo=dynamic_cast<AmdtpPortInfo *>(*it);
868         assert(pinfo); // this should not fail!!
869
870         switch(pinfo->getFormat()) {
871         case AmdtpPortInfo::E_MBLA:
872             if(encodePortToMBLAEvents(static_cast<AmdtpAudioPort *>(*it), (quadlet_t *)data, offset, nevents)) {
873                 debugWarning("Could not encode port %s to MBLA events",(*it)->getName().c_str());
874                 problem=1;
875             }
876             break;
877         case AmdtpPortInfo::E_SPDIF: // still unimplemented
878             break;
879         default: // ignore
880             break;
881         }
882     }
883     return problem;
884
885 }
886
887 int AmdtpTransmitStreamProcessor::transmitSilenceBlock(char *data,
888                        unsigned int nevents, unsigned int offset)
889 {
890     int problem=0;
891
892     for ( PortVectorIterator it = m_PeriodPorts.begin();
893           it != m_PeriodPorts.end();
894           ++it )
895     {
896
897         //FIXME: make this into a static_cast when not DEBUG?
898
899         AmdtpPortInfo *pinfo=dynamic_cast<AmdtpPortInfo *>(*it);
900         assert(pinfo); // this should not fail!!
901
902         switch(pinfo->getFormat()) {
903         case AmdtpPortInfo::E_MBLA:
904             if(encodeSilencePortToMBLAEvents(static_cast<AmdtpAudioPort *>(*it), (quadlet_t *)data, offset, nevents)) {
905                 debugWarning("Could not encode port %s to MBLA events",(*it)->getName().c_str());
906                 problem=1;
907             }
908             break;
909         case AmdtpPortInfo::E_SPDIF: // still unimplemented
910             break;
911         default: // ignore
912             break;
913         }
914     }
915     return problem;
916
917 }
918
919 /**
920  * @brief decode a packet for the packet-based ports
921  *
922  * @param data Packet data
923  * @param nevents number of events in data (including events of other ports & port types)
924  * @param dbc DataBlockCount value for this packet
925  * @return true if all successfull
926  */
927 bool AmdtpTransmitStreamProcessor::encodePacketPorts(quadlet_t *data, unsigned int nevents, unsigned int dbc)
928 {
929     bool ok=true;
930     char byte;
931    
932     quadlet_t *target_event=NULL;
933     unsigned int j;
934    
935     for ( PortVectorIterator it = m_PacketPorts.begin();
936           it != m_PacketPorts.end();
937           ++it )
938     {
939
940 #ifdef DEBUG
941         AmdtpPortInfo *pinfo=dynamic_cast<AmdtpPortInfo *>(*it);
942         assert(pinfo); // this should not fail!!
943
944         // the only packet type of events for AMDTP is MIDI in mbla
945         assert(pinfo->getFormat()==AmdtpPortInfo::E_Midi);
946 #endif
947        
948         AmdtpMidiPort *mp=static_cast<AmdtpMidiPort *>(*it);
949        
950         // we encode this directly (no function call) due to the high frequency
951         /* idea:
952         spec says: current_midi_port=(dbc+j)%8;
953         => if we start at (dbc+stream->location-1)%8 [due to location_min=1],
954         we'll start at the right event for the midi port.
955         => if we increment j with 8, we stay at the right event.
956         */
957         // FIXME: as we know in advance how big a packet is (syt_interval) we can
958         //        predict how much loops will be present here
959         // first prefill the buffer with NO_DATA's on all time muxed channels
960        
961         for(j = (dbc & 0x07)+mp->getLocation()-1; j < nevents; j += 8) {
962        
963             target_event=(quadlet_t *)(data + ((j * m_dimension) + mp->getPosition()));
964            
965             if(mp->canRead()) { // we can send a byte
966                 mp->readEvent(&byte);
967                 *target_event=htonl(
968                     IEC61883_AM824_SET_LABEL((byte)<<16,
969                                              IEC61883_AM824_LABEL_MIDI_1X));
970             } else {
971                 // can't send a byte, either because there is no byte,
972                 // or because this would exceed the maximum rate
973                 *target_event=htonl(
974                     IEC61883_AM824_SET_LABEL(0,IEC61883_AM824_LABEL_MIDI_NO_DATA));
975             }
976         }
977
978     }
979        
980     return ok;
981 }
982
983
984 int AmdtpTransmitStreamProcessor::encodePortToMBLAEvents(AmdtpAudioPort *p, quadlet_t *data,
985                        unsigned int offset, unsigned int nevents)
986 {
987     unsigned int j=0;
988
989     quadlet_t *target_event;
990
991     target_event=(quadlet_t *)(data + p->getPosition());
992
993     switch(p->getDataType()) {
994         default:
995         case Port::E_Int24:
996             {
997                 quadlet_t *buffer=(quadlet_t *)(p->getBufferAddress());
998
999                 assert(nevents + offset <= p->getBufferSize());
1000
1001                 buffer+=offset;
1002
1003                 for(j = 0; j < nevents; j += 1) { // decode max nsamples
1004                     *target_event = htonl((*(buffer) & 0x00FFFFFF) | 0x40000000);
1005                     buffer++;
1006                     target_event += m_dimension;
1007                 }
1008             }
1009             break;
1010         case Port::E_Float:
1011             {
1012                 const float multiplier = (float)(0x7FFFFF00);
1013                 float *buffer=(float *)(p->getBufferAddress());
1014
1015                 assert(nevents + offset <= p->getBufferSize());
1016
1017                 buffer+=offset;
1018
1019                 for(j = 0; j < nevents; j += 1) { // decode max nsamples               
1020    
1021                     // don't care for overflow
1022                     float v = *buffer * multiplier;  // v: -231 .. 231
1023                     unsigned int tmp = ((int)v);
1024                     *target_event = htonl((tmp >> 8) | 0x40000000);
1025                    
1026                     buffer++;
1027                     target_event += m_dimension;
1028                 }
1029             }
1030             break;
1031     }
1032
1033     return 0;
1034 }
1035 int AmdtpTransmitStreamProcessor::encodeSilencePortToMBLAEvents(AmdtpAudioPort *p, quadlet_t *data,
1036                        unsigned int offset, unsigned int nevents)
1037 {
1038     unsigned int j=0;
1039
1040     quadlet_t *target_event;
1041
1042     target_event=(quadlet_t *)(data + p->getPosition());
1043
1044     switch(p->getDataType()) {
1045         default:
1046         case Port::E_Int24:
1047         case Port::E_Float:
1048             {
1049                 for(j = 0; j < nevents; j += 1) { // decode max nsamples
1050                     *target_event = htonl(0x40000000);
1051                     target_event += m_dimension;
1052                 }
1053             }
1054             break;
1055     }
1056
1057     return 0;
1058 }
1059
1060 /* --------------------- RECEIVE ----------------------- */
1061
1062 AmdtpReceiveStreamProcessor::AmdtpReceiveStreamProcessor(int port, int framerate, int dimension)
1063     : ReceiveStreamProcessor(port, framerate), m_dimension(dimension), m_last_timestamp(0), m_last_timestamp2(0) {
1064
1065
1066 }
1067
1068 AmdtpReceiveStreamProcessor::~AmdtpReceiveStreamProcessor() {
1069     freebob_ringbuffer_free(m_event_buffer);
1070     free(m_cluster_buffer);
1071
1072 }
1073
1074 bool AmdtpReceiveStreamProcessor::init() {
1075     // call the parent init
1076     // this has to be done before allocating the buffers,
1077     // because this sets the buffersizes from the processormanager
1078     if(!ReceiveStreamProcessor::init()) {
1079         debugFatal("Could not do base class init (%d)\n",this);
1080         return false;
1081     }
1082
1083     return true;
1084 }
1085
1086 enum raw1394_iso_disposition
1087 AmdtpReceiveStreamProcessor::putPacket(unsigned char *data, unsigned int length,
1088                   unsigned char channel, unsigned char tag, unsigned char sy,
1089                   unsigned int cycle, unsigned int dropped) {
1090    
1091     enum raw1394_iso_disposition retval=RAW1394_ISO_OK;
1092    
1093     struct iec61883_packet *packet = (struct iec61883_packet *) data;
1094     assert(packet);
1095
1096 #ifdef DEBUG
1097     if(dropped>0) {
1098         debugWarning("Dropped %d packets on cycle %d\n",dropped, cycle);
1099     }
1100 #endif
1101    
1102     if((packet->fmt == 0x10) && (packet->fdf != 0xFF) && (packet->syt != 0xFFFF) && (packet->dbs>0) && (length>=2*sizeof(quadlet_t))) {
1103         unsigned int nevents=((length / sizeof (quadlet_t)) - 2)/packet->dbs;
1104        
1105         //=> store the previous timestamp
1106         m_last_timestamp2=m_last_timestamp;
1107        
1108         //=> convert the SYT to ticks
1109         unsigned int syt_timestamp=ntohs(packet->syt);
1110        
1111         debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"ch%2u: CY=%4u, SYT=%08X (%3u secs + %4u cycles + %04u ticks)\n",
1112             channel, cycle,syt_timestamp, CYCLE_TIMER_GET_SECS(syt_timestamp),
1113             CYCLE_TIMER_GET_CYCLES(syt_timestamp), CYCLE_TIMER_GET_OFFSET(syt_timestamp));
1114        
1115         // reconstruct the full cycle
1116         unsigned int cc=m_handler->getCycleTimer();
1117         unsigned int cc_cycles=CYCLE_TIMER_GET_CYCLES(cc);
1118         unsigned int cc_seconds=CYCLE_TIMER_GET_SECS(cc);
1119        
1120         // the cycletimer has wrapped since this packet was received
1121         // we want cc_seconds to reflect the 'seconds' at the point this
1122         // was received
1123         if (cycle>cc_cycles) {
1124             if (cc_seconds) {
1125                 cc_seconds--;
1126             } else {
1127                 // seconds has wrapped around, so we'd better not substract 1
1128                 // the good value is 127
1129                 cc_seconds=127;
1130             }
1131         }
1132        
1133         // reconstruct the top part of the timestamp using the current cycle number
1134         unsigned int now_cycle_masked=cycle & 0xF;
1135         unsigned int syt_cycle=CYCLE_TIMER_GET_CYCLES(syt_timestamp);
1136        
1137         // if this is true, wraparound has occurred, undo this wraparound
1138         if(syt_cycle<now_cycle_masked) syt_cycle += 0x10;
1139        
1140         // this is the difference in cycles wrt the cycle the
1141         // timestamp was received
1142         unsigned int delta_cycles=syt_cycle-now_cycle_masked;
1143        
1144         // reconstruct the cycle part of the timestamp
1145         unsigned int new_cycles=cycle + delta_cycles;
1146        
1147         // if the cycles cause a wraparound of the cycle timer,
1148         // perform this wraparound
1149         // and convert the timestamp into ticks
1150         if(new_cycles<8000) {
1151             m_last_timestamp  = new_cycles * TICKS_PER_CYCLE;
1152         } else {
1153             debugOutput(DEBUG_LEVEL_VERY_VERBOSE,
1154                 "Detected wraparound: %d + %d = %d\n",
1155                 cycle,delta_cycles,new_cycles);
1156            
1157             new_cycles-=8000; // wrap around
1158             m_last_timestamp  = new_cycles * TICKS_PER_CYCLE;
1159             // add one second due to wraparound
1160             m_last_timestamp += TICKS_PER_SECOND;
1161         }
1162        
1163         m_last_timestamp += CYCLE_TIMER_GET_OFFSET(syt_timestamp);
1164         m_last_timestamp += cc_seconds * TICKS_PER_SECOND;
1165        
1166         // the receive processing delay indicates how much
1167         // extra time we need as slack
1168         m_last_timestamp += RECEIVE_PROCESSING_DELAY;
1169
1170         //=> now estimate the device frame rate
1171         if (m_last_timestamp2 && m_last_timestamp) {
1172             // try and estimate the frame rate from the device
1173            
1174             // first get the measured difference between both
1175             // timestamps
1176             int64_t measured_difference;
1177             measured_difference=((int64_t)(m_last_timestamp))
1178                                -((int64_t)(m_last_timestamp2));
1179             // correct for seconds wraparound
1180             if (m_last_timestamp<m_last_timestamp2) {
1181                 measured_difference+=128L*TICKS_PER_SECOND;
1182             }
1183
1184             // implement a 1st order DLL to estimate the framerate
1185             // this is the number of ticks between two samples
1186             float f=measured_difference;
1187             float err = f / (1.0*m_syt_interval) - m_ticks_per_frame;
1188            
1189             debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"SYT: %08X | STMP: %lluticks | DLL: in=%f, current=%f, err=%e\n",syt_timestamp, m_last_timestamp, f,m_ticks_per_frame,err);
1190
1191 #ifdef DEBUG
1192             // this helps to detect wraparound issues
1193             if(f > 1.5*((TICKS_PER_SECOND*1.0) / m_framerate)*m_syt_interval) {
1194                 debugWarning("Timestamp diff more than 50%% of the nominal diff too large!\n");
1195                 debugWarning(" SYT: %08X | STMP: %llu,%llu | DLL: in=%f, current=%f, err=%e\n",syt_timestamp, m_last_timestamp, m_last_timestamp2, f,m_ticks_per_frame,err);
1196                 debugWarning(" CC: %08X | CC_CY: %u | CC_SEC: %u | SYT_CY: %u | NEW_CY: %u\n",
1197                     cc, cc_cycles, cc_seconds, syt_cycle,new_cycles);
1198                
1199             }
1200             if(f < 0.5*((TICKS_PER_SECOND*1.0) / m_framerate)*m_syt_interval) {
1201                 debugWarning("Timestamp diff more than 50%% of the nominal diff too small!\n");
1202                 debugWarning(" SYT: %08X | STMP: %llu,%llu | DLL: in=%f, current=%f, err=%e\n",syt_timestamp, m_last_timestamp, m_last_timestamp2, f,m_ticks_per_frame,err);
1203             }
1204 #endif
1205             // integrate the error
1206             m_ticks_per_frame += RECEIVE_DLL_INTEGRATION_COEFFICIENT*err;
1207            
1208         }
1209        
1210          debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"R-SYT for cycle (%2d %2d)=>%2d: %5uT (%04uC + %04uT) %04X %04X %d\n",
1211             cycle,now_cycle_masked,delta_cycles,
1212             (m_last_timestamp),
1213             TICKS_TO_CYCLES(m_last_timestamp),
1214             TICKS_TO_OFFSET(m_last_timestamp),
1215             ntohs(packet->syt),TICKS_TO_CYCLE_TIMER(m_last_timestamp)&0xFFFF, dropped
1216          );
1217
1218         //=> signal that we're running (if we are)
1219         if(!m_running && nevents && m_last_timestamp2 && m_last_timestamp) m_running=true;
1220
1221         //=> don't process the stream samples when it is not enabled.
1222         if(m_disabled) {
1223
1224             // we keep track of the timestamp here
1225             // this makes sure that we will have a somewhat accurate
1226             // estimate as to when a period might be ready. i.e. it will not
1227             // be ready earlier than this timestamp + period time
1228            
1229             // the next (possible) sample is not this one, but lies
1230             // SYT_INTERVAL * rate later
1231             uint64_t ts=m_last_timestamp+(uint64_t)((float)m_syt_interval * m_ticks_per_frame);
1232            
1233             // wrap if nescessary
1234             if (ts >= TICKS_PER_SECOND * 128L) {
1235                 ts -= TICKS_PER_SECOND * 128L;
1236             }
1237             // set the timestamps
1238             StreamProcessor::setBufferTimestamps(ts,ts);
1239            
1240             // indicate that we are now in a disabled state.
1241             m_is_disabled=true;
1242            
1243             return RAW1394_ISO_DEFER;
1244         }
1245        
1246         // indicate that we are now in an enabled state.
1247         m_is_disabled=false;
1248        
1249         //=> process the packet
1250         unsigned int write_size=nevents*sizeof(quadlet_t)*m_dimension;
1251        
1252         // add the data payload to the ringbuffer
1253         if (freebob_ringbuffer_write(m_event_buffer,(char *)(data+8),write_size) < write_size)
1254         {
1255             debugWarning("Receive buffer overrun (cycle %d, FC=%d, PC=%d)\n",
1256                  cycle, getFrameCounter(), m_handler->getPacketCount());
1257            
1258             m_xruns++;
1259
1260             retval=RAW1394_ISO_DEFER;
1261         } else {
1262             retval=RAW1394_ISO_OK;
1263             // process all ports that should be handled on a per-packet base
1264             // this is MIDI for AMDTP (due to the need of DBC)
1265             if (!decodePacketPorts((quadlet_t *)(data+8), nevents, packet->dbc)) {
1266                 debugWarning("Problem decoding Packet Ports\n");
1267                 retval=RAW1394_ISO_DEFER;
1268             }
1269         }
1270
1271 #ifdef DEBUG
1272         if(packet->dbs) {
1273             debugOutput(DEBUG_LEVEL_VERY_VERBOSE,
1274                 "RCV %04d: CH = %d, FDF = %X. SYT = %6d, DBS = %3d, DBC = %3d, FMT = %3d, LEN = %4d (%2d)\n",
1275                 cycle, channel, packet->fdf,
1276                 packet->syt,
1277                 packet->dbs,
1278                 packet->dbc,
1279                 packet->fmt,
1280                 length,
1281                 ((length / sizeof (quadlet_t)) - 2)/packet->dbs);
1282         }
1283 #endif
1284
1285         // update the frame counter such that it reflects the new value,
1286         // and also update the buffer tail timestamp, as we add new frames
1287         // done in the SP base class
1288         if (!StreamProcessor::putFrames(nevents, m_last_timestamp)) {
1289             debugError("Could not do StreamProcessor::putFrames(%d, %llu)\n",nevents, m_last_timestamp);
1290             return RAW1394_ISO_ERROR;
1291         }
1292
1293     }
1294    
1295     return retval;
1296 }
1297
1298 int64_t AmdtpReceiveStreamProcessor::getTimeUntilNextPeriodUsecs() {
1299     uint64_t time_at_period=getTimeAtPeriod();
1300    
1301     uint64_t cycle_timer=m_handler->getCycleTimerTicks();
1302    
1303     int64_t until_next=time_at_period-cycle_timer;
1304    
1305     // the maximal difference we can allow (64secs)
1306     const int64_t max=TICKS_PER_SECOND*64L;
1307    
1308     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "=> TAP=%11llu, CTR=%11llu, UTN=%11lld, TPUS=%f\n",
1309         time_at_period, cycle_timer, until_next, m_handler->getTicksPerUsec()
1310         );
1311        
1312     if(until_next > max) {
1313         // this means that cycle_timer has wrapped, but
1314         // time_at_period has not. we should unwrap cycle_timer
1315         // by adding TICKS_PER_SECOND*128L, meaning that we should substract
1316         // this value from until_next           
1317         until_next -= TICKS_PER_SECOND*128L;
1318     } else if (until_next < -max) {
1319         // this means that time_at_period has wrapped, but
1320         // cycle_timer has not. we should unwrap time_at_period
1321         // by adding TICKS_PER_SECOND*128L, meaning that we should add
1322         // this value from until_next
1323         until_next += TICKS_PER_SECOND*128L;
1324     }
1325    
1326     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "   TAP=%11llu, CTR=%11llu, UTN=%11lld, TPUS=%f\n",
1327         time_at_period, cycle_timer, until_next, m_handler->getTicksPerUsec()
1328         );
1329    
1330     // now convert to usecs
1331     // don't use the mapping function because it only works
1332     // for absolute times, not the relative time we are
1333     // using here (which can also be negative).
1334     return (int64_t)(((float)until_next) / m_handler->getTicksPerUsec());
1335 }
1336
1337 uint64_t AmdtpReceiveStreamProcessor::getTimeAtPeriodUsecs() {
1338     // then we should convert this into usecs
1339     // FIXME: we assume that the TimeSource of the IsoHandler is
1340     //        in usecs.
1341     return m_handler->mapToTimeSource(getTimeAtPeriod());
1342 }
1343
1344 uint64_t AmdtpReceiveStreamProcessor::getTimeAtPeriod() {
1345
1346     // every time a packet is received both the framecounter and the base
1347     // timestamp are updated. This means that at any instance of time, the
1348     // front of the buffer (latest sample) timestamp is known.
1349     // As we know the number of frames in the buffer, and we now the rate
1350     // in ticks/frame, we can calculate the back of buffer timestamp as:
1351     //    back_of_buffer_time = front_time - nbframes * rate
1352     // the next period boundary time lies m_period frames later:
1353     //    next_period_boundary = back_of_buffer_time + m_period * rate
1354    
1355     // NOTE: we should account for the fact that the timestamp is not for
1356     //       the latest sample, but for the latest sample minus syt_interval-1
1357     //       because it is the timestamp for the first sample in the packet,
1358     //       while the complete packet contains SYT_INTERVAL samples.
1359     //       this makes the equation:
1360     //          back_of_buffer_time = front_time - (nbframes - (syt_interval - 1)) * rate
1361     //          next_period_boundary = back_of_buffer_time + m_period * rate
1362
1363     // NOTE: where do we add the processing delay?
1364     //       if we add it here:
1365     //          next_period_boundary += RECEIVE_PROCESSING_DELAY
1366    
1367     // the complete equation now is:
1368     // next_period_boundary = front_time - (nbframes - (syt_interval - 1)) * rate
1369     //                        + m_period * rate + RECEIVE_PROCESSING_DELAY
1370     // since syt_interval is a constant value, we can equally well ignore it, as
1371     // if it were already included in RECEIVE_PROCESSING_DELAY
1372     // making the equation (simplified:
1373     // next_period_boundary = front_time + (-nbframes + m_period) * rate
1374     //                        + RECEIVE_PROCESSING_DELAY
1375     // currently this is in ticks
1376    
1377     int64_t fc=getFrameCounter();
1378    
1379     int64_t next_period_boundary =  m_last_timestamp;
1380     next_period_boundary     += (int64_t)(((int64_t)m_period
1381                                           - fc) * m_ticks_per_frame);
1382 //     next_period_boundary     += RECEIVE_PROCESSING_DELAY;
1383    
1384     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "=> NPD=%11lld, LTS=%11llu, FC=%5d, TPF=%f\n",
1385         next_period_boundary, m_last_timestamp, fc, m_ticks_per_frame
1386         );
1387    
1388     // this happens if the timestamp wraps around while there are a lot of
1389     // frames in the buffer. We should add the wraparound value of the ticks
1390     // counter
1391     if (next_period_boundary < 0) {
1392         next_period_boundary += TICKS_PER_SECOND * 128L;
1393     }
1394     // this happens when the last timestamp is near wrapping, and
1395     // m_framecounter is low.
1396     // this means: m_last_timestamp is near wrapping and have just had
1397     // a getPackets() from the client side. the projected next_period
1398     // boundary lies beyond the wrap value.
1399     // the action is to wrap the value.
1400     else if (next_period_boundary >= TICKS_PER_SECOND * 128L) {
1401         next_period_boundary -= TICKS_PER_SECOND * 128L;
1402     }
1403    
1404     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "   NPD=%11lld, LTS=%11llu, FC=%5d, TPF=%f\n",
1405         next_period_boundary, m_last_timestamp, fc, m_ticks_per_frame
1406         );
1407
1408     return next_period_boundary;
1409 }
1410
1411 void AmdtpReceiveStreamProcessor::dumpInfo()
1412 {
1413
1414     StreamProcessor::dumpInfo();
1415    
1416         debugOutputShort( DEBUG_LEVEL_NORMAL, "  Device framerate  : %f\n", 24576000.0/m_ticks_per_frame);
1417
1418 }
1419
1420
1421 void AmdtpReceiveStreamProcessor::setVerboseLevel(int l) {
1422         setDebugLevel(l);
1423         ReceiveStreamProcessor::setVerboseLevel(l);
1424
1425 }
1426
1427
1428 bool AmdtpReceiveStreamProcessor::reset() {
1429
1430     debugOutput( DEBUG_LEVEL_VERBOSE, "Resetting...\n");
1431
1432     // reset the event buffer, discard all content
1433     freebob_ringbuffer_reset(m_event_buffer);
1434    
1435     m_PeriodStat.reset();
1436     m_PacketStat.reset();
1437     m_WakeupStat.reset();
1438    
1439 //     m_ticks_per_frame = (TICKS_PER_SECOND*1.0) / ((float)m_framerate);
1440
1441     // reset all non-device specific stuff
1442     // i.e. the iso stream and the associated ports
1443     if(!ReceiveStreamProcessor::reset()) {
1444             debugFatal("Could not do base class reset\n");
1445             return false;
1446     }
1447     return true;
1448 }
1449
1450 bool AmdtpReceiveStreamProcessor::prepare() {
1451
1452     m_PeriodStat.setName("RCV PERIOD");
1453     m_PacketStat.setName("RCV PACKET");
1454     m_WakeupStat.setName("RCV WAKEUP");
1455
1456         // prepare all non-device specific stuff
1457         // i.e. the iso stream and the associated ports
1458         if(!ReceiveStreamProcessor::prepare()) {
1459                 debugFatal("Could not prepare base class\n");
1460                 return false;
1461         }
1462        
1463         debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing...\n");
1464         switch (m_framerate) {
1465         case 32000:
1466                 m_syt_interval = 8;
1467                 break;
1468         case 44100:
1469                 m_syt_interval = 8;
1470                 break;
1471         default:
1472         case 48000:
1473                 m_syt_interval = 8;
1474                 break;
1475         case 88200:
1476                 m_syt_interval = 16;
1477                 break;
1478         case 96000:
1479                 m_syt_interval = 16;
1480                 break;
1481         case 176400:
1482                 m_syt_interval = 32;
1483                 break;
1484         case 192000:
1485                 m_syt_interval = 32;
1486                 break;
1487         }
1488
1489     // prepare the framerate estimate
1490     m_ticks_per_frame = (TICKS_PER_SECOND*1.0) / ((float)m_framerate);
1491
1492     debugOutput(DEBUG_LEVEL_VERBOSE,"Initializing remote ticks/frame to %f\n",m_ticks_per_frame);
1493
1494     // allocate the event buffer
1495     unsigned int ringbuffer_size_frames=m_nb_buffers * m_period;
1496    
1497     // add the processing delay
1498     debugOutput(DEBUG_LEVEL_VERBOSE,"Adding %u frames of SYT slack buffering...\n",
1499         (uint)(RECEIVE_PROCESSING_DELAY/m_ticks_per_frame));
1500     ringbuffer_size_frames+=(uint)(RECEIVE_PROCESSING_DELAY/m_ticks_per_frame);
1501    
1502     if( !(m_event_buffer=freebob_ringbuffer_create(
1503             (m_dimension * ringbuffer_size_frames) * sizeof(quadlet_t)))) {
1504                 debugFatal("Could not allocate memory event ringbuffer");
1505                 return false;
1506         }
1507
1508         // allocate the temporary cluster buffer
1509         if( !(m_cluster_buffer=(char *)calloc(m_dimension,sizeof(quadlet_t)))) {
1510                 debugFatal("Could not allocate temporary cluster buffer");
1511                 freebob_ringbuffer_free(m_event_buffer);
1512                 return false;
1513         }
1514
1515         // set the parameters of ports we can:
1516         // we want the audio ports to be period buffered,
1517         // and the midi ports to be packet buffered
1518         for ( PortVectorIterator it = m_Ports.begin();
1519                   it != m_Ports.end();
1520                   ++it )
1521         {
1522                 debugOutput(DEBUG_LEVEL_VERBOSE, "Setting up port %s\n",(*it)->getName().c_str());
1523                 if(!(*it)->setBufferSize(m_period)) {
1524                         debugFatal("Could not set buffer size to %d\n",m_period);
1525                         return false;
1526                 }
1527
1528                 switch ((*it)->getPortType()) {
1529                         case Port::E_Audio:
1530                                 if(!(*it)->setSignalType(Port::E_PeriodSignalled)) {
1531                                         debugFatal("Could not set signal type to PeriodSignalling");
1532                                         return false;
1533                                 }
1534                                 // buffertype and datatype are dependant on the API
1535                                 debugWarning("---------------- ! Doing hardcoded dummy setup ! --------------\n");
1536                                 // buffertype and datatype are dependant on the API
1537                                 if(!(*it)->setBufferType(Port::E_PointerBuffer)) {
1538                                         debugFatal("Could not set buffer type");
1539                                         return false;
1540                                 }
1541                                 if(!(*it)->useExternalBuffer(true)) {
1542                                         debugFatal("Could not set external buffer usage");
1543                                         return false;
1544                                 }
1545                                 if(!(*it)->setDataType(Port::E_Float)) {
1546                                         debugFatal("Could not set data type");
1547                                         return false;
1548                                 }
1549                                 break;
1550                         case Port::E_Midi:
1551                                 if(!(*it)->setSignalType(Port::E_PacketSignalled)) {
1552                                         debugFatal("Could not set signal type to PacketSignalling");
1553                                         return false;
1554                                 }
1555                                 // buffertype and datatype are dependant on the API
1556                                 // buffertype and datatype are dependant on the API
1557                                 debugWarning("---------------- ! Doing hardcoded test setup ! --------------\n");
1558                                 // buffertype and datatype are dependant on the API
1559                                 if(!(*it)->setBufferType(Port::E_RingBuffer)) {
1560                                         debugFatal("Could not set buffer type");
1561                                         return false;
1562                                 }
1563                                 if(!(*it)->setDataType(Port::E_MidiEvent)) {
1564                                         debugFatal("Could not set data type");
1565                                         return false;
1566                                 }
1567                                 break;
1568                         default:
1569                                 debugWarning("Unsupported port type specified\n");
1570                                 break;
1571                 }
1572
1573         }
1574
1575         // the API specific settings of the ports should already be set,
1576         // as this is called from the processorManager->prepare()
1577         // so we can init the ports
1578         if(!initPorts()) {
1579                 debugFatal("Could not initialize ports!\n");
1580                 return false;
1581         }
1582
1583         if(!preparePorts()) {
1584                 debugFatal("Could not initialize ports!\n");
1585                 return false;
1586         }
1587
1588
1589         debugOutput( DEBUG_LEVEL_VERBOSE, "Prepared for:\n");
1590         debugOutput( DEBUG_LEVEL_VERBOSE, " Samplerate: %d, DBS: %d, SYT: %d\n",
1591                      m_framerate,m_dimension,m_syt_interval);
1592         debugOutput( DEBUG_LEVEL_VERBOSE, " PeriodSize: %d, NbBuffers: %d\n",
1593                      m_period,m_nb_buffers);
1594         debugOutput( DEBUG_LEVEL_VERBOSE, " Port: %d, Channel: %d\n",
1595                      m_port,m_channel);
1596         return true;
1597
1598 }
1599
1600 bool AmdtpReceiveStreamProcessor::prepareForStart() {
1601     disable();
1602     return true;
1603 }
1604
1605 bool AmdtpReceiveStreamProcessor::prepareForStop() {
1606     disable();
1607     return true;
1608 }
1609
1610 bool AmdtpReceiveStreamProcessor::canClientTransferFrames(unsigned int nbframes) {
1611     return getFrameCounter() >= (int) nbframes;
1612 }
1613
1614 bool AmdtpReceiveStreamProcessor::getFrames(unsigned int nbframes, int64_t ts) {
1615
1616     m_PeriodStat.mark(freebob_ringbuffer_read_space(m_event_buffer)/(4*m_dimension));
1617
1618         debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Transferring period...\n");
1619        
1620         int xrun;
1621         unsigned int offset=0;
1622        
1623         freebob_ringbuffer_data_t vec[2];
1624         // we received one period of frames on each connection
1625         // this is period_size*dimension of events
1626
1627         unsigned int events2read=nbframes*m_dimension;
1628         unsigned int bytes2read=events2read*sizeof(quadlet_t);
1629         /* read events2read bytes from the ringbuffer
1630         *  first see if it can be done in one read.
1631         *  if so, ok.
1632         *  otherwise read up to a multiple of clusters directly from the buffer
1633         *  then do the buffer wrap around using ringbuffer_read
1634         *  then read the remaining data directly from the buffer in a third pass
1635         *  Make sure that we cannot end up on a non-cluster aligned position!
1636         */
1637         unsigned int cluster_size=m_dimension*sizeof(quadlet_t);
1638        
1639         while(bytes2read>0) {
1640                 unsigned int framesread=(nbframes*cluster_size-bytes2read)/cluster_size;
1641                 offset=framesread;
1642                
1643                 int bytesread=0;
1644
1645                 freebob_ringbuffer_get_read_vector(m_event_buffer, vec);
1646                        
1647                 if(vec[0].len==0) { // this indicates an empty event buffer
1648                         debugError("RCV: Event buffer underrun in processor %p\n",this);
1649                         break;
1650                 }
1651                        
1652                 /* if we don't take care we will get stuck in an infinite loop
1653                 * because we align to a cluster boundary later
1654                 * the remaining nb of bytes in one read operation can be smaller than one cluster
1655                 * this can happen because the ringbuffer size is always a power of 2
1656                         */
1657                 if(vec[0].len<cluster_size) {
1658                         // use the ringbuffer function to read one cluster
1659                         // the read function handles wrap around
1660                         freebob_ringbuffer_read(m_event_buffer,m_cluster_buffer,cluster_size);
1661
1662                         xrun = receiveBlock(m_cluster_buffer, 1, offset);
1663                                
1664                         if(xrun<0) {
1665                                 // xrun detected
1666                                 debugError("RCV: Frame buffer overrun in processor %p\n",this);
1667                                 break;
1668                         }
1669                                
1670                                 // we advanced one cluster_size
1671                         bytes2read-=cluster_size;
1672                                
1673                 } else { //
1674                        
1675                         if(bytes2read>vec[0].len) {
1676                                         // align to a cluster boundary
1677                                 bytesread=vec[0].len-(vec[0].len%cluster_size);
1678                         } else {
1679                                 bytesread=bytes2read;
1680                         }
1681                                
1682                         xrun = receiveBlock(vec[0].buf, bytesread/cluster_size, offset);
1683                                
1684                         if(xrun<0) {
1685                                         // xrun detected
1686                                 debugError("RCV: Frame buffer overrun in processor %p\n",this);
1687                                 break;
1688                         }
1689
1690                         freebob_ringbuffer_read_advance(m_event_buffer, bytesread);
1691                         bytes2read -= bytesread;
1692                 }
1693                        
1694                 // the bytes2read should always be cluster aligned
1695                 assert(bytes2read%cluster_size==0);
1696         }
1697        
1698     // update the frame counter such that it reflects the new value,
1699     // and also update the buffer head timestamp as we pull frames
1700     // done in the SP base class
1701
1702     // wrap the timestamp if nescessary
1703     if (ts < 0) {
1704         ts += TICKS_PER_SECOND * 128L;
1705     } else if (ts >= TICKS_PER_SECOND * 128L) {
1706         ts -= TICKS_PER_SECOND * 128L;
1707     }
1708    
1709     if (!StreamProcessor::getFrames(nbframes, ts)) {
1710         debugError("Could not do StreamProcessor::getFrames(%d, %llu)\n", nbframes, ts);
1711         return false;
1712     }
1713    
1714     return true;
1715 }
1716
1717 /**
1718  * \brief write received events to the stream ringbuffers.
1719  */
1720 int AmdtpReceiveStreamProcessor::receiveBlock(char *data,
1721                                            unsigned int nevents, unsigned int offset)
1722 {
1723         int problem=0;
1724
1725         for ( PortVectorIterator it = m_PeriodPorts.begin();
1726           it != m_PeriodPorts.end();
1727           ++it )
1728     {
1729
1730         if((*it)->isDisabled()) {continue;};
1731
1732                 //FIXME: make this into a static_cast when not DEBUG?
1733
1734                 AmdtpPortInfo *pinfo=dynamic_cast<AmdtpPortInfo *>(*it);
1735                 assert(pinfo); // this should not fail!!
1736
1737                 switch(pinfo->getFormat()) {
1738                 case AmdtpPortInfo::E_MBLA:
1739                         if(decodeMBLAEventsToPort(static_cast<AmdtpAudioPort *>(*it), (quadlet_t *)data, offset, nevents)) {
1740                                 debugWarning("Could not decode packet MBLA to port %s",(*it)->getName().c_str());
1741                                 problem=1;
1742                         }
1743                         break;
1744                 case AmdtpPortInfo::E_SPDIF: // still unimplemented
1745                         break;
1746         /* for this processor, midi is a packet based port
1747                 case AmdtpPortInfo::E_Midi:
1748                         break;*/
1749                 default: // ignore
1750                         break;
1751                 }
1752     }
1753         return problem;
1754
1755 }
1756
1757 /**
1758  * @brief decode a packet for the packet-based ports
1759  *
1760  * @param data Packet data
1761  * @param nevents number of events in data (including events of other ports & port types)
1762  * @param dbc DataBlockCount value for this packet
1763  * @return true if all successfull
1764  */
1765 bool AmdtpReceiveStreamProcessor::decodePacketPorts(quadlet_t *data, unsigned int nevents, unsigned int dbc)
1766 {
1767         bool ok=true;
1768        
1769         quadlet_t *target_event=NULL;
1770         unsigned int j;
1771        
1772         for ( PortVectorIterator it = m_PacketPorts.begin();
1773           it != m_PacketPorts.end();
1774           ++it )
1775         {
1776
1777 #ifdef DEBUG
1778                 AmdtpPortInfo *pinfo=dynamic_cast<AmdtpPortInfo *>(*it);
1779                 assert(pinfo); // this should not fail!!
1780
1781                 // the only packet type of events for AMDTP is MIDI in mbla
1782                 assert(pinfo->getFormat()==AmdtpPortInfo::E_Midi);
1783 #endif
1784                 AmdtpMidiPort *mp=static_cast<AmdtpMidiPort *>(*it);
1785                
1786                 // we decode this directly (no function call) due to the high frequency
1787                 /* idea:
1788                 spec says: current_midi_port=(dbc+j)%8;
1789                 => if we start at (dbc+stream->location-1)%8 [due to location_min=1],
1790                 we'll start at the right event for the midi port.
1791                 => if we increment j with 8, we stay at the right event.
1792                 */
1793                 // FIXME: as we know in advance how big a packet is (syt_interval) we can
1794                 //        predict how much loops will be present here
1795                 for(j = (dbc & 0x07)+mp->getLocation()-1; j < nevents; j += 8) {
1796                         target_event=(quadlet_t *)(data + ((j * m_dimension) + mp->getPosition()));
1797                         quadlet_t sample_int=ntohl(*target_event);
1798                         // FIXME: this assumes that 2X and 3X speed isn't used,
1799                         // because only the 1X slot is put into the ringbuffer
1800                         if(IEC61883_AM824_GET_LABEL(sample_int) != IEC61883_AM824_LABEL_MIDI_NO_DATA) {
1801                                 sample_int=(sample_int >> 16) & 0x000000FF;
1802                                 if(!mp->writeEvent(&sample_int)) {
1803                                         debugWarning("Packet port events lost\n");
1804                                         ok=false;
1805                                 }
1806                         }
1807                 }
1808
1809         }
1810        
1811         return ok;
1812 }
1813
1814 int AmdtpReceiveStreamProcessor::decodeMBLAEventsToPort(AmdtpAudioPort *p, quadlet_t *data,
1815                                            unsigned int offset, unsigned int nevents)
1816 {
1817         unsigned int j=0;
1818
1819 //      printf("****************\n");
1820 //      hexDumpQuadlets(data,m_dimension*4);
1821 //      printf("****************\n");
1822
1823         quadlet_t *target_event;
1824
1825         target_event=(quadlet_t *)(data + p->getPosition());
1826
1827         switch(p->getDataType()) {
1828                 default:
1829                 case Port::E_Int24:
1830                         {
1831                                 quadlet_t *buffer=(quadlet_t *)(p->getBufferAddress());
1832
1833                                 assert(nevents + offset <= p->getBufferSize());
1834
1835                                 buffer+=offset;
1836
1837                                 for(j = 0; j < nevents; j += 1) { // decode max nsamples
1838                                         *(buffer)=(ntohl((*target_event) ) & 0x00FFFFFF);
1839                                         buffer++;
1840                                         target_event+=m_dimension;
1841                                 }
1842                         }
1843                         break;
1844                 case Port::E_Float:
1845                         {
1846                                 const float multiplier = 1.0f / (float)(0x7FFFFF);
1847                                 float *buffer=(float *)(p->getBufferAddress());
1848
1849                                 assert(nevents + offset <= p->getBufferSize());
1850
1851                                 buffer+=offset;
1852
1853                                 for(j = 0; j < nevents; j += 1) { // decode max nsamples               
1854        
1855                                         unsigned int v = ntohl(*target_event) & 0x00FFFFFF;
1856                                         // sign-extend highest bit of 24-bit int
1857                                         int tmp = (int)(v << 8) / 256;
1858                
1859                                         *buffer = tmp * multiplier;
1860                                
1861                                         buffer++;
1862                                         target_event+=m_dimension;
1863                                 }
1864                         }
1865                         break;
1866         }
1867
1868         return 0;
1869 }
1870
1871 } // end of namespace FreebobStreaming
Note: See TracBrowser for help on using the browser.