mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-03-05 13:40:51 +08:00
完善golang接口 (#4059)
This commit is contained in:
8
golang/.idea/.gitignore
generated
vendored
Normal file
8
golang/.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
10
golang/go.sum
Normal file
10
golang/go.sum
Normal file
@@ -0,0 +1,10 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
62
golang/tester/mk_common_test.go
Normal file
62
golang/tester/mk_common_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package tester
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
"time"
|
||||
"zlmediakit/zlmediakit"
|
||||
)
|
||||
|
||||
func TestCommonEnvInit(t *testing.T) {
|
||||
conf := zlmediakit.EnvInit(
|
||||
0, zlmediakit.LTrace, zlmediakit.LogConsole|zlmediakit.LogCallback|zlmediakit.LogFile,
|
||||
"log", 1, true, "../../conf/config.ini", false, "../../default.pem", "")
|
||||
|
||||
require.Equal(t, 0, conf.ThreadNum())
|
||||
require.Equal(t, zlmediakit.LTrace, conf.LogLevel())
|
||||
require.Equal(t, zlmediakit.LogConsole|zlmediakit.LogCallback|zlmediakit.LogFile, conf.LogMask())
|
||||
require.Equal(t, "log", conf.LogFilePath())
|
||||
require.Equal(t, 1, conf.LogFileDays())
|
||||
require.Equal(t, true, conf.IniIsPath())
|
||||
require.Equal(t, "../../conf/config.ini", conf.Ini())
|
||||
require.Equal(t, false, conf.SslIsPath())
|
||||
require.Equal(t, "../../default.pem", conf.Ssl())
|
||||
require.Equal(t, "", conf.SslPwd())
|
||||
}
|
||||
|
||||
func TestCommonSetLog(t *testing.T) {
|
||||
zlmediakit.SetLog(1, 1)
|
||||
}
|
||||
|
||||
func TestCommonServer(t *testing.T) {
|
||||
p, err := zlmediakit.HttpServerStart(1180, false)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, uint16(1180), p)
|
||||
|
||||
p, err = zlmediakit.RtspServerStart(11935, false)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, uint16(11935), p)
|
||||
|
||||
p, err = zlmediakit.RtmpServerStart(11554, false)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, uint16(11554), p)
|
||||
|
||||
p, err = zlmediakit.RtpServerStart(11111)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, uint16(11111), p)
|
||||
|
||||
p, err = zlmediakit.RtcServerStart(11222) // 未启用webrtc功能
|
||||
require.NotNil(t, err)
|
||||
//require.Equal(t, uint16(11222), p)
|
||||
|
||||
p, err = zlmediakit.SrtServerStart(11333)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, uint16(11333), p)
|
||||
|
||||
p, err = zlmediakit.ShellServerStart(11444)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, uint16(11444), p)
|
||||
|
||||
<-time.After(time.Minute * 5)
|
||||
zlmediakit.StopAllServer()
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package tester
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
"zlmediakit/zlmediakit"
|
||||
)
|
||||
|
||||
func TestServer(t *testing.T) {
|
||||
zlmediakit.MK_env_init2(0, zlmediakit.LTrace, zlmediakit.LOG_CONSOLE|zlmediakit.LOG_CALLBACK|zlmediakit.LOG_FILE,
|
||||
"log", 7, true, "../../conf/config.ini", true, "../../default.pem", "")
|
||||
zlmediakit.MK_http_server_start(80, false)
|
||||
zlmediakit.MK_http_server_start(443, true)
|
||||
time.Sleep(1000 * time.Second)
|
||||
}
|
||||
@@ -1,51 +1,255 @@
|
||||
package zlmediakit
|
||||
|
||||
/*
|
||||
#include "mk_mediakit.h"
|
||||
#cgo CFLAGS: -I../../api/include
|
||||
#cgo LDFLAGS: -L../../release/linux/Debug/ -lmk_api
|
||||
*/
|
||||
//#include "mk_mediakit.h"
|
||||
//#include "mk_common.h"
|
||||
import "C"
|
||||
import (
|
||||
"fmt"
|
||||
"zlmediakit/helper"
|
||||
)
|
||||
|
||||
type LOG_MASK int
|
||||
type LOG_LEVEL int
|
||||
type LogMask int
|
||||
type LogLevel int
|
||||
|
||||
const (
|
||||
LOG_CONSOLE LOG_MASK = 1 << 0
|
||||
LOG_FILE LOG_MASK = 1 << 1
|
||||
LOG_CALLBACK LOG_MASK = 2 << 1
|
||||
LogConsole LogMask = 1 << 0
|
||||
LogFile LogMask = 1 << 1
|
||||
LogCallback LogMask = 2 << 1
|
||||
)
|
||||
|
||||
const (
|
||||
LTrace LOG_LEVEL = 0
|
||||
LDebug LOG_LEVEL = 1
|
||||
LInfo LOG_LEVEL = 2
|
||||
LWarn LOG_LEVEL = 3
|
||||
LError LOG_LEVEL = 4
|
||||
LTrace LogLevel = 0
|
||||
LDebug LogLevel = 1
|
||||
LInfo LogLevel = 2
|
||||
LWarn LogLevel = 3
|
||||
LError LogLevel = 4
|
||||
)
|
||||
|
||||
func btoi(b bool) int {
|
||||
if b {
|
||||
return 1
|
||||
type Config struct {
|
||||
c *C.mk_config
|
||||
}
|
||||
|
||||
func newConfigFromC(c *C.mk_config) *Config {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
return 0
|
||||
return &Config{c: c}
|
||||
}
|
||||
|
||||
func MK_env_init2(thread_num int, log_level LOG_LEVEL, log_mask LOG_MASK, log_file_path string, log_file_days int, ini_is_path bool, ini string, ssl_is_path bool, ssl string, ssl_pwd string) {
|
||||
// 调用 C SDK 的函数
|
||||
C.mk_env_init2(C.int(thread_num), C.int(log_level), C.int(log_mask), C.CString(log_file_path), C.int(log_file_days), C.int(btoi(ini_is_path)), C.CString(ini), C.int(btoi(ssl_is_path)), C.CString(ssl), C.CString(ssl_pwd))
|
||||
func (conf *Config) ThreadNum() int {
|
||||
return int(conf.c.thread_num)
|
||||
}
|
||||
|
||||
func MK_stop_all_server() {
|
||||
// 调用 C SDK 的函数
|
||||
func (conf *Config) SetThreadNum(threadNum int) {
|
||||
conf.c.thread_num = C.int(threadNum)
|
||||
}
|
||||
|
||||
func (conf *Config) LogLevel() LogLevel {
|
||||
return LogLevel(conf.c.log_level)
|
||||
}
|
||||
|
||||
func (conf *Config) SetLogLevel(logLevel LogLevel) {
|
||||
conf.c.log_level = C.int(logLevel)
|
||||
}
|
||||
|
||||
func (conf *Config) LogMask() LogMask {
|
||||
return LogMask(conf.c.log_mask)
|
||||
}
|
||||
|
||||
func (conf *Config) SetLogMask(logMask LogMask) {
|
||||
conf.c.log_mask = C.int(logMask)
|
||||
}
|
||||
|
||||
func (conf *Config) LogFilePath() string {
|
||||
return C.GoString(conf.c.log_file_path)
|
||||
}
|
||||
|
||||
func (conf *Config) SetLogFilePath(logFilePath string) {
|
||||
logFilePathC := C.CString(logFilePath)
|
||||
conf.c.log_file_path = logFilePathC
|
||||
}
|
||||
|
||||
func (conf *Config) LogFileDays() int {
|
||||
return int(conf.c.log_file_days)
|
||||
}
|
||||
|
||||
func (conf *Config) SetLogFileDays(logFileDays int) {
|
||||
conf.c.log_file_days = C.int(logFileDays)
|
||||
}
|
||||
|
||||
func (conf *Config) IniIsPath() bool {
|
||||
return int(conf.c.ini_is_path) == 1
|
||||
}
|
||||
|
||||
func (conf *Config) SetIniIsPath(iniIsPath bool) {
|
||||
conf.c.ini_is_path = C.int(helper.Bool2Int(iniIsPath))
|
||||
}
|
||||
|
||||
func (conf *Config) Ini() string {
|
||||
return C.GoString(conf.c.ini)
|
||||
}
|
||||
|
||||
func (conf *Config) SetIni(ini string) {
|
||||
iniC := C.CString(ini)
|
||||
conf.c.ini = iniC
|
||||
}
|
||||
|
||||
func (conf *Config) Ssl() string {
|
||||
return C.GoString(conf.c.ssl)
|
||||
}
|
||||
|
||||
func (conf *Config) SetSsl(ssl string) {
|
||||
sslC := C.CString(ssl)
|
||||
conf.c.ssl = sslC
|
||||
}
|
||||
|
||||
func (conf *Config) SslIsPath() bool {
|
||||
return int(conf.c.ssl_is_path) == 1
|
||||
}
|
||||
|
||||
func (conf *Config) SetSslIsPath(sslIsPath bool) {
|
||||
conf.c.ssl_is_path = C.int(helper.Bool2Int(sslIsPath))
|
||||
}
|
||||
|
||||
func (conf *Config) SslPwd() string {
|
||||
return C.GoString(conf.c.ssl_pwd)
|
||||
}
|
||||
|
||||
func (conf *Config) SetSslPwd(sslPwd string) {
|
||||
sslPwdC := C.CString(sslPwd)
|
||||
conf.c.ssl_pwd = sslPwdC
|
||||
}
|
||||
|
||||
// EnvInit 初始化环境,调用该库前需要先调用此函数
|
||||
//
|
||||
// threadNum: 线程数
|
||||
// logLevel: 日志级别,支持0~4
|
||||
// logMask: 控制日志输出的掩模,请查看LOG_CONSOLE、LOG_FILE、LOG_CALLBACK等宏
|
||||
// logFilePath: 文件日志保存路径,路径可以不存在(内部可以创建文件夹),设置为NULL关闭日志输出至文件
|
||||
// logFileDays: 文件日志保存天数,设置为0关闭日志文件
|
||||
// iniIsPath: 配置文件是内容还是路径
|
||||
// ini: 配置文件内容或路径,可以为空,如果该文件不存在,那么将导出默认配置至该文件
|
||||
// sslIsPath: ssl证书是内容还是路径
|
||||
// ssl: ssl证书内容或路径,可以为空
|
||||
// sslPwd: 证书密码,可以为空
|
||||
func EnvInit(threadNum int, logLevel LogLevel, logMask LogMask, logFilePath string, logFileDays int, iniIsPath bool, ini string, sslIsPath bool, ssl string, sslPwd string) *Config {
|
||||
var c C.mk_config
|
||||
conf := newConfigFromC(&c)
|
||||
|
||||
conf.SetThreadNum(threadNum)
|
||||
conf.SetLogLevel(logLevel)
|
||||
conf.SetLogMask(logMask)
|
||||
conf.SetLogFilePath(logFilePath)
|
||||
conf.SetLogFileDays(logFileDays)
|
||||
conf.SetIniIsPath(iniIsPath)
|
||||
conf.SetIni(ini)
|
||||
conf.SetSsl(ssl)
|
||||
conf.SetSslIsPath(sslIsPath)
|
||||
conf.SetSslPwd(sslPwd)
|
||||
|
||||
C.mk_env_init(conf.c)
|
||||
return conf
|
||||
}
|
||||
|
||||
// StopAllServer 关闭所有服务器,请在main函数退出时调用
|
||||
func StopAllServer() {
|
||||
C.mk_stop_all_server()
|
||||
}
|
||||
|
||||
func MK_set_log(file_max_size, file_max_count int) {
|
||||
// 调用 C SDK 的函数
|
||||
C.mk_set_log(C.int(file_max_size), C.int(file_max_count))
|
||||
// SetLog 设置日志文件
|
||||
//
|
||||
// fileMaxSize 单个切片文件大小(MB)
|
||||
// fileMaxCount 切片文件个数
|
||||
func SetLog(fileMaxSize, fileMaxCount int) {
|
||||
C.mk_set_log(C.int(fileMaxSize), C.int(fileMaxCount))
|
||||
}
|
||||
|
||||
func MK_http_server_start(port uint16, ssl bool) {
|
||||
C.mk_http_server_start(C.ushort(port), C.int(btoi(ssl)))
|
||||
// HttpServerStart 创建http[s]服务器
|
||||
//
|
||||
// port htt监听端口,推荐80,传入0则随机分配
|
||||
// ssl 是否为ssl类型服务器
|
||||
func HttpServerStart(port uint16, ssl bool) (uint16, error) {
|
||||
ret := C.mk_http_server_start(C.ushort(port), C.int(helper.Bool2Int(ssl)))
|
||||
i := uint16(ret)
|
||||
if i == 0 {
|
||||
return 0, fmt.Errorf("http server start fail")
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
// RtspServerStart 创建rtsp[s]服务器
|
||||
//
|
||||
// port rtsp监听端口,推荐554,传入0则随机分配
|
||||
// ssl 是否为ssl类型服务器
|
||||
func RtspServerStart(port uint16, ssl bool) (uint16, error) {
|
||||
ret := C.mk_rtsp_server_start(C.ushort(port), C.int(helper.Bool2Int(ssl)))
|
||||
i := uint16(ret)
|
||||
if i == 0 {
|
||||
return 0, fmt.Errorf("rtsp server start fail")
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
// RtmpServerStart 创建rtmp[s]服务器
|
||||
//
|
||||
// port rtmp监听端口,推荐1935,传入0则随机分配
|
||||
// ssl 是否为ssl类型服务器
|
||||
func RtmpServerStart(port uint16, ssl bool) (uint16, error) {
|
||||
ret := C.mk_rtmp_server_start(C.ushort(port), C.int(helper.Bool2Int(ssl)))
|
||||
i := uint16(ret)
|
||||
if i == 0 {
|
||||
return 0, fmt.Errorf("rtmp server start fail")
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
// RtpServerStart 创建rtp服务器
|
||||
//
|
||||
// port rtp监听端口(包括udp/tcp)
|
||||
func RtpServerStart(port uint16) (uint16, error) {
|
||||
ret := C.mk_rtp_server_start(C.ushort(port))
|
||||
i := uint16(ret)
|
||||
if i == 0 {
|
||||
return 0, fmt.Errorf("rtp server start fail")
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
// RtcServerStart 创建rtc服务器
|
||||
//
|
||||
// port rtc监听端口
|
||||
func RtcServerStart(port uint16) (uint16, error) {
|
||||
ret := C.mk_rtc_server_start(C.ushort(port))
|
||||
i := uint16(ret)
|
||||
if i == 0 {
|
||||
return 0, fmt.Errorf("rtc server start fail")
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
// todo mk_webrtc_get_answer_sdp
|
||||
// todo mk_webrtc_get_answer_sdp2
|
||||
|
||||
// SrtServerStart 创建srt服务器
|
||||
//
|
||||
// port srt监听端口
|
||||
func SrtServerStart(port uint16) (uint16, error) {
|
||||
ret := C.mk_srt_server_start(C.ushort(port))
|
||||
i := uint16(ret)
|
||||
if i == 0 {
|
||||
return 0, fmt.Errorf("srt server start fail")
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
// ShellServerStart 创建shell服务器
|
||||
//
|
||||
// port shell监听端口
|
||||
func ShellServerStart(port uint16) (uint16, error) {
|
||||
ret := C.mk_shell_server_start(C.ushort(port))
|
||||
i := uint16(ret)
|
||||
if i == 0 {
|
||||
return 0, fmt.Errorf("shell server start fail")
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user