Using SCF to Input Online Media Streams for TRTC

Last updated: 2021-08-26 15:30:20

    Overview

    Use cases

    AI interactive teaching

    You can perform online live interactive teaching through recorded teaching videos combined with AI technology.

    Before class, the knowledge point explanation, interactive questions, question feedback and answers are recorded into video segments and uploaded to the video library based on the teaching curriculum. In the class, the existing recorded video is pushed to the TRTC room for live broadcasting through the SCF. Students can perform interactive learning through voice and touchscreen. Through the AI technology, the server can intelligently recognize students' voice chats and answers, and seamlessly switch teaching segments based on students' performance, and give different feedbacks in real time, so as to provide a personalized teaching.

    Sharing videos in a room

    For live streaming such as games, shows, and sports events, RTMP live stream can be pushed to TRTC room through SCF for real-time communication and driving hotspot. For VOD programs such as movies and music, the media files can be converted into online media streams and input to TRTC rooms through SCF, providing value-added services and creating a community. SCF features one-click triggering, OPS-free operation, and auto scaling, which can quickly support the construction of real-time interactive entertainment and social applications. With programmability, SCF can quickly integrate other Tencent Cloud services and third-party services to expand businesses, and innovate usages efficiently.

    Business process

    This document describes how to use API gateway to integrate SCF to push the recorded videos or RTMP live stream to TRTC room for live broadcasting, providing an out-of-the-box, flexible, convenient, and programmable online media stream input capabilities. If you need to enable real-time records of the pushed live stream, you can use Redis, and the API gateway will write the progress to Redis in real time. The workflow is shown in the figure below:

    img

    The parameters for calling API are as follows:

    Parameter Type Required Description
    videoSrc String Yes The pushed video streams
    sdkAppId String Yes App ID, which is used to distinguish different TRTC applications.
    roomId String Yes Room ID, which is used to uniquely identify a room in a TRTC application.
    userId String Yes User ID, which is used to uniquely identify a user in a TRTC application.
    userSig String Yes User signature, which is used to authenticate the user login.
    clientRole String No Role name. Default value: anchor
    redis Boolean No Whether to use Redis. Default value: false
    redisHost String No The host address of Redis when redis is set as true.
    redisPort Integer No The port number for accessing Redis when redis is set as true.
    redisPassword String No The password for accessing redis when redis is set as true.
    Note:

    • If the redis is set as false, when the stream is pulled from videoSrc video stream for live stream push, the live stream will start from the latest.
    • If the redis is set as true, for the same videoSrc video stream, the API gateway will first check in redis whether there is a record of the last live stream push:
      • If there is a record, the last stream push will be restored.
      • If there is no record, the stream push will be started again. The progress of pushing the live stream is written to Redis in real time through callbacks.

    Directions

    Creating a function

    1. Log in to the SCF console and select Function Service in the left sidebar.
    2. At the top of the Function Service page, select the Beijing region and click Create to enter the function creating page and configure the function as shown below:
      • Create Method: select Template.
      • Fuzzy Search: enter “TRTC” for fuzzy search.
        Click Learn More in the template to view relevant information in the Template Details pop-up window, which can be downloaded.
    3. Click Next and configure the related information as shown below:
      img
      • Function Name: the function name is automatically generated by default.
      • Async Execution: check the checkbox to enable the async execution. When it’s enabled, the function will response to the event in async execution mode. The event goes to async execution status after being invoked without waiting for the process result.
      • Status Trace: check the checkbox to enable the status trace. When it's enabled, it will keep logs of real-time status of response for async function events. You can query and stop the event, and check the related statistics. Data of event status will be kept for three days.
      • Execution Timeout Period: it can be modified as needed.
    4. Configure an API gateway trigger. An API service is created by default and the integration response is disabled. You can customize a trigger, but ensure that the integration response is disabled, as shown below:
      img
    5. Click Complete.
    6. If you need to use Redis to record the push progress in real time, since Redis can only be accessed via private network, you need to configure the SCF in the same VPC as the Redis, as shown below:

    Creating a TRTC application

    1. Log in to the TRTC console and select Development Assistance > Demo Quick Run in the left sidebar.
    2. Enter the demo name and click Create to complete the application creation. You can select a template for your own client to perform a dry run.

    Testing the function

    1. Use Postman to send HTTP requests, as shown below:
    2. After the request is sent, an async function response "Async run task submitted" will be received. The RequstId of this function will be returned via x-scf-reqid in the HTTP header.
    3. On the Function Service page in the SCF console, click the name of the function created in the Creating a function above to enter the function details page.
    4. Select the Log Query tab on the function details page to view the printed push log information.
    5. Log in to the TRTC console, and click the room ID in the Monitoring Dashboard page to view the push monitoring details.
    6. If you want to stop the push during the push process, you can call the TerminateAsyncEvent API and set the parameter InvokeRequestId to stop the push. Status trace must be enabled.
      InvokeRequestId can be obtained from the response header x-scf-reqid in step 2 above.