]> git.sesse.net Git - vlc/blob - bindings/phonon/vlc/vlcmediacontroller.cpp
Add some locking.
[vlc] / bindings / phonon / vlc / vlcmediacontroller.cpp
1 /*****************************************************************************
2  * VLC backend for the Phonon library                                        *
3  * Copyright (C) 2007-2008 Tanguy Krotoff <tkrotoff@gmail.com>               *
4  * Copyright (C) 2008 Lukas Durfina <lukas.durfina@gmail.com>                *
5  * Copyright (C) 2009 Fathi Boudra <fabo@kde.org>                            *
6  *                                                                           *
7  * This program is free software; you can redistribute it and/or             *
8  * modify it under the terms of the GNU Lesser General Public                *
9  * License as published by the Free Software Foundation; either              *
10  * version 3 of the License, or (at your option) any later version.          *
11  *                                                                           *
12  * This program is distributed in the hope that it will be useful,           *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
15  * Lesser General Public License for more details.                           *
16  *                                                                           *
17  * You should have received a copy of the GNU Lesser General Public          *
18  * License along with this package; if not, write to the Free Software       *
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA *
20  *****************************************************************************/
21
22 #include "vlcmediacontroller.h"
23
24 #include "vlcloader.h"
25
26 namespace Phonon
27 {
28 namespace VLC {
29
30 VLCMediaController::VLCMediaController()
31         : MediaController()
32 {
33     p_vlc_media_player = 0;
34 }
35
36 VLCMediaController::~VLCMediaController()
37 {
38 }
39
40 void VLCMediaController::clearMediaController()
41 {
42     current_audio_channel = Phonon::AudioChannelDescription();
43     available_audio_channels.clear();
44
45     current_subtitle = Phonon::SubtitleDescription();
46     available_subtitles.clear();
47
48     i_current_angle = 0;
49     i_available_angles = 0;
50
51 //    current_chapter = Phonon::ChapterDescription();
52 //    available_chapters.clear();
53     current_chapter = 0;
54     available_chapters = 0;
55
56 //    current_title = Phonon::TitleDescription();
57 //    available_titles.clear();
58     current_title = 0;
59     available_titles = 0;
60
61     b_autoplay_titles = false;
62
63     emit availableAudioChannelsChanged();
64     emit availableSubtitlesChanged();
65     emit availableTitlesChanged(0);
66     emit availableChaptersChanged(0);
67 }
68
69 // Add audio channel -> in libvlc it is track, it means audio in another language
70 void VLCMediaController::audioChannelAdded(int id, const QString & lang)
71 {
72     QHash<QByteArray, QVariant> properties;
73     properties.insert("name", lang);
74     properties.insert("description", "");
75
76     available_audio_channels << Phonon::AudioChannelDescription(id, properties);
77     emit availableAudioChannelsChanged();
78 }
79
80 // Add subtitle
81 void VLCMediaController::subtitleAdded(int id, const QString & lang, const QString & type)
82 {
83     QHash<QByteArray, QVariant> properties;
84     properties.insert("name", lang);
85     properties.insert("description", "");
86     properties.insert("type", type);
87
88     available_subtitles << Phonon::SubtitleDescription(id, properties);
89     emit availableSubtitlesChanged();
90 }
91
92 // Add title
93 void VLCMediaController::titleAdded(int id, const QString & name)
94 {
95 //    QHash<QByteArray, QVariant> properties;
96 //    properties.insert("name", name);
97 //    properties.insert("description", "");
98
99 //    available_titles << Phonon::TitleDescription(id, properties);
100     available_titles++;
101     emit availableTitlesChanged(available_titles);
102 }
103
104 // Add chapter
105 void VLCMediaController::chapterAdded(int titleId, const QString & name)
106 {
107 //    QHash<QByteArray, QVariant> properties;
108 //    properties.insert("name", name);
109 //    properties.insert("description", "");
110
111 //    available_chapters << Phonon::ChapterDescription(titleId, properties);
112     available_chapters++;
113     emit availableChaptersChanged(available_chapters);
114 }
115
116 // Audio channel
117
118 void VLCMediaController::setCurrentAudioChannel(const Phonon::AudioChannelDescription & audioChannel)
119 {
120     current_audio_channel = audioChannel;
121     libvlc_audio_set_track(p_vlc_media_player, audioChannel.index(), vlc_exception);
122     vlcExceptionRaised();
123 }
124
125 QList<Phonon::AudioChannelDescription> VLCMediaController::availableAudioChannels() const
126 {
127     return available_audio_channels;
128 }
129
130 Phonon::AudioChannelDescription VLCMediaController::currentAudioChannel() const
131 {
132     return current_audio_channel;
133 }
134
135 void VLCMediaController::refreshAudioChannels()
136 {
137     current_audio_channel = Phonon::AudioChannelDescription();
138     available_audio_channels.clear();
139
140     libvlc_track_description_t * p_info = libvlc_audio_get_track_description(
141                                               p_vlc_media_player, vlc_exception);
142     vlcExceptionRaised();
143     while (p_info) {
144         audioChannelAdded(p_info->i_id, p_info->psz_name);
145         p_info = p_info->p_next;
146     }
147     libvlc_track_description_release(p_info);
148 }
149
150 // Subtitle
151
152 void VLCMediaController::setCurrentSubtitle(const Phonon::SubtitleDescription & subtitle)
153 {
154     current_subtitle = subtitle;
155 //    int id = current_subtitle.index();
156     QString type = current_subtitle.property("type").toString();
157
158     if (type == "file") {
159         QString filename = current_subtitle.property("name").toString();
160         if (!filename.isEmpty()) {
161             libvlc_video_set_subtitle_file(p_vlc_media_player,
162                                            filename.toAscii().data(),
163                                            vlc_exception);
164             vlcExceptionRaised();
165
166             // There is no subtitle event inside libvlc so let's send our own event...
167             available_subtitles << current_subtitle;
168             emit availableSubtitlesChanged();
169         }
170     } else {
171         libvlc_video_set_spu(p_vlc_media_player, subtitle.index(), vlc_exception);
172         vlcExceptionRaised();
173     }
174 }
175
176 QList<Phonon::SubtitleDescription> VLCMediaController::availableSubtitles() const
177 {
178     return available_subtitles;
179 }
180
181 Phonon::SubtitleDescription VLCMediaController::currentSubtitle() const
182 {
183     return current_subtitle;
184 }
185
186 void VLCMediaController::refreshSubtitles()
187 {
188     current_subtitle = Phonon::SubtitleDescription();
189     available_subtitles.clear();
190
191     libvlc_track_description_t *p_info = libvlc_video_get_spu_description(
192                                              p_vlc_media_player, vlc_exception);
193     vlcExceptionRaised();
194     while (p_info) {
195         subtitleAdded(p_info->i_id, p_info->psz_name, "");
196         p_info = p_info->p_next;
197     }
198     libvlc_track_description_release(p_info);
199 }
200
201 // Title
202
203 //void VLCMediaController::setCurrentTitle( const Phonon::TitleDescription & title )
204 void VLCMediaController::setCurrentTitle(int title)
205 {
206     current_title = title;
207
208 //    libvlc_media_player_set_title(p_vlc_media_player, title.index(), vlc_exception);
209     libvlc_media_player_set_title(p_vlc_media_player, title, vlc_exception);
210     vlcExceptionRaised();
211 }
212
213 //QList<Phonon::TitleDescription> VLCMediaController::availableTitles() const
214 int VLCMediaController::availableTitles() const
215 {
216     return available_titles;
217 }
218
219 //Phonon::TitleDescription VLCMediaController::currentTitle() const
220 int VLCMediaController::currentTitle() const
221 {
222     return current_title;
223 }
224
225 void VLCMediaController::setAutoplayTitles(bool autoplay)
226 {
227     b_autoplay_titles = autoplay;
228 }
229
230 bool VLCMediaController::autoplayTitles() const
231 {
232     return b_autoplay_titles;
233 }
234
235 // Chapter
236
237 //void VLCMediaController::setCurrentChapter(const Phonon::ChapterDescription &chapter)
238 void VLCMediaController::setCurrentChapter(int chapter)
239 {
240     current_chapter = chapter;
241 //    libvlc_media_player_set_chapter(p_vlc_media_player, chapter.index(), vlc_exception);
242     libvlc_media_player_set_chapter(p_vlc_media_player, chapter, vlc_exception);
243     vlcExceptionRaised();
244 }
245
246 //QList<Phonon::ChapterDescription> VLCMediaController::availableChapters() const
247 int VLCMediaController::availableChapters() const
248 {
249     return available_chapters;
250 }
251
252 //Phonon::ChapterDescription VLCMediaController::currentChapter() const
253 int VLCMediaController::currentChapter() const
254 {
255     return current_chapter;
256 }
257
258 // We need to rebuild available chapters when title is changed
259 void VLCMediaController::refreshChapters(int i_title)
260 {
261 //    current_chapter = Phonon::ChapterDescription();
262 //    available_chapters.clear();
263     current_chapter = 0;
264     available_chapters = 0;
265
266     // Get the description of available chapters for specific title
267     libvlc_track_description_t *p_info = libvlc_video_get_chapter_description(
268                                              p_vlc_media_player, i_title, vlc_exception);
269     vlcExceptionRaised();
270     while (p_info) {
271         chapterAdded(p_info->i_id, p_info->psz_name);
272         p_info = p_info->p_next;
273     }
274     libvlc_track_description_release(p_info);
275 }
276
277 // Angle
278
279 void VLCMediaController::setCurrentAngle(int angleNumber)
280 {
281     i_current_angle = angleNumber;
282 }
283
284 int VLCMediaController::availableAngles() const
285 {
286     return i_available_angles;
287 }
288
289 int VLCMediaController::currentAngle() const
290 {
291     return i_current_angle;
292 }
293
294 }
295 } // Namespace Phonon::VLC