-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnative_call_widget.py
More file actions
446 lines (371 loc) · 14.9 KB
/
native_call_widget.py
File metadata and controls
446 lines (371 loc) · 14.9 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
# native_call_widget.py
import logging
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel
from PyQt6.QtCore import QObject, pyqtSignal, QTimer, QThread, pyqtSlot, Qt
from PyQt6.QtMultimedia import QMediaDevices, QAudioInput, QAudioOutput, QCamera, QMediaCaptureSession, QAudioSink, QAudioSource
from PyQt6.QtMultimediaWidgets import QVideoWidget
from PyQt6.QtCore import QIODevice, QByteArray
import socket
import json
import asyncio
import threading
log = logging.getLogger(__name__)
class AudioStreamer(QObject):
"""Handle audio streaming for voice calls"""
def __init__(self):
super().__init__()
self.audio_input = None
self.audio_output = None
self.audio_sink = None
self.audio_source = None
self.is_streaming = False
self.remote_socket = None
def start_audio_stream(self, remote_host: str, remote_port: int):
"""Start audio streaming"""
try:
# Setup audio input (microphone)
audio_device = QMediaDevices.defaultAudioInput()
if not audio_device.isNull():
self.audio_input = QAudioInput(audio_device)
# Setup audio output (speakers)
output_device = QMediaDevices.defaultAudioOutput()
if not output_device.isNull():
self.audio_output = QAudioOutput(output_device)
# Create socket for audio streaming
self.remote_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.is_streaming = True
log.info("Audio streaming started")
return True
except Exception as e:
log.error(f"Failed to start audio stream: {e}")
return False
def stop_audio_stream(self):
"""Stop audio streaming"""
self.is_streaming = False
if self.remote_socket:
self.remote_socket.close()
log.info("Audio streaming stopped")
class VideoStreamer(QObject):
"""Handle video streaming for video calls"""
def __init__(self):
super().__init__()
self.camera = None
self.capture_session = None
self.is_streaming = False
def start_video_stream(self, video_widget: QVideoWidget):
"""Start video streaming"""
try:
# Get default camera
cameras = QMediaDevices.videoInputs()
if cameras:
self.camera = QCamera(cameras[0])
self.capture_session = QMediaCaptureSession()
self.capture_session.setCamera(self.camera)
self.capture_session.setVideoOutput(video_widget)
self.camera.start()
self.is_streaming = True
log.info("Video streaming started")
return True
else:
log.error("No cameras found")
return False
except Exception as e:
log.error(f"Failed to start video stream: {e}")
return False
def stop_video_stream(self):
"""Stop video streaming"""
if self.camera:
self.camera.stop()
self.is_streaming = False
log.info("Video streaming stopped")
class VideoPlaceholder(QWidget):
"""Avatar placeholder for when video is disabled"""
def __init__(self, user_name="User", is_local=False, parent=None):
super().__init__(parent)
self.user_name = user_name
self.is_local = is_local
self.setMinimumSize(200, 150)
layout = QVBoxLayout(self)
layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
# Avatar circle
self.avatar_label = QLabel()
size = 60 if is_local else 80
self.avatar_label.setFixedSize(size, size)
self.avatar_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
# Generate initials
initials = ''.join([word[0].upper() for word in user_name.split()[:2]])
if not initials:
initials = "U"
# Color based on user
colors = ["#667eea", "#f093fb", "#4facfe", "#43e97b", "#fa709a"]
color = colors[hash(user_name) % len(colors)]
self.avatar_label.setStyleSheet(f"""
QLabel {{
background-color: {color};
border: 3px solid #ffffff;
border-radius: {size//2}px;
color: white;
font-size: {size//3}px;
font-weight: bold;
}}
""")
self.avatar_label.setText(initials)
# Name label
self.name_label = QLabel("You" if is_local else user_name)
self.name_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.name_label.setStyleSheet("""
QLabel {
color: #e2e8f0;
font-size: 14px;
font-weight: 500;
background-color: transparent;
margin-top: 8px;
}
""")
layout.addWidget(self.avatar_label)
layout.addWidget(self.name_label)
layout.setContentsMargins(10, 10, 10, 10)
# Background styling
self.setStyleSheet("""
VideoPlaceholder {
background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
stop:0 #4a5568, stop:1 #2d3748);
border: 2px solid #4a5568;
border-radius: 12px;
}
""")
class NativeCallWidget(QWidget):
"""Native Qt multimedia call widget"""
# Signals
call_connected = pyqtSignal()
call_disconnected = pyqtSignal()
call_error = pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent)
self.call_id = ""
self.call_type = "voice"
self.remote_user = ""
self.is_connected = False
self.video_enabled = True
# Multimedia components
self.audio_streamer = AudioStreamer()
self.video_streamer = VideoStreamer()
# UI components
self.local_video = QVideoWidget()
self.remote_video = QVideoWidget()
self.local_placeholder = None
self.remote_placeholder = None
self.setup_ui()
self.setup_multimedia()
def setup_ui(self):
"""Setup the user interface"""
layout = QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
# Main video area
self.video_container = QWidget()
self.video_layout = QHBoxLayout(self.video_container)
self.video_layout.setContentsMargins(10, 10, 10, 10)
self.video_layout.setSpacing(10)
# Local video area (smaller, top-left overlay style)
self.local_container = QWidget()
self.local_container.setFixedSize(200, 150)
self.local_layout = QVBoxLayout(self.local_container)
self.local_layout.setContentsMargins(0, 0, 0, 0)
# Remote video area (main area)
self.remote_container = QWidget()
self.remote_container.setMinimumSize(400, 300)
self.remote_layout = QVBoxLayout(self.remote_container)
self.remote_layout.setContentsMargins(0, 0, 0, 0)
# Add to video layout
self.video_layout.addWidget(self.local_container)
self.video_layout.addWidget(self.remote_container, 1)
layout.addWidget(self.video_container, 1)
# Controls (minimal, modern style)
controls_layout = QHBoxLayout()
controls_layout.setSpacing(20)
self.mute_btn = QPushButton()
self.mute_btn.setFixedSize(50, 50)
self.mute_btn.setCheckable(True)
self.mute_btn.clicked.connect(self.toggle_mute)
self.mute_btn.setStyleSheet("""
QPushButton {
background-color: #4a5568;
border: none;
border-radius: 25px;
color: white;
font-size: 20px;
}
QPushButton:hover {
background-color: #5a6578;
}
QPushButton:checked {
background-color: #e53e3e;
}
""")
self.mute_btn.setText("🎤")
self.video_btn = QPushButton()
self.video_btn.setFixedSize(50, 50)
self.video_btn.setCheckable(True)
self.video_btn.clicked.connect(self.toggle_video)
self.video_btn.setStyleSheet("""
QPushButton {
background-color: #4a5568;
border: none;
border-radius: 25px;
color: white;
font-size: 20px;
}
QPushButton:hover {
background-color: #5a6578;
}
QPushButton:checked {
background-color: #e53e3e;
}
""")
self.video_btn.setText("📹")
self.end_btn = QPushButton()
self.end_btn.setFixedSize(60, 60)
self.end_btn.clicked.connect(self.end_call)
self.end_btn.setStyleSheet("""
QPushButton {
background-color: #e53e3e;
border: none;
border-radius: 30px;
color: white;
font-size: 24px;
}
QPushButton:hover {
background-color: #c53030;
}
""")
self.end_btn.setText("📞")
controls_layout.addStretch()
controls_layout.addWidget(self.mute_btn)
controls_layout.addWidget(self.video_btn)
controls_layout.addWidget(self.end_btn)
controls_layout.addStretch()
layout.addLayout(controls_layout)
self.setLayout(layout)
# Set dark theme
self.setStyleSheet("""
NativeCallWidget {
background-color: #1a202c;
}
""")
def setup_multimedia(self):
"""Setup multimedia devices"""
try:
# Check available devices
audio_inputs = QMediaDevices.audioInputs()
audio_outputs = QMediaDevices.audioOutputs()
video_inputs = QMediaDevices.videoInputs()
log.info(f"Found {len(audio_inputs)} audio input devices")
log.info(f"Found {len(audio_outputs)} audio output devices")
log.info(f"Found {len(video_inputs)} video input devices")
if not audio_inputs:
self.call_error.emit("No microphone found")
return False
if not audio_outputs:
self.call_error.emit("No speakers found")
return False
return True
except Exception as e:
log.error(f"Failed to setup multimedia: {e}")
self.call_error.emit(f"Multimedia setup failed: {e}")
return False
def start_call(self, call_id: str, call_type: str, remote_user: str):
"""Start a call"""
self.call_id = call_id
self.call_type = call_type
self.remote_user = remote_user
try:
# Start audio streaming
if not self.audio_streamer.start_audio_stream("127.0.0.1", 8082):
self.call_error.emit("Failed to start audio")
return False
# Setup video or placeholders
if call_type == "video":
# Start video streaming
if self.video_streamer.start_video_stream(self.local_video):
self.show_video_widgets()
else:
# Fallback to placeholders if video fails
self.show_placeholder_widgets()
else:
# Voice call - show placeholders
self.show_placeholder_widgets()
self.is_connected = True
self.call_connected.emit()
log.info(f"Call started: {call_id} ({call_type})")
return True
except Exception as e:
log.error(f"Failed to start call: {e}")
self.call_error.emit(f"Failed to start call: {e}")
return False
def show_video_widgets(self):
"""Show actual video widgets"""
# Clear placeholders
self.clear_containers()
# Add video widgets
self.local_layout.addWidget(self.local_video)
self.remote_layout.addWidget(self.remote_video)
self.local_video.show()
self.remote_video.show()
def show_placeholder_widgets(self):
"""Show avatar placeholders"""
# Clear video widgets
self.clear_containers()
# Create placeholders
self.local_placeholder = VideoPlaceholder("You", is_local=True)
self.remote_placeholder = VideoPlaceholder(self.remote_user, is_local=False)
# Add placeholders
self.local_layout.addWidget(self.local_placeholder)
self.remote_layout.addWidget(self.remote_placeholder)
def clear_containers(self):
"""Clear all widgets from containers"""
# Clear local container
while self.local_layout.count():
child = self.local_layout.takeAt(0)
if child.widget():
child.widget().setParent(None)
# Clear remote container
while self.remote_layout.count():
child = self.remote_layout.takeAt(0)
if child.widget():
child.widget().setParent(None)
def end_call(self):
"""End the current call"""
try:
self.audio_streamer.stop_audio_stream()
self.video_streamer.stop_video_stream()
self.is_connected = False
self.call_disconnected.emit()
log.info(f"Call ended: {self.call_id}")
except Exception as e:
log.error(f"Error ending call: {e}")
def toggle_mute(self):
"""Toggle microphone mute"""
if self.mute_btn.isChecked():
self.mute_btn.setText("🔇")
else:
self.mute_btn.setText("🎤")
def toggle_video(self):
"""Toggle video on/off"""
if self.call_type != "video":
return # Can't toggle video in voice call
self.video_enabled = not self.video_btn.isChecked()
if self.video_enabled:
# Show video
self.show_video_widgets()
self.video_btn.setText("📹")
else:
# Show placeholders
self.show_placeholder_widgets()
self.video_btn.setText("🚫")
def get_local_video_widget(self):
"""Get the local video widget for external use"""
return self.local_video
def get_remote_video_widget(self):
"""Get the remote video widget for external use"""
return self.remote_video