Skip to content

Commit

Permalink
Merge pull request Mercurial#10 from nstr-tan/master
Browse files Browse the repository at this point in the history
Selected Media device disconnection handling adjustment
  • Loading branch information
Mercurial authored Jul 7, 2020
2 parents a60f794 + 426a9f4 commit 88ab455
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 62 deletions.
2 changes: 1 addition & 1 deletion BlazorMedia/BlazorMedia.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<LangVersion>8.0</LangVersion>
<RazorLangVersion>3.0</RazorLangVersion>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
<Version>0.3.0.6</Version>
<Version>0.3.0.7</Version>
<Authors>Rory Clark Manuelle Alesna, Nestor Angelo Tan</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
Expand Down
4 changes: 2 additions & 2 deletions BlazorMedia/tsconfig.tsbuildinfo
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
"affectsGlobalScope": true
},
"./wwwroot/blazormedia.ts": {
"version": "fb217425be265565c0a12b80214d88d6a01005e5d15ff3a7b7e827b6f4d4a800",
"signature": "001318548ae20677fa1c2828829317cca3c513cd14b1b1f50b4f8a0fdd0aea2e",
"version": "cf97729ababdae24740a06108ee38259e73e0a661fa50325f3d16da81770609c",
"signature": "b547b9d15246f3bd076b2a8ad8bd229790bced51876304a9703248924bd33aff",
"affectsGlobalScope": true
},
"./node_modules/@types/dom-mediacapture-record/index.d.ts": {
Expand Down
84 changes: 48 additions & 36 deletions BlazorMedia/wwwroot/BlazorMedia.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,54 +215,66 @@ var BlazorMedia;
};
BlazorMediaInterop.DetectMediaDeviceUsedDisconnection = function (videoElement, componentRef) {
return __awaiter(this, void 0, void 0, function () {
var stream_1, tracks, _loop_1, x;
var stream, tracks;
var _this = this;
return __generator(this, function (_a) {
if (videoElement.mediaStream) {
stream_1 = videoElement.mediaStream;
tracks = stream_1.getTracks();
_loop_1 = function (x) {
var track = tracks[x];
stream = videoElement.mediaStream;
tracks = stream.getTracks();
tracks.forEach(function (track) {
track.onended = function (ev) { return __awaiter(_this, void 0, void 0, function () {
var devices, videoIsStillConnected, audioIsStillConnected, y, device, mediaError;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, navigator.mediaDevices.enumerateDevices()];
case 1:
devices = _a.sent();
videoIsStillConnected = false;
audioIsStillConnected = false;
for (y = 0; y < devices.length; y++) {
device = devices[y];
if (device.deviceId == this.constraints.video.deviceId)
videoIsStillConnected = true;
if (device.deviceId == this.constraints.audio.deviceId)
audioIsStillConnected = true;
if (videoIsStillConnected && audioIsStillConnected)
break;
}
mediaError = { Type: 2, Message: "Audio Device used is disconnected." };
if (!videoIsStillConnected)
mediaError.Message = "Video Device used is disconnected.";
if (!videoIsStillConnected && !audioIsStillConnected)
mediaError.Message = "Audio and Video Device used is disconnected.";
if (!audioIsStillConnected || !videoIsStillConnected) {
componentRef.invokeMethodAsync("ReceiveError", mediaError);
}
stream_1.removeTrack(track);
return [2 /*return*/];
}
setTimeout(function () {
BlazorMediaInterop.HandleDeviceDisconnection(videoElement, componentRef);
}, 500);
return [2 /*return*/];
});
}); };
};
for (x = 0; x < tracks.length; x++) {
_loop_1(x);
}
});
}
return [2 /*return*/];
});
});
};
BlazorMediaInterop.HandleDeviceDisconnection = function (videoElement, componentRef) {
return __awaiter(this, void 0, void 0, function () {
var tracks, devices, videoIsStillConnected, audioIsStillConnected, y, device, mediaError;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
tracks = videoElement.mediaStream.getTracks();
if (!(videoElement.mediaStream && videoElement.mediaRecorder && videoElement.mediaRecorder.state != 'inactive')) return [3 /*break*/, 2];
return [4 /*yield*/, navigator.mediaDevices.enumerateDevices()];
case 1:
devices = _a.sent();
videoIsStillConnected = false;
audioIsStillConnected = false;
for (y = 0; y < devices.length; y++) {
device = devices[y];
if (device.deviceId == this.constraints.video.deviceId)
videoIsStillConnected = true;
if (device.deviceId == this.constraints.audio.deviceId)
audioIsStillConnected = true;
if (videoIsStillConnected && audioIsStillConnected)
break;
}
if (!audioIsStillConnected || !videoIsStillConnected) {
mediaError = { Type: 2, Message: "Audio Device used is disconnected." };
if (!videoIsStillConnected)
mediaError.Message = "Video Device used is disconnected.";
if (!videoIsStillConnected && !audioIsStillConnected)
mediaError.Message = "Audio and Video Device used is disconnected.";
componentRef.invokeMethodAsync("ReceiveError", mediaError);
}
if (videoElement.mediaStream) {
BlazorMediaInterop.Destroy(videoElement);
}
_a.label = 2;
case 2: return [2 /*return*/];
}
});
});
};
/// Defaults
BlazorMediaInterop.constraints = {
audio: {
Expand Down
60 changes: 37 additions & 23 deletions BlazorMedia/wwwroot/BlazorMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,33 +144,47 @@ namespace BlazorMedia {
if (videoElement.mediaStream) {
let stream = videoElement.mediaStream;
let tracks = stream.getTracks();
for (let x = 0; x < tracks.length; x++) {
const track = tracks[x];
tracks.forEach((track: MediaStreamTrack) => {
track.onended = async (ev: Event) => {
let devices = await navigator.mediaDevices.enumerateDevices();
var videoIsStillConnected = false;
var audioIsStillConnected = false;
for (let y = 0; y < devices.length; y++) {
const device = devices[y];
if (device.deviceId == this.constraints.video.deviceId)
videoIsStillConnected = true;
if (device.deviceId == this.constraints.audio.deviceId)
audioIsStillConnected = true;
if (videoIsStillConnected && audioIsStillConnected)
break;
}
let mediaError = { Type: 2, Message: "Audio Device used is disconnected." }
if (!videoIsStillConnected)
mediaError.Message = "Video Device used is disconnected.";
if (!videoIsStillConnected && !audioIsStillConnected)
mediaError.Message = "Audio and Video Device used is disconnected.";
if (!audioIsStillConnected || !videoIsStillConnected) {
componentRef.invokeMethodAsync("ReceiveError", mediaError);
}
stream.removeTrack(track);
setTimeout(function () {
BlazorMediaInterop.HandleDeviceDisconnection(videoElement, componentRef);
}, 500);
};
});
}
}

static async HandleDeviceDisconnection(videoElement: BlazorMediaVideoElement, componentRef: any) {

if (videoElement.mediaStream && videoElement.mediaRecorder && videoElement.mediaRecorder.state != 'inactive') {
let devices = await navigator.mediaDevices.enumerateDevices();
var videoIsStillConnected = false;
var audioIsStillConnected = false;

for (let y = 0; y < devices.length; y++) {
const device = devices[y];
if (device.deviceId == this.constraints.video.deviceId)
videoIsStillConnected = true;
if (device.deviceId == this.constraints.audio.deviceId)
audioIsStillConnected = true;
if (videoIsStillConnected && audioIsStillConnected)
break;
}

if (!audioIsStillConnected || !videoIsStillConnected) {
let mediaError = { Type: 2, Message: "Audio Device used is disconnected." }
if (!videoIsStillConnected)
mediaError.Message = "Video Device used is disconnected.";
if (!videoIsStillConnected && !audioIsStillConnected)
mediaError.Message = "Audio and Video Device used is disconnected.";
componentRef.invokeMethodAsync("ReceiveError", mediaError);
}

if (videoElement.mediaStream) {
BlazorMediaInterop.Destroy(videoElement);
}
}
}

}
}

0 comments on commit 88ab455

Please sign in to comment.