| 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::TOP_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::INFO)<<"Destructing FFmpegImageStream..."<<std::endl; |
|---|
| 43 | |
|---|
| 44 | quit(true); |
|---|
| 45 | |
|---|
| 46 | osg::notify(osg::INFO)<<"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::INFO)<<"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 | |
|---|
| 76 | setPixelAspectRatio(m_decoder->video_decoder().pixelAspectRatio()); |
|---|
| 77 | |
|---|
| 78 | osg::notify(osg::NOTICE)<<"ffmpeg::open("<<filename<<") size("<<s()<<", "<<t()<<") aspect ratio "<<m_decoder->video_decoder().pixelAspectRatio()<<std::endl; |
|---|
| 79 | |
|---|
| 80 | #if 1 |
|---|
| 81 | |
|---|
| 82 | if (s()<=10 || t()<=10) return false; |
|---|
| 83 | #endif |
|---|
| 84 | |
|---|
| 85 | m_decoder->video_decoder().setUserData(this); |
|---|
| 86 | m_decoder->video_decoder().setPublishCallback(publishNewFrame); |
|---|
| 87 | |
|---|
| 88 | if (m_decoder->audio_decoder().validContext()) |
|---|
| 89 | { |
|---|
| 90 | osg::notify(osg::NOTICE)<<"Attaching FFmpegAudioStream"<<std::endl; |
|---|
| 91 | |
|---|
| 92 | getAudioStreams().push_back(new FFmpegAudioStream(m_decoder.get())); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | _status = PAUSED; |
|---|
| 96 | applyLoopingMode(); |
|---|
| 97 | |
|---|
| 98 | start(); |
|---|
| 99 | |
|---|
| 100 | return true; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | void FFmpegImageStream::play() |
|---|
| 106 | { |
|---|
| 107 | m_commands->push(CMD_PLAY); |
|---|
| 108 | |
|---|
| 109 | #if 0 |
|---|
| 110 | |
|---|
| 111 | OpenThreads::ScopedLock<Mutex> lock(m_mutex); |
|---|
| 112 | |
|---|
| 113 | while (duration() > 0 && ! m_frame_published_flag) |
|---|
| 114 | m_frame_published_cond.wait(&m_mutex); |
|---|
| 115 | |
|---|
| 116 | #endif |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | void FFmpegImageStream::pause() |
|---|
| 122 | { |
|---|
| 123 | m_commands->push(CMD_PAUSE); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | void FFmpegImageStream::rewind() |
|---|
| 129 | { |
|---|
| 130 | m_commands->push(CMD_REWIND); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | void FFmpegImageStream::seek(double time) { |
|---|
| 134 | m_seek_time = time; |
|---|
| 135 | m_commands->push(CMD_SEEK); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | void FFmpegImageStream::quit(bool waitForThreadToExit) |
|---|
| 141 | { |
|---|
| 142 | |
|---|
| 143 | if (isRunning()) |
|---|
| 144 | { |
|---|
| 145 | m_commands->push(CMD_STOP); |
|---|
| 146 | |
|---|
| 147 | if (waitForThreadToExit) |
|---|
| 148 | join(); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | m_decoder->close(waitForThreadToExit); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | double FFmpegImageStream::getLength() const |
|---|
| 157 | { |
|---|
| 158 | return m_decoder->duration(); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | double FFmpegImageStream::getReferenceTime () const |
|---|
| 163 | { |
|---|
| 164 | return m_decoder->reference(); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | double FFmpegImageStream::getFrameRate() const |
|---|
| 170 | { |
|---|
| 171 | return m_decoder->video_decoder().frameRate(); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | bool FFmpegImageStream::isImageTranslucent() const |
|---|
| 177 | { |
|---|
| 178 | return m_decoder->video_decoder().alphaChannel(); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | void FFmpegImageStream::run() |
|---|
| 184 | { |
|---|
| 185 | try |
|---|
| 186 | { |
|---|
| 187 | bool done = false; |
|---|
| 188 | |
|---|
| 189 | while (! done) |
|---|
| 190 | { |
|---|
| 191 | if (_status == PLAYING) |
|---|
| 192 | { |
|---|
| 193 | bool no_cmd; |
|---|
| 194 | const Command cmd = m_commands->timedPop(no_cmd, 1); |
|---|
| 195 | |
|---|
| 196 | if (no_cmd) |
|---|
| 197 | { |
|---|
| 198 | m_decoder->readNextPacket(); |
|---|
| 199 | } |
|---|
| 200 | else |
|---|
| 201 | done = ! handleCommand(cmd); |
|---|
| 202 | } |
|---|
| 203 | else |
|---|
| 204 | { |
|---|
| 205 | done = ! handleCommand(m_commands->pop()); |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | catch (const std::exception & error) |
|---|
| 211 | { |
|---|
| 212 | osg::notify(osg::WARN) << "FFmpegImageStream::run : " << error.what() << std::endl; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | catch (...) |
|---|
| 216 | { |
|---|
| 217 | osg::notify(osg::WARN) << "FFmpegImageStream::run : unhandled exception" << std::endl; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | osg::notify(osg::NOTICE)<<"Finished FFmpegImageStream::run()"<<std::endl; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | void FFmpegImageStream::applyLoopingMode() |
|---|
| 226 | { |
|---|
| 227 | m_decoder->loop(getLoopingMode() == LOOPING); |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | bool FFmpegImageStream::handleCommand(const Command cmd) |
|---|
| 233 | { |
|---|
| 234 | switch (cmd) |
|---|
| 235 | { |
|---|
| 236 | case CMD_PLAY: |
|---|
| 237 | cmdPlay(); |
|---|
| 238 | return true; |
|---|
| 239 | |
|---|
| 240 | case CMD_PAUSE: |
|---|
| 241 | cmdPause(); |
|---|
| 242 | return true; |
|---|
| 243 | |
|---|
| 244 | case CMD_REWIND: |
|---|
| 245 | cmdRewind(); |
|---|
| 246 | return true; |
|---|
| 247 | |
|---|
| 248 | case CMD_SEEK: |
|---|
| 249 | cmdSeek(m_seek_time); |
|---|
| 250 | return true; |
|---|
| 251 | |
|---|
| 252 | case CMD_STOP: |
|---|
| 253 | return false; |
|---|
| 254 | |
|---|
| 255 | default: |
|---|
| 256 | assert(false); |
|---|
| 257 | return false; |
|---|
| 258 | } |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | void FFmpegImageStream::cmdPlay() |
|---|
| 264 | { |
|---|
| 265 | if (_status == PAUSED) |
|---|
| 266 | { |
|---|
| 267 | if (! m_decoder->audio_decoder().isRunning()) |
|---|
| 268 | m_decoder->audio_decoder().start(); |
|---|
| 269 | |
|---|
| 270 | if (! m_decoder->video_decoder().isRunning()) |
|---|
| 271 | m_decoder->video_decoder().start(); |
|---|
| 272 | |
|---|
| 273 | m_decoder->video_decoder().pause(false); |
|---|
| 274 | m_decoder->audio_decoder().pause(false); |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | _status = PLAYING; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | void FFmpegImageStream::cmdPause() |
|---|
| 283 | { |
|---|
| 284 | if (_status == PLAYING) |
|---|
| 285 | { |
|---|
| 286 | m_decoder->video_decoder().pause(true); |
|---|
| 287 | m_decoder->audio_decoder().pause(true); |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | _status = PAUSED; |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | void FFmpegImageStream::cmdRewind() |
|---|
| 296 | { |
|---|
| 297 | m_decoder->rewind(); |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | void FFmpegImageStream::cmdSeek(double time) |
|---|
| 301 | { |
|---|
| 302 | m_decoder->seek(time); |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | void FFmpegImageStream::publishNewFrame(const FFmpegDecoderVideo &, void * user_data) |
|---|
| 307 | { |
|---|
| 308 | FFmpegImageStream * const this_ = reinterpret_cast<FFmpegImageStream*>(user_data); |
|---|
| 309 | |
|---|
| 310 | #if 1 |
|---|
| 311 | this_->setImage( |
|---|
| 312 | this_->m_decoder->video_decoder().width(), this_->m_decoder->video_decoder().height(), 1, GL_RGBA, GL_BGRA, GL_UNSIGNED_BYTE, |
|---|
| 313 | const_cast<unsigned char *>(this_->m_decoder->video_decoder().image()), NO_DELETE |
|---|
| 314 | ); |
|---|
| 315 | #else |
|---|
| 316 | |
|---|
| 317 | this_->dirty(); |
|---|
| 318 | #endif |
|---|
| 319 | |
|---|
| 320 | OpenThreads::ScopedLock<Mutex> lock(this_->m_mutex); |
|---|
| 321 | |
|---|
| 322 | if (! this_->m_frame_published_flag) |
|---|
| 323 | { |
|---|
| 324 | this_->m_frame_published_flag = true; |
|---|
| 325 | this_->m_frame_published_cond.signal(); |
|---|
| 326 | } |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | } |
|---|