From db36f2823797c1f9ccc9bf9aa8e54eb53c76cdda Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Fri, 24 Jan 2025 22:01:16 +0800 Subject: [PATCH] =?UTF-8?q?resty=20=E8=B0=83=E7=94=A8=20zlm=20api=20?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/client/main.go | 2 + go.mod | 1 + go.sum | 2 + pkg/config/client.go | 12 +- pkg/config/loader.go | 4 +- pkg/services/zlmediakit/api.go | 24 +++ pkg/services/zlmediakit/api_test.go | 68 ++++++++ pkg/services/zlmediakit/config.go | 7 + pkg/services/zlmediakit/types/base.go | 6 + .../zlmediakit/types/server_config.go | 152 ++++++++++++++++++ pkg/services/zlmediakit/types/version.go | 7 + 11 files changed, 282 insertions(+), 3 deletions(-) create mode 100644 pkg/services/zlmediakit/api.go create mode 100644 pkg/services/zlmediakit/api_test.go create mode 100644 pkg/services/zlmediakit/config.go create mode 100644 pkg/services/zlmediakit/types/base.go create mode 100644 pkg/services/zlmediakit/types/server_config.go create mode 100644 pkg/services/zlmediakit/types/version.go diff --git a/cmd/client/main.go b/cmd/client/main.go index 9c20408..16c918d 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -4,6 +4,7 @@ import ( "git.skcks.cn/Shikong/go-gb28181/pkg/config" "git.skcks.cn/Shikong/go-gb28181/pkg/handler/message" "git.skcks.cn/Shikong/go-gb28181/pkg/log" + "git.skcks.cn/Shikong/go-gb28181/pkg/services/zlmediakit" "github.com/emiago/sipgo" "github.com/emiago/sipgo/sip" "github.com/rs/zerolog" @@ -41,6 +42,7 @@ func main() { } log.SetLogger(&logger) + zlmediakit.SetupZLMediaKitService(clientConfig.ZLMediaKit) ctx := context.Background() addr := fmt.Sprintf("%s:%d", clientConfig.ListenIp, clientConfig.ListenPort) diff --git a/go.mod b/go.mod index 7c73c5c..026015a 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( require ( github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/go-resty/resty/v2 v2.16.5 // indirect github.com/gobwas/httphead v0.1.0 // indirect github.com/gobwas/pool v0.2.1 // indirect github.com/gobwas/ws v1.4.0 // indirect diff --git a/go.sum b/go.sum index 7e2a796..45f41ca 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/go-resty/resty/v2 v2.16.5 h1:hBKqmWrr7uRc3euHVqmh1HTHcKn99Smr7o5spptdhTM= +github.com/go-resty/resty/v2 v2.16.5/go.mod h1:hkJtXbA2iKHzJheXYvQ8snQES5ZLGKMwQ07xAwp/fiA= github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU= github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM= github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og= diff --git a/pkg/config/client.go b/pkg/config/client.go index cee4f0f..107e44e 100644 --- a/pkg/config/client.go +++ b/pkg/config/client.go @@ -1,5 +1,7 @@ package config +import "git.skcks.cn/Shikong/go-gb28181/pkg/services/zlmediakit" + type ClientConfig struct { Debug bool `json:"debug" toml:"debug" yaml:"debug" comment:"调试模式"` @@ -11,6 +13,8 @@ type ClientConfig struct { DeviceId string `json:"deviceId" toml:"deviceId" yaml:"deviceId" comment:"设备Id"` ListenIp string `json:"listenIp" toml:"listenIp" yaml:"listenIp" comment:"监听Ip"` ListenPort int `json:"listenPort" toml:"listenPort" yaml:"listenPort" comment:"监听端口号"` + + ZLMediaKit *zlmediakit.Config `json:"zlmediakit" toml:"zlmediakit" yaml:"zlmediakit" comment:"ZLMediaKit配置"` } func DefaultClientConfig() *ClientConfig { @@ -24,6 +28,12 @@ func DefaultClientConfig() *ClientConfig { ListenIp: "0.0.0.0", DeviceId: "44050100002000000002", - ListenPort: 8081, + ListenPort: 5099, + + ZLMediaKit: &zlmediakit.Config{ + Id: "zlmediakit", + Url: "http://10.10.10.200:5081", + Secret: "zlmediakit", + }, } } diff --git a/pkg/config/loader.go b/pkg/config/loader.go index c73f00e..d72c1d6 100644 --- a/pkg/config/loader.go +++ b/pkg/config/loader.go @@ -2,10 +2,10 @@ package config import ( "errors" - "fmt" "git.skcks.cn/Shikong/go-gb28181/pkg/constants" "github.com/pelletier/go-toml/v2" "github.com/spf13/viper" + "log" "os" "path/filepath" ) @@ -45,7 +45,7 @@ func ReadClientConfig() (*ClientConfig, error) { var configFileNotFoundError viper.ConfigFileNotFoundError if errors.As(err, &configFileNotFoundError) { _ = GenerateConfig() - fmt.Println("未找到配置文件, 已生成示例配置文件于运行路径下") + log.Fatal("未找到配置文件, 已生成示例配置文件于运行路径下") } } diff --git a/pkg/services/zlmediakit/api.go b/pkg/services/zlmediakit/api.go new file mode 100644 index 0000000..e5423ef --- /dev/null +++ b/pkg/services/zlmediakit/api.go @@ -0,0 +1,24 @@ +package zlmediakit + +import ( + "github.com/go-resty/resty/v2" + "time" +) + +type ZLMediaKit struct { + client *resty.Client +} + +var zLMediaKitService *ZLMediaKit + +func SetupZLMediaKitService(config *Config) { + client := resty.New() + client.EnableTrace() + client.SetBaseURL(config.Url) + client.SetQueryParam("secret", config.Secret) + client.SetTimeout(10 * time.Second) + client.SetRetryCount(3) + zLMediaKitService = &ZLMediaKit{ + client: client, + } +} diff --git a/pkg/services/zlmediakit/api_test.go b/pkg/services/zlmediakit/api_test.go new file mode 100644 index 0000000..241cfc1 --- /dev/null +++ b/pkg/services/zlmediakit/api_test.go @@ -0,0 +1,68 @@ +package zlmediakit + +import ( + "encoding/json" + "fmt" + "git.skcks.cn/Shikong/go-gb28181/pkg/services/zlmediakit/types" + "github.com/go-resty/resty/v2" + "testing" +) + +func TestZLMediaKit_API(t *testing.T) { + SetupZLMediaKitService(&Config{ + Id: "amrWMKmbKqoBjRQ9", + Url: "http://10.10.10.200:5081", + Secret: "4155cca6-2f9f-11ee-85e6-8de4ce2e7333", + }) + + versionResp, err := zLMediaKitService.client.R().Get("/index/api/version") + printRequest(versionResp, err) + + version := &types.Data[types.VersionResp]{} + _ = json.Unmarshal(versionResp.Body(), &version) + fmt.Printf("Version: %+v\n\n", version) + + serverConfigResp, err := zLMediaKitService.client.R().Get("/index/api/getServerConfig") + printRequest(serverConfigResp, err) + serverConfig := &types.Data[types.ServerConfigResp]{} + _ = json.Unmarshal(serverConfigResp.Body(), &serverConfig) + fmt.Printf("ServerConfig: %+v\n\n", serverConfig) + + printRequest(zLMediaKitService.client.R().Get("/index/api/getMediaList")) +} + +func printRequest(resp *resty.Response, err error) { + fmt.Println("Request URL:", resp.Request.URL) + printResponse(resp, err) + printResponseTrace(resp) +} + +func printResponse(resp *resty.Response, err error) { + fmt.Println("Response Info:") + fmt.Println(" Error :", err) + fmt.Println(" Status Code:", resp.StatusCode()) + fmt.Println(" Status :", resp.Status()) + fmt.Println(" Proto :", resp.Proto()) + fmt.Println(" Time :", resp.Time()) + fmt.Println(" Received At:", resp.ReceivedAt()) + fmt.Println(" Body :\n", resp) + fmt.Println() +} + +func printResponseTrace(resp *resty.Response) { + fmt.Println("Request Trace Info:") + ti := resp.Request.TraceInfo() + fmt.Println(" DNSLookup :", ti.DNSLookup) + fmt.Println(" ConnTime :", ti.ConnTime) + fmt.Println(" TCPConnTime :", ti.TCPConnTime) + fmt.Println(" TLSHandshake :", ti.TLSHandshake) + fmt.Println(" ServerTime :", ti.ServerTime) + fmt.Println(" ResponseTime :", ti.ResponseTime) + fmt.Println(" TotalTime :", ti.TotalTime) + fmt.Println(" IsConnReused :", ti.IsConnReused) + fmt.Println(" IsConnWasIdle :", ti.IsConnWasIdle) + fmt.Println(" ConnIdleTime :", ti.ConnIdleTime) + fmt.Println(" RequestAttempt:", ti.RequestAttempt) + fmt.Println(" RemoteAddr :", ti.RemoteAddr.String()) + fmt.Println() +} diff --git a/pkg/services/zlmediakit/config.go b/pkg/services/zlmediakit/config.go new file mode 100644 index 0000000..cf08e7a --- /dev/null +++ b/pkg/services/zlmediakit/config.go @@ -0,0 +1,7 @@ +package zlmediakit + +type Config struct { + Id string `json:"id" yaml:"id" toml:"id" comment:"ZLMediaKit服务ID"` + Url string `json:"url" yaml:"url" toml:"url" comment:"ZLMediaKit服务地址"` //... + Secret string `json:"secret" yaml:"secret" toml:"secret" comment:"ZLMediaKit服务密钥"` +} diff --git a/pkg/services/zlmediakit/types/base.go b/pkg/services/zlmediakit/types/base.go new file mode 100644 index 0000000..b6b8b46 --- /dev/null +++ b/pkg/services/zlmediakit/types/base.go @@ -0,0 +1,6 @@ +package types + +type Data[T any] struct { + Code int `json:"code"` + Data T `json:"data"` +} diff --git a/pkg/services/zlmediakit/types/server_config.go b/pkg/services/zlmediakit/types/server_config.go new file mode 100644 index 0000000..ba668c1 --- /dev/null +++ b/pkg/services/zlmediakit/types/server_config.go @@ -0,0 +1,152 @@ +package types + +type ServerConfigResp = []ServerConfig + +type ServerConfig struct { + ApiApiDebug string `json:"api.apiDebug"` + ApiDefaultSnap string `json:"api.defaultSnap"` + ApiDownloadRoot string `json:"api.downloadRoot"` + ApiSecret string `json:"api.secret"` + ApiSnapRoot string `json:"api.snapRoot"` + ClusterOriginUrl string `json:"cluster.origin_url"` + ClusterRetryCount string `json:"cluster.retry_count"` + ClusterTimeoutSec string `json:"cluster.timeout_sec"` + FfmpegBin string `json:"ffmpeg.bin"` + FfmpegCmd string `json:"ffmpeg.cmd"` + FfmpegDownload string `json:"ffmpeg.download"` + FfmpegLog string `json:"ffmpeg.log"` + FfmpegRestartSec string `json:"ffmpeg.restart_sec"` + FfmpegSnap string `json:"ffmpeg.snap"` + GeneralCheckNvidiaDev string `json:"general.check_nvidia_dev"` + GeneralEnableVhost string `json:"general.enableVhost"` + GeneralEnableFfmpegLog string `json:"general.enable_ffmpeg_log"` + GeneralFlowThreshold string `json:"general.flowThreshold"` + GeneralMaxStreamWaitMS string `json:"general.maxStreamWaitMS"` + GeneralMediaServerId string `json:"general.mediaServerId"` + GeneralMergeWriteMS string `json:"general.mergeWriteMS"` + GeneralResetWhenRePlay string `json:"general.resetWhenRePlay"` + GeneralStreamNoneReaderDelayMS string `json:"general.streamNoneReaderDelayMS"` + GeneralUnreadyFrameCache string `json:"general.unready_frame_cache"` + GeneralWaitAddTrackMs string `json:"general.wait_add_track_ms"` + GeneralWaitTrackReadyMs string `json:"general.wait_track_ready_ms"` + HlsBroadcastRecordTs string `json:"hls.broadcastRecordTs"` + HlsDeleteDelaySec string `json:"hls.deleteDelaySec"` + HlsFastRegister string `json:"hls.fastRegister"` + HlsFileBufSize string `json:"hls.fileBufSize"` + HlsSegDelay string `json:"hls.segDelay"` + HlsSegDur string `json:"hls.segDur"` + HlsSegKeep string `json:"hls.segKeep"` + HlsSegNum string `json:"hls.segNum"` + HlsSegRetain string `json:"hls.segRetain"` + HookAliveInterval string `json:"hook.alive_interval"` + HookEnable string `json:"hook.enable"` + HookOnFlowReport string `json:"hook.on_flow_report"` + HookOnHttpAccess string `json:"hook.on_http_access"` + HookOnPlay string `json:"hook.on_play"` + HookOnPublish string `json:"hook.on_publish"` + HookOnRecordMp4 string `json:"hook.on_record_mp4"` + HookOnRecordTs string `json:"hook.on_record_ts"` + HookOnRtpServerTimeout string `json:"hook.on_rtp_server_timeout"` + HookOnRtspAuth string `json:"hook.on_rtsp_auth"` + HookOnRtspRealm string `json:"hook.on_rtsp_realm"` + HookOnSendRtpStopped string `json:"hook.on_send_rtp_stopped"` + HookOnServerExited string `json:"hook.on_server_exited"` + HookOnServerKeepalive string `json:"hook.on_server_keepalive"` + HookOnServerStarted string `json:"hook.on_server_started"` + HookOnShellLogin string `json:"hook.on_shell_login"` + HookOnStreamChanged string `json:"hook.on_stream_changed"` + HookOnStreamNoneReader string `json:"hook.on_stream_none_reader"` + HookOnStreamNotFound string `json:"hook.on_stream_not_found"` + HookRetry string `json:"hook.retry"` + HookRetryDelay string `json:"hook.retry_delay"` + HookStreamChangedSchemas string `json:"hook.stream_changed_schemas"` + HookTimeoutSec string `json:"hook.timeoutSec"` + HttpAllowCrossDomains string `json:"http.allow_cross_domains"` + HttpAllowIpRange string `json:"http.allow_ip_range"` + HttpCharSet string `json:"http.charSet"` + HttpDirMenu string `json:"http.dirMenu"` + HttpForbidCacheSuffix string `json:"http.forbidCacheSuffix"` + HttpForwardedIpHeader string `json:"http.forwarded_ip_header"` + HttpKeepAliveSecond string `json:"http.keepAliveSecond"` + HttpMaxReqSize string `json:"http.maxReqSize"` + HttpNotFound string `json:"http.notFound"` + HttpPort string `json:"http.port"` + HttpRootPath string `json:"http.rootPath"` + HttpSendBufSize string `json:"http.sendBufSize"` + HttpSslPort string `json:"http.sslport"` + HttpVirtualPath string `json:"http.virtualPath"` + MulticastAddrMax string `json:"multicast.addrMax"` + MulticastAddrMin string `json:"multicast.addrMin"` + MulticastUdpTTL string `json:"multicast.udpTTL"` + ProtocolAddMuteAudio string `json:"protocol.add_mute_audio"` + ProtocolAutoClose string `json:"protocol.auto_close"` + ProtocolContinuePushMs string `json:"protocol.continue_push_ms"` + ProtocolEnableAudio string `json:"protocol.enable_audio"` + ProtocolEnableFmp4 string `json:"protocol.enable_fmp4"` + ProtocolEnableHls string `json:"protocol.enable_hls"` + ProtocolEnableHlsFmp4 string `json:"protocol.enable_hls_fmp4"` + ProtocolEnableMp4 string `json:"protocol.enable_mp4"` + ProtocolEnableRtmp string `json:"protocol.enable_rtmp"` + ProtocolEnableRtsp string `json:"protocol.enable_rtsp"` + ProtocolEnableTs string `json:"protocol.enable_ts"` + ProtocolFmp4Demand string `json:"protocol.fmp4_demand"` + ProtocolHlsDemand string `json:"protocol.hls_demand"` + ProtocolHlsSavePath string `json:"protocol.hls_save_path"` + ProtocolModifyStamp string `json:"protocol.modify_stamp"` + ProtocolMp4AsPlayer string `json:"protocol.mp4_as_player"` + ProtocolMp4MaxSecond string `json:"protocol.mp4_max_second"` + ProtocolMp4SavePath string `json:"protocol.mp4_save_path"` + ProtocolPacedSenderMs string `json:"protocol.paced_sender_ms"` + ProtocolRtmpDemand string `json:"protocol.rtmp_demand"` + ProtocolRtspDemand string `json:"protocol.rtsp_demand"` + ProtocolTsDemand string `json:"protocol.ts_demand"` + RecordAppName string `json:"record.appName"` + RecordFastStart string `json:"record.fastStart"` + RecordFileBufSize string `json:"record.fileBufSize"` + RecordFileRepeat string `json:"record.fileRepeat"` + RecordSampleMS string `json:"record.sampleMS"` + RtcExternIP string `json:"rtc.externIP"` + RtcMaxBitrate string `json:"rtc.max_bitrate"` + RtcMinBitrate string `json:"rtc.min_bitrate"` + RtcPort string `json:"rtc.port"` + RtcPreferredCodecA string `json:"rtc.preferredCodecA"` + RtcPreferredCodecV string `json:"rtc.preferredCodecV"` + RtcRembBitRate string `json:"rtc.rembBitRate"` + RtcStartBitrate string `json:"rtc.start_bitrate"` + RtcTcpPort string `json:"rtc.tcpPort"` + RtcTimeoutSec string `json:"rtc.timeoutSec"` + RtmpDirectProxy string `json:"rtmp.directProxy"` + RtmpEnhanced string `json:"rtmp.enhanced"` + RtmpHandshakeSecond string `json:"rtmp.handshakeSecond"` + RtmpKeepAliveSecond string `json:"rtmp.keepAliveSecond"` + RtmpPort string `json:"rtmp.port"` + RtmpSslPort string `json:"rtmp.sslport"` + RtpAudioMtuSize string `json:"rtp.audioMtuSize"` + RtpH264StapA string `json:"rtp.h264_stap_a"` + RtpLowLatency string `json:"rtp.lowLatency"` + RtpRtpMaxSize string `json:"rtp.rtpMaxSize"` + RtpVideoMtuSize string `json:"rtp.videoMtuSize"` + RtpProxyDumpDir string `json:"rtp_proxy.dumpDir"` + RtpProxyGopCache string `json:"rtp_proxy.gop_cache"` + RtpProxyH264Pt string `json:"rtp_proxy.h264_pt"` + RtpProxyH265Pt string `json:"rtp_proxy.h265_pt"` + RtpProxyOpusPt string `json:"rtp_proxy.opus_pt"` + RtpProxyPort string `json:"rtp_proxy.port"` + RtpProxyPortRange string `json:"rtp_proxy.port_range"` + RtpProxyPsPt string `json:"rtp_proxy.ps_pt"` + RtpProxyTimeoutSec string `json:"rtp_proxy.timeoutSec"` + RtspAuthBasic string `json:"rtsp.authBasic"` + RtspDirectProxy string `json:"rtsp.directProxy"` + RtspHandshakeSecond string `json:"rtsp.handshakeSecond"` + RtspKeepAliveSecond string `json:"rtsp.keepAliveSecond"` + RtspLowLatency string `json:"rtsp.lowLatency"` + RtspPort string `json:"rtsp.port"` + RtspRtpTransportType string `json:"rtsp.rtpTransportType"` + RtspSslPort string `json:"rtsp.sslport"` + ShellMaxReqSize string `json:"shell.maxReqSize"` + ShellPort string `json:"shell.port"` + SrtLatencyMul string `json:"srt.latencyMul"` + SrtPktBufSize string `json:"srt.pktBufSize"` + SrtPort string `json:"srt.port"` + SrtTimeoutSec string `json:"srt.timeoutSec"` +} diff --git a/pkg/services/zlmediakit/types/version.go b/pkg/services/zlmediakit/types/version.go new file mode 100644 index 0000000..7ec0776 --- /dev/null +++ b/pkg/services/zlmediakit/types/version.go @@ -0,0 +1,7 @@ +package types + +type VersionResp struct { + BranchName string `json:"branchName"` + BuildTime string `json:"buildTime"` + CommitHash string `json:"commitHash"` +}