whepts 1.0.3 → 1.1.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/README.md +116 -9
- package/dist/core/codec.d.ts +6 -6
- package/dist/core/connection.d.ts +7 -9
- package/dist/core/http.d.ts +8 -5
- package/dist/errors.d.ts +3 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -1
- package/dist/types.d.ts +15 -2
- package/dist/utils/flow-check.d.ts +31 -8
- package/dist/whep.d.ts +23 -3
- package/eslint.config.ts +4 -2
- package/package.json +16 -11
- package/rollup.config.ts +4 -1
- package/.claude/settings.local.json +0 -9
- package/.vscode/settings.json +0 -38
- package/AGENTS.md +0 -92
- package/src/core/codec.ts +0 -33
- package/src/core/connection.ts +0 -103
- package/src/core/http.ts +0 -93
- package/src/core/track.ts +0 -51
- package/src/errors.ts +0 -27
- package/src/index.ts +0 -7
- package/src/types.ts +0 -37
- package/src/utils/flow-check.ts +0 -73
- package/src/utils/sdp.ts +0 -253
- package/src/utils/webrtc.ts +0 -123
- package/src/whep.ts +0 -175
package/AGENTS.md
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
# AGENTS.md - WhepTS Player
|
|
2
|
-
|
|
3
|
-
## Build/Lint Commands
|
|
4
|
-
|
|
5
|
-
- `npm run build` - Production build with minification
|
|
6
|
-
- `npm run build:debug` - Debug build with source maps
|
|
7
|
-
- `npm run lint` - Run ESLint to check code quality
|
|
8
|
-
- `npm run lint:fix` - Auto-fix ESLint issues
|
|
9
|
-
|
|
10
|
-
**Note**: No test framework is currently configured.
|
|
11
|
-
|
|
12
|
-
## Code Style Guidelines
|
|
13
|
-
|
|
14
|
-
### Imports
|
|
15
|
-
|
|
16
|
-
- Use `~/` alias for imports from `src/` (e.g., `import X from '~/utils/observer'`)
|
|
17
|
-
- Import types explicitly using `import type { T } from './module'`
|
|
18
|
-
- Group external dependencies first, then internal imports
|
|
19
|
-
|
|
20
|
-
### Formatting
|
|
21
|
-
|
|
22
|
-
- ESLint uses `@antfu/eslint-config` with formatters enabled
|
|
23
|
-
- Run `npm run lint:fix` before committing
|
|
24
|
-
- No manual formatting required - let ESLint handle it
|
|
25
|
-
|
|
26
|
-
### Types
|
|
27
|
-
|
|
28
|
-
- Strict mode enabled in `tsconfig.json`
|
|
29
|
-
- Use `interface` for object shapes, `type` for unions/aliases
|
|
30
|
-
- Mark optional properties with `?` (e.g., `onError?: (err: WebRTCError) => void`)
|
|
31
|
-
- Use TypeScript strictly - no `any` types
|
|
32
|
-
|
|
33
|
-
### Naming Conventions
|
|
34
|
-
|
|
35
|
-
- **Classes**: PascalCase (e.g., `WebRTCWhep`, `VisibilityObserver`)
|
|
36
|
-
- **Interfaces**: PascalCase (e.g., `Conf`, `ErrorType`)
|
|
37
|
-
- **Functions/Methods**: camelCase (e.g., `setupPeerConnection`, `handleError`)
|
|
38
|
-
- **Constants**: UPPER_SNAKE_CASE (e.g., `ErrorTypes`)
|
|
39
|
-
- **Private members**: prefix with `private` keyword
|
|
40
|
-
|
|
41
|
-
### Error Handling
|
|
42
|
-
|
|
43
|
-
- Use custom `WebRTCError` class for all errors (defined in `src/errors.ts`)
|
|
44
|
-
- Error types: `SIGNAL_ERROR`, `STATE_ERROR`, `NETWORK_ERROR`, `MEDIA_ERROR`, `OTHER_ERROR`
|
|
45
|
-
- Pattern: `throw new WebRTCError(ErrorTypes.NETWORK_ERROR, 'message')`
|
|
46
|
-
- Call `this.handleError(err)` for centralized error management
|
|
47
|
-
|
|
48
|
-
### Comments
|
|
49
|
-
|
|
50
|
-
- Use JSDoc for public APIs and class constructors (in English)
|
|
51
|
-
- Keep implementation comments concise in Chinese as established
|
|
52
|
-
- Example:
|
|
53
|
-
```typescript
|
|
54
|
-
/**
|
|
55
|
-
* Create a WebRTCWhep.
|
|
56
|
-
* @param {Conf} conf - Configuration.
|
|
57
|
-
*/
|
|
58
|
-
constructor(conf: Conf)
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
### File Organization
|
|
62
|
-
|
|
63
|
-
- Core logic in `src/` directory
|
|
64
|
-
- Utilities in `src/utils/` (e.g., `observer.ts`, `flow-check.ts`, `sdp.ts`)
|
|
65
|
-
- Error types in `src/errors.ts`
|
|
66
|
-
- Export main class from `src/index.ts`
|
|
67
|
-
|
|
68
|
-
### State Management
|
|
69
|
-
|
|
70
|
-
- Use union literal types for state (e.g., `'getting_codecs' | 'running' | 'restarting' | 'closed' | 'failed'`)
|
|
71
|
-
- Always check state before operations that depend on it
|
|
72
|
-
- Use getters for derived properties (e.g., `get isRunning()`)
|
|
73
|
-
|
|
74
|
-
### WebRTC Specifics
|
|
75
|
-
|
|
76
|
-
- Always use `unified-plan` SDP semantics
|
|
77
|
-
- Handle ICE candidates with queuing when session URL not ready
|
|
78
|
-
- Support non-advertised codecs (PCMA, multiopus, L16)
|
|
79
|
-
- Use `IntersectionObserver` for visibility-based playback control
|
|
80
|
-
|
|
81
|
-
## Tech Stack
|
|
82
|
-
|
|
83
|
-
- TypeScript 5.9 with strict mode
|
|
84
|
-
- Rollup for bundling (ES module output)
|
|
85
|
-
- ESLint with @antfu/eslint-config
|
|
86
|
-
- pnpm as package manager
|
|
87
|
-
|
|
88
|
-
## Before Committing
|
|
89
|
-
|
|
90
|
-
1. Run `npm run lint` and fix all issues
|
|
91
|
-
2. Build with `npm run build` to verify production build works
|
|
92
|
-
3. No test framework - manually verify WebRTC functionality
|
package/src/core/codec.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { State } from '~/types'
|
|
2
|
-
import { ErrorTypes, WebRTCError } from '~/errors'
|
|
3
|
-
import { WebRtcUtils } from '../utils/webrtc'
|
|
4
|
-
|
|
5
|
-
export interface CodecDetectorCallbacks {
|
|
6
|
-
onCodecsDetected: (codecs: string[]) => void
|
|
7
|
-
onError: (err: Error) => void
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export class CodecDetector {
|
|
11
|
-
constructor(
|
|
12
|
-
private getState: () => State,
|
|
13
|
-
private callbacks: CodecDetectorCallbacks,
|
|
14
|
-
) {}
|
|
15
|
-
|
|
16
|
-
detect(): void {
|
|
17
|
-
Promise.all(
|
|
18
|
-
[
|
|
19
|
-
['pcma/8000/2'],
|
|
20
|
-
['multiopus/48000/6', 'channel_mapping=0,4,1,2,3,5;num_streams=4;coupled_streams=2'],
|
|
21
|
-
['L16/48000/2'],
|
|
22
|
-
].map(c => WebRtcUtils.supportsNonAdvertisedCodec(c[0], c[1]).then(r => (r ? c[0] : false))),
|
|
23
|
-
)
|
|
24
|
-
.then(c => c.filter(e => e !== false))
|
|
25
|
-
.then((codecs) => {
|
|
26
|
-
if (this.getState() !== 'getting_codecs')
|
|
27
|
-
throw new WebRTCError(ErrorTypes.STATE_ERROR, 'closed')
|
|
28
|
-
|
|
29
|
-
this.callbacks.onCodecsDetected(codecs as string[])
|
|
30
|
-
})
|
|
31
|
-
.catch(err => this.callbacks.onError(err))
|
|
32
|
-
}
|
|
33
|
-
}
|
package/src/core/connection.ts
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import type { ParsedOffer } from '../utils/sdp'
|
|
2
|
-
import type { State } from '~/types'
|
|
3
|
-
import { ErrorTypes, WebRTCError } from '~/errors'
|
|
4
|
-
import { SdpUtils } from '../utils/sdp'
|
|
5
|
-
|
|
6
|
-
export interface ConnectionManagerCallbacks {
|
|
7
|
-
onCandidate: (candidate: RTCIceCandidate) => void
|
|
8
|
-
onTrack: (evt: RTCTrackEvent) => void
|
|
9
|
-
onError: (err: WebRTCError) => void
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export class ConnectionManager {
|
|
13
|
-
private pc?: RTCPeerConnection
|
|
14
|
-
private offerData?: ParsedOffer
|
|
15
|
-
|
|
16
|
-
constructor(
|
|
17
|
-
private getState: () => State,
|
|
18
|
-
private callbacks: ConnectionManagerCallbacks,
|
|
19
|
-
private nonAdvertisedCodecs: string[] = [],
|
|
20
|
-
) {}
|
|
21
|
-
|
|
22
|
-
async setupPeerConnection(iceServers: RTCIceServer[]): Promise<string> {
|
|
23
|
-
if (this.getState() !== 'running')
|
|
24
|
-
throw new WebRTCError(ErrorTypes.STATE_ERROR, 'closed')
|
|
25
|
-
|
|
26
|
-
const pc = new RTCPeerConnection({
|
|
27
|
-
iceServers,
|
|
28
|
-
sdpSemantics: 'unified-plan',
|
|
29
|
-
})
|
|
30
|
-
this.pc = pc
|
|
31
|
-
|
|
32
|
-
const direction: RTCRtpTransceiverDirection = 'recvonly'
|
|
33
|
-
pc.addTransceiver('video', { direction })
|
|
34
|
-
pc.addTransceiver('audio', { direction })
|
|
35
|
-
|
|
36
|
-
pc.onicecandidate = (evt: RTCPeerConnectionIceEvent) => this.onLocalCandidate(evt)
|
|
37
|
-
pc.onconnectionstatechange = () => this.onConnectionState()
|
|
38
|
-
pc.oniceconnectionstatechange = () => this.onIceConnectionState()
|
|
39
|
-
pc.ontrack = (evt: RTCTrackEvent) => this.callbacks.onTrack(evt)
|
|
40
|
-
|
|
41
|
-
return pc.createOffer().then((offer) => {
|
|
42
|
-
if (!offer.sdp)
|
|
43
|
-
throw new WebRTCError(ErrorTypes.SIGNAL_ERROR, 'Failed to create offer SDP')
|
|
44
|
-
|
|
45
|
-
offer.sdp = SdpUtils.editOffer(offer.sdp, this.nonAdvertisedCodecs)
|
|
46
|
-
this.offerData = SdpUtils.parseOffer(offer.sdp)
|
|
47
|
-
|
|
48
|
-
return pc.setLocalDescription(offer).then(() => offer.sdp!)
|
|
49
|
-
})
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async setAnswer(answer: string): Promise<void> {
|
|
53
|
-
if (this.getState() !== 'running')
|
|
54
|
-
throw new WebRTCError(ErrorTypes.STATE_ERROR, 'closed')
|
|
55
|
-
|
|
56
|
-
return this.pc!.setRemoteDescription(
|
|
57
|
-
new RTCSessionDescription({
|
|
58
|
-
type: 'answer',
|
|
59
|
-
sdp: answer,
|
|
60
|
-
}),
|
|
61
|
-
)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
getPeerConnection(): RTCPeerConnection | undefined {
|
|
65
|
-
return this.pc
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
getOfferData(): ParsedOffer | undefined {
|
|
69
|
-
return this.offerData
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
close(): void {
|
|
73
|
-
this.pc?.close()
|
|
74
|
-
this.pc = undefined
|
|
75
|
-
this.offerData = undefined
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
private onLocalCandidate(evt: RTCPeerConnectionIceEvent): void {
|
|
79
|
-
if (this.getState() !== 'running')
|
|
80
|
-
return
|
|
81
|
-
|
|
82
|
-
if (evt.candidate)
|
|
83
|
-
this.callbacks.onCandidate(evt.candidate)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
private onConnectionState(): void {
|
|
87
|
-
if (this.getState() !== 'running' || !this.pc)
|
|
88
|
-
return
|
|
89
|
-
|
|
90
|
-
if (this.pc.connectionState === 'failed' || this.pc.connectionState === 'closed')
|
|
91
|
-
this.callbacks.onError(new WebRTCError(ErrorTypes.OTHER_ERROR, 'peer connection closed'))
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
private onIceConnectionState(): void {
|
|
95
|
-
if (this.getState() !== 'running' || !this.pc)
|
|
96
|
-
return
|
|
97
|
-
|
|
98
|
-
if (this.pc.iceConnectionState === 'failed') {
|
|
99
|
-
console.warn('ICE connection failed')
|
|
100
|
-
this.pc.restartIce()
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
package/src/core/http.ts
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import type { ParsedOffer } from '../utils/sdp'
|
|
2
|
-
import type { Conf, State } from '~/types'
|
|
3
|
-
import { ErrorTypes, WebRTCError } from '~/errors'
|
|
4
|
-
import { SdpUtils } from '../utils/sdp'
|
|
5
|
-
import { WebRtcUtils } from '../utils/webrtc'
|
|
6
|
-
|
|
7
|
-
export class HttpClient {
|
|
8
|
-
constructor(
|
|
9
|
-
private config: Conf,
|
|
10
|
-
private getState: () => State,
|
|
11
|
-
private onError: (err: Error | WebRTCError) => void,
|
|
12
|
-
) {}
|
|
13
|
-
|
|
14
|
-
private authHeader(): Record<string, string> {
|
|
15
|
-
if (this.config.user && this.config.user !== '') {
|
|
16
|
-
const credentials = btoa(`${this.config.user}:${this.config.pass}`)
|
|
17
|
-
return { Authorization: `Basic ${credentials}` }
|
|
18
|
-
}
|
|
19
|
-
if (this.config.token && this.config.token !== '') {
|
|
20
|
-
return { Authorization: `Bearer ${this.config.token}` }
|
|
21
|
-
}
|
|
22
|
-
return {}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async requestICEServers(): Promise<RTCIceServer[]> {
|
|
26
|
-
if (this.config.iceServers && this.config.iceServers.length > 0)
|
|
27
|
-
return this.config.iceServers
|
|
28
|
-
|
|
29
|
-
return fetch(this.config.url, {
|
|
30
|
-
method: 'OPTIONS',
|
|
31
|
-
headers: {
|
|
32
|
-
...this.authHeader(),
|
|
33
|
-
},
|
|
34
|
-
}).then(res => WebRtcUtils.linkToIceServers(res.headers.get('Link')))
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
async sendOffer(offer: string): Promise<{ sessionUrl?: string, answer: string }> {
|
|
38
|
-
if (this.getState() !== 'running')
|
|
39
|
-
throw new WebRTCError(ErrorTypes.STATE_ERROR, 'closed')
|
|
40
|
-
|
|
41
|
-
return fetch(this.config.url, {
|
|
42
|
-
method: 'POST',
|
|
43
|
-
headers: {
|
|
44
|
-
...this.authHeader(),
|
|
45
|
-
'Content-Type': 'application/sdp',
|
|
46
|
-
},
|
|
47
|
-
body: offer,
|
|
48
|
-
}).then((res) => {
|
|
49
|
-
switch (res.status) {
|
|
50
|
-
case 201:
|
|
51
|
-
break
|
|
52
|
-
case 404:
|
|
53
|
-
throw new WebRTCError(ErrorTypes.NETWORK_ERROR, 'stream not found')
|
|
54
|
-
case 406:
|
|
55
|
-
throw new WebRTCError(ErrorTypes.NETWORK_ERROR, 'stream not supported')
|
|
56
|
-
case 400:
|
|
57
|
-
return res.json().then((e: { error: string }) => {
|
|
58
|
-
throw new WebRTCError(ErrorTypes.NETWORK_ERROR, e.error)
|
|
59
|
-
})
|
|
60
|
-
default:
|
|
61
|
-
throw new WebRTCError(ErrorTypes.NETWORK_ERROR, `bad status code ${res.status}`)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const location = res.headers.get('Location')
|
|
65
|
-
const sessionUrl = location
|
|
66
|
-
? new URL(location, this.config.url).toString()
|
|
67
|
-
: undefined
|
|
68
|
-
return res.text().then(answer => ({ sessionUrl, answer }))
|
|
69
|
-
})
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
sendLocalCandidates(sessionUrl: string, offerData: ParsedOffer, candidates: RTCIceCandidate[]): void {
|
|
73
|
-
fetch(sessionUrl, {
|
|
74
|
-
method: 'PATCH',
|
|
75
|
-
headers: {
|
|
76
|
-
'Content-Type': 'application/trickle-ice-sdpfrag',
|
|
77
|
-
'If-Match': '*',
|
|
78
|
-
},
|
|
79
|
-
body: SdpUtils.generateSdpFragment(offerData, candidates),
|
|
80
|
-
})
|
|
81
|
-
.then((res) => {
|
|
82
|
-
switch (res.status) {
|
|
83
|
-
case 204:
|
|
84
|
-
break
|
|
85
|
-
case 404:
|
|
86
|
-
throw new WebRTCError(ErrorTypes.NETWORK_ERROR, 'stream not found')
|
|
87
|
-
default:
|
|
88
|
-
throw new WebRTCError(ErrorTypes.NETWORK_ERROR, `bad status code ${res.status}`)
|
|
89
|
-
}
|
|
90
|
-
})
|
|
91
|
-
.catch(err => this.onError(err))
|
|
92
|
-
}
|
|
93
|
-
}
|
package/src/core/track.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
export class TrackManager {
|
|
2
|
-
private stream?: MediaStream
|
|
3
|
-
private observer?: IntersectionObserver
|
|
4
|
-
|
|
5
|
-
constructor(private container: HTMLMediaElement) {}
|
|
6
|
-
|
|
7
|
-
onTrack(evt: RTCTrackEvent): void {
|
|
8
|
-
this.stream = evt.streams[0]
|
|
9
|
-
|
|
10
|
-
// 停止之前的观察器
|
|
11
|
-
this.stopObserver()
|
|
12
|
-
|
|
13
|
-
// 创建新的可见性观察器,自动处理暂停/恢复
|
|
14
|
-
this.observer = new IntersectionObserver(
|
|
15
|
-
([entry]) => {
|
|
16
|
-
if (entry.isIntersecting)
|
|
17
|
-
this.resume()
|
|
18
|
-
else
|
|
19
|
-
this.pause()
|
|
20
|
-
},
|
|
21
|
-
{ threshold: 0.5 },
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
this.observer.observe(this.container)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
private stopObserver(): void {
|
|
28
|
-
if (this.observer) {
|
|
29
|
-
this.observer.disconnect()
|
|
30
|
-
this.observer = undefined
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
get paused(): boolean {
|
|
35
|
-
return this.container.srcObject === null
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
pause(): void {
|
|
39
|
-
this.container.srcObject = null
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
resume(): void {
|
|
43
|
-
if (this.stream && this.paused)
|
|
44
|
-
this.container.srcObject = this.stream
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
stop(): void {
|
|
48
|
-
this.stopObserver()
|
|
49
|
-
this.stream = undefined
|
|
50
|
-
}
|
|
51
|
-
}
|
package/src/errors.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 错误类型
|
|
3
|
-
*/
|
|
4
|
-
export type ErrorType = string
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 错误类型常量
|
|
8
|
-
*/
|
|
9
|
-
export const ErrorTypes = {
|
|
10
|
-
SIGNAL_ERROR: 'SignalError', // 信令异常
|
|
11
|
-
STATE_ERROR: 'StateError', // 状态异常
|
|
12
|
-
NETWORK_ERROR: 'NetworkError',
|
|
13
|
-
MEDIA_ERROR: 'MediaError',
|
|
14
|
-
OTHER_ERROR: 'OtherError',
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 错误
|
|
19
|
-
*/
|
|
20
|
-
export class WebRTCError extends Error {
|
|
21
|
-
type: ErrorType
|
|
22
|
-
|
|
23
|
-
constructor(type: ErrorType, message: string, options?: ErrorOptions) {
|
|
24
|
-
super(message, options)
|
|
25
|
-
this.type = type
|
|
26
|
-
}
|
|
27
|
-
}
|
package/src/index.ts
DELETED
package/src/types.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import type { WebRTCError } from './errors'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Configuration interface for WebRTCWhep.
|
|
5
|
-
*/
|
|
6
|
-
export interface Conf {
|
|
7
|
-
/** Absolute URL of the WHEP endpoint */
|
|
8
|
-
url: string
|
|
9
|
-
/** Media player container */
|
|
10
|
-
container: HTMLMediaElement
|
|
11
|
-
/** Username for authentication */
|
|
12
|
-
user?: string
|
|
13
|
-
/** Password for authentication */
|
|
14
|
-
pass?: string
|
|
15
|
-
/** Token for authentication */
|
|
16
|
-
token?: string
|
|
17
|
-
/** ice server list */
|
|
18
|
-
iceServers?: RTCIceServer[]
|
|
19
|
-
/** Called when there's an error */
|
|
20
|
-
onError?: (err: WebRTCError) => void
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* State type for WebRTCWhep.
|
|
25
|
-
*/
|
|
26
|
-
export type State = 'getting_codecs' | 'running' | 'restarting' | 'closed' | 'failed'
|
|
27
|
-
|
|
28
|
-
/** Extend RTCConfiguration to include experimental properties */
|
|
29
|
-
declare global {
|
|
30
|
-
interface RTCConfiguration {
|
|
31
|
-
sdpSemantics?: 'plan-b' | 'unified-plan'
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
interface RTCIceServer {
|
|
35
|
-
credentialType?: 'password'
|
|
36
|
-
}
|
|
37
|
-
}
|
package/src/utils/flow-check.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { ErrorTypes, WebRTCError } from '~/errors'
|
|
2
|
-
|
|
3
|
-
export interface FlowCheckParams {
|
|
4
|
-
interval: number
|
|
5
|
-
onError: (err: WebRTCError) => void
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Flow checking logic (断流检测)
|
|
10
|
-
*/
|
|
11
|
-
export class FlowCheck {
|
|
12
|
-
private checkInterval: number
|
|
13
|
-
private lastBytesReceived: number = 0
|
|
14
|
-
private checkTimer?: NodeJS.Timeout
|
|
15
|
-
private pc?: RTCPeerConnection
|
|
16
|
-
private onError: (err: WebRTCError) => void
|
|
17
|
-
|
|
18
|
-
constructor(params: FlowCheckParams) {
|
|
19
|
-
this.checkInterval = params.interval
|
|
20
|
-
this.onError = params.onError
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
setPeerConnection(pc: RTCPeerConnection) {
|
|
24
|
-
this.pc = pc
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 启动断流检测
|
|
29
|
-
*/
|
|
30
|
-
start(): void {
|
|
31
|
-
this.stop()
|
|
32
|
-
this.checkTimer = setInterval(() => this.checkFlowState(), this.checkInterval)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* 停止断流检测
|
|
37
|
-
*/
|
|
38
|
-
stop(): void {
|
|
39
|
-
if (this.checkTimer) {
|
|
40
|
-
clearInterval(this.checkTimer)
|
|
41
|
-
this.checkTimer = undefined
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* 检测流状态(通过接收字节数判断是否断流)
|
|
47
|
-
*/
|
|
48
|
-
private async checkFlowState(): Promise<void> {
|
|
49
|
-
if (!this.pc) {
|
|
50
|
-
return
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const stats = await this.pc.getStats()
|
|
54
|
-
let currentBytes = 0
|
|
55
|
-
|
|
56
|
-
// 遍历统计信息,获取视频接收字节数
|
|
57
|
-
stats.forEach((stat: RTCStats) => {
|
|
58
|
-
const inboundRtpStat = stat as RTCInboundRtpStreamStats
|
|
59
|
-
if (stat.type === 'inbound-rtp' && inboundRtpStat.kind === 'video') {
|
|
60
|
-
currentBytes = inboundRtpStat.bytesReceived || 0
|
|
61
|
-
}
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
// 断流判定:连接正常但字节数无变化
|
|
65
|
-
if (currentBytes === this.lastBytesReceived && this.pc.connectionState === 'connected') {
|
|
66
|
-
this.onError(new WebRTCError(ErrorTypes.NETWORK_ERROR, 'data stream interruption'))
|
|
67
|
-
return
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// 更新上一次字节数
|
|
71
|
-
this.lastBytesReceived = currentBytes
|
|
72
|
-
}
|
|
73
|
-
}
|