/** * [GetRTMPPublishURL 生成 RTMP 推流地址] * @param [type] domain [与直播空间绑定的 RTMP 推流域名,可以在 portal.qiniu.com 上绑定] * @param [type] hubName [直播空间名称] * @param string streamKey [流名,流不需要事先存在,推流会自动创建流] * @param *MAC mac [授权信息] * @param int64 expireAfterSeconds [生成的推流地址的有效时间] * @return string [RTMP推流地址] */ func (this *QiniuSdk) GetRTMPPublishURL(domain, hubName, streamKey string, mac *pili.MAC, expireAfterSeconds int64) string { return pili.RTMPPublishURL(domain, hubName, streamKey, mac, expireAfterSeconds) }
func v2() { mac := &pili2.MAC{ACCESS_KEY, []byte(SECRET_KEY)} client := pili2.New(mac, nil) hub := client.Hub(HUB_V2_NAME) router := gin.Default() router.Static("/assets", "./assets") router.LoadHTMLGlob("templates/*") // Stream list router.GET("/", func(c *gin.Context) { streams, _, err := hub.ListLive("", 1000, "") if err != nil { c.HTML(400, "error.tmpl", gin.H{"error": err}) c.Abort() return } c.HTML(200, "index2.tmpl", gin.H{ "streams": streams, }) }) // Player router.GET("/player", func(c *gin.Context) { streamId := c.Query("stream") liveRtmpUrl := pili2.RTMPPlayURL(PLAY_RTMP_DOMAIN, HUB_V2_NAME, streamId) liveHlsUrl := pili2.HLSPlayURL(PLAY_HLS_DOMAIN, HUB_V2_NAME, streamId) liveHdlUrl := pili2.HDLPlayURL(PLAY_HDL_DOMAIN, HUB_V2_NAME, streamId) c.HTML(200, "player2.tmpl", gin.H{ "stream": streamId, "liveRtmpUrl": liveRtmpUrl, "liveHlsUrl": liveHlsUrl, "liveHdlUrl": liveHdlUrl, }) }) // Publisher router.GET("/publisher", func(c *gin.Context) { id := fmt.Sprintf("%d-%d", time.Now().UnixNano(), rand.Int31n(256)) url := pili2.RTMPPublishURL(PUB_DOMAIN, HUB_V2_NAME, id, mac, 3600) baseUrl := fmt.Sprintf("rtmp://%s/%s/", PUB_DOMAIN, HUB_V2_NAME) stream := strings.TrimPrefix(url, baseUrl) c.HTML(200, "publisher2.tmpl", gin.H{ "pubRtmpUrlBase": baseUrl, "pubRtmpUrlStream": stream, }) }) // API router.POST("/api/stream", func(c *gin.Context) { id := fmt.Sprintf("%d-%d", time.Now().UnixNano(), rand.Int31n(256)) url := pili2.RTMPPublishURL(PUB_DOMAIN, HUB_V2_NAME, id, mac, 3600) c.String(200, url) }) // API // "/api/stream/z1.abclive.5633b990eb6f9213a2002473/status" router.GET("/api/stream/:id/status", func(c *gin.Context) { id := c.Params.ByName("id") stream := hub.Stream(id) status, err := stream.LiveStatus() if err != nil { c.JSON(400, err) c.Abort() return } c.JSON(200, status) }) // API router.POST("/api/stream/:id", func(c *gin.Context) { id := c.Params.ByName("id") url := pili2.RTMPPublishURL(PUB_DOMAIN, HUB_V2_NAME, id, mac, 3600) c.String(200, url) }) // API router.GET("/api/stream/:id/play", func(c *gin.Context) { id := c.Params.ByName("id") url := pili2.RTMPPlayURL(PLAY_RTMP_DOMAIN, HUB_V2_NAME, id) c.String(200, url) }) // Listen and server on 0.0.0.0:8070 router.Run(":8070") }