【Event Review】Using Elixir to develop HLS live broadcast backend service

The eighth Elixir Meetup sponsored and organized by Tubi ended successfully last Saturday. Three Elixir senior users, Horvo, Scott and Yang Miao, shared Elixir related applications and ideas with more than 660 online and offline functional programming enthusiasts. Experience. This article reviews the first sharing "Developing HLS Live Backend Service with Elixir". This sharing is given by Horvo, a senior backend engineer at Tubi.

Welcome to pay attention to the official account of Bitu Technology , we will release the sharing review of the other two Elixir Meetups.


The following notes are from audience Liu Lang:

As an online streaming media content provider, Tubi provides users with a wide range of content choices, including on-demand movies and TV shows as well as online live streaming of traditional TV programs. Behind these contents, Tubi chose Elixir to realize the back-end support of these contents. The first share of this Meetup is "Application of Elixir in Tubi Live Backend Service" brought by Tubi senior engineer Horvo.

Why HLS

HTTP Live Streaming (aka, HLS) is a streaming media transmission protocol for transmitting audio and video on the Internet. It was proposed by Apple Inc. in 2009. Since the underlying layer is based on HTTP:

- It caters to the default settings of most firewalls (compared to RTMP requirements on TCP port 1935)

- Can use HTTP key infrastructure, such as global CDN network (even Nginx-rtmp-module supports HLS video playback because it is easier to expand)

- Good platform and device compatibility (HTTP-FLV relies on third-party tools, such as flv.js developed by BiliBili; while MPEG-DASH does not have iOS native support) These features make HLS in comparison with RTMP, MPEG-DASH and other protocols Stand out from the crowd and be selected and used in Tubi's live streaming business.

Your Own Elixir TV

In order to let us better understand the application of Elixir in Tubi's live broadcast business process, Horvo demonstrated how to build Elixir TV from scratch, ready to lead the audience to experience it in the tide of national live broadcast.

The entire Elixir TV system is divided into the following parts:

Transcoder

Accept the original MP4 and other format video files, call the FFmpeg command in the Elixir code (leave the professional work to a professional tool), and generate the multi-bit rate segment corresponding to the video file according to the target bit rate defined in advance. The focus of this step is to define the Data Schema, which will be used when passing information between modules.

Scheduler

Scheduler is the core module of Elixir TV. Unlike the traditional Video on Demand (VOD) platform, which only needs to convert static fixed-length files into static Playlist files and Segment files once and for all, Elixir TV live broadcast stations need to maintain a real-time content queue for each channel.

Scheduler uses GenServer in Exilir for state management, and provides the ability to obtain real-time Playlist files by regularly updating the video segments and corresponding description information in the sliding window, and realizes the functions required for the launch of Elixir TV in a simple, clean, neat and efficient manner .

HLS Server

HLS Server provides three HTTP interfaces for client calls, namely:

- GET /playlist.m3u8 => static master playlist

- GET /:variant/playlist.m3u8 => live variant playlist

- GET /:variant/:video_id/:media_segment_name.ts => serves video files from local file system

The first two interfaces are used to obtain the Playlist file. The client initializes the player based on the information in the Master Playlist, and obtains the address of the Media Segment resource based on the information in the Variant Playlist. Since it is a live broadcast, the client needs to update this information regularly. The third port is for the client to obtain specific video files for playback.

Conclusion

With the above modules, Elixir TV took shape.

Then, Horvo shared with you the real-world large-scale live broadcast product architecture, and pointed out some shortcomings of Elixir TV.

Videos Should be Encrypted

In Da Liu's description, the real universe is a dark forest full of malice, and everyone needs to pretend and silence to protect themselves. As a video platform - Elixir TV also needs to consider protecting its core content, namely video files.

Horvo introduces three different protection strategies:

- Video clip files can be encrypted by AES-128 algorithm to ensure transmission security

- The acquisition interface of the above-mentioned symmetric encryption key needs to authenticate the identity of the visitor to ensure that the video decryption is initiated from a legitimate client

- More advanced content protection, such as DRM and other technologies, can ensure that video files cannot be copied and transferred between different devices.

Monetary Should be Addressed

In Darwin's description, the real world is natural selection and survival of the fittest. Elixir TV also needs to start thinking about monetization from day one. Referring to Tubi’s model of using free video + advertising to build revenue, Elixir TV needs to build Ad Insertion Service as the facade for users to access program lists, and initiate bidding invitations to advertising platforms at the right time, and choose what is more beneficial to the platform The advertising sheet is sold, and such video clips are inserted in the video playback description file returned to the user to earn revenue.

Horvo also introduced the advantages of ad insertion on the server side. Advertisement video files are the same as ordinary video clips. The Ad Insertion Service on the server side is inserted into the video playback Playlist file, so it loses the characteristics of advertisements and can avoid being blocked by some browser ad-blocking plug-ins, thereby protecting the rights and interests of the platform and advertisers .

wonderful interaction

How does the Ad Insertion Service enable personalized ad insertion for each user?

Horvo: The Server Side Ad Insertion (SSAI) service will maintain a separate Media Playlist for each user's Playback Session. The Media Playlist in the live broadcast scene will scroll and refresh over time. When Ad-break information appears in the Playlist, SSAI An advertisement request will be initiated to the Ad Server, and this advertisement request will carry some characteristic information of the user so that the Ad Server can make personalized recommendations for the user. There is also an industry specification (SCTE-35) for ad insertion in HLS streams, and producers of HLS content need to use industry-standard tags to mark the ad position.

Will the Ad (advertisement) be tampered with by the client?

Horvo: The main purpose of our SSAI is to prevent the client from blocking or tampering. The realization of this part is that the SSAI service filters the original Media Playlist with Ad-break information into a Playlist without Ad-break information after completing the advertisement acquisition. The original Media Segment in the Ad-break interval will be replaced by the real Ad Segment. The client cannot monitor the position of the advertisement by detecting the Ad-break Marker, so tampering will not occur.

And if we don't use SSAI service, but do ad request and ad insertion on the client side, the client side can recognize the Ad-break in the M3U8 file. In this case, it is easier for the client side to tamper with or block the ad.

In which part is the M3U8 file generated? What is the meaning of Scheduler?

We will get an HLS output format after Transcoder, this HLS is only an on-demand form for a certain video. The HLS output after the Scheduler arranges is the real content in the form of live broadcast.

Why use Elixir?

For the story of Tubi and Elixir, you can refer to the story shared by Tubi VP of Engineering Chen Tian -  Why use Elixir? Several engineers told the stories behind it .

Based on his five-year development experience using Elixir, Horvo made a brief sharing on Elixir development experience and Elixir's performance in the production environment: "I used to do Ruby on Rails development, Ruby attaches great importance to the developer's development experience, emphasizing Developer Happiness There are many connections between Elixir and Ruby. For example, Jose Valim, the founder of Elixir, was one of the core developers of Ruby. Therefore, Elixir is very similar to Ruby in terms of developer experience and is very friendly.

In addition, in a real production environment, Elixir performs very well when the Web Server needs to process high-concurrency requests.”

Other application cases of Elixir in Tubi

Use Elixir/OTP to build a multimedia E2E processing platform

Application of Ruby ideas in Elixir projects

A 7-year-old performance problem lurking in the Elixir codebase

Join Tubi to grow and become stronger together!

Hot jobs: https://tubi.tech/careers/

WeChat Official Account: [Activity Review] Using Elixir to develop HLS live broadcast backend service

Guess you like

Origin blog.csdn.net/weixin_49193714/article/details/131575841