Esempio n. 1
0
/**
 * [NewClient 初始化授权客户]
 * @param  *pili.MAC         mac [授权信息]
 * @param  http.RoundTripper tr  [http会话]
 * @return *pili.Client                   [授权客户]
 */
func (this *QiniuSdk) NewClient(mac *pili.MAC, tr http.RoundTripper) *pili.Client {
	return pili.New(mac, tr)
}
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")
}