whepts 1.0.2 → 1.0.3
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/.claude/settings.local.json +9 -0
- package/AGENTS.md +92 -0
- package/dist/core/codec.d.ts +11 -0
- package/dist/core/connection.d.ts +24 -0
- package/dist/core/http.d.ts +16 -0
- package/dist/core/track.d.ts +12 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -1
- package/dist/types.d.ts +33 -0
- package/dist/whep.d.ts +8 -98
- package/package.json +8 -9
- package/src/core/codec.ts +33 -0
- package/src/core/connection.ts +103 -0
- package/src/core/http.ts +93 -0
- package/src/core/track.ts +51 -0
- package/src/index.ts +3 -2
- package/src/types.ts +37 -0
- package/src/whep.ts +76 -305
- package/QWEN.md +0 -118
- package/dist/utils/observer.d.ts +0 -8
- package/src/utils/observer.ts +0 -28
package/AGENTS.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
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
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { State } from '~/types';
|
|
2
|
+
export interface CodecDetectorCallbacks {
|
|
3
|
+
onCodecsDetected: (codecs: string[]) => void;
|
|
4
|
+
onError: (err: Error) => void;
|
|
5
|
+
}
|
|
6
|
+
export declare class CodecDetector {
|
|
7
|
+
private getState;
|
|
8
|
+
private callbacks;
|
|
9
|
+
constructor(getState: () => State, callbacks: CodecDetectorCallbacks);
|
|
10
|
+
detect(): void;
|
|
11
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ParsedOffer } from '../utils/sdp';
|
|
2
|
+
import type { State } from '~/types';
|
|
3
|
+
import { WebRTCError } from '~/errors';
|
|
4
|
+
export interface ConnectionManagerCallbacks {
|
|
5
|
+
onCandidate: (candidate: RTCIceCandidate) => void;
|
|
6
|
+
onTrack: (evt: RTCTrackEvent) => void;
|
|
7
|
+
onError: (err: WebRTCError) => void;
|
|
8
|
+
}
|
|
9
|
+
export declare class ConnectionManager {
|
|
10
|
+
private getState;
|
|
11
|
+
private callbacks;
|
|
12
|
+
private nonAdvertisedCodecs;
|
|
13
|
+
private pc?;
|
|
14
|
+
private offerData?;
|
|
15
|
+
constructor(getState: () => State, callbacks: ConnectionManagerCallbacks, nonAdvertisedCodecs?: string[]);
|
|
16
|
+
setupPeerConnection(iceServers: RTCIceServer[]): Promise<string>;
|
|
17
|
+
setAnswer(answer: string): Promise<void>;
|
|
18
|
+
getPeerConnection(): RTCPeerConnection | undefined;
|
|
19
|
+
getOfferData(): ParsedOffer | undefined;
|
|
20
|
+
close(): void;
|
|
21
|
+
private onLocalCandidate;
|
|
22
|
+
private onConnectionState;
|
|
23
|
+
private onIceConnectionState;
|
|
24
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ParsedOffer } from '../utils/sdp';
|
|
2
|
+
import type { Conf, State } from '~/types';
|
|
3
|
+
import { WebRTCError } from '~/errors';
|
|
4
|
+
export declare class HttpClient {
|
|
5
|
+
private config;
|
|
6
|
+
private getState;
|
|
7
|
+
private onError;
|
|
8
|
+
constructor(config: Conf, getState: () => State, onError: (err: Error | WebRTCError) => void);
|
|
9
|
+
private authHeader;
|
|
10
|
+
requestICEServers(): Promise<RTCIceServer[]>;
|
|
11
|
+
sendOffer(offer: string): Promise<{
|
|
12
|
+
sessionUrl?: string;
|
|
13
|
+
answer: string;
|
|
14
|
+
}>;
|
|
15
|
+
sendLocalCandidates(sessionUrl: string, offerData: ParsedOffer, candidates: RTCIceCandidate[]): void;
|
|
16
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare class TrackManager {
|
|
2
|
+
private container;
|
|
3
|
+
private stream?;
|
|
4
|
+
private observer?;
|
|
5
|
+
constructor(container: HTMLMediaElement);
|
|
6
|
+
onTrack(evt: RTCTrackEvent): void;
|
|
7
|
+
private stopObserver;
|
|
8
|
+
get paused(): boolean;
|
|
9
|
+
pause(): void;
|
|
10
|
+
resume(): void;
|
|
11
|
+
stop(): void;
|
|
12
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
+
import type { Conf, State } from './types';
|
|
1
2
|
import { ErrorTypes, WebRTCError } from './errors';
|
|
2
|
-
import WebRTCWhep
|
|
3
|
-
export { Conf, ErrorTypes, WebRTCError };
|
|
3
|
+
import WebRTCWhep from './whep';
|
|
4
|
+
export { Conf, ErrorTypes, State, WebRTCError };
|
|
4
5
|
export default WebRTCWhep;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e={SIGNAL_ERROR:"SignalError",STATE_ERROR:"StateError",NETWORK_ERROR:"NetworkError",MEDIA_ERROR:"MediaError",OTHER_ERROR:"OtherError"};class t extends Error{constructor(e,t,s){super(t,s),this.type=e}}class s{start(e,t){e&&(this.stop(),this.observer=new IntersectionObserver(([e])=>t(e.isIntersecting),{threshold:.5}),this.observer.observe(e))}stop(){this.observer&&(this.observer.disconnect(),this.observer=void 0)}}class r{constructor(e){this.lastBytesReceived=0,this.checkInterval=e.interval,this.onError=e.onError}setPeerConnection(e){this.pc=e}start(){this.stop(),this.checkTimer=setInterval(()=>this.checkFlowState(),this.checkInterval)}stop(){this.checkTimer&&(clearInterval(this.checkTimer),this.checkTimer=void 0)}async checkFlowState(){if(!this.pc)return;const s=await this.pc.getStats();let r=0;s.forEach(e=>{const t=e;"inbound-rtp"===e.type&&"video"===t.kind&&(r=t.bytesReceived||0)}),r!==this.lastBytesReceived||"connected"!==this.pc.connectionState?this.lastBytesReceived=r:this.onError(new t(e.NETWORK_ERROR,"data stream interruption"))}}class n{static parseOffer(e){const t={iceUfrag:"",icePwd:"",medias:[]};for(const s of e.split("\r\n"))s.startsWith("m=")?t.medias.push(s.slice(2)):""===t.iceUfrag&&s.startsWith("a=ice-ufrag:")?t.iceUfrag=s.slice(12):""===t.icePwd&&s.startsWith("a=ice-pwd:")&&(t.icePwd=s.slice(10));return t}static reservePayloadType(e){for(let t=30;t<=127;t++)if((t<=63||t>=96)&&!e.includes(t.toString())){const s=t.toString();return e.push(s),s}throw new Error("unable to find a free payload type")}static enableStereoPcmau(e,t){const s=t.split("\r\n");let r=n.reservePayloadType(e);return s[0]+=` ${r}`,s.splice(s.length-1,0,`a=rtpmap:${r} PCMU/8000/2`),s.splice(s.length-1,0,`a=rtcp-fb:${r} transport-cc`),r=n.reservePayloadType(e),s[0]+=` ${r}`,s.splice(s.length-1,0,`a=rtpmap:${r} PCMA/8000/2`),s.splice(s.length-1,0,`a=rtcp-fb:${r} transport-cc`),s.join("\r\n")}static enableMultichannelOpus(e,t){const s=t.split("\r\n");let r=n.reservePayloadType(e);return s[0]+=` ${r}`,s.splice(s.length-1,0,`a=rtpmap:${r} multiopus/48000/3`),s.splice(s.length-1,0,`a=fmtp:${r} channel_mapping=0,2,1;num_streams=2;coupled_streams=1`),s.splice(s.length-1,0,`a=rtcp-fb:${r} transport-cc`),r=n.reservePayloadType(e),s[0]+=` ${r}`,s.splice(s.length-1,0,`a=rtpmap:${r} multiopus/48000/4`),s.splice(s.length-1,0,`a=fmtp:${r} channel_mapping=0,1,2,3;num_streams=2;coupled_streams=2`),s.splice(s.length-1,0,`a=rtcp-fb:${r} transport-cc`),r=n.reservePayloadType(e),s[0]+=` ${r}`,s.splice(s.length-1,0,`a=rtpmap:${r} multiopus/48000/5`),s.splice(s.length-1,0,`a=fmtp:${r} channel_mapping=0,4,1,2,3;num_streams=3;coupled_streams=2`),s.splice(s.length-1,0,`a=rtcp-fb:${r} transport-cc`),r=n.reservePayloadType(e),s[0]+=` ${r}`,s.splice(s.length-1,0,`a=rtpmap:${r} multiopus/48000/6`),s.splice(s.length-1,0,`a=fmtp:${r} channel_mapping=0,4,1,2,3,5;num_streams=4;coupled_streams=2`),s.splice(s.length-1,0,`a=rtcp-fb:${r} transport-cc`),r=n.reservePayloadType(e),s[0]+=` ${r}`,s.splice(s.length-1,0,`a=rtpmap:${r} multiopus/48000/7`),s.splice(s.length-1,0,`a=fmtp:${r} channel_mapping=0,4,1,2,3,5,6;num_streams=4;coupled_streams=4`),s.splice(s.length-1,0,`a=rtcp-fb:${r} transport-cc`),r=n.reservePayloadType(e),s[0]+=` ${r}`,s.splice(s.length-1,0,`a=rtpmap:${r} multiopus/48000/8`),s.splice(s.length-1,0,`a=fmtp:${r} channel_mapping=0,6,1,4,5,2,3,7;num_streams=5;coupled_streams=4`),s.splice(s.length-1,0,`a=rtcp-fb:${r} transport-cc`),s.join("\r\n")}static enableL16(e,t){const s=t.split("\r\n");let r=n.reservePayloadType(e);return s[0]+=` ${r}`,s.splice(s.length-1,0,`a=rtpmap:${r} L16/8000/2`),s.splice(s.length-1,0,`a=rtcp-fb:${r} transport-cc`),r=n.reservePayloadType(e),s[0]+=` ${r}`,s.splice(s.length-1,0,`a=rtpmap:${r} L16/16000/2`),s.splice(s.length-1,0,`a=rtcp-fb:${r} transport-cc`),r=n.reservePayloadType(e),s[0]+=` ${r}`,s.splice(s.length-1,0,`a=rtpmap:${r} L16/48000/2`),s.splice(s.length-1,0,`a=rtcp-fb:${r} transport-cc`),s.join("\r\n")}static enableStereoOpus(e){let t="";const s=e.split("\r\n");for(let e=0;e<s.length;e++)if(s[e].startsWith("a=rtpmap:")&&s[e].toLowerCase().includes("opus/")){t=s[e].slice(9).split(" ")[0];break}if(""===t)return e;for(let e=0;e<s.length;e++)s[e].startsWith(`a=fmtp:${t} `)&&(s[e].includes("stereo")||(s[e]+=";stereo=1"),s[e].includes("sprop-stereo")||(s[e]+=";sprop-stereo=1"));return s.join("\r\n")}static editOffer(e,t){const s=e.split("m="),r=s.slice(1).map(e=>e.split("\r\n")[0].split(" ").slice(3)).reduce((e,t)=>[...e,...t],[]);for(let e=1;e<s.length;e++)if(s[e].startsWith("audio")){s[e]=n.enableStereoOpus(s[e]),t.includes("pcma/8000/2")&&(s[e]=n.enableStereoPcmau(r,s[e])),t.includes("multiopus/48000/6")&&(s[e]=n.enableMultichannelOpus(r,s[e])),t.includes("L16/48000/2")&&(s[e]=n.enableL16(r,s[e]));break}return s.join("m=")}static generateSdpFragment(e,t){const s={};for(const e of t){const t=e.sdpMLineIndex;t&&(void 0===s[t]&&(s[t]=[]),s[t].push(e))}let r=`a=ice-ufrag:${e.iceUfrag}\r\na=ice-pwd:${e.icePwd}\r\n`,n=0;for(const t of e.medias){if(void 0!==s[n]){r+=`m=${t}\r\na=mid:${n}\r\n`;for(const e of s[n])r+=`a=${e.candidate}\r\n`}n++}return r}}class i{static async supportsNonAdvertisedCodec(e,t){return new Promise(s=>{const r=new RTCPeerConnection({iceServers:[]}),n="audio";let a="";r.addTransceiver(n,{direction:"recvonly"}),r.createOffer().then(s=>{if(!s.sdp)throw new Error("SDP not present");if(s.sdp.includes(` ${e}`))throw new Error("already present");const o=s.sdp.split(`m=${n}`),c=o.slice(1).map(e=>e.split("\r\n")[0].split(" ").slice(3)).reduce((e,t)=>[...e,...t],[]);a=i.reservePayloadType(c);const h=o[1].split("\r\n");return h[0]+=` ${a}`,h.splice(h.length-1,0,`a=rtpmap:${a} ${e}`),void 0!==t&&h.splice(h.length-1,0,`a=fmtp:${a} ${t}`),o[1]=h.join("\r\n"),s.sdp=o.join(`m=${n}`),r.setLocalDescription(s)}).then(()=>r.setRemoteDescription(new RTCSessionDescription({type:"answer",sdp:`v=0\r\no=- 6539324223450680508 0 IN IP4 0.0.0.0\r\ns=-\r\nt=0 0\r\na=fingerprint:sha-256 0D:9F:78:15:42:B5:4B:E6:E2:94:3E:5B:37:78:E1:4B:54:59:A3:36:3A:E5:05:EB:27:EE:8F:D2:2D:41:29:25\r\nm=${n} 9 UDP/TLS/RTP/SAVPF ${a}\r\nc=IN IP4 0.0.0.0\r\na=ice-pwd:7c3bf4770007e7432ee4ea4d697db675\r\na=ice-ufrag:29e036dc\r\na=sendonly\r\na=rtcp-mux\r\na=rtpmap:${a} ${e}\r\n${void 0!==t?`a=fmtp:${a} ${t}\r\n`:""}`}))).then(()=>s(!0)).catch(()=>s(!1)).finally(()=>r.close())})}static unquoteCredential(e){return JSON.parse(`"${e}"`)}static linkToIceServers(s){return s?s.split(", ").map(s=>{const r=s.match(/^<(.+?)>; rel="ice-server"(; username="(.*?)"; credential="(.*?)"; credential-type="password")?/i);if(!r)throw new t(e.SIGNAL_ERROR,"Invalid ICE server link format");const n={urls:[r[1]]};return r[3]&&(n.username=i.unquoteCredential(r[3]),n.credential=i.unquoteCredential(r[4]),n.credentialType="password"),n}):[]}static reservePayloadType(e){for(let t=30;t<=127;t++)if((t<=63||t>=96)&&!e.includes(t.toString())){const s=t.toString();return e.push(s),s}throw new Error("unable to find a free payload type")}}class a{constructor(e){this.retryPause=2e3,this.queuedCandidates=[],this.nonAdvertisedCodecs=[],this.conf=e,this.state="getting_codecs",this.observer=new s,this.flowCheck=new r({interval:5e3,onError:e=>this.handleError(e)}),this.getNonAdvertisedCodecs()}get isRunning(){return"running"===this.state}close(){this.state="closed",this.pc?.close(),this.observer.stop(),this.flowCheck.stop(),this.restartTimeout&&clearTimeout(this.restartTimeout)}handleError(s){this.flowCheck.stop(),"getting_codecs"===this.state||s instanceof t&&s.type===e.SIGNAL_ERROR?this.state="failed":"running"===this.state&&(this.pc?.close(),this.pc=void 0,this.offerData=void 0,this.queuedCandidates=[],this.sessionUrl&&(fetch(this.sessionUrl,{method:"DELETE"}),this.sessionUrl=void 0),this.state="restarting",this.restartTimeout=setTimeout(()=>{this.restartTimeout=void 0,this.state="running",this.start()},this.retryPause),s.message=`${s.message}, retrying in some seconds`),this.conf.onError&&(s instanceof t?this.conf.onError(s):this.conf.onError(new t(e.OTHER_ERROR,s.message)))}getNonAdvertisedCodecs(){Promise.all([["pcma/8000/2"],["multiopus/48000/6","channel_mapping=0,4,1,2,3,5;num_streams=4;coupled_streams=2"],["L16/48000/2"]].map(e=>i.supportsNonAdvertisedCodec(e[0],e[1]).then(t=>!!t&&e[0]))).then(e=>e.filter(e=>!1!==e)).then(s=>{if("getting_codecs"!==this.state)throw new t(e.STATE_ERROR,"closed");this.nonAdvertisedCodecs=s,this.state="running",this.start()}).catch(e=>this.handleError(e))}start(){this.requestICEServers().then(e=>this.setupPeerConnection(e)).then(e=>this.sendOffer(e)).then(e=>this.setAnswer(e)).catch(e=>this.handleError(e))}authHeader(){if(this.conf.user&&""!==this.conf.user){return{Authorization:`Basic ${btoa(`${this.conf.user}:${this.conf.pass}`)}`}}return this.conf.token&&""!==this.conf.token?{Authorization:`Bearer ${this.conf.token}`}:{}}async requestICEServers(){return this.conf.iceServers&&this.conf.iceServers.length>0?this.conf.iceServers:fetch(this.conf.url,{method:"OPTIONS",headers:{...this.authHeader()}}).then(e=>i.linkToIceServers(e.headers.get("Link")))}async setupPeerConnection(s){if("running"!==this.state)throw new t(e.STATE_ERROR,"closed");const r=new RTCPeerConnection({iceServers:s,sdpSemantics:"unified-plan"});this.pc=r,this.flowCheck.setPeerConnection(r);const i="recvonly";return r.addTransceiver("video",{direction:i}),r.addTransceiver("audio",{direction:i}),r.onicecandidate=e=>this.onLocalCandidate(e),r.onconnectionstatechange=()=>this.onConnectionState(),r.ontrack=e=>this.onTrack(e),r.createOffer().then(s=>{if(!s.sdp)throw new t(e.SIGNAL_ERROR,"Failed to create offer SDP");return s.sdp=n.editOffer(s.sdp,this.nonAdvertisedCodecs),this.offerData=n.parseOffer(s.sdp),r.setLocalDescription(s).then(()=>s.sdp)})}sendOffer(s){if("running"!==this.state)throw new t(e.STATE_ERROR,"closed");return fetch(this.conf.url,{method:"POST",headers:{...this.authHeader(),"Content-Type":"application/sdp"},body:s}).then(s=>{switch(s.status){case 201:break;case 404:throw new t(e.NETWORK_ERROR,"stream not found");case 406:throw new t(e.NETWORK_ERROR,"stream not supported");case 400:return s.json().then(s=>{throw new t(e.NETWORK_ERROR,s.error)});default:throw new t(e.NETWORK_ERROR,`bad status code ${s.status}`)}const r=s.headers.get("Location");return r&&(this.sessionUrl=new URL(r,this.conf.url).toString()),s.text()})}setAnswer(s){if("running"!==this.state)throw new t(e.STATE_ERROR,"closed");return this.pc.setRemoteDescription(new RTCSessionDescription({type:"answer",sdp:s})).then(()=>{"running"===this.state&&0!==this.queuedCandidates.length&&(this.sendLocalCandidates(this.queuedCandidates),this.queuedCandidates=[])})}onLocalCandidate(e){"running"===this.state&&e.candidate&&(this.sessionUrl?this.sendLocalCandidates([e.candidate]):this.queuedCandidates.push(e.candidate))}sendLocalCandidates(s){this.sessionUrl&&this.offerData&&fetch(this.sessionUrl,{method:"PATCH",headers:{"Content-Type":"application/trickle-ice-sdpfrag","If-Match":"*"},body:n.generateSdpFragment(this.offerData,s)}).then(s=>{switch(s.status){case 204:break;case 404:throw new t(e.NETWORK_ERROR,"stream not found");default:throw new t(e.NETWORK_ERROR,`bad status code ${s.status}`)}}).catch(e=>this.handleError(e))}onConnectionState(){"running"===this.state&&this.pc&&("failed"===this.pc.connectionState||"closed"===this.pc.connectionState?this.handleError(new t(e.OTHER_ERROR,"peer connection closed")):"connected"===this.pc.connectionState&&this.flowCheck.start())}onTrack(e){this.stream=e.streams[0],this.observer.start(this.conf.container,e=>{e?this.resume():this.pause()})}get paused(){return null===this.conf.container.srcObject}pause(){this.conf.container.srcObject=null}resume(){this.stream&&this.paused&&(this.conf.container.srcObject=this.stream)}}export{e as ErrorTypes,t as WebRTCError,a as default};
|
|
1
|
+
const e={SIGNAL_ERROR:"SignalError",STATE_ERROR:"StateError",NETWORK_ERROR:"NetworkError",MEDIA_ERROR:"MediaError",OTHER_ERROR:"OtherError"};class t extends Error{constructor(e,t,s){super(t,s),this.type=e}}class s{static async supportsNonAdvertisedCodec(e,t){return new Promise(n=>{const r=new RTCPeerConnection({iceServers:[]}),i="audio";let a="";r.addTransceiver(i,{direction:"recvonly"}),r.createOffer().then(n=>{if(!n.sdp)throw new Error("SDP not present");if(n.sdp.includes(` ${e}`))throw new Error("already present");const c=n.sdp.split(`m=${i}`),o=c.slice(1).map(e=>e.split("\r\n")[0].split(" ").slice(3)).reduce((e,t)=>[...e,...t],[]);a=s.reservePayloadType(o);const h=c[1].split("\r\n");return h[0]+=` ${a}`,h.splice(h.length-1,0,`a=rtpmap:${a} ${e}`),void 0!==t&&h.splice(h.length-1,0,`a=fmtp:${a} ${t}`),c[1]=h.join("\r\n"),n.sdp=c.join(`m=${i}`),r.setLocalDescription(n)}).then(()=>r.setRemoteDescription(new RTCSessionDescription({type:"answer",sdp:`v=0\r\no=- 6539324223450680508 0 IN IP4 0.0.0.0\r\ns=-\r\nt=0 0\r\na=fingerprint:sha-256 0D:9F:78:15:42:B5:4B:E6:E2:94:3E:5B:37:78:E1:4B:54:59:A3:36:3A:E5:05:EB:27:EE:8F:D2:2D:41:29:25\r\nm=${i} 9 UDP/TLS/RTP/SAVPF ${a}\r\nc=IN IP4 0.0.0.0\r\na=ice-pwd:7c3bf4770007e7432ee4ea4d697db675\r\na=ice-ufrag:29e036dc\r\na=sendonly\r\na=rtcp-mux\r\na=rtpmap:${a} ${e}\r\n${void 0!==t?`a=fmtp:${a} ${t}\r\n`:""}`}))).then(()=>n(!0)).catch(()=>n(!1)).finally(()=>r.close())})}static unquoteCredential(e){return JSON.parse(`"${e}"`)}static linkToIceServers(n){return n?n.split(", ").map(n=>{const r=n.match(/^<(.+?)>; rel="ice-server"(; username="(.*?)"; credential="(.*?)"; credential-type="password")?/i);if(!r)throw new t(e.SIGNAL_ERROR,"Invalid ICE server link format");const i={urls:[r[1]]};return r[3]&&(i.username=s.unquoteCredential(r[3]),i.credential=s.unquoteCredential(r[4]),i.credentialType="password"),i}):[]}static reservePayloadType(e){for(let t=30;t<=127;t++)if((t<=63||t>=96)&&!e.includes(t.toString())){const s=t.toString();return e.push(s),s}throw new Error("unable to find a free payload type")}}class n{constructor(e,t){this.getState=e,this.callbacks=t}detect(){Promise.all([["pcma/8000/2"],["multiopus/48000/6","channel_mapping=0,4,1,2,3,5;num_streams=4;coupled_streams=2"],["L16/48000/2"]].map(e=>s.supportsNonAdvertisedCodec(e[0],e[1]).then(t=>!!t&&e[0]))).then(e=>e.filter(e=>!1!==e)).then(s=>{if("getting_codecs"!==this.getState())throw new t(e.STATE_ERROR,"closed");this.callbacks.onCodecsDetected(s)}).catch(e=>this.callbacks.onError(e))}}class r{static parseOffer(e){const t={iceUfrag:"",icePwd:"",medias:[]};for(const s of e.split("\r\n"))s.startsWith("m=")?t.medias.push(s.slice(2)):""===t.iceUfrag&&s.startsWith("a=ice-ufrag:")?t.iceUfrag=s.slice(12):""===t.icePwd&&s.startsWith("a=ice-pwd:")&&(t.icePwd=s.slice(10));return t}static reservePayloadType(e){for(let t=30;t<=127;t++)if((t<=63||t>=96)&&!e.includes(t.toString())){const s=t.toString();return e.push(s),s}throw new Error("unable to find a free payload type")}static enableStereoPcmau(e,t){const s=t.split("\r\n");let n=r.reservePayloadType(e);return s[0]+=` ${n}`,s.splice(s.length-1,0,`a=rtpmap:${n} PCMU/8000/2`),s.splice(s.length-1,0,`a=rtcp-fb:${n} transport-cc`),n=r.reservePayloadType(e),s[0]+=` ${n}`,s.splice(s.length-1,0,`a=rtpmap:${n} PCMA/8000/2`),s.splice(s.length-1,0,`a=rtcp-fb:${n} transport-cc`),s.join("\r\n")}static enableMultichannelOpus(e,t){const s=t.split("\r\n");let n=r.reservePayloadType(e);return s[0]+=` ${n}`,s.splice(s.length-1,0,`a=rtpmap:${n} multiopus/48000/3`),s.splice(s.length-1,0,`a=fmtp:${n} channel_mapping=0,2,1;num_streams=2;coupled_streams=1`),s.splice(s.length-1,0,`a=rtcp-fb:${n} transport-cc`),n=r.reservePayloadType(e),s[0]+=` ${n}`,s.splice(s.length-1,0,`a=rtpmap:${n} multiopus/48000/4`),s.splice(s.length-1,0,`a=fmtp:${n} channel_mapping=0,1,2,3;num_streams=2;coupled_streams=2`),s.splice(s.length-1,0,`a=rtcp-fb:${n} transport-cc`),n=r.reservePayloadType(e),s[0]+=` ${n}`,s.splice(s.length-1,0,`a=rtpmap:${n} multiopus/48000/5`),s.splice(s.length-1,0,`a=fmtp:${n} channel_mapping=0,4,1,2,3;num_streams=3;coupled_streams=2`),s.splice(s.length-1,0,`a=rtcp-fb:${n} transport-cc`),n=r.reservePayloadType(e),s[0]+=` ${n}`,s.splice(s.length-1,0,`a=rtpmap:${n} multiopus/48000/6`),s.splice(s.length-1,0,`a=fmtp:${n} channel_mapping=0,4,1,2,3,5;num_streams=4;coupled_streams=2`),s.splice(s.length-1,0,`a=rtcp-fb:${n} transport-cc`),n=r.reservePayloadType(e),s[0]+=` ${n}`,s.splice(s.length-1,0,`a=rtpmap:${n} multiopus/48000/7`),s.splice(s.length-1,0,`a=fmtp:${n} channel_mapping=0,4,1,2,3,5,6;num_streams=4;coupled_streams=4`),s.splice(s.length-1,0,`a=rtcp-fb:${n} transport-cc`),n=r.reservePayloadType(e),s[0]+=` ${n}`,s.splice(s.length-1,0,`a=rtpmap:${n} multiopus/48000/8`),s.splice(s.length-1,0,`a=fmtp:${n} channel_mapping=0,6,1,4,5,2,3,7;num_streams=5;coupled_streams=4`),s.splice(s.length-1,0,`a=rtcp-fb:${n} transport-cc`),s.join("\r\n")}static enableL16(e,t){const s=t.split("\r\n");let n=r.reservePayloadType(e);return s[0]+=` ${n}`,s.splice(s.length-1,0,`a=rtpmap:${n} L16/8000/2`),s.splice(s.length-1,0,`a=rtcp-fb:${n} transport-cc`),n=r.reservePayloadType(e),s[0]+=` ${n}`,s.splice(s.length-1,0,`a=rtpmap:${n} L16/16000/2`),s.splice(s.length-1,0,`a=rtcp-fb:${n} transport-cc`),n=r.reservePayloadType(e),s[0]+=` ${n}`,s.splice(s.length-1,0,`a=rtpmap:${n} L16/48000/2`),s.splice(s.length-1,0,`a=rtcp-fb:${n} transport-cc`),s.join("\r\n")}static enableStereoOpus(e){let t="";const s=e.split("\r\n");for(let e=0;e<s.length;e++)if(s[e].startsWith("a=rtpmap:")&&s[e].toLowerCase().includes("opus/")){t=s[e].slice(9).split(" ")[0];break}if(""===t)return e;for(let e=0;e<s.length;e++)s[e].startsWith(`a=fmtp:${t} `)&&(s[e].includes("stereo")||(s[e]+=";stereo=1"),s[e].includes("sprop-stereo")||(s[e]+=";sprop-stereo=1"));return s.join("\r\n")}static editOffer(e,t){const s=e.split("m="),n=s.slice(1).map(e=>e.split("\r\n")[0].split(" ").slice(3)).reduce((e,t)=>[...e,...t],[]);for(let e=1;e<s.length;e++)if(s[e].startsWith("audio")){s[e]=r.enableStereoOpus(s[e]),t.includes("pcma/8000/2")&&(s[e]=r.enableStereoPcmau(n,s[e])),t.includes("multiopus/48000/6")&&(s[e]=r.enableMultichannelOpus(n,s[e])),t.includes("L16/48000/2")&&(s[e]=r.enableL16(n,s[e]));break}return s.join("m=")}static generateSdpFragment(e,t){const s={};for(const e of t){const t=e.sdpMLineIndex;t&&(void 0===s[t]&&(s[t]=[]),s[t].push(e))}let n=`a=ice-ufrag:${e.iceUfrag}\r\na=ice-pwd:${e.icePwd}\r\n`,r=0;for(const t of e.medias){if(void 0!==s[r]){n+=`m=${t}\r\na=mid:${r}\r\n`;for(const e of s[r])n+=`a=${e.candidate}\r\n`}r++}return n}}class i{constructor(e,t,s=[]){this.getState=e,this.callbacks=t,this.nonAdvertisedCodecs=s}async setupPeerConnection(s){if("running"!==this.getState())throw new t(e.STATE_ERROR,"closed");const n=new RTCPeerConnection({iceServers:s,sdpSemantics:"unified-plan"});this.pc=n;const i="recvonly";return n.addTransceiver("video",{direction:i}),n.addTransceiver("audio",{direction:i}),n.onicecandidate=e=>this.onLocalCandidate(e),n.onconnectionstatechange=()=>this.onConnectionState(),n.oniceconnectionstatechange=()=>this.onIceConnectionState(),n.ontrack=e=>this.callbacks.onTrack(e),n.createOffer().then(s=>{if(!s.sdp)throw new t(e.SIGNAL_ERROR,"Failed to create offer SDP");return s.sdp=r.editOffer(s.sdp,this.nonAdvertisedCodecs),this.offerData=r.parseOffer(s.sdp),n.setLocalDescription(s).then(()=>s.sdp)})}async setAnswer(s){if("running"!==this.getState())throw new t(e.STATE_ERROR,"closed");return this.pc.setRemoteDescription(new RTCSessionDescription({type:"answer",sdp:s}))}getPeerConnection(){return this.pc}getOfferData(){return this.offerData}close(){this.pc?.close(),this.pc=void 0,this.offerData=void 0}onLocalCandidate(e){"running"===this.getState()&&e.candidate&&this.callbacks.onCandidate(e.candidate)}onConnectionState(){"running"===this.getState()&&this.pc&&("failed"!==this.pc.connectionState&&"closed"!==this.pc.connectionState||this.callbacks.onError(new t(e.OTHER_ERROR,"peer connection closed")))}onIceConnectionState(){"running"===this.getState()&&this.pc&&"failed"===this.pc.iceConnectionState&&this.pc.restartIce()}}class a{constructor(e,t,s){this.config=e,this.getState=t,this.onError=s}authHeader(){if(this.config.user&&""!==this.config.user){return{Authorization:`Basic ${btoa(`${this.config.user}:${this.config.pass}`)}`}}return this.config.token&&""!==this.config.token?{Authorization:`Bearer ${this.config.token}`}:{}}async requestICEServers(){return this.config.iceServers&&this.config.iceServers.length>0?this.config.iceServers:fetch(this.config.url,{method:"OPTIONS",headers:{...this.authHeader()}}).then(e=>s.linkToIceServers(e.headers.get("Link")))}async sendOffer(s){if("running"!==this.getState())throw new t(e.STATE_ERROR,"closed");return fetch(this.config.url,{method:"POST",headers:{...this.authHeader(),"Content-Type":"application/sdp"},body:s}).then(s=>{switch(s.status){case 201:break;case 404:throw new t(e.NETWORK_ERROR,"stream not found");case 406:throw new t(e.NETWORK_ERROR,"stream not supported");case 400:return s.json().then(s=>{throw new t(e.NETWORK_ERROR,s.error)});default:throw new t(e.NETWORK_ERROR,`bad status code ${s.status}`)}const n=s.headers.get("Location"),r=n?new URL(n,this.config.url).toString():void 0;return s.text().then(e=>({sessionUrl:r,answer:e}))})}sendLocalCandidates(s,n,i){fetch(s,{method:"PATCH",headers:{"Content-Type":"application/trickle-ice-sdpfrag","If-Match":"*"},body:r.generateSdpFragment(n,i)}).then(s=>{switch(s.status){case 204:break;case 404:throw new t(e.NETWORK_ERROR,"stream not found");default:throw new t(e.NETWORK_ERROR,`bad status code ${s.status}`)}}).catch(e=>this.onError(e))}}class c{constructor(e){this.container=e}onTrack(e){this.stream=e.streams[0],this.stopObserver(),this.observer=new IntersectionObserver(([e])=>{e.isIntersecting?this.resume():this.pause()},{threshold:.5}),this.observer.observe(this.container)}stopObserver(){this.observer&&(this.observer.disconnect(),this.observer=void 0)}get paused(){return null===this.container.srcObject}pause(){this.container.srcObject=null}resume(){this.stream&&this.paused&&(this.container.srcObject=this.stream)}stop(){this.stopObserver(),this.stream=void 0}}class o{constructor(e){this.lastBytesReceived=0,this.checkInterval=e.interval,this.onError=e.onError}setPeerConnection(e){this.pc=e}start(){this.stop(),this.checkTimer=setInterval(()=>this.checkFlowState(),this.checkInterval)}stop(){this.checkTimer&&(clearInterval(this.checkTimer),this.checkTimer=void 0)}async checkFlowState(){if(!this.pc)return;const s=await this.pc.getStats();let n=0;s.forEach(e=>{const t=e;"inbound-rtp"===e.type&&"video"===t.kind&&(n=t.bytesReceived||0)}),n!==this.lastBytesReceived||"connected"!==this.pc.connectionState?this.lastBytesReceived=n:this.onError(new t(e.NETWORK_ERROR,"data stream interruption"))}}class h{constructor(e){this.retryPause=2e3,this.queuedCandidates=[],this.nonAdvertisedCodecs=[],this.conf=e,this.state="getting_codecs",this.flowCheck=new o({interval:5e3,onError:e=>this.handleError(e)}),this.httpClient=new a(this.conf,()=>this.state,e=>this.handleError(e)),this.connectionManager=new i(()=>this.state,{onCandidate:e=>this.handleCandidate(e),onTrack:e=>{this.trackManager.onTrack(e),this.flowCheck.start()},onError:e=>this.handleError(e)},this.nonAdvertisedCodecs),this.trackManager=new c(this.conf.container),this.codecDetector=new n(()=>this.state,{onCodecsDetected:e=>this.handleCodecsDetected(e),onError:e=>this.handleError(e)}),this.codecDetector.detect()}get isRunning(){return"running"===this.state}close(){this.state="closed",this.connectionManager.close(),this.trackManager.stop(),this.flowCheck.stop(),this.restartTimeout&&clearTimeout(this.restartTimeout)}handleError(s){this.flowCheck.stop(),"getting_codecs"===this.state||s instanceof t&&s.type===e.SIGNAL_ERROR?this.state="failed":"running"===this.state&&(this.connectionManager.close(),this.queuedCandidates=[],this.sessionUrl&&(fetch(this.sessionUrl,{method:"DELETE"}),this.sessionUrl=void 0),this.state="restarting",this.restartTimeout=setTimeout(()=>{this.restartTimeout=void 0,this.state="running",this.start()},this.retryPause),s.message=`${s.message}, retrying in some seconds`),this.conf.onError&&(s instanceof t?this.conf.onError(s):this.conf.onError(new t(e.OTHER_ERROR,s.message)))}handleCodecsDetected(e){this.nonAdvertisedCodecs=e,this.state="running",this.start()}start(){this.httpClient.requestICEServers().then(e=>this.connectionManager.setupPeerConnection(e)).then(e=>this.httpClient.sendOffer(e)).then(({sessionUrl:e,answer:t})=>this.handleOfferResponse(e,t)).catch(e=>this.handleError(e))}handleOfferResponse(e,t){return e&&(this.sessionUrl=e),this.connectionManager.setAnswer(t).then(()=>{if("running"===this.state&&0!==this.queuedCandidates.length){const e=this.connectionManager.getOfferData();e&&this.sessionUrl&&(this.httpClient.sendLocalCandidates(this.sessionUrl,e,this.queuedCandidates),this.queuedCandidates=[])}})}handleCandidate(e){if(this.sessionUrl){const t=this.connectionManager.getOfferData();t&&this.httpClient.sendLocalCandidates(this.sessionUrl,t,[e])}else this.queuedCandidates.push(e)}get paused(){return this.trackManager.paused}pause(){this.trackManager.pause()}resume(){this.trackManager.resume()}}export{e as ErrorTypes,t as WebRTCError,h as default};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { WebRTCError } from './errors';
|
|
2
|
+
/**
|
|
3
|
+
* Configuration interface for WebRTCWhep.
|
|
4
|
+
*/
|
|
5
|
+
export interface Conf {
|
|
6
|
+
/** Absolute URL of the WHEP endpoint */
|
|
7
|
+
url: string;
|
|
8
|
+
/** Media player container */
|
|
9
|
+
container: HTMLMediaElement;
|
|
10
|
+
/** Username for authentication */
|
|
11
|
+
user?: string;
|
|
12
|
+
/** Password for authentication */
|
|
13
|
+
pass?: string;
|
|
14
|
+
/** Token for authentication */
|
|
15
|
+
token?: string;
|
|
16
|
+
/** ice server list */
|
|
17
|
+
iceServers?: RTCIceServer[];
|
|
18
|
+
/** Called when there's an error */
|
|
19
|
+
onError?: (err: WebRTCError) => void;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* State type for WebRTCWhep.
|
|
23
|
+
*/
|
|
24
|
+
export type State = 'getting_codecs' | 'running' | 'restarting' | 'closed' | 'failed';
|
|
25
|
+
/** Extend RTCConfiguration to include experimental properties */
|
|
26
|
+
declare global {
|
|
27
|
+
interface RTCConfiguration {
|
|
28
|
+
sdpSemantics?: 'plan-b' | 'unified-plan';
|
|
29
|
+
}
|
|
30
|
+
interface RTCIceServer {
|
|
31
|
+
credentialType?: 'password';
|
|
32
|
+
}
|
|
33
|
+
}
|
package/dist/whep.d.ts
CHANGED
|
@@ -1,117 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
/**
|
|
3
|
-
* Configuration interface for WebRTCWhep.
|
|
4
|
-
*/
|
|
5
|
-
export interface Conf {
|
|
6
|
-
/** Absolute URL of the WHEP endpoint */
|
|
7
|
-
url: string;
|
|
8
|
-
/** Media player container */
|
|
9
|
-
container: HTMLMediaElement;
|
|
10
|
-
/** Username for authentication */
|
|
11
|
-
user?: string;
|
|
12
|
-
/** Password for authentication */
|
|
13
|
-
pass?: string;
|
|
14
|
-
/** Token for authentication */
|
|
15
|
-
token?: string;
|
|
16
|
-
/** ice server list */
|
|
17
|
-
iceServers?: RTCIceServer[];
|
|
18
|
-
/** Called when there's an error */
|
|
19
|
-
onError?: (err: WebRTCError) => void;
|
|
20
|
-
}
|
|
21
|
-
/** Extend RTCConfiguration to include experimental properties */
|
|
22
|
-
declare global {
|
|
23
|
-
interface RTCConfiguration {
|
|
24
|
-
sdpSemantics?: 'plan-b' | 'unified-plan';
|
|
25
|
-
}
|
|
26
|
-
interface RTCIceServer {
|
|
27
|
-
credentialType?: 'password';
|
|
28
|
-
}
|
|
29
|
-
}
|
|
1
|
+
import type { Conf } from './types';
|
|
30
2
|
/** WebRTC/WHEP reader. */
|
|
31
3
|
export default class WebRTCWhep {
|
|
32
4
|
private retryPause;
|
|
33
5
|
private conf;
|
|
34
6
|
private state;
|
|
35
7
|
private restartTimeout?;
|
|
36
|
-
private pc?;
|
|
37
|
-
private offerData?;
|
|
38
8
|
private sessionUrl?;
|
|
39
9
|
private queuedCandidates;
|
|
40
10
|
private nonAdvertisedCodecs;
|
|
41
|
-
private observer;
|
|
42
|
-
private stream?;
|
|
43
11
|
private flowCheck;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
12
|
+
private httpClient;
|
|
13
|
+
private connectionManager;
|
|
14
|
+
private trackManager;
|
|
15
|
+
private codecDetector;
|
|
48
16
|
constructor(conf: Conf);
|
|
49
|
-
/**
|
|
50
|
-
* 媒体是否正常
|
|
51
|
-
*/
|
|
52
17
|
get isRunning(): boolean;
|
|
53
|
-
/**
|
|
54
|
-
* Close the reader and all its resources.
|
|
55
|
-
*/
|
|
56
18
|
close(): void;
|
|
57
|
-
/**
|
|
58
|
-
* Handle errors.
|
|
59
|
-
*/
|
|
60
19
|
private handleError;
|
|
61
|
-
|
|
62
|
-
* Get non-advertised codecs.
|
|
63
|
-
*/
|
|
64
|
-
private getNonAdvertisedCodecs;
|
|
65
|
-
/**
|
|
66
|
-
* Start the WebRTC session.
|
|
67
|
-
*/
|
|
20
|
+
private handleCodecsDetected;
|
|
68
21
|
private start;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
*/
|
|
72
|
-
private authHeader;
|
|
73
|
-
/**
|
|
74
|
-
* Request ICE servers from the endpoint.
|
|
75
|
-
*/
|
|
76
|
-
private requestICEServers;
|
|
77
|
-
/**
|
|
78
|
-
* Setup a peer connection.
|
|
79
|
-
*/
|
|
80
|
-
private setupPeerConnection;
|
|
81
|
-
/**
|
|
82
|
-
* Send an offer to the endpoint.
|
|
83
|
-
*/
|
|
84
|
-
private sendOffer;
|
|
85
|
-
/**
|
|
86
|
-
* Set a remote answer.
|
|
87
|
-
*/
|
|
88
|
-
private setAnswer;
|
|
89
|
-
/**
|
|
90
|
-
* Handle local ICE candidates.
|
|
91
|
-
*/
|
|
92
|
-
private onLocalCandidate;
|
|
93
|
-
/**
|
|
94
|
-
* Send local ICE candidates to the endpoint.
|
|
95
|
-
*/
|
|
96
|
-
private sendLocalCandidates;
|
|
97
|
-
/**
|
|
98
|
-
* Handle peer connection state changes.
|
|
99
|
-
*/
|
|
100
|
-
private onConnectionState;
|
|
101
|
-
/**
|
|
102
|
-
* Handle incoming tracks.
|
|
103
|
-
*/
|
|
104
|
-
private onTrack;
|
|
105
|
-
/**
|
|
106
|
-
* 流是否为空
|
|
107
|
-
*/
|
|
22
|
+
private handleOfferResponse;
|
|
23
|
+
private handleCandidate;
|
|
108
24
|
get paused(): boolean;
|
|
109
|
-
/**
|
|
110
|
-
* 暂停播放
|
|
111
|
-
*/
|
|
112
25
|
pause(): void;
|
|
113
|
-
/**
|
|
114
|
-
* 恢复播放
|
|
115
|
-
*/
|
|
116
26
|
resume(): void;
|
|
117
27
|
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "whepts",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
5
|
-
"packageManager": "pnpm@10.26.2",
|
|
4
|
+
"version": "1.0.3",
|
|
6
5
|
"description": "基于 mediamtx 的 WebRTC WHEP 播放器,支持 ZLM 和 Mediamtx 的播放地址",
|
|
7
6
|
"author": "mapleafgo",
|
|
8
7
|
"license": "MIT",
|
|
@@ -26,12 +25,6 @@
|
|
|
26
25
|
],
|
|
27
26
|
"main": "dist/index.js",
|
|
28
27
|
"types": "dist/index.d.ts",
|
|
29
|
-
"scripts": {
|
|
30
|
-
"build": "rollup --config --environment NODE_ENV:production",
|
|
31
|
-
"build:debug": "rollup --config",
|
|
32
|
-
"lint": "eslint",
|
|
33
|
-
"lint:fix": "eslint --fix"
|
|
34
|
-
},
|
|
35
28
|
"devDependencies": {
|
|
36
29
|
"@antfu/eslint-config": "^6.7.3",
|
|
37
30
|
"@rollup/plugin-commonjs": "^29.0.0",
|
|
@@ -45,5 +38,11 @@
|
|
|
45
38
|
"rollup-plugin-delete": "^3.0.2",
|
|
46
39
|
"tslib": "^2.8.1",
|
|
47
40
|
"typescript": "^5.9.3"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "rollup --config --environment NODE_ENV:production",
|
|
44
|
+
"build:debug": "rollup --config",
|
|
45
|
+
"lint": "eslint",
|
|
46
|
+
"lint:fix": "eslint --fix"
|
|
48
47
|
}
|
|
49
|
-
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
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
|
+
}
|