comparison deps/Platinum/Source/Devices/MediaConnect/PltMediaConnect.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 Connect Device
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 "Platinum.h"
39 #include "PltMediaConnect.h"
40
41 NPT_SET_LOCAL_LOGGER("platinum.devices.mediaconnect")
42
43 /*----------------------------------------------------------------------
44 | forward references
45 +---------------------------------------------------------------------*/
46 extern NPT_UInt8 X_MS_MediaReceiverRegistrarSCPD[];
47
48 /*----------------------------------------------------------------------
49 | PLT_MediaConnect::PLT_MediaConnect
50 +---------------------------------------------------------------------*/
51 PLT_MediaConnect::PLT_MediaConnect(const char* path,
52 const char* friendly_name,
53 bool show_ip /* = false */,
54 const char* uuid /* = NULL */,
55 NPT_UInt16 port /* = 0 */,
56 bool port_rebind /* = false */) :
57 PLT_FileMediaServer(path, friendly_name, show_ip, uuid, port, port_rebind),
58 m_RegistrarService(NULL)
59 {
60 m_ModelName = "Windows Media Player Sharing"; // for Xbox3630 & Sonos to accept us
61 m_ModelNumber = "3.0"; // must be >= 3.0 for Sonos to accept us
62 m_ModelDescription = "Media Server";
63 m_Manufacturer = "Plutinosoft";
64 m_ManufacturerURL = "http://www.plutinosoft.com/";
65 m_ModelURL = "http://www.plutinosoft.com";
66 m_DlnaDoc = "DMS-1.00";
67 }
68
69 /*----------------------------------------------------------------------
70 | PLT_MediaConnect::~PLT_MediaConnect
71 +---------------------------------------------------------------------*/
72 PLT_MediaConnect::~PLT_MediaConnect()
73 {
74 }
75
76 /*----------------------------------------------------------------------
77 | PLT_MediaConnect::SetupServices
78 +---------------------------------------------------------------------*/
79 NPT_Result
80 PLT_MediaConnect::SetupServices(PLT_DeviceData& data)
81 {
82 m_RegistrarService = new PLT_Service(
83 &data,
84 "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1",
85 "urn:microsoft.com:serviceId:X_MS_MediaReceiverRegistrar");
86
87 NPT_CHECK_FATAL(m_RegistrarService->SetSCPDXML((const char*) X_MS_MediaReceiverRegistrarSCPD));
88 NPT_CHECK_FATAL(m_RegistrarService->InitURLs("X_MS_MediaReceiverRegistrar", data.GetUUID()));
89 NPT_CHECK_FATAL(data.AddService(m_RegistrarService));
90
91 m_RegistrarService->SetStateVariable("AuthorizationGrantedUpdateID", "0");
92 m_RegistrarService->SetStateVariable("AuthorizationDeniedUpdateID", "0");
93 m_RegistrarService->SetStateVariable("ValidationSucceededUpdateID", "0");
94 m_RegistrarService->SetStateVariable("ValidationRevokedUpdateID", "0");
95
96 return PLT_FileMediaServer::SetupServices(data);
97 }
98
99 /*----------------------------------------------------------------------
100 | PLT_MediaConnect::ProcessGetDescription
101 +---------------------------------------------------------------------*/
102 NPT_Result
103 PLT_MediaConnect::ProcessGetDescription(NPT_HttpRequest& request,
104 const NPT_HttpRequestContext& context,
105 NPT_HttpResponse& response)
106 {
107 NPT_String m_OldModelName = m_ModelName;
108 NPT_String m_OldModelNumber = m_ModelNumber;
109 NPT_String m_OldDlnaDoc = m_DlnaDoc;
110 NPT_String m_OldDlnaCap = m_DlnaCap;
111
112 // change some things based on User-Agent header
113 NPT_HttpHeader* user_agent = request.GetHeaders().GetHeader(NPT_HTTP_HEADER_USER_AGENT);
114 if (user_agent && user_agent->GetValue().Find("Xbox", 0, true)>=0) {
115 // For the XBox 360 to discover us, ModelName must stay "Windows Media Player Sharing"
116 m_ModelName = "Windows Media Player Sharing";
117 m_ModelNumber = "3.0";
118 }
119
120 // PS3
121 NPT_HttpHeader* client_info = request.GetHeaders().GetHeader("X-AV-Client-Info");
122 if (client_info && client_info->GetValue().Find("PLAYSTATION 3", 0, true)>=0) {
123 m_DlnaDoc = "DMS-1.50";
124 m_DlnaCap = "av-upload,image-upload,audio-upload";
125 }
126
127 NPT_Result res = PLT_FileMediaServer::ProcessGetDescription(request, context, response);
128
129 // reset to old values now
130 m_ModelName = m_OldModelName;
131 m_ModelNumber = m_OldModelNumber;
132 m_DlnaDoc = m_OldDlnaDoc;
133 m_DlnaCap = m_OldDlnaCap;
134
135 return res;
136 }
137
138
139 /*----------------------------------------------------------------------
140 | PLT_MediaConnect::Authorize
141 +---------------------------------------------------------------------*/
142 NPT_Result
143 PLT_MediaConnect::Authorize(PLT_MediaConnectInfo* info, bool state)
144 {
145 info->m_Authorized = state;
146 if (state == true) {
147 return m_RegistrarService->IncStateVariable("AuthorizationGrantedUpdateID");
148 }
149
150 return m_RegistrarService->IncStateVariable("AuthorizationDeniedUpdateID");
151 }
152
153 /*----------------------------------------------------------------------
154 | PLT_MediaConnect::Validate
155 +---------------------------------------------------------------------*/
156 NPT_Result
157 PLT_MediaConnect::Validate(PLT_MediaConnectInfo* info, bool state)
158 {
159 info->m_Validated = state;
160 if (state == true) {
161 return m_RegistrarService->IncStateVariable("ValidationSucceededUpdateID");
162 }
163
164 return m_RegistrarService->IncStateVariable("ValidationRevokedUpdateID");
165 }
166
167 /*----------------------------------------------------------------------
168 | PLT_MediaConnect::OnAction
169 +---------------------------------------------------------------------*/
170 NPT_Result
171 PLT_MediaConnect::OnAction(PLT_ActionReference& action,
172 const PLT_HttpRequestContext& context)
173 {
174 PLT_MediaConnectInfo* mc_info = NULL;
175
176 /* parse the action name */
177 NPT_String name = action->GetActionDesc()->GetName();
178
179 /* handle X_MS_MediaReceiverRegistrar actions here */
180 if (name.Compare("IsAuthorized") == 0) {
181 return OnIsAuthorized(action, mc_info);
182 }
183 if (name.Compare("RegisterDevice") == 0) {
184 return OnRegisterDevice(action, mc_info);
185 }
186 if (name.Compare("IsValidated") == 0) {
187 return OnIsValidated(action, mc_info);
188 }
189
190 return PLT_FileMediaServer::OnAction(action, context);
191 }
192
193 /*----------------------------------------------------------------------
194 | PLT_MediaConnect::LookUpMediaConnectInfo
195 +---------------------------------------------------------------------*/
196 NPT_Result
197 PLT_MediaConnect::LookUpMediaConnectInfo(NPT_String deviceID,
198 PLT_MediaConnectInfo*& mc_info)
199 {
200 mc_info = NULL;
201
202 if (deviceID.GetLength()) {
203 /* lookup the MAC from the UDN */
204 NPT_String* MAC;
205 if (NPT_SUCCEEDED(m_MediaConnectUDNMap.Get(deviceID, MAC))) {
206 /* lookup the PLT_MediaConnectInfo from the MAC now */
207 return m_MediaConnectDeviceInfoMap.Get(*MAC, mc_info);
208 }
209 }
210
211 return NPT_FAILURE;
212 }
213
214 /*----------------------------------------------------------------------
215 | PLT_MediaConnect::OnIsAuthorized
216 +---------------------------------------------------------------------*/
217 NPT_Result
218 PLT_MediaConnect::OnIsAuthorized(PLT_ActionReference& action,
219 PLT_MediaConnectInfo* mc_info)
220 {
221 bool authorized = true;
222
223 NPT_String deviceID;
224 action->GetArgumentValue("DeviceID", deviceID);
225
226 /* is there a device ID passed ? */
227 if (deviceID.GetLength()) {
228 /* lookup the MediaConnectInfo from the UDN */
229 NPT_String MAC;
230 PLT_MediaConnectInfo* device_info;
231 if (NPT_FAILED(LookUpMediaConnectInfo(deviceID, device_info))) {
232 authorized = false;
233 } else {
234 authorized = device_info->m_Authorized;
235 }
236 } else {
237 authorized = mc_info?mc_info->m_Authorized:true;
238 }
239
240 action->SetArgumentValue("Result", authorized?"1":"0");
241 return NPT_SUCCESS;
242 }
243
244 /*----------------------------------------------------------------------
245 | PLT_MediaConnect::OnRegisterDevice
246 +---------------------------------------------------------------------*/
247 NPT_Result
248 PLT_MediaConnect::OnRegisterDevice(PLT_ActionReference& action,
249 PLT_MediaConnectInfo* mc_info)
250 {
251 NPT_COMPILER_UNUSED(mc_info);
252
253 NPT_String reqMsgBase64;
254 action->GetArgumentValue("RegistrationReqMsg", reqMsgBase64);
255
256 NPT_String respMsgBase64;
257 action->SetArgumentValue("RegistrationRespMsg", respMsgBase64);
258 return NPT_SUCCESS;
259 }
260
261 /*----------------------------------------------------------------------
262 | PLT_MediaConnect::OnIsValidated
263 +---------------------------------------------------------------------*/
264 NPT_Result
265 PLT_MediaConnect::OnIsValidated(PLT_ActionReference& action,
266 PLT_MediaConnectInfo* mc_info)
267 {
268 bool validated = true;
269
270 NPT_String deviceID;
271 action->GetArgumentValue("DeviceID", deviceID);
272
273 /* is there a device ID passed ? */
274 if (deviceID.GetLength()) {
275 /* lookup the MediaConnectInfo from the UDN */
276 NPT_String MAC;
277 PLT_MediaConnectInfo* device_info;
278 if (NPT_FAILED(LookUpMediaConnectInfo(deviceID, device_info))) {
279 validated = false;
280 } else {
281 validated = device_info->m_Validated;
282 }
283 } else {
284 validated = mc_info?mc_info->m_Validated:true;
285 }
286
287 action->SetArgumentValue("Result", validated?"1":"0");
288 return NPT_SUCCESS;
289 }
290
291 /*----------------------------------------------------------------------
292 | PLT_MediaConnect::GetFilePath
293 +---------------------------------------------------------------------*/
294 NPT_Result
295 PLT_MediaConnect::GetFilePath(const char* object_id,
296 NPT_String& filepath)
297 {
298 if (!object_id) return NPT_ERROR_INVALID_PARAMETERS;
299
300 // Reroute XBox360 and WMP requests to our route
301 if (NPT_StringsEqual(object_id, "15")) {
302 return PLT_FileMediaServer::GetFilePath("", filepath); // Videos
303 } else if (NPT_StringsEqual(object_id, "16")) {
304 return PLT_FileMediaServer::GetFilePath("", filepath); // Photos
305 } else if (NPT_StringsEqual(object_id, "13")) {
306 return PLT_FileMediaServer::GetFilePath("", filepath); // Music
307 }
308
309 return PLT_FileMediaServer::GetFilePath(object_id, filepath);;
310 }