封装hls

This commit is contained in:
yangjiechina
2024-03-02 18:41:00 +08:00
parent 4947217197
commit f7552240d0
16 changed files with 505 additions and 192 deletions

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"net/http"
"time"
)
type HookFunc func(m map[string]interface{}, success func(response *http.Response), failure func(response *http.Response, err error)) error
@@ -24,8 +25,17 @@ type Hook interface {
DoRecvTimeout(m map[string]interface{}, success func(response *http.Response), failure func(response *http.Response, err error)) error
}
type hookImpl struct {
}
type HookEvent int
const (
HookEventPublish = HookEvent(0x1)
HookEventPublishDone = HookEvent(0x2)
HookEventPlay = HookEvent(0x3)
HookEventPlayDone = HookEvent(0x4)
HookEventRecord = HookEvent(0x5)
HookEventIdleTimeout = HookEvent(0x6)
HookEventRecvTimeout = HookEvent(0x6)
)
// 每个通知的时间都需要携带的字段
type eventInfo struct {
@@ -34,56 +44,64 @@ type eventInfo struct {
remoteAddr string //peer地址
}
func (hook *hookImpl) send(url string, m map[string]interface{}, success func(response *http.Response), failure func(response *http.Response, err error)) error {
marshal, err := json.Marshal(m)
func NewHookEventInfo(stream, protocol, remoteAddr string) eventInfo {
return eventInfo{stream: stream, protocol: protocol, remoteAddr: remoteAddr}
}
type HookSession interface {
send(url string, body interface{}, success func(response *http.Response), failure func(response *http.Response, err error)) error
Hook(event HookEvent, body interface{}, success func(response *http.Response), failure func(response *http.Response, err error)) error
}
var hookUrls map[HookEvent]string
func init() {
hookUrls = map[HookEvent]string{
HookEventPublish: "",
HookEventPublishDone: "",
HookEventPlay: "",
HookEventPlayDone: "",
HookEventRecord: "",
HookEventIdleTimeout: "",
HookEventRecvTimeout: "",
}
}
type hookSessionImpl struct {
}
func (h *hookSessionImpl) send(url string, body interface{}, success func(response *http.Response), failure func(response *http.Response, err error)) error {
marshal, err := json.Marshal(body)
if err != nil {
return err
}
client := &http.Client{}
client := &http.Client{
Timeout: time.Second * time.Duration(AppConfig.Hook.Time),
}
request, err := http.NewRequest("post", url, bytes.NewBuffer(marshal))
if err != nil {
return err
}
request.Header.Set("Content-Type", "application/json")
go func() {
response, err := client.Do(request)
if err != nil || response.StatusCode != http.StatusOK {
failure(response, err)
return
}
response, err := client.Do(request)
if err != nil || response.StatusCode != http.StatusOK {
failure(response, err)
} else {
success(response)
}()
}
return nil
}
func (hook *hookImpl) DoPublish(m map[string]interface{}, success func(response *http.Response), failure func(response *http.Response, err error)) error {
return hook.send(AppConfig.Hook.OnPublish, m, success, failure)
}
func (h *hookSessionImpl) Hook(event HookEvent, body interface{}, success func(response *http.Response), failure func(response *http.Response, err error)) error {
url := hookUrls[event]
if url == "" {
success(nil)
return nil
}
func (hook *hookImpl) DoPublishDone(m map[string]interface{}, success func(response *http.Response), failure func(response *http.Response, err error)) error {
return hook.send(AppConfig.Hook.OnPublishDone, m, success, failure)
}
func (hook *hookImpl) DoPlay(m map[string]interface{}, success func(response *http.Response), failure func(response *http.Response, err error)) error {
return hook.send(AppConfig.Hook.OnPlay, m, success, failure)
}
func (hook *hookImpl) DoPlayDone(m map[string]interface{}, success func(response *http.Response), failure func(response *http.Response, err error)) error {
return hook.send(AppConfig.Hook.OnPlayDone, m, success, failure)
}
func (hook *hookImpl) DoRecord(m map[string]interface{}, success func(response *http.Response), failure func(response *http.Response, err error)) error {
return hook.send(AppConfig.Hook.OnRecord, m, success, failure)
}
func (hook *hookImpl) DoIdleTimeout(m map[string]interface{}, success func(response *http.Response), failure func(response *http.Response, err error)) error {
return hook.send(AppConfig.Hook.OnIdleTimeout, m, success, failure)
}
func (hook *hookImpl) DoRecvTimeout(m map[string]interface{}, success func(response *http.Response), failure func(response *http.Response, err error)) error {
return hook.send(AppConfig.Hook.OnRecvTimeout, m, success, failure)
return h.send(url, body, success, failure)
}