@ajna-inc/webrtc 0.1.0 → 0.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/LICENSE +202 -0
- package/README.md +143 -17
- package/build/index.d.ts +6 -2
- package/build/index.js +5 -1
- package/build/index.js.map +1 -1
- package/build/webrtc/WebRTCApi.d.ts +94 -5
- package/build/webrtc/WebRTCApi.js +107 -4
- package/build/webrtc/WebRTCApi.js.map +1 -1
- package/build/webrtc/WebRTCEvents.d.ts +87 -5
- package/build/webrtc/WebRTCEvents.js +8 -0
- package/build/webrtc/WebRTCEvents.js.map +1 -1
- package/build/webrtc/WebRTCModule.d.ts +50 -0
- package/build/webrtc/WebRTCModule.js +17 -2
- package/build/webrtc/WebRTCModule.js.map +1 -1
- package/build/webrtc/handlers/RenegotiateHandler.d.ts +9 -0
- package/build/webrtc/handlers/RenegotiateHandler.js +31 -0
- package/build/webrtc/handlers/RenegotiateHandler.js.map +1 -0
- package/build/webrtc/handlers/index.d.ts +1 -0
- package/build/webrtc/handlers/index.js +1 -0
- package/build/webrtc/handlers/index.js.map +1 -1
- package/build/webrtc/messages/AnswerMessage.d.ts +9 -0
- package/build/webrtc/messages/AnswerMessage.js +11 -1
- package/build/webrtc/messages/AnswerMessage.js.map +1 -1
- package/build/webrtc/messages/EndMessage.d.ts +1 -0
- package/build/webrtc/messages/EndMessage.js +1 -1
- package/build/webrtc/messages/EndMessage.js.map +1 -1
- package/build/webrtc/messages/IceMessage.d.ts +1 -0
- package/build/webrtc/messages/IceMessage.js +1 -1
- package/build/webrtc/messages/IceMessage.js.map +1 -1
- package/build/webrtc/messages/OfferMessage.d.ts +29 -1
- package/build/webrtc/messages/OfferMessage.js +37 -2
- package/build/webrtc/messages/OfferMessage.js.map +1 -1
- package/build/webrtc/messages/ProposeMessage.d.ts +51 -0
- package/build/webrtc/messages/ProposeMessage.js +48 -2
- package/build/webrtc/messages/ProposeMessage.js.map +1 -1
- package/build/webrtc/messages/RenegotiateMessage.d.ts +52 -0
- package/build/webrtc/messages/RenegotiateMessage.js +65 -0
- package/build/webrtc/messages/RenegotiateMessage.js.map +1 -0
- package/build/webrtc/messages/index.d.ts +1 -0
- package/build/webrtc/messages/index.js +1 -0
- package/build/webrtc/messages/index.js.map +1 -1
- package/build/webrtc/services/WebRTCService.d.ts +66 -5
- package/build/webrtc/services/WebRTCService.js +123 -15
- package/build/webrtc/services/WebRTCService.js.map +1 -1
- package/package.json +6 -7
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2020-present Hyperledger Contributors.
|
|
190
|
+
Copyright 2021 Queen’s Printer for Ontario. Mostafa Youssef (https://github.com/MosCD3), Amit Padmani (https://github.com/nbAmit), Prasad Katkar (https://github.com/NB-PrasadKatkar), Mike Richardson (https://github.com/NB-MikeRichardson)
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,38 +1,95 @@
|
|
|
1
|
-
# @
|
|
1
|
+
# @ajna-inc/webrtc
|
|
2
2
|
|
|
3
|
-
DIDComm v2 signaling protocol for WebRTC P2P calls.
|
|
3
|
+
DIDComm v2 signaling protocol for WebRTC P2P calls with full NAT/firewall traversal support.
|
|
4
4
|
|
|
5
5
|
- PIURI: `https://didcomm.org/webrtc/1.0`
|
|
6
|
-
- Messages: `propose`, `offer`, `answer`, `ice`, `end`
|
|
6
|
+
- Messages: `propose`, `offer`, `answer`, `ice`, `renegotiate`, `end`
|
|
7
7
|
- Advertises support via Discover Features; works with mediators and message pickup.
|
|
8
8
|
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Module-level ICE server defaults** - Configure STUN/TURN servers once
|
|
12
|
+
- **ICE policy control** - `all`, `relay-preferred`, or `relay-only` for strict firewall environments
|
|
13
|
+
- **ICE restart support** - Recover from failed connections
|
|
14
|
+
- **Trickle ICE** - Efficient candidate exchange
|
|
15
|
+
- **Propose message** - Share ICE servers before SDP exchange
|
|
16
|
+
|
|
9
17
|
## Install
|
|
10
18
|
|
|
11
19
|
This package is part of the monorepo. Add the module to your Agent options:
|
|
12
20
|
|
|
13
21
|
```ts
|
|
14
|
-
import { WebRTCModule } from '@
|
|
22
|
+
import { WebRTCModule } from '@ajna-inc/webrtc'
|
|
15
23
|
|
|
16
24
|
const agent = new Agent({
|
|
17
25
|
config: { /* ... */ },
|
|
18
26
|
dependencies: agentDependencies,
|
|
19
27
|
modules: {
|
|
20
|
-
webrtc: new WebRTCModule(
|
|
28
|
+
webrtc: new WebRTCModule({
|
|
29
|
+
// Configure default ICE servers (optional - has sensible defaults)
|
|
30
|
+
iceServers: [
|
|
31
|
+
{ urls: 'stun:stun.l.google.com:19302' },
|
|
32
|
+
{ urls: 'turn:turn.example.org:3478', username: 'user', credential: 'pass' },
|
|
33
|
+
],
|
|
34
|
+
// Default ICE policy: 'all' | 'relay-preferred' | 'relay-only'
|
|
35
|
+
defaultPolicy: 'relay-preferred',
|
|
36
|
+
// Enable trickle ICE by default
|
|
37
|
+
defaultTrickle: true,
|
|
38
|
+
}),
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## NAT/Firewall Traversal
|
|
44
|
+
|
|
45
|
+
For peers behind restrictive NAT or firewalls, use TURN servers:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
const agent = new Agent({
|
|
49
|
+
modules: {
|
|
50
|
+
webrtc: new WebRTCModule({
|
|
51
|
+
iceServers: [
|
|
52
|
+
// STUN for simple NAT
|
|
53
|
+
{ urls: 'stun:stun.l.google.com:19302' },
|
|
54
|
+
// TURN for symmetric NAT and firewalls
|
|
55
|
+
{ urls: 'turn:turn.yourserver.com:3478', username: 'user', credential: 'pass' },
|
|
56
|
+
// TURNS (TLS) for stricter firewalls
|
|
57
|
+
{ urls: 'turns:turn.yourserver.com:5349', username: 'user', credential: 'pass' },
|
|
58
|
+
],
|
|
59
|
+
// Force relay-only for maximum compatibility (hides IP addresses too)
|
|
60
|
+
defaultPolicy: 'relay-only',
|
|
61
|
+
}),
|
|
21
62
|
},
|
|
22
63
|
})
|
|
23
64
|
```
|
|
24
65
|
|
|
25
66
|
## Frontend usage (browser)
|
|
26
67
|
|
|
27
|
-
Below is a
|
|
68
|
+
Below is a setup for P2P WebRTC using Credo TS for signaling.
|
|
28
69
|
|
|
29
70
|
```ts
|
|
71
|
+
// Get configured ICE servers from module
|
|
72
|
+
const iceServers = agent.modules.webrtc.getDefaultIceServers()
|
|
73
|
+
|
|
30
74
|
// Subscribe to inbound signaling
|
|
75
|
+
agent.events.on('WebRTCEvents.IncomingPropose', async ({ payload }) => {
|
|
76
|
+
const { thid, iceServers, policy, media } = payload
|
|
77
|
+
// Caller is proposing a call - you can accept by sending an offer
|
|
78
|
+
// The iceServers from propose can be used to configure RTCPeerConnection
|
|
79
|
+
console.log('Incoming call proposal:', { media, iceServers })
|
|
80
|
+
})
|
|
81
|
+
|
|
31
82
|
agent.events.on('WebRTCEvents.IncomingOffer', async ({ payload }) => {
|
|
32
|
-
const { thid, sdp, context } = payload
|
|
33
|
-
const pc = ensurePeer(thid)
|
|
34
|
-
await pc.setRemoteDescription({ type: 'offer', sdp })
|
|
83
|
+
const { thid, sdp, iceServers, policy, context } = payload
|
|
35
84
|
|
|
85
|
+
// Use ICE servers from offer (or fall back to module defaults)
|
|
86
|
+
const pc = new RTCPeerConnection({
|
|
87
|
+
iceServers: iceServers ?? agent.modules.webrtc.getDefaultIceServers(),
|
|
88
|
+
iceTransportPolicy: policy === 'relay-only' ? 'relay' : 'all',
|
|
89
|
+
})
|
|
90
|
+
peers.set(thid, pc)
|
|
91
|
+
|
|
92
|
+
await pc.setRemoteDescription({ type: 'offer', sdp })
|
|
36
93
|
const answer = await pc.createAnswer()
|
|
37
94
|
await pc.setLocalDescription(answer)
|
|
38
95
|
|
|
@@ -44,8 +101,11 @@ agent.events.on('WebRTCEvents.IncomingOffer', async ({ payload }) => {
|
|
|
44
101
|
})
|
|
45
102
|
|
|
46
103
|
agent.events.on('WebRTCEvents.IncomingAnswer', async ({ payload }) => {
|
|
47
|
-
const { thid, sdp } = payload
|
|
48
|
-
const pc =
|
|
104
|
+
const { thid, sdp, iceServers } = payload
|
|
105
|
+
const pc = peers.get(thid)
|
|
106
|
+
if (!pc) return
|
|
107
|
+
|
|
108
|
+
// If callee provided additional ICE servers, you might want to restart ICE
|
|
49
109
|
await pc.setRemoteDescription({ type: 'answer', sdp })
|
|
50
110
|
})
|
|
51
111
|
|
|
@@ -57,16 +117,41 @@ agent.events.on('WebRTCEvents.IncomingIce', async ({ payload }) => {
|
|
|
57
117
|
else await pc.addIceCandidate(candidate as RTCIceCandidateInit)
|
|
58
118
|
})
|
|
59
119
|
|
|
120
|
+
// Handle renegotiation requests (e.g., ICE restart)
|
|
121
|
+
agent.events.on('WebRTCEvents.RenegotiateRequested', async ({ payload }) => {
|
|
122
|
+
const { thid, iceRestart, iceServers, reason } = payload
|
|
123
|
+
const pc = peers.get(thid)
|
|
124
|
+
if (!pc) return
|
|
125
|
+
|
|
126
|
+
if (iceRestart) {
|
|
127
|
+
// Perform ICE restart
|
|
128
|
+
const offer = await pc.createOffer({ iceRestart: true })
|
|
129
|
+
await pc.setLocalDescription(offer)
|
|
130
|
+
// Send new offer...
|
|
131
|
+
}
|
|
132
|
+
})
|
|
133
|
+
|
|
60
134
|
// Start call (caller)
|
|
61
135
|
async function startCall(connectionId: string, localStream: MediaStream) {
|
|
62
136
|
const thid = crypto.randomUUID()
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
137
|
+
|
|
138
|
+
// Use module's default ICE servers
|
|
139
|
+
const iceServers = agent.modules.webrtc.getDefaultIceServers()
|
|
140
|
+
|
|
141
|
+
const pc = new RTCPeerConnection({ iceServers })
|
|
66
142
|
peers.set(thid, pc)
|
|
67
143
|
|
|
144
|
+
// Handle ICE connection failures
|
|
145
|
+
pc.oniceconnectionstatechange = () => {
|
|
146
|
+
if (pc.iceConnectionState === 'failed') {
|
|
147
|
+
// Request ICE restart
|
|
148
|
+
agent.modules.webrtc.restartIce({ connectionId, threadId: thid })
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
68
152
|
// Render remote stream
|
|
69
153
|
pc.ontrack = e => (remoteVideo.srcObject = e.streams[0])
|
|
154
|
+
|
|
70
155
|
// Send ICE via DIDComm
|
|
71
156
|
pc.onicecandidate = e => agent.modules.webrtc.sendIce({
|
|
72
157
|
connectionId,
|
|
@@ -74,23 +159,64 @@ async function startCall(connectionId: string, localStream: MediaStream) {
|
|
|
74
159
|
candidate: e.candidate ?? undefined,
|
|
75
160
|
endOfCandidates: e.candidate == null,
|
|
76
161
|
})
|
|
162
|
+
|
|
77
163
|
// Add local tracks
|
|
78
164
|
for (const track of localStream.getTracks()) pc.addTrack(track, localStream)
|
|
79
165
|
|
|
80
166
|
const offer = await pc.createOffer({ offerToReceiveVideo: true, offerToReceiveAudio: true })
|
|
81
167
|
await pc.setLocalDescription(offer)
|
|
82
168
|
|
|
169
|
+
// Start call - ICE servers are automatically included from module config
|
|
83
170
|
await agent.modules.webrtc.startCall({
|
|
84
171
|
connectionId,
|
|
85
172
|
threadId: thid,
|
|
86
173
|
sdp: offer.sdp!,
|
|
87
|
-
|
|
174
|
+
// Optionally override policy for this specific call
|
|
175
|
+
policy: 'relay-preferred',
|
|
176
|
+
})
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Request ICE restart when connection fails
|
|
180
|
+
async function handleConnectionFailure(connectionId: string, thid: string) {
|
|
181
|
+
await agent.modules.webrtc.restartIce({
|
|
182
|
+
connectionId,
|
|
183
|
+
threadId: thid,
|
|
184
|
+
// Optionally provide new ICE servers
|
|
185
|
+
iceServers: [{ urls: 'turn:backup-turn.example.org:3478', username: 'u', credential: 'p' }],
|
|
88
186
|
})
|
|
89
187
|
}
|
|
90
188
|
```
|
|
91
189
|
|
|
92
|
-
|
|
190
|
+
## API Reference
|
|
191
|
+
|
|
192
|
+
### WebRTCApi Methods
|
|
193
|
+
|
|
194
|
+
| Method | Description |
|
|
195
|
+
|--------|-------------|
|
|
196
|
+
| `proposeCall()` | Send a call proposal with ICE servers before offer |
|
|
197
|
+
| `startCall()` | Send SDP offer with ICE servers |
|
|
198
|
+
| `acceptCall()` | Send SDP answer |
|
|
199
|
+
| `sendIce()` | Send ICE candidate |
|
|
200
|
+
| `renegotiate()` | Request renegotiation (track changes, codec changes) |
|
|
201
|
+
| `restartIce()` | Request ICE restart (connection recovery) |
|
|
202
|
+
| `endCall()` | End the call |
|
|
203
|
+
| `getDefaultIceServers()` | Get configured default ICE servers |
|
|
204
|
+
|
|
205
|
+
### Events
|
|
206
|
+
|
|
207
|
+
| Event | Description |
|
|
208
|
+
|-------|-------------|
|
|
209
|
+
| `IncomingPropose` | Call proposal received (includes ICE servers) |
|
|
210
|
+
| `IncomingOffer` | SDP offer received |
|
|
211
|
+
| `IncomingAnswer` | SDP answer received |
|
|
212
|
+
| `IncomingIce` | ICE candidate received |
|
|
213
|
+
| `RenegotiateRequested` | Renegotiation requested (e.g., ICE restart) |
|
|
214
|
+
| `CallEnded` | Call ended |
|
|
215
|
+
|
|
216
|
+
## Notes
|
|
217
|
+
|
|
93
218
|
- This module moves only signaling over DIDComm; media flows via WebRTC/DTLS-SRTP.
|
|
94
|
-
- For NAT traversal,
|
|
219
|
+
- For NAT traversal, configure TURN servers in the module config or per-call.
|
|
220
|
+
- Use `relay-only` policy for maximum firewall compatibility (also hides IP addresses).
|
|
95
221
|
- Works with mediators and message pickup (offline delivery), as with any DIDComm message.
|
|
96
222
|
|
package/build/index.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
export { WebRTCModule } from './webrtc/WebRTCModule';
|
|
1
|
+
export { WebRTCModule, WebRTCModuleConfigToken } from './webrtc/WebRTCModule';
|
|
2
|
+
export type { WebRTCModuleConfig, WebRTCIceServerConfig } from './webrtc/WebRTCModule';
|
|
2
3
|
export { WebRTCApi } from './webrtc/WebRTCApi';
|
|
3
|
-
export { WebRTCEvents, type IncomingOfferEvent, type IncomingIceEvent } from './webrtc/WebRTCEvents';
|
|
4
|
+
export { WebRTCEvents, type IceServerEventConfig, type IncomingProposeEvent, type IncomingOfferEvent, type IncomingAnswerEvent, type IncomingIceEvent, type RenegotiateRequestedEvent, type CallEndedEvent, } from './webrtc/WebRTCEvents';
|
|
4
5
|
export * from './webrtc/messages';
|
|
6
|
+
export type { IcePolicy, IceServerConfig } from './webrtc/messages/OfferMessage';
|
|
7
|
+
export type { MediaType } from './webrtc/messages/ProposeMessage';
|
|
8
|
+
export type { RenegotiateReason } from './webrtc/messages/RenegotiateMessage';
|
package/build/index.js
CHANGED
|
@@ -14,12 +14,16 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.WebRTCEvents = exports.WebRTCApi = exports.WebRTCModule = void 0;
|
|
17
|
+
exports.WebRTCEvents = exports.WebRTCApi = exports.WebRTCModuleConfigToken = exports.WebRTCModule = void 0;
|
|
18
|
+
// Module and API
|
|
18
19
|
var WebRTCModule_1 = require("./webrtc/WebRTCModule");
|
|
19
20
|
Object.defineProperty(exports, "WebRTCModule", { enumerable: true, get: function () { return WebRTCModule_1.WebRTCModule; } });
|
|
21
|
+
Object.defineProperty(exports, "WebRTCModuleConfigToken", { enumerable: true, get: function () { return WebRTCModule_1.WebRTCModuleConfigToken; } });
|
|
20
22
|
var WebRTCApi_1 = require("./webrtc/WebRTCApi");
|
|
21
23
|
Object.defineProperty(exports, "WebRTCApi", { enumerable: true, get: function () { return WebRTCApi_1.WebRTCApi; } });
|
|
24
|
+
// Events
|
|
22
25
|
var WebRTCEvents_1 = require("./webrtc/WebRTCEvents");
|
|
23
26
|
Object.defineProperty(exports, "WebRTCEvents", { enumerable: true, get: function () { return WebRTCEvents_1.WebRTCEvents; } });
|
|
27
|
+
// Messages
|
|
24
28
|
__exportStar(require("./webrtc/messages"), exports);
|
|
25
29
|
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,iBAAiB;AACjB,sDAA6E;AAApE,4GAAA,YAAY,OAAA;AAAE,uHAAA,uBAAuB,OAAA;AAE9C,gDAA8C;AAArC,sGAAA,SAAS,OAAA;AAElB,SAAS;AACT,sDAS8B;AAR5B,4GAAA,YAAY,OAAA;AAUd,WAAW;AACX,oDAAiC"}
|
|
@@ -1,36 +1,125 @@
|
|
|
1
1
|
import { ConnectionService, MessageSender, AgentContext } from '@credo-ts/core';
|
|
2
2
|
import { WebRTCService } from './services/WebRTCService';
|
|
3
|
+
import type { IcePolicy, IceServerConfig } from './messages/OfferMessage';
|
|
4
|
+
import type { MediaType } from './messages/ProposeMessage';
|
|
5
|
+
import type { RenegotiateReason } from './messages/RenegotiateMessage';
|
|
6
|
+
import { type WebRTCModuleConfig } from './WebRTCModule';
|
|
3
7
|
export declare class WebRTCApi {
|
|
4
8
|
private connectionService;
|
|
5
9
|
private messageSender;
|
|
6
10
|
private webrtcService;
|
|
7
11
|
private agentContext;
|
|
12
|
+
private config;
|
|
8
13
|
constructor(connectionService: ConnectionService, messageSender: MessageSender, webrtcService: WebRTCService, agentContext: AgentContext);
|
|
9
14
|
onAgentReady: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Get the configured default ICE servers
|
|
17
|
+
*/
|
|
18
|
+
getDefaultIceServers(): WebRTCModuleConfig['iceServers'];
|
|
19
|
+
/**
|
|
20
|
+
* Propose a call to a peer (before sending offer)
|
|
21
|
+
* This allows sharing ICE servers and negotiating capabilities before the SDP exchange
|
|
22
|
+
*/
|
|
23
|
+
proposeCall(options: {
|
|
24
|
+
connectionId: string;
|
|
25
|
+
threadId?: string;
|
|
26
|
+
parentThreadId?: string;
|
|
27
|
+
/** Requested media types */
|
|
28
|
+
media?: MediaType[];
|
|
29
|
+
/** Whether data channel is requested */
|
|
30
|
+
data?: boolean;
|
|
31
|
+
/** Topology: 'mesh' (P2P) or 'sfu' */
|
|
32
|
+
topology?: 'mesh' | 'sfu';
|
|
33
|
+
/** Whether trickle ICE is supported (defaults to module config) */
|
|
34
|
+
trickle?: boolean;
|
|
35
|
+
/** ICE policy (defaults to module config) */
|
|
36
|
+
policy?: IcePolicy;
|
|
37
|
+
/** ICE servers - uses module defaults if not provided */
|
|
38
|
+
iceServers?: IceServerConfig[];
|
|
39
|
+
/** Optional reason/context */
|
|
40
|
+
reason?: string;
|
|
41
|
+
}): Promise<{
|
|
42
|
+
threadId: string;
|
|
43
|
+
}>;
|
|
44
|
+
/**
|
|
45
|
+
* Start a call by sending an SDP offer
|
|
46
|
+
*/
|
|
10
47
|
startCall(options: {
|
|
11
48
|
connectionId: string;
|
|
12
49
|
threadId: string;
|
|
50
|
+
parentThreadId?: string;
|
|
13
51
|
sdp: string;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
52
|
+
/** ICE servers - uses module defaults if not provided */
|
|
53
|
+
iceServers?: IceServerConfig[];
|
|
54
|
+
/** ICE policy (defaults to module config) */
|
|
55
|
+
policy?: IcePolicy;
|
|
56
|
+
/** Whether trickle ICE is enabled (defaults to module config) */
|
|
57
|
+
trickle?: boolean;
|
|
19
58
|
}): Promise<import("./repository/WebRTCCallRecord").WebRTCCallRecord>;
|
|
59
|
+
/**
|
|
60
|
+
* Accept a call by sending an SDP answer
|
|
61
|
+
*/
|
|
20
62
|
acceptCall(options: {
|
|
21
63
|
connectionId: string;
|
|
22
64
|
threadId: string;
|
|
65
|
+
parentThreadId?: string;
|
|
23
66
|
sdp: string;
|
|
67
|
+
/** Optional ICE servers from callee (if different from caller's) */
|
|
68
|
+
iceServers?: IceServerConfig[];
|
|
24
69
|
}): Promise<void>;
|
|
70
|
+
/**
|
|
71
|
+
* Send an ICE candidate
|
|
72
|
+
*/
|
|
25
73
|
sendIce(options: {
|
|
26
74
|
connectionId: string;
|
|
27
75
|
threadId: string;
|
|
76
|
+
parentThreadId?: string;
|
|
28
77
|
candidate?: Record<string, unknown>;
|
|
29
78
|
endOfCandidates?: boolean;
|
|
30
79
|
}): Promise<void>;
|
|
80
|
+
/**
|
|
81
|
+
* Request renegotiation (for ICE restart, adding/removing tracks, etc.)
|
|
82
|
+
*
|
|
83
|
+
* Use this when:
|
|
84
|
+
* - ICE connection failed and needs restart
|
|
85
|
+
* - Adding screenshare or switching cameras
|
|
86
|
+
* - Network changed (WiFi to cellular)
|
|
87
|
+
* - Changing codec or bandwidth settings
|
|
88
|
+
*/
|
|
89
|
+
renegotiate(options: {
|
|
90
|
+
connectionId: string;
|
|
91
|
+
threadId: string;
|
|
92
|
+
parentThreadId?: string;
|
|
93
|
+
/** Reason for renegotiation */
|
|
94
|
+
reason: RenegotiateReason | string;
|
|
95
|
+
/** Whether to perform ICE restart (required for recovering failed connections) */
|
|
96
|
+
iceRestart?: boolean;
|
|
97
|
+
/** New ICE servers (useful when switching networks) */
|
|
98
|
+
iceServers?: IceServerConfig[];
|
|
99
|
+
/** Updated ICE policy */
|
|
100
|
+
policy?: IcePolicy;
|
|
101
|
+
}): Promise<void>;
|
|
102
|
+
/**
|
|
103
|
+
* Request ICE restart (convenience method for renegotiate with iceRestart=true)
|
|
104
|
+
*
|
|
105
|
+
* Use when the WebRTC connection has failed or disconnected
|
|
106
|
+
*/
|
|
107
|
+
restartIce(options: {
|
|
108
|
+
connectionId: string;
|
|
109
|
+
threadId: string;
|
|
110
|
+
parentThreadId?: string;
|
|
111
|
+
/** New ICE servers (optional, useful when original servers are unreachable) */
|
|
112
|
+
iceServers?: IceServerConfig[];
|
|
113
|
+
/** Updated ICE policy */
|
|
114
|
+
policy?: IcePolicy;
|
|
115
|
+
}): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
* End a call
|
|
118
|
+
*/
|
|
31
119
|
endCall(options: {
|
|
32
120
|
connectionId: string;
|
|
33
121
|
threadId: string;
|
|
122
|
+
parentThreadId?: string;
|
|
34
123
|
reason?: string;
|
|
35
124
|
}): Promise<void>;
|
|
36
125
|
}
|