Explorar el Código

add getvodplaylist api&sample

hangzws hace 7 años
padre
commit
bb55985ce5
Se han modificado 2 ficheros con 70 adiciones y 1 borrados
  1. 24 0
      oss/livechannel.go
  2. 46 1
      sample/livechannel.go

+ 24 - 0
oss/livechannel.go

@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"encoding/xml"
 	"fmt"
+	"io"
 	"net/http"
 	"strconv"
 	"time"
@@ -88,6 +89,29 @@ func (bucket Bucket) PostVodPlaylist(channelName, playlistName string, startTime
 	return checkRespCode(resp.StatusCode, []int{http.StatusOK})
 }
 
+// GetVodPlaylist  get the playlist based on the specified channelName, startTime and endTime
+//
+// channelName  the name of the channel
+// startTime    the start time of the playlist
+// endTime      the endtime of the playlist
+//
+// io.ReadCloser reader instance for reading data from response. It must be called close() after the usage and only valid when error is nil.
+// error        nil if success, otherwise error
+//
+func (bucket Bucket) GetVodPlaylist(channelName string, startTime, endTime time.Time) (io.ReadCloser, error) {
+	params := map[string]interface{}{}
+	params["vod"] = nil
+	params["startTime"] = strconv.FormatInt(startTime.Unix(), 10)
+	params["endTime"] = strconv.FormatInt(endTime.Unix(), 10)
+
+	resp, err := bucket.do("GET", channelName, params, nil, nil, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	return resp.Body, nil
+}
+
 //
 // GetLiveChannelStat   Get the state of the live-channel
 //

+ 46 - 1
sample/livechannel.go

@@ -2,6 +2,7 @@ package sample
 
 import (
 	"fmt"
+	"io/ioutil"
 	"time"
 
 	"github.com/aliyun/aliyun-oss-go-sdk/oss"
@@ -136,6 +137,50 @@ func PostVodPlayListSample() {
 	fmt.Println("PostVodPlayListSampleSample completed")
 }
 
+// GetVodPlayListSample Sample for generate playlist and return the content of the playlist
+func GetVodPlayListSample() {
+	channelName := "get-vod-playlist"
+	bucket, err := GetTestBucket(bucketName)
+	if err != nil {
+		HandleError(err)
+	}
+
+	config := oss.LiveChannelConfiguration{
+		Target: oss.LiveChannelTarget{
+			Type:         "HLS", //the type of object, only supports HLS, required
+			PlaylistName: "playlist.m3u8",
+		},
+	}
+
+	_, err = bucket.CreateLiveChannel(channelName, config)
+	if err != nil {
+		HandleError(err)
+	}
+
+	//This stage you can push live stream, and after that you could generator playlist
+
+	endTime := time.Now().Add(-1 * time.Minute)
+	startTime := endTime.Add(-60 * time.Minute)
+	body, err := bucket.GetVodPlaylist(channelName, startTime, endTime)
+	if err != nil {
+		HandleError(err)
+	}
+	defer body.Close()
+
+	data, err := ioutil.ReadAll(body)
+	if err != nil {
+		HandleError(err)
+	}
+	fmt.Printf("content of playlist is:%v\n", string(data))
+
+	err = DeleteTestBucketAndLiveChannel(bucketName)
+	if err != nil {
+		HandleError(err)
+	}
+
+	fmt.Println("PostVodPlayListSampleSample completed")
+}
+
 // GetLiveChannelStatSample Sample for get the state of live-channel
 func GetLiveChannelStatSample() {
 	channelName := "get-livechannel-stat"
@@ -396,5 +441,5 @@ func SignRtmpURLSample() {
 		HandleError(err)
 	}
 
-	fmt.Println("PutObjectSample completed")
+	fmt.Println("SignRtmpURLSample completed")
 }