summaryrefslogtreecommitdiff
path: root/avmedia/source/vlc/vlcframegrabber.cxx
blob: ec5b959da671d40aa8dd8be457df6a8ee7e19a68 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <osl/conditn.hxx>
#include <vcl/bmpacc.hxx>
#include <vcl/graph.hxx>
#include <avmedia/mediawindow.hxx>
#include "vlcframegrabber.hxx"
#include "vlcplayer.hxx"
#include <vlc/libvlc_media_player.h>

using namespace ::com::sun::star;

namespace avmedia {
namespace vlc {

const ::rtl::OUString AVMEDIA_VLC_GRABBER_IMPLEMENTATIONNAME = "com.sun.star.comp.avmedia.VLCFrameGrabber_VLC";
const ::rtl::OUString AVMEDIA_VLC_GRABBER_SERVICENAME = "com.sun.star.media.VLCFrameGrabber_VLC";
const int MSEC_IN_SEC = 1000;

SAL_CALL VLCFrameGrabber::VLCFrameGrabber( boost::shared_ptr<libvlc_media_player_t>& player, const rtl::OUString& url )
    : FrameGrabber_BASE()
    , mPlayer( player )
    , mUrl( url )
{
}

namespace
{
    struct FrameData
    {
        ::osl::Condition mCondition;

        std::vector<sal_uInt8> buffer;

        libvlc_media_player_t *mpPlayer;

        FrameData( libvlc_media_player_t *pPlayer )
            : mpPlayer( pPlayer )
        {
        }

        void updateSize()
        {
            unsigned int w, h;
            libvlc_video_get_size( mpPlayer, 0, &w, &h );

            buffer.resize(w * h * 3);
        }

        ~FrameData()
        {
        }
    };

    void *FrameLock( void *data, void **pPixels )
    {
        FrameData *frameData = static_cast<FrameData*>( data );

        frameData->updateSize();

        *pPixels = frameData->buffer.data();

        return *pPixels;
    }

    void FrameUnlock( void *data, void */* id */, void *const * /* pPixels */ )
    {
        FrameData *frameData = static_cast<FrameData*>( data );

        frameData->mCondition.set();
    }

    void FrameDisplay( void */* data */, void */* id */ )
    {
    }
}

::uno::Reference< css::graphic::XGraphic > SAL_CALL VLCFrameGrabber::grabFrame( double fMediaTime )
{
    if ( mUrl.isEmpty() )
        return ::uno::Reference< css::graphic::XGraphic >();

    libvlc_media_player_t *pPlayer = mPlayer.get();
    FrameData frameData( pPlayer );
    libvlc_video_set_callbacks( pPlayer, FrameLock, FrameUnlock, FrameDisplay, &frameData );

    const unsigned int w = 480, h = 360;

    libvlc_video_set_format( pPlayer, "RV24", w, h, w * 3 );

    libvlc_media_player_set_time( pPlayer, fMediaTime * MSEC_IN_SEC );
    libvlc_media_player_play( pPlayer );

    const TimeValue t = {2, 0};
    frameData.mCondition.wait( &t );

    if ( !frameData.mCondition.check() )
        return ::uno::Reference< css::graphic::XGraphic >();

    Bitmap aBmp( Size( w, h ), 24 );

    sal_uInt8 *pData = frameData.buffer.data();
    BitmapWriteAccess *pWrite = aBmp.AcquireWriteAccess();
    if ( pWrite )
    {
        for ( std::size_t y = 0; y < h; ++y )
        {
            for ( std::size_t x = 0; x < w; ++x )
            {
                sal_uInt8 *p = pData + ( y * w + x ) * 3;
                BitmapColor col( p[0], p[1], p[2] );
                pWrite->SetPixel( y, x, col );
            }
        }
    }
    aBmp.ReleaseAccess( pWrite );

    libvlc_media_player_stop( pPlayer );

    return Graphic( aBmp ).GetXGraphic();
}

::rtl::OUString SAL_CALL VLCFrameGrabber::getImplementationName()
{
    return AVMEDIA_VLC_GRABBER_IMPLEMENTATIONNAME;
}

::sal_Bool SAL_CALL VLCFrameGrabber::supportsService( const ::rtl::OUString& serviceName )
{
    return AVMEDIA_VLC_GRABBER_SERVICENAME == serviceName;
}

::uno::Sequence< ::rtl::OUString > SAL_CALL VLCFrameGrabber::getSupportedServiceNames()
{
    ::uno::Sequence< OUString > aRet(1);
    aRet[0] = AVMEDIA_VLC_GRABBER_SERVICENAME;
    return aRet;
}

}
}