root/branches/api-cleanup/src/libutil/TimestampedBuffer.cpp

Revision 813, 41.4 kB (checked in by ppalmers, 14 years ago)

small simplification

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 program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include "config.h"
25
26 #include "libutil/Atomic.h"
27 #include "libieee1394/cycletimer.h"
28
29 #include "TimestampedBuffer.h"
30 #include "assert.h"
31 #include "errno.h"
32
33
34 #define DLL_PI        (3.141592653589793238)
35 #define DLL_SQRT2     (1.414213562373095049)
36 #define DLL_OMEGA     (2.0*DLL_PI*TIMESTAMPEDBUFFER_DLL_BANDWIDTH)
37 #define DLL_COEFF_B   (DLL_SQRT2 * DLL_OMEGA)
38 #define DLL_COEFF_C   (DLL_OMEGA * DLL_OMEGA)
39
40 /*
41 #define ENTER_CRITICAL_SECTION { \
42     if (pthread_mutex_trylock(&m_framecounter_lock) == EBUSY) { \
43         debugWarning(" (%p) lock clash\n", this); \
44         pthread_mutex_lock(&m_framecounter_lock); \
45     } \
46     }
47 */
48 #define ENTER_CRITICAL_SECTION { \
49     pthread_mutex_lock(&m_framecounter_lock); \
50     }
51 #define EXIT_CRITICAL_SECTION { \
52     pthread_mutex_unlock(&m_framecounter_lock); \
53     }
54
55 namespace Util {
56
57 IMPL_DEBUG_MODULE( TimestampedBuffer, TimestampedBuffer, DEBUG_LEVEL_VERBOSE );
58
59 TimestampedBuffer::TimestampedBuffer(TimestampedBufferClient *c)
60     : m_event_buffer(NULL), m_cluster_buffer(NULL),
61       m_event_size(0), m_events_per_frame(0), m_buffer_size(0),
62       m_bytes_per_frame(0), m_bytes_per_buffer(0),
63       m_enabled( false ), m_transparent ( true ),
64       m_wrap_at(0xFFFFFFFFFFFFFFFFLLU),
65       m_Client(c), m_framecounter(0),
66       m_tick_offset(0.0),
67       m_buffer_tail_timestamp(0.0),
68       m_buffer_next_tail_timestamp(0.0),
69       m_dll_e2(0.0), m_dll_b(DLL_COEFF_B), m_dll_c(DLL_COEFF_C),
70       m_nominal_rate(0.0), m_current_rate(0.0), m_update_period(0)
71 {
72     pthread_mutex_init(&m_framecounter_lock, NULL);
73 }
74
75 TimestampedBuffer::~TimestampedBuffer() {
76     ffado_ringbuffer_free(m_event_buffer);
77     free(m_cluster_buffer);
78 }
79
80 /**
81  * \brief Set the nominal rate in frames/timeunit
82  *
83  * Sets the nominal rate in frames per time unit. This rate is used
84  * to initialize the DLL that will extract the effective rate based
85  * upon the timestamps it gets fed.
86  *
87  * @param r rate
88  * @return true if successful
89  */
90 bool TimestampedBuffer::setNominalRate(float r) {
91     m_nominal_rate=r;
92     debugOutput(DEBUG_LEVEL_VERBOSE," nominal rate=%e set to %e\n",
93                                     m_nominal_rate, r);
94     return true;
95 }
96
97 /**
98  * \brief Set the nominal update period (in frames)
99  *
100  * Sets the nominal update period. This period is the number of frames
101  * between two timestamp updates (hence buffer writes)
102  *
103  * @param n period in frames
104  * @return true if successful
105  */
106 bool TimestampedBuffer::setUpdatePeriod(unsigned int n) {
107     m_update_period=n;
108     return true;
109 }
110
111 /**
112  * \brief Get the nominal update period (in frames)
113  *
114  * Gets the nominal update period. This period is the number of frames
115  * between two timestamp updates (hence buffer writes)
116  *
117  * @return period in frames
118  */
119 unsigned int TimestampedBuffer::getUpdatePeriod() {
120     return m_update_period;
121 }
122
123 /**
124  * \brief set the value at which timestamps should wrap around
125  * @param w value to wrap at
126  * @return true if successful
127  */
128 bool TimestampedBuffer::setWrapValue(ffado_timestamp_t w) {
129     m_wrap_at=w;
130     return true;
131 }
132 #include <math.h>
133
134 /**
135  * \brief return the effective rate
136  *
137  * Returns the effective rate calculated by the DLL.
138  *
139  * @return rate (in timeunits/frame)
140  */
141 float TimestampedBuffer::getRate() {
142     return m_current_rate;
143 }
144
145 /**
146  * \brief calculate the effective rate
147  *
148  * Returns the effective rate calculated by the DLL.
149  * @note should be called with the lock held
150  * @return rate (in timeunits/frame)
151  */
152 float TimestampedBuffer::calculateRate() {
153     ffado_timestamp_t diff;
154    
155     diff=m_buffer_next_tail_timestamp - m_buffer_tail_timestamp;
156    
157     debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"getRate: %f/%f=%f\n",
158         (float)(diff),
159         (float)m_update_period,
160         ((float)(diff))/((float) m_update_period));
161    
162     // the maximal difference we can allow (64secs)
163     const ffado_timestamp_t max=m_wrap_at/((ffado_timestamp_t)2);
164
165     if(diff > max) {
166         diff -= m_wrap_at;
167     } else if (diff < -max) {
168         diff += m_wrap_at;
169     }
170
171     float rate=((float)diff)/((float) m_update_period);
172     if (rate<0.0) debugError("rate < 0! (%f)\n",rate);
173     if (fabsf(m_nominal_rate - rate)>(m_nominal_rate*0.1)) {
174         debugWarning("(%p) rate (%10.5f) more that 10%% off nominal (rate=%10.5f, diff="TIMESTAMP_FORMAT_SPEC", update_period=%d)\n",
175                      this, rate,m_nominal_rate,diff, m_update_period);
176
177         return m_nominal_rate;
178     } else {
179         return rate;
180     }
181 }
182
183 /**
184  * \brief Sets the size of the events
185  * @param s event size in bytes
186  * @return true if successful
187  */
188 bool TimestampedBuffer::setEventSize(unsigned int s) {
189     m_event_size=s;
190
191     m_bytes_per_frame=m_event_size*m_events_per_frame;
192     m_bytes_per_buffer=m_bytes_per_frame*m_buffer_size;
193
194     return true;
195 }
196
197 /**
198  * \brief Sets the number of events per frame
199  * @param n number of events per frame
200  * @return true if successful
201  */
202 bool TimestampedBuffer::setEventsPerFrame(unsigned int n) {
203     m_events_per_frame=n;
204
205     m_bytes_per_frame=m_event_size*m_events_per_frame;
206     m_bytes_per_buffer=m_bytes_per_frame*m_buffer_size;
207
208     return true;
209 }
210 /**
211  * \brief Sets the buffer size in frames
212  * @param n number frames
213  * @return true if successful
214  */
215 bool TimestampedBuffer::setBufferSize(unsigned int n) {
216     m_buffer_size=n;
217
218     m_bytes_per_frame=m_event_size*m_events_per_frame;
219     m_bytes_per_buffer=m_bytes_per_frame*m_buffer_size;
220
221     return true;
222 }
223
224 /**
225  * Sets the buffer offset in ticks.
226  *
227  * A positive value means that the buffer is 'delayed' for nticks ticks.
228  *
229  * @note These offsets are only used when reading timestamps. Any function
230  *       that returns a timestamp will incorporate this offset.
231  * @param nframes the number of ticks (positive = delay buffer)
232  * @return true if successful
233  */
234 bool TimestampedBuffer::setTickOffset(ffado_timestamp_t nticks) {
235     debugOutput(DEBUG_LEVEL_VERBOSE,"Setting ticks offset to "TIMESTAMP_FORMAT_SPEC"\n",nticks);
236
237     // JMW: I think we need to update the internal DLL state to take account
238     // of the new offset.  Doing so certainly makes for a smoother MOTU
239     // startup.
240     ENTER_CRITICAL_SECTION;
241     m_buffer_tail_timestamp = m_buffer_tail_timestamp - m_tick_offset + nticks;
242     m_buffer_next_tail_timestamp = (ffado_timestamp_t)((double)m_buffer_tail_timestamp + m_dll_e2);
243     m_tick_offset=nticks;
244     EXIT_CRITICAL_SECTION;
245
246     return true;
247 }
248
249 /**
250  * \brief Returns the current fill of the buffer
251  *
252  * This returns the buffer fill of the internal ringbuffer. This
253  * can only be used as an indication because it's state is not
254  * guaranteed to be consistent at all times due to threading issues.
255  *
256  * In order to get the number of frames in the buffer, use the
257  * getBufferHeadTimestamp, getBufferTailTimestamp
258  * functions
259  *
260  * @return the internal buffer fill in frames
261  */
262 unsigned int TimestampedBuffer::getBufferFill() {
263     return ffado_ringbuffer_read_space(m_event_buffer)/(m_bytes_per_frame);
264 }
265
266 /**
267  * \brief Returns the current write space in the buffer
268  *
269  * This returns the buffer free space of the internal ringbuffer. This
270  * can only be used as an indication because it's state is not
271  * guaranteed to be consistent at all times due to threading issues.
272  *
273  * @return the internal buffer fill in frames
274  */
275 unsigned int TimestampedBuffer::getBufferSpace() {
276     return ffado_ringbuffer_write_space(m_event_buffer)/(m_bytes_per_frame);
277 }
278
279 /**
280  * \brief Resets the TimestampedBuffer
281  *
282  * Resets the TimestampedBuffer, clearing the buffers and counters.
283  * (not true yet: Also resets the DLL to the nominal values.)
284  *
285  * \note when this is called, you should make sure that the buffer
286  *       tail timestamp gets set before continuing
287  *
288  * @return true if successful
289  */
290 bool TimestampedBuffer::clearBuffer() {
291     debugOutput(DEBUG_LEVEL_VERBOSE, "Clearing buffer\n");
292     ffado_ringbuffer_reset(m_event_buffer);
293     resetFrameCounter();
294     return true;
295 }
296
297 /**
298  * \brief Prepares the TimestampedBuffer
299  *
300  * Prepare the TimestampedBuffer. This allocates all internal buffers and
301  * initializes all data structures.
302  *
303  * This should be called after parameters such as buffer size, event size etc.. are set,
304  * and before any read/write operations are performed.
305  *
306  * @return true if successful
307  */
308 bool TimestampedBuffer::prepare() {
309     debugOutput(DEBUG_LEVEL_VERBOSE,"Preparing buffer (%p)\n",this);
310     debugOutput(DEBUG_LEVEL_VERBOSE," Size=%u events, events/frame=%u, event size=%ubytes\n",
311                                         m_buffer_size,m_events_per_frame,m_event_size);
312
313     debugOutput(DEBUG_LEVEL_VERBOSE," update period %u\n",
314                                     m_update_period);
315     debugOutput(DEBUG_LEVEL_VERBOSE," nominal rate=%f\n",
316                                     m_nominal_rate);
317
318     debugOutput(DEBUG_LEVEL_VERBOSE," wrapping at "TIMESTAMP_FORMAT_SPEC"\n",m_wrap_at);
319
320     assert(m_buffer_size);
321     assert(m_events_per_frame);
322     assert(m_event_size);
323
324     assert(m_nominal_rate != 0.0L);
325     assert(m_update_period != 0);
326
327     m_current_rate = m_nominal_rate;
328
329     if( !(m_event_buffer=ffado_ringbuffer_create(
330             (m_events_per_frame * m_buffer_size) * m_event_size))) {
331         debugFatal("Could not allocate memory event ringbuffer\n");
332         return false;
333     }
334
335     // allocate the temporary cluster buffer
336     if( !(m_cluster_buffer=(char *)calloc(m_events_per_frame,m_event_size))) {
337             debugFatal("Could not allocate temporary cluster buffer\n");
338         ffado_ringbuffer_free(m_event_buffer);
339         return false;
340     }
341
342     // init the DLL
343     m_dll_e2=m_nominal_rate * (float)m_update_period;
344
345     m_dll_b=((float)(DLL_COEFF_B));
346     m_dll_c=((float)(DLL_COEFF_C));
347    
348     // this will init the internal timestamps to a sensible value
349     setBufferTailTimestamp(m_buffer_tail_timestamp);
350    
351     return true;
352 }
353
354 /**
355  * @brief Insert a dummy frame to the head buffer
356  *
357  * Writes one frame of dummy data to the head of the buffer.
358  * This is to assist the phase sync of several buffers.
359  *
360  * Note: currently the dummy data is added to the tail of the
361  *       buffer, but without updating the timestamp.
362  *
363  * @return true if successful
364  */
365 bool TimestampedBuffer::writeDummyFrame() {
366
367     unsigned int write_size=m_event_size*m_events_per_frame;
368    
369     char dummy[write_size]; // one frame of garbage
370     memset(dummy,0,write_size);
371
372     // add the data payload to the ringbuffer
373     if (ffado_ringbuffer_write(m_event_buffer,dummy,write_size) < write_size)
374     {
375 //         debugWarning("writeFrames buffer overrun\n");
376         return false;
377     }
378
379 //     incrementFrameCounter(nframes,ts);
380    
381     // increment without updating the DLL
382     ENTER_CRITICAL_SECTION;
383     m_framecounter++;
384     EXIT_CRITICAL_SECTION;
385     return true;
386 }
387
388 /**
389  * @brief Write frames to the buffer
390  *
391  * Copies \ref nframes of frames from the buffer pointed to by \ref data to the
392  * internal ringbuffer. The time of the last frame in the buffer is set to \ref ts.
393  *
394  * @param nframes number of frames to copy
395  * @param data pointer to the frame buffer
396  * @param ts timestamp of the last frame copied
397  * @return true if successful
398  */
399 bool TimestampedBuffer::writeFrames(unsigned int nframes, char *data, ffado_timestamp_t ts) {
400
401     unsigned int write_size=nframes*m_event_size*m_events_per_frame;
402
403     if (m_transparent) {
404         // while disabled, we don't update the DLL, nor do we write frames
405         // we just set the correct timestamp for the frames
406         setBufferTailTimestamp(ts);
407     } else {
408         // add the data payload to the ringbuffer
409         size_t written = ffado_ringbuffer_write(m_event_buffer, data, write_size);
410         if (written < write_size)
411         {
412             debugWarning("ringbuffer full, %u, %u\n", write_size, written);
413             return false;
414         }
415         incrementFrameCounter(nframes,ts);
416     }
417     return true;
418 }
419
420 /**
421  * @brief Preload frames into the buffer
422  *
423  * Preload \ref nframes of frames from the buffer pointed to by \ref data to the
424  * internal ringbuffer. Does not care about transparency. Keeps the buffer head or tail
425  * timestamp constant.
426  *
427  * @note not thread safe
428  *
429  * @param nframes number of frames to copy
430  * @param data pointer to the frame buffer
431  * @param keep_head_ts if true, keep the head timestamp constant. If false, keep the
432  *                     tail timestamp constant.
433  * @return true if successful
434  */
435 bool TimestampedBuffer::preloadFrames(unsigned int nframes, char *data, bool keep_head_ts) {
436     unsigned int write_size = nframes * m_event_size * m_events_per_frame;
437     // add the data payload to the ringbuffer
438     size_t written = ffado_ringbuffer_write(m_event_buffer, data, write_size);
439     if (written < write_size)
440     {
441         debugWarning("ringbuffer full, request: %u, actual: %u\n", write_size, written);
442         return false;
443     }
444    
445     // make sure the head timestamp remains identical
446     signed int fc;
447     ffado_timestamp_t ts;
448
449     if (keep_head_ts) {
450         getBufferHeadTimestamp(&ts, &fc);
451     } else {
452         getBufferTailTimestamp(&ts, &fc);
453     }
454     // update frame counter
455     m_framecounter += nframes;
456     if (keep_head_ts) {
457         setBufferHeadTimestamp(ts);
458     } else {
459         setBufferTailTimestamp(ts);
460     }
461     return true;
462 }
463
464 /**
465  * @brief Drop frames from the head of the buffer
466  *
467  * drops \ref nframes of frames from the head of internal buffer
468  *
469  * @param nframes number of frames to drop
470  * @return true if successful
471  */
472 bool
473 TimestampedBuffer::dropFrames(unsigned int nframes) {
474     unsigned int read_size = nframes * m_event_size * m_events_per_frame;
475     ffado_ringbuffer_read_advance(m_event_buffer, read_size);
476     decrementFrameCounter(nframes);
477     return true;
478 }
479
480 /**
481  * @brief Read frames from the buffer
482  *
483  * Copies \ref nframes of frames from the internal buffer to the data buffer pointed
484  * to by \ref data.
485  *
486  * @param nframes number of frames to copy
487  * @param data pointer to the frame buffer
488  * @return true if successful
489  */
490 bool TimestampedBuffer::readFrames(unsigned int nframes, char *data) {
491
492     unsigned int read_size=nframes*m_event_size*m_events_per_frame;
493
494     if (m_transparent) {
495         return true; // FIXME: the data still doesn't make sense!
496     } else {
497         // get the data payload to the ringbuffer
498         if ((ffado_ringbuffer_read(m_event_buffer,data,read_size)) < read_size)
499         {
500             debugWarning("readFrames buffer underrun\n");
501             return false;
502         }
503         decrementFrameCounter(nframes);
504     }
505     return true;
506 }
507
508 /**
509  * @brief Performs block processing write of frames
510  *
511  * This function allows for zero-copy writing into the ringbuffer.
512  * It calls the client's processWriteBlock function to write frames
513  * into the internal buffer's data area, in a thread safe fashion.
514  *
515  * It also updates the timestamp.
516  *
517  * @param nbframes number of frames to process
518  * @param ts timestamp of the last frame written to the buffer
519  * @return true if successful
520  */
521 bool TimestampedBuffer::blockProcessWriteFrames(unsigned int nbframes, ffado_timestamp_t ts) {
522
523     debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Transferring period...\n");
524     int xrun;
525     unsigned int offset=0;
526
527     ffado_ringbuffer_data_t vec[2];
528     // we received one period of frames
529     // this is period_size*dimension of events
530     unsigned int events2write=nbframes*m_events_per_frame;
531     unsigned int bytes2write=events2write*m_event_size;
532
533     /* write events2write bytes to the ringbuffer
534     *  first see if it can be done in one read.
535     *  if so, ok.
536     *  otherwise write up to a multiple of clusters directly to the buffer
537     *  then do the buffer wrap around using ringbuffer_write
538     *  then write the remaining data directly to the buffer in a third pass
539     *  Make sure that we cannot end up on a non-cluster aligned position!
540     */
541     unsigned int cluster_size=m_events_per_frame*m_event_size;
542
543     while(bytes2write>0) {
544         int byteswritten=0;
545
546         unsigned int frameswritten=(nbframes*cluster_size-bytes2write)/cluster_size;
547         offset=frameswritten;
548
549         ffado_ringbuffer_get_write_vector(m_event_buffer, vec);
550
551         if(vec[0].len==0) { // this indicates a full event buffer
552             debugError("Event buffer overrun in buffer %p, fill: %u, bytes2write: %u \n",
553                        this, ffado_ringbuffer_read_space(m_event_buffer), bytes2write);
554             debugShowBackLog();
555             return false;
556         }
557
558         /* if we don't take care we will get stuck in an infinite loop
559         * because we align to a cluster boundary later
560         * the remaining nb of bytes in one write operation can be
561         * smaller than one cluster
562         * this can happen because the ringbuffer size is always a power of 2
563         */
564         if(vec[0].len<cluster_size) {
565
566             // encode to the temporary buffer
567             xrun = m_Client->processWriteBlock(m_cluster_buffer, 1, offset);
568
569             if(xrun<0) {
570                 // xrun detected
571                 debugError("Frame buffer underrun in buffer %p\n",this);
572                 return false;
573             }
574
575             // use the ringbuffer function to write one cluster
576             // the write function handles the wrap around.
577             ffado_ringbuffer_write(m_event_buffer,
578                          m_cluster_buffer,
579                          cluster_size);
580
581             // we advanced one cluster_size
582             bytes2write-=cluster_size;
583
584         } else { //
585
586             if(bytes2write>vec[0].len) {
587                 // align to a cluster boundary
588                 byteswritten=vec[0].len-(vec[0].len%cluster_size);
589             } else {
590                 byteswritten=bytes2write;
591             }
592
593             xrun = m_Client->processWriteBlock(vec[0].buf,
594                          byteswritten/cluster_size,
595                          offset);
596
597             if(xrun<0) {
598                     // xrun detected
599                 debugError("Frame buffer underrun in buffer %p\n",this);
600                 return false; // FIXME: return false ?
601             }
602
603             ffado_ringbuffer_write_advance(m_event_buffer, byteswritten);
604             bytes2write -= byteswritten;
605         }
606
607         // the bytes2write should always be cluster aligned
608         assert(bytes2write%cluster_size==0);
609
610     }
611
612     incrementFrameCounter(nbframes,ts);
613
614     return true;
615
616 }
617
618 /**
619  * @brief Performs block processing read of frames
620  *
621  * This function allows for zero-copy reading from the ringbuffer.
622  * It calls the client's processReadBlock function to read frames
623  * directly from the internal buffer's data area, in a thread safe
624  * fashion.
625  *
626  * @param nbframes number of frames to process
627  * @return true if successful
628  */
629 bool TimestampedBuffer::blockProcessReadFrames(unsigned int nbframes) {
630
631     debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Reading %u from buffer (%p)...\n", nbframes, this);
632
633     int xrun;
634     unsigned int offset=0;
635
636     ffado_ringbuffer_data_t vec[2];
637     // we received one period of frames on each connection
638     // this is period_size*dimension of events
639
640     unsigned int events2read=nbframes*m_events_per_frame;
641     unsigned int bytes2read=events2read*m_event_size;
642     /* read events2read bytes from the ringbuffer
643     *  first see if it can be done in one read.
644     *  if so, ok.
645     *  otherwise read up to a multiple of clusters directly from the buffer
646     *  then do the buffer wrap around using ringbuffer_read
647     *  then read the remaining data directly from the buffer in a third pass
648     *  Make sure that we cannot end up on a non-cluster aligned position!
649     */
650     unsigned int cluster_size=m_events_per_frame*m_event_size;
651
652     while(bytes2read>0) {
653         unsigned int framesread=(nbframes*cluster_size-bytes2read)/cluster_size;
654         offset=framesread;
655
656         int bytesread=0;
657
658         ffado_ringbuffer_get_read_vector(m_event_buffer, vec);
659
660         if(vec[0].len==0) { // this indicates an empty event buffer
661             debugError("Event buffer underrun in buffer %p\n",this);
662             return false;
663         }
664
665         /* if we don't take care we will get stuck in an infinite loop
666         * because we align to a cluster boundary later
667         * the remaining nb of bytes in one read operation can be smaller than one cluster
668         * this can happen because the ringbuffer size is always a power of 2
669                 */
670         if(vec[0].len<cluster_size) {
671             // use the ringbuffer function to read one cluster
672             // the read function handles wrap around
673             ffado_ringbuffer_read(m_event_buffer,m_cluster_buffer,cluster_size);
674
675             assert(m_Client);
676             xrun = m_Client->processReadBlock(m_cluster_buffer, 1, offset);
677
678             if(xrun<0) {
679                 // xrun detected
680                 debugError("Frame buffer overrun in buffer %p\n",this);
681                     return false;
682             }
683
684             // we advanced one cluster_size
685             bytes2read-=cluster_size;
686
687         } else { //
688
689             if(bytes2read>vec[0].len) {
690                 // align to a cluster boundary
691                 bytesread=vec[0].len-(vec[0].len%cluster_size);
692             } else {
693                 bytesread=bytes2read;
694             }
695
696             assert(m_Client);
697             xrun = m_Client->processReadBlock(vec[0].buf, bytesread/cluster_size, offset);
698
699             if(xrun<0) {
700                 // xrun detected
701                 debugError("Frame buffer overrun in buffer %p\n",this);
702                 return false;
703             }
704
705             ffado_ringbuffer_read_advance(m_event_buffer, bytesread);
706             bytes2read -= bytesread;
707         }
708
709         // the bytes2read should always be cluster aligned
710         assert(bytes2read%cluster_size==0);
711     }
712
713     decrementFrameCounter(nbframes);
714
715     return true;
716 }
717
718 /**
719  * @brief Sets the buffer tail timestamp.
720  *
721  * Set the buffer tail timestamp to \ref new_timestamp. This will recalculate
722  * the internal state such that the buffer's timeframe starts at
723  * \ref new_timestamp.
724  *
725  * This is thread safe.
726  *
727  * @note considers offsets
728  *
729  * @param new_timestamp
730  */
731 void TimestampedBuffer::setBufferTailTimestamp(ffado_timestamp_t new_timestamp) {
732
733     // add the offsets
734     ffado_timestamp_t ts=new_timestamp;
735     ts += m_tick_offset;
736
737     if (ts >= m_wrap_at) {
738         ts -= m_wrap_at;
739     } else if (ts < 0) {
740         ts += m_wrap_at;
741     }
742
743 #ifdef DEBUG
744     if (new_timestamp >= m_wrap_at) {
745         debugWarning("timestamp not wrapped: "TIMESTAMP_FORMAT_SPEC"\n",new_timestamp);
746     }
747     if ((ts >= m_wrap_at) || (ts < 0 )) {
748         debugWarning("ts not wrapped correctly: "TIMESTAMP_FORMAT_SPEC"\n",ts);
749     }
750 #endif
751
752     ENTER_CRITICAL_SECTION;
753
754     m_buffer_tail_timestamp = ts;
755
756     m_dll_e2=m_update_period * (double)m_nominal_rate;
757     m_buffer_next_tail_timestamp = (ffado_timestamp_t)((double)m_buffer_tail_timestamp + m_dll_e2);
758
759     EXIT_CRITICAL_SECTION;
760
761     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "for (%p) to "
762                                           TIMESTAMP_FORMAT_SPEC" => "TIMESTAMP_FORMAT_SPEC", NTS="
763                                           TIMESTAMP_FORMAT_SPEC", DLL2=%f, RATE=%f\n",
764                 this, new_timestamp, ts, m_buffer_next_tail_timestamp, m_dll_e2, getRate());
765
766 }
767
768 /**
769  * @brief Sets the buffer head timestamp.
770  *
771  * Set the buffer tail timestamp such that the buffer head timestamp becomes
772  * \ref new_timestamp. This does not consider offsets, because it's use is to
773  * make sure the following is true after setBufferHeadTimestamp(x):
774  *   x == getBufferHeadTimestamp()
775  *
776  * This is thread safe.
777  *
778  * @param new_timestamp
779  */
780 void TimestampedBuffer::setBufferHeadTimestamp(ffado_timestamp_t new_timestamp) {
781
782 #ifdef DEBUG
783     if (new_timestamp >= m_wrap_at) {
784         debugWarning("timestamp not wrapped: "TIMESTAMP_FORMAT_SPEC"\n",new_timestamp);
785     }
786 #endif
787
788     ffado_timestamp_t ts = new_timestamp;
789
790     ENTER_CRITICAL_SECTION;
791
792     // add the time
793     ts += (ffado_timestamp_t)(m_nominal_rate * (float)m_framecounter);
794
795     if (ts >= m_wrap_at) {
796         ts -= m_wrap_at;
797     } else if (ts < 0) {
798         ts += m_wrap_at;
799     }
800
801     m_buffer_tail_timestamp = ts;
802
803     m_dll_e2=m_update_period * (double)m_nominal_rate;
804     m_buffer_next_tail_timestamp = (ffado_timestamp_t)((double)m_buffer_tail_timestamp + m_dll_e2);
805
806     EXIT_CRITICAL_SECTION;
807
808     debugOutput(DEBUG_LEVEL_VERBOSE, "for (%p) to "TIMESTAMP_FORMAT_SPEC" => "
809                                           TIMESTAMP_FORMAT_SPEC", NTS="TIMESTAMP_FORMAT_SPEC", DLL2=%f, RATE=%f\n",
810                 this, new_timestamp, ts, m_buffer_next_tail_timestamp, m_dll_e2, getRate());
811
812 }
813
814 /**
815  * @brief Synchronize the buffer head to a specified timestamp
816  *
817  * Try to synchronize the buffer head to a specific timestamp. This
818  * can mean adding or removing samples to/from the buffer such that
819  * the buffer head aligns with the specified timestamp. The alignment
820  * is within ts +/- Tsample/2
821  *
822  * @param target the timestamp to align to
823  * @return true if alignment succeeded, false if not
824  */
825 bool
826 TimestampedBuffer::syncBufferHeadToTimestamp(ffado_timestamp_t target)
827 {
828     uint64_t ts_head;
829     uint64_t ts_target=(uint64_t)target;
830     signed int fc;
831     int32_t lag_ticks;
832     float lag_frames;
833
834     ffado_timestamp_t ts_head_tmp;
835     getBufferHeadTimestamp(&ts_head_tmp, &fc);
836     ts_head=(uint64_t)ts_head_tmp;
837     // if target > ts_head then the wanted buffer head timestamp
838     // is later than the actual. This means that we (might) have to drop
839     // some frames.
840     lag_ticks=diffTicks(ts_target, ts_head);
841     float rate=getRate();
842    
843     assert(rate!=0.0);
844
845     lag_frames=(((float)lag_ticks)/rate);
846    
847     debugOutput( DEBUG_LEVEL_VERBOSE, "(%p): HEAD=%llu, TS=%llu, diff=%ld = %10.5f frames (rate=%10.5f)\n",
848                                       this, ts_head, ts_target, lag_ticks, lag_frames, rate);
849
850     if (lag_frames>=1.0) {
851         // the buffer head is too early
852         // ditch frames until the buffer head is on time
853         char dummy[getBytesPerFrame()]; // one frame of garbage
854         int frames_to_ditch=(int)roundf(lag_frames);
855         debugOutput( DEBUG_LEVEL_VERBOSE, "(%p): ditching %d frames (@ ts=%lld)\n",this,frames_to_ditch,ts_target);
856        
857         while (frames_to_ditch--) {
858             readFrames(1, dummy);
859         }
860        
861     } else if (lag_frames<=-1.0) {
862         // the buffer head is too late
863         // add some padding frames
864         int frames_to_add=(int)roundf(lag_frames);
865         debugOutput( DEBUG_LEVEL_VERBOSE, "(%p): adding %d frames (@ ts=%lld)\n",this,-frames_to_add,ts_target);
866        
867         while (frames_to_add++) {
868              writeDummyFrame();
869         }
870     }
871     getBufferHeadTimestamp(&ts_head_tmp, &fc);
872     ts_head=(uint64_t)ts_head_tmp;
873     debugOutput( DEBUG_LEVEL_VERBOSE, "(%p): new HEAD=%llu, fc=%d, target=%llu, new diff=%lld\n",
874                                       this, ts_head, fc, ts_target, diffTicks(ts_target, ts_head));
875     // FIXME: of course this doesn't always succeed
876     return true;
877 }
878
879 /**
880  * @brief Synchronize the buffer tail to a specified timestamp
881  *
882  * Try to synchronize the buffer tail to a specific timestamp. This
883  * can mean adding or removing samples to/from the buffer such that
884  * the buffer tail aligns with the specified timestamp. The alignment
885  * is within ts +/- Tsample/2
886  *
887  * @param target the timestamp to align to
888  * @return true if alignment succeeded, false if not
889  */
890 bool
891 TimestampedBuffer::syncBufferTailToTimestamp(ffado_timestamp_t target)
892 {
893     uint64_t ts_tail;
894     uint64_t ts_target=(uint64_t)target;
895     signed int fc;
896     int32_t lag_ticks;
897     float lag_frames;
898
899     debugWarning("Untested\n");
900    
901     ffado_timestamp_t ts_tail_tmp;
902     getBufferTailTimestamp(&ts_tail_tmp, &fc);
903     ts_tail=(uint64_t)ts_tail_tmp;
904     // if target < ts_tail then the wanted buffer head timestamp
905     // is later than the actual. This means that we (might) have to drop
906     // some frames.
907     lag_ticks=diffTicks(ts_tail, ts_target);
908     float rate=getRate();
909    
910     assert(rate!=0.0);
911
912     lag_frames=(((float)lag_ticks)/rate);
913    
914     debugOutput( DEBUG_LEVEL_VERBOSE, "(%p): HEAD=%llu, TS=%llu, diff=%ld = %10.5f frames (rate=%10.5f)\n",
915                                       this, ts_tail, ts_target, lag_ticks, lag_frames, rate);
916
917     if (lag_frames>=1.0) {
918         // the buffer head is too early
919         // ditch frames until the buffer head is on time
920         char dummy[getBytesPerFrame()]; // one frame of garbage
921         int frames_to_ditch=(int)roundf(lag_frames);
922         debugOutput( DEBUG_LEVEL_VERBOSE, "(%p): ditching %d frames (@ ts=%lld)\n",this,frames_to_ditch,ts_target);
923        
924         while (frames_to_ditch--) {
925             readFrames(1, dummy);
926         }
927        
928     } else if (lag_frames<=-1.0) {
929         // the buffer head is too late
930         // add some padding frames
931         int frames_to_add=(int)roundf(lag_frames);
932         debugOutput( DEBUG_LEVEL_VERBOSE, "(%p): adding %d frames (@ ts=%lld)\n",this,-frames_to_add,ts_target);
933        
934         while (frames_to_add++) {
935              writeDummyFrame();
936         }
937     }
938     getBufferHeadTimestamp(&ts_tail_tmp, &fc);
939     ts_tail=(uint64_t)ts_tail_tmp;
940     debugOutput( DEBUG_LEVEL_VERBOSE, "(%p): new HEAD=%llu, fc=%d, target=%llu, new diff=%lld\n",
941                                       this, ts_tail, fc, ts_target, diffTicks(ts_target, ts_tail));
942     // FIXME: of course this doesn't always succeed
943     return true;
944 }
945
946 /**
947  * @brief correct lag
948  *
949  * Try to synchronize the buffer tail to a specific timestamp. This
950  * can mean adding or removing samples to/from the buffer such that
951  * the buffer tail aligns with the specified timestamp. The alignment
952  * is within ts +/- Tsample/2
953  *
954  * @param target the timestamp to align to
955  * @return true if alignment succeeded, false if not
956  */
957 bool
958 TimestampedBuffer::syncCorrectLag(int64_t lag_ticks)
959 {
960     float lag_frames;
961     float rate=getRate();
962     assert(rate!=0.0);
963
964     lag_frames=(((float)lag_ticks)/rate);
965     if (lag_frames >= 1.0) {
966         // the buffer head is too late
967         // add some padding frames
968         int frames_to_add=(int)roundf(lag_frames);
969         debugOutput( DEBUG_LEVEL_VERBOSE, "(%p): adding %d frames\n",this,frames_to_add);
970
971         while (frames_to_add++) {
972              writeDummyFrame();
973         }
974     } else if (lag_frames <= -1.0) {
975         // the buffer head is too early
976         // ditch frames until the buffer head is on time
977         char dummy[getBytesPerFrame()]; // one frame of garbage
978         int frames_to_ditch=(int)roundf(lag_frames);
979         debugOutput( DEBUG_LEVEL_VERBOSE, "(%p): ditching %d frames\n",this,-frames_to_ditch);
980
981         while (frames_to_ditch--) {
982             readFrames(1, dummy);
983         }
984     }
985     return true;
986 }
987
988 /**
989  * \brief return the timestamp of the first frame in the buffer
990  *
991  * This function returns the timestamp of the very first sample in
992  * the StreamProcessor's buffer. It also returns the framecounter value
993  * for which this timestamp is valid.
994  *
995  * @param ts address to store the timestamp in
996  * @param fc address to store the associated framecounter in
997  */
998 void TimestampedBuffer::getBufferHeadTimestamp(ffado_timestamp_t *ts, signed int *fc) {
999     // NOTE: this is still ok with threads, because we use *fc to compute
1000     //       the timestamp
1001     *fc = m_framecounter;
1002     *ts = getTimestampFromTail(*fc);
1003 }
1004
1005 /**
1006  * \brief return the timestamp of the last frame in the buffer
1007  *
1008  * This function returns the timestamp of the last frame in
1009  * the StreamProcessor's buffer. It also returns the framecounter
1010  * value for which this timestamp is valid.
1011  *
1012  * @param ts address to store the timestamp in
1013  * @param fc address to store the associated framecounter in
1014  */
1015 void TimestampedBuffer::getBufferTailTimestamp(ffado_timestamp_t *ts, signed int *fc) {
1016     // NOTE: this is still ok with threads, because we use *fc to compute
1017     //       the timestamp
1018     *fc = m_framecounter;
1019     *ts = getTimestampFromTail(0);
1020 }
1021
1022 /**
1023  * @brief Get timestamp for a specific position from the buffer tail
1024  *
1025  * Returns the timestamp for a position that is nframes earlier than the
1026  * buffer tail
1027  *
1028  * @param nframes number of frames
1029  * @return timestamp value
1030  */
1031 ffado_timestamp_t TimestampedBuffer::getTimestampFromTail(int nframes)
1032 {
1033     // ts(x) = m_buffer_tail_timestamp -
1034     //         (m_buffer_next_tail_timestamp - m_buffer_tail_timestamp)/(samples_between_updates)*(x)
1035     ffado_timestamp_t timestamp;
1036     timestamp = m_buffer_tail_timestamp;
1037
1038     timestamp -= (ffado_timestamp_t)((nframes) * m_current_rate);
1039
1040     if(timestamp >= m_wrap_at) {
1041         timestamp -= m_wrap_at;
1042     } else if(timestamp < 0) {
1043         timestamp += m_wrap_at;
1044     }
1045
1046     return timestamp;
1047 }
1048
1049 /**
1050  * @brief Get timestamp for a specific position from the buffer head
1051  *
1052  * Returns the timestamp for a position that is nframes later than the
1053  * buffer head
1054  *
1055  * @param nframes number of frames
1056  * @return timestamp value
1057  */
1058 ffado_timestamp_t TimestampedBuffer::getTimestampFromHead(int nframes)
1059 {
1060     return getTimestampFromTail(m_framecounter-nframes);
1061 }
1062
1063 /**
1064  * Resets the frame counter, in a atomic way. This
1065  * is thread safe.
1066  */
1067 void TimestampedBuffer::resetFrameCounter() {
1068     ENTER_CRITICAL_SECTION;
1069     m_framecounter = 0;
1070     EXIT_CRITICAL_SECTION;
1071 }
1072
1073 /**
1074  * Decrements the frame counter in a thread safe way.
1075  *
1076  * @param nbframes number of frames to decrement
1077  */
1078 void TimestampedBuffer::decrementFrameCounter(int nbframes) {
1079     ENTER_CRITICAL_SECTION;
1080     m_framecounter -= nbframes;
1081     EXIT_CRITICAL_SECTION;
1082 }
1083
1084 /**
1085  * Increments the frame counter in a thread safe way.
1086  * Also updates the timestamp.
1087  *
1088  * @note the offsets defined by setTicksOffset and setFrameOffset
1089  *       are added here.
1090  *
1091  * @param nbframes the number of frames to add
1092  * @param new_timestamp the new timestamp
1093  */
1094 void TimestampedBuffer::incrementFrameCounter(int nbframes, ffado_timestamp_t new_timestamp) {
1095
1096     // add the offsets
1097     ffado_timestamp_t diff;
1098    
1099     ENTER_CRITICAL_SECTION;
1100     diff = m_buffer_next_tail_timestamp - m_buffer_tail_timestamp;
1101     EXIT_CRITICAL_SECTION;
1102
1103     if (diff < 0) diff += m_wrap_at;
1104
1105     ffado_timestamp_t ts = new_timestamp;
1106     ts += m_tick_offset;
1107
1108     if (ts >= m_wrap_at) {
1109         ts -= m_wrap_at;
1110     } else if (ts < 0) {
1111         ts += m_wrap_at;
1112     }
1113
1114 #ifdef DEBUG
1115     if (new_timestamp >= m_wrap_at) {
1116         debugWarning("timestamp not wrapped: "TIMESTAMP_FORMAT_SPEC"\n", new_timestamp);
1117     }
1118     if ((ts >= m_wrap_at) || (ts < 0 )) {
1119         debugWarning("ts not wrapped correctly: "TIMESTAMP_FORMAT_SPEC"\n",ts);
1120     }
1121 #endif
1122 // FIXME: JMW: at some points during startup the timestamp doesn't change.
1123 // This still needs to be verified in more detail. 
1124 // if (ts>m_buffer_tail_timestamp-1 && ts<m_buffer_tail_timestamp+1) {
1125 //   ENTER_CRITICAL_SECTION;
1126 //   m_framecounter += nbframes;
1127 //   EXIT_CRITICAL_SECTION;
1128 //   return;
1129 // }
1130     ffado_timestamp_t pred_buffer_next_tail_timestamp;
1131     if(nbframes == m_update_period) {
1132         pred_buffer_next_tail_timestamp = m_buffer_next_tail_timestamp;
1133     } else {
1134         debugOutput( DEBUG_LEVEL_VERBOSE,
1135                      "Number of frames (%u) != update period (%u)\n",
1136                      nbframes, m_update_period );
1137         // calculate the predicted timestamp for nframes (instead of m_update_period)
1138         // after the previous update.
1139         float rel_step = ((float)nbframes)/((float)m_update_period);
1140         ENTER_CRITICAL_SECTION; // FIXME: do we need these?
1141         ffado_timestamp_t corrected_step = (m_buffer_next_tail_timestamp - m_buffer_tail_timestamp) * rel_step;
1142         pred_buffer_next_tail_timestamp = m_buffer_tail_timestamp + corrected_step;
1143         EXIT_CRITICAL_SECTION;
1144        
1145         debugOutput( DEBUG_LEVEL_VERBOSE,
1146                      "Updated ("TIMESTAMP_FORMAT_SPEC","TIMESTAMP_FORMAT_SPEC") to ("TIMESTAMP_FORMAT_SPEC","TIMESTAMP_FORMAT_SPEC")\n",
1147                      m_buffer_tail_timestamp, m_buffer_next_tail_timestamp,
1148                      m_buffer_tail_timestamp, pred_buffer_next_tail_timestamp);
1149     }
1150    
1151     // the difference between the given TS and the one predicted for this time instant
1152     // this is the error for the DLL
1153     diff = ts - pred_buffer_next_tail_timestamp;
1154
1155     // check whether the update is within the allowed bounds
1156     const float max_deviation = (50.0/100.0); // maximal relative difference considered normal
1157     ffado_timestamp_t one_update_step = nbframes * getRate();
1158     ffado_timestamp_t max_abs_diff = one_update_step * (1.0 + max_deviation);
1159    
1160     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, " nbframes: %d, m_update_period: %d \n", nbframes, m_update_period);
1161     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, " tail TS: "TIMESTAMP_FORMAT_SPEC", next tail TS: "TIMESTAMP_FORMAT_SPEC"\n",
1162                                           m_buffer_tail_timestamp, m_buffer_next_tail_timestamp);
1163     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, " new TS: "TIMESTAMP_FORMAT_SPEC", wrapped new TS: "TIMESTAMP_FORMAT_SPEC"\n",
1164                                           new_timestamp, ts);
1165
1166     if (diff > max_abs_diff) {
1167         debugShowBackLogLines(100);
1168         debugWarning("(%p) difference rather large (+): diff="TIMESTAMP_FORMAT_SPEC", max="TIMESTAMP_FORMAT_SPEC", "TIMESTAMP_FORMAT_SPEC", "TIMESTAMP_FORMAT_SPEC"\n",
1169             this, diff, max_abs_diff, ts, pred_buffer_next_tail_timestamp);
1170     } else if (diff < -max_abs_diff) {
1171         debugShowBackLogLines(100);
1172         debugWarning("(%p) difference rather large (-): diff="TIMESTAMP_FORMAT_SPEC", max="TIMESTAMP_FORMAT_SPEC", "TIMESTAMP_FORMAT_SPEC", "TIMESTAMP_FORMAT_SPEC"\n",
1173             this, diff, -max_abs_diff, ts, pred_buffer_next_tail_timestamp);
1174     }
1175
1176     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "(%p): diff="TIMESTAMP_FORMAT_SPEC" ",
1177                 this, diff);
1178
1179     double err = diff;
1180
1181     debugOutputShort(DEBUG_LEVEL_VERY_VERBOSE, "diff2="TIMESTAMP_FORMAT_SPEC" err=%f\n",
1182                     diff, err);
1183     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "B: FC=%10u, TS="TIMESTAMP_FORMAT_SPEC", NTS="TIMESTAMP_FORMAT_SPEC"\n",
1184                     m_framecounter, m_buffer_tail_timestamp, pred_buffer_next_tail_timestamp);
1185
1186     ENTER_CRITICAL_SECTION;
1187     m_framecounter += nbframes;
1188     m_buffer_tail_timestamp = pred_buffer_next_tail_timestamp;
1189     m_buffer_next_tail_timestamp = pred_buffer_next_tail_timestamp + (ffado_timestamp_t)(m_dll_b * err + m_dll_e2);
1190     m_dll_e2 += m_dll_c*err;
1191
1192     if (m_buffer_next_tail_timestamp >= m_wrap_at) {
1193         debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "Unwrapping next tail timestamp: "TIMESTAMP_FORMAT_SPEC"",
1194                 m_buffer_next_tail_timestamp);
1195
1196         m_buffer_next_tail_timestamp -= m_wrap_at;
1197
1198         debugOutputShort(DEBUG_LEVEL_VERY_VERBOSE, " => "TIMESTAMP_FORMAT_SPEC"\n",
1199                 m_buffer_next_tail_timestamp);
1200
1201     }
1202     m_current_rate = calculateRate();
1203     EXIT_CRITICAL_SECTION;
1204
1205     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "A: TS="TIMESTAMP_FORMAT_SPEC", NTS="TIMESTAMP_FORMAT_SPEC", DLLe2=%f, RATE=%f\n",
1206                 m_buffer_tail_timestamp, m_buffer_next_tail_timestamp, m_dll_e2, m_current_rate);
1207
1208
1209     if(m_buffer_tail_timestamp>=m_wrap_at) {
1210         debugError("Wrapping failed for m_buffer_tail_timestamp! "TIMESTAMP_FORMAT_SPEC"\n",m_buffer_tail_timestamp);
1211         debugOutput(DEBUG_LEVEL_VERY_VERBOSE, " IN="TIMESTAMP_FORMAT_SPEC", TS="TIMESTAMP_FORMAT_SPEC", NTS="TIMESTAMP_FORMAT_SPEC"\n",
1212                     ts, m_buffer_tail_timestamp, m_buffer_next_tail_timestamp);
1213
1214     }
1215     if(m_buffer_next_tail_timestamp>=m_wrap_at) {
1216         debugError("Wrapping failed for m_buffer_next_tail_timestamp! "TIMESTAMP_FORMAT_SPEC"\n",m_buffer_next_tail_timestamp);
1217         debugOutput(DEBUG_LEVEL_VERY_VERBOSE, " IN="TIMESTAMP_FORMAT_SPEC", TS="TIMESTAMP_FORMAT_SPEC", NTS="TIMESTAMP_FORMAT_SPEC"\n",
1218                     ts, m_buffer_tail_timestamp, m_buffer_next_tail_timestamp);
1219     }
1220    
1221     if(m_buffer_tail_timestamp==m_buffer_next_tail_timestamp) {
1222         debugError("Current and next timestamps are equal: "TIMESTAMP_FORMAT_SPEC" "TIMESTAMP_FORMAT_SPEC"\n",
1223                    m_buffer_tail_timestamp,m_buffer_next_tail_timestamp);
1224    
1225     }
1226
1227     // this DLL allows the calculation of any sample timestamp relative to the buffer tail,
1228     // to the next period and beyond (through extrapolation)
1229     //
1230     // ts(x) = m_buffer_tail_timestamp +
1231     //         (m_buffer_next_tail_timestamp - m_buffer_tail_timestamp)/(samples_between_updates)*x
1232 }
1233
1234 /**
1235  * @brief Print status info.
1236  */
1237 void TimestampedBuffer::dumpInfo() {
1238
1239     ffado_timestamp_t ts_head;
1240     signed int fc;
1241     getBufferHeadTimestamp(&ts_head,&fc);
1242
1243 #ifdef DEBUG
1244     ffado_timestamp_t diff=(ffado_timestamp_t)ts_head - (ffado_timestamp_t)m_buffer_tail_timestamp;
1245 #endif
1246
1247     debugOutputShort( DEBUG_LEVEL_NORMAL, "  TimestampedBuffer (%p): %04d frames, %04d events\n",
1248                                           this, m_framecounter, getBufferFill());
1249     debugOutputShort( DEBUG_LEVEL_NORMAL, "   Timestamps           : head: "TIMESTAMP_FORMAT_SPEC", Tail: "TIMESTAMP_FORMAT_SPEC", Next tail: "TIMESTAMP_FORMAT_SPEC"\n",
1250                                           ts_head, m_buffer_tail_timestamp, m_buffer_next_tail_timestamp);
1251     debugOutputShort( DEBUG_LEVEL_NORMAL, "    Head - Tail         : "TIMESTAMP_FORMAT_SPEC"\n", diff);
1252     debugOutputShort( DEBUG_LEVEL_NORMAL, "   DLL Rate             : %f (%f)\n", m_dll_e2, m_dll_e2/m_update_period);
1253 }
1254
1255 } // end of namespace Util
Note: See TracBrowser for help on using the browser.