]> git.sesse.net Git - vlc/blob - bindings/phonon/vlc/devicemanager.cpp
Add some locking.
[vlc] / bindings / phonon / vlc / devicemanager.cpp
1 /*  This file is part of the KDE project.
2
3     Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
5     This library is free software: you can redistribute it and/or modify
6     it under the terms of the GNU Lesser General Public License as published by
7     the Free Software Foundation, either version 2.1 or 3 of the License.
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
12     GNU Lesser General Public License for more details.
13
14     You should have received a copy of the GNU Lesser General Public License
15     along with this library.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "devicemanager.h"
19 #include "backend.h"
20 //#include "videowidget.h"
21 //#include "widgetrenderer.h"
22 #include "vlcloader.h"
23
24 #ifdef PHONON_PULSESUPPORT
25 #  include <phonon/pulsesupport.h>
26 #endif
27
28 /**
29  * This class manages the list of currently active output devices.
30  */
31
32 QT_BEGIN_NAMESPACE
33
34 namespace Phonon
35 {
36 namespace VLC {
37
38 AudioDevice::AudioDevice(DeviceManager *manager, const QByteArray &deviceId, const QByteArray &hw_id)
39 {
40     // Get an id
41     static int counter = 0;
42     id = counter++;
43     // Get name from device
44     if (vlcId == "default") {
45         description = "Default audio device";
46     } else {
47         vlcId = deviceId;
48         description = "";
49     }
50     hwId = hw_id;
51 }
52
53 DeviceManager::DeviceManager(Backend *parent)
54         : QObject(parent)
55         , m_backend(parent)
56 {
57     updateDeviceList();
58 }
59
60 DeviceManager::~DeviceManager()
61 {
62     m_audioDeviceList.clear();
63 }
64
65 bool DeviceManager::canOpenDevice() const
66 {
67     return true;
68 }
69
70 /**
71  * Return a positive device id or -1 if device does not exist.
72  */
73 int DeviceManager::deviceId(const QByteArray &nameId) const
74 {
75     for (int i = 0 ; i < m_audioDeviceList.size() ; ++i) {
76         if (m_audioDeviceList[i].vlcId == nameId)
77             return m_audioDeviceList[i].id;
78     }
79     return -1;
80 }
81
82 /**
83  * Get a human-readable description from a device id.
84  */
85 QByteArray DeviceManager::deviceDescription(int i_id) const
86 {
87     for (int i = 0 ; i < m_audioDeviceList.size() ; ++i) {
88         if (m_audioDeviceList[i].id == i_id)
89             return m_audioDeviceList[i].description;
90     }
91     return QByteArray();
92 }
93
94 /**
95  * Update the current list of active devices.
96  */
97 void DeviceManager::updateDeviceList()
98 {
99     QList<QByteArray> list, list_hw;
100     list.append("default");
101     list_hw.append("");
102
103     // Get the list of available audio outputs
104     libvlc_audio_output_t *p_ao_list = libvlc_audio_output_list_get(
105                                            vlc_instance, vlc_exception);
106     vlcExceptionRaised();
107     libvlc_audio_output_t *p_start = p_ao_list;
108
109     bool checkpulse = false;
110 #ifdef PHONON_PULSESUPPORT
111     PulseSupport *pulse = PulseSupport::getInstance();
112     checkpulse = pulse->isActive();
113 #endif
114     bool haspulse = false;
115     while (p_ao_list) {
116         if (checkpulse && 0 == strcmp(p_ao_list->psz_name, "pulse")) {
117             haspulse = true;
118             break;
119         }
120         list.append(p_ao_list->psz_name);
121         list_hw.append("");
122         p_ao_list = p_ao_list->p_next;
123     }
124     libvlc_audio_output_list_release(p_start);
125
126
127 #ifdef PHONON_PULSESUPPORT
128     if (haspulse)
129         return;
130     pulse->enable(false);
131 #endif
132
133     for (int i = 0 ; i < list.size() ; ++i) {
134         QByteArray nameId = list.at(i);
135         QByteArray hwId = list_hw.at(i);
136         if (deviceId(nameId) == -1) {
137             // This is a new device, add it
138             qDebug() << "add aout " << nameId.data();
139             m_audioDeviceList.append(AudioDevice(this, nameId, hwId));
140             emit deviceAdded(deviceId(nameId));
141         }
142     }
143     if (list.size() < m_audioDeviceList.size()) {
144         // A device was removed
145         for (int i = m_audioDeviceList.size() - 1 ; i >= 0 ; --i) {
146             QByteArray currId = m_audioDeviceList[i].vlcId;
147             bool b_found = false;
148             for (int k = list.size() - 1  ; k >= 0 ; --k) {
149                 if (currId == list[k]) {
150                     b_found = true;
151                     break;
152                 }
153             }
154             if (!b_found) {
155                 emit deviceRemoved(deviceId(currId));
156                 m_audioDeviceList.removeAt(i);
157             }
158         }
159     }
160 }
161
162 /**
163  * Return a list of hardware id.
164  */
165 const QList<AudioDevice> DeviceManager::audioOutputDevices() const
166 {
167     return m_audioDeviceList;
168 }
169
170 }
171 }
172
173 QT_END_NAMESPACE