tencent cloud

Feedback

How to Receive Event Notification

Last updated: 2023-05-15 17:39:11

    Overview

    About this document

    In this document, we will run you through the process of uploading and transcoding a video and show you how to use VOD's event notifications.

    Architecture and workflow

    In the demo, an HTTP server was built based on SCF to receive event notification requests from VOD. The server initiates transcoding tasks and gets the transcoding results based on the NewFileUpload and ProcedureStateChanged notifications it receives.
    The system consists of four main components: the console, API Gateway, SCF, and VOD. API Gateway and SCF are the deployment objects of the demo.
    
    The workflow is as follows:
    1. Upload a video to the VOD console.
    2. VOD sends a NewFileUpload notification request to the demo.
    3. The demo parses the notification and calls VOD's ProcessMedia API to transcode the uploaded video using the preset transcoding templates 100010 and 100020.
    4. After completing the transcoding task, VOD sends a ProcedureStateChanged notification request to the demo.
    5. The demo parses the notification and prints the URL of the transcoding output in SCF logs.
    Note:
    The SCF code in the demo is developed based on Python 3.6. SCF also supports other programming languages such as Python 2.7, Node.js, Go, PHP, and Java. For more information, see Code Development.

    Fees

    The demo for receiving VOD event notifications provided in this document is open-source and free of charge, but to build the demo, you may incur the following fees:
    Fees for purchasing a Tencent Cloud CVM instance to execute the script. For more information, see CVM Billing Mode.
    Fees for using SCF to distribute signatures. For more information, see Pay-As-You-Go and Free Tier.
    Fees for using Tencent Cloud API Gateway to provide public network APIs for SCF. For more information, see Shared Instance Billing.
    Fees for storing videos uploaded to VOD. For details, see Pay-As-You-Go and Prepaid Packages.
    Fees for transcoding videos stored in VOD. For details, see Pay-As-You-Go and Prepaid Packages.

    Impact on your production environment

    The demo uses VOD's event notification mechanism, for which you need to configure an event notification address. If there is already a VOD-based production environment under your account, changing the event notification address may affect your active business. Therefore, before doing so, make sure it will not affect your production environment. If you are not sure, please use a new account to deploy the demo.

    Quickly Deploying Event Notification Receipt Service

    Step 1. Prepare a CVM instance

    The deployment script needs to be executed on a CVM instance that mees the following requirements:
    Region: No limit.
    Model: The minimum specification (1 CPU core and 1 GB memory) or higher.
    Public network: A public IP address is required, and the bandwidth should be at least 1 Mbps.
    Operating system: Official public image Ubuntu Server 16.04.1 LTS 64-bit or Ubuntu Server 18.04.1 LTS 64-bit.
    For detailed directions on how to purchase a CVM instance and reinstall the system, see Operation Guide - Creating Instances via CVM Purchase Page and Operation Guide - Reinstalling System.
    Note:
    The demo itself does not depend on CVM, but it needs a CVM instance to run the deployment script.
    If you do not have a CVM instance that meets the above conditions, you can also run the script on other Linux (such as CentOS or Debian) or macOS servers with public network access, but you need to modify certain commands in the deployment script based on the operating system.

    Step 2. Activate VOD

    Activate VOD. For detailed directions, see Getting Started - Step 1. Activate VOD.

    Step 3. Get the API key and APPID

    Your API key (i.e., SecretId and SecretKey) and APPID are required for deploying and running the demo.
    If you have not created an API key yet, create one as instructed in Creating an API key for a root account. If you have already created a key, follow the steps in Viewing an API key of a root account to view the key.
    You can view your APPID on the Account Information page of the console.
    

    Step 4. Deploy the notification receipt service

    Log in to the CVM instance prepared in Step 1 and run the following command on the remote terminal:
    ubuntu@VM-69-2-ubuntu:~$ export SECRET_ID=AKxxxxxxxxxxxxxxxxxxxxxxx; export SECRET_KEY=xxxxxxxxxxxxxxxxxxxxx;export APPID=125xxxxxxx;git clone https://github.com/tencentyun/vod-server-demo.git ~/vod-server-demo; bash ~/vod-server-demo/installer/callback_scf.sh
    Note:
    You need to assign values to SECRET_ID, SECRET_KEY, and APPID according to the key and APPID obtained in Step 3.
    This command will download the demo source code from GitHub and automatically run the installation script. The installation process will take several minutes (subject to the CVM network conditions), during which the remote device will print the following information:
    [2020-06-05 17:16:08]Start checking npm.
    [2020-06-05 17:16:12] npm is successfully installed.
    [2020-06-05 17:16:12]Start installing serverless.
    [2020-06-05 17:16:13]Serverless is successfully installed.
    [2020-06-05 17:16:14] Start deploying the event notification receipt service.
    [2020-06-05 17:16:24] The event notification receipt service is deployed.
    [2020-06-05 17:16:26] Service address: https://service-xxxxxxxx-125xxxxxxx.gz.apigw.tencentcs.com/release/callback
    Copy the address of the event notification receipt service in the output log (which is https://service-xxxxxxxx-125xxxxxxx.gz.apigw.tencentcs.com/release/callback in this example).
    Note:
    If the following warning is printed in the output log, it is probably because the CVM instance cannot parse the domain deployed immediately. You can ignore this warning.
    [2020-04-25 17:18:44] Warning: The event notification receipt service failed the test.

    Step 5. Configure the event notification address

    Note:
    As mentioned in Impact on your production environment, before you perform the following operations, please make sure that your active business does not depend on VOD event notifications.
    1. Log in to the VOD console and select Application Management on the left sidebar.
    2. Select the target application.
    3. Click Callback Settings on the left sidebar and click Set. For Event Notification Method, select "Normal Callback", and enter the address obtained in Step 4. Select all event notification types, and click Confirm.
    
    Note:
    If you see two callback URL configurations (v2.0 and v3.0) in the console, configure v3.0.

    Step 6. Test the demo

    1. Upload a test video to VOD (select No processing after upload). After the video is uploaded, if the status of the video on the Uploaded page is "Processing", it indicates that the demo has received the NewFileUpload notification and initiated a transcoding task.
    2. After the transcoding is finished (status becomes "Normal"), click Quick View, and you will see the two transcoding outputs for the file on the right.
    3. Log in to the SCF console and go to the log page. In the latest log, you can see that the URLs of the two output files have been printed. You can use SCF to record the URLs in your database or send them to viewers.
    Note:
    There may be a delay in log generation. If you can't find the URLs, please wait for a minute or two and click Reset to refresh the page.

    System Design

    API protocol

    The event notification receipt function uses API Gateway to provide APIs. For the API protocols, see Video Upload Completion and Task Flow Status Change.

    Code interpretation

    1. main_handler() is the entry function.
    2. Call parse_conf_file() to read configuration information from the config.json file.
    Field
    Type
    Description
    secret_id
    String
    The API key.
    secret_key
    String
    The API key.
    region
    String
    The TencentCloud API request region, which can be any region.
    definitions
    Array of Integer
    The transcoding template.
    subappid
    Integer
    The VOD subapplication the event notification is from.
    3. For NewFileUpload event notifications, call deal_new_file_event() to parse the request and get the file ID of the uploaded video.
    if event_type == "NewFileUpload":
    fileid = deal_new_file_event(body)
    if fileid is None:
    return ERR_RETURN
    4. Call trans_media() to initiate transcoding, output the API's response packets to SCF logs, and send the response packets to the event notification service of VOD.
    rsp = trans_media(configuration, fileid)
    if rsp is None:
    return ERR_RETURN
    print(rsp)
    5. In trans_media(), call the TencentCloud API ProcessMedia:
    cred = credential.Credential(conf["secret_id"], conf["secret_key"])
    client = vod_client.VodClient(cred, conf["region"])
    
    method = getattr(models, API_NAME + "Request")
    req = method()
    req.from_json_string(json.dumps(params))
    
    method = getattr(client, API_NAME)
    rsp = method(req)
    return rsp
    6. For ProcedureStateChanged event notifications, call deal_procedure_event() to parse the request, get the URL of the transcoding output, and print it in SCF logs:
    elif event_type == "ProcedureStateChanged":
    rsp = deal_procedure_event(body)
    if rsp is None:
    return ERR_RETURN
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support