github.com/gkstretton/dark/services/goo@v0.0.0-20231114224855-2d1a2074d446/livecapture/orchestration.go (about) 1 package livecapture 2 3 import ( 4 "fmt" 5 "sync" 6 7 "github.com/gkstretton/dark/services/goo/session" 8 ) 9 10 var ( 11 rec *recorder 12 ) 13 14 type recorder struct { 15 sm *session.SessionManager 16 recording bool 17 stopRecording chan bool 18 mutex *sync.RWMutex 19 } 20 21 func Start(sm *session.SessionManager) { 22 rec = &recorder{ 23 sm: sm, 24 recording: false, 25 stopRecording: make(chan bool), 26 mutex: &sync.RWMutex{}, 27 } 28 go rec.run() 29 } 30 31 func (r *recorder) isRecording() bool { 32 r.mutex.RLock() 33 defer r.mutex.RUnlock() 34 return r.recording 35 } 36 37 func (r *recorder) setIsRecording(b bool) { 38 r.mutex.Lock() 39 defer r.mutex.Unlock() 40 r.recording = b 41 } 42 43 func (r *recorder) run() { 44 r.evaluateAction() 45 46 // Listen for ongoing begin/end session events 47 ch := r.sm.SubscribeToEvents() 48 for { 49 <-ch 50 r.evaluateAction() 51 } 52 } 53 54 func (r *recorder) evaluateAction() { 55 latestSession, err := r.sm.GetLatestSession() 56 if err != nil { 57 fmt.Printf("failed to GetLatestSession in livecapture: %v\n", err) 58 return 59 } 60 if latestSession == nil { 61 // no session 62 return 63 } 64 65 // If we're recording but shouldn't be 66 if r.isRecording() && (latestSession.Complete || latestSession.Paused) { 67 // Stop recording 68 r.stopRecording <- true 69 } 70 71 // If we're not recording but should be 72 if !r.isRecording() && (!latestSession.Complete && !latestSession.Paused) { 73 // Start recording 74 go r.record(latestSession.Id) 75 } 76 }