| 1 | |
|---|
| 2 | #include "FFmpegImageStream.hpp" |
|---|
| 3 | #include "FFmpegAudioStream.hpp" |
|---|
| 4 | |
|---|
| 5 | #include <OpenThreads/ScopedLock> |
|---|
| 6 | #include <osg/Notify> |
|---|
| 7 | |
|---|
| 8 | #include <memory> |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | namespace osgFFmpeg { |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | FFmpegImageStream::FFmpegImageStream() : |
|---|
| 17 | m_decoder(0), |
|---|
| 18 | m_commands(0), |
|---|
| 19 | m_frame_published_flag(false) |
|---|
| 20 | { |
|---|
| 21 | setOrigin(osg::Image::BOTTOM_LEFT); |
|---|
| 22 | |
|---|
| 23 | std::auto_ptr<FFmpegDecoder> decoder(new FFmpegDecoder); |
|---|
| 24 | std::auto_ptr<CommandQueue> commands(new CommandQueue); |
|---|
| 25 | |
|---|
| 26 | m_decoder = decoder.release(); |
|---|
| 27 | m_commands = commands.release(); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | FFmpegImageStream::FFmpegImageStream(const FFmpegImageStream & image, const osg::CopyOp & copyop) : |
|---|
| 33 | osg::ImageStream(image, copyop) |
|---|
| 34 | { |
|---|
| 35 | |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | FFmpegImageStream::~FFmpegImageStream() |
|---|
| 41 | { |
|---|
| 42 | osg::notify(osg::NOTICE)<<"Destructing FFMpegImageStream..."<<std::endl; |
|---|
| 43 | |
|---|
| 44 | quit(true); |
|---|
| 45 | |
|---|
| 46 | osg::notify(osg::NOTICE)<<"Have done quit"<<std::endl; |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | getAudioStreams().clear(); |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | m_decoder = 0; |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | delete m_commands; |
|---|
| 57 | |
|---|
| 58 | osg::notify(osg::NOTICE)<<"Destructed FFMpegImageStream."<<std::endl; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | bool FFmpegImageStream::open(const std::string & filename) |
|---|
| 64 | { |
|---|
| 65 | setFileName(filename); |
|---|
| 66 | |
|---|
| 67 | if (! m_decoder->open(filename)) |
|---|
| 68 | return false; |
|---|
| 69 | |
|---|
| 70 | setImage( |
|---|
| 71 | m_decoder->video_decoder().width(), m_decoder->video_decoder().height(), 1, GL_RGBA, GL_BGRA, GL_UNSIGNED_BYTE, |
|---|
| 72 | const_cast<unsigned char *>(m_decoder->video_decoder().image()), NO_DELETE |
|---|
| 73 | ); |
|---|
| 74 | |
|---|
| 75 | setOrigin(osg::Image::TOP_LEFT); |
|---|
| 76 | |
|---|
| 77 | m_decoder->video_decoder().setUserData(this); |
|---|
| 78 | m_decoder->video_decoder().setPublishCallback(publishNewFrame); |
|---|
| 79 | |
|---|
| 80 | if (m_decoder->audio_decoder().validContext()) |
|---|
| 81 | { |
|---|
| 82 | osg::notify(osg::NOTICE)<<"Attaching FFmpegAudioStream"<<std::endl; |
|---|
| 83 | |
|---|
| 84 | getAudioStreams().push_back(new FFmpegAudioStream(m_decoder.get())); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | _status = PAUSED; |
|---|
| 88 | applyLoopingMode(); |
|---|
| 89 | |
|---|
| 90 | start(); |
|---|
| 91 | |
|---|
| 92 | return true; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | void FFmpegImageStream::play() |
|---|
| 98 | { |
|---|
| 99 | m_commands->push(CMD_PLAY); |
|---|
| 100 | |
|---|
| 101 | #if 0 |
|---|
| 102 | |
|---|
| 103 | OpenThreads::ScopedLock<Mutex> lock(m_mutex); |
|---|
| 104 | |
|---|
| 105 | while (duration() > 0 && ! m_frame_published_flag) |
|---|
| 106 | m_frame_published_cond.wait(&m_mutex); |
|---|
| 107 | |
|---|
| 108 | #endif |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | void FFmpegImageStream::pause() |
|---|
| 114 | { |
|---|
| 115 | m_commands->push(CMD_PAUSE); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | void FFmpegImageStream::rewind() |
|---|
| 121 | { |
|---|
| 122 | m_commands->push(CMD_REWIND); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | void FFmpegImageStream::quit(bool waitForThreadToExit) |
|---|
| 128 | { |
|---|
| 129 | |
|---|
| 130 | if (isRunning()) |
|---|
| 131 | { |
|---|
| 132 | m_commands->push(CMD_STOP); |
|---|
| 133 | |
|---|
| 134 | if (waitForThreadToExit) |
|---|
| 135 | join(); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | m_decoder->close(waitForThreadToExit); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | double FFmpegImageStream::duration() const |
|---|
| 144 | { |
|---|
| 145 | return m_decoder->duration(); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | bool FFmpegImageStream::videoAlphaChannel() const |
|---|
| 151 | { |
|---|
| 152 | return m_decoder->video_decoder().alphaChannel(); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | double FFmpegImageStream::videoAspectRatio() const |
|---|
| 158 | { |
|---|
| 159 | return m_decoder->video_decoder().aspectRatio(); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | double FFmpegImageStream::videoFrameRate() const |
|---|
| 165 | { |
|---|
| 166 | return m_decoder->video_decoder().frameRate(); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | void FFmpegImageStream::run() |
|---|
| 171 | { |
|---|
| 172 | try |
|---|
| 173 | { |
|---|
| 174 | bool done = false; |
|---|
| 175 | |
|---|
| 176 | while (! done) |
|---|
| 177 | { |
|---|
| 178 | if (_status == PLAYING) |
|---|
| 179 | { |
|---|
| 180 | bool no_cmd; |
|---|
| 181 | const Command cmd = m_commands->timedPop(no_cmd, 1); |
|---|
| 182 | |
|---|
| 183 | if (no_cmd) |
|---|
| 184 | { |
|---|
| 185 | m_decoder->readNextPacket(); |
|---|
| 186 | } |
|---|
| 187 | else |
|---|
| 188 | done = ! handleCommand(cmd); |
|---|
| 189 | } |
|---|
| 190 | else |
|---|
| 191 | { |
|---|
| 192 | done = ! handleCommand(m_commands->pop()); |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | catch (const std::exception & error) |
|---|
| 198 | { |
|---|
| 199 | osg::notify(osg::WARN) << "FFmpegImageStream::run : " << error.what() << std::endl; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | catch (...) |
|---|
| 203 | { |
|---|
| 204 | osg::notify(osg::WARN) << "FFmpegImageStream::run : unhandled exception" << std::endl; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | osg::notify(osg::NOTICE)<<"Finished FFmpegImageStream::run()"<<std::endl; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | void FFmpegImageStream::applyLoopingMode() |
|---|
| 213 | { |
|---|
| 214 | m_decoder->loop(getLoopingMode() == LOOPING); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | bool FFmpegImageStream::handleCommand(const Command cmd) |
|---|
| 220 | { |
|---|
| 221 | switch (cmd) |
|---|
| 222 | { |
|---|
| 223 | case CMD_PLAY: |
|---|
| 224 | cmdPlay(); |
|---|
| 225 | return true; |
|---|
| 226 | |
|---|
| 227 | case CMD_PAUSE: |
|---|
| 228 | cmdPause(); |
|---|
| 229 | return true; |
|---|
| 230 | |
|---|
| 231 | case CMD_REWIND: |
|---|
| 232 | cmdRewind(); |
|---|
| 233 | return true; |
|---|
| 234 | |
|---|
| 235 | case CMD_STOP: |
|---|
| 236 | return false; |
|---|
| 237 | |
|---|
| 238 | default: |
|---|
| 239 | assert(false); |
|---|
| 240 | return false; |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | void FFmpegImageStream::cmdPlay() |
|---|
| 247 | { |
|---|
| 248 | if (_status == PAUSED) |
|---|
| 249 | { |
|---|
| 250 | if (! m_decoder->audio_decoder().isRunning()) |
|---|
| 251 | m_decoder->audio_decoder().start(); |
|---|
| 252 | |
|---|
| 253 | if (! m_decoder->video_decoder().isRunning()) |
|---|
| 254 | m_decoder->video_decoder().start(); |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | _status = PLAYING; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | void FFmpegImageStream::cmdPause() |
|---|
| 263 | { |
|---|
| 264 | if (_status == PLAYING) |
|---|
| 265 | { |
|---|
| 266 | |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | _status = PAUSED; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | void FFmpegImageStream::cmdRewind() |
|---|
| 275 | { |
|---|
| 276 | m_decoder->rewind(); |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | void FFmpegImageStream::publishNewFrame(const FFmpegDecoderVideo &, void * user_data) |
|---|
| 282 | { |
|---|
| 283 | FFmpegImageStream * const this_ = reinterpret_cast<FFmpegImageStream*>(user_data); |
|---|
| 284 | |
|---|
| 285 | #if 1 |
|---|
| 286 | this_->setImage( |
|---|
| 287 | this_->m_decoder->video_decoder().width(), this_->m_decoder->video_decoder().height(), 1, GL_RGBA, GL_BGRA, GL_UNSIGNED_BYTE, |
|---|
| 288 | const_cast<unsigned char *>(this_->m_decoder->video_decoder().image()), NO_DELETE |
|---|
| 289 | ); |
|---|
| 290 | #else |
|---|
| 291 | |
|---|
| 292 | this_->dirty(); |
|---|
| 293 | #endif |
|---|
| 294 | |
|---|
| 295 | OpenThreads::ScopedLock<Mutex> lock(this_->m_mutex); |
|---|
| 296 | |
|---|
| 297 | if (! this_->m_frame_published_flag) |
|---|
| 298 | { |
|---|
| 299 | this_->m_frame_published_flag = true; |
|---|
| 300 | this_->m_frame_published_cond.signal(); |
|---|
| 301 | } |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | } |
|---|