Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More

@iyio/media-common

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@iyio/media-common - npm Package Compare versions

Comparing version 0.7.0 to 0.7.2

@@ -1,2 +0,1 @@

import { __awaiter } from "tslib";
import { decode, encode } from "blurhash";

@@ -25,9 +24,8 @@ import { imageSourceToImageDataAsync } from "./media-lib";

}
return canvas.toDataURL(options === null || options === void 0 ? void 0 : options.imageType, options === null || options === void 0 ? void 0 : options.quality);
return canvas.toDataURL(options?.imageType, options?.quality);
};
export const imageToBlurHashAsync = (src, options) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c, _d;
export const imageToBlurHashAsync = async (src, options) => {
try {
const imageData = yield imageSourceToImageDataAsync(src, (_a = options === null || options === void 0 ? void 0 : options.width) !== null && _a !== void 0 ? _a : defaultHashBlurWidth, (_b = options === null || options === void 0 ? void 0 : options.height) !== null && _b !== void 0 ? _b : defaultHashBlurHeight);
return encode(imageData.data, imageData.width, imageData.height, (_c = options === null || options === void 0 ? void 0 : options.componentX) !== null && _c !== void 0 ? _c : 4, (_d = options === null || options === void 0 ? void 0 : options.componentY) !== null && _d !== void 0 ? _d : 4);
const imageData = await imageSourceToImageDataAsync(src, options?.width ?? defaultHashBlurWidth, options?.height ?? defaultHashBlurHeight);
return encode(imageData.data, imageData.width, imageData.height, options?.componentX ?? 4, options?.componentY ?? 4);
}

@@ -38,3 +36,3 @@ catch (ex) {

}
});
};
//# sourceMappingURL=blurhash.js.map

@@ -1,2 +0,1 @@

import { __awaiter } from "tslib";
import { addQueryToPath, safeParseNumberOrUndefined, uuid } from "@iyio/common";

@@ -22,3 +21,2 @@ const getCanvas = () => {

export const getSnapshot = ({ source, type = 'image/jpeg', width, height, viewBoxWidth, viewBoxHeight, flipHorizontal, flipVertical, includeStats, }) => {
var _a, _b;
let tw = width;

@@ -28,5 +26,5 @@ let th = height;

const vid = source;
const stream = vid === null || vid === void 0 ? void 0 : vid.srcObject;
const stream = vid?.srcObject;
if (stream) {
const trackSettings = (_b = (_a = stream.getVideoTracks()) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.getSettings();
const trackSettings = stream.getVideoTracks()?.[0]?.getSettings();
if (trackSettings) {

@@ -58,4 +56,4 @@ if (tw === undefined) {

const tr = tw / th;
const vw = (viewBoxWidth !== null && viewBoxWidth !== void 0 ? viewBoxWidth : tw) || 1;
const vh = (viewBoxHeight !== null && viewBoxHeight !== void 0 ? viewBoxHeight : th) || 1;
const vw = (viewBoxWidth ?? tw) || 1;
const vh = (viewBoxHeight ?? th) || 1;
const vr = vw / vh;

@@ -124,3 +122,2 @@ let cw = 0; // capture width

export const getContentTypeFileExt = (contentType) => {
var _a;
switch (contentType.toLowerCase()) {

@@ -130,7 +127,7 @@ case 'image/jpeg':

default:
return (_a = contentType.split('/')[1]) !== null && _a !== void 0 ? _a : 'jpg';
return contentType.split('/')[1] ?? 'jpg';
}
};
export const getImageSizeAsync = (src) => __awaiter(void 0, void 0, void 0, function* () {
const img = yield loadImageAsync(src);
export const getImageSizeAsync = async (src) => {
const img = await loadImageAsync(src);
return {

@@ -140,3 +137,3 @@ width: img.width,

};
});
};
export const loadImageAsync = (src) => {

@@ -190,7 +187,7 @@ let revokeUrl = false;

};
export const imageSourceToImageDataAsync = (src, width, height) => __awaiter(void 0, void 0, void 0, function* () {
const img = yield loadImageAsync(src);
export const imageSourceToImageDataAsync = async (src, width, height) => {
const img = await loadImageAsync(src);
const canvas = document.createElement('canvas');
canvas.width = width !== null && width !== void 0 ? width : img.width;
canvas.height = height !== null && height !== void 0 ? height : img.height;
canvas.width = width ?? img.width;
canvas.height = height ?? img.height;
const context = canvas.getContext('2d');

@@ -200,16 +197,15 @@ if (!context) {

}
context.drawImage(img, 0, 0, width !== null && width !== void 0 ? width : img.width, height !== null && height !== void 0 ? height : img.height);
return context.getImageData(0, 0, width !== null && width !== void 0 ? width : img.width, height !== null && height !== void 0 ? height : img.height);
});
export const fetchBlobAsync = (url_1, ...args_1) => __awaiter(void 0, [url_1, ...args_1], void 0, function* (url, bustCache = false) {
context.drawImage(img, 0, 0, width ?? img.width, height ?? img.height);
return context.getImageData(0, 0, width ?? img.width, height ?? img.height);
};
export const fetchBlobAsync = async (url, bustCache = false) => {
if (bustCache) {
url = addQueryToPath(url, { _: uuid() });
}
const r = yield fetch(url);
return yield r.blob();
});
const r = await fetch(url);
return await r.blob();
};
export const getVideoInfoAsync = (src) => {
return new Promise((resolve, reject) => {
var _a, _b;
const video = (_b = (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.document) === null || _b === void 0 ? void 0 : _b.createElement('video');
const video = globalThis.window?.document?.createElement('video');
if (!video) {

@@ -229,4 +225,3 @@ resolve({ width: 0, height: 0, durationSeconds: 0 });

const onMeta = () => {
var _a;
resolve({ width: video.videoWidth, height: video.videoHeight, durationSeconds: (_a = safeParseNumberOrUndefined(video.duration)) !== null && _a !== void 0 ? _a : 0 });
resolve({ width: video.videoWidth, height: video.videoHeight, durationSeconds: safeParseNumberOrUndefined(video.duration) ?? 0 });
cleanUp();

@@ -240,3 +235,3 @@ };

video.addEventListener('error', onError);
video.src = blobUrl !== null && blobUrl !== void 0 ? blobUrl : src;
video.src = blobUrl ?? src;
});

@@ -246,4 +241,3 @@ };

return new Promise((resolve, reject) => {
var _a, _b;
const video = (_b = (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.document) === null || _b === void 0 ? void 0 : _b.createElement('video');
const video = globalThis.window?.document?.createElement('video');
if (!video) {

@@ -269,3 +263,3 @@ reject();

}
video.src = blobUrl !== null && blobUrl !== void 0 ? blobUrl : src;
video.src = blobUrl ?? src;
});

@@ -272,0 +266,0 @@ };

@@ -1,2 +0,1 @@

import { __awaiter } from "tslib";
import { asArray, createPromiseSource, delayAsync } from "@iyio/common";

@@ -23,3 +22,3 @@ import { BehaviorSubject, Subject } from "rxjs";

this.endSource = null;
this.options = Object.assign({}, options);
this.options = { ...options };
}

@@ -29,3 +28,2 @@ get isDisposed() { return this._isDisposed; }

dispose() {
var _a, _b;
if (this._isDisposed) {

@@ -36,8 +34,8 @@ return;

try {
(_a = this._recorder.value) === null || _a === void 0 ? void 0 : _a.stop();
this._recorder.value?.stop();
}
catch (_c) {
catch {
//
}
(_b = this.endSource) === null || _b === void 0 ? void 0 : _b.resolve();
this.endSource?.resolve();
}

@@ -47,126 +45,125 @@ start() {

}
recordAsync() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.recordPromise) {
this.recordPromise = this._recordAsync();
}
return yield this.recordPromise;
});
async recordAsync() {
if (!this.recordPromise) {
this.recordPromise = this._recordAsync();
}
return await this.recordPromise;
}
_recordAsync() {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
if (this._isDisposed) {
return null;
}
const getOptions = () => {
var _a, _b;
const mo = Object.assign({ audioBitsPerSecond: ((_a = this.options.audioKBPerSecond) !== null && _a !== void 0 ? _a : defaultMediaRecordingAudioKBPerSecond) * 8000, videoBitsPerSecond: ((_b = this.options.videoKBPerSecond) !== null && _b !== void 0 ? _b : defaultMediaRecordingVideoKBPerSecond) * 8000, mimeType: this.options.mimeType }, this.options.recorderOptions);
return mo;
async _recordAsync() {
if (this._isDisposed) {
return null;
}
const getOptions = () => {
const mo = {
audioBitsPerSecond: (this.options.audioKBPerSecond ?? defaultMediaRecordingAudioKBPerSecond) * 8000,
videoBitsPerSecond: (this.options.videoKBPerSecond ?? defaultMediaRecordingVideoKBPerSecond) * 8000,
mimeType: this.options.mimeType,
...this.options.recorderOptions,
};
let recorder = this.options.recorder;
if (!recorder && this.options.streams) {
const stream = new MediaStream();
const streams = this.options.stream ?
[this.options.stream, ...this.options.streams] :
this.options.streams;
for (const s of streams) {
const tracks = s.getTracks();
for (const t of tracks) {
stream.addTrack(t);
}
return mo;
};
let recorder = this.options.recorder;
if (!recorder && this.options.streams) {
const stream = new MediaStream();
const streams = this.options.stream ?
[this.options.stream, ...this.options.streams] :
this.options.streams;
for (const s of streams) {
const tracks = s.getTracks();
for (const t of tracks) {
stream.addTrack(t);
}
recorder = new MediaRecorder(stream, getOptions());
}
if (!recorder && this.options.stream) {
recorder = new MediaRecorder(this.options.stream, getOptions());
}
let disposeStream = null;
if (!recorder && this.options.mediaConstraints) {
const ary = (_a = asArray(this.options.mediaConstraints)) !== null && _a !== void 0 ? _a : [];
for (const mc of ary) {
if (!mc) {
recorder = new MediaRecorder(stream, getOptions());
}
if (!recorder && this.options.stream) {
recorder = new MediaRecorder(this.options.stream, getOptions());
}
let disposeStream = null;
if (!recorder && this.options.mediaConstraints) {
const ary = asArray(this.options.mediaConstraints) ?? [];
for (const mc of ary) {
if (!mc) {
continue;
}
try {
const stream = await navigator.mediaDevices.getUserMedia(mc);
if (!stream) {
continue;
}
try {
const stream = yield navigator.mediaDevices.getUserMedia(mc);
if (!stream) {
continue;
}
disposeStream = stream;
recorder = new MediaRecorder(stream, getOptions());
break;
}
catch (_c) {
//
}
disposeStream = stream;
recorder = new MediaRecorder(stream, getOptions());
break;
}
catch {
//
}
}
if (!recorder) {
throw new Error('Unable to stream for recording');
}
try {
const data = [];
if (this._isDisposed) {
return {
recorder,
data,
mimeType: '',
};
}
const endSource = createPromiseSource();
recorder.addEventListener('dataavailable', e => {
if (!this.options.storeAllData) {
data.splice(0, data.length);
}
data.push(e.data);
this._data.next([...data]);
this._onData.next(e.data);
});
recorder.addEventListener('pause', () => this.isPaused = true);
recorder.addEventListener('resume', () => this.isPaused = false);
recorder.addEventListener('stop', () => { this._recording.next(false); endSource.resolve(); });
recorder.addEventListener('start', () => this._recording.next(true));
recorder.addEventListener('error', e => endSource.reject(e));
if (this.options.timesliceMs) {
recorder.start(this.options.timesliceMs);
}
else {
recorder.start();
}
if (this.isPaused) {
recorder.pause();
}
this.endSource = endSource;
this._recorder.next(recorder);
yield endSource.promise;
const start = Date.now();
while (!data.length && Date.now() - start < 2000) {
yield delayAsync(1);
}
}
if (!recorder) {
throw new Error('Unable to stream for recording');
}
try {
const data = [];
if (this._isDisposed) {
return {
recorder,
data,
mimeType: recorder.mimeType,
mimeType: '',
};
}
finally {
if (disposeStream) {
try {
disposeStream.getTracks().forEach(t => {
t.stop();
});
}
catch (_d) {
//
}
const endSource = createPromiseSource();
recorder.addEventListener('dataavailable', e => {
if (!this.options.storeAllData) {
data.splice(0, data.length);
}
data.push(e.data);
this._data.next([...data]);
this._onData.next(e.data);
});
recorder.addEventListener('pause', () => this.isPaused = true);
recorder.addEventListener('resume', () => this.isPaused = false);
recorder.addEventListener('stop', () => { this._recording.next(false); endSource.resolve(); });
recorder.addEventListener('start', () => this._recording.next(true));
recorder.addEventListener('error', e => endSource.reject(e));
if (this.options.timesliceMs) {
recorder.start(this.options.timesliceMs);
}
else {
recorder.start();
}
if (this.isPaused) {
recorder.pause();
}
this.endSource = endSource;
this._recorder.next(recorder);
await endSource.promise;
const start = Date.now();
while (!data.length && Date.now() - start < 2000) {
await delayAsync(1);
}
return {
recorder,
data,
mimeType: recorder.mimeType,
};
}
finally {
if (disposeStream) {
try {
(_b = this._recorder.value) === null || _b === void 0 ? void 0 : _b.stop();
disposeStream.getTracks().forEach(t => {
t.stop();
});
}
catch (_e) {
catch {
//
}
}
});
try {
this._recorder.value?.stop();
}
catch {
//
}
}
}

@@ -177,3 +174,2 @@ stop() {

pause() {
var _a;
if (this.isPaused) {

@@ -183,6 +179,5 @@ return;

this.isPaused = true;
(_a = this._recorder.value) === null || _a === void 0 ? void 0 : _a.pause();
this._recorder.value?.pause();
}
resume() {
var _a;
if (!this.isPaused) {

@@ -192,5 +187,5 @@ return;

this.isPaused = false;
(_a = this._recorder.value) === null || _a === void 0 ? void 0 : _a.resume();
this._recorder.value?.resume();
}
}
//# sourceMappingURL=MediaRecording.js.map

@@ -46,3 +46,2 @@ import { BehaviorSubject } from "rxjs";

init() {
var _a, _b, _c, _d, _e;
if (this._isDisposed) {

@@ -59,7 +58,7 @@ return;

if (this.disableOnWindowBlur) {
(_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.addEventListener('blur', this._updateFocus);
(_b = globalThis.document) === null || _b === void 0 ? void 0 : _b.addEventListener('blur', this._updateFocus);
(_c = globalThis.window) === null || _c === void 0 ? void 0 : _c.addEventListener('focus', this._updateFocus);
(_d = globalThis.document) === null || _d === void 0 ? void 0 : _d.addEventListener('focus', this._updateFocus);
(_e = globalThis.document) === null || _e === void 0 ? void 0 : _e.addEventListener('visibilitychange', this._updateFocus);
globalThis.window?.addEventListener('blur', this._updateFocus);
globalThis.document?.addEventListener('blur', this._updateFocus);
globalThis.window?.addEventListener('focus', this._updateFocus);
globalThis.document?.addEventListener('focus', this._updateFocus);
globalThis.document?.addEventListener('visibilitychange', this._updateFocus);
this.hasFocus = windowHasFocus();

@@ -73,3 +72,2 @@ }

dispose() {
var _a, _b, _c, _d, _e;
if (this._isDisposed) {

@@ -81,7 +79,7 @@ return;

if (this.disableOnWindowBlur) {
(_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.removeEventListener('blur', this._updateFocus);
(_b = globalThis.document) === null || _b === void 0 ? void 0 : _b.removeEventListener('blur', this._updateFocus);
(_c = globalThis.window) === null || _c === void 0 ? void 0 : _c.removeEventListener('focus', this._updateFocus);
(_d = globalThis.document) === null || _d === void 0 ? void 0 : _d.removeEventListener('focus', this._updateFocus);
(_e = globalThis.document) === null || _e === void 0 ? void 0 : _e.removeEventListener('visibilitychange', this._updateFocus);
globalThis.window?.removeEventListener('blur', this._updateFocus);
globalThis.document?.removeEventListener('blur', this._updateFocus);
globalThis.window?.removeEventListener('focus', this._updateFocus);
globalThis.document?.removeEventListener('focus', this._updateFocus);
globalThis.document?.removeEventListener('visibilitychange', this._updateFocus);
}

@@ -101,3 +99,3 @@ }

}
const windowHasFocus = () => { var _a, _b; return (_b = (_a = globalThis.document) === null || _a === void 0 ? void 0 : _a.hasFocus()) !== null && _b !== void 0 ? _b : true; };
const windowHasFocus = () => globalThis.document?.hasFocus() ?? true;
//# sourceMappingURL=VideoElementMonitor.js.map
{
"name": "@iyio/media-common",
"version": "0.7.0",
"version": "0.7.2",
"sideEffects": false,

@@ -13,3 +13,3 @@ "dependencies": {

"peerDependencies": {
"@iyio/common": "^0.7.0"
"@iyio/common": "^0.7.2"
},

@@ -16,0 +16,0 @@ "main": "./src/index.js",

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.imageToBlurHashAsync = exports.blurHashToDataUri = exports.blurHashToCanvas = exports.defaultHashBlurHeight = exports.defaultHashBlurWidth = void 0;
const tslib_1 = require("tslib");
const blurhash_1 = require("blurhash");

@@ -29,10 +28,9 @@ const media_lib_1 = require("./media-lib");

}
return canvas.toDataURL(options === null || options === void 0 ? void 0 : options.imageType, options === null || options === void 0 ? void 0 : options.quality);
return canvas.toDataURL(options?.imageType, options?.quality);
};
exports.blurHashToDataUri = blurHashToDataUri;
const imageToBlurHashAsync = (src, options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c, _d;
const imageToBlurHashAsync = async (src, options) => {
try {
const imageData = yield (0, media_lib_1.imageSourceToImageDataAsync)(src, (_a = options === null || options === void 0 ? void 0 : options.width) !== null && _a !== void 0 ? _a : exports.defaultHashBlurWidth, (_b = options === null || options === void 0 ? void 0 : options.height) !== null && _b !== void 0 ? _b : exports.defaultHashBlurHeight);
return (0, blurhash_1.encode)(imageData.data, imageData.width, imageData.height, (_c = options === null || options === void 0 ? void 0 : options.componentX) !== null && _c !== void 0 ? _c : 4, (_d = options === null || options === void 0 ? void 0 : options.componentY) !== null && _d !== void 0 ? _d : 4);
const imageData = await (0, media_lib_1.imageSourceToImageDataAsync)(src, options?.width ?? exports.defaultHashBlurWidth, options?.height ?? exports.defaultHashBlurHeight);
return (0, blurhash_1.encode)(imageData.data, imageData.width, imageData.height, options?.componentX ?? 4, options?.componentY ?? 4);
}

@@ -43,4 +41,4 @@ catch (ex) {

}
});
};
exports.imageToBlurHashAsync = imageToBlurHashAsync;
//# sourceMappingURL=blurhash.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.mediaRecordingResultToBlob = exports.getVideoAsync = exports.getVideoInfoAsync = exports.fetchBlobAsync = exports.imageSourceToImageDataAsync = exports.loadAudioAsync = exports.loadImageAsync = exports.getImageSizeAsync = exports.getContentTypeFileExt = exports.getImageDataStats = exports.getSnapshot = void 0;
const tslib_1 = require("tslib");
const common_1 = require("@iyio/common");

@@ -25,3 +24,2 @@ const getCanvas = () => {

const getSnapshot = ({ source, type = 'image/jpeg', width, height, viewBoxWidth, viewBoxHeight, flipHorizontal, flipVertical, includeStats, }) => {
var _a, _b;
let tw = width;

@@ -31,5 +29,5 @@ let th = height;

const vid = source;
const stream = vid === null || vid === void 0 ? void 0 : vid.srcObject;
const stream = vid?.srcObject;
if (stream) {
const trackSettings = (_b = (_a = stream.getVideoTracks()) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.getSettings();
const trackSettings = stream.getVideoTracks()?.[0]?.getSettings();
if (trackSettings) {

@@ -61,4 +59,4 @@ if (tw === undefined) {

const tr = tw / th;
const vw = (viewBoxWidth !== null && viewBoxWidth !== void 0 ? viewBoxWidth : tw) || 1;
const vh = (viewBoxHeight !== null && viewBoxHeight !== void 0 ? viewBoxHeight : th) || 1;
const vw = (viewBoxWidth ?? tw) || 1;
const vh = (viewBoxHeight ?? th) || 1;
const vr = vw / vh;

@@ -129,3 +127,2 @@ let cw = 0; // capture width

const getContentTypeFileExt = (contentType) => {
var _a;
switch (contentType.toLowerCase()) {

@@ -135,8 +132,8 @@ case 'image/jpeg':

default:
return (_a = contentType.split('/')[1]) !== null && _a !== void 0 ? _a : 'jpg';
return contentType.split('/')[1] ?? 'jpg';
}
};
exports.getContentTypeFileExt = getContentTypeFileExt;
const getImageSizeAsync = (src) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const img = yield (0, exports.loadImageAsync)(src);
const getImageSizeAsync = async (src) => {
const img = await (0, exports.loadImageAsync)(src);
return {

@@ -146,3 +143,3 @@ width: img.width,

};
});
};
exports.getImageSizeAsync = getImageSizeAsync;

@@ -199,7 +196,7 @@ const loadImageAsync = (src) => {

exports.loadAudioAsync = loadAudioAsync;
const imageSourceToImageDataAsync = (src, width, height) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const img = yield (0, exports.loadImageAsync)(src);
const imageSourceToImageDataAsync = async (src, width, height) => {
const img = await (0, exports.loadImageAsync)(src);
const canvas = document.createElement('canvas');
canvas.width = width !== null && width !== void 0 ? width : img.width;
canvas.height = height !== null && height !== void 0 ? height : img.height;
canvas.width = width ?? img.width;
canvas.height = height ?? img.height;
const context = canvas.getContext('2d');

@@ -209,18 +206,17 @@ if (!context) {

}
context.drawImage(img, 0, 0, width !== null && width !== void 0 ? width : img.width, height !== null && height !== void 0 ? height : img.height);
return context.getImageData(0, 0, width !== null && width !== void 0 ? width : img.width, height !== null && height !== void 0 ? height : img.height);
});
context.drawImage(img, 0, 0, width ?? img.width, height ?? img.height);
return context.getImageData(0, 0, width ?? img.width, height ?? img.height);
};
exports.imageSourceToImageDataAsync = imageSourceToImageDataAsync;
const fetchBlobAsync = (url_1, ...args_1) => tslib_1.__awaiter(void 0, [url_1, ...args_1], void 0, function* (url, bustCache = false) {
const fetchBlobAsync = async (url, bustCache = false) => {
if (bustCache) {
url = (0, common_1.addQueryToPath)(url, { _: (0, common_1.uuid)() });
}
const r = yield fetch(url);
return yield r.blob();
});
const r = await fetch(url);
return await r.blob();
};
exports.fetchBlobAsync = fetchBlobAsync;
const getVideoInfoAsync = (src) => {
return new Promise((resolve, reject) => {
var _a, _b;
const video = (_b = (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.document) === null || _b === void 0 ? void 0 : _b.createElement('video');
const video = globalThis.window?.document?.createElement('video');
if (!video) {

@@ -240,4 +236,3 @@ resolve({ width: 0, height: 0, durationSeconds: 0 });

const onMeta = () => {
var _a;
resolve({ width: video.videoWidth, height: video.videoHeight, durationSeconds: (_a = (0, common_1.safeParseNumberOrUndefined)(video.duration)) !== null && _a !== void 0 ? _a : 0 });
resolve({ width: video.videoWidth, height: video.videoHeight, durationSeconds: (0, common_1.safeParseNumberOrUndefined)(video.duration) ?? 0 });
cleanUp();

@@ -251,3 +246,3 @@ };

video.addEventListener('error', onError);
video.src = blobUrl !== null && blobUrl !== void 0 ? blobUrl : src;
video.src = blobUrl ?? src;
});

@@ -258,4 +253,3 @@ };

return new Promise((resolve, reject) => {
var _a, _b;
const video = (_b = (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.document) === null || _b === void 0 ? void 0 : _b.createElement('video');
const video = globalThis.window?.document?.createElement('video');
if (!video) {

@@ -281,3 +275,3 @@ reject();

}
video.src = blobUrl !== null && blobUrl !== void 0 ? blobUrl : src;
video.src = blobUrl ?? src;
});

@@ -284,0 +278,0 @@ };

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MediaRecording = exports.defaultMediaRecordingVideoKBPerSecond = exports.defaultMediaRecordingAudioKBPerSecond = void 0;
const tslib_1 = require("tslib");
const common_1 = require("@iyio/common");

@@ -26,3 +25,3 @@ const rxjs_1 = require("rxjs");

this.endSource = null;
this.options = Object.assign({}, options);
this.options = { ...options };
}

@@ -32,3 +31,2 @@ get isDisposed() { return this._isDisposed; }

dispose() {
var _a, _b;
if (this._isDisposed) {

@@ -39,8 +37,8 @@ return;

try {
(_a = this._recorder.value) === null || _a === void 0 ? void 0 : _a.stop();
this._recorder.value?.stop();
}
catch (_c) {
catch {
//
}
(_b = this.endSource) === null || _b === void 0 ? void 0 : _b.resolve();
this.endSource?.resolve();
}

@@ -50,126 +48,125 @@ start() {

}
recordAsync() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (!this.recordPromise) {
this.recordPromise = this._recordAsync();
}
return yield this.recordPromise;
});
async recordAsync() {
if (!this.recordPromise) {
this.recordPromise = this._recordAsync();
}
return await this.recordPromise;
}
_recordAsync() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
var _a, _b;
if (this._isDisposed) {
return null;
}
const getOptions = () => {
var _a, _b;
const mo = Object.assign({ audioBitsPerSecond: ((_a = this.options.audioKBPerSecond) !== null && _a !== void 0 ? _a : exports.defaultMediaRecordingAudioKBPerSecond) * 8000, videoBitsPerSecond: ((_b = this.options.videoKBPerSecond) !== null && _b !== void 0 ? _b : exports.defaultMediaRecordingVideoKBPerSecond) * 8000, mimeType: this.options.mimeType }, this.options.recorderOptions);
return mo;
async _recordAsync() {
if (this._isDisposed) {
return null;
}
const getOptions = () => {
const mo = {
audioBitsPerSecond: (this.options.audioKBPerSecond ?? exports.defaultMediaRecordingAudioKBPerSecond) * 8000,
videoBitsPerSecond: (this.options.videoKBPerSecond ?? exports.defaultMediaRecordingVideoKBPerSecond) * 8000,
mimeType: this.options.mimeType,
...this.options.recorderOptions,
};
let recorder = this.options.recorder;
if (!recorder && this.options.streams) {
const stream = new MediaStream();
const streams = this.options.stream ?
[this.options.stream, ...this.options.streams] :
this.options.streams;
for (const s of streams) {
const tracks = s.getTracks();
for (const t of tracks) {
stream.addTrack(t);
}
return mo;
};
let recorder = this.options.recorder;
if (!recorder && this.options.streams) {
const stream = new MediaStream();
const streams = this.options.stream ?
[this.options.stream, ...this.options.streams] :
this.options.streams;
for (const s of streams) {
const tracks = s.getTracks();
for (const t of tracks) {
stream.addTrack(t);
}
recorder = new MediaRecorder(stream, getOptions());
}
if (!recorder && this.options.stream) {
recorder = new MediaRecorder(this.options.stream, getOptions());
}
let disposeStream = null;
if (!recorder && this.options.mediaConstraints) {
const ary = (_a = (0, common_1.asArray)(this.options.mediaConstraints)) !== null && _a !== void 0 ? _a : [];
for (const mc of ary) {
if (!mc) {
recorder = new MediaRecorder(stream, getOptions());
}
if (!recorder && this.options.stream) {
recorder = new MediaRecorder(this.options.stream, getOptions());
}
let disposeStream = null;
if (!recorder && this.options.mediaConstraints) {
const ary = (0, common_1.asArray)(this.options.mediaConstraints) ?? [];
for (const mc of ary) {
if (!mc) {
continue;
}
try {
const stream = await navigator.mediaDevices.getUserMedia(mc);
if (!stream) {
continue;
}
try {
const stream = yield navigator.mediaDevices.getUserMedia(mc);
if (!stream) {
continue;
}
disposeStream = stream;
recorder = new MediaRecorder(stream, getOptions());
break;
}
catch (_c) {
//
}
disposeStream = stream;
recorder = new MediaRecorder(stream, getOptions());
break;
}
catch {
//
}
}
if (!recorder) {
throw new Error('Unable to stream for recording');
}
try {
const data = [];
if (this._isDisposed) {
return {
recorder,
data,
mimeType: '',
};
}
const endSource = (0, common_1.createPromiseSource)();
recorder.addEventListener('dataavailable', e => {
if (!this.options.storeAllData) {
data.splice(0, data.length);
}
data.push(e.data);
this._data.next([...data]);
this._onData.next(e.data);
});
recorder.addEventListener('pause', () => this.isPaused = true);
recorder.addEventListener('resume', () => this.isPaused = false);
recorder.addEventListener('stop', () => { this._recording.next(false); endSource.resolve(); });
recorder.addEventListener('start', () => this._recording.next(true));
recorder.addEventListener('error', e => endSource.reject(e));
if (this.options.timesliceMs) {
recorder.start(this.options.timesliceMs);
}
else {
recorder.start();
}
if (this.isPaused) {
recorder.pause();
}
this.endSource = endSource;
this._recorder.next(recorder);
yield endSource.promise;
const start = Date.now();
while (!data.length && Date.now() - start < 2000) {
yield (0, common_1.delayAsync)(1);
}
}
if (!recorder) {
throw new Error('Unable to stream for recording');
}
try {
const data = [];
if (this._isDisposed) {
return {
recorder,
data,
mimeType: recorder.mimeType,
mimeType: '',
};
}
finally {
if (disposeStream) {
try {
disposeStream.getTracks().forEach(t => {
t.stop();
});
}
catch (_d) {
//
}
const endSource = (0, common_1.createPromiseSource)();
recorder.addEventListener('dataavailable', e => {
if (!this.options.storeAllData) {
data.splice(0, data.length);
}
data.push(e.data);
this._data.next([...data]);
this._onData.next(e.data);
});
recorder.addEventListener('pause', () => this.isPaused = true);
recorder.addEventListener('resume', () => this.isPaused = false);
recorder.addEventListener('stop', () => { this._recording.next(false); endSource.resolve(); });
recorder.addEventListener('start', () => this._recording.next(true));
recorder.addEventListener('error', e => endSource.reject(e));
if (this.options.timesliceMs) {
recorder.start(this.options.timesliceMs);
}
else {
recorder.start();
}
if (this.isPaused) {
recorder.pause();
}
this.endSource = endSource;
this._recorder.next(recorder);
await endSource.promise;
const start = Date.now();
while (!data.length && Date.now() - start < 2000) {
await (0, common_1.delayAsync)(1);
}
return {
recorder,
data,
mimeType: recorder.mimeType,
};
}
finally {
if (disposeStream) {
try {
(_b = this._recorder.value) === null || _b === void 0 ? void 0 : _b.stop();
disposeStream.getTracks().forEach(t => {
t.stop();
});
}
catch (_e) {
catch {
//
}
}
});
try {
this._recorder.value?.stop();
}
catch {
//
}
}
}

@@ -180,3 +177,2 @@ stop() {

pause() {
var _a;
if (this.isPaused) {

@@ -186,6 +182,5 @@ return;

this.isPaused = true;
(_a = this._recorder.value) === null || _a === void 0 ? void 0 : _a.pause();
this._recorder.value?.pause();
}
resume() {
var _a;
if (!this.isPaused) {

@@ -195,3 +190,3 @@ return;

this.isPaused = false;
(_a = this._recorder.value) === null || _a === void 0 ? void 0 : _a.resume();
this._recorder.value?.resume();
}

@@ -198,0 +193,0 @@ }

@@ -49,3 +49,2 @@ "use strict";

init() {
var _a, _b, _c, _d, _e;
if (this._isDisposed) {

@@ -62,7 +61,7 @@ return;

if (this.disableOnWindowBlur) {
(_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.addEventListener('blur', this._updateFocus);
(_b = globalThis.document) === null || _b === void 0 ? void 0 : _b.addEventListener('blur', this._updateFocus);
(_c = globalThis.window) === null || _c === void 0 ? void 0 : _c.addEventListener('focus', this._updateFocus);
(_d = globalThis.document) === null || _d === void 0 ? void 0 : _d.addEventListener('focus', this._updateFocus);
(_e = globalThis.document) === null || _e === void 0 ? void 0 : _e.addEventListener('visibilitychange', this._updateFocus);
globalThis.window?.addEventListener('blur', this._updateFocus);
globalThis.document?.addEventListener('blur', this._updateFocus);
globalThis.window?.addEventListener('focus', this._updateFocus);
globalThis.document?.addEventListener('focus', this._updateFocus);
globalThis.document?.addEventListener('visibilitychange', this._updateFocus);
this.hasFocus = windowHasFocus();

@@ -76,3 +75,2 @@ }

dispose() {
var _a, _b, _c, _d, _e;
if (this._isDisposed) {

@@ -84,7 +82,7 @@ return;

if (this.disableOnWindowBlur) {
(_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.removeEventListener('blur', this._updateFocus);
(_b = globalThis.document) === null || _b === void 0 ? void 0 : _b.removeEventListener('blur', this._updateFocus);
(_c = globalThis.window) === null || _c === void 0 ? void 0 : _c.removeEventListener('focus', this._updateFocus);
(_d = globalThis.document) === null || _d === void 0 ? void 0 : _d.removeEventListener('focus', this._updateFocus);
(_e = globalThis.document) === null || _e === void 0 ? void 0 : _e.removeEventListener('visibilitychange', this._updateFocus);
globalThis.window?.removeEventListener('blur', this._updateFocus);
globalThis.document?.removeEventListener('blur', this._updateFocus);
globalThis.window?.removeEventListener('focus', this._updateFocus);
globalThis.document?.removeEventListener('focus', this._updateFocus);
globalThis.document?.removeEventListener('visibilitychange', this._updateFocus);
}

@@ -105,3 +103,3 @@ }

exports.VideoElementMonitor = VideoElementMonitor;
const windowHasFocus = () => { var _a, _b; return (_b = (_a = globalThis.document) === null || _a === void 0 ? void 0 : _a.hasFocus()) !== null && _b !== void 0 ? _b : true; };
const windowHasFocus = () => globalThis.document?.hasFocus() ?? true;
//# sourceMappingURL=VideoElementMonitor.js.map

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet