@micdrop/webrtc 0.0.0-stage → 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/README.md CHANGED
@@ -1,3 +1,60 @@
1
- # Temporary Holding Version
1
+ # 🖐️🎤 Micdrop: Real-Time Voice Conversations with AI
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [Micdrop website](https://micdrop.dev) | [Documentation](https://micdrop.dev/docs/transports/webrtc)
4
+
5
+ Micdrop is a set of open source Typescript packages to build real-time voice conversations with AI agents. It handles all the complexities on the client and server side (microphone, speaker, VAD, network communication, etc) and provides ready-to-use implementations for various AI providers.
6
+
7
+ # @micdrop/webrtc
8
+
9
+ Micdrop calls over WebRTC, between the browser or a React Native app and your own Node.js server. The microphone and the assistant voice travel as Opus tracks over UDP, where a lost packet is skipped rather than resent, and the Micdrop protocol on a data channel.
10
+
11
+ The server runs on [werift](https://github.com/shinyoshiaki/werift-webrtc), a WebRTC stack in TypeScript, and libopus compiled to WebAssembly: no media server to rent, nothing native to build.
12
+
13
+ ```bash
14
+ # In the app
15
+ npm install @micdrop/webrtc
16
+
17
+ # On the server, with the WebRTC stack and the Opus codec it runs on
18
+ npm install @micdrop/webrtc werift opusscript
19
+ ```
20
+
21
+ ## Server
22
+
23
+ ```typescript
24
+ import { MicdropServer } from '@micdrop/server'
25
+ import { WebRtcTransport } from '@micdrop/webrtc/server'
26
+
27
+ app.post('/call', async (request) => {
28
+ const transport = await WebRtcTransport.accept(request.body)
29
+ new MicdropServer(transport, { agent, stt, tts })
30
+ return transport.answer
31
+ })
32
+ ```
33
+
34
+ ## Browser
35
+
36
+ ```typescript
37
+ import { Micdrop } from '@micdrop/web'
38
+ import { webRtcTransport } from '@micdrop/webrtc'
39
+
40
+ await Micdrop.start({
41
+ transport: webRtcTransport({ url: 'https://example.com/call' }),
42
+ })
43
+ ```
44
+
45
+ ## React Native
46
+
47
+ ```typescript
48
+ import { Micdrop } from '@micdrop/react-native'
49
+ import { webRtcTransport } from '@micdrop/webrtc/react-native'
50
+
51
+ await Micdrop.start({
52
+ transport: webRtcTransport({ url: 'https://example.com/call' }),
53
+ })
54
+ ```
55
+
56
+ On React Native, the call opens its own microphone track with react-native-webrtc, so the server detects the turns: give it `vad` in its config.
57
+
58
+ ## License
59
+
60
+ MIT
@@ -0,0 +1,136 @@
1
+ import { MicdropDevice, MicdropClientTransport, MicdropClientMessage, SpeakerDriver } from '@micdrop/client';
2
+
3
+ /** The microphone track a call sends */
4
+ interface WebRtcMicTrack {
5
+ track: any;
6
+ stream?: any;
7
+ /** Releases the microphone, when the call opened it for itself */
8
+ release?(): void;
9
+ }
10
+ /** Plays the voice of the assistant, which arrives as a remote track */
11
+ interface WebRtcPlayer {
12
+ setMuted(muted: boolean): void;
13
+ /** Level of what plays, in decibels, when the platform can measure it */
14
+ getVolume?(): number | undefined;
15
+ setOutputDevice?(deviceId: string): Promise<void>;
16
+ getOutputDevices?(): Promise<MicdropDevice[]>;
17
+ destroy(): void;
18
+ }
19
+ /**
20
+ * What WebRTC takes on each platform: the browser has it built in, React
21
+ * Native gets it from react-native-webrtc, and the test suite from werift.
22
+ */
23
+ interface WebRtcPlatform {
24
+ /** The constructor of the peer connection */
25
+ RTCPeerConnection: new (configuration: {
26
+ iceServers?: any[];
27
+ }) => any;
28
+ /** Opens or reuses the microphone, as a track to send */
29
+ getMicTrack(): Promise<WebRtcMicTrack>;
30
+ /**
31
+ * Plays the remote track of the assistant
32
+ * @param track - The remote audio track
33
+ * @param stream - Its stream, when the platform gives one
34
+ */
35
+ createPlayer(track: any, stream?: any): WebRtcPlayer;
36
+ /** Posts the offer, the global fetch by default */
37
+ fetch?: typeof fetch;
38
+ }
39
+
40
+ interface WebRtcClientOptions {
41
+ /** Address the offer is posted to, where the server answers it */
42
+ url: string;
43
+ /** Headers of that request, to authenticate the call for instance */
44
+ headers?: Record<string, string>;
45
+ /** STUN and TURN servers, the user behind a strict network needs TURN */
46
+ iceServers?: {
47
+ urls: string | string[];
48
+ username?: string;
49
+ credential?: string;
50
+ }[];
51
+ /** How long the browser gathers its candidates before posting, 2000 ms */
52
+ gatheringTimeout?: number;
53
+ /** How long a lost connection may take to come back, 5000 ms */
54
+ disconnectTimeout?: number;
55
+ }
56
+ /**
57
+ * Carries a call to a `WebRtcTransport` of `@micdrop/webrtc/server`: the
58
+ * microphone as an Opus track, the assistant voice on the track the server
59
+ * sends back, and the Micdrop protocol on a data channel.
60
+ *
61
+ * The client keeps detecting the turns on its microphone, and the server
62
+ * takes the beginning of each turn back from the track. Told `ServerTurns`,
63
+ * the client leaves the turns to the server, as over a WebSocket.
64
+ */
65
+ declare class WebRtcClientTransport extends MicdropClientTransport {
66
+ private options;
67
+ readonly sendsMic = true;
68
+ readonly connectionTimeout = 15000;
69
+ private state;
70
+ private pc?;
71
+ private channel?;
72
+ private mic?;
73
+ private sender?;
74
+ private muted;
75
+ private speaker;
76
+ private previousSpeaker?;
77
+ private disconnectTimer?;
78
+ constructor(options: WebRtcClientOptions & {
79
+ platform: WebRtcPlatform;
80
+ });
81
+ get isOpen(): boolean;
82
+ get isConnecting(): boolean;
83
+ private get isClosed();
84
+ connect(params?: Record<string, any>): void;
85
+ send(message: MicdropClientMessage): void;
86
+ sendAudio(): void;
87
+ setMicMuted(muted: boolean): void;
88
+ close(): void;
89
+ private start;
90
+ private waitForGathering;
91
+ private onOpen;
92
+ private onText;
93
+ private onConnectionState;
94
+ private onMicStreamChange;
95
+ /** Sends the microphone, or nothing at all while muted */
96
+ private applyMicTrack;
97
+ /** The call ended on the other side, or failed */
98
+ private end;
99
+ private teardown;
100
+ }
101
+
102
+ /**
103
+ * The speaker of a call on WebRTC.
104
+ *
105
+ * The voice arrives as a track the platform plays on its own, so nothing is
106
+ * queued here: the server says when it starts and stops sending, and an
107
+ * interruption mutes the track until the next answer starts.
108
+ */
109
+ declare class WebRtcSpeaker extends SpeakerDriver {
110
+ private player?;
111
+ private playing;
112
+ private silenced;
113
+ private stopTimer?;
114
+ private volumeTimer?;
115
+ private _deviceId;
116
+ get isPlaying(): boolean;
117
+ get deviceId(): string | undefined;
118
+ /** Plays through a player, once the remote track arrived */
119
+ attach(player: WebRtcPlayer): void;
120
+ /** The server started sending an answer */
121
+ playbackStarted(): void;
122
+ /** The server sent the last of the answer */
123
+ playbackStopped(): void;
124
+ start(): Promise<void>;
125
+ play(): void;
126
+ stopAudio(): void;
127
+ stop(): Promise<void>;
128
+ changeDevice(deviceId: string): Promise<void>;
129
+ getDevices(): Promise<MicdropDevice[]>;
130
+ /** Lets go of the player, at the end of the call */
131
+ detach(): void;
132
+ private setPlaying;
133
+ private clearStopTimer;
134
+ }
135
+
136
+ export { type WebRtcPlatform as W, type WebRtcClientOptions as a, WebRtcClientTransport as b, type WebRtcMicTrack as c, type WebRtcPlayer as d, WebRtcSpeaker as e };
@@ -0,0 +1,136 @@
1
+ import { MicdropDevice, MicdropClientTransport, MicdropClientMessage, SpeakerDriver } from '@micdrop/client';
2
+
3
+ /** The microphone track a call sends */
4
+ interface WebRtcMicTrack {
5
+ track: any;
6
+ stream?: any;
7
+ /** Releases the microphone, when the call opened it for itself */
8
+ release?(): void;
9
+ }
10
+ /** Plays the voice of the assistant, which arrives as a remote track */
11
+ interface WebRtcPlayer {
12
+ setMuted(muted: boolean): void;
13
+ /** Level of what plays, in decibels, when the platform can measure it */
14
+ getVolume?(): number | undefined;
15
+ setOutputDevice?(deviceId: string): Promise<void>;
16
+ getOutputDevices?(): Promise<MicdropDevice[]>;
17
+ destroy(): void;
18
+ }
19
+ /**
20
+ * What WebRTC takes on each platform: the browser has it built in, React
21
+ * Native gets it from react-native-webrtc, and the test suite from werift.
22
+ */
23
+ interface WebRtcPlatform {
24
+ /** The constructor of the peer connection */
25
+ RTCPeerConnection: new (configuration: {
26
+ iceServers?: any[];
27
+ }) => any;
28
+ /** Opens or reuses the microphone, as a track to send */
29
+ getMicTrack(): Promise<WebRtcMicTrack>;
30
+ /**
31
+ * Plays the remote track of the assistant
32
+ * @param track - The remote audio track
33
+ * @param stream - Its stream, when the platform gives one
34
+ */
35
+ createPlayer(track: any, stream?: any): WebRtcPlayer;
36
+ /** Posts the offer, the global fetch by default */
37
+ fetch?: typeof fetch;
38
+ }
39
+
40
+ interface WebRtcClientOptions {
41
+ /** Address the offer is posted to, where the server answers it */
42
+ url: string;
43
+ /** Headers of that request, to authenticate the call for instance */
44
+ headers?: Record<string, string>;
45
+ /** STUN and TURN servers, the user behind a strict network needs TURN */
46
+ iceServers?: {
47
+ urls: string | string[];
48
+ username?: string;
49
+ credential?: string;
50
+ }[];
51
+ /** How long the browser gathers its candidates before posting, 2000 ms */
52
+ gatheringTimeout?: number;
53
+ /** How long a lost connection may take to come back, 5000 ms */
54
+ disconnectTimeout?: number;
55
+ }
56
+ /**
57
+ * Carries a call to a `WebRtcTransport` of `@micdrop/webrtc/server`: the
58
+ * microphone as an Opus track, the assistant voice on the track the server
59
+ * sends back, and the Micdrop protocol on a data channel.
60
+ *
61
+ * The client keeps detecting the turns on its microphone, and the server
62
+ * takes the beginning of each turn back from the track. Told `ServerTurns`,
63
+ * the client leaves the turns to the server, as over a WebSocket.
64
+ */
65
+ declare class WebRtcClientTransport extends MicdropClientTransport {
66
+ private options;
67
+ readonly sendsMic = true;
68
+ readonly connectionTimeout = 15000;
69
+ private state;
70
+ private pc?;
71
+ private channel?;
72
+ private mic?;
73
+ private sender?;
74
+ private muted;
75
+ private speaker;
76
+ private previousSpeaker?;
77
+ private disconnectTimer?;
78
+ constructor(options: WebRtcClientOptions & {
79
+ platform: WebRtcPlatform;
80
+ });
81
+ get isOpen(): boolean;
82
+ get isConnecting(): boolean;
83
+ private get isClosed();
84
+ connect(params?: Record<string, any>): void;
85
+ send(message: MicdropClientMessage): void;
86
+ sendAudio(): void;
87
+ setMicMuted(muted: boolean): void;
88
+ close(): void;
89
+ private start;
90
+ private waitForGathering;
91
+ private onOpen;
92
+ private onText;
93
+ private onConnectionState;
94
+ private onMicStreamChange;
95
+ /** Sends the microphone, or nothing at all while muted */
96
+ private applyMicTrack;
97
+ /** The call ended on the other side, or failed */
98
+ private end;
99
+ private teardown;
100
+ }
101
+
102
+ /**
103
+ * The speaker of a call on WebRTC.
104
+ *
105
+ * The voice arrives as a track the platform plays on its own, so nothing is
106
+ * queued here: the server says when it starts and stops sending, and an
107
+ * interruption mutes the track until the next answer starts.
108
+ */
109
+ declare class WebRtcSpeaker extends SpeakerDriver {
110
+ private player?;
111
+ private playing;
112
+ private silenced;
113
+ private stopTimer?;
114
+ private volumeTimer?;
115
+ private _deviceId;
116
+ get isPlaying(): boolean;
117
+ get deviceId(): string | undefined;
118
+ /** Plays through a player, once the remote track arrived */
119
+ attach(player: WebRtcPlayer): void;
120
+ /** The server started sending an answer */
121
+ playbackStarted(): void;
122
+ /** The server sent the last of the answer */
123
+ playbackStopped(): void;
124
+ start(): Promise<void>;
125
+ play(): void;
126
+ stopAudio(): void;
127
+ stop(): Promise<void>;
128
+ changeDevice(deviceId: string): Promise<void>;
129
+ getDevices(): Promise<MicdropDevice[]>;
130
+ /** Lets go of the player, at the end of the call */
131
+ detach(): void;
132
+ private setPlaying;
133
+ private clearStopTimer;
134
+ }
135
+
136
+ export { type WebRtcPlatform as W, type WebRtcClientOptions as a, WebRtcClientTransport as b, type WebRtcMicTrack as c, type WebRtcPlayer as d, WebRtcSpeaker as e };
@@ -0,0 +1,20 @@
1
+ import { MicdropClientTransportFactory } from '@micdrop/client';
2
+ import { W as WebRtcPlatform, a as WebRtcClientOptions } from './WebRtcSpeaker-C4pG0yfh.mjs';
3
+ export { b as WebRtcClientTransport, c as WebRtcMicTrack, d as WebRtcPlayer, e as WebRtcSpeaker } from './WebRtcSpeaker-C4pG0yfh.mjs';
4
+ export { D as DATA_CHANNEL_LABEL, M as MicdropWebRtcAnswer, a as MicdropWebRtcOffer, P as PLAYBACK_START, b as PLAYBACK_STOP, d as decodeStartSpeaking, e as encodeStartSpeaking } from './protocol-Ba3tnNV7.mjs';
5
+
6
+ /**
7
+ * WebRTC as a browser has it. The microphone track is the one the client
8
+ * already records from, so the VAD and the server hear the same audio, and
9
+ * the voice of the assistant plays through an audio element, which lets the
10
+ * browser cancel its echo.
11
+ */
12
+ declare const webPlatform: WebRtcPlatform;
13
+ /**
14
+ * Creates the transport of each call, to hand to `MicdropClient` as
15
+ * `transport`
16
+ * @param options - Address of the offer, headers and ICE servers
17
+ */
18
+ declare function webRtcTransport(options: WebRtcClientOptions): MicdropClientTransportFactory;
19
+
20
+ export { WebRtcClientOptions, WebRtcPlatform, webPlatform, webRtcTransport };
@@ -0,0 +1,20 @@
1
+ import { MicdropClientTransportFactory } from '@micdrop/client';
2
+ import { W as WebRtcPlatform, a as WebRtcClientOptions } from './WebRtcSpeaker-C4pG0yfh.js';
3
+ export { b as WebRtcClientTransport, c as WebRtcMicTrack, d as WebRtcPlayer, e as WebRtcSpeaker } from './WebRtcSpeaker-C4pG0yfh.js';
4
+ export { D as DATA_CHANNEL_LABEL, M as MicdropWebRtcAnswer, a as MicdropWebRtcOffer, P as PLAYBACK_START, b as PLAYBACK_STOP, d as decodeStartSpeaking, e as encodeStartSpeaking } from './protocol-Ba3tnNV7.js';
5
+
6
+ /**
7
+ * WebRTC as a browser has it. The microphone track is the one the client
8
+ * already records from, so the VAD and the server hear the same audio, and
9
+ * the voice of the assistant plays through an audio element, which lets the
10
+ * browser cancel its echo.
11
+ */
12
+ declare const webPlatform: WebRtcPlatform;
13
+ /**
14
+ * Creates the transport of each call, to hand to `MicdropClient` as
15
+ * `transport`
16
+ * @param options - Address of the offer, headers and ICE servers
17
+ */
18
+ declare function webRtcTransport(options: WebRtcClientOptions): MicdropClientTransportFactory;
19
+
20
+ export { WebRtcClientOptions, WebRtcPlatform, webPlatform, webRtcTransport };