MCPcopy Index your code
hub / github.com/Monibuca/engine / run

Method run

stream.go:443–688  ·  view source on GitHub ↗

流状态处理中枢,包括接收订阅发布指令等

()

Source from the content-addressed store, hash-verified

441
442// 流状态处理中枢,包括接收订阅发布指令等
443func (s *Stream) run() {
444 EventBus <- SEcreate{StreamEvent{Event[*Stream]{Target: s, Time: time.Now()}}}
445 pulseTicker := time.NewTicker(EngineConfig.PulseInterval)
446 defer pulseTicker.Stop()
447 var timeOutInfo zap.Field
448 var timeStart time.Time
449 for pulseSuber := make(map[ISubscriber]struct{}); ; s.checkRunCost(timeStart, timeOutInfo) {
450 select {
451 case <-pulseTicker.C:
452 timeStart = time.Now()
453 timeOutInfo = zap.String("type", "pulse")
454 for sub := range pulseSuber {
455 sub.OnEvent(PulseEvent{CreateEvent(struct{}{})})
456 }
457 case <-s.timeout.C:
458 timeStart = time.Now()
459 timeOutInfo = zap.String("state", s.State.String())
460 if s.State == STATE_PUBLISHING || s.State == STATE_WAITTRACK {
461 for sub := range s.Subscribers.internal {
462 if sub.IsClosed() {
463 delete(s.Subscribers.internal, sub)
464 s.Info("innersuber -1", zap.Int("remains", len(s.Subscribers.internal)))
465 }
466 }
467 for sub := range s.Subscribers.public {
468 if sub.IsClosed() {
469 s.onSuberClose(sub)
470 }
471 }
472 if !s.NeverTimeout {
473 lost := false
474 trackCount := 0
475 timeout := s.PublishTimeout
476 if s.IsPause {
477 timeout = s.PauseTimeout
478 }
479 s.Tracks.Range(func(name string, t common.Track) {
480 trackCount++
481 switch t.(type) {
482 case *track.Video, *track.Audio:
483 // track 超过一定时间没有更新数据了
484 if lastWriteTime := t.LastWriteTime(); !lastWriteTime.IsZero() && time.Since(lastWriteTime) > timeout {
485 s.Warn("track timeout", zap.String("name", name), zap.Time("last writetime", lastWriteTime), zap.Duration("timeout", timeout))
486 lost = true
487 }
488 }
489 })
490 if !lost {
491 if trackCount == 0 {
492 s.Warn("no tracks")
493 if time.Since(s.StartTime) > timeout {
494 lost = true
495 }
496 } else if s.Publisher != nil && s.Publisher.IsClosed() {
497 s.Warn("publish is closed", zap.Error(context.Cause(s.publisher)), zap.String("ptr", fmt.Sprintf("%p", s.publisher.Context)))
498 lost = true
499 }
500 }

Callers 1

findOrCreateStreamFunction · 0.45

Calls 15

checkRunCostMethod · 0.95
onSuberCloseMethod · 0.95
actionMethod · 0.95
resetTimerMethod · 0.95
IsClosedMethod · 0.95
AcceptMethod · 0.95
GetPublisherConfigMethod · 0.95
CreateEventFunction · 0.85
AbortWaitMethod · 0.80
RejectMethod · 0.80
ResolveMethod · 0.80
WaitMethod · 0.80

Tested by

no test coverage detected