c64-ready 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist-ts/emulator/c64-emulator.d.ts +7 -0
- package/dist-ts/emulator/c64-emulator.d.ts.map +1 -1
- package/dist-ts/emulator/c64-emulator.js +50 -6
- package/dist-ts/emulator/c64-emulator.js.map +1 -1
- package/dist-ts/emulator/constants.d.ts +3 -1
- package/dist-ts/emulator/constants.d.ts.map +1 -1
- package/dist-ts/emulator/constants.js +2 -0
- package/dist-ts/emulator/constants.js.map +1 -1
- package/dist-ts/emulator/input.d.ts +21 -1
- package/dist-ts/emulator/input.d.ts.map +1 -1
- package/dist-ts/emulator/input.js +295 -10
- package/dist-ts/emulator/input.js.map +1 -1
- package/dist-ts/types/emulator.d.ts +1 -1
- package/dist-ts/types/emulator.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/emulator/c64-emulator.ts +3 -0
- package/src/emulator/constants.ts +5 -1
- package/src/emulator/input.ts +131 -39
- package/src/headless/webrtc-server.mjs +73 -8
package/src/emulator/input.ts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
import type {C64Emulator} from './c64-emulator';
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import type { C64Emulator } from './c64-emulator';
|
|
2
|
+
import {
|
|
3
|
+
JOYSTICK_DIRECTION,
|
|
4
|
+
JOYSTICK_FIRE_1,
|
|
5
|
+
JOYSTICK_FIRE_2,
|
|
6
|
+
JOYSTICK_FIRE_3,
|
|
7
|
+
JOYSTICK_PORT_2,
|
|
8
|
+
type JoystickInput,
|
|
9
|
+
} from './constants';
|
|
10
|
+
import type { JoystickPort } from './constants';
|
|
4
11
|
|
|
5
12
|
export const KEY_TO_JOYSTICK = {
|
|
6
13
|
ArrowUp: JOYSTICK_DIRECTION.UP,
|
|
@@ -39,6 +46,8 @@ export const MIXED_JOYSTICK_KEYS: Record<string, number> = {
|
|
|
39
46
|
ArrowLeft: JOYSTICK_DIRECTION.LEFT,
|
|
40
47
|
ArrowRight: JOYSTICK_DIRECTION.RIGHT,
|
|
41
48
|
KeyZ: JOYSTICK_FIRE_1,
|
|
49
|
+
KeyX: JOYSTICK_FIRE_2,
|
|
50
|
+
KeyC: JOYSTICK_FIRE_3,
|
|
42
51
|
ControlLeft: JOYSTICK_FIRE_1,
|
|
43
52
|
};
|
|
44
53
|
|
|
@@ -132,9 +141,9 @@ export function domKeyToC64Actions(
|
|
|
132
141
|
const isDown = eventType === 'keydown';
|
|
133
142
|
const actions: C64KeyAction[] = [];
|
|
134
143
|
|
|
135
|
-
const press = (idx: number) => actions.push({key: idx, action: 'press'});
|
|
136
|
-
const release = (idx: number) => actions.push({key: idx, action: 'release'});
|
|
137
|
-
const main = (idx: number) => actions.push({key: idx, action: isDown ? 'press' : 'release'});
|
|
144
|
+
const press = (idx: number) => actions.push({ key: idx, action: 'press' });
|
|
145
|
+
const release = (idx: number) => actions.push({ key: idx, action: 'release' });
|
|
146
|
+
const main = (idx: number) => actions.push({ key: idx, action: isDown ? 'press' : 'release' });
|
|
138
147
|
|
|
139
148
|
// Handle host shift state first for plain alpha/numeric keys
|
|
140
149
|
if (isDown) {
|
|
@@ -479,6 +488,9 @@ export function domKeyToC64Actions(
|
|
|
479
488
|
|
|
480
489
|
type MappedControl = keyof typeof KEY_TO_JOYSTICK;
|
|
481
490
|
|
|
491
|
+
const POT_INACTIVE = 128;
|
|
492
|
+
const POT_ACTIVE = 0;
|
|
493
|
+
|
|
482
494
|
export class EmulatorInput {
|
|
483
495
|
private readonly emulator: C64Emulator;
|
|
484
496
|
private readonly target: EventTarget;
|
|
@@ -488,7 +500,14 @@ export class EmulatorInput {
|
|
|
488
500
|
private updateFrame: number = -1;
|
|
489
501
|
private gamepadIndex: number = -1;
|
|
490
502
|
private gamepadPresses: Array<boolean> = [];
|
|
491
|
-
|
|
503
|
+
/**
|
|
504
|
+
* Ultimate 8bitdo axes:
|
|
505
|
+
* [LeftX, LeftX, LeftUp, LeftDown, L2, RightX, RightX, RightUp, RightDown, R2, DpadX, DpadX, DpadY, DpadY]
|
|
506
|
+
**/
|
|
507
|
+
private gamepadAxes: Array<boolean> =
|
|
508
|
+
[false, false, false, false, false, false, false, false, false, false, false, false, false, false];
|
|
509
|
+
private potFire2Pressed = false;
|
|
510
|
+
private potFire3Pressed = false;
|
|
492
511
|
private readonly pressedControls = new Set<MappedControl>();
|
|
493
512
|
private readonly keyDownHandler = (event: KeyboardEvent): void => {
|
|
494
513
|
this.handleKeyDown(event);
|
|
@@ -515,14 +534,23 @@ export class EmulatorInput {
|
|
|
515
534
|
|
|
516
535
|
attach(): void {
|
|
517
536
|
this.target.addEventListener('gamepadconnected', this.gamepadConnectedHandler as EventListener);
|
|
518
|
-
this.target.addEventListener(
|
|
537
|
+
this.target.addEventListener(
|
|
538
|
+
'gamepaddisconnected',
|
|
539
|
+
this.gamepadDisconnectedHandler as EventListener,
|
|
540
|
+
);
|
|
519
541
|
this.target.addEventListener('keydown', this.keyDownHandler as EventListener);
|
|
520
542
|
this.target.addEventListener('keyup', this.keyUpHandler as EventListener);
|
|
521
543
|
}
|
|
522
544
|
|
|
523
545
|
detach(): void {
|
|
524
|
-
this.target.removeEventListener(
|
|
525
|
-
|
|
546
|
+
this.target.removeEventListener(
|
|
547
|
+
'gamepadconnected',
|
|
548
|
+
this.gamepadConnectedHandler as EventListener,
|
|
549
|
+
);
|
|
550
|
+
this.target.removeEventListener(
|
|
551
|
+
'gamepaddisconnected',
|
|
552
|
+
this.gamepadDisconnectedHandler as EventListener,
|
|
553
|
+
);
|
|
526
554
|
this.target.removeEventListener('keydown', this.keyDownHandler as EventListener);
|
|
527
555
|
this.target.removeEventListener('keyup', this.keyUpHandler as EventListener);
|
|
528
556
|
this.releaseAll();
|
|
@@ -544,11 +572,24 @@ export class EmulatorInput {
|
|
|
544
572
|
for (let b = 0; b < gp.buttons.length; b++) {
|
|
545
573
|
if (gp.buttons[b].pressed) {
|
|
546
574
|
this.gamepadPresses[b] = true;
|
|
547
|
-
|
|
575
|
+
console.log('Buttons Pressed', b, gp.buttons[b]);
|
|
548
576
|
switch (b) {
|
|
549
577
|
case 0:
|
|
550
578
|
this.emulator.joystickPush(this.joystickPort, JOYSTICK_FIRE_1);
|
|
551
579
|
break;
|
|
580
|
+
case 1:
|
|
581
|
+
this.emulator.joystickPush(this.joystickPort, JOYSTICK_DIRECTION.UP);
|
|
582
|
+
break;
|
|
583
|
+
case 2:
|
|
584
|
+
this.emulator.keyDown(C64_KEY.SPACE);
|
|
585
|
+
break;
|
|
586
|
+
case 3:
|
|
587
|
+
//this.pushJoystickInput(JOYSTICK_FIRE_3);
|
|
588
|
+
break;
|
|
589
|
+
case 7:
|
|
590
|
+
case 9:
|
|
591
|
+
this.emulator.keyDown(C64_KEY.RUN_STOP);
|
|
592
|
+
break;
|
|
552
593
|
case 14:
|
|
553
594
|
this.emulator.joystickPush(this.joystickPort, JOYSTICK_DIRECTION.LEFT);
|
|
554
595
|
break;
|
|
@@ -569,6 +610,20 @@ export class EmulatorInput {
|
|
|
569
610
|
case 0:
|
|
570
611
|
this.emulator.joystickRelease(this.joystickPort, JOYSTICK_FIRE_1);
|
|
571
612
|
break;
|
|
613
|
+
case 1:
|
|
614
|
+
this.emulator.joystickRelease(this.joystickPort, JOYSTICK_DIRECTION.UP);
|
|
615
|
+
break;
|
|
616
|
+
case 2:
|
|
617
|
+
this.emulator.keyUp(C64_KEY.SPACE);
|
|
618
|
+
//this.releaseJoystickInput(JOYSTICK_FIRE_2);
|
|
619
|
+
break;
|
|
620
|
+
case 3:
|
|
621
|
+
//this.releaseJoystickInput(JOYSTICK_FIRE_3);
|
|
622
|
+
break;
|
|
623
|
+
case 7:
|
|
624
|
+
case 9:
|
|
625
|
+
this.emulator.keyUp(C64_KEY.RUN_STOP);
|
|
626
|
+
break;
|
|
572
627
|
case 14:
|
|
573
628
|
this.emulator.joystickRelease(this.joystickPort, JOYSTICK_DIRECTION.LEFT);
|
|
574
629
|
break;
|
|
@@ -587,7 +642,7 @@ export class EmulatorInput {
|
|
|
587
642
|
}
|
|
588
643
|
}
|
|
589
644
|
|
|
590
|
-
if (gp.axes[0] <= -0.5) {
|
|
645
|
+
if (gp.axes[0] <= -0.5 || gp.axes[6] <= -0.5) {
|
|
591
646
|
this.gamepadAxes[0] = true;
|
|
592
647
|
this.emulator.joystickPush(this.joystickPort, JOYSTICK_DIRECTION.LEFT);
|
|
593
648
|
} else if (this.gamepadAxes[0] === true) {
|
|
@@ -595,24 +650,23 @@ export class EmulatorInput {
|
|
|
595
650
|
this.emulator.joystickRelease(this.joystickPort, JOYSTICK_DIRECTION.LEFT);
|
|
596
651
|
}
|
|
597
652
|
|
|
598
|
-
if (gp.axes[0] >= 0.5) {
|
|
653
|
+
if (gp.axes[0] >= 0.5 || gp.axes[6] >= 0.5) {
|
|
599
654
|
this.emulator.joystickPush(this.joystickPort, JOYSTICK_DIRECTION.RIGHT);
|
|
600
655
|
this.gamepadAxes[1] = true;
|
|
601
656
|
} else if (this.gamepadAxes[1] === true) {
|
|
602
657
|
this.emulator.joystickRelease(this.joystickPort, JOYSTICK_DIRECTION.RIGHT);
|
|
603
658
|
this.gamepadAxes[1] = false;
|
|
604
|
-
|
|
605
659
|
}
|
|
606
660
|
|
|
607
|
-
if (gp.axes[1] <= -0.5) {
|
|
661
|
+
if (gp.axes[1] <= -0.5 || gp.axes[7] <= -0.5) {
|
|
608
662
|
this.emulator.joystickPush(this.joystickPort, JOYSTICK_DIRECTION.UP);
|
|
609
|
-
this.gamepadAxes[2] = true
|
|
663
|
+
this.gamepadAxes[2] = true;
|
|
610
664
|
} else if (this.gamepadAxes[2] === true) {
|
|
611
665
|
this.emulator.joystickRelease(this.joystickPort, JOYSTICK_DIRECTION.UP);
|
|
612
|
-
this.gamepadAxes[2] = false
|
|
666
|
+
this.gamepadAxes[2] = false;
|
|
613
667
|
}
|
|
614
668
|
|
|
615
|
-
if (gp.axes[1] >= 0.5) {
|
|
669
|
+
if (gp.axes[1] >= 0.5 || gp.axes[7] >= 0.5) {
|
|
616
670
|
this.emulator.joystickPush(this.joystickPort, JOYSTICK_DIRECTION.DOWN);
|
|
617
671
|
this.gamepadAxes[3] = true;
|
|
618
672
|
} else if (this.gamepadAxes[3] === true) {
|
|
@@ -634,6 +688,8 @@ export class EmulatorInput {
|
|
|
634
688
|
|
|
635
689
|
const buttonToInput: Partial<Record<number, JoystickInput>> = {
|
|
636
690
|
0: JOYSTICK_FIRE_1,
|
|
691
|
+
2: JOYSTICK_FIRE_2,
|
|
692
|
+
3: JOYSTICK_FIRE_3,
|
|
637
693
|
12: JOYSTICK_DIRECTION.UP,
|
|
638
694
|
13: JOYSTICK_DIRECTION.DOWN,
|
|
639
695
|
14: JOYSTICK_DIRECTION.LEFT,
|
|
@@ -643,7 +699,7 @@ export class EmulatorInput {
|
|
|
643
699
|
for (const [button, input] of Object.entries(buttonToInput)) {
|
|
644
700
|
if (!input) continue;
|
|
645
701
|
if (this.gamepadPresses[Number(button)] === true) {
|
|
646
|
-
this.
|
|
702
|
+
this.releaseJoystickInput(input);
|
|
647
703
|
}
|
|
648
704
|
}
|
|
649
705
|
|
|
@@ -661,11 +717,7 @@ export class EmulatorInput {
|
|
|
661
717
|
}
|
|
662
718
|
|
|
663
719
|
private handleGamepadDisconnected(event: GamepadEvent): void {
|
|
664
|
-
console.log(
|
|
665
|
-
"Gamepad disconnected from index %d: %s",
|
|
666
|
-
event.gamepad.index,
|
|
667
|
-
event.gamepad.id,
|
|
668
|
-
);
|
|
720
|
+
console.log('Gamepad disconnected from index %d: %s', event.gamepad.index, event.gamepad.id);
|
|
669
721
|
const wasActive = this.gamepadIndex === event.gamepad.index;
|
|
670
722
|
if (wasActive) {
|
|
671
723
|
this.releaseAllGamepadButtons();
|
|
@@ -684,14 +736,14 @@ export class EmulatorInput {
|
|
|
684
736
|
detail: {
|
|
685
737
|
name: event.gamepad.id,
|
|
686
738
|
index: event.gamepad.index,
|
|
687
|
-
}
|
|
739
|
+
},
|
|
688
740
|
});
|
|
689
741
|
this.target.dispatchEvent(gamepadDisconnected);
|
|
690
742
|
}
|
|
691
743
|
|
|
692
744
|
private handleGamepadConnected(event: GamepadEvent): void {
|
|
693
745
|
console.log(
|
|
694
|
-
|
|
746
|
+
'Gamepad connected at index %d: %s. %d buttons, %d axes.',
|
|
695
747
|
event.gamepad.index,
|
|
696
748
|
event.gamepad.id,
|
|
697
749
|
event.gamepad.buttons.length,
|
|
@@ -704,7 +756,7 @@ export class EmulatorInput {
|
|
|
704
756
|
detail: {
|
|
705
757
|
name: event.gamepad.id,
|
|
706
758
|
index: event.gamepad.index,
|
|
707
|
-
}
|
|
759
|
+
},
|
|
708
760
|
});
|
|
709
761
|
|
|
710
762
|
this.target.dispatchEvent(gamepadConnected);
|
|
@@ -733,10 +785,7 @@ export class EmulatorInput {
|
|
|
733
785
|
if (event.code in MIXED_JOYSTICK_KEYS) {
|
|
734
786
|
// Joystick path
|
|
735
787
|
const dir = MIXED_JOYSTICK_KEYS[event.code] as JoystickInput;
|
|
736
|
-
this.
|
|
737
|
-
this.joystickPort,
|
|
738
|
-
dir,
|
|
739
|
-
);
|
|
788
|
+
this.pushJoystickInput(dir);
|
|
740
789
|
event.preventDefault();
|
|
741
790
|
} else {
|
|
742
791
|
// Keyboard path — but exclude the joystick codes from matrix so
|
|
@@ -759,7 +808,7 @@ export class EmulatorInput {
|
|
|
759
808
|
return;
|
|
760
809
|
}
|
|
761
810
|
this.pressedControls.add(control);
|
|
762
|
-
this.
|
|
811
|
+
this.pushJoystickInput(KEY_TO_JOYSTICK[control]);
|
|
763
812
|
event.preventDefault();
|
|
764
813
|
}
|
|
765
814
|
|
|
@@ -779,10 +828,7 @@ export class EmulatorInput {
|
|
|
779
828
|
if (this.inputMode === 'mixed') {
|
|
780
829
|
if (event.code in MIXED_JOYSTICK_KEYS) {
|
|
781
830
|
const dir = MIXED_JOYSTICK_KEYS[event.code];
|
|
782
|
-
this.
|
|
783
|
-
this.joystickPort,
|
|
784
|
-
dir as JoystickInput,
|
|
785
|
-
);
|
|
831
|
+
this.releaseJoystickInput(dir as JoystickInput);
|
|
786
832
|
event.preventDefault();
|
|
787
833
|
} else {
|
|
788
834
|
const acts = domKeyToC64Actions(event.key, event.shiftKey, 'keyup');
|
|
@@ -803,7 +849,7 @@ export class EmulatorInput {
|
|
|
803
849
|
return;
|
|
804
850
|
}
|
|
805
851
|
this.pressedControls.delete(control);
|
|
806
|
-
this.
|
|
852
|
+
this.releaseJoystickInput(KEY_TO_JOYSTICK[control]);
|
|
807
853
|
event.preventDefault();
|
|
808
854
|
}
|
|
809
855
|
|
|
@@ -814,11 +860,57 @@ export class EmulatorInput {
|
|
|
814
860
|
return null;
|
|
815
861
|
}
|
|
816
862
|
|
|
817
|
-
private
|
|
863
|
+
private pushJoystickInput(input: JoystickInput): void {
|
|
864
|
+
if (input === JOYSTICK_FIRE_2) {
|
|
865
|
+
this.potFire2Pressed = true;
|
|
866
|
+
this.syncPotFireButtons();
|
|
867
|
+
return;
|
|
868
|
+
}
|
|
869
|
+
if (input === JOYSTICK_FIRE_3) {
|
|
870
|
+
this.potFire3Pressed = true;
|
|
871
|
+
this.syncPotFireButtons();
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
this.emulator.joystickPush(this.joystickPort, input);
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
private releaseJoystickInput(input: JoystickInput): void {
|
|
879
|
+
if (input === JOYSTICK_FIRE_2) {
|
|
880
|
+
this.potFire2Pressed = false;
|
|
881
|
+
this.syncPotFireButtons();
|
|
882
|
+
return;
|
|
883
|
+
}
|
|
884
|
+
if (input === JOYSTICK_FIRE_3) {
|
|
885
|
+
this.potFire3Pressed = false;
|
|
886
|
+
this.syncPotFireButtons();
|
|
887
|
+
return;
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
this.emulator.joystickRelease(this.joystickPort, input);
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
private releaseAll(): void {
|
|
818
894
|
for (const control of this.pressedControls) {
|
|
819
|
-
this.
|
|
895
|
+
this.releaseJoystickInput(KEY_TO_JOYSTICK[control]);
|
|
820
896
|
}
|
|
821
897
|
this.pressedControls.clear();
|
|
898
|
+
this.potFire2Pressed = false;
|
|
899
|
+
this.potFire3Pressed = false;
|
|
900
|
+
this.syncPotFireButtons();
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
private syncPotFireButtons(): void {
|
|
904
|
+
if (!this.potFire2Pressed && !this.potFire3Pressed) {
|
|
905
|
+
this.emulator.setMousePortEnabled?.(this.joystickPort, false);
|
|
906
|
+
return;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
this.emulator.setMousePortEnabled?.(this.joystickPort, true);
|
|
910
|
+
this.emulator.mousePosition?.(
|
|
911
|
+
this.potFire2Pressed ? POT_ACTIVE : POT_INACTIVE,
|
|
912
|
+
this.potFire3Pressed ? POT_ACTIVE : POT_INACTIVE,
|
|
913
|
+
);
|
|
822
914
|
}
|
|
823
915
|
|
|
824
916
|
/** Set which joystick port keyboard controls should target (1 or 2). */
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
22
|
import http from 'http';
|
|
23
|
+
import crypto from 'crypto';
|
|
23
24
|
import { WebSocketServer } from 'ws';
|
|
24
25
|
import wrtc from '@roamhq/wrtc';
|
|
25
26
|
|
|
@@ -32,15 +33,51 @@ function parseCsvList(raw) {
|
|
|
32
33
|
.filter(Boolean);
|
|
33
34
|
}
|
|
34
35
|
|
|
36
|
+
function normalizeTurnTtlSeconds(rawTtl, fallback = 24 * 3600) {
|
|
37
|
+
const parsed = Number.parseInt(String(rawTtl ?? ''), 10);
|
|
38
|
+
if (Number.isFinite(parsed) && parsed > 0) return parsed;
|
|
39
|
+
return fallback;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function buildTimedTurnCredentials({
|
|
43
|
+
secret,
|
|
44
|
+
ttlSeconds = 24 * 3600,
|
|
45
|
+
usernameLabel = 'c64live',
|
|
46
|
+
nowSeconds = Math.floor(Date.now() / 1000),
|
|
47
|
+
} = {}) {
|
|
48
|
+
const safeSecret = String(secret ?? '').trim();
|
|
49
|
+
if (!safeSecret) throw new Error('TURN secret is required');
|
|
50
|
+
|
|
51
|
+
const safeLabel = String(usernameLabel ?? '').trim() || 'c64live';
|
|
52
|
+
const expiresAt = Math.floor(nowSeconds) + normalizeTurnTtlSeconds(ttlSeconds);
|
|
53
|
+
const username = `${expiresAt}:${safeLabel}`;
|
|
54
|
+
const credential = crypto
|
|
55
|
+
.createHmac('sha1', safeSecret)
|
|
56
|
+
.update(username)
|
|
57
|
+
.digest('base64');
|
|
58
|
+
|
|
59
|
+
return { username, credential };
|
|
60
|
+
}
|
|
61
|
+
|
|
35
62
|
function buildIceServers({
|
|
36
63
|
iceStunUrls,
|
|
37
64
|
iceTurnUrls,
|
|
65
|
+
iceTurnSecret,
|
|
66
|
+
iceTurnTtlSeconds,
|
|
67
|
+
iceTurnUsernameLabel,
|
|
38
68
|
iceTurnUsername,
|
|
39
69
|
iceTurnPassword,
|
|
40
70
|
verbose = false,
|
|
41
71
|
} = {}) {
|
|
42
72
|
const stunUrls = parseCsvList(iceStunUrls ?? process.env.ICE_STUN_URLS);
|
|
43
73
|
const turnUrls = parseCsvList(iceTurnUrls ?? process.env.ICE_TURN_URLS);
|
|
74
|
+
const turnSecret = String(iceTurnSecret ?? process.env.ICE_TURN_SECRET ?? '').trim();
|
|
75
|
+
const turnTtlSeconds = normalizeTurnTtlSeconds(
|
|
76
|
+
iceTurnTtlSeconds ?? process.env.ICE_TURN_TTL_SECONDS,
|
|
77
|
+
);
|
|
78
|
+
const turnUsernameLabel = String(
|
|
79
|
+
iceTurnUsernameLabel ?? process.env.ICE_TURN_USERNAME_LABEL ?? 'c64live',
|
|
80
|
+
).trim();
|
|
44
81
|
const turnUsername = String(iceTurnUsername ?? process.env.ICE_TURN_USERNAME ?? '').trim();
|
|
45
82
|
const turnPassword = String(iceTurnPassword ?? process.env.ICE_TURN_PASSWORD ?? '').trim();
|
|
46
83
|
|
|
@@ -49,7 +86,18 @@ function buildIceServers({
|
|
|
49
86
|
iceServers.push({ urls: stunUrls });
|
|
50
87
|
}
|
|
51
88
|
|
|
52
|
-
if (turnUrls.length > 0 &&
|
|
89
|
+
if (turnUrls.length > 0 && turnSecret) {
|
|
90
|
+
const creds = buildTimedTurnCredentials({
|
|
91
|
+
secret: turnSecret,
|
|
92
|
+
ttlSeconds: turnTtlSeconds,
|
|
93
|
+
usernameLabel: turnUsernameLabel,
|
|
94
|
+
});
|
|
95
|
+
iceServers.push({
|
|
96
|
+
urls: turnUrls,
|
|
97
|
+
username: creds.username,
|
|
98
|
+
credential: creds.credential,
|
|
99
|
+
});
|
|
100
|
+
} else if (turnUrls.length > 0 && turnUsername && turnPassword) {
|
|
53
101
|
iceServers.push({
|
|
54
102
|
urls: turnUrls,
|
|
55
103
|
username: turnUsername,
|
|
@@ -57,7 +105,7 @@ function buildIceServers({
|
|
|
57
105
|
});
|
|
58
106
|
} else if (turnUrls.length > 0 && verbose) {
|
|
59
107
|
console.error(
|
|
60
|
-
'[webrtc] ICE_TURN_URLS provided without ICE_TURN_USERNAME/ICE_TURN_PASSWORD; skipping TURN',
|
|
108
|
+
'[webrtc] ICE_TURN_URLS provided without ICE_TURN_SECRET or ICE_TURN_USERNAME/ICE_TURN_PASSWORD; skipping TURN',
|
|
61
109
|
);
|
|
62
110
|
}
|
|
63
111
|
|
|
@@ -68,9 +116,13 @@ function buildIceServers({
|
|
|
68
116
|
if (verbose) {
|
|
69
117
|
const stunCount = stunUrls.length;
|
|
70
118
|
const turnCount = turnUrls.length;
|
|
71
|
-
const
|
|
119
|
+
const turnMode = turnUrls.length > 0 && turnSecret
|
|
120
|
+
? 'timed'
|
|
121
|
+
: turnUrls.length > 0 && turnUsername && turnPassword
|
|
122
|
+
? 'static'
|
|
123
|
+
: 'disabled';
|
|
72
124
|
console.error(
|
|
73
|
-
`[webrtc] ICE config stunUrls=${stunCount} turnUrls=${turnCount}
|
|
125
|
+
`[webrtc] ICE config stunUrls=${stunCount} turnUrls=${turnCount} turnMode=${turnMode}`,
|
|
74
126
|
);
|
|
75
127
|
}
|
|
76
128
|
|
|
@@ -92,6 +144,9 @@ function buildIceServers({
|
|
|
92
144
|
* @param {number} [opts.maxBitrateKbps=600] SDP x-google-max-bitrate for VP8 answer
|
|
93
145
|
* @param {string|string[]} [opts.iceStunUrls] Optional STUN URL list
|
|
94
146
|
* @param {string|string[]} [opts.iceTurnUrls] Optional TURN URL list
|
|
147
|
+
* @param {string} [opts.iceTurnSecret] Optional TURN shared secret for timed creds
|
|
148
|
+
* @param {number} [opts.iceTurnTtlSeconds] Optional TURN credential TTL in seconds
|
|
149
|
+
* @param {string} [opts.iceTurnUsernameLabel] Optional TURN username suffix for timed creds
|
|
95
150
|
* @param {string} [opts.iceTurnUsername] Optional TURN username
|
|
96
151
|
* @param {string} [opts.iceTurnPassword] Optional TURN credential/password
|
|
97
152
|
* @param {(pc: RTCPeerConnection) => void} opts.onOffer
|
|
@@ -113,20 +168,28 @@ export function createWebRTCServer({
|
|
|
113
168
|
maxBitrateKbps = 600,
|
|
114
169
|
iceStunUrls,
|
|
115
170
|
iceTurnUrls,
|
|
171
|
+
iceTurnSecret,
|
|
172
|
+
iceTurnTtlSeconds,
|
|
173
|
+
iceTurnUsernameLabel,
|
|
116
174
|
iceTurnUsername,
|
|
117
175
|
iceTurnPassword,
|
|
118
176
|
onOffer,
|
|
119
177
|
onPeerConnected,
|
|
120
178
|
onDataChannelInput,
|
|
121
179
|
} = {}) {
|
|
122
|
-
const
|
|
180
|
+
const buildRuntimeIceServers = () => buildIceServers({
|
|
123
181
|
iceStunUrls,
|
|
124
182
|
iceTurnUrls,
|
|
183
|
+
iceTurnSecret,
|
|
184
|
+
iceTurnTtlSeconds,
|
|
185
|
+
iceTurnUsernameLabel,
|
|
125
186
|
iceTurnUsername,
|
|
126
187
|
iceTurnPassword,
|
|
127
188
|
verbose,
|
|
128
189
|
});
|
|
129
190
|
|
|
191
|
+
const iceServers = buildRuntimeIceServers();
|
|
192
|
+
|
|
130
193
|
/** Emit a structured [event] line — same format as input-server.mjs. */
|
|
131
194
|
function logEv(tag, fields = {}) {
|
|
132
195
|
if (!logEvents) return;
|
|
@@ -468,11 +531,13 @@ export function createWebRTCServer({
|
|
|
468
531
|
|
|
469
532
|
const httpServer = http.createServer((req, res) => {
|
|
470
533
|
if (req.method === 'GET' && (req.url === '/' || req.url === '/index.html')) {
|
|
534
|
+
const requestIceServers = buildRuntimeIceServers();
|
|
471
535
|
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
472
|
-
res.end(buildBrowserHtml(inputPort, minBitrateKbpsSafe, maxBitrateKbpsSafe,
|
|
536
|
+
res.end(buildBrowserHtml(inputPort, minBitrateKbpsSafe, maxBitrateKbpsSafe, requestIceServers));
|
|
473
537
|
} else if (req.method === 'GET' && req.url === '/ice-config') {
|
|
538
|
+
const requestIceServers = buildRuntimeIceServers();
|
|
474
539
|
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
475
|
-
res.end(JSON.stringify({ iceServers }));
|
|
540
|
+
res.end(JSON.stringify({ iceServers: requestIceServers }));
|
|
476
541
|
} else if (req.url === '/favicon.ico') {
|
|
477
542
|
// Return a minimal 1×1 transparent ICO so browsers don't log a 404
|
|
478
543
|
res.writeHead(204);
|
|
@@ -554,7 +619,7 @@ export function createWebRTCServer({
|
|
|
554
619
|
logEv('webrtc-peer-connected', { addr: remoteAddr });
|
|
555
620
|
logLoadSnapshot('webrtc-load-change', { reason: 'peer-connected' });
|
|
556
621
|
|
|
557
|
-
const pc = new RTCPeerConnection({ iceServers });
|
|
622
|
+
const pc = new RTCPeerConnection({ iceServers: buildRuntimeIceServers() });
|
|
558
623
|
const controller = {
|
|
559
624
|
pc,
|
|
560
625
|
ws,
|