Welcome to cloud + community, get more Tencent mass technology practice dry goods oh ~

The author:
Tencent Video Cloud


Original title: Tencent cloud live answer plan analysis

The original link: https://cloud.tencent.com/developer/article/1027814

Entering 2018, the hottest new thing is undoubtedly “live quiz”, with millions of prize money attracting a large number of users to participate. A live broadcast is often millions of bonus, each person can be allocated dozens of yuan or even one million yuan, more and more people hope to use their IQ to earn some pocket money. How popular is live answering?

Multiple livestream platform servers suffered multiple outages. In fact, having millions of people online at the same time can put a lot of pressure on servers, let alone new apps, which may not expect to be so popular.

Recently, many livestream answering apps showed incorrect amount of prize money on individual accounts, online users were unable to enter the game, and the game had to be temporarily cancelled due to technical problems. In one case, the system incorrectly judged the correct answer, forcing millions of people to stop playing the game. Every few days

In addition to the App’s lack of preparation, the technical glitch also reflects the popularity of the genre. In order to allow many companies and developers to catch up with the storm as soon as possible, Tencent Cloud Live team is also the first time online “online knowledge competition solution”.

Effect of experience

IOS Platform (IPA) Android Platform (APK) Obs Studio

iOS                      Android                    Obs Studio

Online answer room

Create a room to experience the host side of the effect; Enter the room to experience the audience side effect.

The Demo for iPhone uses enterprise signature mode. You need to add the trust certificate in Settings – General – Device Management.

Demo is only used for demonstration, to show the technical capabilities of Tencent Cloud, does not represent the final product form, access please read the document carefully.

Experience Mode 2: Obs Studio

This experience is directly streamed by using Obs Studio transformed by Tencent Cloud:

  • We have added a “Distribute topics” button to the Tools menu bar, which allows topics to be uploaded directly to the live stream. Questions can be pre-edited in the INI file.
  • Use the iOS or Android demo App, select the answer player function, you can experience the audience side of the question collection effect.
  • For details about how to use OBS Studio, see the instruction manual of Tencent Cloud Customized OBS Studio.

Download the SDK

Download address

Our advantages

  • Both Tencent cloud SDK and cloud support the insertion of topic or time synchronization signaling in the live stream, which can achieve perfect synchronization of sound, picture and topic pop up.
  • The delay correction technology supported by the extreme playback mode of Tencent cloud SDK can make the delay difference between the audience within 1s, so as to ensure the synchronization of the audience’s answers.
  • Tencent cloud SDK has been packaged in wechat version by default, and provided externally in the form of tag. Set mode to live, and set min-cache and max-cache to 1. Can achieve very low delay playback effect.

plan

Scheme 1: Transparent transmission scheme

The principle of description

  • Message sending (Obs) : If you are using Obs streaming in the Studio, you can directly replace the existing Obs software with Obs Studio modified by Tencent Cloud. – We have added a “topic distribution” button in the “Tools” menu bar, which can directly put the topic into the live stream, and the topic can be pre-edited in the INI file.
  • Message sending (APP) : If you want to simply use APP to push the stream, you can use the sendMessage method of TXLivePusher of Tencent cloud terminal SDK, which can plug a buffer into the RTMP stream (the maximum length of buffer is limited to 10K).

    / / iOS sample code [_answerPusher sendMessage: [mesg dataUsingEncoding: NSUTF8StringEncoding]].

    / / Android sample code mTXLivePusher. SendMessage (questionInfo. GetBytes (” utf-8 “));

  • Message reception: onPlayEvent (PLAY_EVT_GET_MESSAGE: 2012) function, when the player plays to the specified screen, the MESSAGE can be synchronously notified to your APP, so as to synchronously spread the topic to a large number of audiences.

Message access scheme can refer to our access document (iOS | Android platform)

Scheme 2: NTP time synchronization scheme

The principle of description

  1. Tencent Cloud will insert the international standard time stamp of NTP calibration into your live stream in real time every 1s.
  2. The director of the studio according to the rhythm of the host, in the appropriate time to control the issue, the issue system will be in each issue with the international standard time.
  3. When the SDK plays this time-stamped video stream, it will periodically inform your APP when the current VIDEO stream played by the SDK was recorded (since there is a fixed delay from the broadcast station to the cloud, you need to make an error correction in advance).
  4. Your APP can display the specified topic as required according to the time notification of SDK (i.e. when the current screen is recorded).

To sum up: The biggest difference between plan 2 and Plan 1 lies in the diffusion of the topic. The core idea of Plan 2 is to send the topic to the audience APP quickly through THE IM channel, and then cache the topic on the audience APP and display the topic after the player notifies the expected NTP time stamp.

Solution 3: small program solution

Both plan 1 and Plan 2 can achieve the perfect synchronization of “sound-speaking-topic”. However, compared with this small optimization in experience, the diffusion ability of APP is more important. Applets are exactly what give apps the ability to go viral.

Tencent Cloud SDK has been packaged in wechat version by default, and provided externally in the form of < live-Player > tag. If:

  • Use FLV to play the address
  • Set mode to Live
  • Set both min-cache and max-cache to 1
  • The GOP of the push stream is 1

Therefore, the ideal playback delay can be achieved, and the delay error between each audience can be within 1s, although the exact “sound – speech – question” cannot be achieved (when wechat packaged video cloud SDK, online answer mode has not emerged, so it does not support embedded messages in audio and video streams). But it can also achieve an almost app-like experience.

All that remains is to issue questions to the applet via the webSocket channel of the applet, or our WeBIM solution.

Access Guide (Solution 1)

Step 1: Open Tencent cloud live streaming service

Contact us to open Tencent cloud live service, if you are in a hurry, you can call us 400 urgent review customer service.

Step 2: Get the URL of the push stream

For an easy way to get a stream URL, see the documentation: Get An Address Quickly.

If you want to know the relationship between the push stream address and the live broadcast ID, you can refer to the documentation: Background automatic assembly.

If you want to know how to protect your push stream address from theft, you can refer to the document: anti-theft signature.

Step 3: Obtain the playback URL

Play URL and push URL are mapped one by one, and the mapping rules can be understood by referring to the following illustration:

Be sure to use the playing address in FLV format. RTMP is prone to lag in high concurrency scenarios.

Step 4: Configure the push end

If you are using the APP to push flow, directly reference documentation (iOS | Android).

If you are using Obs to push streams, note the following important Settings:

I Frame interval (GOP)

There are two ways to access the general broadcasting station: OBS Studio push stream or coding box push stream, and these two push stream tools have relatively mature Settings interface. It is recommended that the GOP (also called keyframe interval) be set to 1s so that the delay difference on the audience side is very small.

X264’s GOP Settings don’t have a great effect on encoding efficiency, but they do have a great effect on latency: the larger the GOP, the more server cache. Since the SDK’s delayed correction requires a correction time, if the GOP is too large, it will have a great impact on the audience who just entered.

Here is a graphical representation of how Obs Studio sets the keyframe interval:

Encoding parameters

Recommended Configuration

The resolution of the

Video bit rate

Frame rate

Track number

Sampling rate

Audio bit rate

Give priority to quality

540×960

1000kbps

25

1

48k

72kbps

Give priority to the cost

360×640

600kbps

20

1

48k

72kbps

Step 5: Connect to the player

  1. Download the SDK versions listed in Part 2 of the documentation.
  2. To access the document (iOS | Android) complete player access. It will take about 0.5 days to complete both platforms.
  3. Modify the default configuration because the default configuration of the SDK is common live broadcast, you need to modify the configuration as follows:
TXLivePlayConfig *config = [[TXLivePlayConfig alloc] init]; TXLivePlayer *player = [[TXLivePlayer alloc] init]; // // enableMessage acceptance (default: disabled) config.enableMessage = YES; / / / / set delay balance for 1 s (consider to delay the introduction of the cloud and push the flow, the actual delay for more than 2 s, SDK pushed the flow, 2 s, obs push flow: 3-4 seconds) config. BAutoAdjustCacheTime = YES; config.maxAutoAdjustCacheTime = 1; config.minAutoAdjustCacheTime = 1; config.cacheTime = 1; config.connectRetryCount = 3; config.connectRetryInterval = 3; config.enableAEC = NO; / / firstsetThe Config startPlay again [playersetConfig:config]; MTXLivePlayConfig = new TXLivePlayConfig(); mTXLivePlayer = new TXLivePlayer(context); / / / / open the message received, don't get message is didn't open this (default: off) mTXLivePlayConfig setEnableMessage (true); / / / / set delay balance for 1 s (consider to delay the introduction of the cloud and push flow, the actual delay for more than 2 s, SDK pushed the flow, 2 s, obs push flow: 3-4 seconds) mTXLivePlayConfig. SetAutoAdjustCacheTime (true); MTXLivePlayConfig. SetCacheTime (1.0 f); MTXLivePlayConfig. SetMaxAutoAdjustCacheTime (1.0 f); mTXLivePlayConfig.setMinAutoAdjustCacheTime (1.0 f); / / / / firstsetThe Config again startPlay mTXLivePlayer. SetConfig (mTXLivePlayConfig);Copy the code

Be sure to use the playing address in FLV format. RTMP is prone to lag in high concurrency scenarios.Copy the code

Step 6: Diffusion of topics

  • If it is to use the APP to send, you just need to reference TXLivePusher sendMessage calls method, see reference documentation (iOS | Android).
  • If you are Posting questions using our custom Obs Studio, you can use the local INI file to edit the questions in advance and have the moderator spread them out at the appropriate time.

Reliability assessment

Some customers may worry: the audio and video channel itself is not very stable, so if the card or video data is lost, the audience will not see the title.

  • First of all, the frame loss of live audio and video data is determined according to the unit of GOP. If GOP =1, 1s of audio and video data will be lost each time
  • Secondly, according to the current node deployment situation of Tencent cloud, more than 90% of video lag is caused by the insufficient network speed of the audience end, in this case, other network communication is not very smooth.

Therefore, the solution to this problem is to send the topic message once every second (on the premise that the GOP is set to 1s), and the same topic number is repeated on the audience side, so as to avoid the impact of occasional audio and video lag on the reliability of topic arrival.

Step 7: Receive the topic message

In our push-stream APP Demo and custom version of Obs Studio, we organized the topics into a buffer in JSON format and sent it in a stream of audio and video.

Once you have this buffer, you can parse it out and complete the UI presentation. If you need to adjust the JSON format to support more customization, please modify the source code or contact us.

  • Set the enableMessage switch in TXLivePlayConfig to YES.
  • TXLivePlayer listens for messages via TXLivePlayListener, PLAY_EVT_GET_MESSAGE (2012)
-(void) onPlayEvent:(int)EvtID withParam:(NSDictionary *)param {[self asyncRun:^{if(EvtID == PLAY_EVT_GET_MESSAGE) {dispatch_async(dispatch_get_main_queue(), ^if ([_delegate respondsToSelector:@selector(onPlayerMessage:)]) {
                    [_delegate onPlayerMessage:param[@"EVT_GET_MSG"]]. }}); }}]; } / / Android sample code mTXLivePlayer. SetPlayListener (newITXLivePlayListener() {
        @Override
        public void onPlayEvent(int event, Bundle param) {
            if (event == TXLiveConstants.PLAY_ERR_NET_DISCONNECT) {
                roomListenerCallback.onDebugLog("[AnswerRoom] Failed to pull flow: Network Disconnection");
                roomListenerCallback.onError(-1, "Network disconnection, pull flow failed.");
            }
            else if (event == TXLiveConstants.PLAY_EVT_GET_MESSAGE) {
                String msg = null;
                try {
                    msg = new String(param.getByteArray(TXLiveConstants.EVT_GET_MSG), "UTF-8"); roomListenerCallback.onRecvAnswerMsg(msg); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }}}});Copy the code

Step 8: Develop the answer system

Due to the positioning of Tencent Cloud PAAS, we will not involve the question answering and payment system closely bound to the customer’s business, so you are required to participate in the development.

The solution commonly adopted here is to summarize the customer’s answers to the answer server in the form of HTTP(S) request, but the implementation process needs to pay attention to solve the instantaneous high concurrency request pressure.

Some customers may ask whether THE IM system is suitable for answering questions. At present, it is not suitable for answering questions, because the main ability of IM system lies in message diffusion, and the main goal of answering questions is information collection.

Step 9: Answer result display

After the general question comes out for a period of time, it will enter the closed state. At this time, the answer system will be statistical summary of the results, and the summary results will be sent to the audience.

If we use our customized version of Obs Studio to diffuse the results, build a simple server and provide an HTTP interface, and then communicate questions, answers and numbers with Obs according to our agreed JSON format. So as to realize the distribution of questions and answers.

Due to the limitation of the length of the document, this part of the agreement is not listed in detail in the document for the time being. If you need it, please contact us by phone at 400.

Access Guide (Solution 2)

Step 1: Open Tencent cloud live streaming service

The same as plan 1, no further details.

Step 2: Obtain the URL of the push stream & add the NTP timestamp

Refer to scheme 1. Different from Scheme 1, push stream URL requires an additional parameter:

The NTP time stamp is added

Add the parameter &txaddTimestamp =1 after the push stream URL, and the server will inject an SEI timestamp with international standard time (within 100ms) into your live stream every 1s. If you use our player to play this video stream, you will receive a message notification every second representing the current screen NTP time.

Step 3: Obtain the playback URL

The same as plan 1, no further details.

Step 4: Configure the push end

The same as plan 1, no further details.

Step 5: Connect to the player

The difference is that instead of retrieving a JSON message, you get an 8-byte 64-bit timestamp.

long timeStamp = byteArrayToInt(param.getByteArray(TXLiveConstants.EVT_GET_MSG)); Public static long byteArrayToInt(byte[] byteArray) {byte[] a = new byte[8]; / / Public static Long byteArrayToInt(byte[] byteArray) {byte[] a = new byte[8]; int i = a.length - 1, j = byteArray.length - 1;for(; i >= 0; I --, j--) {// Copy data from the end of bif (j >= 0)
                a[i] = byteArray[j];
            elsea[i] = 0; // If b.length is less than 4, insert 0} // If b.length is less than 4, insert 0} // If b.length is less than 4, insert 0} // If b.length is less than 4, insert 0} // If b.length is less than 4, insert 0} // If b.length is less than 4, insert 0} // If b.length is less than 4, insert 0} // If b.length is less than 4, insert 0} // If b.length is less than 4, insert 0} // If b.length is less than 4, insert 0}. Note that an undeclared number will be treated as an int. long v0 = (long) (a[0] & 0xff) << 56; Long v1 = (long) (a[1] &0xff) << 48; long v1 = (long) (a[1] &0xff) << 48; long v2 = (long) (a[2] & 0xff) << 40; long v3 = (long) (a[3] & 0xff) << 32; long v4 = (long) (a[4] & 0xff) << 24; long v5 = (long) (a[5] & 0xff) << 16; long v6 = (long) (a[6] & 0xff) << 8; long v7 = (long) (a[7] & 0xff);return v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7;
}Copy the code

Step 6: Diffusion of topics

If you use your own IM system to send questions, please ignore this part. If you want to use Tencent cloud IM service to send questions, please connect to the following steps:

  • 1. Open IM communication service: open Tencent Yunyun communication service.
  • 2. Configure the IM service: Perform the initial configuration according to the document. Ensure that the integration mode is set to independent mode.
  • 3. Use REST API to create a BChatRoom for questions: Tencent Cloud IM’S REST API is specially provided for server access. The operation of creating a group is usually triggered by your server, so it is suitable to use REST API solution for access. BChatRoom is very suitable for Posting questions, because it is originally used for system notification, so the message reach rate is high and the message reliability is good. To create a group, run v4/ group_open_HTTP_svc /create_group. For the test method, see using IM Background RESTAPI in the SDK development package. PDF – Step 3.
  • 4. Use the REST API to create an AVChatRoom for barrage messages: The AVChatRoom type is ideal for barrage messages in chat rooms, with strict filter-word filtering and frequent-limiting logic, optimized for large chat room scenarios. To create a group, run v4/ group_open_HTTP_svc /create_group. For the test method, see using IM Background RESTAPI in the SDK development package. PDF – Step 4. The default frequency control in AVChatroom is 40 /s, please contact us if you need to adjust the limit, as the more messages, the higher the bandwidth charge.
  • 5. Use REST API to broadcast questions in BChatRoom: To send messages, use V4 / group_open_HTTP_svc /send_group_msg. For the test method, see the USAGE method of IM Background RESTAPI in the SDK development package. PDF – Step 5.
  • 6. How to obtain the online population in real time? : REST API does not support client call, but even background call, Tencent cloud REST API background call frequency limit is only 100 times/second, so to obtain real-time online number, you need to obtain the number of online users through the REST API low frequency, and then through the message channel, The number of online users is delivered to the client. 1) Get the online number of the group by your business server through V4 / group_open_HTTP_svc /get_group_info.

2) After that, your business server periodically sends the number of people to the client through v4/ group_open_HTTP_svc /send_group_msg. The timing frequency can be set to 3-5s once.

Special note: THE REST API does not support client calls, which can lead to private key leakage, which can lead to your cloud services being misused and charged.

Step 7: Subject receiving & Projectile sending and receiving

The client uses the IM SDK to receive messages and send and receive bullet-screen messages. You can perform the following steps to connect to the client

  • 1. IMSDK Lite: The IMSDK is located in the SDK development package. Access Guide -IMSDK. PDF in the ZIP package provides detailed description.
  • 2. Refer to the source code to complete the connection: there is a source file called AnswerPlayIMCenter in the SDK that encapsulates a simple call to the IMSDK. For example, you can refer to this class’s member functions as follows:

A member function

role

initIMCenter

For initialization, you need to fill in your IM service information in Tencent Cloud.

loginIMUser

For login, you can think of imSDK as a no-interface QQ. You must log in by sending and receiving messages with QQ, but change the QQ number and login password to your Userid and UserSig issued by your server.

joinIMGroup

Use to join the BChatRoom and AVChatRoom created by your backend server through the REST API in Step 6.

sendChatMessage

Used to send barrage comment messages

onRecvChatMessage

It is used to receive barrage messages from AVChatRoom, pay attention to the frequency limit of rendering, do not brush the screen once you receive a barrage message, the performance of the phone may not be able to bear, many customers are negligent here, because there are not many messages not easy to find during the test.

onRecvIssueMessage

Used to receive topic messages from BChatRoom, according to the design of plan 2, each topic has the NTP international time it should display.

  • 3. Topic display time: onRecvIssueMessage received the topic do not immediately display, to wait for the GET_MESSAGE callback from the player, if the callback time >= topic NTP international time, you can display the specified topic.
  • 4. How to calculate UserSig? : UserSig is an important information required by loginIMUser. It is equivalent to the password used for logging in to QQ, but this password is issued by your server for your users. The password signing scheme used here is based on RSA asymmetric encryption, so the security is very high. UserSig is usually issued by your server, the issuing scheme can refer to: TLS background API, but in order to enable you to debug quickly, we also provide a Windows signing small tool, you can debug the terminal logic before the background students join the project. Note: the public and private keys in the gadget are for testing purposes only, so don’t use them seriously.

Step 8: Develop the answer system

Due to the positioning of Tencent Cloud PAAS, we will not involve the question answering and payment system closely bound to the customer’s business, so you are required to participate in the development.

The solution commonly adopted here is to summarize the customer’s answers to the answer server in the form of HTTP(S) request, but the implementation process needs to pay attention to solve the instantaneous high concurrency request pressure.

Some customers may ask whether THE IM system is suitable for answering questions. At present, it is not suitable for answering questions, because the main ability of IM system lies in message diffusion, and the main goal of answering questions is information collection.

Step 9: Answer result display

After the general question comes out for a period of time, it will enter the closed state. At this time, the answer system will make a statistical summary of the results and send the summary results to the audience, and the result distribution can continue to use the question diffusion channel in Step 6.

reading

Webrtc point-to-point live streaming

Tencent cloud live access instructions

Cloud construction of live broadcasting services

This article has been authorized by the author yunjia community published, reproduced please indicate the source of the original