mirror of
				https://github.com/Monibuca/engine.git
				synced 2025-10-31 11:56:25 +08:00 
			
		
		
		
	修复配置合并bug
This commit is contained in:
		
							
								
								
									
										119
									
								
								stream.go
									
									
									
									
									
								
							
							
						
						
									
										119
									
								
								stream.go
									
									
									
									
									
								
							| @@ -62,6 +62,9 @@ var StreamFSM = [STATE_DESTROYED + 1]map[StreamAction]StreamState{ | ||||
| 	{ | ||||
| 		ACTION_TIMEOUT: STATE_DESTROYED, | ||||
| 	}, | ||||
| 	{ | ||||
|  | ||||
| 	}, | ||||
| } | ||||
|  | ||||
| // Streams 所有的流集合 | ||||
| @@ -124,11 +127,11 @@ func findOrCreateStream(streamPath string, waitTimeout time.Duration) (s *Stream | ||||
| 	} | ||||
| 	p := strings.Split(u.Path, "/") | ||||
| 	if len(p) < 2 { | ||||
| 		log.Warnln(Red("Stream Path Format Error:"), streamPath) | ||||
| 		log.Warn(Red("Stream Path Format Error:"), streamPath) | ||||
| 		return nil, false | ||||
| 	} | ||||
| 	if s, ok := Streams.Map[u.Path]; ok { | ||||
| 		s.Debugln(Green("Stream Found")) | ||||
| 		s.Debug(Green("Stream Found")) | ||||
| 		return s, false | ||||
| 	} else { | ||||
| 		p := strings.Split(u.Path, "/") | ||||
| @@ -138,7 +141,7 @@ func findOrCreateStream(streamPath string, waitTimeout time.Duration) (s *Stream | ||||
| 			StreamName: util.LastElement(p), | ||||
| 			Entry:      log.WithField("stream", u.Path), | ||||
| 		} | ||||
| 		s.Infoln("created:", streamPath) | ||||
| 		s.Info("created") | ||||
| 		s.WaitTimeout = waitTimeout | ||||
| 		Streams.Map[u.Path] = s | ||||
| 		s.actionChan = make(chan any, 1) | ||||
| @@ -152,43 +155,48 @@ func findOrCreateStream(streamPath string, waitTimeout time.Duration) (s *Stream | ||||
| } | ||||
|  | ||||
| func (r *Stream) action(action StreamAction) bool { | ||||
| 	r.Tracef("action:%d", action) | ||||
| 	if next, ok := StreamFSM[r.State][action]; ok { | ||||
| 		if r.Publisher == nil || r.Publisher.OnStateChange(r.State, next) { | ||||
| 		if r.Publisher != nil { | ||||
| 			// 给Publisher状态变更的回调,方便进行远程拉流等操作 | ||||
| 			defer r.Publisher.OnStateChanged(r.State, next) | ||||
| 			r.Debugln(action, " :", r.State, "->", next) | ||||
| 			r.State = next | ||||
| 			switch next { | ||||
| 			case STATE_WAITPUBLISH: | ||||
| 				r.Publisher = nil | ||||
| 				Bus.Publish(Event_REQUEST_PUBLISH, r) | ||||
| 				r.timeout.Reset(r.WaitTimeout) | ||||
| 				if _, ok = PullOnSubscribeList[r.Path]; ok { | ||||
| 					PullOnSubscribeList[r.Path].Pull(r.Path) | ||||
| 				} | ||||
| 			case STATE_WAITTRACK: | ||||
| 				r.timeout.Reset(time.Second * 5) | ||||
| 			case STATE_PUBLISHING: | ||||
| 				r.WaitDone() | ||||
| 				r.timeout.Reset(r.PublishTimeout) | ||||
| 				Bus.Publish(Event_PUBLISH, r) | ||||
| 			case STATE_WAITCLOSE: | ||||
| 				r.timeout.Reset(r.WaitCloseTimeout) | ||||
| 			case STATE_CLOSED: | ||||
| 				r.cancel() | ||||
| 				if r.Publisher != nil { | ||||
| 					r.Publisher.Close() | ||||
| 				} | ||||
| 				r.WaitDone() | ||||
| 				Bus.Publish(Event_STREAMCLOSE, r) | ||||
| 				Streams.Delete(r.Path) | ||||
| 				r.timeout.Reset(time.Second) // 延迟1秒钟销毁,防止访问到已关闭的channel | ||||
| 			case STATE_DESTROYED: | ||||
| 				close(r.actionChan) | ||||
| 				fallthrough | ||||
| 			default: | ||||
| 				r.timeout.Stop() | ||||
| 			if !r.Publisher.OnStateChange(r.State, next) { | ||||
| 				return false | ||||
| 			} | ||||
| 		} | ||||
| 		r.Debug(action, " :", r.State, "->", next) | ||||
| 		r.State = next | ||||
| 		switch next { | ||||
| 		case STATE_WAITPUBLISH: | ||||
| 			r.Publisher = nil | ||||
| 			Bus.Publish(Event_REQUEST_PUBLISH, r) | ||||
| 			r.timeout.Reset(r.WaitTimeout) | ||||
| 			if _, ok = PullOnSubscribeList[r.Path]; ok { | ||||
| 				PullOnSubscribeList[r.Path].Pull(r.Path) | ||||
| 			} | ||||
| 		case STATE_WAITTRACK: | ||||
| 			r.timeout.Reset(time.Second * 5) | ||||
| 		case STATE_PUBLISHING: | ||||
| 			r.WaitDone() | ||||
| 			r.timeout.Reset(r.PublishTimeout) | ||||
| 			Bus.Publish(Event_PUBLISH, r) | ||||
| 		case STATE_WAITCLOSE: | ||||
| 			r.timeout.Reset(r.WaitCloseTimeout) | ||||
| 		case STATE_CLOSED: | ||||
| 			r.cancel() | ||||
| 			if r.Publisher != nil { | ||||
| 				r.Publisher.Close() | ||||
| 			} | ||||
| 			r.WaitDone() | ||||
| 			Bus.Publish(Event_STREAMCLOSE, r) | ||||
| 			Streams.Delete(r.Path) | ||||
| 			r.timeout.Reset(time.Second) // 延迟1秒钟销毁,防止访问到已关闭的channel | ||||
| 		case STATE_DESTROYED: | ||||
| 			close(r.actionChan) | ||||
| 			fallthrough | ||||
| 		default: | ||||
| 			r.timeout.Stop() | ||||
| 		} | ||||
| 		return true | ||||
| 	} | ||||
| 	return false | ||||
| @@ -197,7 +205,7 @@ func (r *Stream) IsClosed() bool { | ||||
| 	if r == nil { | ||||
| 		return true | ||||
| 	} | ||||
| 	return r.State == STATE_CLOSED | ||||
| 	return r.State >= STATE_CLOSED | ||||
| } | ||||
|  | ||||
| func (r *Stream) Close() { | ||||
| @@ -207,13 +215,13 @@ func (r *Stream) Close() { | ||||
| } | ||||
|  | ||||
| func (r *Stream) UnSubscribe(sub *Subscriber) { | ||||
| 	r.Debugln("unsubscribe", sub.ID) | ||||
| 	r.Debug("unsubscribe", sub.ID) | ||||
| 	if !r.IsClosed() { | ||||
| 		r.actionChan <- UnSubscibeAction(sub) | ||||
| 	} | ||||
| } | ||||
| func (r *Stream) Subscribe(sub *Subscriber) { | ||||
| 	r.Debugln("subscribe", sub.ID) | ||||
| 	r.Debug("subscribe", sub.ID) | ||||
| 	if !r.IsClosed() { | ||||
| 		sub.Stream = r | ||||
| 		sub.Context, sub.cancel = context.WithCancel(r) | ||||
| @@ -223,14 +231,15 @@ func (r *Stream) Subscribe(sub *Subscriber) { | ||||
|  | ||||
| // 流状态处理中枢,包括接收订阅发布指令等 | ||||
| func (r *Stream) run() { | ||||
| 	var done = r.Done() | ||||
| 	for { | ||||
| 		select { | ||||
| 		case <-r.timeout.C: | ||||
| 			r.Debugln(r.State, "timeout") | ||||
| 			r.Debugf("%v timeout", r.State) | ||||
| 			r.action(ACTION_TIMEOUT) | ||||
| 		case <-r.Done(): | ||||
| 		case <-done: | ||||
| 			r.action(ACTION_CLOSE) | ||||
|  | ||||
| 			done = nil | ||||
| 		case action, ok := <-r.actionChan: | ||||
| 			if ok { | ||||
| 				switch v := action.(type) { | ||||
| @@ -243,14 +252,14 @@ func (r *Stream) run() { | ||||
| 				case *Subscriber: | ||||
| 					r.Subscribers.Add(v) | ||||
| 					Bus.Publish(Event_SUBSCRIBE, v) | ||||
| 					v.Infoln(Sprintf(Yellow("added remains:%d"), Cyan(v.ID), Blue(len(r.Subscribers)))) | ||||
| 					v.Info(Sprintf(Yellow("added remains:%d") ,len(r.Subscribers))) | ||||
| 					if r.Subscribers.Len() == 1 { | ||||
| 						r.action(ACTION_FIRSTENTER) | ||||
| 					} | ||||
| 				case UnSubscibeAction: | ||||
| 					if r.Subscribers.Delete(v) { | ||||
| 						Bus.Publish(Event_UNSUBSCRIBE, v) | ||||
| 						(*Subscriber)(v).Infoln(Sprintf(Yellow("removed remains:%d"), Cyan(v.ID), Blue(len(r.Subscribers)))) | ||||
| 						(*Subscriber)(v).Info(Sprintf(Yellow("removed remains:%d"), len(r.Subscribers))) | ||||
| 						if r.Subscribers.Len() == 0 && r.WaitCloseTimeout > 0 { | ||||
| 							r.action(ACTION_LASTLEAVE) | ||||
| 						} | ||||
| @@ -266,7 +275,7 @@ func (r *Stream) run() { | ||||
| // Update 更新数据重置超时定时器 | ||||
| func (r *Stream) Update() uint32 { | ||||
| 	if r.State == STATE_PUBLISHING { | ||||
| 		r.Traceln("update") | ||||
| 		r.Trace("update") | ||||
| 		r.timeout.Reset(r.PublishTimeout) | ||||
| 	} | ||||
| 	return atomic.AddUint32(&r.FrameCount, 1) | ||||
| @@ -274,31 +283,29 @@ func (r *Stream) Update() uint32 { | ||||
|  | ||||
| // 如果暂时不知道编码格式可以用这个 | ||||
| func (r *Stream) NewVideoTrack() (vt *track.UnknowVideo) { | ||||
| 	r.Debugln("create unknow video track") | ||||
| 	vt = &track.UnknowVideo{ | ||||
| 		Stream: r, | ||||
| 	} | ||||
| 	r.Debug("create unknow video track") | ||||
| 	vt = &track.UnknowVideo{} | ||||
| 	vt.Stream = r | ||||
| 	return | ||||
| } | ||||
| func (r *Stream) NewAudioTrack() (at *track.UnknowAudio) { | ||||
| 	r.Debugln("create unknow audio track") | ||||
| 	at = &track.UnknowAudio{ | ||||
| 		Stream: r, | ||||
| 	} | ||||
| 	r.Debug("create unknow audio track") | ||||
| 	at = &track.UnknowAudio{} | ||||
| 	at.Stream = r | ||||
| 	return | ||||
| } | ||||
| func (r *Stream) NewH264Track() *track.H264 { | ||||
| 	r.Debugln("create h264 track") | ||||
| 	r.Debug("create h264 track") | ||||
| 	return track.NewH264(r) | ||||
| } | ||||
|  | ||||
| func (r *Stream) NewH265Track() *track.H265 { | ||||
| 	r.Debugln("create h265 track") | ||||
| 	r.Debug("create h265 track") | ||||
| 	return track.NewH265(r) | ||||
| } | ||||
|  | ||||
| func (r *Stream) NewAACTrack() *track.AAC { | ||||
| 	r.Debugln("create aac track") | ||||
| 	r.Debug("create aac track") | ||||
| 	return track.NewAAC(r) | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 dexter
					dexter