| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | #ifndef _MPEGIMAGESTREAM_H_ |
|---|
| 29 | #define _MPEGIMAGESTREAM_H_ |
|---|
| 30 | |
|---|
| 31 | #include <osg/ImageStream> |
|---|
| 32 | #include <osg/Notify> |
|---|
| 33 | |
|---|
| 34 | #include <OpenThreads/Thread> |
|---|
| 35 | #include <OpenThreads/Mutex> |
|---|
| 36 | |
|---|
| 37 | #define NUM_CMD_INDEX 4 |
|---|
| 38 | |
|---|
| 39 | namespace osg { |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | class SG_EXPORT MpegImageStream : public osg::ImageStream, public OpenThreads::Thread |
|---|
| 45 | { |
|---|
| 46 | public: |
|---|
| 47 | MpegImageStream(const char* fileName = NULL); |
|---|
| 48 | |
|---|
| 49 | virtual Object* clone() const { return new MpegImageStream; } |
|---|
| 50 | virtual bool isSameKindAs(const Object* obj) const { |
|---|
| 51 | return dynamic_cast<const MpegImageStream*>(obj) != NULL; |
|---|
| 52 | } |
|---|
| 53 | virtual const char* className() const { return "MpegImageStream"; } |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | virtual void play() |
|---|
| 57 | { |
|---|
| 58 | if (!isRunning()) start(); |
|---|
| 59 | |
|---|
| 60 | osg::notify(osg::INFO)<<"Play video"<<this<<std::endl; |
|---|
| 61 | |
|---|
| 62 | setCmd(THREAD_START); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | virtual void pause() |
|---|
| 67 | { |
|---|
| 68 | osg::notify(osg::INFO)<<"Pause video"<<this<<std::endl; |
|---|
| 69 | setCmd(THREAD_STOP); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | virtual void rewind() { setCmd(THREAD_REWIND); } |
|---|
| 74 | |
|---|
| 75 | virtual void quit(bool wiatForThreadToExit); |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | inline void enableMMX(bool b) { _useMMX = b; } |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | inline void setFrameRate(float fps) { _fps = (fps < 0.0f ? 0.0f : fps); } |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | inline float getFrameRate() const { return _fps; } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | inline long getNumFrames() const { return _frames; } |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | inline float getLength() const { return _len; } |
|---|
| 95 | |
|---|
| 96 | void load(const char* fileName); |
|---|
| 97 | |
|---|
| 98 | virtual void run(); |
|---|
| 99 | |
|---|
| 100 | protected: |
|---|
| 101 | virtual ~MpegImageStream(); |
|---|
| 102 | |
|---|
| 103 | bool _useMMX; |
|---|
| 104 | float _fps; |
|---|
| 105 | long _frames; |
|---|
| 106 | float _len; |
|---|
| 107 | |
|---|
| 108 | void swapData(); |
|---|
| 109 | |
|---|
| 110 | enum ThreadCommand { |
|---|
| 111 | THREAD_IDLE = 0, |
|---|
| 112 | THREAD_START, |
|---|
| 113 | THREAD_STOP, |
|---|
| 114 | THREAD_REWIND, |
|---|
| 115 | THREAD_CLOSE, |
|---|
| 116 | THREAD_QUIT |
|---|
| 117 | }; |
|---|
| 118 | ThreadCommand _cmd[NUM_CMD_INDEX]; |
|---|
| 119 | int _wrIndex, _rdIndex; |
|---|
| 120 | |
|---|
| 121 | OpenThreads::Mutex _mutex; |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | void setCmd(ThreadCommand cmd); |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | ThreadCommand getCmd(); |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | void* _mpg; |
|---|
| 132 | unsigned char** _rows; |
|---|
| 133 | unsigned char* _videoWriteData; |
|---|
| 134 | |
|---|
| 135 | }; |
|---|
| 136 | |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | #endif |
|---|