Huawei Cloud SparkRTC - Cloud Recording Example (Go Version)

1. Introduction

Introduction to Huawei Cloud SparkRTC Cloud Recording

SparkRTC, built on Huawei's long-established video technologies and expertise, delivers a high-concurrency and high-definition (HD) experience in real time. It offers dependable security at low latency and is an ideal option for a broad array of real-time communication (RTC) scenarios, such as online education, cloud conferencing, and social entertainment.

img

The SparkRTC service supports real-time communication content recording and content storage in OBS buckets. You can download and share the recorded files in the OBS service.

img

If you want to record the video and audio content during the communication, you need to configure the recording rule and callback. For more details, see Cloud Recording and Playback.

Prerequisites of configuring the recording rules:

  1. Create an OBS bucket: Create an OBS bucket for storing recordings. If you already have one, go to 2.
  2. Authorize SparkRTC to store recordings in the bucket: Allow SparkRTC to save the recordings in the OBS bucket.

This example shows how users can configure recording rules and callbacks, perform recording, and view recording files through Huawei Cloud Go Software Development Kit (Go SDK).

This document also illustrates other recording management APIs, including deleting, listing, querying, and updating recording rules. Users can customize the recording rules through these APIs.

2. Prerequisites

  • An application has been created.
  • An OBS bucket has been created and SparkRTC has been authorized to access the OBS bucket.

3. Installing SDK

This example is developed based on Huawei Cloud Go SDK V3.0.

go get -u github.com/huaweicloud/huaweicloud-sdk-go-v3

4. Getting Started

Step 1: Creating a Recording Rule

Description

Creates or updates a recording rule.

  • If the current application does not have a recording rule in the requested location, a new recording rule will be created.
  • If the current application has a recording rule in the requested location, the recording rule will be replaced with the new one.
Sample Code
package main

import (
	"fmt"

	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
	region "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/region"
	cloudrtc "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cloudrtc/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cloudrtc/v2/model"
)

func main() {
	ak := "<YOUR AK>"
	sk := "<YOUR SK>"
	endpoint := "<CloudRTC URL>"
	projectID := "<YOUR projectID>"
	
	auth := basic.NewCredentialsBuilder().
		WithProjectId(projectID).
		WithAk(ak).
		WithSk(sk).
		Build()

	client := cloudrtc.NewCloudRTCClient(
		cloudrtc.CloudRTCClientBuilder().
			WithRegion(region.NewRegion("<YOUR REGION>", endpoint)).
			WithCredential(auth).
			Build())

	object := "/record"
	var ecordMaxDurationToMergeFile int32 = 120
	var recordPrefix string = "{publish_domain}/{record_format}/{stream}/{file_start_time}"
	var recordSliceDuration int32 = 60

	request := &model.CreateRecordRuleRequest{
		AppId: "2312",
		Body: &model.RecordRuleReq{
			ObsAddr: &model.RecordObsFileAddr{
				Location:  model.GetRecordObsFileAddrLocationEnum().CN_NORTH_4,
				ProjectId: "123123",
				Bucket:    "123123",
				Object:    &object,
			},
			RecordFormats: []model.RecordRuleReqRecordFormats{
				model.GetRecordRuleReqRecordFormatsEnum().HLS,
				model.GetRecordRuleReqRecordFormatsEnum().MP4,
			},
			HlsConfig: &model.HlsRecordConfig{
				RecordCycle:                  300,
				RecordPrefix:                 &recordPrefix,
				RecordSliceDuration:          &recordSliceDuration,
				RecordMaxDurationToMergeFile: &ecordMaxDurationToMergeFile,
			},
			Mp4Config: &model.Mp4RecordConfig{
				RecordCycle:                  300,
				RecordPrefix:                 &recordPrefix,
				RecordMaxDurationToMergeFile: &ecordMaxDurationToMergeFile,
			},
		},
	}
	response, err := client.CreateRecordRule(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
}

Step 2: Configuring a Recording Event Callback

Description

Configures a recording event callback.

When a subscribed event occurs in a task, the callback address configured through this API is used to receive a notification.

For details about the callback content, see (Value-added) Receiving a Recording Event Callback Notification.

Sample Code
package main

import (
	"fmt"
	
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/region"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
	cloudrtc "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cloudrtc/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cloudrtc/v2/model"
)

func main() {
	ak := "<YOUR AK>"
	sk := "<YOUR SK>"
	endpoint := "<CloudRTC URL>"
	projectID := "<YOUR projectID>"
	regionInfo := "<YOUR REGION>"
	auth := basic.NewCredentialsBuilder().
		WithProjectId(projectID).
		WithAk(ak).
		WithSk(sk).
		Build()
	
	client := cloudrtc.NewCloudRTCClient(
		cloudrtc.CloudRTCClientBuilder().
			WithRegion(region.NewRegion(regionInfo, endpoint)).
			WithCredential(auth).
			Build())
	authKey := "<AuthKey>"
	request := &model.UpdateRecordCallbackRequest{
		AppId: "<APPID>",
		Body: &model.AppCallbackUrlReq{
			Url:     "<Your URL>",
			AuthKey: &authKey,
		},
	}
	response, err := client.UpdateRecordCallback(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
}

Step 3: Starting a Single Stream Recording Job

Description

Starts a single stream recording job.

Sample Code
package main

import (
	"fmt"
	
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/region"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
	cloudrtc "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cloudrtc/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cloudrtc/v2/model"
)

func main() {
	ak := "<YOUR AK>"
	sk := "<YOUR SK>"
	endpoint := "<CloudRTC URL>"
	projectID := "<YOUR projectID>"
	regionInfo := "<YOUR REGION>"
	auth := basic.NewCredentialsBuilder().
		WithProjectId(projectID).
		WithAk(ak).
		WithSk(sk).
		Build()
	
	client := cloudrtc.NewCloudRTCClient(
		cloudrtc.CloudRTCClientBuilder().
			WithRegion(region.NewRegion(regionInfo, endpoint)).
			WithCredential(auth).
			Build())
	isRecordAudio := true
	var MaxTime int32 = 60
	// CAMERASTREAM or SCREENSTREAM
	videoType := model.GetIndividualStreamJobReqVideoTypeEnum().CAMERASTREAM
	// LD, SD, HD, or FHD
	streamType := model.GetIndividualStreamJobReqSelectStreamTypeEnum().HD
	request := &model.CreateIndividualStreamJobRequest{
		AppId: "<APPID>",
		Body: &model.IndividualStreamJobReq{
			RoomId:           "<RoomID>",
			UserId:           "<UserID>",
			IsRecordAudio:    &isRecordAudio,
			VideoType:        &videoType,
			SelectStreamType: &streamType,
			MaxIdleTime:      &MaxTime,
			RecordParam: &model.RecordParam{
				RecordRuleId: "<RecordRuleId>",
			},
		},
	}
	response, err := client.CreateIndividualStreamJob(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
}

Step 4: Viewing Recordings

When the recording is complete, view recordings on the OBS console or through callback messages.

  • Viewing recordings on the OBS console

    1. In the navigation pane of the OBS console, choose Object Storage.

    2. In the bucket list, click the bucket that stores SparkRTC recordings.

      The Objects page is displayed.

    3. In the navigation pane, choose Objects to view the recording information.

    4. Download and share the recordings as required. For details, see OBS Documentation.

  • Viewing recordings through the recording event callback

    ​ If you set the recording callback URL, you can receive the callback message of the recording task in the configured callback URL every time a recording is complete.

Recording Rule Management: Listing Recording Rules

Description

Lists all recording rules.

Sample Code
package main

import (
	"fmt"

	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
	region "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/region"
	cloudrtc "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cloudrtc/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cloudrtc/v2/model"
)

func main() {
	ak := "<YOUR AK>"
	sk := "<YOUR SK>"
	endpoint := "<CloudRTC URL>"
	projectID := "<YOUR projectID>"

	auth := basic.NewCredentialsBuilder().
		WithProjectId(projectID).
		WithAk(ak).
		WithSk(sk).
		Build()

	client := cloudrtc.NewCloudRTCClient(
		cloudrtc.CloudRTCClientBuilder().
			WithRegion(region.NewRegion("<YOUR REGION>", endpoint)).
			WithCredential(auth).
			Build())

	request := &model.ListRecordRulesRequest{
		AppId: "<APPID>",
	}
	response, err := client.ListRecordRules(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
}

Recording Rule Management: Updating a Recording Rule

Description

Updates a recording rule by rule_id.

Sample Code
package main

import (
	"fmt"

	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
	region "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/region"
	cloudrtc "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cloudrtc/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cloudrtc/v2/model"
)

func main() {
	ak := "<YOUR AK>"
	sk := "<YOUR SK>"
	endpoint := "<CloudRTC URL>"
	projectID := "<YOUR projectID>"

	auth := basic.NewCredentialsBuilder().
		WithProjectId(projectID).
		WithAk(ak).
		WithSk(sk).
		Build()

	client := cloudrtc.NewCloudRTCClient(
		cloudrtc.CloudRTCClientBuilder().
			WithRegion(region.NewRegion("<YOUR REGION>", endpoint)).
			WithCredential(auth).
			Build())

	object := "/record"
	var ecordMaxDurationToMergeFile int32 = 120
	var recordPrefix string = "{publish_domain}/{record_format}/{stream}/{file_start_time}"
	var recordSliceDuration int32 = 60
	request := &model.UpdateRecordRuleRequest{
		AppId:  "<APPID>",
		RuleId: "<RuleID>",
		Body: &model.RecordRuleReq{
			ObsAddr: &model.RecordObsFileAddr{
				Location:  model.GetRecordObsFileAddrLocationEnum().CN_NORTH_4,
				ProjectId: "1234",
				Bucket:    "1234",
				Object:    &object,
			},
			RecordFormats: []model.RecordRuleReqRecordFormats{
				model.GetRecordRuleReqRecordFormatsEnum().HLS,
				model.GetRecordRuleReqRecordFormatsEnum().MP4,
			},
			HlsConfig: &model.HlsRecordConfig{
				RecordCycle:                  300,
				RecordPrefix:                 &recordPrefix,
				RecordSliceDuration:          &recordSliceDuration,
				RecordMaxDurationToMergeFile: &ecordMaxDurationToMergeFile,
			},
			Mp4Config: &model.Mp4RecordConfig{
				RecordCycle:                  300,
				RecordPrefix:                 &recordPrefix,
				RecordMaxDurationToMergeFile: &ecordMaxDurationToMergeFile,
			},
		},
	}
	response, err := client.UpdateRecordRule(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
}

    

Recording Rule Management: Querying Recording Rules

Description

Queries recording rules by rule_id.

Sample Code
package main

import (
	"fmt"

	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
	region "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/region"
	cloudrtc "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cloudrtc/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cloudrtc/v2/model"
)

func main() {
	ak := "<YOUR AK>"
	sk := "<YOUR SK>"
	endpoint := "<CloudRTC URL>"
	projectID := "<YOUR projectID>"

	auth := basic.NewCredentialsBuilder().
		WithProjectId(projectID).
		WithAk(ak).
		WithSk(sk).
		Build()

	client := cloudrtc.NewCloudRTCClient(
		cloudrtc.CloudRTCClientBuilder().
			WithRegion(region.NewRegion("<YOUR REGION>", endpoint)).
			WithCredential(auth).
			Build())

	request := &model.ShowRecordRuleRequest{
		AppId:  "<APPID>",
		RuleId: "<RuleID>",
	}
	response, err := client.ShowRecordRule(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
}

Recording Rule Management: Deleting a Recording Rule

Description

Deletes a recording rule by rule_id.

Sample Code
package main

import (
	"fmt"

	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
	region "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/region"
	cloudrtc "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cloudrtc/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cloudrtc/v2/model"
)

func main() {
	ak := "<YOUR AK>"
	sk := "<YOUR SK>"
	endpoint := "<CloudRTC URL>"
	projectID := "<YOUR projectID>"

	auth := basic.NewCredentialsBuilder().
		WithProjectId(projectID).
		WithAk(ak).
		WithSk(sk).
		Build()

	client := cloudrtc.NewCloudRTCClient(
		cloudrtc.CloudRTCClientBuilder().
			WithRegion(region.NewRegion("<YOUR REGION>", endpoint)).
			WithCredential(auth).
			Build())

	request := &model.DeleteRecordRuleRequest{
		AppId:  "<APPID>",
		RuleId: "<RuleID>",
	}
	response, err := client.DeleteRecordRule(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
}

5. Reference

6. Change History

Release Date Version Description
2023-08-11 1.0 This issue is the first official release.