@@ -1354,24 +1354,34 @@ func (d *Daemon) getOrStartPreparedCaptureGroup(ctx context.Context, entry *acti
13541354 if d .captureGroups == nil {
13551355 d .captureGroups = make (map [videoCaptureKey ]* videoCaptureGroup )
13561356 }
1357- if group := d .captureGroups [key ]; group != nil {
1357+ group := d .captureGroups [key ]
1358+ var captureCtx context.Context
1359+ var captureCancel context.CancelFunc
1360+ if group != nil {
13581361 if group .resetReservedBy != nil && group .resetReservedBy != entry {
13591362 d .mu .Unlock ()
13601363 return nil , 0 , fmt .Errorf ("%w: %dx%d" , errCaptureGroupResetReserved , key .maxWidth , key .maxHeight )
13611364 }
1362- entry .captureGroup = group
1363- broadcast := group .broadcast
1364- d .mu .Unlock ()
1365- preparation .Close ()
1366- if broadcast == nil {
1367- return nil , 0 , fmt .Errorf ("capture group %dx%d has no broadcast" , key .maxWidth , key .maxHeight )
1365+ if group .resetReservedBy == entry && group .broadcast == nil && group .capture == nil && group .cancel == nil {
1366+ captureCtx , captureCancel = context .WithCancel (context .Background ())
1367+ group .cancel = captureCancel
1368+ group .resetReservedBy = nil
1369+ } else {
1370+ entry .captureGroup = group
1371+ broadcast := group .broadcast
1372+ d .mu .Unlock ()
1373+ preparation .Close ()
1374+ if broadcast == nil {
1375+ return nil , 0 , fmt .Errorf ("capture group %dx%d has no broadcast" , key .maxWidth , key .maxHeight )
1376+ }
1377+ return broadcast , group .minimumVideoLead , nil
13681378 }
1369- return broadcast , group .minimumVideoLead , nil
1379+ } else {
1380+ captureCtx , captureCancel = context .WithCancel (context .Background ())
1381+ group = & videoCaptureGroup {key : key , cancel : captureCancel }
1382+ d .captureGroups [key ] = group
1383+ entry .captureGroup = group
13701384 }
1371-
1372- captureCtx , captureCancel := context .WithCancel (context .Background ())
1373- group := & videoCaptureGroup {key : key , cancel : captureCancel }
1374- d .captureGroups [key ] = group
13751385 entry .captureGroup = group
13761386 d .mu .Unlock ()
13771387
@@ -1464,26 +1474,33 @@ func (d *Daemon) getOrStartCaptureGroup(entry *activeStream, restoreToken, devic
14641474 if d .captureGroups == nil {
14651475 d .captureGroups = make (map [videoCaptureKey ]* videoCaptureGroup )
14661476 }
1467- if group := d .captureGroups [key ]; group != nil {
1477+ group := d .captureGroups [key ]
1478+ var captureCtx context.Context
1479+ var captureCancel context.CancelFunc
1480+ if group != nil {
14681481 if group .resetReservedBy != nil && group .resetReservedBy != entry {
14691482 d .mu .Unlock ()
14701483 return nil , fmt .Errorf ("%w: %dx%d" , errCaptureGroupResetReserved , key .maxWidth , key .maxHeight )
14711484 }
1472- entry .captureGroup = group
1473- broadcast := group .broadcast
1474- d .mu .Unlock ()
1475- if broadcast == nil {
1476- return nil , fmt .Errorf ("capture group %dx%d has no broadcast" , key .maxWidth , key .maxHeight )
1485+ if group .resetReservedBy == entry && group .broadcast == nil && group .capture == nil && group .cancel == nil {
1486+ captureCtx , captureCancel = context .WithCancel (context .Background ())
1487+ group .cancel = captureCancel
1488+ group .resetReservedBy = nil
1489+ } else {
1490+ entry .captureGroup = group
1491+ broadcast := group .broadcast
1492+ d .mu .Unlock ()
1493+ if broadcast == nil {
1494+ return nil , fmt .Errorf ("capture group %dx%d has no broadcast" , key .maxWidth , key .maxHeight )
1495+ }
1496+ return broadcast , nil
14771497 }
1478- return broadcast , nil
1498+ } else {
1499+ captureCtx , captureCancel = context .WithCancel (context .Background ())
1500+ group = & videoCaptureGroup {key : key , cancel : captureCancel }
1501+ d .captureGroups [key ] = group
1502+ entry .captureGroup = group
14791503 }
1480-
1481- // Publish the group and cancellation hook before entering the display portal
1482- // or launching GStreamer. A targeted disconnect can then cancel an orphaned
1483- // startup without affecting captures used by other canvas groups.
1484- captureCtx , captureCancel := context .WithCancel (context .Background ())
1485- group := & videoCaptureGroup {key : key , cancel : captureCancel }
1486- d .captureGroups [key ] = group
14871504 entry .captureGroup = group
14881505 d .mu .Unlock ()
14891506
0 commit comments