root/branches/ppalmers-streaming/src/libstreaming/StreamProcessorManager.cpp

Revision 720, 31.0 kB (checked in by ppalmers, 13 years ago)

first working version of the reworked streaming code

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 "StreamProcessorManager.h"
25 #include "generic/StreamProcessor.h"
26 #include "generic/Port.h"
27 #include "util/cycletimer.h"
28
29 #include <errno.h>
30 #include <assert.h>
31
32 #define RUNNING_TIMEOUT_MSEC 4000
33 #define PREPARE_TIMEOUT_MSEC 4000
34 #define ENABLE_TIMEOUT_MSEC 4000
35
36 // allows to add some processing margin. This shifts the time
37 // at which the buffer is transfer()'ed, making things somewhat
38 // more robust. It should be noted though that shifting the transfer
39 // time to a later time instant also causes the xmit buffer fill to be
40 // lower on average.
41 #define FFADO_SIGNAL_DELAY_TICKS 3072
42
43 namespace Streaming {
44
45 IMPL_DEBUG_MODULE( StreamProcessorManager, StreamProcessorManager, DEBUG_LEVEL_VERBOSE );
46
47 StreamProcessorManager::StreamProcessorManager(unsigned int period, unsigned int framerate, unsigned int nb_buffers)
48     : m_is_slave( false )
49     , m_SyncSource(NULL)
50     , m_nb_buffers(nb_buffers)
51     , m_period(period)
52     , m_nominal_framerate ( framerate )
53     , m_xruns(0)
54     , m_isoManager(0)
55     , m_nbperiods(0)
56 {
57     addOption(Util::OptionContainer::Option("slaveMode",false));
58 }
59
60 StreamProcessorManager::~StreamProcessorManager() {
61     if (m_isoManager) delete m_isoManager;
62 }
63
64 /**
65  * Registers \ref processor with this manager.
66  *
67  * also registers it with the isohandlermanager
68  *
69  * be sure to call isohandlermanager->init() first!
70  * and be sure that the processors are also ->init()'ed
71  *
72  * @param processor
73  * @return true if successfull
74  */
75 bool StreamProcessorManager::registerProcessor(StreamProcessor *processor)
76 {
77     debugOutput( DEBUG_LEVEL_VERBOSE, "Registering processor (%p)\n",processor);
78     assert(processor);
79     assert(m_isoManager);
80
81     if (processor->getType() == StreamProcessor::ePT_Receive) {
82         processor->setVerboseLevel(getDebugLevel()); // inherit debug level
83
84         m_ReceiveProcessors.push_back(processor);
85         processor->setManager(this);
86         return true;
87     }
88
89     if (processor->getType() == StreamProcessor::ePT_Transmit) {
90         processor->setVerboseLevel(getDebugLevel()); // inherit debug level
91
92         m_TransmitProcessors.push_back(processor);
93         processor->setManager(this);
94         return true;
95     }
96
97     debugFatal("Unsupported processor type!\n");
98     return false;
99 }
100
101 bool StreamProcessorManager::unregisterProcessor(StreamProcessor *processor)
102 {
103     debugOutput( DEBUG_LEVEL_VERBOSE, "Unregistering processor (%p)\n",processor);
104     assert(processor);
105
106     if (processor->getType()==StreamProcessor::ePT_Receive) {
107
108         for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin();
109               it != m_ReceiveProcessors.end();
110               ++it )
111         {
112             if ( *it == processor ) {
113                 m_ReceiveProcessors.erase(it);
114                 processor->clearManager();
115                 if(!m_isoManager->unregisterStream(processor)) {
116                     debugOutput(DEBUG_LEVEL_VERBOSE,"Could not unregister receive stream processor from the Iso manager\n");
117                     return false;
118                 }
119                 return true;
120             }
121         }
122     }
123
124     if (processor->getType()==StreamProcessor::ePT_Transmit) {
125         for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin();
126               it != m_TransmitProcessors.end();
127               ++it )
128         {
129             if ( *it == processor ) {
130                 m_TransmitProcessors.erase(it);
131                 processor->clearManager();
132                 if(!m_isoManager->unregisterStream(processor)) {
133                     debugOutput(DEBUG_LEVEL_VERBOSE,"Could not unregister transmit stream processor from the Iso manager\n");
134                     return false;
135                 }
136                 return true;
137             }
138         }
139     }
140
141     debugFatal("Processor (%p) not found!\n",processor);
142     return false; //not found
143 }
144
145 bool StreamProcessorManager::setSyncSource(StreamProcessor *s) {
146     debugOutput( DEBUG_LEVEL_VERBOSE, "Setting sync source to (%p)\n", s);
147     m_SyncSource=s;
148     return true;
149 }
150
151 bool StreamProcessorManager::init()
152 {
153     debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n");
154     m_isoManager = new IsoHandlerManager(m_thread_realtime, m_thread_priority + 1);
155     if(!m_isoManager) {
156         debugFatal("Could not create IsoHandlerManager\n");
157         return false;
158     }
159     m_isoManager->setVerboseLevel(getDebugLevel());
160     if(!m_isoManager->init()) {
161         debugFatal("Could not initialize IsoHandlerManager\n");
162         return false;
163     }
164
165     m_xrun_happened=false;
166     return true;
167 }
168
169 bool StreamProcessorManager::prepare() {
170
171     debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing...\n");
172
173     m_is_slave=false;
174     if(!getOption("slaveMode", m_is_slave)) {
175         debugWarning("Could not retrieve slaveMode parameter, defaulting to false\n");
176     }
177
178     // if no sync source is set, select one here
179     if(m_SyncSource == NULL) {
180        debugWarning("Sync Source is not set. Defaulting to first StreamProcessor.\n");
181     }
182
183     // FIXME: put into separate method
184     for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin();
185           it != m_ReceiveProcessors.end();
186           ++it )
187     {
188         if(m_SyncSource == NULL) {
189             debugWarning(" => Sync Source is %p.\n", *it);
190             m_SyncSource = *it;
191         }
192     }
193     for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin();
194           it != m_TransmitProcessors.end();
195           ++it )
196     {
197         if(m_SyncSource == NULL) {
198             debugWarning(" => Sync Source is %p.\n", *it);
199             m_SyncSource = *it;
200         }
201     }
202
203     // now do the actual preparation of the SP's
204     debugOutput( DEBUG_LEVEL_VERBOSE, "Prepare Receive processors...\n");
205     for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin();
206         it != m_ReceiveProcessors.end();
207         ++it ) {
208
209         if(!(*it)->setOption("slaveMode", m_is_slave)) {
210             debugOutput(DEBUG_LEVEL_VERBOSE, " note: could not set slaveMode option for (%p)...\n",(*it));
211         }
212
213         if(!(*it)->prepare()) {
214             debugFatal(  " could not prepare (%p)...\n",(*it));
215             return false;
216         }
217     }
218     debugOutput( DEBUG_LEVEL_VERBOSE, "Prepare Transmit processors...\n");
219     for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin();
220         it != m_TransmitProcessors.end();
221         ++it ) {
222         if(!(*it)->setOption("slaveMode", m_is_slave)) {
223             debugOutput(DEBUG_LEVEL_VERBOSE, " note: could not set slaveMode option for (%p)...\n",(*it));
224         }
225         if(!(*it)->prepare()) {
226             debugFatal( " could not prepare (%p)...\n",(*it));
227             return false;
228         }
229     }
230
231     // if there are no stream processors registered,
232     // fail
233     if (m_ReceiveProcessors.size() + m_TransmitProcessors.size() == 0) {
234         debugFatal("No stream processors registered, can't do anything usefull\n");
235         return false;
236     }
237     return true;
238 }
239
240 bool StreamProcessorManager::startDryRunning() {
241     debugOutput( DEBUG_LEVEL_VERBOSE, "Waiting for StreamProcessor streams to start dry-running...\n");
242     for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin();
243             it != m_ReceiveProcessors.end();
244             ++it ) {
245         if(!(*it)->startDryRunning(-1)) {
246             debugError("Could not put SP %p into the dry-running state\n", *it);
247             return false;
248         }
249     }
250     for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin();
251             it != m_TransmitProcessors.end();
252             ++it ) {
253         if(!(*it)->startDryRunning(-1)) {
254             debugError("Could not put SP %p into the dry-running state\n", *it);
255             return false;
256         }
257     }
258     debugOutput( DEBUG_LEVEL_VERBOSE, " StreamProcessor streams dry-running...\n");
259     return true;
260 }
261
262 bool StreamProcessorManager::syncStartAll() {
263     // figure out when to get the SP's running.
264     // the xmit SP's should also know the base timestamp
265     // streams should be aligned here
266
267     // now find out how long we have to delay the wait operation such that
268     // the received frames will all be presented to the SP
269     debugOutput( DEBUG_LEVEL_VERBOSE, "Finding minimal sync delay...\n");
270     int max_of_min_delay=0;
271     int min_delay=0;
272     for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin();
273             it != m_ReceiveProcessors.end();
274             ++it ) {
275         min_delay=(*it)->getMaxFrameLatency();
276         if(min_delay>max_of_min_delay) max_of_min_delay=min_delay;
277     }
278
279     // add some processing margin. This only shifts the time
280     // at which the buffer is transfer()'ed. This makes things somewhat
281     // more robust. It should be noted though that shifting the transfer
282     // time to a later time instant also causes the xmit buffer fill to be
283     // lower on average.
284     max_of_min_delay += FFADO_SIGNAL_DELAY_TICKS;
285     debugOutput( DEBUG_LEVEL_VERBOSE, " sync delay = %d ticks (%03us %04uc %04ut)...\n",
286         max_of_min_delay,
287         (unsigned int)TICKS_TO_SECS(max_of_min_delay),
288         (unsigned int)TICKS_TO_CYCLES(max_of_min_delay),
289         (unsigned int)TICKS_TO_OFFSET(max_of_min_delay));
290     m_SyncSource->setSyncDelay(max_of_min_delay);
291
292     //STEP X: when we implement such a function, we can wait for a signal from the devices that they
293     //        have aquired lock
294     //debugOutput( DEBUG_LEVEL_VERBOSE, "Waiting for device(s) to indicate clock sync lock...\n");
295     //sleep(2); // FIXME: be smarter here
296
297     // make sure that we are dry-running long enough for the
298     // DLL to have a decent sync (FIXME: does the DLL get updated when dry-running)?
299     debugOutput( DEBUG_LEVEL_VERBOSE, "Waiting for sync...\n");
300     int nb_sync_runs=20;
301     int64_t time_till_next_period;
302     while(nb_sync_runs--) { // or while not sync-ed?
303         // check if we were waked up too soon
304         time_till_next_period=m_SyncSource->getTimeUntilNextPeriodSignalUsecs();
305         debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "waiting for %d usecs...\n", time_till_next_period);
306         if(time_till_next_period > 0) {
307             // wait for the period
308             usleep(time_till_next_period);
309         }
310     }
311
312     debugOutput( DEBUG_LEVEL_VERBOSE, "Propagate sync info...\n");
313     // FIXME: in the SPM it would be nice to have system time instead of
314     //        1394 time
315
316     // we now should have decent sync info on the sync source
317     // determine a point in time where the system should start
318     // figure out where we are now
319     uint64_t time_of_first_sample = m_SyncSource->getTimeAtPeriod();
320     debugOutput( DEBUG_LEVEL_VERBOSE, " sync at TS=%011llu (%03us %04uc %04ut)...\n",
321         time_of_first_sample,
322         (unsigned int)TICKS_TO_SECS(time_of_first_sample),
323         (unsigned int)TICKS_TO_CYCLES(time_of_first_sample),
324         (unsigned int)TICKS_TO_OFFSET(time_of_first_sample));
325
326     #define CYCLES_FOR_STARTUP 200
327     // start wet-running in CYCLES_FOR_STARTUP cycles
328     // this is the time window we have to setup all SP's such that they
329     // can start wet-running correctly.
330     time_of_first_sample = addTicks(time_of_first_sample,
331                                     CYCLES_FOR_STARTUP * TICKS_PER_CYCLE);
332
333     debugOutput( DEBUG_LEVEL_VERBOSE, "  => first sample at TS=%011llu (%03us %04uc %04ut)...\n",
334         time_of_first_sample,
335         (unsigned int)TICKS_TO_SECS(time_of_first_sample),
336         (unsigned int)TICKS_TO_CYCLES(time_of_first_sample),
337         (unsigned int)TICKS_TO_OFFSET(time_of_first_sample));
338
339     // we should start wet-running the transmit SP's some cycles in advance
340     // such that we know it is wet-running when it should output its first sample
341     #define PRESTART_CYCLES_FOR_XMIT 20
342     uint64_t time_to_start_xmit = substractTicks(time_of_first_sample,
343                                                  PRESTART_CYCLES_FOR_XMIT * TICKS_PER_CYCLE);
344
345     #define PRESTART_CYCLES_FOR_RECV 0
346     uint64_t time_to_start_recv = substractTicks(time_of_first_sample,
347                                                  PRESTART_CYCLES_FOR_RECV * TICKS_PER_CYCLE);
348     debugOutput( DEBUG_LEVEL_VERBOSE, "  => xmit starts at  TS=%011llu (%03us %04uc %04ut)...\n",
349         time_to_start_xmit,
350         (unsigned int)TICKS_TO_SECS(time_to_start_xmit),
351         (unsigned int)TICKS_TO_CYCLES(time_to_start_xmit),
352         (unsigned int)TICKS_TO_OFFSET(time_to_start_xmit));
353     debugOutput( DEBUG_LEVEL_VERBOSE, "  => recv starts at  TS=%011llu (%03us %04uc %04ut)...\n",
354         time_to_start_recv,
355         (unsigned int)TICKS_TO_SECS(time_to_start_recv),
356         (unsigned int)TICKS_TO_CYCLES(time_to_start_recv),
357         (unsigned int)TICKS_TO_OFFSET(time_to_start_recv));
358
359     // at this point the buffer head timestamp of the transmit buffers can be set
360     // this is the presentation time of the first sample in the buffer
361     for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin();
362           it != m_TransmitProcessors.end();
363           ++it ) {
364         (*it)->setBufferHeadTimestamp(time_of_first_sample);
365     }
366
367     // STEP X: switch SP's over to the running state
368     for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin();
369           it != m_ReceiveProcessors.end();
370           ++it ) {
371         if(!(*it)->scheduleStartRunning(time_to_start_recv)) {
372             debugError("%p->scheduleStartRunning(%11llu) failed\n", *it, time_to_start_recv);
373             return false;
374         }
375     }
376     for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin();
377           it != m_TransmitProcessors.end();
378           ++it ) {
379         if(!(*it)->scheduleStartRunning(time_to_start_xmit)) {
380             debugError("%p->scheduleStartRunning(%11llu) failed\n", *it, time_to_start_xmit);
381             return false;
382         }
383     }
384     // wait for the syncsource to start running.
385     // that will block the waitForPeriod call until everyone has started (theoretically)
386     int cnt = CYCLES_FOR_STARTUP * 2; // by then it should have started
387     while (!m_SyncSource->isRunning() && cnt) {
388         usleep(125);
389         cnt--;
390     }
391     if(cnt==0) {
392         debugOutput(DEBUG_LEVEL_VERBOSE, " Timeout waiting for the SyncSource to get started\n");
393         return false;
394     }
395     debugOutput( DEBUG_LEVEL_VERBOSE, " StreamProcessor streams running...\n");
396     return true;
397 }
398
399 bool StreamProcessorManager::start() {
400     debugOutput( DEBUG_LEVEL_VERBOSE, "Starting Processors...\n");
401     assert(m_isoManager);
402
403     debugOutput( DEBUG_LEVEL_VERBOSE, "Creating handlers for the StreamProcessors...\n");
404     debugOutput( DEBUG_LEVEL_VERBOSE, " Receive processors...\n");
405     for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin();
406           it != m_ReceiveProcessors.end();
407           ++it )
408     {
409         if (!m_isoManager->registerStream(*it)) {
410             debugOutput(DEBUG_LEVEL_VERBOSE,"Could not register receive stream processor (%p) with the Iso manager\n",*it);
411             return false;
412         }
413     }
414     debugOutput( DEBUG_LEVEL_VERBOSE, " Transmit processors...\n");
415     for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin();
416           it != m_TransmitProcessors.end();
417           ++it )
418     {
419         if (!m_isoManager->registerStream(*it)) {
420             debugOutput(DEBUG_LEVEL_VERBOSE,"Could not register transmit stream processor (%p) with the Iso manager\n",*it);
421             return false;
422         }
423     }
424
425     debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing IsoHandlerManager...\n");
426     if (!m_isoManager->prepare()) {
427         debugFatal("Could not prepare isoManager\n");
428         return false;
429     }
430
431     debugOutput( DEBUG_LEVEL_VERBOSE, "Starting IsoHandlers...\n");
432     if (!m_isoManager->startHandlers(-1)) {
433         debugFatal("Could not start handlers...\n");
434         return false;
435     }
436
437     // put all SP's into dry-running state
438     if (!startDryRunning()) {
439         debugFatal("Could not put SP's in dry-running state\n");
440         return false;
441     }
442
443     // start all SP's synchonized
444     if (!syncStartAll()) {
445         debugFatal("Could not syncStartAll...\n");
446         return false;
447     }
448
449     // dump the iso stream information when in verbose mode
450     if(getDebugLevel()>=DEBUG_LEVEL_VERBOSE) {
451         m_isoManager->dumpInfo();
452     }
453
454     return true;
455 }
456
457 bool StreamProcessorManager::stop() {
458     debugOutput( DEBUG_LEVEL_VERBOSE, "Stopping...\n");
459     assert(m_isoManager);
460
461     debugOutput( DEBUG_LEVEL_VERBOSE, "Waiting for all StreamProcessors to prepare to stop...\n");
462     // Most stream processors can just stop without special treatment.  However, some
463     // (like the MOTU) need to do a few things before it's safe to turn off the iso
464     // handling.
465     for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin();
466           it != m_ReceiveProcessors.end();
467           ++it ) {
468         if(!(*it)->stop()) {
469             debugError("Could not stop SP %p", (*it));
470         }
471     }
472     for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin();
473           it != m_TransmitProcessors.end();
474           ++it ) {
475         if(!(*it)->stop()) {
476             debugError("Could not stop SP %p", (*it));
477         }
478     }
479
480     debugOutput( DEBUG_LEVEL_VERBOSE, "Stopping handlers...\n");
481     if(!m_isoManager->stopHandlers()) {
482        debugFatal("Could not stop ISO handlers\n");
483        return false;
484     }
485
486     debugOutput( DEBUG_LEVEL_VERBOSE, "Unregistering processors from handlers...\n");
487     // now unregister all streams from iso manager
488     debugOutput( DEBUG_LEVEL_VERBOSE, " Receive processors...\n");
489     for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin();
490           it != m_ReceiveProcessors.end();
491           ++it ) {
492         if (!m_isoManager->unregisterStream(*it)) {
493             debugOutput(DEBUG_LEVEL_VERBOSE,"Could not unregister receive stream processor (%p) from the Iso manager\n",*it);
494             return false;
495         }
496     }
497     debugOutput( DEBUG_LEVEL_VERBOSE, " Transmit processors...\n");
498     for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin();
499           it != m_TransmitProcessors.end();
500           ++it ) {
501         if (!m_isoManager->unregisterStream(*it)) {
502             debugOutput(DEBUG_LEVEL_VERBOSE,"Could not unregister transmit stream processor (%p) from the Iso manager\n",*it);
503             return false;
504         }
505     }
506
507     return true;
508 }
509
510 /**
511  * Called upon Xrun events. This brings all StreamProcessors back
512  * into their starting state, and then carries on streaming. This should
513  * have the same effect as restarting the whole thing.
514  *
515  * @return true if successful, false otherwise
516  */
517 bool StreamProcessorManager::handleXrun() {
518
519     debugOutput( DEBUG_LEVEL_VERBOSE, "Handling Xrun ...\n");
520
521     dumpInfo();
522
523     /*
524      * Reset means:
525      * 1) Disabling the SP's, so that they don't process any packets
526      *    note: the isomanager does keep on delivering/requesting them
527      * 2) Bringing all buffers & streamprocessors into a know state
528      *    - Clear all capture buffers
529      *    - Put nb_periods*period_size of null frames into the playback buffers
530      * 3) Re-enable the SP's
531      */
532
533     // put all SP's back into dry-running state
534     if (!startDryRunning()) {
535         debugFatal("Could not put SP's in dry-running state\n");
536         return false;
537     }
538
539     debugOutput( DEBUG_LEVEL_VERBOSE, "Restarting StreamProcessors...\n");
540     // start all SP's synchonized
541     if (!syncStartAll()) {
542         debugFatal("Could not syncStartAll...\n");
543         return false;
544     }
545
546     debugOutput( DEBUG_LEVEL_VERBOSE, "Xrun handled...\n");
547
548     return true;
549 }
550
551 /**
552  * @brief Waits until the next period of samples is ready
553  *
554  * This function does not return until a full period of samples is (or should be)
555  * ready to be transferred.
556  *
557  * @return true if the period is ready, false if an xrun occurred
558  */
559 bool StreamProcessorManager::waitForPeriod() {
560     int time_till_next_period;
561     bool xrun_occurred=false;
562
563     debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "enter...\n");
564
565     assert(m_SyncSource);
566
567     time_till_next_period=m_SyncSource->getTimeUntilNextPeriodSignalUsecs();
568
569     while(time_till_next_period > 0) {
570         debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "waiting for %d usecs...\n", time_till_next_period);
571
572         // wait for the period
573         usleep(time_till_next_period);
574
575         // check for underruns on the ISO side,
576         // those should make us bail out of the wait loop
577         for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin();
578             it != m_ReceiveProcessors.end();
579             ++it ) {
580             // a xrun has occurred on the Iso side
581             xrun_occurred |= (*it)->xrunOccurred();
582         }
583         for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin();
584             it != m_TransmitProcessors.end();
585             ++it ) {
586             // a xrun has occurred on the Iso side
587             xrun_occurred |= (*it)->xrunOccurred();
588         }
589
590         if(xrun_occurred) break;
591
592         // check if we were waked up too soon
593         time_till_next_period=m_SyncSource->getTimeUntilNextPeriodSignalUsecs();
594     }
595
596     debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "delayed for %d usecs...\n", time_till_next_period);
597
598     // this is to notify the client of the delay
599     // that we introduced
600     m_delayed_usecs = -time_till_next_period;
601
602     // we save the 'ideal' time of the transfer at this point,
603     // because we can have interleaved read - process - write
604     // cycles making that we modify a receiving stream's buffer
605     // before we get to writing.
606     // NOTE: before waitForPeriod() is called again, both the transmit
607     //       and the receive processors should have done their transfer.
608     m_time_of_transfer = m_SyncSource->getTimeAtPeriod();
609     debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "transfer at %llu ticks...\n",
610         m_time_of_transfer);
611
612 #ifdef DEBUG
613     int rcv_bf=0, xmt_bf=0;
614     for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin();
615         it != m_ReceiveProcessors.end();
616         ++it ) {
617         rcv_bf = (*it)->getBufferFill();
618     }
619     for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin();
620         it != m_TransmitProcessors.end();
621         ++it ) {
622         xmt_bf = (*it)->getBufferFill();
623     }
624     debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "XF at %011llu ticks, RBF=%d, XBF=%d, SUM=%d...\n",
625         m_time_of_transfer, rcv_bf, xmt_bf, rcv_bf+xmt_bf);
626
627 #endif
628
629     xrun_occurred=false;
630
631     // check if xruns occurred on the Iso side.
632     // also check if xruns will occur should we transfer() now
633
634     for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin();
635           it != m_ReceiveProcessors.end();
636           ++it ) {
637         // a xrun has occurred on the Iso side
638         xrun_occurred |= (*it)->xrunOccurred();
639
640         // if this is true, a xrun will occur
641         xrun_occurred |= !((*it)->canClientTransferFrames(m_period));
642
643 #ifdef DEBUG
644         if ((*it)->xrunOccurred()) {
645             debugWarning("Xrun on RECV SP %p due to ISO xrun\n",*it);
646             (*it)->dumpInfo();
647         }
648         if (!((*it)->canClientTransferFrames(m_period))) {
649             debugWarning("Xrun on RECV SP %p due to buffer xrun\n",*it);
650             (*it)->dumpInfo();
651         }
652 #endif
653
654     }
655     for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin();
656           it != m_TransmitProcessors.end();
657           ++it ) {
658         // a xrun has occurred on the Iso side
659         xrun_occurred |= (*it)->xrunOccurred();
660
661         // if this is true, a xrun will occur
662         xrun_occurred |= !((*it)->canClientTransferFrames(m_period));
663
664 #ifdef DEBUG
665         if ((*it)->xrunOccurred()) {
666             debugWarning("Xrun on XMIT SP %p due to ISO xrun\n",*it);
667         }
668         if (!((*it)->canClientTransferFrames(m_period))) {
669             debugWarning("Xrun on XMIT SP %p due to buffer xrun\n",*it);
670         }
671 #endif
672     }
673
674     m_nbperiods++;
675
676     // now we can signal the client that we are (should be) ready
677     return !xrun_occurred;
678 }
679
680 /**
681  * @brief Transfer one period of frames for both receive and transmit StreamProcessors
682  *
683  * Transfers one period of frames from the client side to the Iso side and vice versa.
684  *
685  * @return true if successful, false otherwise (indicates xrun).
686  */
687 bool StreamProcessorManager::transfer() {
688
689     debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Transferring period...\n");
690     bool retval=true;
691     retval &= transfer(StreamProcessor::ePT_Receive);
692     retval &= transfer(StreamProcessor::ePT_Transmit);
693     return retval;
694 }
695
696 /**
697  * @brief Transfer one period of frames for either the receive or transmit StreamProcessors
698  *
699  * Transfers one period of frames from the client side to the Iso side or vice versa.
700  *
701  * @param t The processor type to tranfer for (receive or transmit)
702  * @return true if successful, false otherwise (indicates xrun).
703  */
704
705 bool StreamProcessorManager::transfer(enum StreamProcessor::eProcessorType t) {
706     debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "transfer(%d) at TS=%011llu (%03us %04uc %04ut)...\n",
707         t, m_time_of_transfer,
708         (unsigned int)TICKS_TO_SECS(m_time_of_transfer),
709         (unsigned int)TICKS_TO_CYCLES(m_time_of_transfer),
710         (unsigned int)TICKS_TO_OFFSET(m_time_of_transfer));
711
712     bool retval = true;
713     // a static cast could make sure that there is no performance
714     // penalty for the virtual functions (to be checked)
715     if (t==StreamProcessor::ePT_Receive) {
716         for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin();
717                 it != m_ReceiveProcessors.end();
718                 ++it ) {
719             if(!(*it)->getFrames(m_period, m_time_of_transfer)) {
720                     debugWarning("could not getFrames(%u, %11llu) from stream processor (%p)\n",
721                             m_period, m_time_of_transfer,*it);
722                 retval &= false; // buffer underrun
723             }
724         }
725     } else {
726         // FIXME: in the SPM it would be nice to have system time instead of
727         //        1394 time
728         float rate = m_SyncSource->getTicksPerFrame();
729         int64_t one_ringbuffer_in_ticks=(int64_t)(((float)(m_nb_buffers * m_period)) * rate);
730
731         // the data we are putting into the buffer is intended to be transmitted
732         // one ringbuffer size after it has been received
733         int64_t transmit_timestamp = addTicks(m_time_of_transfer, one_ringbuffer_in_ticks);
734
735         for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin();
736                 it != m_TransmitProcessors.end();
737                 ++it ) {
738             // FIXME: in the SPM it would be nice to have system time instead of
739             //        1394 time
740             if(!(*it)->putFrames(m_period, transmit_timestamp)) {
741                 debugWarning("could not putFrames(%u,%llu) to stream processor (%p)\n",
742                         m_period, transmit_timestamp, *it);
743                 retval &= false; // buffer underrun
744             }
745         }
746     }
747     return retval;
748 }
749
750 void StreamProcessorManager::dumpInfo() {
751     debugOutputShort( DEBUG_LEVEL_NORMAL, "----------------------------------------------------\n");
752     debugOutputShort( DEBUG_LEVEL_NORMAL, "Dumping StreamProcessorManager information...\n");
753     debugOutputShort( DEBUG_LEVEL_NORMAL, "Period count: %6d\n", m_nbperiods);
754
755     debugOutputShort( DEBUG_LEVEL_NORMAL, " Receive processors...\n");
756     for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin();
757         it != m_ReceiveProcessors.end();
758         ++it ) {
759         (*it)->dumpInfo();
760     }
761
762     debugOutputShort( DEBUG_LEVEL_NORMAL, " Transmit processors...\n");
763     for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin();
764         it != m_TransmitProcessors.end();
765         ++it ) {
766         (*it)->dumpInfo();
767     }
768
769     debugOutputShort( DEBUG_LEVEL_NORMAL, "Iso handler info:\n");
770     m_isoManager->dumpInfo();
771     debugOutputShort( DEBUG_LEVEL_NORMAL, "----------------------------------------------------\n");
772
773 }
774
775 void StreamProcessorManager::setVerboseLevel(int l) {
776     setDebugLevel(l);
777
778     if (m_isoManager) m_isoManager->setVerboseLevel(l);
779
780     debugOutput( DEBUG_LEVEL_VERBOSE, " Receive processors...\n");
781     for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin();
782         it != m_ReceiveProcessors.end();
783         ++it ) {
784         (*it)->setVerboseLevel(l);
785     }
786
787     debugOutput( DEBUG_LEVEL_VERBOSE, " Transmit processors...\n");
788     for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin();
789         it != m_TransmitProcessors.end();
790         ++it ) {
791         (*it)->setVerboseLevel(l);
792     }
793 }
794
795
796 int StreamProcessorManager::getPortCount(enum Port::E_PortType type, enum Port::E_Direction direction) {
797     int count=0;
798
799     if (direction == Port::E_Capture) {
800         for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin();
801             it != m_ReceiveProcessors.end();
802             ++it ) {
803             count += (*it)->getPortCount(type);
804         }
805     } else {
806         for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin();
807             it != m_TransmitProcessors.end();
808             ++it ) {
809             count += (*it)->getPortCount(type);
810         }
811     }
812     return count;
813 }
814
815 int StreamProcessorManager::getPortCount(enum Port::E_Direction direction) {
816     int count=0;
817
818     if (direction == Port::E_Capture) {
819         for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin();
820             it != m_ReceiveProcessors.end();
821             ++it ) {
822             count += (*it)->getPortCount();
823         }
824     } else {
825         for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin();
826             it != m_TransmitProcessors.end();
827             ++it ) {
828             count += (*it)->getPortCount();
829         }
830     }
831     return count;
832 }
833
834 // TODO: implement a port map here, instead of the loop
835
836 Port* StreamProcessorManager::getPortByIndex(int idx, enum Port::E_Direction direction) {
837     int count=0;
838     int prevcount=0;
839
840     if (direction == Port::E_Capture) {
841         for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin();
842             it != m_ReceiveProcessors.end();
843             ++it ) {
844             count += (*it)->getPortCount();
845             if (count > idx) {
846                 return (*it)->getPortAtIdx(idx-prevcount);
847             }
848             prevcount=count;
849         }
850     } else {
851         for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin();
852             it != m_TransmitProcessors.end();
853             ++it ) {
854             count += (*it)->getPortCount();
855             if (count > idx) {
856                 return (*it)->getPortAtIdx(idx-prevcount);
857             }
858             prevcount=count;
859         }
860     }
861     return NULL;
862 }
863
864 bool StreamProcessorManager::setThreadParameters(bool rt, int priority) {
865     m_thread_realtime=rt;
866     m_thread_priority=priority;
867     return true;
868 }
869
870
871 } // end of namespace
Note: See TracBrowser for help on using the browser.