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

Revision 707, 10.3 kB (checked in by ppalmers, 15 years ago)

- code cleanup
- make transmit handler AMDTP compliant (don't send too much in advance)

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 bool StreamProcessor::init()
90 {
91     debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "enter...\n");
92
93     m_data_buffer->init();
94
95     return IsoStream::init();
96 }
97
98 /**
99  * Resets the frame counter, the xrun counter, the ports and the iso stream.
100  * @return true if reset succeeded
101  */
102 bool StreamProcessor::reset() {
103
104     debugOutput( DEBUG_LEVEL_VERBOSE, "Resetting...\n");
105
106     // reset the event buffer, discard all content
107     if (!m_data_buffer->reset()) {
108         debugFatal("Could not reset data buffer\n");
109         return false;
110     }
111
112     resetXrunCounter();
113
114     // loop over the ports to reset them
115     if (!PortManager::resetPorts()) {
116         debugFatal("Could not reset ports\n");
117         return false;
118     }
119
120     // reset the iso stream
121     if (!IsoStream::reset()) {
122         debugFatal("Could not reset isostream\n");
123         return false;
124     }
125     return true;
126
127 }
128
129 bool StreamProcessor::prepareForEnable(uint64_t time_to_enable_at) {
130     debugOutput(DEBUG_LEVEL_VERBOSE," StreamProcessor::prepareForEnable for (%p)\n",this);
131     debugOutput(DEBUG_LEVEL_VERBOSE,"  Now                   : %011u\n",m_handler->getCycleTimerTicks());
132     debugOutput(DEBUG_LEVEL_VERBOSE,"  Enable at             : %011u\n",time_to_enable_at);
133     m_data_buffer->dumpInfo();
134     return true;
135 }
136
137 bool StreamProcessor::prepareForDisable() {
138     debugOutput(DEBUG_LEVEL_VERBOSE," StreamProcessor::prepareForDisable for (%p)\n",this);
139     debugOutput(DEBUG_LEVEL_VERBOSE,"  Now                   : %011u\n",m_handler->getCycleTimerTicks());
140     m_data_buffer->dumpInfo();
141     return true;
142 }
143
144 bool StreamProcessor::prepare() {
145
146     debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing...\n");
147
148     // init the ports
149
150     if(!m_manager) {
151         debugFatal("Not attached to a manager!\n");
152         return -1;
153     }
154
155     m_nb_buffers=m_manager->getNbBuffers();
156     debugOutput( DEBUG_LEVEL_VERBOSE, "Setting m_nb_buffers  : %d\n", m_nb_buffers);
157
158     m_period=m_manager->getPeriodSize();
159     debugOutput( DEBUG_LEVEL_VERBOSE, "Setting m_period      : %d\n", m_period);
160
161     // loop over the ports to reset them
162     PortManager::preparePorts();
163
164     // reset the iso stream
165     IsoStream::prepare();
166
167     return true;
168
169 }
170
171 int StreamProcessor::getBufferFill() {
172 //     return m_data_buffer->getFrameCounter();
173     return m_data_buffer->getBufferFill();
174 }
175
176 uint64_t StreamProcessor::getTimeNow() {
177     return m_handler->getCycleTimerTicks();
178 }
179
180
181 bool StreamProcessor::isRunning() {
182     return m_running;
183 }
184
185 bool StreamProcessor::enable(uint64_t time_to_enable_at)  {
186     // FIXME: time_to_enable_at will be in 'time' not cycles
187     m_cycle_to_enable_at=time_to_enable_at;
188
189     if(!m_running) {
190             debugWarning("The StreamProcessor is not running yet, enable() might not be a good idea.\n");
191     }
192
193 #ifdef DEBUG
194     uint64_t now_cycles=CYCLE_TIMER_GET_CYCLES(m_handler->getCycleTimer());
195     const int64_t max=(int64_t)(CYCLES_PER_SECOND/2);
196
197     int64_t diff=(int64_t)m_cycle_to_enable_at-(int64_t)now_cycles;
198
199     if (diff > max) {
200         diff-=TICKS_PER_SECOND;
201     } else if (diff < -max) {
202         diff+=TICKS_PER_SECOND;
203     }
204
205     if (diff<0) {
206         debugWarning("Request to enable streamprocessor %lld cycles ago (now=%llu, cy=%llu).\n",
207             diff,now_cycles,time_to_enable_at);
208     }
209 #endif
210     m_data_buffer->enable();
211
212     m_disabled=false;
213     return true;
214 }
215
216 bool StreamProcessor::disable()  {
217     m_data_buffer->disable();
218     m_disabled=true;
219     return true;
220 }
221
222 bool StreamProcessor::setSyncSource(StreamProcessor *s) {
223     m_SyncSource=s;
224     return true;
225 }
226
227 float
228 StreamProcessor::getTicksPerFrame() {
229     if (m_data_buffer) {
230         float rate=m_data_buffer->getRate();
231         if (fabsf(m_ticks_per_frame - rate)>(m_ticks_per_frame*0.1)) {
232             debugWarning("TimestampedBuffer rate (%10.5f) more that 10%% off nominal (%10.5f)\n",rate,m_ticks_per_frame);
233             return m_ticks_per_frame;
234         }
235 //         return m_ticks_per_frame;
236         if (rate<0.0) debugError("rate < 0! (%f)\n",rate);
237        
238         return rate;
239     } else {
240         return 0.0;
241     }
242 }
243
244 int64_t StreamProcessor::getTimeUntilNextPeriodSignalUsecs() {
245     uint64_t time_at_period=getTimeAtPeriod();
246
247     // we delay the period signal with the sync delay
248     // this makes that the period signals lag a little compared to reality
249     // ISO buffering causes the packets to be received at max
250     // m_handler->getWakeupInterval() later than the time they were received.
251     // hence their payload is available this amount of time later. However, the
252     // period boundary is predicted based upon earlier samples, and therefore can
253     // pass before these packets are processed. Adding this extra term makes that
254     // the period boundary is signalled later
255     time_at_period = addTicks(time_at_period, m_SyncSource->getSyncDelay());
256
257     uint64_t cycle_timer=m_handler->getCycleTimerTicks();
258
259     // calculate the time until the next period
260     int32_t until_next=diffTicks(time_at_period,cycle_timer);
261
262     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "=> TAP=%11llu, CTR=%11llu, UTN=%11ld\n",
263         time_at_period, cycle_timer, until_next
264         );
265
266     // now convert to usecs
267     // don't use the mapping function because it only works
268     // for absolute times, not the relative time we are
269     // using here (which can also be negative).
270     return (int64_t)(((float)until_next) / TICKS_PER_USEC);
271 }
272
273 uint64_t StreamProcessor::getTimeAtPeriodUsecs() {
274     return (uint64_t)((float)getTimeAtPeriod() * TICKS_PER_USEC);
275 }
276
277 bool StreamProcessor::dropFrames(unsigned int nbframes) {
278     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "StreamProcessor::dropFrames(%d)\n", nbframes);
279     return m_data_buffer->dropFrames(nbframes);
280 }
281
282 /**
283  * Resets the xrun counter, in a atomic way. This
284  * is thread safe.
285  */
286 void StreamProcessor::resetXrunCounter() {
287     ZERO_ATOMIC((SInt32 *)&m_xruns);
288 }
289
290 void StreamProcessor::setVerboseLevel(int l) {
291     setDebugLevel(l);
292     IsoStream::setVerboseLevel(l);
293     PortManager::setVerboseLevel(l);
294 }
295
296 uint64_t ReceiveStreamProcessor::getTimeAtPeriod() {
297     ffado_timestamp_t next_period_boundary=m_data_buffer->getTimestampFromHead(m_period);
298
299     #ifdef DEBUG
300     ffado_timestamp_t ts;
301     signed int fc;
302    
303     m_data_buffer->getBufferTailTimestamp(&ts,&fc);
304
305     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "=> NPD="TIMESTAMP_FORMAT_SPEC", LTS="TIMESTAMP_FORMAT_SPEC", FC=%5u, TPF=%f\n",
306         next_period_boundary, ts, fc, getTicksPerFrame()
307         );
308     #endif
309
310     return (uint64_t)next_period_boundary;
311 }
312
313 bool ReceiveStreamProcessor::canClientTransferFrames(unsigned int nbframes) {
314     return m_data_buffer->getFrameCounter() >= (int) nbframes;
315 }
316
317 uint64_t TransmitStreamProcessor::getTimeAtPeriod() {
318     ffado_timestamp_t next_period_boundary=m_data_buffer->getTimestampFromTail((m_nb_buffers-1) * m_period);
319
320     #ifdef DEBUG
321     ffado_timestamp_t ts;
322     signed int fc;
323     m_data_buffer->getBufferTailTimestamp(&ts,&fc);
324
325     debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "=> NPD="TIMESTAMP_FORMAT_SPEC", LTS="TIMESTAMP_FORMAT_SPEC", FC=%5u, TPF=%f\n",
326         next_period_boundary, ts, fc, getTicksPerFrame()
327         );
328     #endif
329
330     return (uint64_t)next_period_boundary;
331 }
332
333 bool TransmitStreamProcessor::canClientTransferFrames(unsigned int nbframes) {
334     bool can_transfer;
335     // there has to be enough space to put the frames in
336     can_transfer = m_data_buffer->getBufferSize() - m_data_buffer->getFrameCounter() > nbframes;
337     // or the buffer is transparent
338     can_transfer |= m_data_buffer->isTransparent();
339     return can_transfer;
340 }
341
342
343 }
Note: See TracBrowser for help on using the browser.