Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use AVCaptureSession API for macos shadow servers #1872

Open
totaam opened this issue Jun 10, 2018 · 5 comments
Open

use AVCaptureSession API for macos shadow servers #1872

totaam opened this issue Jun 10, 2018 · 5 comments
Labels
enhancement New feature or request macos shadow
Milestone

Comments

@totaam
Copy link
Collaborator

totaam commented Jun 10, 2018

The API we currently use (CGRegisterScreenRefreshCallback)is deprecated, we have to switch to AVCaptureSession and AVCaptureScreenInput.

This may be blocked by the same bug as #1231, see [/attachment/ticket/1231/webcam_avfoundation.py].

@totaam
Copy link
Collaborator Author

totaam commented May 23, 2020

Found a better code sample to start from : PyImageSnap.
It doesn't work for me, but that could just be virtualbox's USB pass-through causing problems.

@totaam
Copy link
Collaborator Author

totaam commented May 23, 2020

And a screen recording example here https://gist.github.com/timsutton/0c6439eb6eb1621a5964:

#!/usr/bin/python

# pylint: disable-msg=e1101,e0611
import time
import AVFoundation as AVF
import Quartz

from Foundation import NSObject, NSURL

def main():
    display_id = Quartz.CGMainDisplayID()

    session = AVF.AVCaptureSession.alloc().init()
    screen_input = AVF.AVCaptureScreenInput.alloc().initWithDisplayID_(display_id)
    file_output = AVF.AVCaptureMovieFileOutput.alloc().init()

    session.addInput_(screen_input)
    session.addOutput_(file_output)
    session.startRunning()

    file_url = NSURL.fileURLWithPath_('foo.mov')
    # Cheat and pass a dummy delegate object where normally we'd have a
    # AVCaptureFileOutputRecordingDelegate
    file_url = file_output.startRecordingToOutputFileURL_recordingDelegate_(
                file_url, NSObject.alloc().init())
    time.sleep(10)
    session.stopRunning()

if __name__ == '__main__':
    main()

@totaam
Copy link
Collaborator Author

totaam commented May 23, 2020

Managed to put together a screen recording-to-delegate example based on the one above

#!/usr/bin/python

import time
import AVFoundation as AVF
import Quartz
import libdispatch

from Foundation import NSObject, NSURL

def main():
    display_id = Quartz.CGMainDisplayID()

    session = AVF.AVCaptureSession.alloc().init()
    screen_input = AVF.AVCaptureScreenInput.alloc().initWithDisplayID_(display_id)
    screen_input.setCapturesCursor_(True)
    screen_input.setCapturesMouseClicks_(True)
    screen_input.setScaleFactor_(1.0)
    output = AVF.AVCaptureVideoDataOutput.alloc().init()
    output.setAlwaysDiscardsLateVideoFrames_(True)
    print("codecs:", output.availableVideoCodecTypes())
    print("pixel formats:", output.availableVideoCVPixelFormatTypes())

    class SampleBufferDelegate(NSObject):
        def captureOutput_didOutputSampleBuffer_fromConnection_(self, captureOutput, sampleBuffer, connection):
            print("captureOutput: captureOutput=%s, sampleBuffer=%s, connection=%s",
                  captureOutput, sampleBuffer, connection,
                )
        def captureOutput_didDropSampleBuffer_fromConnection_(self, captureOutput, sampleBuffer, connection):
            print("dropped a frame!")
    delegate = SampleBufferDelegate.alloc().init()

    queue = libdispatch.dispatch_queue_create(b"Image Queue", None)
    output.setSampleBufferDelegate_queue_(delegate, queue)

    session.addInput_(screen_input)
    session.addOutput_(output)
    session.startRunning()

    print("connections=", output.connections())

    time.sleep(10)
    session.stopRunning()

if __name__ == '__main__':
    main()

@totaam
Copy link
Collaborator Author

totaam commented May 24, 2020

2020-05-24 06:12:21: antoine uploaded file avf.py (5.2 KiB)

more complete example

@totaam totaam added enhancement New feature or request macos shadow labels Jan 23, 2021
@totaam totaam added this to the 5.0 milestone Jan 23, 2021
@totaam
Copy link
Collaborator Author

totaam commented Jan 28, 2022

This OBS commit might be what we need: obsproject/obs-studio@551c54b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request macos shadow
Projects
None yet
Development

No branches or pull requests

1 participant