root/branches/ppalmers-streaming/src/libutil/TimestampedBuffer.cpp

Revision 727, 41.3 kB (checked in by ppalmers, 15 years ago)

stream alignment implemented

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