go-media-devices-state icon indicating copy to clipboard operation
go-media-devices-state copied to clipboard

Does not detect change in state

Open tradiff opened this issue 3 years ago • 1 comments

This module doesn't detect a change in camera state. Here is an example program which polls the module every 1 second. While the program is running, if I start or stop using the camera, the output never reflects the new state. Terminating my program and starting it again does reflect the new state.

package main

import (
	"log"
	"time"

	mediaDevices "github.com/antonfisher/go-media-devices-state"
)

func main() {
	for {
		isCameraOn, err := mediaDevices.IsCameraOn()
		if err != nil {
			log.Println("Error:", err)
		} else {
			log.Println("Is camera on:", isCameraOn)
		}

		time.Sleep(1 * time.Second)
	}
}

tradiff avatar May 02 '21 15:05 tradiff

Hi @TravisTX,

Yes, can confirm i see the same. Interesting that if i run it like:

while sleep 1; do go run ./cmd/demo.go; done;

it detects the camera properly. One workaround can be spawning a process for each check:

./cmd/demo.go:

package main

import (
	"fmt"
	"os/exec"
	"time"
)

func main() {
	for {
		checkCmd := exec.Command("bash", "-c", "go run /tmp/check.go")
		checkOut, _ := checkCmd.Output()
		fmt.Printf("Is camera on: %v", string(checkOut))
		time.Sleep(1 * time.Second)
	}
}

/tmp/check.go:

package main

import (
	"fmt"

	mediaDevices "github.com/antonfisher/go-media-devices-state"
)

func main() {
	isCameraOn, _ := mediaDevices.IsCameraOn()
	fmt.Println(isCameraOn)
}

and run it like:

go run ./cmd/demo.go

There seem to be some kind of caching for running in the same process 🤔

antonfisher avatar May 04 '21 00:05 antonfisher