root/branches/ppalmers-streaming/src/libstreaming/generic/StreamProcessor.cpp

Revision 706, 10.2 kB (checked in by ppalmers, 13 years ago)

code cleanup

Line 
1 /*
2  * Copyright (C) 2005-2007 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License version 2.1, as published by the Free Software Foundation;
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21  * MA 02110-1301 USA
22  */
23
24 #include "StreamProcessor.h"
25 #include "../util/cycletimer.h"
26 #include "../StreamProcessorManager.h"
27
28 #include "libutil/Atomic.h"
29
30 #include <assert.h>
31 #include <math.h>
32
33 namespace Streaming {
34
35 IMPL_DEBUG_MODULE( StreamProcessor, StreamProcessor, DEBUG_LEVEL_VERBOSE );
36
37 StreamProcessor::StreamProcessor(enum IsoStream::EStreamType type, int port, int framerate)
38     : IsoStream(type, port)
39     , m_nb_buffers(0)
40     , m_period(0)
41     , m_xruns(0)
42     , m_framerate(framerate)
43     , m_manager(NULL)
44     , m_running(false)
45     , m_disabled(true)
46     , m_is_disabled(true)
47     , m_cycle_to_enable_at(0)
48     , m_SyncSource(NULL)
49     , m_ticks_per_frame(0)
50     , m_last_cycle(0)
51     , m_sync_delay(0)
52 {
53     // create the timestamped buffer and register ourselves as its client
54     m_data_buffer=new Util::TimestampedBuffer(this);
55
56 }
57
58 StreamProcessor::~StreamProcessor() {
59     if (m_data_buffer) delete m_data_buffer;
60 }
61
62 void StreamProcessor::dumpInfo()
63 {
64     debugOutputShort( DEBUG_LEVEL_NORMAL, " StreamProcessor information\n");
65     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Iso stream info:\n");
66
67     IsoStream::dumpInfo();
68     debugOutputShort( DEBUG_LEVEL_NORMAL, "  StreamProcessor info:\n");
69     if (m_handler)
70         debugOutputShort( DEBUG_LEVEL_NORMAL, "  Now                   : %011u\n",m_handler->getCycleTimerTicks());
71     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Xruns                 : %d\n", m_xruns);
72     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Running               : %d\n", m_running);
73     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Enabled               : %s\n", m_disabled ? "No" : "Yes");
74     debugOutputShort( DEBUG_LEVEL_NORMAL, "   enable status        : %s\n", m_is_disabled ? "No" : "Yes");
75
76     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Nominal framerate     : %u\n", m_framerate);
77     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Device framerate      : Sync: %f, Buffer %f\n",
78         24576000.0/m_SyncSource->m_data_buffer->getRate(),
79         24576000.0/m_data_buffer->getRate()
80         );
81
82     m_data_buffer->dumpInfo();
83
84     m_PeriodStat.dumpInfo();
85     m_PacketStat.dumpInfo();
86     m_WakeupStat.dumpInfo();
87
88 }
89
90 bool StreamProcessor::init()
91 {
92     debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "enter...\n");
93
94     m_data_buffer->init();
95
96     return IsoStream::init();
97 }
98
99 /**
100  * Resets the frame counter, the xrun counter, the ports and the iso stream.
101  * @return true if reset succeeded
102  */
103 bool StreamProcessor::reset() {
104
105     debugOutput( DEBUG_LEVEL_VERBOSE, "Resetting...\n");
106
107     // reset the event buffer, discard all content
108     if (!m_data_buffer->reset()) {
109         debugFatal("Could not reset data buffer\n");
110         return false;
111     }
112
113     resetXrunCounter();
114
115     // loop over the ports to reset them
116     if (!PortManager::resetPorts()) {
117         debugFatal("Could not reset ports\n");
118         return false;
119     }
120
121     // reset the iso stream
122     if (!IsoStream::reset()) {
123         debugFatal("Could not reset isostream\n");
124         return false;
125     }
126     return true;
127
128 }
129
130 bool StreamProcessor::prepareForEnable(uint64_t time_to_enable_at) {
131     debugOutput(DEBUG_LEVEL_VERBOSE," StreamProcessor::prepareForEnable for (%p)\n",this);
132     debugOutput(DEBUG_LEVEL_VERBOSE,"  Now                   : %011u\n",m_handler->getCycleTimerTicks());
133     debugOutput(DEBUG_LEVEL_VERBOSE,"  Enable at             : %011u\n",time_to_enable_at);
134     m_data_buffer->dumpInfo();
135     return true;
136 }
137
138 bool StreamProcessor::prepareForDisable() {
139     debugOutput(DEBUG_LEVEL_VERBOSE," StreamProcessor::prepareForDisable for (%p)\n",this);
140     debugOutput(DEBUG_LEVEL_VERBOSE,"  Now                   : %011u\n",m_handler->getCycleTimerTicks());
141     m_data_buffer->dumpInfo();
142     return true;
143 }
144
145 bool StreamProcessor::prepare() {
146
147     debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing...\n");
148
149     // init the ports
150
151     if(!m_manager) {
152         debugFatal("Not attached to a manager!\n");
153         return -1;
154     }
155
156     m_nb_buffers=m_manager->getNbBuffers();
157     debugOutput( DEBUG_LEVEL_VERBOSE, "Setting m_nb_buffers  : %d\n", m_nb_buffers);
158
159     m_period=m_manager->getPeriodSize();
160     debugOutput( DEBUG_LEVEL_VERBOSE, "Setting m_period      : %d\n", m_period);
161
162     // loop over the ports to reset them
163     PortManager::preparePorts();
164
165     // reset the iso stream
166     IsoStream::prepare();
167
168     return true;
169
170 }
171
172 int StreamProcessor::getBufferFill() {
173 //     return m_data_buffer->getFrameCounter();
174     return m_data_buffer->getBufferFill();
175 }
176
177 uint64_t StreamProcessor::getTimeNow() {
178     return m_handler->getCycleTimerTicks();
179 }
180
181
182 bool StreamProcessor::isRunning() {
183     return m_running;
184 }
185
186 bool StreamProcessor::enable(uint64_t time_to_enable_at)  {
187     // FIXME: time_to_enable_at will be in 'time' not cycles
188     m_cycle_to_enable_at=time_to_enable_at;
189
190     if(!m_running) {
191             debugWarning("The StreamProcessor is not running yet, enable() might not be a good idea.\n");
192     }
193
194 #ifdef DEBUG
195     uint64_t now_cycles=CYCLE_TIMER_GET_CYCLES(m_handler->getCycleTimer());
196     const int64_t max=(int64_t)(CYCLES_PER_SECOND/2);
197
198     int64_t diff=(int64_t)m_cycle_to_enable_at-(int64_t)now_cycles;
199
200     if (diff > max) {
201         diff-=TICKS_PER_SECOND;
202     } else if (diff < -max) {
203         diff+=TICKS_PER_SECOND;
204     }
205
206     if (diff<0) {
207         debugWarning("Request to enable streamprocessor %lld cycles ago (now=%llu, cy=%llu).\n",
208             diff,now_cycles,time_to_enable_at);
209     }
210 #endif
211     m_data_buffer->enable();
212
213     m_disabled=false;
214     return true;
215 }
216
217 bool StreamProcessor::disable()  {
218     m_data_buffer->disable();
219     m_disabled=true;
220     return true;
221 }
222
223 bool StreamProcessor::setSyncSource(StreamProcessor *s) {
224     m_SyncSource=s;
225     return true;
226 }
227
228 float
229 StreamProcessor::getTicksPerFrame() {
230     if (m_data_buffer) {
231         float rate=m_data_buffer->getRate();
232         if (fabsf(m_ticks_per_frame - rate)>(m_ticks_per_frame*0.1)) {
233             debugWarning("TimestampedBuffer rate (%10.5f) more that 10%% off nominal (%10.5f)\n",rate,m_ticks_per_frame);
234             return m_ticks_per_frame;
235         }
236 //         return m_ticks_per_frame;
237         if (rate<0.0) debugError("rate < 0! (%f)\n",rate);
238        
239         return rate;
240     } else {
241         return 0.0;
242     }
243 }
244
245 int64_t StreamProcessor::getTimeUntilNextPeriodSignalUsecs() {
246     uint64_t time_at_period=getTimeAtPeriod();
247
248     // we delay the period signal with the sync delay
249     // this makes that the period signals lag a little compared to reality
250     // ISO buffering causes the packets to be received at max
251     // m_handler->getWakeupInterval() later than the time they were received.
252     // hence their payload is available this amount of time later. However, the
253     // period boundary is predicted based upon earlier samples, and therefore can
254     // pass before these packets are processed. Adding this extra term makes that
255     // the period boundary is signalled later
256     time_at_period = addTicks(time_at_period, m_SyncSource->getSyncDelay());
257
258     uint64_t cycle_timer=m_handler->getCycleTimerTicks();
259
260     // calculate the time until the next period
261     int32_t until_next=diffTicks(time_at_period,cycle_timer);
262
263     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "=> TAP=%11llu, CTR=%11llu, UTN=%11ld\n",
264         time_at_period, cycle_timer, until_next
265         );
266
267     // now convert to usecs
268     // don't use the mapping function because it only works
269     // for absolute times, not the relative time we are
270     // using here (which can also be negative).
271     return (int64_t)(((float)until_next) / TICKS_PER_USEC);
272 }
273
274 uint64_t StreamProcessor::getTimeAtPeriodUsecs() {
275     return (uint64_t)((float)getTimeAtPeriod() * TICKS_PER_USEC);
276 }
277
278 bool StreamProcessor::dropFrames(unsigned int nbframes) {
279     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "StreamProcessor::dropFrames(%d)\n", nbframes);
280     return m_data_buffer->dropFrames(nbframes);
281 }
282
283 /**
284  * Resets the xrun counter, in a atomic way. This
285  * is thread safe.
286  */
287 void StreamProcessor::resetXrunCounter() {
288     ZERO_ATOMIC((SInt32 *)&m_xruns);
289 }
290
291 void StreamProcessor::setVerboseLevel(int l) {
292     setDebugLevel(l);
293     IsoStream::setVerboseLevel(l);
294     PortManager::setVerboseLevel(l);
295
296 }
297
298 uint64_t ReceiveStreamProcessor::getTimeAtPeriod() {
299     ffado_timestamp_t next_period_boundary=m_data_buffer->getTimestampFromHead(m_period);
300
301     #ifdef DEBUG
302     ffado_timestamp_t ts;
303     signed int fc;
304    
305     m_data_buffer->getBufferTailTimestamp(&ts,&fc);
306
307     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "=> NPD="TIMESTAMP_FORMAT_SPEC", LTS="TIMESTAMP_FORMAT_SPEC", FC=%5u, TPF=%f\n",
308         next_period_boundary, ts, fc, getTicksPerFrame()
309         );
310     #endif
311
312     return (uint64_t)next_period_boundary;
313 }
314
315 bool ReceiveStreamProcessor::canClientTransferFrames(unsigned int nbframes) {
316     return m_data_buffer->getFrameCounter() >= (int) nbframes;
317 }
318
319 uint64_t TransmitStreamProcessor::getTimeAtPeriod() {
320     ffado_timestamp_t next_period_boundary=m_data_buffer->getTimestampFromTail((m_nb_buffers-1) * m_period);
321
322     #ifdef DEBUG
323     ffado_timestamp_t ts;
324     signed int fc;
325     m_data_buffer->getBufferTailTimestamp(&ts,&fc);
326
327     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "=> NPD="TIMESTAMP_FORMAT_SPEC", LTS="TIMESTAMP_FORMAT_SPEC", FC=%5u, TPF=%f\n",
328         next_period_boundary, ts, fc, getTicksPerFrame()
329         );
330     #endif
331
332     return (uint64_t)next_period_boundary;
333 }
334
335 bool TransmitStreamProcessor::canClientTransferFrames(unsigned int nbframes) {
336     // there has to be enough space to put the frames in
337     return m_data_buffer->getBufferSize() - m_data_buffer->getFrameCounter() > nbframes;
338 }
339
340
341 }
Note: See TracBrowser for help on using the browser.