BufferedInputFrameSeries.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef MEDIA_BUFFEREDINPUTFRAMESERIES_C_DEFINED
00039 #define MEDIA_BUFFEREDINPUTFRAMESERIES_C_DEFINED
00040
00041 #include "Media/BufferedInputFrameSeries.H"
00042
00043 #include "Util/log.H"
00044
00045 struct BufferedInputFrameSeries::Checkpoint
00046 {
00047 Checkpoint(int f, int q, int mq) : framenum(f), qsize(q), minqsize(mq) {}
00048
00049 int framenum;
00050 int qsize;
00051 int minqsize;
00052 };
00053
00054
00055 BufferedInputFrameSeries::
00056 BufferedInputFrameSeries(OptionManager& mgr, const size_t qsize,
00057 const bool forcergb)
00058 :
00059 ModelComponent(mgr, "Input Buffer", "BufferedInputFrameSeries"),
00060 itsSrc(new InputFrameSeries(mgr)),
00061 itsFrameSpec(),
00062 itsQ(qsize),
00063 itsInputDone(false),
00064 itsStop(false),
00065 itsNumFilled(),
00066 itsMinNumFilled(int(qsize)),
00067 itsFrameNum(0),
00068 itsCheckpoints(),
00069 itsForceRGB(forcergb)
00070 {
00071 if (0 != pthread_mutex_init(&itsQmut, NULL))
00072 LFATAL("pthread_mutex_init() failed");
00073
00074 this->addSubComponent(itsSrc);
00075 }
00076
00077
00078 GenericFrameSpec BufferedInputFrameSeries::peekFrameSpec() const
00079 {
00080 if (!this->started())
00081 LFATAL("must be start()-ed before peekFrameSpec()");
00082
00083 return itsFrameSpec;
00084 }
00085
00086
00087 GenericFrame BufferedInputFrameSeries::get(bool* did_underflow)
00088 {
00089 GenericFrame result;
00090
00091 const bool pop_ok = itsQ.pop_front(result);
00092
00093 if (pop_ok)
00094 {
00095 const int n = itsNumFilled.atomic_decr_return();
00096 if (n < itsMinNumFilled)
00097 itsMinNumFilled = n;
00098 }
00099
00100 if (did_underflow != 0)
00101
00102
00103 *did_underflow = (!pop_ok && !itsInputDone);
00104
00105 if (++itsFrameNum % 100 == 0)
00106 itsCheckpoints.push_back
00107 (Checkpoint(itsFrameNum, itsNumFilled.atomic_get(),
00108 itsMinNumFilled));
00109
00110 return result;
00111 }
00112
00113
00114 void BufferedInputFrameSeries::start2()
00115 {
00116 itsFrameSpec = itsSrc->peekFrameSpec();
00117
00118 if (0 != pthread_create(&itsFillThread, NULL, &c_fill,
00119 static_cast<void*>(this)))
00120 LFATAL("pthread_create() failed");
00121
00122 itsMinNumFilled = int(itsQ.size());
00123 itsFrameNum = 0;
00124 itsCheckpoints.clear();
00125 }
00126
00127
00128 void BufferedInputFrameSeries::stop1()
00129 {
00130 itsStop = true;
00131
00132 if (0 != pthread_join(itsFillThread, NULL))
00133 LERROR("pthread_join() failed");
00134
00135 for (std::list<Checkpoint>::const_iterator
00136 itr = itsCheckpoints.begin(), stop = itsCheckpoints.end();
00137 itr != stop; ++itr)
00138 {
00139 LINFO("checkpoint frame %06d - queue fill : %d/%d (min %d)",
00140 (*itr).framenum, (*itr).qsize, int(itsQ.size()),
00141 (*itr).minqsize);
00142 }
00143 }
00144
00145
00146 void* BufferedInputFrameSeries::c_fill(void* p)
00147 {
00148 try
00149 {
00150 BufferedInputFrameSeries* const b =
00151 static_cast<BufferedInputFrameSeries*>(p);
00152
00153 while (true)
00154 {
00155 if (b->itsStop)
00156 break;
00157
00158
00159 const FrameState is = b->itsSrc->updateNext();
00160 if (is == FRAME_COMPLETE)
00161 {
00162 b->itsInputDone = true;
00163 return NULL;
00164 }
00165
00166 GenericFrame f = b->itsSrc->readFrame();
00167 if (!f.initialized())
00168 {
00169 b->itsInputDone = true;
00170 return NULL;
00171 }
00172
00173
00174 if (b->itsForceRGB)
00175 f = GenericFrame(f.asRgb());
00176
00177
00178
00179 while (true)
00180 {
00181 if (b->itsStop)
00182 break;
00183
00184 if (b->itsQ.push_back(f) == true)
00185 {
00186 b->itsNumFilled.atomic_incr();
00187 break;
00188 }
00189
00190 usleep(20000);
00191 }
00192 }
00193 }
00194 catch (...)
00195 {
00196 REPORT_CURRENT_EXCEPTION;
00197 exit(1);
00198 }
00199
00200 return NULL;
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210 #endif // MEDIA_BUFFEREDINPUTFRAMESERIES_C_DEFINED