VLCMediaPlayer.cpp 7.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************
 * VLCMediaPlayer.cpp: Binding for libvlc_media_player
 *****************************************************************************
 * Copyright (C) 2008-2009 the VLMC team
 *
 * Authors: Hugo Beauzee-Luyssen <hugo@vlmc.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

23
#include <QtDebug>
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
24 25
#include <cassert>
#include "VLCMediaPlayer.h"
26
#include "VLCInstance.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
27 28 29

using namespace LibVLCpp;

30 31 32
MediaPlayer::MediaPlayer()
{
    m_internalPtr = libvlc_media_player_new( LibVLCpp::Instance::getInstance()->getInternalPtr(), m_ex );
33
    CheckVlcppException( m_ex );
34 35 36 37 38 39 40 41 42 43 44 45

    // Initialize the event manager
    p_em = libvlc_media_player_event_manager( m_internalPtr, m_ex );

    // Register the callback
    libvlc_event_attach( p_em, libvlc_MediaPlayerSnapshotTaken, callbacks, this, m_ex );
    libvlc_event_attach( p_em, libvlc_MediaPlayerTimeChanged, callbacks, this, m_ex );
    libvlc_event_attach( p_em, libvlc_MediaPlayerPlaying, callbacks, this, m_ex );
    libvlc_event_attach( p_em, libvlc_MediaPlayerPaused, callbacks, this, m_ex );
    libvlc_event_attach( p_em, libvlc_MediaPlayerStopped, callbacks, this, m_ex );
    libvlc_event_attach( p_em, libvlc_MediaPlayerEndReached, callbacks, this, m_ex );
    libvlc_event_attach( p_em, libvlc_MediaPlayerPositionChanged, callbacks, this, m_ex );
46
    libvlc_event_attach( p_em, libvlc_MediaPlayerLengthChanged, callbacks,this,m_ex );
47 48
}

49
MediaPlayer::MediaPlayer( Media* media )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
50
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
51
    m_internalPtr = libvlc_media_player_new_from_media( media->getInternalPtr(), m_ex );
52
    CheckVlcppException( m_ex );
Ludovic Fauvet's avatar
Ludovic Fauvet committed
53

Ludovic Fauvet's avatar
Ludovic Fauvet committed
54
    // Initialize the event manager
Ludovic Fauvet's avatar
Ludovic Fauvet committed
55
    p_em = libvlc_media_player_event_manager( m_internalPtr, m_ex );
Ludovic Fauvet's avatar
Ludovic Fauvet committed
56 57

    // Register the callback
Ludovic Fauvet's avatar
Ludovic Fauvet committed
58
    libvlc_event_attach( p_em, libvlc_MediaPlayerSnapshotTaken, callbacks, this, m_ex );
59
    libvlc_event_attach( p_em, libvlc_MediaPlayerTimeChanged, callbacks, this, m_ex );
60
    libvlc_event_attach( p_em, libvlc_MediaPlayerPlaying, callbacks, this, m_ex );
61 62 63
    libvlc_event_attach( p_em, libvlc_MediaPlayerPaused, callbacks, this, m_ex );
    libvlc_event_attach( p_em, libvlc_MediaPlayerStopped, callbacks, this, m_ex );
    libvlc_event_attach( p_em, libvlc_MediaPlayerEndReached, callbacks, this, m_ex );
64
    libvlc_event_attach( p_em, libvlc_MediaPlayerPositionChanged, callbacks, this, m_ex );
Ludovic Fauvet's avatar
Ludovic Fauvet committed
65 66
}

Ludovic Fauvet's avatar
Ludovic Fauvet committed
67 68 69
/**
 * Event dispatcher.
 */
Ludovic Fauvet's avatar
Ludovic Fauvet committed
70 71 72 73 74
void                            MediaPlayer::callbacks( const libvlc_event_t* event, void* ptr )
{
    MediaPlayer* self = reinterpret_cast<MediaPlayer*>( ptr );
    switch ( event->type )
    {
75 76 77
    case libvlc_MediaPlayerPlaying:
        self->emit playing();
        break;
78 79
    case libvlc_MediaPlayerPaused:
        self->emit paused();
80
        break;
81 82
    case libvlc_MediaPlayerStopped:
        self->emit stopped();
83
        break;
84 85
    case libvlc_MediaPlayerEndReached:
        self->emit endReached();
86
        break;
87
    case libvlc_MediaPlayerTimeChanged:
88 89
        //self->timeChangedFilter();
        self->emit timeChanged();
90
        break;
91 92 93
    case libvlc_MediaPlayerPositionChanged:
        self->emit positionChanged();
        break;
94 95 96
    case libvlc_MediaPlayerLengthChanged:
        self->emit lengthChanged();
        break;
Ludovic Fauvet's avatar
Ludovic Fauvet committed
97 98 99
    case libvlc_MediaPlayerSnapshotTaken:
        self->emit snapshotTaken();
        break;
100 101 102 103 104 105 106 107 108
    case libvlc_MediaPlayerSeekableChanged:
    case libvlc_MediaPlayerPausableChanged:
    case libvlc_MediaPlayerTitleChanged:
    case libvlc_MediaPlayerNothingSpecial:
    case libvlc_MediaPlayerOpening:
    case libvlc_MediaPlayerBuffering:
    case libvlc_MediaPlayerForward:
    case libvlc_MediaPlayerBackward:
    case libvlc_MediaPlayerEncounteredError:
Ludovic Fauvet's avatar
Ludovic Fauvet committed
109
    default:
110
        qDebug() << "Unknown mediaPlayerEvent: " << event->type;
Ludovic Fauvet's avatar
Ludovic Fauvet committed
111 112
        break;
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
113 114 115 116
}

void                            MediaPlayer::play()
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
117
    libvlc_media_player_play( m_internalPtr, m_ex );
118
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
119 120 121 122
}

void                            MediaPlayer::pause()
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
123
    libvlc_media_player_pause( m_internalPtr, m_ex );
124
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
125 126 127 128
}

void                            MediaPlayer::stop()
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
129
    libvlc_media_player_stop( m_internalPtr, m_ex );
130
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
131 132 133 134
}

qint64                          MediaPlayer::getTime()
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
135
    qint64 t = libvlc_media_player_get_time( m_internalPtr, m_ex );
136
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
137 138 139
    return t;
}

Ludovic Fauvet's avatar
Ludovic Fauvet committed
140
void                            MediaPlayer::setTime( qint64 time )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
141
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
142
    libvlc_media_player_set_time( m_internalPtr, time, m_ex );
143
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
144 145
}

146 147 148
float                           MediaPlayer::getPosition()
{
    float p = libvlc_media_player_get_position( m_internalPtr, m_ex );
149
    CheckVlcppException( m_ex );
150 151 152 153 154 155
    return p;
}

void                            MediaPlayer::setPosition( float pos )
{
    libvlc_media_player_set_position( m_internalPtr, pos, m_ex );
156
    CheckVlcppException( m_ex );
157 158
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
159 160
qint64                          MediaPlayer::getLength()
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
161
    qint64 length = libvlc_media_player_get_length( m_internalPtr, m_ex );
162
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
163 164
    return length;
}
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
165

166
void                            MediaPlayer::takeSnapshot( const char* outputFile, unsigned int width, unsigned int heigth )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
167
{
168 169
    libvlc_video_take_snapshot( *this, outputFile, width, heigth, m_ex );
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
170
}
171

172
bool                                MediaPlayer::isPlaying()
173
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
174
    int res = libvlc_media_player_is_playing( m_internalPtr, m_ex );
175
    CheckVlcppException( m_ex );
176
    return (res == 1);
177 178
}

179
bool                                MediaPlayer::isSeekable()
180
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
181
    int res = libvlc_media_player_is_seekable( m_internalPtr, m_ex );
182
    CheckVlcppException( m_ex );
183
    return (res == 1);
184
}
185

186
void                                MediaPlayer::setDrawable( void* hwnd )
187
{
188
    libvlc_media_player_set_hwnd( m_internalPtr, hwnd, m_ex );
189
    CheckVlcppException( m_ex );
190 191 192 193 194
}

void                                MediaPlayer::setDrawable( uint32_t drawable )
{
    libvlc_media_player_set_xwindow( m_internalPtr, drawable, m_ex );
195
    CheckVlcppException( m_ex );
196
}
197 198 199 200 201 202 203 204 205 206 207

void                                MediaPlayer::timeChangedFilter()
{
    // Don't flood the gui with too many signals
    qint64 currentTime = getTime() / 100;
    static qint64 lastTime = 0;

    if ( currentTime != lastTime )
        emit timeChanged();
    lastTime = currentTime;
}
208 209 210 211

void                                MediaPlayer::setMedia( Media* media )
{
    libvlc_media_player_set_media( m_internalPtr, media->getInternalPtr(), m_ex);
212
    CheckVlcppException( m_ex );
213
}
214 215 216 217

int                                 MediaPlayer::getWidth()
{
    int width = libvlc_video_get_width( m_internalPtr, m_ex );
218
    CheckVlcppException( m_ex );
219 220 221 222 223 224
    return width;
}

int                                 MediaPlayer::getHeight()
{
    int height = libvlc_video_get_height( m_internalPtr, m_ex );
225
    CheckVlcppException( m_ex );
226 227
    return height;
}