Skip to content

Commit 8e69739

Browse files
author
wanjiewu
committed
支持边转边播
1 parent 9762204 commit 8e69739

File tree

5 files changed

+495
-40
lines changed

5 files changed

+495
-40
lines changed

ci_media.go

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import (
1616
"github.com/mitchellh/mapstructure"
1717
)
1818

19+
type VodInfo struct {
20+
FileId string `xml:"FileId,omitempty"`
21+
SubAppId string `xml:"SubAppId,omitempty"`
22+
}
23+
1924
// JobInput TODO
2025
type JobInput struct {
2126
Object string `xml:"Object,omitempty"`
@@ -26,7 +31,8 @@ type JobInput struct {
2631
Key string `xml:"Key"`
2732
Value string `xml:"Value"`
2833
} `xml:"CosHeaders"`
29-
Url string `xml:"Url,omitempty"`
34+
Url string `xml:"Url,omitempty"`
35+
Vod *VodInfo `xml:"Vod,omitempty"`
3036
}
3137

3238
// StreamExtract TODO
@@ -1475,6 +1481,10 @@ func (s *CIService) CreatePlayKey(ctx context.Context) (*PlayKeyResult, *Respons
14751481
return &res, resp, err
14761482
}
14771483

1484+
func (s *CIService) CreateMediaPlayKey(ctx context.Context) (*PlayKeyResult, *Response, error) {
1485+
return s.CreatePlayKey(ctx)
1486+
}
1487+
14781488
func (s *CIService) GetPlayKey(ctx context.Context) (*PlayKeyResult, *Response, error) {
14791489
var res PlayKeyResult
14801490
sendOpt := sendOptions{
@@ -1487,6 +1497,36 @@ func (s *CIService) GetPlayKey(ctx context.Context) (*PlayKeyResult, *Response,
14871497
return &res, resp, err
14881498
}
14891499

1500+
func (s *CIService) DescribeMediaPlayKey(ctx context.Context) (*PlayKeyResult, *Response, error) {
1501+
var res PlayKeyResult
1502+
sendOpt := sendOptions{
1503+
baseURL: s.client.BaseURL.CIURL,
1504+
uri: "/playKey",
1505+
method: http.MethodGet,
1506+
result: &res,
1507+
}
1508+
resp, err := s.client.send(ctx, &sendOpt)
1509+
return &res, resp, err
1510+
}
1511+
1512+
type UpdateMediaPlayKeyOptions struct {
1513+
MasterPlayKey string `url:"masterPlayKey,omitempty"`
1514+
BackupPlayKey string `url:"backupPlayKey,omitempty"`
1515+
}
1516+
1517+
func (s *CIService) UpdateMediaPlayKey(ctx context.Context, opt *UpdateMediaPlayKeyOptions) (*PlayKeyResult, *Response, error) {
1518+
var res PlayKeyResult
1519+
sendOpt := sendOptions{
1520+
baseURL: s.client.BaseURL.CIURL,
1521+
uri: "/playKey",
1522+
method: http.MethodPut,
1523+
optQuery: opt,
1524+
result: &res,
1525+
}
1526+
resp, err := s.client.send(ctx, &sendOpt)
1527+
return &res, resp, err
1528+
}
1529+
14901530
// GenerateMediaInfoOptions TODO
14911531
type GenerateMediaInfoOptions struct {
14921532
XMLName xml.Name `xml:"Request"`
@@ -3998,3 +4038,63 @@ type ImageOCRTextWordPolygon struct {
39984038
Y int `xml:"Y,omitempty"`
39994039
} `xml:"RightBottom,omitempty"`
40004040
}
4041+
4042+
type LiveTanscodeVideo struct {
4043+
Codec string `xml:"Codec"`
4044+
Width string `xml:"Width,omitempty"`
4045+
Height string `xml:"Height,omitempty"`
4046+
Fps string `xml:"Fps,omitempty"`
4047+
Bitrate string `xml:"Bitrate,omitempty"`
4048+
Gop string `xml:"Gop,omitempty"`
4049+
Maxrate string `xml:"Maxrate,omitempty"`
4050+
}
4051+
4052+
type LiveTanscodeTransConfig struct {
4053+
InitialClipNum string `xml:"InitialClipNum,omitempty"`
4054+
CosTag string `xml:"CosTag,omitempty"`
4055+
HlsEncrypt *HlsEncrypt `xml:"HlsEncrypt,omitempty"`
4056+
}
4057+
4058+
type LiveTanscode struct {
4059+
Container *Container `xml:"Container,omitempty"`
4060+
Video *LiveTanscodeVideo `xml:"Video,omitempty"`
4061+
// TimeInterval *TimeInterval `xml:"TimeInterval,omitempty"`
4062+
// Audio *Audio `xml:"Audio,omitempty"`
4063+
TransConfig *LiveTanscodeTransConfig `xml:"TransConfig,omitempty"`
4064+
}
4065+
4066+
type GeneratePlayListJobOperation struct {
4067+
Tag string `xml:"Tag,omitempty"`
4068+
Output *JobOutput `xml:"Output,omitempty"`
4069+
MediaResult *MediaResult `xml:"MediaResult,omitempty"`
4070+
MediaInfo *MediaInfo `xml:"MediaInfo,omitempty"`
4071+
Transcode *LiveTanscode `xml:"Transcode,omitempty"`
4072+
UserData string `xml:"UserData,omitempty"`
4073+
JobLevel int `xml:"JobLevel,omitempty"`
4074+
}
4075+
4076+
type CreateGeneratePlayListJobOptions struct {
4077+
XMLName xml.Name `xml:"Request"`
4078+
Tag string `xml:"Tag,omitempty"`
4079+
Input *JobInput `xml:"Input,omitempty"`
4080+
Operation *GeneratePlayListJobOperation `xml:"Operation,omitempty"`
4081+
QueueId string `xml:"QueueId,omitempty"`
4082+
QueueType string `xml:"QueueType,omitempty"`
4083+
CallBackFormat string `xml:"CallBackFormat,omitempty"`
4084+
CallBackType string `xml:"CallBackType,omitempty"`
4085+
CallBack string `xml:"CallBack,omitempty"`
4086+
CallBackMqConfig *NotifyConfigCallBackMqConfig `xml:"CallBackMqConfig,omitempty"`
4087+
}
4088+
4089+
func (s *CIService) CreateGeneratePlayListJob(ctx context.Context, opt *CreateGeneratePlayListJobOptions) (*CreateJobsResult, *Response, error) {
4090+
var res CreateJobsResult
4091+
sendOpt := sendOptions{
4092+
baseURL: s.client.BaseURL.CIURL,
4093+
uri: "/jobs",
4094+
method: http.MethodPost,
4095+
body: opt,
4096+
result: &res,
4097+
}
4098+
resp, err := s.client.send(ctx, &sendOpt)
4099+
return &res, resp, err
4100+
}

example/CI/media_process/generate_token.go

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ import (
55
"fmt"
66
"net/http"
77
"net/url"
8+
"os"
89
"time"
910

1011
"github.com/dgrijalva/jwt-go"
1112
"github.com/tencentyun/cos-go-sdk-v5"
1213
)
1314

15+
type URLToken struct {
16+
SessionToken string `url:"x-cos-security-token,omitempty" header:"-"`
17+
}
18+
1419
type JwtTokens struct {
1520
// base info
1621
Type string `json:"Type"`
@@ -31,26 +36,23 @@ type JwtTokens struct {
3136
ProtectContentKey int `json:"ProtectContentKey"`
3237
}
3338

34-
// 定义secret
35-
var Secret = []byte("xxx")
36-
3739
func (token JwtTokens) Valid() error {
3840
return nil
3941
}
4042

4143
// 生成jwt
42-
func GenerateToken() (string, error) {
44+
func GenerateToken(appId string, bucketId string, objectKey string, secret []byte) (string, error) {
4345
t := time.Now()
4446
now := t.Unix()
4547
payLoad := JwtTokens{
4648
// 固定为 CosCiToken, 必填参数
4749
Type: "CosCiToken",
4850
// app id,必填参数
49-
AppId: "1234567890",
51+
AppId: appId,
5052
// 播放文件所在的BucketId, 必填参数
51-
BucketId: "test-1234567890",
53+
BucketId: bucketId,
5254
// 播放文件名
53-
Object: "hls_test/no_uri_key.m3u8",
55+
Object: url.QueryEscape(objectKey),
5456
// 固定为client,必填参数
5557
Issuer: "client",
5658
// token颁发时间戳,必填参数
@@ -70,48 +72,79 @@ func GenerateToken() (string, error) {
7072
token := jwt.NewWithClaims(jwt.SigningMethodHS256, payLoad)
7173

7274
//使用指定的secret签名并获得完成的编码后的字符串token
73-
return token.SignedString(Secret)
75+
return token.SignedString(secret)
7476
}
7577

76-
type URLToken struct {
77-
SessionToken string `url:"x-cos-security-token,omitempty" header:"-"`
78-
}
78+
// 验证环境url
79+
func GetCIDomainURL(tak string, tsk string, token *URLToken, appId string, bucketId string, region string, objectKey string, playkey []byte) {
80+
// 固定为getplaylist
81+
name := "getplaylist"
7982

80-
func GetURL() {
81-
// 替换成您的密钥
82-
tak := "xxx"
83-
tsk := "xxx"
84-
token := &URLToken{
85-
SessionToken: "",
86-
}
83+
u, _ := url.Parse("https://" + bucketId + ".ci." + region + ".myqcloud.com")
84+
b := &cos.BaseURL{BucketURL: u}
85+
c := cos.NewClient(b, &http.Client{
86+
Transport: &cos.AuthorizationTransport{
87+
SecretID: tak,
88+
SecretKey: tsk,
89+
SessionToken: token.SessionToken,
90+
},
91+
})
92+
ctx := context.Background()
8793

88-
// 替换成您的桶名称
89-
bucketName := "test-1234567890"
90-
// 替换成您桶所在的region
91-
region := "ap-chongqing"
92-
// 替换成您需要播放的视频名称
93-
objectName := "hls_test/no_uri_key.m3u8"
94-
// 固定为pm3u8
95-
name := "pm3u8"
94+
// 获取预签名
95+
presignedURL, err := c.Object.GetPresignedURL(ctx, http.MethodGet, name, tak, tsk, time.Hour, token)
96+
if err != nil {
97+
fmt.Printf("Error: %v\n", err)
98+
return
99+
}
100+
// 生成token
101+
generateToken, _ := GenerateToken(appId, bucketId, objectKey, playkey)
102+
resultUrl := presignedURL.String() + "&tokenType=JwtToken&expires=3600&object=" + url.QueryEscape(objectKey) + "&token=" + generateToken
103+
fmt.Println(resultUrl)
104+
}
96105

97-
u, _ := url.Parse("http://" + bucketName + ".ci." + region + ".myqcloud.com")
106+
// cos环境url
107+
func GetCOSDomainURL(tak string, tsk string, token *URLToken, appId string, bucketId string, region string, objectKey string, playkey []byte) {
108+
u, _ := url.Parse("https://" + bucketId + ".cos." + region + ".myqcloud.com")
98109
b := &cos.BaseURL{BucketURL: u}
99-
c := cos.NewClient(b, &http.Client{})
110+
c := cos.NewClient(b, &http.Client{
111+
Transport: &cos.AuthorizationTransport{
112+
SecretID: tak,
113+
SecretKey: tsk,
114+
SessionToken: token.SessionToken,
115+
},
116+
})
100117
ctx := context.Background()
101118

102119
// 获取预签名
103-
presignedURL, err := c.Object.GetPresignedURL(ctx, http.MethodGet, name, tak, tsk, time.Hour, token)
120+
presignedURL, err := c.Object.GetPresignedURL3(ctx, http.MethodGet, objectKey, time.Hour, token)
104121
if err != nil {
105122
fmt.Printf("Error: %v\n", err)
106123
return
107124
}
108-
fmt.Println(presignedURL.String())
109125
// 生成token
110-
generateToken, _ := GenerateToken()
111-
resultUrl := presignedURL.String() + "&tokenType=JwtToken&expires=3600&object=" + objectName + "&token=" + generateToken
126+
generateToken, _ := GenerateToken(appId, bucketId, objectKey, playkey)
127+
resultUrl := presignedURL.String() + "&ci-process=pm3u8&expires=43200&&tokenType=JwtToken&token=" + generateToken
112128
fmt.Println(resultUrl)
113129
}
114130

115131
func main() {
116-
GetURL()
132+
// 替换成您的密钥
133+
tak := os.Getenv("COS_SECRETID")
134+
tsk := os.Getenv("COS_SECRETKEY")
135+
token := &URLToken{
136+
SessionToken: "",
137+
}
138+
// 替换成您的桶名称
139+
appId := "1250000000"
140+
// 替换成您的桶名称
141+
bucketId := "test-1250000000"
142+
// 替换成您桶所在的region
143+
region := "ap-chongqing"
144+
// 替换成您需要播放的视频名称
145+
objectKey := "live/a.m3u8"
146+
// 定义secret
147+
var secret = []byte("aaaaaaaaaaa")
148+
GetCIDomainURL(tak, tsk, token, appId, bucketId, region, objectKey, secret)
149+
GetCOSDomainURL(tak, tsk, token, appId, bucketId, region, objectKey, secret)
117150
}

0 commit comments

Comments
 (0)