Switch to unified view

a b/upmpd/ohvolume.cxx
1
/* Copyright (C) 2014 J.F.Dockes
2
 *     This program is free software; you can redistribute it and/or modify
3
 *     it under the terms of the GNU General Public License as published by
4
 *     the Free Software Foundation; either version 2 of the License, or
5
 *     (at your option) any later version.
6
 *
7
 *     This program is distributed in the hope that it will be useful,
8
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 *     GNU General Public License for more details.
11
 *
12
 *     You should have received a copy of the GNU General Public License
13
 *     along with this program; if not, write to the
14
 *     Free Software Foundation, Inc.,
15
 *     59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
17
18
#include <stdio.h>
19
#include <stdlib.h>
20
#include <unistd.h>
21
#include <sys/types.h>
22
#include <pwd.h>
23
24
#include <string>
25
#include <iostream>
26
#include <sstream>
27
#include <vector>
28
#include <functional>
29
#include <set>
30
using namespace std;
31
using namespace std::placeholders;
32
33
#include "libupnpp/upnpplib.hxx"
34
#include "libupnpp/soaphelp.hxx"
35
#include "libupnpp/device.hxx"
36
#include "libupnpp/log.hxx"
37
38
#include "upmpd.hxx"
39
#include "ohvolume.hxx"
40
#include "mpdcli.hxx"
41
#include "upmpdutils.hxx"
42
#include "rendering.hxx"
43
44
static const string sTpProduct("urn:av-openhome-org:service:Volume:1");
45
static const string sIdProduct("urn:av-openhome-org:serviceId:Volume");
46
47
OHVolume::OHVolume(UpMpd *dev, UpMpdRenderCtl *ctl)
48
    : UpnpService(sTpProduct, sIdProduct, dev), m_dev(dev), m_ctl(ctl)
49
{
50
    dev->addActionMapping("Characteristics", 
51
                          bind(&OHVolume::characteristics, this, _1, _2));
52
    dev->addActionMapping("SetVolume", 
53
                          bind(&OHVolume::setVolume, this, _1, _2));
54
    dev->addActionMapping("Volume", 
55
                          bind(&OHVolume::volume, this, _1, _2));
56
    dev->addActionMapping("VolumeInc", 
57
                          bind(&OHVolume::volumeInc, this, _1, _2));
58
    dev->addActionMapping("VolumeDec", 
59
                          bind(&OHVolume::volumeDec, this, _1, _2));
60
    dev->addActionMapping("VolumeLimit", 
61
                          bind(&OHVolume::volumeLimit, this, _1, _2));
62
    dev->addActionMapping("Mute", 
63
                          bind(&OHVolume::mute, this, _1, _2));
64
    dev->addActionMapping("SetMute", 
65
                          bind(&OHVolume::setMute, this, _1, _2));
66
}
67
68
void OHVolume::getdata(string& trackcount, string &duration, 
69
                     string& seconds)
70
{
71
    const MpdStatus &mpds =  m_dev->getMpdStatus();
72
73
    char cbuf[30];
74
    sprintf(cbuf, "%d", mpds.trackcounter);
75
    trackcount = cbuf;
76
    bool is_song = (mpds.state == MpdStatus::MPDS_PLAY) || 
77
        (mpds.state == MpdStatus::MPDS_PAUSE);
78
    if (is_song) {
79
        sprintf(cbuf, "%u", mpds.songlenms / 1000);
80
        duration = cbuf;
81
        sprintf(cbuf, "%u", mpds.songelapsedms / 1000);
82
        seconds = cbuf;
83
    }
84
}
85
86
bool OHVolume::makestate(unordered_map<string, string> &st)
87
{
88
    st.clear();
89
90
    st["VolumeMax"] = "100";
91
    st["VolumeLimit"] = 100;
92
    st["VolumeUnity"] = "100";
93
    st["VolumeSteps"] = "100";
94
    st["VolumeMilliDbPerSteps"] = "500";
95
    st["BalanceMax"] = "0";
96
    st["FadeMax"] = "0";
97
    char cbuf[30];
98
    int volume = m_ctl->getvolume_i();
99
    sprintf(cbuf, "%d", volume);
100
    st["Volume"] = cbuf;
101
    st["Mute"] = volume == 0 ? "1" : "0";
102
    return true;
103
}
104
105
bool OHVolume::getEventData(bool all, std::vector<std::string>& names, 
106
                            std::vector<std::string>& values)
107
{
108
    LOGDEB("OHVolume::getEventData" << endl);
109
110
    unordered_map<string, string> state;
111
    makestate(state);
112
113
    unordered_map<string, string> changed;
114
    if (all) {
115
        changed = state;
116
    } else {
117
        changed = diffmaps(m_state, state);
118
    }
119
    m_state = state;
120
121
    for (unordered_map<string, string>::iterator it = changed.begin();
122
         it != changed.end(); it++) {
123
        names.push_back(it->first);
124
        values.push_back(it->second);
125
    }
126
127
    return true;
128
}
129
130
int OHVolume::characteristics(const SoapArgs& sc, SoapData& data)
131
{
132
    LOGDEB("OHVolume::charact." << endl);
133
    data.addarg("VolumeMax", "100");
134
    data.addarg("VolumeUnity", "100");
135
    data.addarg("VolumeSteps", "100");
136
    // No idea...
137
    data.addarg("VolumeMilliDbPerStep", "500");
138
    data.addarg("BalanceMax", "0");
139
    data.addarg("FadeMax", "0");
140
    return UPNP_E_SUCCESS;
141
}
142
143
int OHVolume::setVolume(const SoapArgs& sc, SoapData& data)
144
{
145
    LOGDEB("OHVolume::setVolume" << endl);
146
    map<string, string>::const_iterator it;
147
148
    it = sc.args.find("Value");
149
    if (it == sc.args.end() || it->second.empty()) {
150
        return UPNP_E_INVALID_PARAM;
151
    }
152
    int volume = atoi(it->second.c_str());
153
    m_ctl->setvolume_i(volume);
154
155
    m_dev->loopWakeup();
156
    return UPNP_E_SUCCESS;
157
}
158
159
int OHVolume::setMute(const SoapArgs& sc, SoapData& data)
160
{
161
    LOGDEB("OHVolume::setMute" << endl);
162
    map<string, string>::const_iterator it;
163
164
    it = sc.args.find("Value");
165
    if (it == sc.args.end() || it->second.empty()) {
166
        return UPNP_E_INVALID_PARAM;
167
    }
168
    bool mute;
169
    if (it->second[0] == 'F' || it->second[0] == '0') {
170
        mute = false;
171
    } else if (it->second[0] == 'T' || it->second[0] == '1') {
172
        mute = true;
173
    } else {
174
        return UPNP_E_INVALID_PARAM;
175
    }
176
    m_ctl->setmute_i(mute);
177
    return UPNP_E_SUCCESS;
178
}
179
180
int OHVolume::volumeInc(const SoapArgs& sc, SoapData& data)
181
{
182
    LOGDEB("OHVolume::volumeInc" << endl);
183
    int newvol = m_ctl->getvolume_i()+1;
184
    if (newvol > 100)
185
        newvol = 100;
186
    m_ctl->setvolume_i(newvol);
187
    m_dev->loopWakeup();
188
    return UPNP_E_SUCCESS;
189
}
190
191
int OHVolume::volumeDec(const SoapArgs& sc, SoapData& data)
192
{
193
    LOGDEB("OHVolume::volumeDec" << endl);
194
    int newvol = m_ctl->getvolume_i() - 1;
195
    if (newvol < 0)
196
        newvol = 0;
197
    m_ctl->setvolume_i(newvol);
198
    m_dev->loopWakeup();
199
    return UPNP_E_SUCCESS;
200
}
201
202
int OHVolume::volume(const SoapArgs& sc, SoapData& data)
203
{
204
    LOGDEB("OHVolume::volume" << endl);
205
    char cbuf[30];
206
    int volume = m_ctl->getvolume_i();
207
    sprintf(cbuf, "%d", volume);
208
    data.addarg("Value", cbuf);
209
    return UPNP_E_SUCCESS;
210
}
211
212
int OHVolume::mute(const SoapArgs& sc, SoapData& data)
213
{
214
    LOGDEB("OHVolume::mute" << endl);
215
    bool mute = m_ctl->getvolume_i() == 0;
216
    data.addarg("Value", mute ? "1" : "0");
217
    return UPNP_E_SUCCESS;
218
}
219
220
int OHVolume::volumeLimit(const SoapArgs& sc, SoapData& data)
221
{
222
    LOGDEB("OHVolume::volumeLimit" << endl);
223
    data.addarg("Value", "100");
224
    return UPNP_E_SUCCESS;
225
}
226