comparison deps/Platinum/Source/Devices/MediaServer/PltMediaBrowser.cpp @ 0:3425707ddbf6

Initial import (hopefully this mercurial stuff works...)
author fraserofthenight
date Mon, 06 Jul 2009 08:06:28 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3425707ddbf6
1 /*****************************************************************
2 |
3 | Platinum - AV Media Browser (Media Server Control Point)
4 |
5 | Copyright (c) 2004-2008, Plutinosoft, LLC.
6 | All rights reserved.
7 | http://www.plutinosoft.com
8 |
9 | This program is free software; you can redistribute it and/or
10 | modify it under the terms of the GNU General Public License
11 | as published by the Free Software Foundation; either version 2
12 | of the License, or (at your option) any later version.
13 |
14 | OEMs, ISVs, VARs and other distributors that combine and
15 | distribute commercially licensed software with Platinum software
16 | and do not wish to distribute the source code for the commercially
17 | licensed software under version 2, or (at your option) any later
18 | version, of the GNU General Public License (the "GPL") must enter
19 | into a commercial license agreement with Plutinosoft, LLC.
20 |
21 | This program is distributed in the hope that it will be useful,
22 | but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | GNU General Public License for more details.
25 |
26 | You should have received a copy of the GNU General Public License
27 | along with this program; see the file LICENSE.txt. If not, write to
28 | the Free Software Foundation, Inc.,
29 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 | http://www.gnu.org/licenses/gpl-2.0.html
31 |
32 ****************************************************************/
33
34 /*----------------------------------------------------------------------
35 | includes
36 +---------------------------------------------------------------------*/
37 #include "Neptune.h"
38 #include "PltMediaBrowser.h"
39 #include "PltDidl.h"
40
41 NPT_SET_LOCAL_LOGGER("platinum.media.server.browser")
42
43 /*----------------------------------------------------------------------
44 | PLT_MediaBrowser::PLT_MediaBrowser
45 +---------------------------------------------------------------------*/
46 PLT_MediaBrowser::PLT_MediaBrowser(PLT_CtrlPointReference& ctrl_point,
47 PLT_MediaBrowserDelegate* delegate /* = NULL */) :
48 m_CtrlPoint(ctrl_point),
49 m_Delegate(delegate)
50 {
51 m_CtrlPoint->AddListener(this);
52 }
53
54 /*----------------------------------------------------------------------
55 | PLT_MediaBrowser::~PLT_MediaBrowser
56 +---------------------------------------------------------------------*/
57 PLT_MediaBrowser::~PLT_MediaBrowser()
58 {
59 m_CtrlPoint->RemoveListener(this);
60 }
61
62 /*----------------------------------------------------------------------
63 | PLT_MediaBrowser::OnDeviceAdded
64 +---------------------------------------------------------------------*/
65 NPT_Result
66 PLT_MediaBrowser::OnDeviceAdded(PLT_DeviceDataReference& device)
67 {
68 // verify the device implements the function we need
69 PLT_Service* serviceCDS;
70 PLT_Service* serviceCMR;
71 NPT_String type;
72
73 type = "urn:schemas-upnp-org:service:ContentDirectory:1";
74 if (NPT_FAILED(device->FindServiceByType(type, serviceCDS))) {
75 NPT_LOG_WARNING_2("Service %s not found in device \"%s\"",
76 type.GetChars(),
77 device->GetFriendlyName().GetChars());
78 return NPT_FAILURE;
79 }
80
81 type = "urn:schemas-upnp-org:service:ConnectionManager:1";
82 if (NPT_FAILED(device->FindServiceByType(type, serviceCMR))) {
83 NPT_LOG_WARNING_2("Service %s not found in device \"%s\"",
84 type.GetChars(),
85 device->GetFriendlyName().GetChars());
86 return NPT_FAILURE;
87 }
88
89 {
90 NPT_AutoLock lock(m_MediaServers);
91
92 PLT_DeviceDataReference data;
93 NPT_String uuid = device->GetUUID();
94 // is it a new device?
95 if (NPT_SUCCEEDED(NPT_ContainerFind(m_MediaServers, PLT_DeviceDataFinder(uuid), data))) {
96 NPT_LOG_WARNING_1("Device (%s) is already in our list!", (const char*)uuid);
97 return NPT_FAILURE;
98 }
99
100 NPT_LOG_FINE("Device Found:");
101 device->ToLog(NPT_LOG_LEVEL_FINE);
102
103 m_MediaServers.Add(device);
104 }
105
106 if (m_Delegate && m_Delegate->OnMSAdded(device)) {
107 m_CtrlPoint->Subscribe(serviceCDS);
108 m_CtrlPoint->Subscribe(serviceCMR);
109 }
110
111 return NPT_SUCCESS;
112 }
113
114 /*----------------------------------------------------------------------
115 | PLT_MediaBrowser::OnDeviceRemoved
116 +---------------------------------------------------------------------*/
117 NPT_Result
118 PLT_MediaBrowser::OnDeviceRemoved(PLT_DeviceDataReference& device)
119 {
120 PLT_DeviceDataReference data;
121
122 {
123 NPT_AutoLock lock(m_MediaServers);
124
125 // only release if we have kept it around
126 NPT_String uuid = device->GetUUID();
127 // is it a new device?
128 if (NPT_FAILED(NPT_ContainerFind(m_MediaServers, PLT_DeviceDataFinder(uuid), data))) {
129 NPT_LOG_WARNING_1("Device (%s) not found in our list!", (const char*)uuid);
130 return NPT_FAILURE;
131 }
132
133 NPT_LOG_FINE("Device Removed:");
134 device->ToLog(NPT_LOG_LEVEL_FINE);
135
136 m_MediaServers.Remove(device);
137 }
138
139 if (m_Delegate) {
140 m_Delegate->OnMSRemoved(device);
141 }
142
143 return NPT_SUCCESS;
144 }
145
146 /*----------------------------------------------------------------------
147 | PLT_MediaBrowser::Browse
148 +---------------------------------------------------------------------*/
149 NPT_Result
150 PLT_MediaBrowser::Browse(PLT_DeviceDataReference& device,
151 const char* obj_id,
152 NPT_UInt32 start_index,
153 NPT_UInt32 count,
154 bool browse_metadata,
155 const char* filter,
156 const char* sort_criteria,
157 void* userdata)
158 {
159 // look for the service
160 PLT_Service* service;
161 NPT_String type;
162
163 type = "urn:schemas-upnp-org:service:ContentDirectory:1";
164 if (NPT_FAILED(device->FindServiceByType(type, service))) {
165 NPT_LOG_WARNING_1("Service %s not found", (const char*)type);
166 return NPT_FAILURE;
167 }
168
169 PLT_ActionDesc* action_desc = service->FindActionDesc("Browse");
170 if (action_desc == NULL) {
171 NPT_LOG_WARNING("Action Browse not found in service");
172 return NPT_FAILURE;
173 }
174
175 PLT_ActionReference action(new PLT_Action(action_desc));
176
177 // Set the object id
178 PLT_Arguments args;
179 if (NPT_FAILED(action->SetArgumentValue("ObjectID", obj_id))) {
180 return NPT_ERROR_INVALID_PARAMETERS;
181 }
182
183 // set the browse_flag
184 if (NPT_FAILED(action->SetArgumentValue("BrowseFlag", browse_metadata?"BrowseMetadata":"BrowseDirectChildren"))) {
185 return NPT_ERROR_INVALID_PARAMETERS;
186 }
187
188 // set the Filter
189 if (NPT_FAILED(action->SetArgumentValue("Filter", filter))) {
190 return NPT_ERROR_INVALID_PARAMETERS;
191 }
192
193 // set the Starting Index
194 if (NPT_FAILED(action->SetArgumentValue("StartingIndex", NPT_String::FromInteger(start_index)))) {
195 return NPT_ERROR_INVALID_PARAMETERS;
196 }
197
198 // set the Requested Count
199 if (NPT_FAILED(action->SetArgumentValue("RequestedCount", NPT_String::FromInteger(count)))) {
200 return NPT_ERROR_INVALID_PARAMETERS;
201 }
202
203 // set the Requested Count
204 if (NPT_FAILED(action->SetArgumentValue("SortCriteria", sort_criteria))) {
205 return NPT_ERROR_INVALID_PARAMETERS;
206 }
207
208 // invoke the action
209 if (NPT_FAILED(m_CtrlPoint->InvokeAction(action, userdata))) {
210 return NPT_ERROR_INVALID_PARAMETERS;
211 }
212
213 return NPT_SUCCESS;
214 }
215
216 /*----------------------------------------------------------------------
217 | PLT_MediaBrowser::OnActionResponse
218 +---------------------------------------------------------------------*/
219 NPT_Result
220 PLT_MediaBrowser::OnActionResponse(NPT_Result res, PLT_ActionReference& action, void* userdata)
221 {
222 if (m_Delegate == NULL) return NPT_SUCCESS;
223
224 PLT_DeviceDataReference device;
225 {
226 NPT_AutoLock lock(m_MediaServers);
227 NPT_String uuid = action->GetActionDesc()->GetService()->GetDevice()->GetUUID();
228 if (NPT_FAILED(NPT_ContainerFind(m_MediaServers, PLT_DeviceDataFinder(uuid), device))) {
229 NPT_LOG_WARNING_1("Device (%s) not found in our list of servers", (const char*)uuid);
230 return NPT_FAILURE;
231 }
232 }
233
234 NPT_String actionName = action->GetActionDesc()->GetName();
235
236 // Browse action response
237 if (actionName.Compare("Browse", true) == 0) {
238 return OnBrowseResponse(res, device, action, userdata);
239 }
240
241 return NPT_SUCCESS;
242 }
243
244 /*----------------------------------------------------------------------
245 | PLT_MediaBrowser::OnBrowseResponse
246 +---------------------------------------------------------------------*/
247 NPT_Result
248 PLT_MediaBrowser::OnBrowseResponse(NPT_Result res, PLT_DeviceDataReference& device, PLT_ActionReference& action, void* userdata)
249 {
250 NPT_String value;
251 PLT_BrowseInfo info;
252 NPT_String unescaped;
253
254 if (NPT_FAILED(res) || action->GetErrorCode() != 0) {
255 goto bad_action;
256 }
257
258 if (NPT_FAILED(action->GetArgumentValue("ObjectID", info.object_id))) {
259 goto bad_action;
260 }
261 if (NPT_FAILED(action->GetArgumentValue("UpdateID", value)) ||
262 value.GetLength() == 0 ||
263 NPT_FAILED(value.ToInteger(info.uid))) {
264 goto bad_action;
265 }
266 if (NPT_FAILED(action->GetArgumentValue("NumberReturned", value)) ||
267 value.GetLength() == 0 ||
268 NPT_FAILED(value.ToInteger(info.nr))) {
269 goto bad_action;
270 }
271 if (NPT_FAILED(action->GetArgumentValue("TotalMatches", value)) ||
272 value.GetLength() == 0 ||
273 NPT_FAILED(value.ToInteger(info.tm))) {
274 goto bad_action;
275 }
276 if (NPT_FAILED(action->GetArgumentValue("Result", value)) ||
277 value.GetLength() == 0) {
278 goto bad_action;
279 }
280
281 if (NPT_FAILED(PLT_Didl::FromDidl(value, info.items))) {
282 goto bad_action;
283 }
284
285 m_Delegate->OnBrowseResult(NPT_SUCCESS, device, &info, userdata);
286 return NPT_SUCCESS;
287
288 bad_action:
289 m_Delegate->OnBrowseResult(NPT_FAILURE, device, NULL, userdata);
290 return NPT_FAILURE;
291 }
292
293 /*----------------------------------------------------------------------
294 | PLT_MediaBrowser::OnEventNotify
295 +---------------------------------------------------------------------*/
296 NPT_Result
297 PLT_MediaBrowser::OnEventNotify(PLT_Service* service, NPT_List<PLT_StateVariable*>* vars)
298 {
299 if (!m_Delegate) return NPT_SUCCESS;
300
301 PLT_DeviceDataReference data;
302
303 {
304 NPT_AutoLock lock(m_MediaServers);
305 NPT_String uuid = service->GetDevice()->GetUUID();
306 if (NPT_FAILED(NPT_ContainerFind(m_MediaServers, PLT_DeviceDataFinder(uuid), data))) {
307 NPT_LOG_WARNING_1("Device (%s) not found in our list!", (const char*)uuid);
308 return NPT_FAILURE;
309 }
310 }
311
312 if (m_Delegate) m_Delegate->OnMSStateVariablesChanged(service, vars);
313 return NPT_SUCCESS;
314 }