1 | /* |
2 | Copyright (C) 2011 Harald Sitter <sitter@kde.org> |
3 | |
4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2.1 of the License, or (at your option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with this library. If not, see <http://www.gnu.org/licenses/>. |
16 | */ |
17 | |
18 | #include "media.h" |
19 | |
20 | #include <QtCore/QDebug> |
21 | |
22 | #include <vlc/vlc.h> |
23 | |
24 | #include "utils/debug.h" |
25 | #include "utils/libvlc.h" |
26 | #include "utils/vstring.h" |
27 | |
28 | namespace Phonon { |
29 | namespace VLC { |
30 | |
31 | Media::Media(const QByteArray &mrl, QObject *parent) : |
32 | QObject(parent), |
33 | m_media(libvlc_media_new_location(pvlc_libvlc, psz_mrl: mrl.constData())), |
34 | m_mrl(mrl) |
35 | { |
36 | Q_ASSERT(m_media); |
37 | |
38 | libvlc_event_manager_t *manager = libvlc_media_event_manager(p_md: m_media); |
39 | libvlc_event_type_t events[] = { |
40 | libvlc_MediaMetaChanged, |
41 | libvlc_MediaSubItemAdded, |
42 | libvlc_MediaDurationChanged, |
43 | libvlc_MediaParsedChanged, |
44 | libvlc_MediaFreed, |
45 | libvlc_MediaStateChanged |
46 | }; |
47 | const int eventCount = sizeof(events) / sizeof(*events); |
48 | for (int i = 0; i < eventCount; ++i) { |
49 | libvlc_event_attach(p_event_manager: manager, i_event_type: events[i], f_callback: event_cb, user_data: this); |
50 | } |
51 | } |
52 | |
53 | Media::~Media() |
54 | { |
55 | if (m_media) { |
56 | libvlc_media_release(p_md: m_media); |
57 | m_media = 0; |
58 | } |
59 | } |
60 | |
61 | void Media::addOption(const QString &option) |
62 | { |
63 | libvlc_media_add_option_flag(p_md: m_media, |
64 | psz_options: option.toUtf8().data(), |
65 | i_flags: libvlc_media_option_trusted); |
66 | } |
67 | |
68 | QString Media::meta(libvlc_meta_t meta) |
69 | { |
70 | return VString(libvlc_media_get_meta(p_md: m_media, e_meta: meta)).toQString(); |
71 | } |
72 | |
73 | void Media::event_cb(const libvlc_event_t *event, void *opaque) |
74 | { |
75 | Media *that = reinterpret_cast<Media *>(opaque); |
76 | Q_ASSERT(that); |
77 | |
78 | switch (event->type) { |
79 | case libvlc_MediaDurationChanged: |
80 | QMetaObject::invokeMethod( |
81 | obj: that, member: "durationChanged" , |
82 | c: Qt::QueuedConnection, |
83 | Q_ARG(qint64, event->u.media_duration_changed.new_duration)); |
84 | break; |
85 | case libvlc_MediaMetaChanged: |
86 | QMetaObject::invokeMethod( |
87 | obj: that, member: "metaDataChanged" , |
88 | c: Qt::QueuedConnection); |
89 | break; |
90 | case libvlc_MediaSubItemAdded: |
91 | case libvlc_MediaParsedChanged: |
92 | case libvlc_MediaFreed: |
93 | case libvlc_MediaStateChanged: |
94 | break; |
95 | } |
96 | } |
97 | |
98 | void Media::setCdTrack(int track) |
99 | { |
100 | debug() << "setting CDDA track" << track; |
101 | addOption(option: QLatin1String(":cdda-track=" ), argument: QVariant(track)); |
102 | } |
103 | |
104 | } // namespace VLC |
105 | } // namespace Phonon |
106 | |