Media.cpp 2.83 KB
Newer Older
1
#include <QtDebug>
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
2 3 4 5
#include "Media.h"

Media::Media(const QString& mrl) : _mrl(mrl), _snapshot(NULL)
{
6 7 8 9 10 11 12 13 14 15
    char const *vlc_argv[] =
    {
        "-verbose",
        "3",
        //"--snapshot-format", "jpg",
        //"--plugin-path", VLC_TREE "/modules",
        //"--ignore-config", /* Don't use VLC's config files */
    };
    int vlc_argc = sizeof(vlc_argv) / sizeof(*vlc_argv);
    this->_instance = new LibVLCpp::Instance(vlc_argc, vlc_argv);
16

17
    this->_vlcMedia = new LibVLCpp::Media(this->_instance, this->_mrl);
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
18 19 20 21 22

    this->_vlcMedia->outputInVmem();
    this->_vlcMedia->setLockCallback(Media::lock);
    this->_vlcMedia->setUnlockCallback(Media::unlock);

23 24 25 26 27 28 29 30 31 32 33
    char    width[64], height[64], chroma[64], pitch[64];
    sprintf(width, ":vmem-width=%i", VIDEOWIDTH);
    sprintf(height, ":vmem-height=%i", VIDEOHEIGHT);
    sprintf(chroma, ":vmem-chroma=%s", "RV32");
    sprintf(pitch, ":vmem-pitch=%i", VIDEOWIDTH * 4);

    this->_vlcMedia->addOption(width);
    this->_vlcMedia->addOption(height);
    this->_vlcMedia->addOption(chroma);
    this->_vlcMedia->addOption(pitch);

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
34
    this->_pixelBuffer = new uchar[VIDEOHEIGHT * VIDEOWIDTH * 4];
35
    this->_image = new QImage(this->_pixelBuffer, VIDEOWIDTH, VIDEOHEIGHT, VIDEOWIDTH * 4, QImage::Format_RGB32);
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
36 37
    this->_image->fill(0);

38
    //Once we got the pixel buffer up and running, we can put it in the "render context"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
39
    this->_vlcMedia->setPixelBuffer(this->_pixelBuffer);
40 41 42 43
    this->_vlcMedia->setDataCtx();

    //And now we play the media
    this->_vlcMediaPlayer = new LibVLCpp::MediaPlayer(this->_vlcMedia);
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
}

Media::~Media()
{
    delete this->_image;
    delete this->_pixelBuffer;
    delete this->_vlcMedia;
    delete this->_vlcMediaPlayer;
    delete this->_instance;
}

void        Media::lock(LibVLCpp::Media::DataCtx* ctx, void **renderPtr)
{
    ctx->mutex->lock();
    *renderPtr = ctx->media->getPixelBuffer();
}

void        Media::unlock(LibVLCpp::Media::DataCtx* ctx)
{
63
//    qDebug() << "frame just rendered";
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
64 65 66
    ctx->mutex->unlock();
}

67
QImage*      Media::takeSnapshot(unsigned int width, unsigned int height)
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
68
{
69 70 71 72 73
    //FIXME
    //this isn't working, but it seems like a vlc problem, since lastest release of vlc segfault when a screenshot is taken... -_-
    return NULL;


Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
74 75
    if ( this->_snapshot == NULL )
    {
76 77 78 79 80
        qDebug() << "trying to take a snapshot";
        this->_vlcMediaPlayer->takeSnapshot("/tmp/vlmcscreenshot.tmp.gif", width, height);
        qDebug() << "done snapshoting";
        this->_snapshot = new QImage("/tmp/vlmcscreenshot.tmp.gif");
        qDebug() << "written to a QImage";
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
81 82 83
    }
    return this->_snapshot;
}
84 85 86 87 88 89 90 91 92 93

int         Media::isPlaying()
{
    return this->_vlcMediaPlayer->isPlaying();
}

int         Media::isSeekable()
{
    return this->_vlcMediaPlayer->isSeekable();
}
94 95 96 97 98 99

qint64      Media::getLength()
{
    return this->_vlcMediaPlayer->getLength();
}