package task import ( "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" vod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod/v20180717" "live/app/lib/config" live2 "live/app/logic/live" live3 "live/app/model/live" "log" "net/url" "strconv" ) type Live struct { } func (live *Live) Run() { credential := common.NewCredential( config.Config.Section("tencent").Key("secretId").String(), config.Config.Section("tencent").Key("secretKey").String(), ) cpf := profile.NewClientProfile() cpf.HttpProfile.Endpoint = "vod.tencentcloudapi.com" client, _ := vod.NewClient(credential, "", cpf) request := vod.NewPullEventsRequest() request1 := vod.NewConfirmEventsRequest() for { response, err := client.PullEvents(request) if sdkErr, ok := err.(*errors.TencentCloudSDKError); ok { if sdkErr.GetCode() == "ResourceNotFound" { continue } else { log.Println("回调错误,err:", err) continue } } handle := []string{} for _, event := range response.Response.EventSet { handle = append(handle, *event.EventHandle) switch *event.EventType { case "NewFileUpload": live.upSuc(event) } } request1.EventHandles = common.StringPtrs(handle) client.ConfirmEvents(request1) } } // @Title 视频上传完成 func (live *Live) upSuc(event *vod.EventContent) { if event.FileUploadEvent != nil { switch *event.FileUploadEvent.MediaBasicInfo.SourceInfo.SourceType { case "Record": live.record(event) } } } // @Title 录播完成 func (live *Live) record(event *vod.EventContent) { log.Println("视频录制完成") query, _ := url.Parse(*event.FileUploadEvent.MediaBasicInfo.SourceInfo.SourceContext) liveItemId, _ := strconv.ParseUint(query.Query().Get("liveItemId"), 10, 64) liveItemModel := &live3.LiveItem{} liveItemInfo := liveItemModel.FindById(uint(liveItemId)) if liveItemInfo.Id <= 0 { log.Println("直播场次获取失败,err:", liveItemId) return } liveItemModel.UpdateById(liveItemInfo.Id, map[string]interface{}{ "file_id": event.FileUploadEvent.FileId, "video_url": event.FileUploadEvent.MediaBasicInfo.MediaUrl, }) if liveItemInfo.Status == live3.LIVE_STATUS_ing { // 尚未结束直播 if err := live2.LiveLogic.Stop(liveItemInfo.UserId, liveItemInfo.Id); err != nil { log.Println("直播结束失败,err:", err) return } } }