00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024
00025 #include <iostream>
00026
00027 #include <QDebug>
00028 #include <QMutex>
00029 #include <QWaitCondition>
00030 #include <QApplication>
00031
00032 #include <QVProcessingBlock>
00033 #include <QVVideoReaderBlock>
00034
00035 QVProcessingBlock::QVProcessingBlock(const QString name):QVPropertyContainer(name), numIterations(0), status(Running), triggerList(), minms(0)
00036 {
00037 qDebug() << "QVProcessingBlock::QVProcessingBlock(" << name << ")";
00038 Q_ASSERT_X(qvApp != NULL, "QVProcessingBlock::QVProcessingBlock()", "QVApplication object does not exist.");
00039 if (qvApp == NULL)
00040 {
00041 QString str = "QVProcessingBlock::QVProcessingBlock(): the QVProcessingBlock cannot be created before the QVApplication instance. Aborting.";
00042 std::cerr << qPrintable(str) << std::endl;
00043 exit(1);
00044 }
00045
00046 addProperty<int>("max block iterations", inputFlag | guiInvisible | internalProp, -1, "Maximal number of iterations to execute block");
00047 maxIterations = getPropertyValue<int>("max block iterations");
00048
00049 addProperty<bool>("stats enabled", inputFlag | guiInvisible | internalProp, TRUE, "Block CPU stats are enabled/disabled");
00050 statsEnabled = getPropertyValue<bool>("stats enabled");
00051
00052 if (statsEnabled)
00053 cpuStatControler = new QVStatControler();
00054
00055 if (statsEnabled)
00056 addProperty<QVStat>("cpu stats", outputFlag, cpuStatControler->value(), "CPU stats");
00057 else
00058 addProperty<QVStat>("cpu stats", outputFlag, QVStat(), "CPU stats");
00059
00060 informer.setParent(this);
00061
00062 addProperty<int>("stats printing frequency", inputFlag | guiInvisible | internalProp, 0, "Frequency to print CPU statistics (in iterations)");
00063 const int printStatsFrequency = getPropertyValue<int>("stats printing frequency");
00064
00065 if (printStatsFrequency > 0)
00066 setPrintStatsFrequency(printStatsFrequency);
00067
00068 qDebug() << "QVProcessingBlock::QVProcessingBlock(" << name << ") <- return";
00069 };
00070
00071 QVProcessingBlock::QVProcessingBlock(const QVProcessingBlock &other):QThread(), QVPropertyContainer(other), statsEnabled(other.statsEnabled), numIterations(other.numIterations),
00072 maxIterations(other.maxIterations), status(other.status), triggerList(other.triggerList), iterationTime(other.iterationTime), curms(other.curms),
00073 minms(other.minms)
00074 {
00075 if (statsEnabled) cpuStatControler = new QVStatControler();
00076 }
00077
00078 QVProcessingBlock::~QVProcessingBlock()
00079 {
00080 if (statsEnabled)
00081 delete cpuStatControler;
00082
00083 informer.setParent(0);
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 void QVProcessingBlock::run()
00097 {
00098 qDebug() << "QVProcessingBlock::run()";
00099
00100 while(status != Finished)
00101 {
00102 qDebug() << "Processing events in block " << qPrintable(getName());
00103
00104
00105
00106 qApp->processEvents();
00107
00108 qDebug() << "QVProcessingBlock::iterate(): iteration" << numIterations;
00109
00110
00111
00112
00113 usleep(1000);
00114
00115 switch (status)
00116 {
00117 case RunningOneStep:
00118 qDebug() << "QVProcessingBlock::iterate(): RunningOneStep";
00119 status = Paused;
00120
00121 case Running:
00122 iterationTime.start();
00123 foreach(QList<QVPropertyContainer *> level, slavesByLevel)
00124 foreach(QVPropertyContainer * slave, level)
00125 if(dynamic_cast<QVProcessingBlock *>(slave) != NULL) ((QVProcessingBlock *)slave)->blockIterate();
00126
00127 curms = iterationTime.elapsed();
00128 if(minms > curms)
00129 usleep(1000*(minms-curms));
00130
00131
00132
00133 break;
00134
00135 case Stopped:
00136
00137
00138
00139 readInputProperties();
00140 writeOutputProperties();
00141 usleep(100);
00142 break;
00143
00144
00145 case Paused:
00146 qDebug() << "QVProcessingBlock::iterate(): Paused";
00147 if(dynamic_cast<QVVideoReaderBlock*>(this) != NULL)
00148 {
00149
00150
00151 readInputProperties();
00152 writeOutputProperties();
00153 }
00154 usleep(100);
00155 break;
00156
00157 case Finished:
00158 qDebug() << "QVProcessingBlock::iterate(): Finished";
00159 break;
00160 }
00161
00162 if (maxIterations != -1 && numIterations >= maxIterations)
00163 finish();
00164
00165 qDebug() << "QVProcessingBlock::iterate() <- return";
00166 }
00167
00168
00169 foreach(QList<QVPropertyContainer *> level, slavesByLevel)
00170 foreach(QVPropertyContainer * slave, level)
00171 if(dynamic_cast<QVProcessingBlock *>(slave) != NULL) ((QVProcessingBlock *)slave)->moveToThread(qvApp->thread());
00172
00173 foreach(QList<QVPropertyContainer *> level, slavesByLevel)
00174 foreach(QVPropertyContainer * slave, level)
00175 if(dynamic_cast<QVProcessingBlock *>(slave) != NULL) {
00176 QMutexLocker locker(&qvApp->mutex);
00177 ((QVProcessingBlock *)slave)->unlink();
00178 }
00179
00180 qApp->processEvents();
00181
00182 qDebug() << "QVProcessingBlock::run() <- return";
00183 }
00184
00185 void QVProcessingBlock::blockIterate()
00186 {
00187 if (statsEnabled) cpuStatControler->step();
00188 emit startIteration();
00189 readInputProperties();
00190 timeFlag("System");
00191 iterate();
00192 if (statsEnabled) setPropertyValue<QVStat>("cpu stats", cpuStatControler->value());
00193 writeOutputProperties();
00194 numIterations++;
00195 emit endIteration(getId(), getIteration());
00196 }
00197