mirror of
https://github.com/lkmio/lkm.git
synced 2025-09-27 03:26:01 +08:00
74 lines
2.0 KiB
Go
74 lines
2.0 KiB
Go
package stream
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/lkmio/avformat/utils"
|
|
"github.com/lkmio/lkm/log"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func PreparePublishSource(source Source, hook bool) (*http.Response, utils.HookState) {
|
|
var response *http.Response
|
|
|
|
if err := SourceManager.Add(source); err != nil {
|
|
return nil, utils.HookStateOccupy
|
|
}
|
|
|
|
if hook && AppConfig.Hooks.IsEnablePublishEvent() {
|
|
rep, state := HookPublishEvent(source)
|
|
if utils.HookStateOK != state {
|
|
_, _ = SourceManager.Remove(source.GetID())
|
|
return rep, state
|
|
}
|
|
|
|
response = rep
|
|
}
|
|
|
|
source.SetCreateTime(time.Now())
|
|
|
|
urls := GetStreamPlayUrls(source.GetID())
|
|
indent, _ := json.MarshalIndent(urls, "", "\t")
|
|
|
|
log.Sugar.Infof("%s准备推流 source:%s 拉流地址:\r\n%s", source.GetType().String(), source.GetID(), indent)
|
|
|
|
return response, utils.HookStateOK
|
|
}
|
|
|
|
func HookPublishEvent(source Source) (*http.Response, utils.HookState) {
|
|
var response *http.Response
|
|
|
|
if AppConfig.Hooks.IsEnablePublishEvent() {
|
|
hook, err := Hook(HookEventPublish, source.UrlValues().Encode(), NewHookPublishEventInfo(source))
|
|
if err != nil {
|
|
return hook, utils.HookStateFailure
|
|
}
|
|
|
|
response = hook
|
|
}
|
|
|
|
return response, utils.HookStateOK
|
|
}
|
|
|
|
func HookPublishDoneEvent(source Source) {
|
|
if AppConfig.Hooks.IsEnablePublishEvent() {
|
|
_, _ = Hook(HookEventPublishDone, source.UrlValues().Encode(), NewHookPublishEventInfo(source))
|
|
}
|
|
}
|
|
|
|
func HookReceiveTimeoutEvent(source Source) (*http.Response, error) {
|
|
utils.Assert(AppConfig.Hooks.IsEnableOnReceiveTimeout())
|
|
return Hook(HookEventReceiveTimeout, source.UrlValues().Encode(), NewHookPublishEventInfo(source))
|
|
}
|
|
|
|
func HookIdleTimeoutEvent(source Source) (*http.Response, error) {
|
|
utils.Assert(AppConfig.Hooks.IsEnableOnIdleTimeout())
|
|
return Hook(HookEventIdleTimeout, source.UrlValues().Encode(), NewHookPublishEventInfo(source))
|
|
}
|
|
|
|
func HookRecordEvent(source Source, path string) {
|
|
if AppConfig.Hooks.IsEnableOnRecord() {
|
|
_, _ = Hook(HookEventRecord, "", NewRecordEventInfo(source, path))
|
|
}
|
|
}
|