-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmini-dlna-server.py
1318 lines (1134 loc) · 56 KB
/
mini-dlna-server.py
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import errno
import os
import sys
import socket
import logging
import time
import uuid
import threading
from pathlib import Path
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import unquote, quote
from logging.handlers import RotatingFileHandler
from xml.etree.ElementTree import Element, SubElement, tostring, fromstring
from datetime import datetime
from mutagen import File
# DLNA/UPnP Constants
DEVICE_UUID = uuid.uuid5(uuid.NAMESPACE_DNS, socket.gethostname())
DEVICE_NAME = f"Python Media Server ({socket.gethostname()})"
SSDP_ADDR = '239.255.255.250'
SSDP_PORT = 1900
# Supported extensions
VIDEO_EXTENSIONS = {'.mp4': 'video/mp4', '.mkv': 'video/x-matroska', '.avi': 'video/x-msvideo'}
AUDIO_EXTENSIONS = {'.mp3': 'audio/mpeg', '.flac': 'audio/flac', '.wav': 'audio/wav'}
IMAGE_EXTENSIONS = {'.jpg': 'image/jpeg', '.png': 'image/png', '.gif': 'image/gif'}
def setup_logging():
"""Set up logging configuration with both file and console handlers"""
logger = logging.getLogger('DLNAServer')
logger.setLevel(logging.DEBUG)
log_dir = Path('logs')
log_dir.mkdir(exist_ok=True)
file_handler = RotatingFileHandler(
log_dir / 'dlna_server.log',
maxBytes=5*1024*1024,
backupCount=5
)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(
logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(
logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
)
logger.addHandler(file_handler)
logger.addHandler(console_handler)
return logger
class NetworkErrorHandler:
"""Handles network-related errors and implements retry logic"""
def __init__(self, logger, max_retries=3, retry_delay=1):
self.logger = logger
self.max_retries = max_retries
self.retry_delay = retry_delay
def with_retry(self, func, *args, **kwargs):
"""Execute a function with retry logic"""
last_exception = None
for attempt in range(self.max_retries):
try:
return func(*args, **kwargs)
except socket.error as e:
last_exception = e
self.logger.warning(f"Network operation failed (attempt {attempt + 1}/{self.max_retries}): {str(e)}")
if attempt < self.max_retries - 1:
time.sleep(self.retry_delay)
raise last_exception
class SSDPServer:
"""SSDP server that coexists with Windows 11 UPnP"""
def __init__(self, http_server_address):
self.logger = logging.getLogger('DLNAServer')
self.http_server_address = http_server_address
self.running = False
self.discovery_count = 0
self.announcement_interval = 5
self.error_handler = NetworkErrorHandler(self.logger)
self.socket = None
self.announce_socket = None
def send_alive_notification(self):
"""Send SSDP presence notification with enhanced Samsung compatibility"""
services = [
'upnp:rootdevice',
'urn:schemas-upnp-org:device:MediaServer:1',
'urn:schemas-upnp-org:service:ContentDirectory:1',
'urn:schemas-upnp-org:service:ConnectionManager:1'
]
location = f'http://{self.http_server_address[0]}:{self.http_server_address[1]}/description.xml'
for service in services:
try:
notify_msg = '\r\n'.join([
'NOTIFY * HTTP/1.1',
f'HOST: {SSDP_ADDR}:{SSDP_PORT}',
'CACHE-CONTROL: max-age=1800',
f'LOCATION: {location}',
'NT: ' + service,
'NTS: ssdp:alive',
'SERVER: Windows/10.0 UPnP/1.0 Python-DLNA/1.0', # Changed server string
f'USN: uuid:{DEVICE_UUID}::{service}',
'BOOTID.UPNP.ORG: 1',
'CONFIGID.UPNP.ORG: 1',
'SEARCHPORT.UPNP.ORG: 1900',
# Samsung-specific headers
'X-DLNADOC: DMS-1.50',
'X-DLNACAP: av-upload,time-seek,connection-stalling,range', # Added range
'Content-Length: 0',
'',
''
])
# Send notification on all interfaces
interfaces = self.get_all_interfaces()
for interface in interfaces:
try:
# Bind to specific interface
self.announce_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
self.announce_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.announce_socket.bind((interface, 0))
# Set TTL to 4 for better network traversal
self.announce_socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 4)
# Send the notification
self.error_handler.with_retry(
lambda: self.announce_socket.sendto(notify_msg.encode(), (SSDP_ADDR, SSDP_PORT))
)
self.logger.info(f"Sent alive notification for service ['{service}'] on interface ['{interface}']")
except Exception as e:
self.logger.warning(f"Failed to send notification on interface {interface}: {str(e)}")
finally:
try:
self.announce_socket.close()
except:
pass
except Exception as e:
self.logger.error(f"Failed to send notification for service {service}: {str(e)}")
continue
def periodic_announce(self):
"""Periodically send presence announcements"""
initial_interval = 1 # Start with more frequent announcements
max_interval = 5 # Maximum interval between announcements
current_interval = initial_interval
while self.running:
try:
self.error_handler.with_retry(self.send_alive_notification)
# Sleep with interruption check
for _ in range(int(current_interval)):
if not self.running:
break
time.sleep(1)
# Gradually increase interval up to max_interval
if current_interval < max_interval:
current_interval = min(current_interval * 1.5, max_interval)
except Exception as e:
self.logger.error(f"Error in periodic announce: {str(e)}")
time.sleep(1) # Prevent tight loop on error
def get_all_interfaces(self):
"""Get all IPv4 addresses of all network interfaces with error handling"""
addresses = []
try:
# Try using netifaces library if available
try:
import netifaces
for interface in netifaces.interfaces():
try:
addrs = netifaces.ifaddresses(interface)
if netifaces.AF_INET in addrs:
for addr in addrs[netifaces.AF_INET]:
ip = addr['addr']
if not ip.startswith('127.'):
addresses.append(ip)
except Exception as e:
self.logger.debug(f"Error getting addresses for interface {interface}: {str(e)}")
except ImportError:
# Fallback to socket method
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# Doesn't actually connect but gets local interface IP
s.connect(('8.8.8.8', 80))
addresses.append(s.getsockname()[0])
finally:
s.close()
# If all methods fail, add localhost
if not addresses:
addresses.append('127.0.0.1')
self.logger.info(f"Found network interfaces: {addresses}")
return addresses
except Exception as e:
self.logger.error(f"Error getting network interfaces: {str(e)}")
return ['127.0.0.1'] # Return localhost as fallback
def initialize_sockets(self):
"""Initialize network sockets with Windows-specific handling"""
try:
# Create main multicast socket with specific options for Windows
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Set multicast TTL
self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 4)
# Allow multiple bindings for Windows
if hasattr(socket, 'SO_REUSEPORT'): # Not available on Windows
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
# Create separate socket for sending with specific options
self.announce_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
self.announce_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.announce_socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 4)
# Bind to all interfaces
try:
self.socket.bind(('', SSDP_PORT))
except socket.error as e:
if e.errno == errno.EADDRINUSE:
# Port in use is expected on Windows - we can still send/receive
self.logger.info("SSDP port already in use (Windows UPnP). Continuing in shared mode.")
return True
else:
raise
# Set multicast interface for the announce socket
interfaces = self.get_all_interfaces()
if interfaces:
self.announce_socket.setsockopt(
socket.IPPROTO_IP,
socket.IP_MULTICAST_IF,
socket.inet_aton(interfaces[0])
)
self.logger.info(f"SSDP server initialized successfully")
return True
except Exception as e:
self.logger.error(f"Failed to initialize SSDP sockets: {str(e)}")
self.cleanup_sockets()
return False
def cleanup_sockets(self):
"""Clean up network sockets"""
try:
if self.socket:
self.socket.close()
if self.announce_socket:
self.announce_socket.close()
except Exception as e:
self.logger.error(f"Error during socket cleanup: {str(e)}")
def join_multicast_group(self):
"""Join multicast group with Windows-specific handling"""
successful_joins = 0
interfaces = self.get_all_interfaces()
for addr in interfaces:
try:
# Windows-specific: Join group on specific interface
mreq = socket.inet_aton(SSDP_ADDR) + socket.inet_aton(addr)
self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
# Set multicast interface
self.socket.setsockopt(
socket.IPPROTO_IP,
socket.IP_MULTICAST_IF,
socket.inet_aton(addr)
)
self.logger.info(f"Successfully joined multicast group on interface: {addr}")
successful_joins += 1
except Exception as e:
self.logger.warning(f"Failed to join multicast group on interface {addr}: {str(e)}")
if successful_joins == 0:
self.logger.error("Failed to join multicast group on any interface")
return False
return True
def safe_send(self, data, addr):
"""Send data with error handling"""
try:
return self.error_handler.with_retry(
lambda: self.announce_socket.sendto(data, addr)
)
except Exception as e:
self.logger.error(f"Failed to send data to {addr}: {str(e)}")
return None
def handle_request(self, data, addr):
"""Handle incoming SSDP requests with error handling"""
try:
request = data.decode().split('\r\n')
self.logger.info(f"Received SSDP request from {addr[0]}:{addr[1]}")
self.logger.debug(f"Full request: {request}")
if 'M-SEARCH * HTTP/1.1' in request[0]:
try:
# Parse headers
headers = {}
for line in request:
if ': ' in line:
key, value = line.split(': ', 1)
headers[key.upper()] = value
self.logger.debug(f"M-SEARCH headers: {headers}")
# Check various search targets
st = headers.get('ST', '')
search_targets = [
'ssdp:all',
'upnp:rootdevice',
f'uuid:{DEVICE_UUID}',
'urn:schemas-upnp-org:device:MediaServer:1',
'urn:schemas-upnp-org:service:ContentDirectory:1'
]
if st in search_targets:
self.logger.info(f"Matching search target: {st}")
self.send_discovery_response(addr, st)
else:
self.logger.debug(f"Ignoring search request with ST: {st}")
except Exception as e:
self.logger.error(f"Error processing M-SEARCH request: {str(e)}")
except UnicodeDecodeError as e:
self.logger.warning(f"Received malformed SSDP request from {addr[0]}:{addr[1]}: {str(e)}")
except Exception as e:
self.logger.error(f"Error handling SSDP request from {addr[0]}:{addr[1]}: {str(e)}")
def send_discovery_response(self, addr, st):
"""Send SSDP discovery response with error handling"""
try:
self.discovery_count += 1
self.logger.info(f"Sending discovery response to {addr[0]}:{addr[1]} (Total responses: {self.discovery_count})")
response = '\r\n'.join([
'HTTP/1.1 200 OK',
'CACHE-CONTROL: max-age=1800',
'DATE: ' + time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime()),
'EXT:',
f'LOCATION: http://{self.http_server_address[0]}:{self.http_server_address[1]}/description.xml',
'SERVER: Windows/10.0 UPnP/1.0 Python-DLNA/1.0',
f'ST: {st}',
f'USN: uuid:{DEVICE_UUID}::{st}',
'BOOTID.UPNP.ORG: 1',
'CONFIGID.UPNP.ORG: 1',
'X-DLNADOC: DMS-1.50',
'',
''
])
try:
self.error_handler.with_retry(
lambda: self.socket.sendto(response.encode(), addr)
)
self.logger.debug(f"Discovery response sent successfully to {addr[0]}:{addr[1]}")
except Exception as e:
raise Exception(f"Failed to send discovery response: {str(e)}")
except Exception as e:
self.logger.error(f"Error preparing/sending discovery response to {addr[0]}:{addr[1]}: {str(e)}")
def start(self):
"""Start SSDP server with error handling"""
if not self.initialize_sockets():
self.logger.error("Failed to initialize SSDP server")
return
if not self.join_multicast_group():
self.logger.error("Failed to join multicast group")
self.cleanup_sockets()
return
self.running = True
self.logger.info(f"SSDP server started on {SSDP_ADDR}:{SSDP_PORT}")
# Start announcement thread with error handling
self.announcement_thread = threading.Thread(target=self.periodic_announce)
self.announcement_thread.daemon = True
self.announcement_thread.start()
try:
self.send_alive_notification() # Send initial announcement
except Exception as e:
self.logger.error(f"Failed to send initial announcement: {str(e)}")
while self.running:
try:
data, addr = self.socket.recvfrom(1024)
self.handle_request(data, addr)
except socket.timeout:
continue
except socket.error as e:
if self.running:
self.logger.error(f"Socket error during receive: {str(e)}")
# Try to recover from socket error
if not self.recover_from_error():
break
except Exception as e:
if self.running:
self.logger.error(f"Unexpected error in SSDP server: {str(e)}")
if not self.recover_from_error():
break
self.cleanup_sockets()
def recover_from_error(self):
"""Attempt to recover from network errors"""
try:
self.logger.info("Attempting to recover from network error...")
self.cleanup_sockets()
time.sleep(1) # Wait before attempting recovery
if self.initialize_sockets() and self.join_multicast_group():
self.logger.info("Successfully recovered from network error")
return True
else:
self.logger.error("Failed to recover from network error")
return False
except Exception as e:
self.logger.error(f"Error during recovery attempt: {str(e)}")
return False
class DLNAServer(BaseHTTPRequestHandler):
def __init__(self, *args, **kwargs):
self.logger = logging.getLogger('DLNAServer')
self.protocol_version = 'HTTP/1.1'
self.timeout = 60 # Set timeout to 60 seconds
super().__init__(*args, **kwargs)
def do_GET(self):
"""Handle GET requests with improved error handling"""
try:
# Parse the path properly
from urllib.parse import urlparse
parsed_path = urlparse(self.path)
clean_path = parsed_path.path # This removes any http:// prefixes
if clean_path == '/description.xml':
self.send_device_description()
elif clean_path == '/ContentDirectory.xml':
self.send_content_directory()
elif clean_path == '/ConnectionManager.xml':
self.send_connection_manager()
elif clean_path == '/AVTransport.xml':
self.send_av_transport()
else:
# Remove leading slash and handle media file requests
path = clean_path[1:]
if path:
self.serve_media_file(path)
else:
self.send_error(404, "File not found")
except ConnectionAbortedError:
self.logger.warning("Client connection was aborted")
return
except Exception as e:
self.logger.error(f"Error handling GET request: {str(e)}")
try:
self.send_error(500, "Internal server error")
except:
# If we can't send the error response, just log it
self.logger.error("Could not send error response to client")
return
def do_POST(self):
"""Handle POST requests"""
try:
if self.path == '/ContentDirectory/control':
self.handle_content_directory_control()
else:
self.send_error(501, "Unsupported method ('POST')")
except Exception as e:
self.logger.error(f"Error handling POST request: {str(e)}")
try:
self.send_error(500, "Internal server error")
except Exception as e:
self.logger.error(f"Could not return error response to client: {str(e)}")
return
def handle_content_directory_control(self):
"""Handle POST requests to /ContentDirectory/control with improved XML handling"""
try:
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
self.logger.info(f"Received POST data: {post_data}")
# Parse the SOAP request
envelope = fromstring(post_data)
body = envelope.find('{http://schemas.xmlsoap.org/soap/envelope/}Body')
browse = body.find('{urn:schemas-upnp-org:service:ContentDirectory:1}Browse')
if browse is not None:
object_id = browse.find('ObjectID').text
browse_flag = browse.find('BrowseFlag').text
filter = browse.find('Filter').text
starting_index = int(browse.find('StartingIndex').text)
requested_count = int(browse.find('RequestedCount').text)
sort_criteria = browse.find('SortCriteria').text
# Handle root container specially
if object_id == '0' or object_id == '':
result = self.handle_root_container(browse_flag, starting_index, requested_count)
else:
# Handle regular browse requests
result = self.handle_browse_request(object_id, browse_flag, starting_index, requested_count)
# Create SOAP response
soap_response = f'''<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:BrowseResponse xmlns:u="urn:schemas-upnp-org:service:ContentDirectory:1">
<Result>{result['Result']}</Result>
<NumberReturned>{result['NumberReturned']}</NumberReturned>
<TotalMatches>{result['TotalMatches']}</TotalMatches>
<UpdateID>1</UpdateID>
</u:BrowseResponse>
</s:Body>
</s:Envelope>'''
self.send_response(200)
self.send_header('Content-Type', 'text/xml; charset="utf-8"')
self.send_header('Ext', '')
self.send_header('Server', 'Windows/1.0 UPnP/1.0 MiniDLNA/1.0')
self.send_header('Content-Length', len(soap_response))
self.end_headers()
self.wfile.write(soap_response.encode('utf-8'))
else:
self.send_error(400, "Invalid SOAP request")
except Exception as e:
self.logger.error(f"Error handling content directory control: {str(e)}")
self.send_error(500, "Internal server error")
def handle_root_container(self, browse_flag, starting_index, requested_count):
"""Handle browsing of the root container"""
try:
root = Element('DIDL-Lite', {
'xmlns': 'urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/',
'xmlns:dc': 'http://purl.org/dc/elements/1.1/',
'xmlns:upnp': 'urn:schemas-upnp-org:metadata-1-0/upnp/',
'xmlns:dlna': 'urn:schemas-dlna-org:metadata-1-0'
})
if browse_flag == 'BrowseMetadata':
# Return root container metadata
container = SubElement(root, 'container', {
'id': '0',
'parentID': '-1',
'restricted': '1',
'searchable': '1'
})
SubElement(container, 'dc:title').text = 'Root'
SubElement(container, 'upnp:class').text = 'object.container'
SubElement(container, 'upnp:storageUsed').text = '-1'
return {
'Result': tostring(root, encoding='unicode'),
'NumberReturned': 1,
'TotalMatches': 1
}
elif browse_flag == 'BrowseDirectChildren':
# List contents of the media folder
items = sorted(os.listdir(self.server.media_folder))
total_matches = len(items)
# Apply pagination
items = items[starting_index:starting_index + requested_count if requested_count > 0 else None]
for item in items:
item_path = os.path.join(self.server.media_folder, item)
if os.path.isdir(item_path):
self.add_container_to_didl(root, item_path, item, '0')
else:
self.add_item_to_didl(root, item_path, item, '0')
return {
'Result': tostring(root, encoding='unicode'),
'NumberReturned': len(items),
'TotalMatches': total_matches
}
except Exception as e:
self.logger.error(f"Error handling root container: {str(e)}")
raise
def handle_browse_request(self, object_id, browse_flag, starting_index, requested_count):
"""Handle browsing of non-root containers and items"""
try:
root = Element('DIDL-Lite', {
'xmlns': 'urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/',
'xmlns:dc': 'http://purl.org/dc/elements/1.1/',
'xmlns:upnp': 'urn:schemas-upnp-org:metadata-1-0/upnp/',
'xmlns:dlna': 'urn:schemas-dlna-org:metadata-1-0'
})
path = os.path.join(self.server.media_folder, unquote(object_id))
if not os.path.exists(path):
raise ValueError(f"Path does not exist: {path}")
if browse_flag == 'BrowseMetadata':
# Return metadata for the specific item/container
if os.path.isdir(path):
self.add_container_to_didl(root, path, os.path.basename(path), os.path.dirname(object_id))
else:
self.add_item_to_didl(root, path, os.path.basename(path), os.path.dirname(object_id))
return {
'Result': tostring(root, encoding='unicode'),
'NumberReturned': 1,
'TotalMatches': 1
}
elif browse_flag == 'BrowseDirectChildren' and os.path.isdir(path):
# List contents of the directory
items = sorted(os.listdir(path))
total_matches = len(items)
# Apply pagination
items = items[starting_index:starting_index + requested_count if requested_count > 0 else None]
for item in items:
item_path = os.path.join(path, item)
if os.path.isdir(item_path):
self.add_container_to_didl(root, item_path, item, object_id)
else:
self.add_item_to_didl(root, item_path, item, object_id)
return {
'Result': tostring(root, encoding='unicode'),
'NumberReturned': len(items),
'TotalMatches': total_matches
}
except Exception as e:
self.logger.error(f"Error handling browse request: {str(e)}")
raise
def add_container_to_didl(self, root, path, title, parent_id):
"""Add a container (directory) to the DIDL-Lite XML"""
try:
container = SubElement(root, 'container', {
'id': quote(os.path.relpath(path, self.server.media_folder)),
'parentID': quote(parent_id),
'restricted': '1',
'searchable': '1',
'childCount': str(len(os.listdir(path)))
})
SubElement(container, 'dc:title').text = title
SubElement(container, 'upnp:class').text = 'object.container.storageFolder'
# Add creation date
creation_time = datetime.fromtimestamp(os.path.getctime(path))
SubElement(container, 'dc:date').text = creation_time.isoformat()
except Exception as e:
self.logger.error(f"Error adding container to DIDL: {str(e)}")
raise
def add_item_to_didl(self, root, path, title, parent_id):
"""Add an item (file) to the DIDL-Lite XML with improved metadata"""
try:
mime_type, upnp_class = self.get_mime_and_upnp_class(title)
relative_path = os.path.relpath(path, self.server.media_folder)
item = SubElement(root, 'item', {
'id': quote(relative_path),
'parentID': quote(parent_id),
'restricted': '1'
})
SubElement(item, 'dc:title').text = title
SubElement(item, 'upnp:class').text = upnp_class
# Add resource element
res = SubElement(item, 'res')
file_size = os.path.getsize(path)
url = f'http://{self.server.server_address[0]}:{self.server.server_address[1]}/{quote(relative_path)}'
res.text = url
# Add protocol info with DLNA parameters
protocol_info = f'http-get:*:{mime_type}:'
protocol_info += 'DLNA.ORG_OP=01;' # Allow range requests
protocol_info += 'DLNA.ORG_CI=0;' # No conversion
protocol_info += 'DLNA.ORG_FLAGS=01700000000000000000000000000000' # Standard DLNA flags
res.set('protocolInfo', protocol_info)
res.set('size', str(file_size))
# Add media-specific metadata
if upnp_class.startswith('object.item.audioItem'):
self.add_audio_metadata(path, item)
elif upnp_class.startswith('object.item.videoItem'):
self.add_video_metadata(path, item)
elif upnp_class.startswith('object.item.imageItem'):
self.add_image_metadata(path, item)
# Add creation date
creation_time = datetime.fromtimestamp(os.path.getctime(path))
SubElement(item, 'dc:date').text = creation_time.isoformat()
except Exception as e:
self.logger.error(f"Error adding item to DIDL: {str(e)}")
raise
def get_metadata_for_object(self, object_id):
try:
path = os.path.abspath(object_id)
if not os.path.exists(path):
return self.create_default_metadata(object_id)
if os.path.isdir(path):
return self.get_directory_metadata(path)
else:
return self.get_file_metadata(path)
except Exception as e:
self.logger.error(f"Error handling content directory control: {str(e)}")
self.send_error(500, "Internal server error")
def get_file_metadata(self, path):
metadata = {
'id': path,
'parentID': os.path.dirname(path),
'title': os.path.basename(path),
'date': datetime.fromtimestamp(os.path.getmtime(path)).isoformat(),
'size': str(os.path.getsize(path)),
'class': 'object.item'
}
if path.lower().endswith(('.mp3', '.flac', '.m4a', '.wma')):
try:
audio = File(path)
if audio:
metadata.update({
'artist': audio.get('artist', ['Unknown'])[0],
'album': audio.get('album', ['Unknown'])[0],
'genre': audio.get('genre', ['Unknown'])[0],
'class': 'object.item.audioItem.musicTrack'
})
except:
pass
return metadata
def get_directory_metadata(self, path):
return {
'id': path,
'parentID': os.path.dirname(path),
'title': os.path.basename(path),
'date': datetime.fromtimestamp(os.path.getmtime(path)).isoformat(),
'class': 'object.container.storageFolder'
}
def create_default_metadata(self, object_id):
return {
'id': object_id,
'parentID': '0',
'title': 'Unknown',
'date': datetime.now().isoformat(),
'class': 'object.item',
'artist': 'Unknown',
'album': 'Unknown',
'genre': 'Unknown'
}
def create_browse_metadata_response(self, metadata):
# Implement the logic to create a response for BrowseMetadata
response = f"""
<DIDL-Lite xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/"
xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/">
<item id="{metadata['id']}" parentID="0" restricted="1">
<dc:title>{metadata['title']}</dc:title>
<upnp:artist>{metadata['artist']}</upnp:artist>
<upnp:album>{metadata['album']}</upnp:album>
<upnp:genre>{metadata['genre']}</upnp:genre>
<dc:date>{metadata['date']}</dc:date>
</item>
</DIDL-Lite>
"""
return response
def generate_browse_response(self, object_id, starting_index, requested_count):
"""Generate a DIDL-Lite XML response for the Browse request"""
root = Element('DIDL-Lite', {
'xmlns': 'urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/',
'xmlns:dc': 'http://purl.org/dc/elements/1.1/',
'xmlns:upnp': 'urn:schemas-upnp-org:metadata-1-0/upnp/',
'xmlns:dlna': 'urn:schemas-dlna-org:metadata-1-0'
})
media_path = os.path.abspath(os.path.join(self.server.media_folder, object_id))
items = []
total_matches = 0
try:
if os.path.isdir(media_path):
items = sorted(os.listdir(media_path))
total_matches = len(items)
items = items[starting_index:starting_index + requested_count]
for item in items:
item_path = os.path.join(media_path, item)
item_id = os.path.relpath(item_path, self.server.media_folder)
if os.path.isdir(item_path):
container = SubElement(root, 'container', {
'id': quote(item_id),
'parentID': quote(object_id),
'restricted': '1',
'searchable': '1',
'childCount': str(len(os.listdir(item_path)))
})
SubElement(container, 'dc:title').text = item
SubElement(container, 'upnp:class').text = 'object.container.storageFolder'
else:
# File handling
item_element = SubElement(root, 'item', {
'id': quote(item_id),
'parentID': quote(object_id),
'restricted': '1'
})
SubElement(item_element, 'dc:title').text = item
# Determine media type and set appropriate class
mime_type, upnp_class = self.get_mime_and_upnp_class(item)
SubElement(item_element, 'upnp:class').text = upnp_class
# Add resource element with proper DLNA attributes
res = SubElement(item_element, 'res')
file_size = os.path.getsize(item_path)
url = f'http://{self.server.server_address[0]}:{self.server.server_address[1]}/{quote(item_id)}'
res.text = url
res.set('protocolInfo', f'http-get:*:{mime_type}:DLNA.ORG_OP=01;DLNA.ORG_CI=0;')
res.set('size', str(file_size))
# Add media-specific metadata
if upnp_class.startswith('object.item.audioItem'):
self.add_audio_metadata(item_path, item_element)
elif upnp_class.startswith('object.item.videoItem'):
self.add_video_metadata(item_path, item_element)
elif upnp_class.startswith('object.item.imageItem'):
self.add_image_metadata(item_path, item_element)
except Exception as e:
self.logger.error(f"Error generating browse response: {str(e)}")
raise
response = f'''<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:BrowseResponse xmlns:u="urn:schemas-upnp-org:service:ContentDirectory:1">
<Result>{tostring(root, encoding='unicode')}</Result>
<NumberReturned>{len(items)}</NumberReturned>
<TotalMatches>{total_matches}</TotalMatches>
<UpdateID>1</UpdateID>
</u:BrowseResponse>
</s:Body>
</s:Envelope>'''
return response
def get_mime_and_upnp_class(self, filename):
"""Determine MIME type and UPnP class based on file extension"""
ext = os.path.splitext(filename)[1].lower()
# Video files
if ext in ['.mp4', '.mkv', '.avi', '.mov']:
return 'video/mp4', 'object.item.videoItem'
# Audio files
elif ext in ['.mp3', '.flac', '.wav', '.m4a']:
return 'audio/mpeg', 'object.item.audioItem.musicTrack'
# Image files
elif ext in ['.jpg', '.jpeg', '.png', '.gif']:
return 'image/jpeg', 'object.item.imageItem.photo'
# Default
return 'application/octet-stream', 'object.item'
def add_audio_metadata(self, file_path, item_element):
"""Add audio-specific metadata"""
try:
audio = File(file_path)
if audio:
if hasattr(audio, 'tags'):
tags = audio.tags
if 'artist' in tags:
SubElement(item_element, 'upnp:artist').text = str(tags['artist'][0])
if 'album' in tags:
SubElement(item_element, 'upnp:album').text = str(tags['album'][0])
if 'genre' in tags:
SubElement(item_element, 'upnp:genre').text = str(tags['genre'][0])
except Exception as e:
self.logger.warning(f"Error reading audio metadata: {str(e)}")
def add_video_metadata(self, file_path, item_element):
"""Add video-specific metadata"""
# Add basic video metadata
SubElement(item_element, 'upnp:genre').text = "Unknown"
SubElement(item_element, 'dc:publisher').text = "Unknown"
# You could expand this using a video metadata library like ffmpeg-python
# to extract resolution, duration, etc.
def add_image_metadata(self, file_path, item_element):
"""Add image-specific metadata"""
try:
from PIL import Image
with Image.open(file_path) as img:
width, height = img.size
SubElement(item_element, 'upnp:resolution').text = f"{width}x{height}"
except Exception as e:
self.logger.warning(f"Error reading image metadata: {str(e)}")
def send_device_description(self):
"""Send DLNA device description XML with Samsung compatibility"""
try:
# Create the device description XML
root = Element('root', {
'xmlns': 'urn:schemas-upnp-org:device-1-0',
'xmlns:dlna': 'urn:schemas-dlna-org:device-1-0',
'xmlns:sec': 'http://www.sec.co.kr/dlna' # Added Samsung namespace
})
# Add specVersion
spec_version = SubElement(root, 'specVersion')
SubElement(spec_version, 'major').text = '1'
SubElement(spec_version, 'minor').text = '0'
# Add device information
device = SubElement(root, 'device')
SubElement(device, 'deviceType').text = 'urn:schemas-upnp-org:device:MediaServer:1'
SubElement(device, 'friendlyName').text = DEVICE_NAME
SubElement(device, 'manufacturer').text = 'Python DLNA'
SubElement(device, 'manufacturerURL').text = 'http://example.com'
SubElement(device, 'modelDescription').text = 'Python DLNA Media Server'
SubElement(device, 'modelName').text = 'Python DLNA'
SubElement(device, 'modelNumber').text = '1.0'
SubElement(device, 'modelURL').text = 'http://example.com'
SubElement(device, 'serialNumber').text = '1'
SubElement(device, 'UDN').text = f'uuid:{DEVICE_UUID}'
# Add Samsung-specific elements
SubElement(device, 'sec:ProductCap').text = 'smi,DCM10,getMediaInfo.sec,getCaptionInfo.sec'
SubElement(device, 'sec:X_ProductCap').text = 'smi,DCM10,getMediaInfo.sec,getCaptionInfo.sec'
# Add dlna:X_DLNADOC
SubElement(device, 'dlna:X_DLNADOC').text = 'DMS-1.50'
# Add service list
service_list = SubElement(device, 'serviceList')
# Content Directory service
service1 = SubElement(service_list, 'service')
SubElement(service1, 'serviceType').text = 'urn:schemas-upnp-org:service:ContentDirectory:1'
SubElement(service1, 'serviceId').text = 'urn:upnp-org:serviceId:ContentDirectory'
SubElement(service1, 'SCPDURL').text = '/ContentDirectory.xml'
SubElement(service1, 'controlURL').text = '/ContentDirectory/control'
SubElement(service1, 'eventSubURL').text = '/ContentDirectory/event'
# Connection Manager service
service2 = SubElement(service_list, 'service')
SubElement(service2, 'serviceType').text = 'urn:schemas-upnp-org:service:ConnectionManager:1'
SubElement(service2, 'serviceId').text = 'urn:upnp-org:serviceId:ConnectionManager'
SubElement(service2, 'SCPDURL').text = '/ConnectionManager.xml'
SubElement(service2, 'controlURL').text = '/ConnectionManager/control'
SubElement(service2, 'eventSubURL').text = '/ConnectionManager/event'
# AVTransport service
service3 = SubElement(service_list, 'service')
SubElement(service3, 'serviceType').text = 'urn:schemas-upnp-org:service:AVTransport:1'