godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.16k stars 97 forks source link

Add permission-related methods to CameraServer #10829

Open j20001970 opened 1 month ago

j20001970 commented 1 month ago

Describe the project you are working on

GDExtension library that involves accessing camera devices for frames capture.

Describe the problem or limitation you are having in your project

Many platforms (AFAIK: Android, iOS, macOS, Linux with XDG Portal and PipeWire when running in sandbox, Web) require explicit user interaction for camera permission before accessing camera devices for frames capture. While CameraServer is not supported on all platforms, there has been progress on implementing support for Linux.

But with currently available methods provided by CameraServer, there is no way no know if camera permission is granted in advance, and requesting permission is handled in activate_feed, if user refused camera access, there is no way to react to it.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

The following enhancement should be able to solve the limitation stated above:

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

...

func _ready():
    CameraServer.permission_changed.connect(self.on_camera_permission_changed)
    start_camera()

func on_camera_permission_changed(granted: bool):
    if not granted and camera_started:
        # Do something about it
    elif granted and camera_started:
        # Access camera feeds

func start_camera():
    camera_started = true
    if CameraServer.permission_granted():
        # Access camera feeds
    else:
        CameraServer.request_permission()

If this enhancement will not be used often, can it be worked around with a few lines of script?

Permission-related operation usually requires access to platform-specific API.

While OS.request_permission exists, it is currently specific to Android, and IMO leaving camera permission handling to CameraServer feels more consistent.

Is there a reason why this should be core and not an add-on in the asset library?

It is absolutely possible to handle camera permission with GDExtension/GDNative library, but it would feel redundant, and since CameraServer is already there, it wouldn't hurt to add permission-related methods to CameraServer for supported platforms.

Calinou commented 1 month ago

On Windows, you can also prevent apps from accessing the camera (or microphone) in the system settings, although no explicit permission is required when access is allowed. We could make the permission grant status return false in this case.