VLCMediaPlayer.cpp 7.8 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
        qDebug() << "Event received" << event->type;
76
    case libvlc_MediaPlayerPlaying:
77
//        qDebug() << "Media player playing";
78 79
        self->emit playing();
        break;
80
    case libvlc_MediaPlayerPaused:
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
81
//        qDebug() << "Media player paused";
82
        self->emit paused();
83
        break;
84 85
    case libvlc_MediaPlayerStopped:
        self->emit stopped();
86
        break;
87 88
    case libvlc_MediaPlayerEndReached:
        self->emit endReached();
89
        break;
90
    case libvlc_MediaPlayerTimeChanged:
91
        self->emit timeChanged();
92
        break;
93 94 95
    case libvlc_MediaPlayerPositionChanged:
        self->emit positionChanged();
        break;
96 97 98
    case libvlc_MediaPlayerLengthChanged:
        self->emit lengthChanged();
        break;
Ludovic Fauvet's avatar
Ludovic Fauvet committed
99 100 101
    case libvlc_MediaPlayerSnapshotTaken:
        self->emit snapshotTaken();
        break;
102 103 104 105 106 107 108 109 110
    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
111
    default:
112
        qDebug() << "Unknown mediaPlayerEvent: " << event->type;
Ludovic Fauvet's avatar
Ludovic Fauvet committed
113 114
        break;
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
115 116 117 118
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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;
}
210 211 212 213

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

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

int                                 MediaPlayer::getHeight()
{
    int height = libvlc_video_get_height( m_internalPtr, m_ex );
227
    CheckVlcppException( m_ex );
228 229
    return height;
}
230 231 232 233 234 235 236

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