VLCMediaPlayer.cpp 9.2 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 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 );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
46
    libvlc_event_attach( p_em, libvlc_MediaPlayerLengthChanged, callbacks,this,m_ex );
47 48
}

49
MediaPlayer::MediaPlayer( Media* media ) : m_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
    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();
104
        break;
105 106 107
    case libvlc_MediaPlayerPositionChanged:
        self->emit positionChanged();
        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 118 119 120 121 122
    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
123
    default:
124
        qDebug() << "Unknown mediaPlayerEvent: " << event->type;
Ludovic Fauvet's avatar
Ludovic Fauvet committed
125 126
        break;
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
127 128 129 130
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

float                               MediaPlayer::getFps()
{
    float   fps = libvlc_media_player_get_fps( m_internalPtr, m_ex );
    CheckVlcppException( m_ex );
    return fps;
}
249 250 251 252 253 254

void                                MediaPlayer::nextFrame()
{
    libvlc_media_player_next_frame( m_internalPtr, m_ex );
    CheckVlcppException( m_ex );
}
255 256 257 258 259 260 261

bool                                MediaPlayer::hasVout()
{
    bool    res = libvlc_media_player_has_vout( m_internalPtr, m_ex );
    CheckVlcppException( m_ex );
    return res;
}
262 263 264 265 266 267 268 269 270 271 272 273 274

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 );
}