improve daemon's toggle
This commit is contained in:
+24
-38
@@ -10,7 +10,6 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/leonardotrapani/hyprvoice/internal/bus"
|
"github.com/leonardotrapani/hyprvoice/internal/bus"
|
||||||
"github.com/leonardotrapani/hyprvoice/internal/notify"
|
"github.com/leonardotrapani/hyprvoice/internal/notify"
|
||||||
@@ -35,8 +34,7 @@ type Daemon struct {
|
|||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
|
|
||||||
pipeline pipeline.Pipeline
|
pipeline pipeline.Pipeline
|
||||||
pipelineCancel context.CancelFunc
|
actionChannel chan<- pipeline.Action
|
||||||
statusCh <-chan Status
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(n notify.Notifier) *Daemon {
|
func New(n notify.Notifier) *Daemon {
|
||||||
@@ -61,14 +59,12 @@ func (d *Daemon) Status() Status {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *Daemon) startStatusReader(ctx context.Context, statusCh <-chan Status) {
|
func (d *Daemon) startStatusReader(ctx context.Context, statusCh <-chan Status) {
|
||||||
go func() {
|
|
||||||
defer func() {
|
defer func() {
|
||||||
// Always clean up when this goroutine exits
|
// Always clean up when this goroutine exits
|
||||||
d.mu.Lock()
|
d.mu.Lock()
|
||||||
d.status = Idle
|
d.status = Idle
|
||||||
d.statusCh = nil
|
|
||||||
d.pipeline = nil
|
d.pipeline = nil
|
||||||
d.pipelineCancel = nil
|
d.actionChannel = nil
|
||||||
d.mu.Unlock()
|
d.mu.Unlock()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -92,7 +88,6 @@ func (d *Daemon) startStatusReader(ctx context.Context, statusCh <-chan Status)
|
|||||||
return // Context cancelled
|
return // Context cancelled
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Daemon) Run() error {
|
func (d *Daemon) Run() error {
|
||||||
@@ -139,7 +134,6 @@ func (d *Daemon) Run() error {
|
|||||||
log.Printf("Accept error: %v", err)
|
log.Printf("Accept error: %v", err)
|
||||||
return fmt.Errorf("accept failed: %w", err)
|
return fmt.Errorf("accept failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
go d.handle(c)
|
go d.handle(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -165,7 +159,7 @@ func (d *Daemon) handle(c net.Conn) {
|
|||||||
fmt.Fprint(c, "OK toggled\n")
|
fmt.Fprint(c, "OK toggled\n")
|
||||||
case 's':
|
case 's':
|
||||||
status := d.Status()
|
status := d.Status()
|
||||||
fmt.Fprintf(c, "STATUS recording=%s\n", status)
|
fmt.Fprintf(c, "STATUS status=%s\n", status)
|
||||||
case 'v':
|
case 'v':
|
||||||
fmt.Fprintf(c, "STATUS proto=%s\n", bus.ProtoVer)
|
fmt.Fprintf(c, "STATUS proto=%s\n", bus.ProtoVer)
|
||||||
case 'q':
|
case 'q':
|
||||||
@@ -178,44 +172,36 @@ func (d *Daemon) handle(c net.Conn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *Daemon) toggle() {
|
func (d *Daemon) toggle() {
|
||||||
d.mu.Lock()
|
|
||||||
defer d.mu.Unlock()
|
|
||||||
|
|
||||||
var notification func()
|
|
||||||
|
|
||||||
switch d.status {
|
switch d.status {
|
||||||
case Idle:
|
case Idle:
|
||||||
ctx, cancel := context.WithTimeout(d.ctx, 5*time.Minute)
|
// Defensive: if a prior pipeline exists, stop and wait before starting a new one
|
||||||
p := pipeline.New()
|
if d.pipeline != nil {
|
||||||
d.pipeline = p
|
d.pipeline.Stop()
|
||||||
d.pipelineCancel = cancel
|
d.pipeline = nil
|
||||||
d.statusCh = p.Run(ctx)
|
}
|
||||||
notification = d.notifier.RecordingStarted
|
|
||||||
|
|
||||||
// Start status reader for this pipeline
|
p := pipeline.New()
|
||||||
d.startStatusReader(ctx, d.statusCh)
|
statusCh, actionCh := p.Run(d.ctx)
|
||||||
|
|
||||||
|
d.pipeline = p
|
||||||
|
d.actionChannel = actionCh
|
||||||
|
|
||||||
|
go d.notifier.RecordingStarted()
|
||||||
|
|
||||||
|
go d.startStatusReader(d.ctx, statusCh)
|
||||||
|
|
||||||
case Recording:
|
case Recording:
|
||||||
if d.pipelineCancel != nil {
|
go d.notifier.RecordingEnded()
|
||||||
d.pipelineCancel() // Context cleanup handles the rest
|
d.pipeline.Stop()
|
||||||
}
|
|
||||||
notification = d.notifier.RecordingEnded
|
|
||||||
|
|
||||||
case Transcribing:
|
case Transcribing:
|
||||||
if d.pipeline != nil {
|
select {
|
||||||
d.pipeline.Inject()
|
case d.actionChannel <- pipeline.Inject:
|
||||||
|
default:
|
||||||
}
|
}
|
||||||
// No notification for injection start
|
|
||||||
|
|
||||||
case Injecting:
|
case Injecting:
|
||||||
if d.pipelineCancel != nil {
|
go d.notifier.RecordingEnded()
|
||||||
d.pipelineCancel() // Context cleanup handles the rest
|
d.pipeline.Stop() // aborted during injection
|
||||||
}
|
|
||||||
notification = d.notifier.RecordingEnded
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send notification after releasing lock
|
|
||||||
if notification != nil {
|
|
||||||
go notification()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package pipeline
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,38 +16,48 @@ const (
|
|||||||
Injecting Status = "injecting"
|
Injecting Status = "injecting"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Action represents commands sent to the pipeline to drive state
|
||||||
|
// transitions or trigger work. Keeping this separate from Status
|
||||||
|
// preserves directionality: callers send Actions, pipeline emits Statuses.
|
||||||
|
type Action int
|
||||||
|
|
||||||
|
const (
|
||||||
|
Inject Action = iota
|
||||||
|
)
|
||||||
|
|
||||||
type Pipeline interface {
|
type Pipeline interface {
|
||||||
Run(ctx context.Context) <-chan Status
|
Run(ctx context.Context) (<-chan Status, chan<- Action)
|
||||||
Inject()
|
Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
type pipeline struct {
|
type pipeline struct {
|
||||||
injectCh chan struct{}
|
actionCh chan Action
|
||||||
|
wg sync.WaitGroup
|
||||||
|
cancel context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() Pipeline {
|
func New() Pipeline {
|
||||||
return &pipeline{
|
return &pipeline{
|
||||||
injectCh: make(chan struct{}, 1),
|
actionCh: make(chan Action, 1),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pipeline) Run(ctx context.Context) <-chan Status {
|
func (p *pipeline) Run(ctx context.Context) (<-chan Status, chan<- Action) {
|
||||||
statusCh := make(chan Status, 1)
|
statusCh := make(chan Status, 1)
|
||||||
go p.run(ctx, statusCh)
|
runCtx, cancel := context.WithTimeout(ctx, 5*time.Minute)
|
||||||
return statusCh
|
p.cancel = cancel
|
||||||
}
|
p.wg.Add(1)
|
||||||
|
go p.run(runCtx, statusCh)
|
||||||
func (p *pipeline) Inject() {
|
return statusCh, p.actionCh
|
||||||
select {
|
|
||||||
case p.injectCh <- struct{}{}:
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pipeline) run(ctx context.Context, statusCh chan<- Status) {
|
func (p *pipeline) run(ctx context.Context, statusCh chan<- Status) {
|
||||||
defer close(statusCh)
|
defer func() {
|
||||||
|
// Ensure status channel is closed and wg is decremented
|
||||||
|
close(statusCh)
|
||||||
|
p.wg.Done()
|
||||||
|
}()
|
||||||
|
|
||||||
// Start recording
|
|
||||||
log.Printf("Pipeline: Starting recording")
|
log.Printf("Pipeline: Starting recording")
|
||||||
statusCh <- Recording
|
statusCh <- Recording
|
||||||
|
|
||||||
@@ -60,9 +71,12 @@ func (p *pipeline) run(ctx context.Context, statusCh chan<- Status) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for injection or timeout
|
// TODO: when integrating the recorder, ensure its context is cancelled here on exit paths
|
||||||
|
// Wait for an action or timeout
|
||||||
select {
|
select {
|
||||||
case <-p.injectCh:
|
case action := <-p.actionCh:
|
||||||
|
switch action {
|
||||||
|
case Inject:
|
||||||
log.Printf("Pipeline: Injection started")
|
log.Printf("Pipeline: Injection started")
|
||||||
statusCh <- Injecting
|
statusCh <- Injecting
|
||||||
|
|
||||||
@@ -70,11 +84,14 @@ func (p *pipeline) run(ctx context.Context, statusCh chan<- Status) {
|
|||||||
select {
|
select {
|
||||||
case <-time.After(1 * time.Second):
|
case <-time.After(1 * time.Second):
|
||||||
log.Printf("Pipeline: Injection complete")
|
log.Printf("Pipeline: Injection complete")
|
||||||
statusCh <- Idle // Instead of Completed
|
statusCh <- Idle
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
log.Printf("Pipeline: Stopped during injection")
|
log.Printf("Pipeline: Stopped during injection")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
// Unknown action: ignore for now
|
||||||
|
}
|
||||||
|
|
||||||
case <-time.After(10 * time.Second): // use context timeout
|
case <-time.After(10 * time.Second): // use context timeout
|
||||||
log.Printf("Pipeline: Auto-timeout, completing")
|
log.Printf("Pipeline: Auto-timeout, completing")
|
||||||
@@ -85,3 +102,10 @@ func (p *pipeline) run(ctx context.Context, statusCh chan<- Status) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *pipeline) Stop() {
|
||||||
|
if p.cancel != nil {
|
||||||
|
p.cancel()
|
||||||
|
}
|
||||||
|
p.wg.Wait()
|
||||||
|
}
|
||||||
|
|||||||
@@ -101,12 +101,8 @@ func (r *Recorder) Stop() error {
|
|||||||
if cancel != nil {
|
if cancel != nil {
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Recorder) Wait() {
|
|
||||||
r.wg.Wait()
|
r.wg.Wait()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Recorder) captureLoop(ctx context.Context, frameCh chan<- AudioFrame, errCh chan<- error) {
|
func (r *Recorder) captureLoop(ctx context.Context, frameCh chan<- AudioFrame, errCh chan<- error) {
|
||||||
@@ -144,16 +140,16 @@ func (r *Recorder) captureLoop(ctx context.Context, frameCh chan<- AudioFrame, e
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r.mu.Lock()
|
|
||||||
r.cmd = cmd
|
|
||||||
r.mu.Unlock()
|
|
||||||
|
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
r.emitErr(errCh, fmt.Errorf("start pw-record: %w", err))
|
r.emitErr(errCh, fmt.Errorf("start pw-record: %w", err))
|
||||||
r.requestCancel()
|
r.requestCancel()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r.mu.Lock()
|
||||||
|
r.cmd = cmd
|
||||||
|
r.mu.Unlock()
|
||||||
|
|
||||||
// Log stderr lines to aid diagnostics.
|
// Log stderr lines to aid diagnostics.
|
||||||
go func() {
|
go func() {
|
||||||
scanner := bufio.NewScanner(stderr)
|
scanner := bufio.NewScanner(stderr)
|
||||||
@@ -221,7 +217,6 @@ func (r *Recorder) emitErr(errCh chan<- error, err error) {
|
|||||||
select {
|
select {
|
||||||
case errCh <- err:
|
case errCh <- err:
|
||||||
default:
|
default:
|
||||||
// Best-effort; avoid blocking
|
|
||||||
}
|
}
|
||||||
log.Printf("Recording error: %v", err)
|
log.Printf("Recording error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -246,9 +241,6 @@ func CheckPipeWireAvailable(ctx context.Context) error {
|
|||||||
return fmt.Errorf("pw-record not found: %w (install pipewire-tools)", err)
|
return fmt.Errorf("pw-record not found: %w (install pipewire-tools)", err)
|
||||||
}
|
}
|
||||||
// Use a short timeout to avoid hangs on misconfigured systems.
|
// Use a short timeout to avoid hangs on misconfigured systems.
|
||||||
if ctx == nil {
|
|
||||||
ctx = context.Background()
|
|
||||||
}
|
|
||||||
checkCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
checkCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
cmd := exec.CommandContext(checkCtx, "pw-cli", "info")
|
cmd := exec.CommandContext(checkCtx, "pw-cli", "info")
|
||||||
|
|||||||
Reference in New Issue
Block a user