The content of this page has been automatically translated by AI. If you encounter any problems while reading, you can view the corresponding content in Chinese.

Active Polling Method

Last updated: 2024-12-20 10:36:31

Document Transcoding Access Process (Active Polling Method)

Active Polling Method, due to the limitation of polling interval duration (recommended interval 2s for polling transcoding progress), it is not possible to get the transcoding result in a timely manner, so it is more recommended to use Registration Callback Method.

Preparations

Bucket Configuration (CFS resources after document transcoding rely on COS, before using the document transcoding feature, please first perform Bucket Configuration)
Note:
Due to the time-consuming nature and queuing time of document transcoding, it is recommended to use the server-side API for pre-transcoding, and directly use the transcoding result on the client. It is not recommended to directly call the transcoding interface applyFileTranscode of the Interactive Whiteboard SDK on the client to avoid long waiting times, affecting the product experience.
Interaction Process (Active Polling Method):
交互流程



Access Steps

1. Upload Document (using Tencent Cloud COS as an example)

To enable the Tencent Cloud transcoding server to obtain your courseware for transcoding, you need to provide a URL that the transcoding server can use to download the courseware.
Note:
1. It is recommended to use Tencent Cloud's COS service to provide the download address. Of course, you can also upload to other storage servers or upload in other ways.
2. Here, using Tencent Cloud COS in the Go language as an example. For more language implementations, please refer to Tencent Cloud COS.
Sample code:
package main

import (
"context"
"fmt"
"log"
"net/http"
"net/url"
"github.com/tencentyun/cos-go-sdk-v5"
"os"
)

func main() {
// Initialize COS resources TODO: Replace `examplebucket-1250000000` and `COS_REGION` with the actual information
domain := "https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com"
u, _ := url.Parse(domain)
b := &cos.BaseURL{BucketURL: u}
cosClient := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
// TODO: COS_SECRETID needs to be replaced with the user's real SECRETID, COS_SECRETKEY needs to be replaced with the user's real SECRETKEY
SecretID: "COS_SECRETID",
SecretKey: "COS_SECRETKEY",
},
})
// TODO: Object key name to upload to COS
keyName := "test/objectPut.go"
// Local file path
localFilePath := "test/objectPut.go"
// Get file size
file_info, err := os.Stat(localFilePath)
if err != nil {
panic(err)
}

opt := &cos.ObjectPutOptions{}
opt.ObjectPutHeaderOptions = &cos.ObjectPutHeaderOptions{}
opt.ObjectPutHeaderOptions.ContentLength = int(file_info.Size())

// The object key is the unique identifier of the object in the bucket.
// For example, in the access domain name `examplebucket-1250000000.cos.COS_REGION.myqcloud.com/test/objectPut.go`, the object key is `test/objectPut.go`
// Start the upload
_, err = cosClient.Object.PutFromFile(context.Background(), keyName, localFilePath, nil)
if err != nil {
// Upload failed
panic(err)
}
// Upload successful, assemble resultUrl
resultUrl := fmt.Sprintf("%s/%s", domain, keyName)
log.Printf("upload successful! resultUrl[]%s", resultUrl)
}

2. Initiate transcoding

Due to the dynamic transcoding time and queuing delays, it is recommended to use the server-side API for transcoding in advance. The client can directly use the transcoding results. It is not recommended to call the transcoding interface directly on the client side addTranscodeFile to avoid long waits and affecting user experience.
Note:
1. For API description, please refer to Document Transcoding Related Interfaces.
2. Get SecretId and SecretKey from API Key Management
3. For sample code of other programming languages or transcoding APIs, please refer to Sample Code Generator.
Sample code (golang example):
package main

import (
"fmt"

"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"
tiw "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tiw/v20190919"
)

func main() {
// "SECRETID" and "SECRETKEY" need to be obtained from the API Key Management in the console
credential := common.NewCredential(
"SECRETID",
"SECRETKEY",
)
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "tiw.tencentcloudapi.com"
client, _ := tiw.NewClient(credential, "ap-guangzhou", cpf)

request := tiw.NewCreateTranscodeRequest()
// SdkAppId is your own interactive whiteboard application ID. Url is the download address obtained after uploading the document
params := "{\"SdkAppId\":\"xxxxxxxxx\",\"Url\":\"https://board-sdk-1259648581.file.myqcloud.com/TIC/1590997573551/WelcomeNewStudents.pptx\"}"
err := request.FromJsonString(params)
if err != nil {
panic(err)
}
response, err := client.CreateTranscode(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return
}
if err != nil {
panic(err)
}
fmt.Printf("%s", response.ToJsonString())
}

3. Query Progress by Task ID

Use the task ID returned in step 2 to check the current progress of the task.
Note:
1. For API description, please refer to Document Transcoding Related Interfaces.
2. Get SecretId and SecretKey from API Key Management
3. For sample code of other programming languages or transcoding APIs, please refer to Sample Code Generator.
Sample code (using Golang as an example):
package main

import (
"fmt"
"time"

"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"
tiw "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tiw/v20190919"
)

const (
// File Transcoding Status
StatusQueued = "QUEUED" // Queued for conversion
StatusProcessing = "PROCESSING" // Converting
StatusFinished = "FINISHED" // Conversion finished
)

func main() {
// "SECRETID" and "SECRETKEY" need to be obtained from the API Key Management in the console
credential := common.NewCredential(
"SECRETID",
"SECRETKEY",
)
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "tiw.tencentcloudapi.com"
client, _ := tiw.NewClient(credential, "ap-guangzhou", cpf)
//
for {
request := tiw.NewDescribeTranscodeRequest()
// SdkAppId is the user's own interactive whiteboard application ID. TaskId is the task ID returned by the transcoding server when the transcoding task is initiated
params := "{\"SdkAppId\":xxxxxxx,\"TaskId\":\"07lt80iij017uhjp12sb\"}"
err := request.FromJsonString(params)
if err != nil {
panic(err)
}
response, err := client.DescribeTranscode(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return
}
if err != nil {
panic(err)
}
// Queried successfully
fmt.Printf("%s", response.ToJsonString())
if nil == response.Response {
panic(fmt.Errorf("response.Response is nil!"))
}
// Process query results
if StatusQueued == *response.Response.Status {
// Task in queue
fmt.Printf("transcode task status is [%s]", *response.Response.Status)
} else if StatusProcessing == *response.Response.Status {
// Task being transcoded
fmt.Printf("transcode task status is [%s] progress [%d]", *response.Response.Status, *response.Response.Progress)
} else if StatusFinished == *response.Response.Status {
resultUrl := *response.Response.ResultUrl
// Transcoding success
fmt.Printf("transcode successful! resultUrl[%s]", resultUrl)
}
time.Sleep(1 * time.Second)
}
}


4. Use the transcoding results

The client sets the transcoding results to the interactive whiteboard to achieve features such as document previous/next page.
Note:
1. For the whiteboard API description, please refer to Whiteboard API Description.
2. The following takes web-side JS as an example. Other languages need to be implemented by yourself.
Sample code (JS as an example):
// The client assembles parameters based on the document transcoding returned results
let config = {
url: response.Response.ResultUrl,
title: response.Response.Title,
pages: response.Response.Pages,
resolution: response.Response.Resolution
}
// Add the document transcoding results to the whiteboard
this.teduBoard.addTranscodeFile(config);
Display effect: