Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·605 lines (507 loc) · 21.2 KB
/
Copy pathserver.py
File metadata and controls
executable file
·605 lines (507 loc) · 21.2 KB
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
#!/usr/bin/env python3
"""
Camera Streaming HTTP Server
"""
import os
import sys
import json
import time
import signal
import threading
import zipfile
from io import BytesIO
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse
from socketserver import ThreadingMixIn
import struct
import hashlib
import base64
from camera import CameraCapture
from settings import Settings
class WebSocketClient:
def __init__(self, sock):
self.sock = sock
self.connected = True
def send(self, data):
try:
if isinstance(data, str):
data = data.encode('utf-8')
frame = bytearray()
frame.append(0x82)
if len(data) < 126:
frame.append(len(data))
elif len(data) < 65536:
frame.append(126)
frame.extend(struct.pack('>H', len(data)))
else:
frame.append(127)
frame.extend(struct.pack('>Q', len(data)))
frame.extend(data)
self.sock.sendall(bytes(frame))
except:
self.connected = False
def close(self):
self.connected = False
try:
self.sock.close()
except:
pass
class ThreadingHTTPServer(ThreadingMixIn, HTTPServer):
allow_reuse_address = True
daemon_threads = True
class AppState:
def __init__(self):
self.camera = None
self.settings = None
self.recording = False
self.recording_frames = []
self.static_dir = None
self.ws_clients = []
self.browser_frame = None
self.capture_mode = 'termux'
self.frame_lock = threading.Lock()
self.current_fps = 0
self.fps_counter = 0
self.fps_timer = time.time()
self.browser_settings = {
'quality': 50,
'width': 640,
'height': 480,
'rotation': 0,
'zoom': 1.0,
'mirror_h': False,
'mirror_v': False
}
app = AppState()
class CameraHandler(BaseHTTPRequestHandler):
def _send_response(self, code, content_type='text/html', body=None, headers=None):
try:
self.send_response(code)
self.send_header('Content-Type', content_type)
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
if headers:
for key, value in headers.items():
self.send_header(key, value)
if body:
if isinstance(body, str):
body = body.encode('utf-8')
self.send_header('Content-Length', len(body))
self.end_headers()
if body:
self.wfile.write(body)
self.wfile.flush()
except:
pass
def _read_body(self):
try:
content_length = int(self.headers.get('Content-Length', 0))
if content_length > 0:
return self.rfile.read(content_length)
except:
pass
return b'{}'
def _broadcast_to_capture_clients(self, message):
"""Send message to all connected WebSocket clients"""
dead_clients = []
for client in app.ws_clients:
try:
client.send(message)
except:
dead_clients.append(client)
for client in dead_clients:
if client in app.ws_clients:
app.ws_clients.remove(client)
def _handle_websocket_upgrade(self):
try:
key = self.headers.get('Sec-WebSocket-Key', '')
if not key:
return False
magic = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
accept_key = base64.b64encode(
hashlib.sha1((key + magic).encode()).digest()
).decode()
self.send_response(101)
self.send_header('Upgrade', 'websocket')
self.send_header('Connection', 'Upgrade')
self.send_header('Sec-WebSocket-Accept', accept_key)
self.end_headers()
client = WebSocketClient(self.request)
app.ws_clients.append(client)
print(f"WebSocket client connected (total: {len(app.ws_clients)})")
# Send current settings immediately
settings_msg = json.dumps({
'type': 'settings',
'settings': app.browser_settings
})
client.send(settings_msg)
# Send mode status
mode_msg = json.dumps({
'type': 'mode_change',
'mode': app.capture_mode,
'should_capture': app.capture_mode == 'browser'
})
client.send(mode_msg)
while client.connected:
try:
header = self.request.recv(2)
if not header or len(header) < 2:
break
opcode = header[0] & 0x0F
if opcode == 0x8:
break
masked = header[1] & 0x80
length = header[1] & 0x7F
if length == 126:
length = struct.unpack('>H', self.request.recv(2))[0]
elif length == 127:
length = struct.unpack('>Q', self.request.recv(8))[0]
if masked:
mask_key = self.request.recv(4)
data = self.request.recv(length)
data = bytes(b ^ mask_key[i % 4] for i, b in enumerate(data))
else:
data = self.request.recv(length)
# Handle text messages
if opcode == 0x1:
try:
msg = json.loads(data.decode('utf-8'))
if msg.get('type') == 'get_settings':
settings_msg = json.dumps({
'type': 'settings',
'settings': app.browser_settings
})
client.send(settings_msg)
print("Sent settings to client:", app.browser_settings)
except:
pass
# Store frames only if in browser mode
if data and opcode == 0x2 and app.capture_mode == 'browser':
with app.frame_lock:
app.browser_frame = data
app.fps_counter += 1
now = time.time()
if now - app.fps_timer >= 1.0:
app.current_fps = app.fps_counter
app.fps_counter = 0
app.fps_timer = now
if app.recording:
app.recording_frames.append(data)
except:
break
if client in app.ws_clients:
app.ws_clients.remove(client)
client.close()
print(f"WebSocket client disconnected (total: {len(app.ws_clients)})")
return True
except Exception as e:
print(f"WebSocket error: {e}")
return False
def log_message(self, format, *args):
if '/stream' not in self.path and '/api/stats' not in self.path:
print(f"[{self.log_date_time_string()}] {self.path}")
def do_OPTIONS(self):
self._send_response(200)
def do_GET(self):
try:
if self.headers.get('Upgrade', '').lower() == 'websocket':
if self._handle_websocket_upgrade():
return
parsed = urlparse(self.path)
path = parsed.path
if path == '/api/settings':
self._handle_get_settings()
elif path == '/api/browser_settings':
self._handle_get_browser_settings()
elif path == '/api/stats':
self._handle_get_stats()
elif path == '/api/status':
self._handle_get_status()
elif path == '/stream':
self._handle_stream()
elif path == '/snapshot':
self._handle_snapshot()
elif path == '/recording/start':
self._handle_recording_start()
elif path == '/recording/stop':
self._handle_recording_stop()
elif path == '/download/zip':
self._handle_download_zip()
elif path == '/' or path == '':
self._serve_file('index.html')
elif path == '/capture':
self._serve_file('capture.html')
elif path.startswith('/'):
self._serve_file(path[1:])
else:
self._send_response(404, body='Not Found')
except Exception as e:
print(f"GET Error: {e}")
def do_POST(self):
try:
parsed = urlparse(self.path)
path = parsed.path
body = self._read_body()
try:
data = json.loads(body)
except:
data = {}
if path == '/api/settings':
self._handle_post_settings(data)
elif path == '/api/browser_settings':
self._handle_post_browser_settings(data)
elif path == '/api/command':
self._handle_command(data)
else:
self._send_response(404, body='Not Found')
except Exception as e:
print(f"POST Error: {e}")
def _serve_file(self, filename):
try:
filename = filename.split('?')[0]
filename = os.path.basename(filename)
if not filename:
filename = 'index.html'
filepath = os.path.join(app.static_dir, filename)
if not os.path.exists(filepath):
self._send_response(404, body='Not Found')
return
content_types = {
'.html': 'text/html; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.js': 'application/javascript; charset=utf-8',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.png': 'image/png',
'.gif': 'image/gif',
'.ico': 'image/x-icon',
'.svg': 'image/svg+xml',
'.json': 'application/json'
}
ext = os.path.splitext(filename)[1].lower()
content_type = content_types.get(ext, 'application/octet-stream')
with open(filepath, 'rb') as f:
content = f.read()
headers = {}
if ext in ['.jpg', '.jpeg', '.png', '.gif', '.ico', '.svg']:
headers['Cache-Control'] = 'public, max-age=3600'
else:
headers['Cache-Control'] = 'no-cache'
self._send_response(200, content_type, content, headers)
except Exception as e:
print(f"File error: {e}")
def _handle_get_settings(self):
if app.settings:
settings = app.settings.get_all()
self._send_response(200, 'application/json', json.dumps(settings))
else:
self._send_response(200, 'application/json', '{}')
def _handle_get_browser_settings(self):
print("GET browser settings:", app.browser_settings)
self._send_response(200, 'application/json', json.dumps(app.browser_settings))
def _handle_get_stats(self):
fps = app.current_fps if app.capture_mode == 'browser' else (app.camera.current_fps if app.camera else 0)
stats = {
'fps': fps,
'recording': app.recording,
'capture_mode': app.capture_mode,
'ws_clients': len(app.ws_clients)
}
self._send_response(200, 'application/json', json.dumps(stats))
def _handle_get_status(self):
status = {
'capture_mode': app.capture_mode,
'should_capture': app.capture_mode == 'browser',
'browser_settings': app.browser_settings
}
self._send_response(200, 'application/json', json.dumps(status))
def _handle_post_settings(self, data):
if app.settings and data:
data.pop('fps', None)
app.settings.update(data)
if app.camera:
app.camera.update_settings(data)
self._send_response(200, 'application/json', json.dumps({'status': 'ok'}))
def _handle_post_browser_settings(self, data):
print("POST browser settings received:", data)
for key in app.browser_settings:
if key in data:
app.browser_settings[key] = data[key]
print("Updated browser settings:", app.browser_settings)
# Broadcast to all capture clients
settings_msg = json.dumps({
'type': 'settings',
'settings': app.browser_settings
})
self._broadcast_to_capture_clients(settings_msg)
print(f"Broadcasted settings to {len(app.ws_clients)} clients")
self._send_response(200, 'application/json', json.dumps({'status': 'ok', 'settings': app.browser_settings}))
def _handle_command(self, data):
command = data.get('command', '')
if command == 'flash_on':
if app.camera:
app.camera.toggle_flash(True)
elif command == 'flash_off':
if app.camera:
app.camera.toggle_flash(False)
elif command == 'switch_capture':
mode = data.get('mode', 'termux')
app.capture_mode = mode
if mode == 'browser':
if app.camera:
app.camera.stop()
with app.frame_lock:
app.browser_frame = None
print("Switched to browser mode - Termux camera stopped")
else:
if app.camera:
app.camera.start()
with app.frame_lock:
app.browser_frame = None
app.current_fps = 0
print("Switched to termux mode - Termux camera started")
# Notify all capture clients
status_msg = json.dumps({
'type': 'mode_change',
'mode': mode,
'should_capture': mode == 'browser'
})
self._broadcast_to_capture_clients(status_msg)
# Also send current browser settings when switching to browser mode
if mode == 'browser':
settings_msg = json.dumps({
'type': 'settings',
'settings': app.browser_settings
})
self._broadcast_to_capture_clients(settings_msg)
self._send_response(200, 'application/json',
json.dumps({'status': 'ok', 'mode': mode}))
return
else:
self._send_response(400, 'application/json',
json.dumps({'error': f'Unknown command: {command}'}))
return
self._send_response(200, 'application/json', json.dumps({'status': 'ok'}))
def _handle_stream(self):
try:
self.send_response(200)
self.send_header('Content-Type',
'multipart/x-mixed-replace; boundary=frame')
self.send_header('Cache-Control', 'no-cache')
self.send_header('Connection', 'close')
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Pragma', 'no-cache')
self.end_headers()
last_frame = None
while True:
frame = None
if app.capture_mode == 'browser':
with app.frame_lock:
frame = app.browser_frame
elif app.camera:
frame = app.camera.get_frame()
if frame and frame != last_frame:
try:
self.wfile.write(b'--frame\r\n')
self.wfile.write(b'Content-Type: image/jpeg\r\n')
self.wfile.write(f'Content-Length: {len(frame)}\r\n'.encode())
self.wfile.write(b'\r\n')
self.wfile.write(frame)
self.wfile.write(b'\r\n')
self.wfile.flush()
last_frame = frame
except:
break
time.sleep(0.001 if app.capture_mode == 'browser' else 0.05)
except (BrokenPipeError, ConnectionResetError):
pass
except Exception as e:
print(f"Stream error: {e}")
def _handle_snapshot(self):
frame = None
if app.capture_mode == 'browser':
with app.frame_lock:
frame = app.browser_frame
elif app.camera:
frame = app.camera.get_frame()
if frame:
self._send_response(200, 'image/jpeg', frame, {
'Content-Disposition': 'attachment; filename="snapshot.jpg"'
})
else:
self._send_response(404, body='No frame')
def _handle_recording_start(self):
app.recording = True
app.recording_frames = []
self._send_response(200, 'application/json',
json.dumps({'status': 'recording_started'}))
def _handle_recording_stop(self):
app.recording = False
os.makedirs('recordings', exist_ok=True)
filename = f'recording_{int(time.time())}.mjpeg'
filepath = os.path.join('recordings', filename)
with open(filepath, 'wb') as f:
for frame in app.recording_frames:
f.write(b'--frame\r\n')
f.write(b'Content-Type: image/jpeg\r\n')
f.write(f'Content-Length: {len(frame)}\r\n'.encode())
f.write(b'\r\n')
f.write(frame)
f.write(b'\r\n')
self._send_response(200, 'application/json', json.dumps({
'status': 'recording_stopped',
'filename': filename,
'frames': len(app.recording_frames)
}))
def _handle_download_zip(self):
try:
zip_buffer = BytesIO()
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zf:
if os.path.exists('recordings'):
for f in os.listdir('recordings'):
filepath = os.path.join('recordings', f)
if os.path.isfile(filepath):
zf.write(filepath, f)
zip_buffer.seek(0)
self._send_response(200, 'application/zip', zip_buffer.getvalue(), {
'Content-Disposition': 'attachment; filename="recordings.zip"'
})
except Exception as e:
print(f"ZIP error: {e}")
self._send_response(500, body='Error')
def main():
import argparse
parser = argparse.ArgumentParser(description='Termux Camera Stream Server')
parser.add_argument('-p', '--port', type=int, default=8080, help='Port (default: 8080)')
args = parser.parse_args()
base_dir = os.path.dirname(os.path.abspath(__file__))
app.static_dir = os.path.join(base_dir, 'static')
os.makedirs(app.static_dir, exist_ok=True)
os.makedirs('recordings', exist_ok=True)
app.settings = Settings()
app.camera = CameraCapture(app.settings)
app.camera.start()
server = ThreadingHTTPServer(('', args.port), CameraHandler)
print("=" * 50)
print(" Termux Camera Streaming Server")
print("=" * 50)
print(f"\n📡 Server running on port {args.port}")
print(f"📱 Phone capture: http://localhost:{args.port}/capture")
print(f"🖥️ PC viewer: http://localhost:{args.port}")
print(f"\nPress Ctrl+C to stop\n")
server_thread = threading.Thread(target=server.serve_forever)
server_thread.daemon = True
server_thread.start()
try:
while server_thread.is_alive():
server_thread.join(1)
except KeyboardInterrupt:
print("\n\nShutting down...")
app.camera.stop()
server.shutdown()
server.server_close()
print("Server stopped. Goodbye!")
sys.exit(0)
if __name__ == '__main__':
main()