summaryrefslogtreecommitdiff
path: root/avmedia/source/vlc/vlcplayer.cxx
blob: 5cd516214ee20053517278fa26e69f924dd51e72 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <boost/bind.hpp>
#include <vcl/syschild.hxx>
#include <vcl/sysdata.hxx>
#include <cppuhelper/supportsservice.hxx>

#include "vlcplayer.hxx"
#include "vlcwindow.hxx"
#include "vlcframegrabber.hxx"
#include "wrapper/Instance.hxx"

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

namespace avmedia {
namespace vlc {

namespace
{
    const ::rtl::OUString AVMEDIA_VLC_PLAYER_IMPLEMENTATIONNAME = "com.sun.star.comp.avmedia.Player_VLC";
    const ::rtl::OUString AVMEDIA_VLC_PLAYER_SERVICENAME = "com.sun.star.media.Player_VLC";

    const int MS_IN_SEC = 1000; // Millisec in sec
}

VLCPlayer::VLCPlayer( const rtl::OUString& url,
                      wrapper::Instance& instance,
                      wrapper::EventHandler& eh )
    : VLC_Base( m_aMutex )
    , mInstance( instance )
    , mEventHandler( eh )
    , mMedia( url, mInstance )
    , mPlayer( mMedia )
    , mEventManager( mPlayer, mEventHandler )
    , mUrl( url )
    , mPlaybackLoop( false )
    , mPrevWinID( 0 )
{
    mPlayer.setMouseHandling( false );
}

unsigned VLCPlayer::getWidth() const
{
    return mPlayer.getWidth();
}

unsigned VLCPlayer::getHeight() const
{
    return mPlayer.getHeight();
}

void SAL_CALL VLCPlayer::start() throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    ::osl::MutexGuard aGuard(m_aMutex);
    if (!mPlayer.play())
    {
        // TODO: Error
    }
}

void SAL_CALL VLCPlayer::stop() throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    ::osl::MutexGuard aGuard(m_aMutex);
    mPlayer.pause();
}

::sal_Bool SAL_CALL VLCPlayer::isPlaying() throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    ::osl::MutexGuard aGuard(m_aMutex);
    return mPlayer.isPlaying();
}

double SAL_CALL VLCPlayer::getDuration() throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    ::osl::MutexGuard aGuard(m_aMutex);
    return static_cast<double>( mMedia.getDuration() ) / MS_IN_SEC;
}

void SAL_CALL VLCPlayer::setScale( float factor )
{
    ::osl::MutexGuard aGuard(m_aMutex);
    mPlayer.setScale( factor );
}

void SAL_CALL VLCPlayer::setMediaTime( double fTime ) throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    ::osl::MutexGuard aGuard(m_aMutex);
    if ( fTime < 0.00000001 && !mPlayer.isPlaying() )
    {
        mPlayer.stop();
    }

    mPlayer.setTime( fTime * MS_IN_SEC );
}

double SAL_CALL VLCPlayer::getMediaTime() throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    ::osl::MutexGuard aGuard(m_aMutex);
    return static_cast<double>( mPlayer.getTime() ) / MS_IN_SEC;
}

double SAL_CALL VLCPlayer::getRate() throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    ::osl::MutexGuard aGuard(m_aMutex);
    return mPlayer.getRate();
}

void VLCPlayer::replay()
{
    setPlaybackLoop( false );
    stop();
    setMediaTime( 0 );
    start();
}

void SAL_CALL VLCPlayer::setPlaybackLoop( ::sal_Bool bSet ) throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    ::osl::MutexGuard aGuard(m_aMutex);
    mPlaybackLoop = bSet;

    if ( bSet )
        mEventManager.onEndReached(boost::bind(&VLCPlayer::replay, this));
    else
        mEventManager.onEndReached();
}

::sal_Bool SAL_CALL VLCPlayer::isPlaybackLoop() throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    ::osl::MutexGuard aGuard(m_aMutex);
    return mPlaybackLoop;
}

void SAL_CALL VLCPlayer::setVolumeDB( ::sal_Int16 nDB ) throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    ::osl::MutexGuard aGuard(m_aMutex);
    mPlayer.setVolume( static_cast<sal_Int16>( ( nDB + 40 ) * 10.0  / 4 ) );
}

::sal_Int16 SAL_CALL VLCPlayer::getVolumeDB() throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    ::osl::MutexGuard aGuard(m_aMutex);
    return static_cast<sal_Int16>( mPlayer.getVolume() / 10.0 * 4 - 40 );
}

void SAL_CALL VLCPlayer::setMute( ::sal_Bool bSet ) throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    ::osl::MutexGuard aGuard(m_aMutex);
    mPlayer.setMute( bSet );
}

::sal_Bool SAL_CALL VLCPlayer::isMute() throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    ::osl::MutexGuard aGuard(m_aMutex);
    return mPlayer.getMute();
}

css::awt::Size SAL_CALL VLCPlayer::getPreferredPlayerWindowSize() throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    return css::awt::Size( 480, 360 );
}

namespace
{
    // TODO: Move this function to the common space for avoiding duplication with
    // gstreamer/gstwindow::createPlayerWindow functionality
    intptr_t GetWindowID( const uno::Sequence< uno::Any >& arguments )
    {
        if (arguments.getLength() <= 2)
            return -1;

        sal_IntPtr pIntPtr = 0;

        arguments[ 2 ] >>= pIntPtr;

        SystemChildWindow *pParentWindow = reinterpret_cast< SystemChildWindow* >( pIntPtr );

        const SystemEnvData* pEnvData = pParentWindow ? pParentWindow->GetSystemData() : NULL;

        if (pEnvData == NULL)
            return -1;

#if defined MACOSX
        const intptr_t id = reinterpret_cast<intptr_t>( pEnvData->mpNSView );
#elif defined WNT
        const intptr_t id = reinterpret_cast<intptr_t>( pEnvData->hWnd );
#else
        const intptr_t id = static_cast<intptr_t>( pEnvData->aWindow );
#endif

        return id;
    }
}

void SAL_CALL VLCPlayer::setWindowID( const intptr_t windowID )
{
    ::osl::MutexGuard aGuard( m_aMutex );
    mPlayer.stop();
    mPlayer.setWindow( windowID );
}

void VLCPlayer::setVideoSize( unsigned width, unsigned height )
{
    ::osl::MutexGuard aGuard( m_aMutex );
    mPlayer.setVideoSize( width, height );
}

uno::Reference< css::media::XPlayerWindow > SAL_CALL VLCPlayer::createPlayerWindow( const uno::Sequence< uno::Any >& aArguments )
     throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    ::osl::MutexGuard aGuard( m_aMutex );

    const intptr_t winID = GetWindowID( aArguments );
    VLCWindow * window;
    if ( mPrevWinID == 0 )
    {
        mPrevWinID = winID;
        window = new VLCWindow( *this, 0 );
    }
    else
        window = new VLCWindow( *this, mPrevWinID );

    if ( winID != -1 )
    {
        setWindowID( winID );
    }

    return ::com::sun::star::uno::Reference< css::media::XPlayerWindow >( window );
}

uno::Reference< css::media::XFrameGrabber > SAL_CALL VLCPlayer::createFrameGrabber()
     throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    ::osl::MutexGuard aGuard(m_aMutex);

    if ( !mrFrameGrabber.is() )
    {
        VLCFrameGrabber *frameGrabber = new VLCFrameGrabber( mEventHandler, mUrl );
        mrFrameGrabber = uno::Reference< css::media::XFrameGrabber >( frameGrabber );
    }

    return mrFrameGrabber;
}

::rtl::OUString SAL_CALL VLCPlayer::getImplementationName()
     throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    return AVMEDIA_VLC_PLAYER_IMPLEMENTATIONNAME;
}

::sal_Bool SAL_CALL VLCPlayer::supportsService( const ::rtl::OUString& serviceName )
     throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    return cppu::supportsService(this, serviceName);
}

::uno::Sequence< ::rtl::OUString > SAL_CALL VLCPlayer::getSupportedServiceNames()
     throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
    uno::Sequence< OUString > aRet(1);
    aRet[0] = AVMEDIA_VLC_PLAYER_SERVICENAME;
    return aRet;
}

}
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */