getPermissions(name: PermissionName): Observable<PermissionState> {
return from(
navigator.permissions.query({ name }).then((permission: PermissionStatus) => {
console.log(name," permission is ",permission.state);
return permission.state;
}),
);
}
getMediaPermissions = async (inputtype : any) => {
await navigator.mediaDevices.enumerateDevices().then(async devices =>
devices.forEach( async(device) => {
if(device.kind == inputtype && device.label) {
//return "present";
console.log(inputtype);
if(inputtype == 'videoinput'){
//console.log("getting ",this.getPermissions("camera"));
return await this.getPermissions("camera").toPromise();
}
else if(inputtype == 'audioinput'){
return await this.getPermissions("microphone").toPromise();
}
}
//if(device.kind == 'videoinput' && device.label) return "granted";
}
))
.catch(err => {
return "denied";
});
return "denied";
}
function call
async getMediaPermissionsCall() {
this.subscriptions.push(
combineLatest(
await this.localMediaService.getMediaPermissions('videoinput'),
await this.localMediaService.getMediaPermissions('audioinput'),
).subscribe(([cameraPermissions, microphonePermissions]) => {
if (cameraPermissions === 'granted' && microphonePermissions === 'granted') {
this.getMediaDevices();
console.log("granted");
} else if (cameraPermissions === 'prompt' || microphonePermissions === 'prompt') {
navigator.mediaDevices.getUserMedia({ audio: true, video: true }).then((stream: MediaStream) => {
stream.getTracks().forEach((track: MediaStreamTrack) => track.stop());
this.getMediaPermissions();
console.log("prompt");
});
} else if (cameraPermissions === 'denied' || microphonePermissions === 'denied') {
alert('Browser denied permission');
console.log("denied");
}
else{
console.log("error");
}
}),
);
}
If I change the function call to to just check the data I am getting, I get denied as result
async getMediaPermissionsCall() {
await this.localMediaService.getMediaPermissions('videoinput').then(data=>{
console.log(data);
});
}
However, it's clear that there is no issue in checking the permission status. The problem is with the flow of the code. The service is returning "denied" without waiting for the promise. I tried few more ways, but still couldn't figure out.