]> git.sesse.net Git - vlc/blob - bindings/phonon/vlc/devicemanager.cpp
Phonon Backend using VLC
[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 /**
25  * This class manages the list of currently active output devices.
26  */
27
28 QT_BEGIN_NAMESPACE
29
30 namespace Phonon
31 {
32 namespace VLC {
33
34 AudioDevice::AudioDevice(DeviceManager *manager, const QByteArray &deviceId, const QByteArray &hw_id)
35 {
36     // Get an id
37     static int counter = 0;
38     id = counter++;
39     // Get name from device
40     if (vlcId == "default") {
41         description = "Default audio device";
42     } else {
43         vlcId = deviceId;
44         description = "";
45     }
46     hwId = hw_id;
47 }
48
49 DeviceManager::DeviceManager(Backend *parent)
50         : QObject(parent)
51         , m_backend(parent)
52 {
53     updateDeviceList();
54 }
55
56 DeviceManager::~DeviceManager()
57 {
58     m_audioDeviceList.clear();
59 }
60
61 bool DeviceManager::canOpenDevice() const
62 {
63     return true;
64 }
65
66 /**
67  * Return a positive device id or -1 if device does not exist.
68  */
69 int DeviceManager::deviceId(const QByteArray &nameId) const
70 {
71     for (int i = 0 ; i < m_audioDeviceList.size() ; ++i) {
72         if (m_audioDeviceList[i].vlcId == nameId)
73             return m_audioDeviceList[i].id;
74     }
75     return -1;
76 }
77
78 /**
79  * Get a human-readable description from a device id.
80  */
81 QByteArray DeviceManager::deviceDescription(int i_id) const
82 {
83     for (int i = 0 ; i < m_audioDeviceList.size() ; ++i) {
84         if (m_audioDeviceList[i].id == i_id)
85             return m_audioDeviceList[i].description;
86     }
87     return QByteArray();
88 }
89
90 /**
91  * Update the current list of active devices.
92  */
93 void DeviceManager::updateDeviceList()
94 {
95     QList<QByteArray> list, list_hw;
96     list.append("default");
97     list_hw.append("");
98
99     // Get the list of available audio outputs
100     libvlc_audio_output_t *p_ao_list = libvlc_audio_output_list_get(
101                                            vlc_instance, vlc_exception);
102     vlcExceptionRaised();
103     libvlc_audio_output_t *p_start = p_ao_list;
104
105     while (p_ao_list) {
106         list.append(p_ao_list->psz_name);
107         list_hw.append("");
108         p_ao_list = p_ao_list->p_next;
109     }
110     libvlc_audio_output_list_release(p_start);
111
112     for (int i = 0 ; i < list.size() ; ++i) {
113         QByteArray nameId = list.at(i);
114         QByteArray hwId = list_hw.at(i);
115         if (deviceId(nameId) == -1) {
116             // This is a new device, add it
117             qDebug() << "add aout " << nameId.data();
118             m_audioDeviceList.append(AudioDevice(this, nameId, hwId));
119             emit deviceAdded(deviceId(nameId));
120         }
121     }
122     if (list.size() < m_audioDeviceList.size()) {
123         // A device was removed
124         for (int i = m_audioDeviceList.size() - 1 ; i >= 0 ; --i) {
125             QByteArray currId = m_audioDeviceList[i].vlcId;
126             bool b_found = false;
127             for (int k = list.size() - 1  ; k >= 0 ; --k) {
128                 if (currId == list[k]) {
129                     b_found = true;
130                     break;
131                 }
132             }
133             if (!b_found) {
134                 emit deviceRemoved(deviceId(currId));
135                 m_audioDeviceList.removeAt(i);
136             }
137         }
138     }
139 }
140
141 /**
142  * Return a list of hardware id.
143  */
144 const QList<AudioDevice> DeviceManager::audioOutputDevices() const
145 {
146     return m_audioDeviceList;
147 }
148
149 }
150 }
151
152 QT_END_NAMESPACE