File indexing completed on 2024-12-01 04:21:25

0001 /*
0002     Copyright (C) 2011 Harald Sitter <sitter@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Lesser General Public
0006     License as published by the Free Software Foundation; either
0007     version 2.1 of the License, or (at your option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Lesser General Public License for more details.
0013 
0014     You should have received a copy of the GNU Lesser General Public
0015     License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0016 */
0017 
0018 #include "media.h"
0019 
0020 #include <QtCore/QDebug>
0021 
0022 #include <vlc/vlc.h>
0023 
0024 #include "utils/debug.h"
0025 #include "utils/libvlc.h"
0026 #include "utils/vstring.h"
0027 
0028 namespace Phonon {
0029 namespace VLC {
0030 
0031 Media::Media(const QByteArray &mrl, QObject *parent) :
0032     QObject(parent),
0033     m_media(libvlc_media_new_location(pvlc_libvlc, mrl.constData())),
0034     m_mrl(mrl)
0035 {
0036     Q_ASSERT(m_media);
0037 
0038     libvlc_event_manager_t *manager = libvlc_media_event_manager(m_media);
0039     libvlc_event_type_t events[] = {
0040         libvlc_MediaMetaChanged,
0041         libvlc_MediaSubItemAdded,
0042         libvlc_MediaDurationChanged,
0043         libvlc_MediaParsedChanged,
0044         libvlc_MediaFreed,
0045         libvlc_MediaStateChanged
0046     };
0047     const int eventCount = sizeof(events) / sizeof(*events);
0048     for (int i = 0; i < eventCount; ++i) {
0049         libvlc_event_attach(manager, events[i], event_cb, this);
0050     }
0051 }
0052 
0053 Media::~Media()
0054 {
0055     if (m_media) {
0056         libvlc_media_release(m_media);
0057         m_media = 0;
0058     }
0059 }
0060 
0061 void Media::addOption(const QString &option)
0062 {
0063     libvlc_media_add_option_flag(m_media,
0064                                  option.toUtf8().data(),
0065                                  libvlc_media_option_trusted);
0066 }
0067 
0068 QString Media::meta(libvlc_meta_t meta)
0069 {
0070     return VString(libvlc_media_get_meta(m_media, meta)).toQString();
0071 }
0072 
0073 void Media::event_cb(const libvlc_event_t *event, void *opaque)
0074 {
0075     Media *that = reinterpret_cast<Media *>(opaque);
0076     Q_ASSERT(that);
0077 
0078     switch (event->type) {
0079     case libvlc_MediaDurationChanged:
0080         QMetaObject::invokeMethod(
0081                     that, "durationChanged",
0082                     Qt::QueuedConnection,
0083                     Q_ARG(qint64, event->u.media_duration_changed.new_duration));
0084         break;
0085     case libvlc_MediaMetaChanged:
0086         QMetaObject::invokeMethod(
0087                     that, "metaDataChanged",
0088                     Qt::QueuedConnection);
0089         break;
0090     case libvlc_MediaSubItemAdded:
0091     case libvlc_MediaParsedChanged:
0092     case libvlc_MediaFreed:
0093     case libvlc_MediaStateChanged:
0094         break;
0095     }
0096 }
0097 
0098 void Media::setCdTrack(int track)
0099 {
0100     debug() << "setting CDDA track" << track;
0101     addOption(QLatin1String(":cdda-track="), QVariant(track));
0102 }
0103 
0104 } // namespace VLC
0105 } // namespace Phonon