@tencent-rtc/mcp 1.0.3 → 1.0.5
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/package.json +1 -1
- package/resource/doc/rtcengine/faq/web.md +309 -0
- package/resource/doc/rtcengine/web/.gitkeep +0 -0
- package/resource/doc/rtcengine/web/api_RTCEngine.md +2425 -0
- package/resource/doc/rtcengine/web/how_to_integrated_web_rtc_engine.md +492 -0
- package/resource/doc/rtcengine/web/how_to_run_web_rtc_engine_demo.md +63 -0
- package/server.js +30 -0
- package/server.js.map +1 -1
package/package.json
CHANGED
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
# Handle Autoplay Restriction
|
|
2
|
+
|
|
3
|
+
## Introduction
|
|
4
|
+
|
|
5
|
+
In order to prevent web pages from automatically playing audio and video and causing interference to users, browsers have restricted the automatic playback function of audio and video: **before the user interacts with the web page (such as clicking, touching the page, etc.), the web page will be prohibited from playing media with sound. In iOS 10+, if low power mode is enabled, media without sound will also be prohibited from playing.**
|
|
6
|
+
|
|
7
|
+
Affected by the above browser autoplay policy, when using the TRTC Web SDK, there may be problems with playing silently. The most common scenario is: in your product design, support users to automatically enter the room and pull the stream for playback without the need for users to click after refreshing the page.
|
|
8
|
+
|
|
9
|
+
This article mainly introduces **how to solve the problem of playback failure caused by the restriction of autoplay policy**.
|
|
10
|
+
|
|
11
|
+
## Solution 1: Use the SDK's autoplay popup window
|
|
12
|
+
|
|
13
|
+
By default, when autoplay fails, the SDK will pop up to guide the user to interact with the page. After the interaction occurs, the SDK will actively call the interface to resume playback.
|
|
14
|
+
|
|
15
|
+
- **Advantages**: The business side does not need to do anything, simple and efficient.
|
|
16
|
+
- **Disadvantages**: The popup window provided by the SDK may not meet the design requirements of the business product. At this time, you can consider using Solution 2.
|
|
17
|
+
|
|
18
|
+
The popup window provided by the SDK is adapted to desktop and mobile browser, and the style is as follows:
|
|
19
|
+
|
|
20
|
+

|
|
21
|
+

|
|
22
|
+
|
|
23
|
+
- The SDK will switch the Chinese and English popup prompts according to the value of [navigator.language](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/language).
|
|
24
|
+
- When the user clicks "OK", the SDK automatically calls the relevant interface to resume playback.
|
|
25
|
+
- In order to ensure that the popup window is displayed on the top layer as much as possible, its z-index value is 1500.
|
|
26
|
+
|
|
27
|
+
## Solution 2: Custom handling of autoplay restrictions
|
|
28
|
+
|
|
29
|
+
If you want to have complete control over the user experience when autoplay fails, you can follow these steps:
|
|
30
|
+
|
|
31
|
+
### Step 1: Disable SDK default popup
|
|
32
|
+
|
|
33
|
+
Set the `enableAutoPlayDialog` parameter to `false` when calling the [TRTC.enterRoom](./TRTC.html#enterRoom) interface:
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
await trtc.enterRoom({
|
|
37
|
+
// ... other parameters
|
|
38
|
+
enableAutoPlayDialog: false
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Step 2: Listen for autoplay failure events (Required)
|
|
43
|
+
|
|
44
|
+
Listen to the [AUTOPLAY_FAILED](./module-EVENT.html#.AUTOPLAY_FAILED) event to handle autoplay failures. The SDK will automatically monitor user click events and resume playback after the user clicks.
|
|
45
|
+
|
|
46
|
+
```javascript
|
|
47
|
+
trtc.on(TRTC.EVENT.AUTOPLAY_FAILED, event => {
|
|
48
|
+
// Add your custom handling logic here
|
|
49
|
+
// For example: display a custom prompt to guide users to click on the page
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Step 3: Actively resume playback (Optional)
|
|
54
|
+
|
|
55
|
+
> Supported from version 5.9.0
|
|
56
|
+
|
|
57
|
+
In some special scenarios (such as in iframes or when using specific frontend frameworks), you may need to actively call the resume playback method. In this case, you can use the `resume` method provided in the event:
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
trtc.on(TRTC.EVENT.AUTOPLAY_FAILED, (event) => {
|
|
61
|
+
showCustomDialog(event);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
function showCustomDialog(event) {
|
|
65
|
+
const { userId, resume } = event;
|
|
66
|
+
// The restricted user id is userId, resume is the function to resume playback for this user
|
|
67
|
+
// Need to display a custom button, prompt that userId playback is restricted, and call 'resume()' in the button click callback
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Important Notes
|
|
72
|
+
|
|
73
|
+
1. Some browsers may check user interaction status before each playback, so autoplay failure events may be triggered multiple times.
|
|
74
|
+
|
|
75
|
+
2. Special requirements in iOS WeChat browser and mini-program webview:
|
|
76
|
+
- When playing remote streams for the first time, you must call the playback interface directly in the user click event callback
|
|
77
|
+
- You cannot use asynchronous methods like setTimeout to call the playback interface, otherwise playback will fail
|
|
78
|
+
|
|
79
|
+
## Disable Autoplay Policy in Webview
|
|
80
|
+
|
|
81
|
+
The default autoplay policy in Android and iOS Webview may be different to the browser. You can refer to the following document to turn off autoplay restrictions in your App.
|
|
82
|
+
|
|
83
|
+
- **Android Webview**: Call [setMediaPlaybackRequiresUserGesture(false)](https://developer.android.com/reference/android/webkit/WebSettings#setMediaPlaybackRequiresUserGesture(boolean)).
|
|
84
|
+
- **iOS Webview**: Set [mediaTypesRequiringUserActionForPlayback](https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/1851524-mediatypesrequiringuseractionfor) to WKAudiovisualMediaTypeNone.
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
# Handle Firewall Restriction
|
|
90
|
+
|
|
91
|
+
## Function Description
|
|
92
|
+
|
|
93
|
+
This article mainly introduces the best practices for dealing with firewall restrictions. For example, in a network environment with a firewall such as an enterprise intranet, TRTC Web SDK cannot be used normally due to firewall restrictions. In this case, there are two solutions:
|
|
94
|
+
|
|
95
|
+
- Solution 1: Listen for SDK errors and guide users to change networks or configure firewall whitelists.
|
|
96
|
+
- Solution 2: Use the Nginx + coturn proxy solution.
|
|
97
|
+
|
|
98
|
+
> The TRTC Web SDK uses UDP to communicate with the TRTC server by default, and has a built-in Turn Server that supports relaying media data through UDP or TCP.
|
|
99
|
+
> In the public network, users do not need to set up any proxies, as the SDK will attempt to establish media connections in the order of direct connection, Turn Server UDP, and Turn Server TCP.
|
|
100
|
+
> If it is known that the user will be using the SDK within an internal network firewall, it may not be possible to establish a media connection, and a proxy will need to be set up.
|
|
101
|
+
|
|
102
|
+
## Prerequisites
|
|
103
|
+
|
|
104
|
+
- Solution 2 requires the deployment of two servers, Nginx + Turn Server. You can contact your company's operations and maintenance colleagues to assist in building. The Nginx proxy server is used to proxy and forward the Websocket signaling data packets of TRTC Web SDK; the Turn Server is used to forward audio and video data packets.
|
|
105
|
+
|
|
106
|
+
## Implementation Process
|
|
107
|
+
|
|
108
|
+
### Solution 1
|
|
109
|
+
|
|
110
|
+
> This solution is suitable for: You cannot confirm whether the user's network will be restricted by the firewall. At this time, you can listen for SDK errors, guide users to change networks or check firewalls.
|
|
111
|
+
|
|
112
|
+
When you call APIs such as startLocalVideo, startLocalAudio, startRemoteVideo, etc., the SDK will establish a media connection channel internally for transmitting audio and video data. When encountering firewall restrictions, the SDK may fail to establish a connection, and the SDK will throw a firewall-restricted error and continue to retry.
|
|
113
|
+
|
|
114
|
+
You can refer to the following code example to listen for this error and guide users to change networks or check network firewalls and whitelist the domains and ports used by TRTC Web SDK. Reference: [TRTC Web SDK Domain and Port Whitelist](https://trtc.io/document/59667?platform=web&product=rtcengine&menulabel=coresdk).
|
|
115
|
+
|
|
116
|
+
```javascript
|
|
117
|
+
trtc.on(TRTC.EVENT.ERROR, error => {
|
|
118
|
+
// User network firewall restrictions may cause audio and video calls to fail.
|
|
119
|
+
// At this time, guide users to change networks or check network firewall settings.
|
|
120
|
+
if (error.code === TRTC.ERROR_CODE.OPERATION_FAILED && error.extraCode === 5501) {
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Solution 2
|
|
126
|
+
|
|
127
|
+
> This solution is suitable for: You confirm that the user's network is restricted by the firewall, and you need to set up a proxy server to solve the problem.
|
|
128
|
+
|
|
129
|
+
| Solution | Applicable scenarios | Network requirements |
|
|
130
|
+
|----------|---------------------|---------------------|
|
|
131
|
+
| A | Users can access a specific external proxy server on the network | The proxy server is deployed on the external network, and the internal network firewall needs to open a whitelist to allow internal network users to access the external proxy server. |
|
|
132
|
+
| B | Users can only access an internal proxy server on the network | The proxy server is deployed on the internal network, and the internal network firewall needs to open a whitelist to allow the internal proxy server to access the external network. |
|
|
133
|
+
|
|
134
|
+

|
|
135
|
+
|
|
136
|
+
*Example of Solution 2-A*
|
|
137
|
+
|
|
138
|
+

|
|
139
|
+
|
|
140
|
+
*Example of Solution 2-B*
|
|
141
|
+
|
|
142
|
+
### Setting up a proxy server
|
|
143
|
+
|
|
144
|
+
After you have set up Nginx and Turn server, you can refer to the following example to set up a proxy server.
|
|
145
|
+
|
|
146
|
+
```javascript
|
|
147
|
+
const trtc = TRTC.create();
|
|
148
|
+
await trtc.enterRoom({
|
|
149
|
+
...,
|
|
150
|
+
proxy: {
|
|
151
|
+
// Set up a Websocket proxy to relay signaling data packets between the SDK and the TRTC backend.
|
|
152
|
+
websocketProxy: 'wss://proxy.example.com/ws/',
|
|
153
|
+
// Set up a turn server to relay media data packets between the SDK and the TRTC backend. 14.3.3.3:3478 is the IP address and port of the turn server.
|
|
154
|
+
turnServer: { url: '14.3.3.3:3478', username: 'turn', credential: 'turn', credentialType: 'password' },
|
|
155
|
+
// By default, the SDK will connect to trtc server directly, if connection failed, then SDK will try to connect the TURN server to relay the media data. You can set 'relay' to force the connection through the TURN server.
|
|
156
|
+
iceTransportPolicy: 'all',
|
|
157
|
+
// By default, the SDK reports logs to the yun.tim.qq.com domain name. If this domain name cannot be accessed in your internal network, you need to whitelist the domain name or configure the following log proxy.
|
|
158
|
+
// Set up a log reporting proxy. Logs are key data for troubleshooting, so be sure to set up this proxy.
|
|
159
|
+
loggerProxy: 'https://proxy.example.com/logger/',
|
|
160
|
+
}
|
|
161
|
+
})
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Solution 2-A
|
|
165
|
+
|
|
166
|
+
#### Setting up Nginx server
|
|
167
|
+
|
|
168
|
+
1. **Deploy Nginx server**
|
|
169
|
+
|
|
170
|
+
Refer to the Nginx server deployment tutorial found on the Internet for deployment. If the enterprise has already deployed the Nginx service, it can be configured directly.
|
|
171
|
+
|
|
172
|
+
2. **Configure Nginx server**
|
|
173
|
+
|
|
174
|
+
```sh
|
|
175
|
+
vi /etc/nginx/nginx.conf
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
```nginx
|
|
179
|
+
http {
|
|
180
|
+
server {
|
|
181
|
+
# The access domain name of the Nginx server
|
|
182
|
+
server_name proxy.example.com;
|
|
183
|
+
# The access port of the Nginx server
|
|
184
|
+
listen 443;
|
|
185
|
+
ssl on;
|
|
186
|
+
location /ws/ { # Corresponding to the websocketProxy parameter in setProxyServer
|
|
187
|
+
proxy_pass https://signaling.rtc.qq.com/; # TRTC server
|
|
188
|
+
proxy_http_version 1.1;
|
|
189
|
+
proxy_set_header Upgrade $http_upgrade;
|
|
190
|
+
proxy_set_header Connection "upgrade";
|
|
191
|
+
}
|
|
192
|
+
location /logger/ { # Corresponding to the loggerProxy parameter in setProxyServer
|
|
193
|
+
proxy_pass https://yun.tim.qq.com/;
|
|
194
|
+
}
|
|
195
|
+
# SSL certificate corresponding to the domain name, used for HTTPS, users need to apply for it themselves
|
|
196
|
+
ssl_certificate ./crt/1_proxy.trtcapi.com_bundle.crt;
|
|
197
|
+
ssl_certificate_key ./crt/2_proxy.trtcapi.com.key;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
3. **Reload Nginx**
|
|
203
|
+
|
|
204
|
+
```sh
|
|
205
|
+
sudo nginx -s reload
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
4. Confirm that the company's firewall allows access to the Nginx server IP and port.
|
|
209
|
+
|
|
210
|
+
#### Setting up Turn server
|
|
211
|
+
|
|
212
|
+
You can search for turn server setup tutorials on the Internet for setup, or you can use the following script to set up a turn server in `Centos`.
|
|
213
|
+
|
|
214
|
+
1. **Create a script file `turn.sh` in the Linux server, and the script content is as follows:**
|
|
215
|
+
|
|
216
|
+
```sh
|
|
217
|
+
#!/usr/bin/env bash
|
|
218
|
+
# current file name is turn.sh
|
|
219
|
+
# ref:
|
|
220
|
+
# https://gabrieltanner.org/blog/turn-server STEP 3 testing turn server
|
|
221
|
+
# https://medium.com/av-transcode/what-is-webrtc-and-how-to-setup-stun-turn-server-for-webrtc-communication-63314728b9d0
|
|
222
|
+
# as super-user
|
|
223
|
+
# usage: current_program <external-ip>
|
|
224
|
+
set -x
|
|
225
|
+
set -e
|
|
226
|
+
ip a
|
|
227
|
+
pwd
|
|
228
|
+
whoami
|
|
229
|
+
display_usage() {
|
|
230
|
+
echo "This script must be run with super-user privileges."
|
|
231
|
+
echo -e "\nUsage: $0 <external-ip> \ne.g. $0 154.8.246.205"
|
|
232
|
+
}
|
|
233
|
+
# if less than two arguments supplied, display usage
|
|
234
|
+
if [ $# -lt 1 ]
|
|
235
|
+
then
|
|
236
|
+
display_usage
|
|
237
|
+
exit 1
|
|
238
|
+
fi
|
|
239
|
+
if [[ $1 =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
|
|
240
|
+
echo "get external ip $1"
|
|
241
|
+
else
|
|
242
|
+
echo "wrong external ip $1 , must not have whitespace, tab and other char"
|
|
243
|
+
exit 2
|
|
244
|
+
fi
|
|
245
|
+
yum install -y coturn
|
|
246
|
+
# $1 is <external-ip>
|
|
247
|
+
cat <<EOF > /etc/coturn/turnserver.conf
|
|
248
|
+
external-ip=$1
|
|
249
|
+
listening-port=3478
|
|
250
|
+
lt-cred-mech
|
|
251
|
+
max-port=65535
|
|
252
|
+
min-port=20000
|
|
253
|
+
no-dtls
|
|
254
|
+
no-tls
|
|
255
|
+
realm=tencent
|
|
256
|
+
user=turn:turn
|
|
257
|
+
verbose
|
|
258
|
+
EOF
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
2. **Add executable permission**
|
|
262
|
+
|
|
263
|
+
```sh
|
|
264
|
+
chmod +x turn.sh
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
3. **Execute the script as root, `sudo ./turn.sh <server public IP>`, for example:**
|
|
268
|
+
|
|
269
|
+
```sh
|
|
270
|
+
sudo ./turn.sh 14.3.3.3
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
4. **Start the turn server**
|
|
274
|
+
|
|
275
|
+
```sh
|
|
276
|
+
systemctl start coturn
|
|
277
|
+
# Check if turn is started successfully
|
|
278
|
+
ps aux | grep coturn
|
|
279
|
+
# If you want to restart the service, execute
|
|
280
|
+
service coturn restart
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
5. **Configure the firewall for the turn server, open inbound port 3478 (TCP & UDP), and outbound ports (UDP) between min and max ports in the configuration above.**
|
|
284
|
+
|
|
285
|
+
6. **Configure the company's internal network firewall to allow access to the turn server's IP and open outbound port 3478 (TCP & UDP).**
|
|
286
|
+
|
|
287
|
+
7. **Test the turn server**
|
|
288
|
+
|
|
289
|
+
Use this [test page](https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/) to test whether the turn server is accessible. If the result shows "done" as shown in the screenshot below, the turn server is working properly.
|
|
290
|
+
|
|
291
|
+

|
|
292
|
+
|
|
293
|
+
### Solution 2-B
|
|
294
|
+
|
|
295
|
+
Solution B builds the Nginx proxy in the same way as Scenario A. There are two main differences:
|
|
296
|
+
|
|
297
|
+
1. When building a turn server, the external-ip field in the configuration file must be filled in with the address of the server on your corporate intranet.
|
|
298
|
+
|
|
299
|
+
```sh
|
|
300
|
+
# The start script in option A is the server's external address, e.g. 14.3.3.3
|
|
301
|
+
sudo . /turn.sh 14.3.3.3
|
|
302
|
+
# In option B, the start script fills in the server's intranet address, e.g. 10.0.0.4 for the intranet
|
|
303
|
+
sudo . /turn.sh 10.0.0.4
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
2. Firewall configuration:
|
|
307
|
+
|
|
308
|
+
- For the Nginx server, the domain name whitelist needs to be configured in the company's intranet firewall to allow the Nginx server to access TRTC's related domain names. Refer to: [TRTC Web SDK domain and port whitelist](https://trtc.io/document/59667?platform=web&product=rtcengine&menulabel=coresdk)
|
|
309
|
+
- For the Turn Server, allow the Turn Server to access the external network.
|
|
File without changes
|