Skip to content

Commit

Permalink
screencap: iterate on screencap example
Browse files Browse the repository at this point in the history
  • Loading branch information
tmc committed Nov 23, 2023
1 parent bc05a8b commit 1904836
Showing 1 changed file with 69 additions and 11 deletions.
80 changes: 69 additions & 11 deletions macos/_examples/screencap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ import (
)

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

sc := screencapturekit.NewStreamConfiguration()
cf := screencapturekit.NewContentFilter()
s := screencapturekit.NewStreamWithFilterConfigurationDelegate(cf, sc, streamOutputHandler)
//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(streamOutputHandler, screencapturekit.StreamOutputTypeScreen, dispatchQueue, err)
ok := s.AddStreamOutputTypeSampleHandlerQueueError(captureHandler, screencapturekit.StreamOutputTypeScreen, dispatchQueue, err)
if !ok {
fmt.Println("s.AddStreamOutputTypeSampleHandlerQueueError", err)
}
Expand All @@ -34,27 +38,81 @@ func main() {
dispatch.Main()
}

type streamHandler struct{}
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 = (*streamHandler)(nil)
var _ screencapturekit.PStreamDelegate = (*streamHandler)(nil)
var _ screencapturekit.PStreamOutput = (*screenCaptureHandler)(nil)
var _ screencapturekit.PStreamDelegate = (*screenCaptureHandler)(nil)

// StreamOutput methods

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

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

// StreamDelegate methods

func (sh *streamHandler) StreamDidStopWithError(s screencapturekit.Stream, err foundation.Error) {
func (sh *screenCaptureHandler) StreamDidStopWithError(s screencapturekit.Stream, err foundation.Error) {
fmt.Println("StreamDidStopWithError", err)
}
func (sh *streamHandler) HasStreamDidStopWithError() bool {
func (sh *screenCaptureHandler) HasStreamDidStopWithError() bool {
fmt.Println("HasStreamDidStopWithError")
return true
}

0 comments on commit 1904836

Please sign in to comment.