Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

# Licensed under the MIT license 

# http://opensource.org/licenses/mit-license.php 

 

# Copyright 2006-2010 Frank Scholz <dev@coherence-project.org> 

 

import traceback 

 

from twisted.internet import reactor 

from twisted.web import xmlrpc, client 

 

import coherence.extern.louie as louie 

from coherence import log 

from coherence.upnp.core import service 

from coherence.upnp.core.event import EventServer 

from coherence.upnp.devices.internet_gateway_device_client import \ 

    InternetGatewayDeviceClient 

from coherence.upnp.devices.media_renderer_client import MediaRendererClient 

from coherence.upnp.devices.media_server_client import MediaServerClient 

 

 

class DeviceQuery(object): 

 

    def __init__(self, type, pattern, callback, timeout=0, oneshot=True): 

        self.type = type 

        self.pattern = pattern 

        self.callback = callback 

        self.fired = False 

        self.timeout = timeout 

        self.oneshot = oneshot 

        if self.type == 'uuid' and self.pattern.startswith('uuid:'): 

            self.pattern = self.pattern[5:] 

 

    def fire(self, device): 

        if callable(self.callback): 

            self.callback(device) 

        elif isinstance(self.callback, str): 

            louie.send(self.callback, None, device=device) 

        self.fired = True 

 

    def check(self, device): 

        if self.fired and self.oneshot: 

            return 

        if (self.type == 'host' and 

                device.host == self.pattern): 

            self.fire(device) 

        elif (self.type == 'friendly_name' and 

              device.friendly_name == self.pattern): 

            self.fire(device) 

        elif (self.type == 'uuid' and 

              device.get_uuid() == self.pattern): 

            self.fire(device) 

 

 

class ControlPoint(log.LogAble): 

    logCategory = 'controlpoint' 

 

    def __init__(self, coherence, auto_client=None): 

        log.LogAble.__init__(self) 

 

        if not auto_client: 

            auto_client = ['MediaServer', 'MediaRenderer'] 

        self.coherence = coherence 

        self.auto_client = auto_client 

        self.queries = [] 

 

        self.info("Coherence UPnP ControlPoint starting...") 

        self.event_server = EventServer(self) 

        self.coherence.add_web_resource('RPC2', XMLRPC(self)) 

 

        for device in self.get_devices(): 

            self.info('ControlPoint [check device]: {}'.format(device)) 

            self.check_device(device) 

 

        louie.connect(self.check_device, 

                      'Coherence.UPnP.Device.detection_completed', 

                      louie.Any) 

        louie.connect(self.remove_client, 

                      'Coherence.UPnP.Device.remove_client', 

                      louie.Any) 

        louie.connect(self.completed, 

                      'Coherence.UPnP.DeviceClient.detection_completed', 

                      louie.Any) 

 

    def shutdown(self): 

        louie.disconnect(self.check_device, 

                         'Coherence.UPnP.Device.detection_completed', 

                         louie.Any) 

        louie.disconnect(self.remove_client, 

                         'Coherence.UPnP.Device.remove_client', 

                         louie.Any) 

        louie.disconnect(self.completed, 

                         'Coherence.UPnP.DeviceClient.detection_completed', 

                         louie.Any) 

 

    def auto_client_append(self, device_type): 

        if device_type in self.auto_client: 

            return 

        self.auto_client.append(device_type) 

        for device in self.get_devices(): 

            self.check_device(device) 

 

    def browse(self, device): 

        self.info('ControlPoint.browse: {}'.format(device)) 

        device = self.coherence.get_device_with_usn(device.get_usn()) 

        if not device: 

            return 

        self.check_device(device) 

 

    def process_queries(self, device): 

        for query in self.queries: 

            query.check(device) 

 

    def add_query(self, query): 

        for device in self.get_devices(): 

            query.check(device) 

        if not query.fired and query.timeout == 0: 

            query.callback(None) 

        else: 

            self.queries.append(query) 

 

    def connect(self, receiver, signal=louie.signal.All, 

                sender=louie.sender.Any, weak=True): 

        """ wrapper method around louie.connect 

        """ 

        louie.connect(receiver, signal=signal, sender=sender, weak=weak) 

 

    def disconnect(self, receiver, signal=louie.signal.All, 

                   sender=louie.sender.Any, weak=True): 

        """ wrapper method around louie.disconnect 

        """ 

        louie.disconnect(receiver, signal=signal, sender=sender, weak=weak) 

 

    def get_devices(self): 

        return self.coherence.get_devices() 

 

    def get_device_with_id(self, id): 

        return self.coherence.get_device_with_id(id) 

 

    def get_device_by_host(self, host): 

        return self.coherence.get_device_by_host(host) 

 

    def check_device(self, device): 

        if device.client is None: 

            self.info("found device %s of type %s - %r", 

                      device.get_friendly_name(), 

                      device.get_device_type(), device.client) 

            short_type = device.get_friendly_device_type() 

            if short_type in self.auto_client and short_type is not None: 

                self.info("identified %s %r", short_type, 

                          device.get_friendly_name()) 

 

                if short_type == 'MediaServer': 

                    client = MediaServerClient(device) 

                if short_type == 'MediaRenderer': 

                    client = MediaRendererClient(device) 

                if short_type == 'InternetGatewayDevice': 

                    client = InternetGatewayDeviceClient(device) 

 

                client.coherence = self.coherence 

                device.set_client(client) 

 

        self.process_queries(device) 

 

    def completed(self, client, udn): 

        self.info('sending signal Coherence.UPnP.ControlPoint.%s.detected %r', 

                  client.device_type, udn) 

        louie.send( 

            'Coherence.UPnP.ControlPoint.%s.detected' % client.device_type, 

            None, 

            client=client, udn=udn) 

 

    def remove_client(self, udn, client): 

        louie.send( 

            'Coherence.UPnP.ControlPoint.%s.removed' % client.device_type, 

            None, udn=udn) 

        self.info("removed %s %s", client.device_type, 

                  client.device.get_friendly_name()) 

        client.remove() 

 

    def propagate(self, event): 

        self.info('propagate: %r', event) 

        if event.get_sid() in service.subscribers: 

            try: 

                service.subscribers[event.get_sid()].process_event(event) 

            except Exception as msg: 

                self.debug(msg) 

                self.debug(traceback.format_exc()) 

                pass 

 

    def put_resource(self, url, path): 

        def got_result(result): 

            print(result) 

 

        def got_error(result): 

            print("error", result) 

 

        try: 

            f = open(path) 

            data = f.read() 

            f.close() 

            headers = { 

                b"Content-Type": b"application/octet-stream", 

                b"Content-Length": bytes(str(len(data)), encoding='utf-8') 

            } 

            df = client.getPage( 

                url, method=b"POST", 

                headers=headers, postdata=data) 

            df.addCallback(got_result) 

            df.addErrback(got_error) 

            return df 

        except IOError: 

            pass 

 

 

class XMLRPC(xmlrpc.XMLRPC): 

 

    def __init__(self, control_point): 

        xmlrpc.XMLRPC.__init__(self) 

        self.control_point = control_point 

        self.allowNone = True 

 

    def xmlrpc_list_devices(self): 

        print("list_devices") 

        r = [] 

        for device in self.control_point.get_devices(): 

            # print(device.get_friendly_name(), device.get_service_type(), 

            #       device.get_location(), device.get_id()) 

            d = {'friendly_name': device.get_friendly_name(), 

                 'device_type': device.get_device_type(), 

                 'location': str(device.get_location()), 

                 'id': str(device.get_id())} 

            r.append(d) 

        return r 

 

    def xmlrpc_mute_device(self, device_id): 

        print("mute") 

        device = self.control_point.get_device_with_id(device_id) 

        if device is not None: 

            client = device.get_client() 

            client.rendering_control.set_mute(desired_mute=1) 

            return "Ok" 

        return "Error" 

 

    def xmlrpc_unmute_device(self, device_id): 

        print("unmute", device_id) 

        device = self.control_point.get_device_with_id(device_id) 

        if device is not None: 

            client = device.get_client() 

            client.rendering_control.set_mute(desired_mute=0) 

            return "Ok" 

        return "Error" 

 

    def xmlrpc_set_volume(self, device_id, volume): 

        print("set volume") 

        device = self.control_point.get_device_with_id(device_id) 

        if device is not None: 

            client = device.get_client() 

            client.rendering_control.set_volume(desired_volume=volume) 

            return "Ok" 

        return "Error" 

 

    def xmlrpc_play(self, device_id): 

        print("play") 

        device = self.control_point.get_device_with_id(device_id) 

        if device is not None: 

            client = device.get_client() 

            client.av_transport.play() 

            return "Ok" 

        return "Error" 

 

    def xmlrpc_pause(self, device_id): 

        print("pause") 

        device = self.control_point.get_device_with_id(device_id) 

        if device is not None: 

            client = device.get_client() 

            client.av_transport.pause() 

            return "Ok" 

        return "Error" 

 

    def xmlrpc_stop(self, device_id): 

        print("stop") 

        device = self.control_point.get_device_with_id(device_id) 

        if device is not None: 

            client = device.get_client() 

            client.av_transport.stop() 

            return "Ok" 

        return "Error" 

 

    def xmlrpc_next(self, device_id): 

        print("next") 

        device = self.control_point.get_device_with_id(device_id) 

        if device is not None: 

            client = device.get_client() 

            next(client.av_transport) 

            return "Ok" 

        return "Error" 

 

    def xmlrpc_previous(self, device_id): 

        print("previous") 

        device = self.control_point.get_device_with_id(device_id) 

        if device is not None: 

            client = device.get_client() 

            client.av_transport.previous() 

            return "Ok" 

        return "Error" 

 

    def xmlrpc_set_av_transport_uri(self, device_id, uri): 

        print("set_av_transport_uri") 

        device = self.control_point.get_device_with_id(device_id) 

        if device is not None: 

            client = device.get_client() 

            client.av_transport.set_av_transport_uri(current_uri=uri) 

            return "Ok" 

        return "Error" 

 

    def xmlrpc_create_object(self, device_id, container_id, arguments): 

        print("create_object", arguments) 

        device = self.control_point.get_device_with_id(device_id) 

        if device is not None: 

            client = device.get_client() 

            client.content_directory.create_object(container_id, arguments) 

            return "Ok" 

        return "Error" 

 

    def xmlrpc_import_resource(self, device_id, source_uri, destination_uri): 

        print("import_resource", source_uri, destination_uri) 

        device = self.control_point.get_device_with_id(device_id) 

        if device is not None: 

            client = device.get_client() 

            client.content_directory.import_resource(source_uri, 

                                                     destination_uri) 

            return "Ok" 

        return "Error" 

 

    def xmlrpc_put_resource(self, url, path): 

        print("put_resource", url, path) 

        self.control_point.put_resource(url, path) 

        return "Ok" 

 

    def xmlrpc_ping(self): 

        print("ping") 

        return "Ok" 

 

 

def startXMLRPC(control_point, port): 

    from twisted.web import server 

    r = XMLRPC(control_point) 

    print("XMLRPC-API on port %d ready" % port) 

    reactor.listenTCP(port, server.Site(r)) 

 

 

if __name__ == '__main__': 

 

    config = {} 

    config['logmode'] = 'warning' 

    config['serverport'] = 30020 

    from coherence.base import Coherence 

 

    ctrl = ControlPoint(Coherence(config), 

                        auto_client=[]) 

 

    def show_devices(): 

        print("show_devices") 

        for d in ctrl.get_devices(): 

            print(d, d.get_id()) 

 

    def the_result(r): 

        print("result", r, r.get_id()) 

 

    def query_devices(): 

        print("query_devices") 

        ctrl.add_query(DeviceQuery('host', '192.168.0.1', the_result)) 

 

    def query_devices2(): 

        print("query_devices with timeout") 

        ctrl.add_query( 

            DeviceQuery('host', '192.168.0.1', the_result, timeout=10, 

                        oneshot=False)) 

 

    def stop_reactor(*args): 

        reactor.stop() 

        print("Stoped reactor successfully") 

 

    reactor.callLater(2, show_devices) 

    reactor.callLater(3, query_devices) 

    reactor.callLater(4, query_devices2) 

    reactor.callLater(5, ctrl.add_query, DeviceQuery( 

        'friendly_name', 'Coherence Test Content', 

        the_result, timeout=10, oneshot=False)) 

    reactor.callLater(6, stop_reactor) 

 

    reactor.run()