Skip to content

Commit 9ce9e23

Browse files
Merge pull request #10 from apivideo/destroy-method
feat: add destroy() method
2 parents 91d3039 + 1fc4874 commit 9ce9e23

File tree

7 files changed

+45
-4
lines changed

7 files changed

+45
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22
All changes to this project will be documented in this file.
33

4+
## [0.1.7] - 2022-09-29
5+
- add destroy() method
6+
- automatically remove expired drawing from drawings array
7+
48
## [0.1.6] - 2022-09-16
59
- Fix screencast stream size ratio issue
610

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
- [`setDrawingSettings(settings: Partial<DrawingSettings>): void`](#setdrawingsettingssettings-partialdrawingsettings-void)
4545
- [`clearDrawing(): void`](#cleardrawing-void)
4646
- [`addEventListener(event: string, listener: Function)`](#addeventlistenerevent-string-listener-function)
47+
- [`destroy()`](#destroy)
4748
- [Full examples](#full-examples)
4849
- ["Loom-like"](#loom-like)
4950

@@ -486,7 +487,9 @@ Define an event listener for the media recorder. The following events are availa
486487
});
487488
```
488489
490+
### `destroy()`
489491
492+
Destroys all streams and releases all resources in use.
490493
491494
# Full examples
492495

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@ export declare class MediaStreamComposer {
7272
private drawingSettings;
7373
private drawings;
7474
private lastStreamId;
75+
private drawingsCleanerInterval?;
7576
constructor(options: Partial<Options>);
7677
startRecording(options: RecordingOptions): void;
78+
destroy(): void;
79+
private destroyIfNeeded;
7780
addEventListener(type: EventType, callback: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions | undefined): void;
7881
stopRecording(): Promise<VideoUploadResponse>;
7982
updateStream(streamId: string, options: StreamOptions): void;

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@api.video/media-stream-composer",
3-
"version": "0.1.6",
3+
"version": "0.1.7",
44
"description": "api.video media stream composer",
55
"repository": {
66
"type": "git",

src/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export class MediaStreamComposer {
103103
}
104104
private drawings: ({ coords: [number, number][], startTime: number, } & DrawingSettings)[] = [];
105105
private lastStreamId = 0;
106+
private drawingsCleanerInterval?: any;
106107

107108
constructor(options: Partial<Options>) {
108109
this.eventTarget = new EventTarget();
@@ -131,6 +132,26 @@ export class MediaStreamComposer {
131132
this.recorder.start();
132133
}
133134

135+
public destroy() {
136+
Object.keys(this.streams).forEach(streamId => this.removeStream(streamId));
137+
Object.keys(this.audioSources).forEach(streamId => this.removeStream(streamId));
138+
this.merger?.removeStream("drawing");
139+
this.clearDrawing();
140+
this.merger?.destroy();
141+
if (this.drawingsCleanerInterval) {
142+
clearInterval(this.drawingsCleanerInterval);
143+
this.drawingsCleanerInterval = undefined;
144+
}
145+
this.canvas?.parentElement?.removeChild(this.canvas);
146+
this.merger = undefined;
147+
}
148+
149+
private destroyIfNeeded() {
150+
if (this.drawings.length === 0 && Object.keys(this.audioSources).length === 0 && Object.keys(this.streams).length === 0) {
151+
this.destroy();
152+
}
153+
}
154+
134155
public addEventListener(type: EventType, callback: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions | undefined): void {
135156
this.eventTarget.addEventListener(type, callback, options);
136157
}
@@ -181,6 +202,7 @@ export class MediaStreamComposer {
181202
if (stream) {
182203
this.merger?.removeStream(stream.stream);
183204
delete this.streams[id];
205+
stream.stream.getTracks().forEach(x => x.stop());
184206
}
185207
this.cleanIndexes();
186208
}
@@ -210,6 +232,7 @@ export class MediaStreamComposer {
210232
if (audioSource) {
211233
this.merger?.removeStream(audioSource.stream);
212234
delete this.audioSources[id];
235+
audioSource.stream.getTracks().forEach(x => x.stop());
213236
}
214237
}
215238

@@ -653,6 +676,14 @@ export class MediaStreamComposer {
653676
this.result = this.merger.result;
654677
this.createDrawingStream(this.merger);
655678

679+
const d = this;
680+
681+
this.drawingsCleanerInterval = setInterval(() => {
682+
const currentTime = new Date().getTime();
683+
d.drawings = d.drawings.filter((drawing) => drawing.autoEraseDelay === 0 || (currentTime - drawing.startTime) / 1000 <= drawing.autoEraseDelay);
684+
d.destroyIfNeeded();
685+
}, 1000);
686+
656687
const mouseEventListener = new MouseEventListener(this.canvas!, this.streams);
657688
mouseEventListener.onClick((e) => e.stream?.options.onClick && e.stream.options.onClick(e.stream.id, { x: e.x, y: e.y }));
658689
mouseEventListener.onDrag((e) => this.onMouseDrag(e));

0 commit comments

Comments
 (0)