0
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");
        }
      }),
    );
  }

console: enter image description here

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);
    });
  }
  

Output: enter image description here

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.

NIKITA AGARWAL
  • 141
  • 1
  • 7
  • You shouldn't mix Promises, async/await, and Observables. Turn your Promises into Observables using `from` or `defer`. – Will Alexander Jan 23 '22 at 14:58
  • You're not waiting for what is happening in the `devices.forEach( async(device) => {` loop. – Bergi Jan 23 '22 at 15:33
  • Do away with the `.then()` and the `forEach` and use `const devices = await navigator.mediaDevices.enumerateDevices(); for (const device of devices) { …` – Bergi Jan 23 '22 at 15:35

0 Answers0