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

Add screencapturekit #228

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions dispatch/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package dispatch
// void Dispatch_Release(void* queue);
// void Dispatch_Async(void* queue, uintptr_t task);
// void Dispatch_Sync(void* queue, uintptr_t task);
// void* Dispatch_Create_Queue(const char* label, int type);
// void Dispatch_Main();
import "C"
import (
"math"
Expand All @@ -30,6 +32,13 @@ const (
QueuePriorityBackground QueuePriority = math.MinInt16
)

type QueueType int

const (
QueueTypeSerial QueueType = 0
QueueTypeConcurrent QueueType = 1
)

// Returns the serial dispatch queue associated with the application’s main thread. [Full Topic]
//
// [Full Topic]: https://developer.apple.com/documentation/dispatch/1452921-dispatch_get_main_queue?language=objc
Expand Down Expand Up @@ -73,3 +82,17 @@ func runQueueTask(p C.uintptr_t) {
task()
h.Delete()
}

// Main executes blocks submitted to the main queue.
func Main() {
C.Dispatch_Main()
}

// Creates a new dispatch queue to which blocks can be submitted.
//
// [Full Topic]: https://developer.apple.com/documentation/dispatch/1453030-dispatch_queue_create?language=objc
func CreateQueue(label string, queueType QueueType) Queue {
cLabel := C.CString(label)
p := C.Dispatch_Create_Queue(cLabel, C.int(queueType))
return Queue{p}
}
13 changes: 12 additions & 1 deletion dispatch/queue.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,15 @@ void Dispatch_Sync(void* queue, uintptr_t task) {
dispatch_sync((dispatch_queue_t)queue, ^{
runQueueTask(task);
});
}
}

void Dispatch_Main() {
dispatch_main();
}

void* Dispatch_Create_Queue(const char* label, int type) {
if (type == 0) {
return dispatch_queue_create(label, DISPATCH_QUEUE_SERIAL);
}
return dispatch_queue_create(label, DISPATCH_QUEUE_CONCURRENT);
}
30 changes: 30 additions & 0 deletions generate/modules/enums/macos/screencapturekit
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
SCFrameStatusBlank 2
SCFrameStatusComplete 0
SCFrameStatusIdle 1
SCFrameStatusStarted 4
SCFrameStatusStopped 5
SCFrameStatusSuspended 3
SCStreamErrorAttemptToConfigState -3810
SCStreamErrorAttemptToStartStreamState -3807
SCStreamErrorAttemptToStopStreamState -3808
SCStreamErrorAttemptToUpdateFilterState -3809
SCStreamErrorFailedApplicationConnectionInterrupted -3805
SCStreamErrorFailedApplicationConnectionInvalid -3804
SCStreamErrorFailedNoMatchingApplicationContext -3806
SCStreamErrorFailedToStart -3802
SCStreamErrorInternalError -3811
SCStreamErrorInvalidParameter -3812
SCStreamErrorMissingEntitlements -3803
SCStreamErrorNoCaptureSource -3815
SCStreamErrorNoDisplayList -3814
SCStreamErrorNoWindowList -3813
SCStreamErrorRemovingStream -3816
SCStreamErrorUserDeclined -3801
SCStreamOutputTypeScreen 0
SCStreamErrorDomain com.apple.ScreenCaptureKit.SCStreamErrorDomain
SCStreamFrameInfoContentRect SCStreamUpdateFrameContentRect
SCStreamFrameInfoContentScale SCStreamUpdateFrameContentScale
SCStreamFrameInfoDirtyRects SCStreamUpdateFrameDirtyRect
SCStreamFrameInfoDisplayTime SCStreamUpdateFrameDisplayTime
SCStreamFrameInfoScaleFactor SCStreamUpdateFrameDisplayResolution
SCStreamFrameInfoStatus SCStreamUpdateFrameStatus
4 changes: 2 additions & 2 deletions generate/tools/enumexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func exportConstants(db *generate.SymbolCache, framework *modules.Module, platfo
continue
}
if s.Kind == "Constant" && s.Type == "Global Variable" {
stmt, err := s.Parse()
stmt, err := s.Parse(TargetPlatform)
if err != nil {
log.Fatalf("%s: %s in '%s'", s.Name, err, s.Declaration)
}
Expand All @@ -177,7 +177,7 @@ func exportConstants(db *generate.SymbolCache, framework *modules.Module, platfo
if typ.Declaration == "" {
return nil, false
}
typdef, err := typ.Parse()
typdef, err := typ.Parse(TargetPlatform)
if err != nil {
log.Fatalf("%s: %s in '%s'", typ.Name, err, typ.Declaration)
}
Expand Down
118 changes: 118 additions & 0 deletions macos/_examples/screencap/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package main

import (
"errors"
"fmt"

"github.com/progrium/macdriver/dispatch"
"github.com/progrium/macdriver/macos/coremedia"
"github.com/progrium/macdriver/macos/foundation"
"github.com/progrium/macdriver/macos/screencapturekit"
)

func main() {
captureHandler := &screenCaptureHandler{}
captureHandler.refreshCapturableWindows()

sc := screencapturekit.NewStreamConfiguration()
//cf := screencapturekit.NewContentFilter()

dispatch.MainQueue().DispatchAsync(func() {
cf := captureHandler.GetContentFilter()
s := screencapturekit.NewStreamWithFilterConfigurationDelegate(cf, sc, captureHandler)

var dispatchQueue dispatch.Queue
//dispatchQueue = dispatch.CreateQueue("com.example.queue", dispatch.QueueTypeSerial)
dispatchQueue = dispatch.MainQueue()
err := foundation.Error{}
ok := s.AddStreamOutputTypeSampleHandlerQueueError(captureHandler, screencapturekit.StreamOutputTypeScreen, dispatchQueue, err)
if !ok {
fmt.Println("s.AddStreamOutputTypeSampleHandlerQueueError", err)
}

fmt.Println("s.StartCaptureWithCompletionHandler")
s.StartCaptureWithCompletionHandler(func(err foundation.Error) {
fmt.Println("s.StartCaptureWithCompletionHandler", err)
})

dispatch.Main()
}

type CaptureType int

const (
CaptureTypeDisplay CaptureType = iota
CaptureTypeWindow CaptureType = iota
)

func (h *screenCaptureHandler) refreshCapturableWindows() {
fmt.Println("listing sharable content")
ch := make(chan struct{})
screencapturekit.ShareableContentClass.GetShareableContentWithCompletionHandler(func(sc screencapturekit.ShareableContent, err foundation.Error) {
defer close(ch)
h.availabileDisplays = sc.Displays()
if h.selectedDisplay == nil && len(h.availabileDisplays) > 0 {
h.selectedDisplay = &h.availabileDisplays[0]
}
h.availbleWindows = sc.Windows()
if h.selectedWindow == nil && len(h.availbleWindows) > 0 {
h.selectedWindow = &h.availbleWindows[0]
}
for _, app := range sc.Applications() {
fmt.Println("app", app.ApplicationName())
}
fmt.Println("done listing sharable content")
})
<-ch
}

type screenCaptureHandler struct {
availabileDisplays []screencapturekit.Display
availbleWindows []screencapturekit.Window

selectedDisplay *screencapturekit.Display
selectedWindow *screencapturekit.Window

captureType CaptureType
}

func (sh *screenCaptureHandler) GetContentFilter() screencapturekit.ContentFilter {
filter := screencapturekit.NewContentFilter()

switch sh.captureType {
case CaptureTypeDisplay:
display := sh.selectedDisplay
windows := []screencapturekit.IWindow{}
for _, w := range sh.availbleWindows {
windows = append(windows, w)
}
fmt.Println("display:", display)
fmt.Println("windows:", windows)
filter = screencapturekit.NewContentFilterWithDisplayIncludingWindows(display, windows)
case CaptureTypeWindow:
}
return filter
}

var _ screencapturekit.PStreamOutput = (*screenCaptureHandler)(nil)
var _ screencapturekit.PStreamDelegate = (*screenCaptureHandler)(nil)

// StreamOutput methods

func (sh *screenCaptureHandler) HasStreamDidOutputSampleBufferOfType() bool {
panic(errors.New("*streamHandler.HasStreamDidOutputSampleBufferOfType not implemented"))
}

func (sh *screenCaptureHandler) StreamDidOutputSampleBufferOfType(s screencapturekit.Stream, buf coremedia.SampleBufferRef, out screencapturekit.StreamOutputType) {
panic(errors.New("*streamHandler.StreamDidOutputSampleBufferOfType not implemented"))
}

// StreamDelegate methods

func (sh *screenCaptureHandler) StreamDidStopWithError(s screencapturekit.Stream, err foundation.Error) {
fmt.Println("StreamDidStopWithError", err)
}
func (sh *screenCaptureHandler) HasStreamDidStopWithError() bool {
fmt.Println("HasStreamDidStopWithError")
return true
}
82 changes: 82 additions & 0 deletions macos/screencapturekit/content_filter.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading