1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
/*
* pluginManager.cpp
*
* Created on: 05.09.2012
* Author: savop
*/
#include "../include/plugin.h"
#include "../include/tools.h"
#include <string>
using namespace std;
namespace upnp {
cMetadata::ValidatorMap cMetadata::validators;
class PropertyValidator {
private:
string key;
public:
PropertyValidator(string key) : key(key) {
cMetadata::RegisterPropertyValidator(this);
}
virtual ~PropertyValidator(){}
virtual string GetPropertyKey() const { return key; };
virtual bool Validate(const cMetadata::Property& property) = 0;
};
void cMetadata::RegisterPropertyValidator(PropertyValidator* validator){
string key = validator->GetPropertyKey();
validators[key] = validator;
}
bool cMetadata::SetObjectIDByUri(const string& uri){
return SetProperty(Property(property::object::KEY_OBJECTID, tools::GenerateUUIDFromURL(uri)));
}
bool cMetadata::SetParentIDByUri(const string& uri){
return SetProperty(Property(property::object::KEY_PARENTID, tools::GenerateUUIDFromURL(uri)));
}
bool cMetadata::AddProperty(const Property& property){
string key = property.GetKey();
// Try to find a validator
PropertyValidator* validator = validators[key];
// If there is one and it fails to validate the property, return false.
// Otherwise ignore it.
if(validator && !validator->Validate(property)) return false;
if(properties.find(key) != properties.end()){
if(key.compare(property::object::KEY_CHANNEL_NAME) == 0 ||
key.compare(property::object::KEY_CHANNEL_NR) == 0 ||
key.compare(property::object::KEY_CLASS) == 0 ||
key.compare(property::object::KEY_CREATOR) == 0 ||
key.compare(property::object::KEY_DATE) == 0 ||
key.compare(property::object::KEY_DESCRIPTION) == 0 ||
key.compare(property::object::KEY_LANGUAGE) == 0 ||
key.compare(property::object::KEY_LONG_DESCRIPTION) == 0 ||
key.compare(property::object::KEY_OBJECTID) == 0 ||
key.compare(property::object::KEY_PARENTID) == 0 ||
key.compare(property::object::KEY_RESTRICTED) == 0 ||
key.compare(property::object::KEY_SCHEDULED_END) == 0 ||
key.compare(property::object::KEY_SCHEDULED_START) == 0 ||
key.compare(property::object::KEY_TITLE) == 0)
{
esyslog("UPnP\tProperty '%s' already exist!", key.c_str());
return false;
}
}
properties.insert(pair<string, Property>(property.GetKey(), property));
return true;
}
bool cMetadata::SetProperty(const Property& property, int index){
// Try to find a validator
PropertyValidator* validator = validators[property.GetKey()];
// If there is one and it fails to validate the property, return false.
// Otherwise ignore it.
if(validator && !validator->Validate(property)) return false;
PropertyRange ret = properties.equal_range(property.GetKey());
// No property with the given name found. Let's add it to the map.
if(ret.first == ret.second) return AddProperty(property);
int i = 0;
for(PropertyMap::iterator it = ret.first; it != ret.second; ++it){
if(i == index){
(*it).second = property;
return true;
}
++i;
}
return false;
}
cMetadata::PropertyRange cMetadata::GetPropertiesByKey(const string& property) {
return properties.equal_range(property);
}
cMetadata::PropertyRange cMetadata::GetAllProperties() {
PropertyRange range(properties.begin(), properties.end());
return range;
}
cMetadata::Property& cMetadata::GetPropertyByKey(const string& property) {
return (*properties.find(property)).second;
}
cMetadata::Property::Property(string key, bool val)
: key(key)
{
value = val ? "true" : "false";
}
cMetadata::Property::Property(string key, string value)
: key(key)
, value(value)
{
}
cMetadata::Property::Property(string key, long val)
: key(key)
{
value = tools::ToString(val);
}
string cMetadata::Property::GetString() const {
return value;
}
bool cMetadata::Property::GetBoolean() const {
return value.compare("true") == 0 ? true : false;
}
long cMetadata::Property::GetInteger() const {
return atol(value.c_str());
}
string cMetadata::Property::GetKey() const {
return key;
}
bool cMetadata::Resource::SetResourceUri(string resourceUri){
this->resourceUri = resourceUri;
return true;
}
bool cMetadata::Resource::SetProtocolInfo(string protocolInfo){
this->protocolInfo = protocolInfo;
return true;
}
bool cMetadata::Resource::SetDuration(string duration){
this->duration = duration;
return true;
}
bool cMetadata::Resource::SetResolution(string resolution){
this->resolution = resolution;
return true;
}
bool cMetadata::Resource::SetBitrate(uint32_t bitrate){
this->bitrate = bitrate;
return true;
}
bool cMetadata::Resource::SetSize(uint32_t size){
this->size = size;
return true;
}
bool cMetadata::Resource::SetSampleFrequency(uint32_t sampleFrequency){
this->sampleFrequency = sampleFrequency;
return true;
}
bool cMetadata::Resource::SetBitsPerSample(uint32_t bitsPerSample){
this->bitsPerSample = bitsPerSample;
return true;
}
bool cMetadata::Resource::SetNrAudioChannels(uint32_t nrAudioChannels){
this->nrAudioChannels = nrAudioChannels;
return true;
}
bool cMetadata::Resource::SetColorDepth(uint32_t colorDepth){
this->colorDepth = colorDepth;
return true;
}
class ClassValidator : public PropertyValidator {
public:
ClassValidator() : PropertyValidator(property::object::KEY_CLASS) {}
virtual bool Validate(const cMetadata::Property& property){
string value = property.GetString();
if(value.find("object.container", 0) == 0 ||
value.find("object.item", 0) == 0)
{
return true;
}
else
{
return false;
}
}
} ClassValidatorInst;
cMetadata cUPnPResourceProvider::GetMetadata(string uri){
cMetadata metadata;
metadata.SetObjectIDByUri(uri);
metadata.SetParentIDByUri(uri.substr(0,uri.find_last_of("/")));
metadata.SetProperty(cMetadata::Property(property::object::KEY_TITLE, uri.substr(uri.find_last_of("/")+1)));
metadata.SetProperty(cMetadata::Property(property::object::KEY_CLASS, "object.container"));
metadata.SetProperty(cMetadata::Property(property::object::KEY_RESTRICTED, true));
return metadata;
}
string cUPnPResourceProvider::GetHTTPUri(string uri){
return string();
}
bool cUPnPResourceProvider::Seekable() const {
return false;
}
bool cUPnPResourceProvider::Open(string uri){
return false;
}
size_t cUPnPResourceProvider::Read(char* buf, size_t bufLen){
return -1;
}
bool cUPnPResourceProvider::Seek(size_t offset, int origin){
return false;
}
void cUPnPResourceProvider::Close(){
}
} // namespace uünü
|