VLCMediaPlayer.cpp 8.99 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
MediaPlayer::MediaPlayer() : m_media( NULL )
31 32
{
    m_internalPtr = libvlc_media_player_new( LibVLCpp::Instance::getInstance()->getInternalPtr(), m_ex );
33
    CheckVlcppException( m_ex );
34 35 36

    // Initialize the event manager
    p_em = libvlc_media_player_event_manager( m_internalPtr, m_ex );
37
    registerEvents();
38 39
}

40
MediaPlayer::MediaPlayer( Media* media ) : m_media( media )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
41
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
42
    m_internalPtr = libvlc_media_player_new_from_media( media->getInternalPtr(), m_ex );
43
    CheckVlcppException( m_ex );
Ludovic Fauvet's avatar
Ludovic Fauvet committed
44

Ludovic Fauvet's avatar
Ludovic Fauvet committed
45
    // Initialize the event manager
Ludovic Fauvet's avatar
Ludovic Fauvet committed
46
    p_em = libvlc_media_player_event_manager( m_internalPtr, m_ex );
47
    registerEvents();
Ludovic Fauvet's avatar
Ludovic Fauvet committed
48 49
}

50 51 52 53 54 55 56 57 58 59 60 61
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 );
}

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
void
MediaPlayer::registerEvents()
{
    // 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 );
    libvlc_event_attach( p_em, libvlc_MediaPlayerLengthChanged,   callbacks, this, m_ex );
    libvlc_event_attach( p_em, libvlc_MediaPlayerEncounteredError,callbacks, this, m_ex );
    libvlc_event_attach( p_em, libvlc_MediaPlayerPausableChanged, callbacks, this, m_ex );
    libvlc_event_attach( p_em, libvlc_MediaPlayerSeekableChanged, callbacks, this, m_ex );
}

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
    case libvlc_MediaPlayerStopped:
96
        qDebug() << "Media player stopped";
97
        self->emit stopped();
98
        break;
99 100
    case libvlc_MediaPlayerEndReached:
        self->emit endReached();
101
        break;
102
    case libvlc_MediaPlayerTimeChanged:
103
        self->emit timeChanged( event->u.media_player_time_changed.new_time / 1000 );
104
        break;
105
    case libvlc_MediaPlayerPositionChanged:
106
        self->emit positionChanged( event->u.media_player_position_changed.new_position );
107
        break;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
108 109 110
    case libvlc_MediaPlayerLengthChanged:
        self->emit lengthChanged();
        break;
Ludovic Fauvet's avatar
Ludovic Fauvet committed
111 112 113
    case libvlc_MediaPlayerSnapshotTaken:
        self->emit snapshotTaken();
        break;
114 115 116 117
    case libvlc_MediaPlayerEncounteredError:
        qDebug() << "libvlc_MediaPlayerEncounteredError received."
                << "This is not looking good...";
        break ;
118 119 120 121 122 123 124 125
    case libvlc_MediaPlayerSeekableChanged:
    case libvlc_MediaPlayerPausableChanged:
    case libvlc_MediaPlayerTitleChanged:
    case libvlc_MediaPlayerNothingSpecial:
    case libvlc_MediaPlayerOpening:
    case libvlc_MediaPlayerBuffering:
    case libvlc_MediaPlayerForward:
    case libvlc_MediaPlayerBackward:
Ludovic Fauvet's avatar
Ludovic Fauvet committed
126
    default:
127
//        qDebug() << "Unknown mediaPlayerEvent: " << event->type;
Ludovic Fauvet's avatar
Ludovic Fauvet committed
128 129
        break;
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
130 131 132 133
}

void                            MediaPlayer::play()
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
134
    libvlc_media_player_play( m_internalPtr, m_ex );
135
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
136 137 138 139
}

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

void                            MediaPlayer::stop()
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
146
    libvlc_media_player_stop( m_internalPtr, m_ex );
147
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
148 149 150 151
}

qint64                          MediaPlayer::getTime()
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
152
    qint64 t = libvlc_media_player_get_time( m_internalPtr, m_ex );
153
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
154 155 156
    return t;
}

Ludovic Fauvet's avatar
Ludovic Fauvet committed
157
void                            MediaPlayer::setTime( qint64 time )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
158
{
159
    qDebug() << this << "MediaPlayer::setTime: setting time to" << time;
Ludovic Fauvet's avatar
Ludovic Fauvet committed
160
    libvlc_media_player_set_time( m_internalPtr, time, m_ex );
161
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
162 163
}

164 165 166
float                           MediaPlayer::getPosition()
{
    float p = libvlc_media_player_get_position( m_internalPtr, m_ex );
167
    CheckVlcppException( m_ex );
168 169 170 171 172 173
    return p;
}

void                            MediaPlayer::setPosition( float pos )
{
    libvlc_media_player_set_position( m_internalPtr, pos, m_ex );
174
    CheckVlcppException( m_ex );
175 176
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
177 178
qint64                          MediaPlayer::getLength()
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
179
    qint64 length = libvlc_media_player_get_length( m_internalPtr, m_ex );
180
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
181 182
    return length;
}
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
183

184
void                            MediaPlayer::takeSnapshot( const char* outputFile, unsigned int width, unsigned int heigth )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
185
{
186 187
    libvlc_video_take_snapshot( *this, outputFile, width, heigth, m_ex );
    CheckVlcppException( m_ex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
188
}
189

190
bool                            MediaPlayer::isPlaying()
191
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
192
    int res = libvlc_media_player_is_playing( m_internalPtr, m_ex );
193
    CheckVlcppException( m_ex );
194
    return (res == 1);
195 196
}

197
bool                                MediaPlayer::isSeekable()
198
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
199
    int res = libvlc_media_player_is_seekable( m_internalPtr, m_ex );
200
    CheckVlcppException( m_ex );
201
    return (res == 1);
202
}
203

204
void                                MediaPlayer::setDrawable( void* hwnd )
205
{
206
    libvlc_media_player_set_hwnd( m_internalPtr, hwnd, m_ex );
207
    CheckVlcppException( m_ex );
208 209 210 211 212
}

void                                MediaPlayer::setDrawable( uint32_t drawable )
{
    libvlc_media_player_set_xwindow( m_internalPtr, drawable, m_ex );
213
    CheckVlcppException( m_ex );
214
}
215

216 217 218
void                                MediaPlayer::setMedia( Media* media )
{
    libvlc_media_player_set_media( m_internalPtr, media->getInternalPtr(), m_ex);
219
    CheckVlcppException( m_ex );
220
}
221 222 223 224

int                                 MediaPlayer::getWidth()
{
    int width = libvlc_video_get_width( m_internalPtr, m_ex );
225
    CheckVlcppException( m_ex );
226 227 228 229 230 231
    return width;
}

int                                 MediaPlayer::getHeight()
{
    int height = libvlc_video_get_height( m_internalPtr, m_ex );
232
    CheckVlcppException( m_ex );
233 234
    return height;
}
235 236 237 238 239 240 241

float                               MediaPlayer::getFps()
{
    float   fps = libvlc_media_player_get_fps( m_internalPtr, m_ex );
    CheckVlcppException( m_ex );
    return fps;
}
242 243 244 245 246 247

void                                MediaPlayer::nextFrame()
{
    libvlc_media_player_next_frame( m_internalPtr, m_ex );
    CheckVlcppException( m_ex );
}
248 249 250 251 252 253 254

bool                                MediaPlayer::hasVout()
{
    bool    res = libvlc_media_player_has_vout( m_internalPtr, m_ex );
    CheckVlcppException( m_ex );
    return res;
}
255 256 257 258 259 260 261 262 263 264 265 266 267

const QString&                      MediaPlayer::getLoadedFileName() const
{
    return m_media->getFileName();
}

QString                             MediaPlayer::getLoadedMRL()
{
    Media::internalPtr     media = libvlc_media_player_get_media( m_internalPtr, m_ex );
    CheckVlcppException( m_ex );
    char* str = libvlc_media_get_mrl( media );
    return QString( str );
}