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 APPPSYCHO_PSYCHO_VIDEO_REPLAY_C_DEFINED
00039 #define APPPSYCHO_PSYCHO_VIDEO_REPLAY_C_DEFINED
00040
00041 #include "Component/ModelManager.H"
00042 #include "Image/DrawOps.H"
00043 #include "Image/Image.H"
00044 #include "Media/BufferedInputFrameSeries.H"
00045 #include "Psycho/PsychoDisplay.H"
00046 #include "Psycho/EyeTrackerConfigurator.H"
00047 #include "Psycho/EyeTracker.H"
00048 #include "Psycho/PsychoOpts.H"
00049 #include "Component/EventLog.H"
00050 #include "Component/ComponentOpts.H"
00051 #include "Raster/GenericFrame.H"
00052 #include "Util/FileUtil.H"
00053 #include "Util/StringUtil.H"
00054 #include "Util/Types.H"
00055 #include "Util/csignals.H"
00056 #include "Video/VideoFrame.H"
00057
00058 #include <SDL/SDL.h>
00059 #include <fstream>
00060 #include <iterator>
00061 #include <unistd.h>
00062 #include <vector>
00063
00064 namespace
00065 {
00066 struct Question
00067 {
00068 Question(const std::string& p, const std::string& a)
00069 : prompt(p), valid_answers(a)
00070 {}
00071
00072 std::string prompt;
00073 std::string valid_answers;
00074 };
00075
00076 void parseQuestions(const std::string& fname,
00077 std::vector<Question>& qv)
00078 {
00079 std::ifstream questions(fname.c_str());
00080
00081 if (!questions.is_open())
00082 LFATAL("couldn't open %s for reading", fname.c_str());
00083
00084 std::string line;
00085 while (std::getline(questions, line))
00086 {
00087 std::vector<std::string> toks;
00088 split(line, "|", std::back_inserter(toks));
00089
00090 if (toks.size() != 2)
00091 LFATAL("invalid line in %s (expected 2 tokens, got %d:\n%s",
00092 fname.c_str(), int(toks.size()), line.c_str());
00093
00094 qv.push_back(Question(toks[0], toks[1]));
00095 }
00096 }
00097
00098 void doQuestion(PsychoDisplay& d, const Question& q,
00099 std::ostream& dest)
00100 {
00101 while (d.checkForKey() != -1)
00102 { }
00103
00104 const PixRGB<byte> bgcol(0,0,0);
00105 const PixRGB<byte> msgcol(255,255,255);
00106 const PixRGB<byte> anscol(0,255,0);
00107
00108 const Dims dims = d.getDims();
00109
00110 Image<PixRGB<byte> > screen(d.getDims(), NO_INIT);
00111 screen.clear(bgcol);
00112
00113
00114 Point2D<int> p; p.i = dims.w() / 2 - (10 * q.prompt.length()) / 2;
00115 p.j = dims.h() / 2 - 10;
00116 if (p.i < 0)
00117 LFATAL("Text '%s' does not fit on screen!", q.prompt.c_str());
00118
00119 writeText(screen, p, q.prompt.c_str(), msgcol, bgcol);
00120
00121 d.displayImage(screen);
00122
00123
00124 int answer_id;
00125 std::string answer;
00126 while (true)
00127 {
00128 const int c = toupper(d.waitForKey());
00129
00130 std::string::size_type p = q.valid_answers.find(char(c));
00131
00132 if (p != q.valid_answers.npos)
00133 {
00134 answer_id = int(p);
00135 answer = char(c);
00136 break;
00137 }
00138 }
00139
00140 p.i = dims.w() / 2 - 5;
00141 p.j += 25;
00142 writeText(screen, p, answer.c_str(), anscol, bgcol);
00143
00144 d.displayImage(screen);
00145
00146
00147 dest << answer_id << " % '" << answer
00148 << "' of '" << q.valid_answers << "' ("
00149 << q.prompt << ")\n";
00150
00151 LINFO("A: %d Q: %s", answer_id, q.prompt.c_str());
00152
00153 usleep(750000);
00154
00155 d.SDLdisplay::clearScreen(bgcol);
00156
00157 usleep(250000);
00158 }
00159 }
00160
00161
00162
00163
00164 int submain(const int argc, char** argv)
00165 {
00166 MYLOGVERB = LOG_INFO;
00167
00168
00169 volatile int signum = 0;
00170 catchsignals(&signum);
00171
00172
00173 ModelManager manager("Psycho Video Replay");
00174
00175
00176
00177 nub::ref<BufferedInputFrameSeries> bifs
00178 (new BufferedInputFrameSeries(manager, 256));
00179 manager.addSubComponent(bifs);
00180
00181 nub::ref<PsychoDisplay> d(new PsychoDisplay(manager));
00182 manager.addSubComponent(d);
00183
00184 nub::ref<EyeTrackerConfigurator>
00185 etc(new EyeTrackerConfigurator(manager));
00186 manager.addSubComponent(etc);
00187
00188 nub::ref<EventLog> el(new EventLog(manager));
00189 manager.addSubComponent(el);
00190
00191 manager.exportOptions(MC_RECURSE);
00192
00193 manager.setOptionValString(&OPT_EventLogFileName, "psychodata.psy");
00194 manager.setOptionValString(&OPT_EyeTrackerType, "ISCAN");
00195
00196
00197 if (manager.parseCommandLine(argc, argv, "<outdir> <questions>",
00198 2, 2) == false)
00199 return(1);
00200
00201
00202 nub::soft_ref<EyeTracker> et = etc->getET();
00203 d->setEyeTracker(et);
00204 d->setEventLog(el);
00205 et->setEventLog(el);
00206
00207 const std::string outdir = manager.getExtraArg(0);
00208
00209 makeDirectory(outdir);
00210 el->setModelParamString("EventLogFileName",
00211 outdir + "/psychodata.psy");
00212
00213 std::ofstream answers((outdir + "/answers").c_str());
00214 if (!answers.is_open())
00215 LFATAL("couldn't open %s/answers for writing", outdir.c_str());
00216
00217 std::vector<Question> questions;
00218 parseQuestions(manager.getExtraArg(1), questions);
00219
00220
00221 manager.start();
00222
00223 const VideoFormat vf = bifs->peekFrameSpec().videoFormat;
00224
00225 d->setDesiredRefreshDelayUsec(1000000.0/59.94, 0.2F);
00226
00227
00228 d->clearScreen();
00229 d->displayISCANcalib();
00230 d->waitForKey();
00231
00232
00233 d->displayText("<SPACE> to calibrate; other key to skip");
00234 int c = d->waitForKey();
00235 if (c == ' ') d->displayEyeTrackerCalibration(3, 3, 3);
00236 d->clearScreen();
00237
00238
00239 sleep(1);
00240 sync();
00241
00242
00243 d->displayText("<SPACE> to start experiment");
00244 d->waitForKey();
00245
00246
00247 d->clearScreen();
00248 d->displayFixation();
00249
00250
00251 d->createVideoOverlay(vf);
00252
00253
00254 d->waitForKey();
00255 d->waitNextRequestedVsync(false, true);
00256 d->pushEvent("===== START =====");
00257
00258
00259 et->track(true);
00260
00261
00262 d->displayFixationBlink();
00263
00264
00265 int framenum = 0;
00266 bool did_underflow = false;
00267 while (true)
00268 {
00269 if (signum != 0)
00270 {
00271 LINFO("quitting because %s was caught", signame(signum));
00272 return -1;
00273 }
00274
00275
00276
00277
00278
00279 if (d->checkForKey() == '.')
00280 break;
00281
00282
00283 const GenericFrame frame = bifs->get(&did_underflow);
00284 if (!frame.initialized())
00285 break;
00286
00287
00288 d->displayVideoOverlay(frame.asVideo(), framenum,
00289 SDLdisplay::NEXT_FRAMETIME);
00290
00291 ++framenum;
00292 }
00293
00294 LINFO("displayed %d frames", framenum);
00295
00296 if (did_underflow)
00297 LERROR("input ended due to premature underflow");
00298
00299
00300
00301
00302
00303 d->destroyYUVoverlay();
00304 d->clearScreen();
00305 d->clearScreen();
00306
00307
00308 usleep(50000);
00309 et->track(false);
00310
00311 d->clearScreen();
00312
00313 for (size_t i = 0; i < questions.size(); ++i)
00314 doQuestion(*d, questions[i], answers);
00315
00316 d->displayText("Experiment complete. Thank you!");
00317 d->waitForKey();
00318
00319
00320 manager.stop();
00321
00322
00323 return 0;
00324 }
00325
00326 extern "C" int main(const int argc, char** argv)
00327 {
00328 try
00329 {
00330 return submain(argc, argv);
00331 }
00332 catch (...)
00333 {
00334 REPORT_CURRENT_EXCEPTION;
00335 }
00336
00337 return 1;
00338 }
00339
00340
00341
00342
00343
00344
00345
00346
00347 #endif // APPPSYCHO_PSYCHO_VIDEO_REPLAY_C_DEFINED