All Products
Search
Document Center

AppServer source code integration

Last Updated: Aug 01, 2019

Overview

This topic describes the methods of the Alibaba Cloud SmartVideo AppServer. Using these methods, you can resolve issues such as data exchange between the SmartVideo application and ApsaraVideo for VOD and business logic processing. This topic can also help you quickly manage data. For example, you can randomly generate users, obtain video lists, obtain credential information, upload videos, and process various callback events. The technologies involved include the Spring Boot microservice framework, RDS for MySQL, and MyBatis persistence framework.

SmartVideo methods

Method type Method name Description
Credentials (AuthController) /vod/getSts Obtains the AccessKey ID, AccessKey Secret, and security token for upload authorization.
Credentials /vod/getVideoUploadAuth Obtains the video upload URL and credential based on the information about a video, such as its title and mezzanine file name.
Credentials /vod/refreshVideoUploadAuth Updates the video upload credential based on the video ID.
Credentials /vod/getVideoPlayAuth Obtains the video playback credential based on the video ID. You can use the video playback credential to play the relevant video on the client.
Credentials /vod/getImageUploadAuth Obtains the image upload URL and credential based on the information about an image, such as the image type.
Users (UserProfileController) /user/randomUser Creates a random user and obtains the user token to display user logic.
Videos (VodController) Publishes videos, deletes videos, obtains video lists, and performs other operations on the client.
Business logic /vod/videoPublish Publishes a video.
Business logic /vod/getRecommendVideoList Obtains a list of recommended videos.
Business logic /vod/getPersonalVideoList Obtains a list of videos in the user center.
Business logic /vod/deleteVideoById Deletes a video based on the video ID.
Resources (ResourceMediaController) Obtains the information about motion graphics materials, captions, or other resources.
Business logic /resource/getPasterInfo Obtains the information about motion graphics materials.
Business logic /resource/getMv Obtains the information about music videos (MVs).
Business logic /resource/getTextPasterList Obtains the information about captions based on the caption ID.
Business logic /resource/getPasterList Obtains the information about motion graphics materials based on the motion graphics material ID.
Business logic /resource/getFrontPasterList Obtains a list of facial motion graphics materials.
Business logic /resource/getFont Obtains the font information based on the font ID.
Music (DmhController) Obtains a list of recommended music and obtains the music playback URL based on the music ID.
Business logic /music/getRecommendMusic Obtains a list of recommended music.
Business logic /music/getPlayPath Obtains the music playback URL based on the music ID.

For more information, see AppServer SmartVideo methods.

Console methods

Method type Method name Description
Console (ConsoleController) /console/vod/getVideos Obtains a list of videos based on query criteria.
Console /console/vod/recommendVideo Adds a video in the user center to the list of recommended videos.
Console /console/vod/getRecommendVideoById Obtains the details of a recommended video based on the video ID.
Console /console/vod/pushObjectCache Preloads the content of trending videos.
Console /console/vod/getRecommendVideos Obtains a list of recommended videos based on query criteria.
Console /console/vod/createAudit Manually reviews or blocks a video.
Console /console/vod/getVideoById Obtains the details of a video based on the video ID.
Console /console/vod/recommendVideo Adds a video in the user center to the list of recommended videos.
Console /console/vod/deleteRecommendById Removes a video from the list of recommended videos.
Console /console/vod/deleteVideoById Deletes a video based on the video ID.
Console /console/vod/submitTabTranscode Submits a Narrowband HD transcoding job based on the transcoding template ID.
Console /console/vod/submitTranscode Submits a non-Narrowband HD transcoding job based on the transcoding template ID.
Console users (ConsoleUserController) /console/user/login Logs on to the console.
Console users /console/user/signOut Logs off from the console.

For more information, see AppServer console methods.

Alibaba Cloud service activation

The following Alibaba Cloud services are required: ECS, ApsaraDB for RDS, and ApsaraVideo for VOD.

You need to purchase all Alibaba Cloud services in the same region.

ECS

ECS is a computing service that features auto scaling. It can help you reduce IT costs and improve O&M efficiency, so that you can concentrate on core business innovation.

You need to set up a Java environment for ECS. We recommend that you install Java SE 8.

[alivcsdk@ilvb-app-server-1 ~]$ java -version 
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

ApsaraDB for RDS

ApsaraDB for RDS is a database that is optimized or deployed in a virtual computing environment. It has advantages such as pay-as-you-go, on-demand scaling, high availability, and storage integration.

  • The SmartVideo demo supports only MySQL.
  • You need to purchase an ApsaraDB for RDS instance in the same region as your ECS instance.

Purchase an ApsaraDB for RDS instance

ApsaraVideo for VOD

ApsaraVideo for VOD is an all-in-one audio/video-on-demand solution. It integrates video collection, editing, and upload, media resource management, automatic transcoding (Narrowband HD™), video content moderation and analysis, and CDN for accelerated content delivery. ApsaraVideo for VOD product page

Alibaba Cloud service configuration

ApsaraVideo for VOD

ApsaraVideo for VOD provides a variety of media processing capabilities to help you transcode audio and video files into formats suitable for playback on PCs, TVs, and mobile terminals. Based on the transcoding capability of ApsaraVideo for VOD, SmartVideo provides advanced capabilities such as secure watermark sharing, Narrowband HD, and automated review. To use SmartVideo, you need to configure domain names, transcoding templates, and callback events.

1. Configure domain names.

To access videos or images in ApsaraVideo for VOD, you need to configure at least a domain name. This domain name is also required when you configure projects. For more information, see Add domain names.

2. Configure transcoding templates.

Considering user experience and security in short video scenarios, you need to play videos without watermarks on an application or webpage, but add watermarks to downloaded videos and shared videos. We also recommend that you configure the same definition for played videos and downloaded videos. You can implement these features by using the transcoding service of ApsaraVideo for VOD. The configuration procedure is as follows: Log on to the ApsaraVideo for VOD console. In the left-side navigation pane, choose [Global Settings] > [Transcode]. On the Transcode page, click Create Template to create two transcoding templates and set Definition to Low Definition and Standard Definition, respectively, as follows:

  • Low Definition: 720p without watermarks
  • Standard Definition: 720p with watermarks Configure transcoding templates

For more information, see Transcoding settings.

3. Configure callback events.

Video processing services such as automated review, video snapshot, and transcoding jobs are processed asynchronously. ApsaraVideo for VOD notifies business parties of all the status updates and processing results by sending asynchronous messages or callback event notifications. The SmartVideo demo uses HTTP requests as the callback method to send callback event notifications. The configuration procedure is as follows: Log on to the ApsaraVideo for VOD console. In the left-side navigation pane, choose [Global Settings] > [Callback]. Select callback events related to transcoding, snapshot, video AI processing, and automated review for SmartVideo. For more information, see Instructions on callback event notifications. Configure callback events

ApsaraVideo for VOD allows you to add a specific signature header to each HTTP or HTTPS callback request. In this way, the server that receives callback event notifications can authenticate the signature to prevent illegal or invalid requests. For more information, see HTTP callback authentication. Configure callback authentication

ApsaraDB for RDS

Log on to the ApsaraDB for RDS console. You can view a list of activated ApsaraDB for RDS instances under your account, as shown in the following figure. List of ApsaraDB for RDS instances

Select an RDS for MySQL instance and click Manage in the Actions column to go to the instance management page, where you can create accounts and databases for this instance.

1. Manage accounts.

You can use a created account to log on to created databases and use relevant features. We recommend that you keep the username and password properly. Using this account, you can do as follows:

  • Create database connections in the code.
  • Log on to a database in the DMS console to create tables and initialize data.

On the instance management page, click Accounts in the left-side navigation pane. On the Accounts page, click Create Account. On the Create Account page, set parameters to create an account, including Database Account, Account Type, Password, and Re-enter Password. Manage RDS accounts

Generally, you can set Account Type to Standard Account. You can also leave Authorized Database blank and select authorized databases later. For more information, see Create accounts and databases.

2. Create a database. undefined

Key parameter Description
Supported Character Set The supported character set. Select utf8mb4 to support emoticons.
Authorized Account The account that is authorized to log on to this database. Select the previously created account.
Account Type The account permission. Select Read/Write to grant the required read and write permissions to the account.

3. Initialize the database. Logon page

In the ApsaraDB for RDS console, click Log on to DB in the upper-right corner to go to the database logon page in the DMS console. Select the network address of the relevant instance and use the username and password of the previously created account to log on to the previously created database. Then, you can create tables and initialize data as follows:

  • Create tables.

    Find the appserver_create_table.sql file in the sql folder in the root directory of your project. Use the table creation statements in the file to create tables.

  • Initialize table data.

    Run the following code to create an administrator account for the SmartVideo console:

    ```

insert into console_userinfo(id,user_id,user_name,avatar_url,password,remark) values(1,'12351232334','admin','','00000000','administrator');

> Change the values of the user_id, user_name, avatar_url, and password parameters as required.


### Alibaba Cloud RAM
RAM is an Alibaba Cloud service designed for user identity management and resource access control. You can use RAM to create and manage RAM users (such as employees, systems, or applications) and control their access permissions on resources under your Alibaba Cloud account. If multiple people in your enterprise work collaboratively and share resources, you do not have to share the AccessKey of your Alibaba Cloud account with others. You can use RAM to grant RAM users the minimum permissions necessary for them to complete their work, thereby reducing your enterprise's information security risks. RAM is required for almost all Alibaba Cloud services. STS is an Alibaba Cloud service that manages temporary access permissions for Alibaba Cloud accounts (or RAM users). Using STS, you can grant each RAM user (who you can manage under your Alibaba Cloud account) an access credential with a custom validity period and custom permissions. Then, RAM users can use this temporary access credential granted by STS to call Alibaba Cloud APIs or log on to the Alibaba Cloud console to access authorized resources.

[DO NOT TRANSLATE] [DO NOT TRANSLATE] [DO NOT TRANSLATE] The SmartVideo solution also uses STS to grant temporary access credentials. For more information, see [STS authorization](~~57114~~).

## Quick start
### Source code download
You can click [SDK download](~~51992~~) to download the SmartVideo AppServer source code package.

### Project description
The AppServer project supports the client in calling relevant methods and organizes data logic for the client. The directory structure of the project can be divided into the control layer, dao layer, service layer, and persistence layer.
![ undefined](http://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/assets/pic/108783/cn_zh/1552460542253/10.png)
> Note: To use the source code, you need to replace the values of some parameters based on your configuration, such as the database configuration, the AccessKey ID and AccessKey Secret, and relevant service configuration. For more information, see the following AppServer project configuration.
## AppServer project configuration

### Database
You must ensure that ApsaraDB for RDS is activated and the account and password are created for the ApsaraDB for RDS instance. Then, you need to replace the relevant configuration in source code files. For more information, see the application.properties file in the /sdk-api/src/main/resources directory.

DATABASE (After creating a database, you can use the username and password of the previously created account to log on to the database.)

spring.datasource.url = jdbc:mysql://{dburl}:3306/{dbname}? useSSL=false&useUnicode=true&characterEncoding=UTF-8 spring.datasource.username = <# The username used to log on to the RDS database. > spring.datasource.password = <# The password used to log on to the RDS database. >

>- {dburl}: the network address of the RDS instance. You can obtain this information from the ApsaraDB for RDS console as follows: Go to the instance management page and choose Basic Information > Intranet Address. The default port number is 3306.
- username and password: the username and password of the previously created RDS account.
- {dbname}: the name of the previously created RDS database.

### ApsaraVideo for VOD
You must ensure that ApsaraVideo for VOD is activated and the account and password are created for the ApsaraDB for RDS instance. Then, you need to replace the relevant configuration in source code files. For more information, see the application.properties file in the /sdk-api/src/main/resources directory.

ak information

REGION_CN_HANGZHOU = cn-hangzhou STS_API_VERSION = 2015-04-01 roleArn = roleSessionName = vod-role roleName =

transcoding template ID

TEMPLATEGROUP_ID = TAB_TEMPLATEGROUP_ID =

domain name

DOMAIN_NAME = AVATAR_DOMAIN_NAME = VOD_REGIONID = cn-shanghai

for VOD services policy

policy = {"Version": "1", "Statement": [ {"Action": ["vod:"], "Resource": [""],"Effect": "Allow"}]}

taihe music appkey

OPEN_API_URL = Q_SOURCE =

After you configure callback authentication on the Callback page of the ApsaraVideo for VOD console, you need to synchronize the configured authentication key to the VodCallBackController.java file in the /sdk-api/src/main/java/com/alivc/vod/controller directory.

/ Specifies the callback configuration key (privatekey). / public static final String CALLBACK_PRIVETEKEY = "";

### STS and dynamic identity and authorization for cloud applications
You must ensure that RAM is activated and configured by referring to [STS authorization](~~57114~~), and a temporary access credential is obtained. In addition, RAM is used to allow applications to obtain the dynamic STS token of a RAM role for access to Alibaba Cloud APIs. For more information, see [Dynamic identity and permission management of cloud applications](~~93746~~). Then, you need to replace the values of the following parameters in source code files: REGION_CN_HANGZHOU, STS_API_VERSION, roleArn, roleSessionName, TEMPLATEGROUP_ID, TAB_TEMPLATEGROUP_ID, DOMAIN_NAME, AVATAR_DOMAIN_NAME, policy, OPEN_API_URL, and Q_SOURCE. For more information, see the application.properties file in the /sdk-api/src/main/resources directory.

ak information

REGION_CN_HANGZHOU = cn-hangzhou STS_API_VERSION = 2015-04-01 roleArn = roleSessionName = vod-role roleName =

transcoding template ID

TEMPLATEGROUP_ID = TAB_TEMPLATEGROUP_ID =

domain name

DOMAIN_NAME = AVATAR_DOMAIN_NAME = VOD_REGIONID = cn-shanghai

for VOD services policy

policy = {"Version": "1", "Statement": [ {"Action": ["vod:"], "Resource": [""],"Effect": "Allow"}]}

taihe music appkey

OPEN_API_URL = Q_SOURCE =

## Service packaging and compilation
1. Compile and package project files: Run the following command to go to the root directory of the disk where the project files are stored:
mvn clean package -Dmaven.test.skip=true
```
![ build](http://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/assets/pic/108783/cn_zh/1552460554660/11.png)

As shown in the preceding figure, the "BUILD SUCCESS" message indicates that the project files are packaged. You can find a JAR package named sdk-api-0.0.1-SNAPSHOT.jar in the target/ directory.
  1. Deploy the AppServer: Upload the JAR package to the current user directory of your ECS instance. Run the following command to start the AppServer:
    nohup java -jar sdk-api.jar &
    

undefined

  1. Access the project: Use the default port number 8080 and your server IP address to access the AppServer project (at http://{ip}/swagger-ui.html#!/). The following figure shows that the AppServer is deployed. undefined

Note: You can modify the port number 8080 in the Application.java file of the project in Spring Boot.

public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(8080);
}
- {ip}: the public IP address of the ECS instance. You can obtain this information from the ECS console. If you have configured a domain name, you can also resolve the domain name to this IP address and use the domain name to access the project.
- For example, use http://{ip}:{port}/ as the endpoint for calling methods.
  1. View logs.

    Run the following command to view logs:

tail -f nohup.out

Log files

AppServer frontend webpage configuration

You can find the frontend source code in the video-admin folder in the root directory of the AppServer project. To visit the frontend webpage, you need to configure the resources tag in the pom.xml file and change the folder name specified by the targetPath parameter to the package name of the frontend source code. For more information, see AppServer console source code integration.

Key business logic

Video publish

When you upload a video on the client, you also submit a first frame snapshot job, a video snapshot job (for taking eight video snapshots), and a video review job asynchronously. Then, ApsaraVideo for VOD takes video snapshots and reviews the video. When ApsaraVideo for VOD completes the review, it sends a callback event notification to notify the AppServer of the review result. After receiving the review result, the AppServer distinguishes the first frame snapshot job from the video snapshot job based on the returned snapshot type and number of snapshots. If the first frame snapshot job is completed, the AppServer modifies the first frame data in the video library. If the video snapshot job is completed, the AppServer inserts the snapshot data (images and user ID) into the snapshot table.

Callback after the completion of a video snapshot job

After a video snapshot job is completed, ApsaraVideo for VOD triggers a SnapshotComplete event and sends an event notification to notify the AppServer of the job result. For more information, see SnapshotComplete. In the AppServer project, you submit two snapshot jobs when you upload a video: a first frame snapshot job that takes a snapshot of the first frame in the video, and a video snapshot job that takes multiple video snapshots for you to view in the SmartVideo console. After receiving a notification of the SnapshotComplete event, the AppServer checks the returned data as follows: Check the SubType parameter first. If the value is SpecifiedTime, the snapshots are taken through the SubmitSnapshotJob operation. Then, check the Status parameter. If the value is success, the snapshot job succeeded. Then, check the number of elements in the JSON-formatted array of the SnapshotInfos parameter. Multiple elements indicate multiple video snapshots. The AppServer stores the snapshots in the snapshot table. A single element indicates the first frame snapshot. The AppServer changes the snapshot status to successful in the vod_videos table and updates the first frame in the table to this snapshot.

Callback after the completion of a video review job

After a video review job is completed, ApsaraVideo for VOD triggers an AIMediaAuditComplete event and sends an event notification to notify the AppServer of the job result. For more information, seeAIMediaAuditComplete. After receiving this event notification, the AppServer checks the returned data as follows: Check the Status parameter first. If the value is success, the review job succeeded. Then, check the Suggestion parameter of the Data parameter. If the value of the Suggestion parameter is pass, the video passes the review. The AppServer changes the review status to successful and calls a manual review method to submit a non-Narrowband HD transcoding job. If the value of the Suggestion parameter is block, the video contains violations. The AppServer changes the review status to failed. If the value of the Suggestion parameter is review, the video needs to be reviewed manually. The AppServer changes the review status to to-be-reviewed. If the value of the Status parameter is fail, the review job failed. The AppServer changes the review status to failed.

Callback after the completion of a transcoding job

After a transcoding job is completed, ApsaraVideo for VOD triggers a TranscodeComplete event and sends an event notification to notify the AppServer of the job result. For more information, see TranscodeComplete. After receiving this event notification, the AppServer checks the returned data as follows: Check the Status parameter first. If the value is success, the transcoding job succeeded. The AppServer changes the transcoding status to successful and stores the playback URL (FileUrl) and video ID (VideoId) of the transcoded video in the transcoding table. If the value is fail, the transcoding job failed. The AppServer changes the transcoding status to failed. Sequence diagram SmartVideo sequence diagram