zhouwg / FFmpeg

This is source code of my customized FFmpeg, used as multimedia engine for project KanTV. moved into project KanTV since 03-21-2024 and this project is no longer maintained accordingly
Other
3 stars 4 forks source link

HLS H264 Sample-AES not support in FFmpeg #1

Closed zhouwg closed 3 years ago

zhouwg commented 3 years ago

Required Info
Host Operating System & Version {Ubuntu 20.04.2}
Kernel Version (Linux Only) (5.x)
Branch { latest 4.4 }
Android Studio Version 4.2.1
NDK Version NDK-r14b
Xcode Version 12.5
Language {C }
Section { HLS }

Issue Description

HLS H264 Sample-AES encrypted content not support in FFmpeg

How to reproduce the issue

  1. open a H264 Sample-AES encrypted HLS content
  2. launch playback

Root cause

lack of H264 Sample-AES supportive in latest FFmpeg4.4

How to fix

the point is here:

new added files:

https://github.com/zhouwg/FFmpeg/blob/release/4.4/libavutil/hlsencryptinfo.h

https://github.com/zhouwg/FFmpeg/blob/release/4.4/libavutil/hlsencryptinfo.c

https://github.com/zhouwg/FFmpeg/blob/release/4.4/libavutil/hlsdecryptor.h

https://github.com/zhouwg/FFmpeg/blob/release/4.4/libavutil/hlsdecryptor.c

modified files:

https://github.com/zhouwg/FFmpeg/blob/release/4.4/libavformat/hls.c

https://github.com/zhouwg/FFmpeg/blob/release/4.4/libavformat/crypto.c

https://github.com/zhouwg/FFmpeg/blob/release/4.4/libavformat/mpegts.c#L3097

https://github.com/zhouwg/FFmpeg/blob/release/4.4/libavcodec/avcodec.h#L3543

https://github.com/zhouwg/FFmpeg/blob/release/4.4/libavformat/utils.c#L1626

https://github.com/zhouwg/FFmpeg/blob/release/4.4/libavcodec/parser.c#L59

https://github.com/zhouwg/FFmpeg/blob/release/4.4/libavcodec/h264_parser.c#L627

Status

HLS H264 SampleAES works fine for the first time in FFmpeg4.4

Defects

  1. only validated on x86-64 & Android, I think it should work fine on iOS device;
  2. not work with DRM(Digital Rights Management) system: this solution wouldn't work fine as expected if the Content Encryption Key couldn't be fetched from remote HTTP server directly

References

HLS_Sample_Encryption

http://www.nrta.gov.cn/art/2019/6/15/art_113_46189.html

https://github.com/google/ExoPlayer/pull/2921

zhouwg commented 3 years ago

short summary about this new feature:

the idea for this new feature is come from add SampleAES AVC/H264 support in ExoPlayer, and with great help from John Chen -- a smart and kind-hearted software engineer

the Pros and Cons of this solution Pros:

  1. re-use the internal framework/mechanism of the original FFmpeg4.4;
  2. extensible for Sample AES encrypted HLS H265 content , Sample AES encrypted HLS China-AVS content,Sample China-SM4-CBC encrypted HLS 264 content, China-SM4-CBC encrypted HLS 264 content,Sample China-SM4-CBC encrypted HLS H265 content, China-SM4-CBC encrypted HLS H265 content , Sample China-SM4-CBC encrypted HLS China-AVS content, China-SM4-CBC encrypted HLS China-AVS content;
  3. the whole data path is already done and validated at the moment, developers and users could implement there proprietary DRM client-subsystem base on data path according to <<GY/T277-2019:视音频内容分发数字版权管理技术规范 or GY/T277-2019:Video and audio content distribution digital rights management technical specifications>>

Cons:

  1. internal infos in libavformat/mpegts.c be accessed by FFmpeg public function, it's not OOP manner;
  2. better software design to reduce potential memory copy between various internal modules in FFmpeg for performance concern --- I tried my best to reduce memory copy in this solution at the moment;
  3. only validated on x86-64 QEMU simulator & aarch64 Android phone at the moment; I think it should work fine on iOS device;