app-iplayer.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 APPMEDIA_APP_STREAM_C_DEFINED
00039 #define APPMEDIA_APP_STREAM_C_DEFINED
00040
00041 #define INIT_BUFF_LENGTH 100
00042
00043 #include "Component/ModelManager.H"
00044 #include "Media/Streamer.H"
00045 #include "Util/StringConversions.H"
00046 #include "Devices/KeyBoard.H"
00047 #include "Raster/Raster.H"
00048
00049 #include <iostream>
00050 #include <fstream>
00051 #include <deque>
00052 #include <vector>
00053
00054 class iPlayer : public Streamer
00055 {
00056 public:
00057 iPlayer()
00058 :
00059 Streamer("iPlayer"),
00060 itsOutPrefix("iplayer-output"),
00061 itsKeyBoard(),
00062 itsBuffer(),
00063 itsFrameCount(-1),
00064 itsStart(),
00065 itsEnd(),
00066 itsEdits(),
00067 itsOutFile(),
00068 itsBuffLength(INIT_BUFF_LENGTH)
00069 { };
00070
00071 ~iPlayer()
00072 {
00073 if ( (itsStart.size() > 0) && (itsEnd.size() > 0) )
00074 {
00075 if (itsStart.size() < itsEnd.size())
00076 itsEnd.pop_back();
00077 else if (itsStart.size() > itsEnd.size())
00078 itsStart.pop_back();
00079
00080 for (uint ii = 0; ii < itsStart.size(); ++ii)
00081 itsOutStream << itsStart[ii] << " " << itsEnd[ii] << std::endl;
00082
00083 itsOutStream.close();
00084 }
00085 }
00086 private:
00087
00088 virtual void handleExtraArgs(const ModelManager& mgr)
00089 {
00090 itsOutFile = mgr.getExtraArg(0);
00091 itsOutStream.open(itsOutFile.c_str(), std::ios::out | std::ios::app);
00092
00093 if (mgr.numExtraArgs() > 1)
00094 itsBuffLength = fromStr<uint>(mgr.getExtraArg(1));
00095
00096 if (!itsOutStream.is_open())
00097 LFATAL("Could not open '%s' for output.", itsOutFile.c_str());
00098
00099 LINFO("Key bindings: ");
00100 LINFO("<space>: pause");
00101 LINFO("<,>: backward");
00102 LINFO("<.>: forward");
00103 LINFO("<z>: mark start of sequence");
00104 LINFO("<x>: mark end of sequence");
00105 LINFO("<u>: undo add sequence start/end");
00106 LINFO(" ");
00107 LINFO(" ");
00108 };
00109
00110
00111 virtual void onFrame(const GenericFrame& input,
00112 FrameOstream& ofs,
00113 const int frameNum)
00114 {
00115 itsBuffer.push_back(input);
00116 if (itsBuffer.size() > itsBuffLength)
00117 itsBuffer.pop_front();
00118
00119
00120 ofs.writeFrame(itsBuffer.back(), itsOutPrefix,
00121 FrameInfo("copy of input frame", SRC_POS));
00122
00123 ++itsFrameCount;
00124 LINFO("showing frame: %d ", itsFrameCount);
00125
00126 int key = itsKeyBoard.getKeyAsChar(false);
00127 switch (key)
00128 {
00129 case 32:
00130 {
00131 LINFO("entering edit mode");
00132 key = -1;
00133 uint itsEditPos = itsFrameCount;
00134 uint itsBufPos = itsBuffer.size() - 1;
00135
00136 while (key != 32)
00137 {
00138 key = itsKeyBoard.getKeyAsChar(true);
00139 switch (key)
00140 {
00141 case 27:
00142 LFATAL("Playback aborted by user, exiting.");
00143 break;
00144
00145 case 44:
00146 {
00147 LINFO("back one frame");
00148 if (itsBufPos <= 0)
00149 LINFO("cannot seek backward any further.");
00150 else
00151 {
00152 --itsBufPos;
00153 --itsEditPos;
00154 }
00155 }
00156 break;
00157
00158 case 46:
00159 {
00160 LINFO("forward one frame");
00161 if ( itsBufPos >= (itsBuffer.size() - 1) )
00162 LINFO("cannot seek forward any further.");
00163 else
00164 {
00165 ++itsBufPos;
00166 ++itsEditPos;
00167 }
00168 }
00169 break;
00170
00171 case 122:
00172 {
00173 int diff = itsStart.size() - itsEnd.size();
00174 if (diff > 0)
00175 LINFO("You have attempted to add two clip onset markers "
00176 "in a row, ignoring command");
00177 else
00178 {
00179 LINFO("marked clip onset");
00180 itsStart.push_back(itsEditPos);
00181 itsEdits.push_back(ADD_START);
00182 }
00183 }
00184 break;
00185
00186 case 120:
00187 {
00188 int diff = itsStart.size() - itsEnd.size();
00189 if (diff < 0)
00190 LINFO("You have attempted to add two clip offset markers "
00191 "in a row, ignoring command");
00192 else
00193 {
00194 LINFO("marked clip offset");
00195 itsEnd.push_back(itsEditPos);
00196 itsEdits.push_back(ADD_END);
00197 }
00198 }
00199 break;
00200
00201 case 117:
00202 {
00203 if (itsEdits.size() > 0)
00204 {
00205 COMMAND last = itsEdits.back();
00206 LINFO("undoing last edit: '%s' ",
00207 last ? "mark end" : "mark start");
00208 itsEdits.pop_back();
00209
00210 if (last)
00211 itsEnd.pop_back();
00212 else
00213 itsStart.pop_back();
00214 }
00215 else
00216 LINFO("No more edits to undo");
00217 }
00218 break;
00219 }
00220
00221
00222 ofs.writeFrame(itsBuffer[itsBufPos], itsOutPrefix,
00223 FrameInfo("copy of input frame", SRC_POS));
00224 LINFO("showing frame: %d ", itsEditPos);
00225 }
00226 LINFO("exiting edit mode");
00227 }
00228 break;
00229
00230 case 27:
00231 LFATAL("Playback aborted by user, exiting.");
00232 break;
00233 }
00234 };
00235
00236 enum COMMAND {ADD_START = 0, ADD_END = 1};
00237
00238 std::string itsOutPrefix;
00239 KeyBoard itsKeyBoard;
00240 std::deque<GenericFrame> itsBuffer;
00241 int itsFrameCount;
00242 std::vector<uint> itsStart, itsEnd;
00243 std::vector<COMMAND> itsEdits;
00244 std::string itsOutFile;
00245 std::ofstream itsOutStream;
00246 uint itsBuffLength;
00247 };
00248
00249 int main(const int argc, const char **argv)
00250 {
00251 iPlayer s;
00252 return s.run(argc, argv, "<output file name>", 1, 2);
00253 }
00254
00255
00256
00257
00258
00259
00260
00261 #endif // APPMEDIA_APP_STREAM_C_DEFINED