warpr 1.0.0 → 1.0.9

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.
@@ -1,150 +1,150 @@
1
- import { Injectable } from '@angular/core';
2
- import { MessagingService } from './messaging.service';
3
- import { PeerConnectionCandidateMessage, PeerConnectionDescriptionMessage, WarprSignalingMessage, WarprSignalingMessageType } from '../data/signaling-messages';
4
- import { IMessagingClient } from '../networking/messaging-client';
5
- import { EncodedFrame, FrameType } from '../data/frames';
6
- import { EventOwner, EventPublisher } from '../infrastructure/events';
7
- import { MessageAssembler } from '../infrastructure/message-assembler';
8
-
9
- @Injectable({
10
- providedIn: 'root'
11
- })
12
- export class StreamingService {
13
-
14
- private _peerConnection?: RTCPeerConnection;
15
- private _reliableConnection?: RTCDataChannel;
16
- private _lowLatencyConnection?: RTCDataChannel;
17
- private _messageBuilder? : MessageAssembler;
18
-
19
- private readonly _events = new EventOwner();
20
- public readonly FrameReceived = new EventPublisher<StreamingService, EncodedFrame>(this._events);
21
- public readonly Connected = new EventPublisher<StreamingService, any>(this._events);
22
-
23
- private static _config: RTCConfiguration = {
24
- iceServers: [
25
- { urls: "stun:stun.l.google.com:19302" },
26
- { urls: "stun:stun.relay.metered.ca:80" },
27
- {
28
- urls: "turn:global.relay.metered.ca:80",
29
- username: "cb51ef701a61be3cdc89466f",
30
- credential: "BBnGCvnTLaxhVbuf"
31
- },
32
- {
33
- urls: "turn:global.relay.metered.ca:80?transport=tcp",
34
- username: "cb51ef701a61be3cdc89466f",
35
- credential: "BBnGCvnTLaxhVbuf"
36
- },
37
- {
38
- urls: "turn:global.relay.metered.ca:443",
39
- username: "cb51ef701a61be3cdc89466f",
40
- credential: "BBnGCvnTLaxhVbuf"
41
- },
42
- {
43
- urls: "turns:global.relay.metered.ca:443?transport=tcp",
44
- username: "cb51ef701a61be3cdc89466f",
45
- credential: "BBnGCvnTLaxhVbuf"
46
- }
47
- ]
48
- };
49
-
50
- constructor(
51
- private _messagingService: MessagingService) {
52
-
53
- _messagingService.MessageReceived.Subscribe((sender: IMessagingClient<WarprSignalingMessage>, message: WarprSignalingMessage) => this.OnMessageReceived(sender, message));
54
-
55
- this.Reconnect();
56
- }
57
-
58
- private Reconnect() {
59
- this._reliableConnection?.close();
60
- this._lowLatencyConnection?.close();
61
- this._peerConnection?.close();
62
- this._messageBuilder = new MessageAssembler();
63
-
64
- this._peerConnection = new RTCPeerConnection(StreamingService._config);
65
- this._peerConnection.onicecandidate = (event) => this.OnIceCandidateAdded(event.candidate?.candidate);
66
- this._peerConnection.ondatachannel = (event) => this.OnDataChannel(event);
67
- this._peerConnection.onconnectionstatechange = (event) => this.OnConnectionStateChanged();
68
- }
69
-
70
- public SendMessage(message: any) {
71
- let json = JSON.stringify(message);
72
- this._reliableConnection?.send(json);
73
- }
74
-
75
- private OnConnectionStateChanged() {
76
- console.log("WebRTC: " + this._peerConnection?.connectionState);
77
- }
78
-
79
- private OnDataChannel(event: RTCDataChannelEvent) {
80
- console.log(`WebRTC data channel ${event.channel.label} connected.`);
81
-
82
- switch (event.channel.label) {
83
- case "reliable":
84
- this._reliableConnection = event.channel;
85
- this._events.Raise(this.Connected, this, null);
86
- break;
87
- case "low_latency":
88
- this._lowLatencyConnection = event.channel;
89
- this._lowLatencyConnection.binaryType = "arraybuffer"
90
- this._lowLatencyConnection.onmessage = (event) => this.OnLowLatencyMessage(event);
91
- break;
92
- }
93
- }
94
-
95
- private _lastRefreshTime = performance.now();
96
- private _count = 0;
97
-
98
- private OnLowLatencyMessage(event: MessageEvent<any>) {
99
-
100
- let message = this._messageBuilder?.PushMessage(event.data as ArrayBuffer);
101
- if (!message) return;
102
-
103
- let now = performance.now();
104
- let elapsed = now - this._lastRefreshTime;
105
- this._count++;
106
-
107
- if (elapsed > 1000) {
108
- this._lastRefreshTime = now;
109
-
110
- console.log(`Messages: ${this._count / elapsed * 1000} FPS`);
111
- this._count = 0;
112
- }
113
-
114
- let frame = new EncodedFrame(message);
115
- this._events.Raise(this.FrameReceived, this, frame);
116
- }
117
-
118
- private OnIceCandidateAdded(candidate?: string) {
119
- if (candidate == null) return;
120
-
121
- let message = new PeerConnectionCandidateMessage();
122
- message.Candidate = candidate;
123
- this._messagingService.SendMessage(message);
124
- }
125
-
126
- private async OnMessageReceived(sender: IMessagingClient<WarprSignalingMessage>, message: WarprSignalingMessage) {
127
-
128
- if (!this._peerConnection) return;
129
-
130
- switch (message.$type) {
131
- case WarprSignalingMessageType.PairingCompleteMessage:
132
- this.Reconnect();
133
- break;
134
- case WarprSignalingMessageType.PeerConnectionDescriptionMessage:
135
- await this._peerConnection.setRemoteDescription({ type: "offer", sdp: message.Description });
136
- let answer = await this._peerConnection.createAnswer();
137
- await this._peerConnection.setLocalDescription(answer);
138
-
139
- let answerMessage = new PeerConnectionDescriptionMessage();
140
- answerMessage.Description = answer.sdp;
141
- this._messagingService.SendMessage(answerMessage);
142
-
143
- break;
144
- case WarprSignalingMessageType.PeerConnectionCandidateMessage:
145
- await this._peerConnection.addIceCandidate({ candidate: message.Candidate, sdpMid: "0" });
146
- break;
147
- }
148
-
149
- }
150
- }
1
+ import { Injectable } from '@angular/core';
2
+ import { MessagingService } from './messaging.service';
3
+ import { PeerConnectionCandidateMessage, PeerConnectionDescriptionMessage, WarprSignalingMessage, WarprSignalingMessageType } from '../data/signaling-messages';
4
+ import { IMessagingClient } from '../networking/messaging-client';
5
+ import { EncodedFrame, FrameType } from '../data/frames';
6
+ import { EventOwner, EventPublisher } from '../infrastructure/events';
7
+ import { MessageAssembler } from '../infrastructure/message-assembler';
8
+
9
+ @Injectable({
10
+ providedIn: 'root'
11
+ })
12
+ export class StreamingService {
13
+
14
+ private _peerConnection?: RTCPeerConnection;
15
+ private _reliableConnection?: RTCDataChannel;
16
+ private _lowLatencyConnection?: RTCDataChannel;
17
+ private _messageBuilder? : MessageAssembler;
18
+
19
+ private readonly _events = new EventOwner();
20
+ public readonly FrameReceived = new EventPublisher<StreamingService, EncodedFrame>(this._events);
21
+ public readonly Connected = new EventPublisher<StreamingService, any>(this._events);
22
+
23
+ private static _config: RTCConfiguration = {
24
+ iceServers: [
25
+ { urls: "stun:stun.l.google.com:19302" },
26
+ { urls: "stun:stun.relay.metered.ca:80" },
27
+ {
28
+ urls: "turn:global.relay.metered.ca:80",
29
+ username: "cb51ef701a61be3cdc89466f",
30
+ credential: "BBnGCvnTLaxhVbuf"
31
+ },
32
+ {
33
+ urls: "turn:global.relay.metered.ca:80?transport=tcp",
34
+ username: "cb51ef701a61be3cdc89466f",
35
+ credential: "BBnGCvnTLaxhVbuf"
36
+ },
37
+ {
38
+ urls: "turn:global.relay.metered.ca:443",
39
+ username: "cb51ef701a61be3cdc89466f",
40
+ credential: "BBnGCvnTLaxhVbuf"
41
+ },
42
+ {
43
+ urls: "turns:global.relay.metered.ca:443?transport=tcp",
44
+ username: "cb51ef701a61be3cdc89466f",
45
+ credential: "BBnGCvnTLaxhVbuf"
46
+ }
47
+ ]
48
+ };
49
+
50
+ constructor(
51
+ private _messagingService: MessagingService) {
52
+
53
+ _messagingService.MessageReceived.Subscribe((sender: IMessagingClient<WarprSignalingMessage>, message: WarprSignalingMessage) => this.OnMessageReceived(sender, message));
54
+
55
+ this.Reconnect();
56
+ }
57
+
58
+ private Reconnect() {
59
+ this._reliableConnection?.close();
60
+ this._lowLatencyConnection?.close();
61
+ this._peerConnection?.close();
62
+ this._messageBuilder = new MessageAssembler();
63
+
64
+ this._peerConnection = new RTCPeerConnection(StreamingService._config);
65
+ this._peerConnection.onicecandidate = (event) => this.OnIceCandidateAdded(event.candidate?.candidate);
66
+ this._peerConnection.ondatachannel = (event) => this.OnDataChannel(event);
67
+ this._peerConnection.onconnectionstatechange = (event) => this.OnConnectionStateChanged();
68
+ }
69
+
70
+ public SendMessage(message: any) {
71
+ let json = JSON.stringify(message);
72
+ this._reliableConnection?.send(json);
73
+ }
74
+
75
+ private OnConnectionStateChanged() {
76
+ console.log("WebRTC: " + this._peerConnection?.connectionState);
77
+ }
78
+
79
+ private OnDataChannel(event: RTCDataChannelEvent) {
80
+ console.log(`WebRTC data channel ${event.channel.label} connected.`);
81
+
82
+ switch (event.channel.label) {
83
+ case "reliable":
84
+ this._reliableConnection = event.channel;
85
+ this._events.Raise(this.Connected, this, null);
86
+ break;
87
+ case "low_latency":
88
+ this._lowLatencyConnection = event.channel;
89
+ this._lowLatencyConnection.binaryType = "arraybuffer"
90
+ this._lowLatencyConnection.onmessage = (event) => this.OnLowLatencyMessage(event);
91
+ break;
92
+ }
93
+ }
94
+
95
+ private _lastRefreshTime = performance.now();
96
+ private _count = 0;
97
+
98
+ private OnLowLatencyMessage(event: MessageEvent<any>) {
99
+
100
+ let message = this._messageBuilder?.PushMessage(event.data as ArrayBuffer);
101
+ if (!message) return;
102
+
103
+ let now = performance.now();
104
+ let elapsed = now - this._lastRefreshTime;
105
+ this._count++;
106
+
107
+ if (elapsed > 1000) {
108
+ this._lastRefreshTime = now;
109
+
110
+ console.log(`Messages: ${this._count / elapsed * 1000} FPS`);
111
+ this._count = 0;
112
+ }
113
+
114
+ let frame = new EncodedFrame(message);
115
+ this._events.Raise(this.FrameReceived, this, frame);
116
+ }
117
+
118
+ private OnIceCandidateAdded(candidate?: string) {
119
+ if (candidate == null) return;
120
+
121
+ let message = new PeerConnectionCandidateMessage();
122
+ message.Candidate = candidate;
123
+ this._messagingService.SendMessage(message);
124
+ }
125
+
126
+ private async OnMessageReceived(sender: IMessagingClient<WarprSignalingMessage>, message: WarprSignalingMessage) {
127
+
128
+ if (!this._peerConnection) return;
129
+
130
+ switch (message.$type) {
131
+ case WarprSignalingMessageType.PairingCompleteMessage:
132
+ this.Reconnect();
133
+ break;
134
+ case WarprSignalingMessageType.PeerConnectionDescriptionMessage:
135
+ await this._peerConnection.setRemoteDescription({ type: "offer", sdp: message.Description });
136
+ let answer = await this._peerConnection.createAnswer();
137
+ await this._peerConnection.setLocalDescription(answer);
138
+
139
+ let answerMessage = new PeerConnectionDescriptionMessage();
140
+ answerMessage.Description = answer.sdp;
141
+ this._messagingService.SendMessage(answerMessage);
142
+
143
+ break;
144
+ case WarprSignalingMessageType.PeerConnectionCandidateMessage:
145
+ await this._peerConnection.addIceCandidate({ candidate: message.Candidate, sdpMid: "0" });
146
+ break;
147
+ }
148
+
149
+ }
150
+ }
package/src/public-api.ts CHANGED
@@ -1,15 +1,15 @@
1
- /*
2
- * Public API Surface of warpr-lib
3
- */
4
-
5
- export * from './lib/data/frames';
6
- export * from './lib/data/signaling-messages';
7
- export * from './lib/data/streaming-messages';
8
- export * from './lib/infrastructure/array-stream';
9
- export * from './lib/infrastructure/events';
10
- export * from './lib/infrastructure/message-assembler';
11
- export * from './lib/networking/messaging-client';
12
- export * from './lib/networking/web-socket-client';
13
- export * from './lib/services/messaging.service';
14
- export * from './lib/services/streaming.service';
15
- export * from './lib/components/stream-host/stream-host.component';
1
+ /*
2
+ * Public API Surface of warpr-lib
3
+ */
4
+
5
+ export * from './lib/data/frames';
6
+ export * from './lib/data/signaling-messages';
7
+ export * from './lib/data/streaming-messages';
8
+ export * from './lib/infrastructure/array-stream';
9
+ export * from './lib/infrastructure/events';
10
+ export * from './lib/infrastructure/message-assembler';
11
+ export * from './lib/networking/messaging-client';
12
+ export * from './lib/networking/web-socket-client';
13
+ export * from './lib/services/messaging.service';
14
+ export * from './lib/services/streaming.service';
15
+ export * from './lib/components/stream-host/stream-host.component';
package/tsconfig.lib.json CHANGED
@@ -1,14 +1,14 @@
1
- /* To learn more about this file see: https://angular.io/config/tsconfig. */
2
- {
3
- "extends": "../../tsconfig.json",
4
- "compilerOptions": {
5
- "outDir": "../../out-tsc/lib",
6
- "declaration": true,
7
- "declarationMap": true,
8
- "inlineSources": true,
9
- "types": []
10
- },
11
- "exclude": [
12
- "**/*.spec.ts"
13
- ]
14
- }
1
+ /* To learn more about this file see: https://angular.io/config/tsconfig. */
2
+ {
3
+ "extends": "../../tsconfig.json",
4
+ "compilerOptions": {
5
+ "outDir": "../../out-tsc/lib",
6
+ "declaration": true,
7
+ "declarationMap": true,
8
+ "inlineSources": true,
9
+ "types": []
10
+ },
11
+ "exclude": [
12
+ "**/*.spec.ts"
13
+ ]
14
+ }
@@ -1,10 +1,10 @@
1
- /* To learn more about this file see: https://angular.io/config/tsconfig. */
2
- {
3
- "extends": "./tsconfig.lib.json",
4
- "compilerOptions": {
5
- "declarationMap": false
6
- },
7
- "angularCompilerOptions": {
8
- "compilationMode": "partial"
9
- }
10
- }
1
+ /* To learn more about this file see: https://angular.io/config/tsconfig. */
2
+ {
3
+ "extends": "./tsconfig.lib.json",
4
+ "compilerOptions": {
5
+ "declarationMap": false
6
+ },
7
+ "angularCompilerOptions": {
8
+ "compilationMode": "partial"
9
+ }
10
+ }
@@ -1,14 +1,14 @@
1
- /* To learn more about this file see: https://angular.io/config/tsconfig. */
2
- {
3
- "extends": "../../tsconfig.json",
4
- "compilerOptions": {
5
- "outDir": "../../out-tsc/spec",
6
- "types": [
7
- "jasmine"
8
- ]
9
- },
10
- "include": [
11
- "**/*.spec.ts",
12
- "**/*.d.ts"
13
- ]
14
- }
1
+ /* To learn more about this file see: https://angular.io/config/tsconfig. */
2
+ {
3
+ "extends": "../../tsconfig.json",
4
+ "compilerOptions": {
5
+ "outDir": "../../out-tsc/spec",
6
+ "types": [
7
+ "jasmine"
8
+ ]
9
+ },
10
+ "include": [
11
+ "**/*.spec.ts",
12
+ "**/*.d.ts"
13
+ ]
14
+ }
Binary file
package/warpr-1.0.0.0.tgz DELETED
Binary file
package/warpr-1.0.0.tgz DELETED
Binary file