How to Receive Event Notification

Last updated: 2020-11-10 16:23:24

    Overview

    Demo features

    This document uses the video upload and transcoding process as an example to describe how to use the event notification mechanism of VOD.

    Architecture and process

    An HTTP service is built based on SCF in the demo to receive event notification requests from VOD. It initiates video transcoding and gets the transcoding result by processing NewFileUpload (video upload completion) and ProcedureStateChanged (task flow status change) event notifications.

    The system mainly involves four components: console, API Gateway, SCF, and VOD. Here, API Gateway and SCF are the deployment objects of this demo as shown below:

    The specific business process is as follows:

    1. Upload a video to VOD in the console.
    2. The VOD backend initiates the NewFileUpload event notification request to the demo.
    3. The demo parses the event notification content and calls the ProcessMedia API of VOD to initiate transcoding for the uploaded video with the 100010 and 100020 preset transcoding templates.
    4. After completing the transcoding task, VOD initiates the ProcedureStateChanged event notification request to the demo.
    5. The demo parses the event notification content and prints the URL of the transcoding output file into 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 your choice as needed. For more information, please see Development Guide.

    Fees

    The VOD event notification receipt service demo provided in this document is open-source and free of charge, but it may incur the following fees during service building and use:

    • Fees for purchasing a Tencent Cloud CVM instance to run the service deployment script. For more information, please see Instance Billing Modes.
    • Fees for using signature distribution service provided by SCF. For more information, please see Billing Mode and Free Tier.
    • Fees for using Tencent Cloud API Gateway to provide public network APIs for SCF. For more information, please see Billing Overview.
    • VOD storage space will be taken up by uploaded videos. For more information, please see Video Storage Pricing.
    • VOD transcoding service will be used for video transcoding. For more information, please see Video Transcoding Pricing.

    Avoiding affecting production environment

    The business logic of the event notification receipt service demo uses the VOD event notification mechanism; therefore, you need to set the event notification address during deployment. If there is already a VOD-based production environment under your account, changing the event notification address may cause business exceptions. Before doing so, please confirm that this will not affect the 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 meeting the following requirements:

    • Region: not limited.
    • Model: the minimum official configuration (1 CPU core and 1 GB memory) is sufficient.
    • Public network: a public IP 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, please see Operation Guide - Creating Instances via CVM Purchase Page and Operation Guide - Reinstalling System, respectively.

    Note:

    • The event notification receipt service demo itself does not depend on CVM but only uses CVM to run the deployment script.
    • If you do not have a CVM instance satisfying the above conditions, you can also run the script on another Linux (such as CentOS or Debian) or macOS server with public network access, but you need to modify certain commands in the deployment script based on the operating system. Please search for the specific modification method by yourself.

    Step 2. Activate VOD

    Please activate the VOD service as instructed in Getting Started - Step 1.

    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 event notification receipt service demo.

    • If you have not created an API key yet, please generate one as instructed in Root Account Access Key. If you have already created a key, please get it as instructed in the same document.
    • You can view the APPID on the Account Information page in the console as shown below:

      Step 4. Deploy the event notification receipt service

    Log in to the CVM instance prepared in step 1 as instructed in Logging into Linux Instance in Standard Login Method and enter and run the following command on the remote device:

    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:

    Please assign the corresponding values obtained in step 3 to SECRET_ID, SECRET_KEY, and APPID in the command.

    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 installing pip3.
    [2020-06-05 17:16:12] pip3 is successfully installed.
    [2020-06-05 17:16:12] Start installing Tencent Cloud SCF.
    [2020-06-05 17:16:13] SCF is successfully installed.
    [2020-06-05 17:16:13] Start configuring SCF.
    [2020-06-05 17:16:14] SCF configuration is completed.
    [2020-06-05 17:16:14] Start deploying the event notification receipt service of VOD.
    [2020-06-05 17:16:24] The event notification receipt service of VOD 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 displayed in the output log, it is generally because the CVM instance cannot immediately parse the service domain name deployed just now. 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

    As described in Avoiding affecting production environment, please confirm that your business in the production environment does not depend on VOD event notifications before performing the following operations:

    Log in to the VOD Console, click Settings, select Normal Callback as the callback mode, enter the event notification receipt service address obtained in step 4 as the callback URL, check all callback events, and click OK as shown below:

    Note:

    If two callback URL configuration items (v2.0 format and v3.0 format) are displayed at the same time in the console, please configure the v3.0 one.

    Step 6. Test the demo

    Upload a test video to VOD as instructed in Uploading Video - Local Upload. Select the default option "No Processing After Upload" during the upload. After the upload is completed, you can see that the video status is "Processing" on the "Uploaded" tab, which indicates that the demo has received the NewFileUpload event notification and initiated a transcoding request.

    Wait for the video processing to complete (indicated by the "Normal" status), click Quick View, and you can see that there are two output videos for the uploaded video on the right of the page.

    Log in to the SCF Console and enter the log page to view the SCF logs. In the latest log, you can see that the URLs of the two output files have been printed out. In actual use cases, you can use SCF to record the URLs in your database or publish them to viewers through other channels.

    Note:

    SCF log generation may have a certain delay. If no logs are displayed on the page, please wait for one or two minutes and click Reset to refresh.

    System Design Description

    API protocol

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

    Event notification receipt service code interpretation

    1. main_handler() is the entry function.

    2. Call parse_conf_file() and read the configuration information from the config.json file. The configuration items are as described below:

      Field Data Type Description
      secret_id String API key
      secret_key String API key
      region String TencentCloud API request region, which can be any region for VOD
      definitions Integer Array Transcoding template
      subappid Integer Whether the event notification is from a VOD subapplication
    3. For NewFileUpload event notifications, call deal_new_file_event() to parse the request and get the FileId of the newly uploaded video from it.

              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 TencentCloud 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 SDK to initiate the ProcessMedia request:

           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 to get the transcoding output video URL and print it to SCF logs:

              elif event_type == "ProcedureStateChanged":
                  rsp = deal_procedure_event(body)
                  if rsp is None:
                      return ERR_RETURN

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help