VLCMediaPlayer.cpp 8.43 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
}

67 68 69 70 71 72 73 74 75 76 77 78
MediaPlayer::~MediaPlayer()
{
    libvlc_event_detach( p_em, libvlc_MediaPlayerSnapshotTaken, callbacks, this, m_ex );
    libvlc_event_detach( p_em, libvlc_MediaPlayerTimeChanged, callbacks, this, m_ex );
    libvlc_event_detach( p_em, libvlc_MediaPlayerPlaying, callbacks, this, m_ex );
    libvlc_event_detach( p_em, libvlc_MediaPlayerPaused, callbacks, this, m_ex );
    libvlc_event_detach( p_em, libvlc_MediaPlayerStopped, callbacks, this, m_ex );
    libvlc_event_detach( p_em, libvlc_MediaPlayerEndReached, callbacks, this, m_ex );
    libvlc_event_detach( p_em, libvlc_MediaPlayerPositionChanged, callbacks, this, m_ex );
    libvlc_media_player_release( m_internalPtr );
}

Ludovic Fauvet's avatar
Ludovic Fauvet committed
79 80 81
/**
 * Event dispatcher.
 */
Ludovic Fauvet's avatar
Ludovic Fauvet committed
82 83 84 85 86
void                            MediaPlayer::callbacks( const libvlc_event_t* event, void* ptr )
{
    MediaPlayer* self = reinterpret_cast<MediaPlayer*>( ptr );
    switch ( event->type )
    {
87
    case libvlc_MediaPlayerPlaying:
88
//        qDebug() << "Media player playing";
89 90
        self->emit playing();
        break;
91
    case libvlc_MediaPlayerPaused:
92
//        qDebug() << "Media player paused";
93
        self->emit paused();
94
        break;
95 96
    case libvlc_MediaPlayerStopped:
        self->emit stopped();
97
        break;
98 99
    case libvlc_MediaPlayerEndReached:
        self->emit endReached();
100
        break;
101
    case libvlc_MediaPlayerTimeChanged:
102
        self->emit timeChanged();
103
        break;
104 105 106
    case libvlc_MediaPlayerPositionChanged:
        self->emit positionChanged();
        break;
107 108 109
//    case libvlc_MediaPlayerLengthChanged:
//        self->emit lengthChanged();
//        break;
Ludovic Fauvet's avatar
Ludovic Fauvet committed
110 111 112
    case libvlc_MediaPlayerSnapshotTaken:
        self->emit snapshotTaken();
        break;
113 114 115 116 117 118 119 120 121
    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
122
    default:
123
        qDebug() << "Unknown mediaPlayerEvent: " << event->type;
Ludovic Fauvet's avatar
Ludovic Fauvet committed
124 125
        break;
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
126 127 128 129
}

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

void                            MediaPlayer::pause()
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
136
    libvlc_media_player_pause( m_internalPtr, m_ex );
137
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
138 139 140 141
}

void                            MediaPlayer::stop()
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
142
    libvlc_media_player_stop( m_internalPtr, m_ex );
143
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
144 145 146 147
}

qint64                          MediaPlayer::getTime()
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
148
    qint64 t = libvlc_media_player_get_time( m_internalPtr, m_ex );
149
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
150 151 152
    return t;
}

Ludovic Fauvet's avatar
Ludovic Fauvet committed
153
void                            MediaPlayer::setTime( qint64 time )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
154
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
155
    libvlc_media_player_set_time( m_internalPtr, time, m_ex );
156
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
157 158
}

159 160 161
float                           MediaPlayer::getPosition()
{
    float p = libvlc_media_player_get_position( m_internalPtr, m_ex );
162
    CheckVlcppException( m_ex );
163 164 165 166 167 168
    return p;
}

void                            MediaPlayer::setPosition( float pos )
{
    libvlc_media_player_set_position( m_internalPtr, pos, m_ex );
169
    CheckVlcppException( m_ex );
170 171
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
172 173
qint64                          MediaPlayer::getLength()
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
174
    qint64 length = libvlc_media_player_get_length( m_internalPtr, m_ex );
175
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
176 177
    return length;
}
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
178

179
void                            MediaPlayer::takeSnapshot( const char* outputFile, unsigned int width, unsigned int heigth )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
180
{
181 182
    libvlc_video_take_snapshot( *this, outputFile, width, heigth, m_ex );
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
183
}
184

185
bool                            MediaPlayer::isPlaying()
186
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
187
    int res = libvlc_media_player_is_playing( m_internalPtr, m_ex );
188
    CheckVlcppException( m_ex );
189
    return (res == 1);
190 191
}

192
bool                                MediaPlayer::isSeekable()
193
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
194
    int res = libvlc_media_player_is_seekable( m_internalPtr, m_ex );
195
    CheckVlcppException( m_ex );
196
    return (res == 1);
197
}
198

199
void                                MediaPlayer::setDrawable( void* hwnd )
200
{
201
    libvlc_media_player_set_hwnd( m_internalPtr, hwnd, m_ex );
202
    CheckVlcppException( m_ex );
203 204 205 206 207
}

void                                MediaPlayer::setDrawable( uint32_t drawable )
{
    libvlc_media_player_set_xwindow( m_internalPtr, drawable, m_ex );
208
    CheckVlcppException( m_ex );
209
}
210 211 212 213 214 215 216 217 218 219 220

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;
}
221 222 223 224

void                                MediaPlayer::setMedia( Media* media )
{
    libvlc_media_player_set_media( m_internalPtr, media->getInternalPtr(), m_ex);
225
    CheckVlcppException( m_ex );
226
}
227 228 229 230

int                                 MediaPlayer::getWidth()
{
    int width = libvlc_video_get_width( m_internalPtr, m_ex );
231
    CheckVlcppException( m_ex );
232 233 234 235 236 237
    return width;
}

int                                 MediaPlayer::getHeight()
{
    int height = libvlc_video_get_height( m_internalPtr, m_ex );
238
    CheckVlcppException( m_ex );
239 240
    return height;
}
241 242 243 244 245 246 247

float                               MediaPlayer::getFps()
{
    float   fps = libvlc_media_player_get_fps( m_internalPtr, m_ex );
    CheckVlcppException( m_ex );
    return fps;
}