src/qvcameras/qvmplayercamera.h

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00024 
00025 #ifndef MPLAYERCAMERA_H
00026 #define MPLAYERCAMERA_H
00027 
00028 #include <QObject>
00029 #include <QProcess>
00030 #include <QString>
00031 #include <QList>
00032 #include <QUrl>
00033 #include <QThread>
00034 #include <QMutex>
00035 #include <QWaitCondition>
00036 #include <QFile>
00037 #include <QTimer>
00038 #include <QQueue>
00039 
00040 #include <qvcore/qvcamera.h>
00041 #include <qvutils/qnamedpipe.h>
00042 
00043 #ifndef DOXYGEN_IGNORE_THIS
00044 // Auxiliary QVCheckOKMPlayerCamera Class.
00045 // This is an internal convenience thread used only to unblock the open function
00046 // when it is waiting for mplayer to read from the fifo, in the case when
00047 // mplayer aborted when starting (due to bad URL, or wrong file format, for
00048 // example).
00049 
00050 class QVCheckOKMplayerCamera: public QThread
00051 {
00052         Q_OBJECT
00053         public:
00054                 QVCheckOKMplayerCamera(QFile & fifo_file,int max_time_ms_to_wait_for_open);
00055                 ~QVCheckOKMplayerCamera();
00056                 void run();
00057         private slots:
00058                 void writeErrorInFifo();
00059         private:
00060                 QFile & _fifo_file;
00061                 int _max_time_ms_to_wait_for_open;
00062 };
00063 
00064 // Auxiliary QVCameraThread Class.
00065 // This is an internal convenience thread used in real time cameras to keep
00066 // updating the frames from mplayer independently of the user grabs.
00067 
00068 class QVCameraThread: public QThread
00069         {
00070         Q_OBJECT
00071         public:
00072                 QVCameraThread(QObject *object,char *slot);
00073                 ~QVCameraThread();
00074                 void run();
00075         private:
00076                 QObject *_object;
00077                 char *_slot;
00078         };
00079 
00080 // Auxiliary QVMPlayerIOProcessor class.
00081 // Auxiliary class to communicate through stdin and stdout with mplayer, and
00082 // read data such as the number of rows and cols of the video, the fps, and also
00083 // to send commands such as pausing, ask for current position, and so on.
00084 
00085 class QVMPlayerIOProcessor: public QObject
00086         {
00087         Q_OBJECT
00088         public:
00089                 QVMPlayerIOProcessor(QProcess *mplayer);
00090                 ~QVMPlayerIOProcessor();
00091                 void queueCommandToMPlayer(const QString &, bool ifEmpty = false);
00092                 double fps, speed, time_length, time_pos;
00093                 uInt cols, rows;
00094         public slots:
00095                 int interpretMPlayerOutput();
00096         private slots:
00097                 void sendCommandToMPlayer();
00098         private:
00099                 QQueue<QString> command_queue;
00100                 QMutex command_queue_mutex;
00101                 QProcess *mplayer;
00102         };
00103 
00104 // Auxiliary QVMPlayerFrameGrabber Class.
00105 class QVMPlayerFrameGrabber: public QObject
00106         {
00107         Q_OBJECT
00108 
00109         public:
00110                 QVMPlayerFrameGrabber(QVMPlayerIOProcessor *, QFile *, uInt rows, uInt cols, bool isRGB, bool realTime);
00111                 ~QVMPlayerFrameGrabber();
00112                 uInt getFrameSize() const { return buf_size; };
00113 
00114                 void getQVImageGray(QVImage<uChar> &imgGray) const;
00115                 void getQVImageRGB(QVImage<uChar,3> &imgRGB) const;
00116                 void getQVImageYUV(QVImage<uChar> &imgY, QVImage<uChar> &imgU, QVImage<uChar> &imgV) const;
00117 
00118                 void updateFrameBuffer();
00119                 uInt frames_read;
00120                 bool finished;
00121                 QMutex mutex;
00122                 QWaitCondition condition;
00123 
00124         public slots:
00125                 void updateSlot();
00126 
00127         signals:
00128                 void newReadFrameGrabber();
00129                 // This signal is to defer the call to sendCommandToMplayer, and avoid
00130                 // the "QSocketNotifier:: socket notifier cannot be enabled from another
00131                 // thread" warning:
00132                 void sendCommandSignal();
00133 
00134         private:
00135                 QVCameraThread *qvcamerathread;
00136                 QFile *fifoInput;       
00137                 QVMPlayerIOProcessor *mplayerIOProcessor;
00138 
00139                 bool debug_in_memcpy, debug_in_updateSlot;
00140                 bool isYUV, realTime;
00141                 uInt buf_size;
00142 
00143                 QVImage<uChar> imgY, img_auxY, imgU, img_auxU,  imgV, img_auxV;
00144                 QVImage<uChar,3> imgRGB, img_auxRGB;
00145                 void readToBuffer(uChar *buf_img_aux, uInt buf_size);
00146                 void getNewFrame(QVImage<uChar,3> &img);
00147                 void getNewFrame(QVImage<uChar> &imgY, QVImage<uChar> &imgU, QVImage<uChar> &imgV);
00148         };
00149 #endif
00150 
00151 // This is for doxygen get the detailed description for QVMPlayerCamera correctly.
00152 class QVWorker;
00153 class QVMPlayerIOProcessor;
00154 
00242 class QVMPlayerCamera : public QVCamera
00243         {
00244         Q_OBJECT
00245 
00246         public:
00248                 QVMPlayerCamera(QString name = QString());
00250                 ~QVMPlayerCamera();
00251 
00252                 // Camera opening options as flags. Combine using | operator to create
00253                 // an OpenOptions flags (observe plural) object:
00255                 enum OpenOption {
00257                         Default = 0x0,
00259                         RealTime = 0x1,
00261                         Deinterlaced = 0x2,
00263                         NoLoop = 0x4,
00265                         RGBMEncoder = 0x8
00266                 };
00267 
00268                 Q_DECLARE_FLAGS(OpenOptions,OpenOption);
00269 
00274                 bool openCam(const QString & url,OpenOptions opts = Default);
00275 
00284                 bool openCam(const QString & urlstring, unsigned int r, unsigned int c, OpenOptions opts = Default);
00285 
00286                 #ifndef DOXYGEN_IGNORE_THIS
00305                 bool openCam();
00306                 #endif
00307 
00313                 bool grab(QVImage<uChar,3> &image);
00314 
00320                 bool grab(QVImage<uChar,1> &image);
00321 
00333                 bool grab(QVImage<uChar> &imgY, QVImage<uChar> &imgU, QVImage<uChar> &imgV);
00334 
00338                 const QString getUrlBase() const { return path.split("/").last(); }
00339 
00342                 OpenOptions getOptions() const          { return open_options; };
00343 
00346                 int getBufferSize() const               { return mplayerFrameGrabber->getFrameSize(); };
00347 
00350                 unsigned int getFramesUpdated() const   { return mplayerFrameGrabber->frames_read; }
00351 
00354                 unsigned int getRows() const            { return mplayerIOProcessor->rows; };
00355 
00358                 unsigned int getCols() const            { return mplayerIOProcessor->cols; };
00359 
00362                 double getFPS() const                   { return mplayerIOProcessor->fps; };
00363 
00366                 double getTimeLength() const            { return mplayerIOProcessor->time_length; };
00367 
00370                 double getTimePos() const               { return mplayerIOProcessor->time_pos; };
00371 
00374                 double getSpeed() const                 { return mplayerIOProcessor->speed; };
00375 
00378                 unsigned int getFramesGrabbed() const           { return frames_grabbed; };
00379 
00382                 unsigned int getFramesRead() const              { return mplayerFrameGrabber->frames_read; };
00383 
00386                 bool isLiveCamera() const               { return live_camera; };
00387 
00396                 static bool getFrame(const QString uri, QVImage<uChar,3> &img, int frame = 0)
00397                         {
00398                         QVMPlayerCamera camera;
00399                         if(camera.openCam(uri, QVMPlayerCamera::RGBMEncoder) == -1)
00400                                 return false;
00401                         bool result;
00402                         for (int i=-1; i < frame; i++)
00403                                 result = camera.grab(img);
00404                         camera.closeCam();
00405                         return result;
00406                         }
00407 
00416                 static bool getFrame(const QString uri, QVImage<uChar,1> &img, int frame = 0)
00417                         {
00418                         QVMPlayerCamera camera;
00419                         if(camera.openCam(uri, QVMPlayerCamera::Default) == -1)
00420                                 return false;
00421 
00422                         for (int i=-1; i < frame; i++)
00423                                 if (!camera.grab(img))
00424                                         return false;
00425 
00426                         camera.closeCam();
00427 
00428                         return true;
00429                         }
00430 
00441                 static bool getFrame(const QString uri, QVImage<uChar> &imgY, QVImage<uChar> &imgU, QVImage<uChar> &imgV,
00442                                 int frame = 0)
00443                         {
00444                         QVMPlayerCamera camera;
00445                         if(camera.openCam(uri, QVMPlayerCamera::Default) == -1)
00446                                 return false;
00447                         bool result;
00448                         for (int i=-1; i < frame; i++)
00449                                 result = camera.grab(imgY, imgU, imgV);
00450                         camera.closeCam();
00451                         return result;
00452                         }
00453 
00454         public slots:
00461                 bool grab();
00462 
00464                 void pauseCam();
00466                 void unpauseCam();
00468                 void nextFrameCam();
00471                 void setSpeedCam(double d);
00478                 void seekCam(QVCamera::TSeekType type, double pos);     
00480                 void closeCam();
00481 
00482                 bool link(QVWorker *,QString imageName);
00483                 bool link(QVWorker *, const QString imageY, const QString imageU, const QString imageV);
00484 
00485         signals:
00486                 // This signal is to defer the call to sendCommandToMplayer, and avoid
00487                 // the "QSocketNotifier:: socket notifier cannot be enabled from another
00488                 // thread" warning:
00489                 void sendCommandSignal();
00490 
00491         private:
00492                 // this boolean value becomes true if any RGB image is linked to the camera, from a worker.
00493                 bool rgbMode;
00494 
00495                 QWaitCondition conditionJustToSleepMiliseconds;
00496 
00497                 OpenOptions open_options;
00498                 QVMPlayerFrameGrabber * mplayerFrameGrabber;
00499                 QVMPlayerIOProcessor * mplayerIOProcessor;
00500 
00501                 QNamedPipe *namedPipe, *namedPipeAux;   // The named fifo(s);
00502                 QFile fifo_file,fifo_file_2;            // Named fifo;
00503                 QProcess mplayer, mencoder;             // mplayer and mencoder processes;
00504 
00505                 void initMPlayerArgs(QString urlString, unsigned int rows, unsigned int cols);
00506                 bool performGrab();
00507 
00508                 QStringList mplayer_args;               // MPlayer arguments
00509                 QString path, schema;                   // path and schema extracted from the URL
00510                 
00511                 bool live_camera;
00512                 int frames_grabbed;
00513         };
00514 
00515 // Camera opening flags | operator:
00516 Q_DECLARE_OPERATORS_FOR_FLAGS(QVMPlayerCamera::OpenOptions)
00517 
00518 #endif
00519 
00520 
00521 
00522 

Generated on Thu Jul 17 17:23:27 2008 for QVision by  doxygen 1.5.3