react-native-alpha-video 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (283) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +903 -2
  3. package/THIRD_PARTY_NOTICES.md +30 -0
  4. package/android/av1/build.gradle +67 -0
  5. package/android/av1/consumer-rules.pro +4 -0
  6. package/android/av1/src/main/cpp/Av1Codec_jni.cpp +8 -0
  7. package/android/av1/src/main/cpp/CMakeLists.txt +34 -0
  8. package/android/av1/src/main/java/dev/mediacache/alphavideo/av1/Av1Codec.java +21 -0
  9. package/android/build.gradle +85 -0
  10. package/android/consumer-rules.pro +2 -0
  11. package/android/gradle.properties +1 -0
  12. package/android/publish.gradle +82 -0
  13. package/android/settings.gradle +5 -0
  14. package/android/src/main/AndroidManifest.xml +1 -0
  15. package/android/src/main/cpp/CMakeLists.txt +59 -0
  16. package/android/src/main/java/dev/mediacache/alphavideo/AlphaVideoCodecs.java +73 -0
  17. package/android/src/main/java/dev/mediacache/alphavideo/AlphaVideoView.java +1966 -0
  18. package/android/src/main/java/dev/mediacache/alphavideo/BufferedDualStreamPlayer.java +978 -0
  19. package/android/src/main/java/dev/mediacache/alphavideo/Fetcher.java +37 -0
  20. package/android/src/main/java/dev/mediacache/alphavideo/HttpFetcher.java +156 -0
  21. package/android/src/main/java/dev/mediacache/alphavideo/MediaCodecColor.java +47 -0
  22. package/android/src/main/java/dev/mediacache/alphavideo/MediaCodecDecoders.java +135 -0
  23. package/android/src/main/java/dev/mediacache/alphavideo/RangeSourceLink.java +54 -0
  24. package/android/src/main/java/dev/mediacache/alphavideo/SourceReader.java +174 -0
  25. package/android/src/main/java/dev/mediacache/alphavideo/SurfaceTextureVideoDecoder.java +802 -0
  26. package/android/src/main/java/dev/mediacache/alphavideo/WebMAlphaPlayerNative.java +95 -0
  27. package/android/src/main/java/dev/mediacache/alphavideo/WebMFrame.java +54 -0
  28. package/android/src/main/java/dev/mediacache/alphavideo/WebMSource.java +160 -0
  29. package/android/src/rangesource/java/dev/mediacache/alphavideo/AppDataSource.java +50 -0
  30. package/android/vpx/build.gradle +67 -0
  31. package/android/vpx/consumer-rules.pro +4 -0
  32. package/android/vpx/src/main/cpp/CMakeLists.txt +34 -0
  33. package/android/vpx/src/main/cpp/VpxCodec_jni.cpp +8 -0
  34. package/android/vpx/src/main/java/dev/mediacache/alphavideo/vpx/VpxCodec.java +21 -0
  35. package/codecs/av1/Dav1dDecoderPlugin.cpp +157 -0
  36. package/codecs/av1/Dav1dDecoderPlugin.h +17 -0
  37. package/codecs/vpx/VpxDecoderPlugin.cpp +103 -0
  38. package/codecs/vpx/VpxDecoderPlugin.h +17 -0
  39. package/cpp/AlphaDemuxer.cpp +984 -0
  40. package/cpp/AlphaDemuxer.h +123 -0
  41. package/cpp/AlphaVideoDecoderPlugin.h +84 -0
  42. package/cpp/Av1SequenceHeader.cpp +175 -0
  43. package/cpp/Av1SequenceHeader.h +25 -0
  44. package/cpp/ByteSource.cpp +116 -0
  45. package/cpp/ByteSource.h +59 -0
  46. package/cpp/RangeSourceClient.h +40 -0
  47. package/cpp/VideoDecoder.cpp +157 -0
  48. package/cpp/VideoDecoder.h +83 -0
  49. package/cpp/WebMAlphaPlayer.cpp +1666 -0
  50. package/cpp/WebMAlphaPlayer.h +389 -0
  51. package/cpp/WebMAlphaPlayer_jni.cpp +1380 -0
  52. package/cpp/YuvToRgb.cpp +39 -0
  53. package/cpp/YuvToRgb.h +40 -0
  54. package/ios/Sources/AlphaVideo/AlphaVideoCodecs.mm +63 -0
  55. package/ios/Sources/AlphaVideo/AlphaVideoInternal.h +70 -0
  56. package/ios/Sources/AlphaVideo/AlphaVideoPlayer.mm +994 -0
  57. package/ios/Sources/AlphaVideo/AlphaVideoSource.mm +476 -0
  58. package/ios/Sources/AlphaVideo/AlphaVideoView.mm +374 -0
  59. package/ios/Sources/AlphaVideo/include/AlphaVideo.h +303 -0
  60. package/ios/Sources/AlphaVideoAV1/AlphaVideoAV1Codec.mm +16 -0
  61. package/ios/Sources/AlphaVideoAV1/include/AlphaVideoAV1.h +18 -0
  62. package/ios/Sources/AlphaVideoVPX/AlphaVideoVPXCodec.mm +16 -0
  63. package/ios/Sources/AlphaVideoVPX/include/AlphaVideoVPX.h +17 -0
  64. package/lib/commonjs/AlphaVideoView.js +187 -0
  65. package/lib/commonjs/AlphaVideoView.js.map +1 -0
  66. package/lib/commonjs/AlphaVideoViewNativeComponent.js +47 -0
  67. package/lib/commonjs/AlphaVideoViewNativeComponent.js.map +1 -0
  68. package/lib/commonjs/AlphaVideoViewNativeComponent.web.js +11 -0
  69. package/lib/commonjs/AlphaVideoViewNativeComponent.web.js.map +1 -0
  70. package/lib/commonjs/NativeAlphaVideoModule.js +10 -0
  71. package/lib/commonjs/NativeAlphaVideoModule.js.map +1 -0
  72. package/lib/commonjs/NativeAlphaVideoModule.web.js +9 -0
  73. package/lib/commonjs/NativeAlphaVideoModule.web.js.map +1 -0
  74. package/lib/commonjs/WebPlayer.js +12 -0
  75. package/lib/commonjs/WebPlayer.js.map +1 -0
  76. package/lib/commonjs/WebPlayer.web.js +92 -0
  77. package/lib/commonjs/WebPlayer.web.js.map +1 -0
  78. package/lib/commonjs/index.js +124 -0
  79. package/lib/commonjs/index.js.map +1 -0
  80. package/lib/module/AlphaVideoView.js +187 -0
  81. package/lib/module/AlphaVideoView.js.map +1 -0
  82. package/lib/module/AlphaVideoViewNativeComponent.js +47 -0
  83. package/lib/module/AlphaVideoViewNativeComponent.js.map +1 -0
  84. package/lib/module/AlphaVideoViewNativeComponent.web.js +11 -0
  85. package/lib/module/AlphaVideoViewNativeComponent.web.js.map +1 -0
  86. package/lib/module/NativeAlphaVideoModule.js +10 -0
  87. package/lib/module/NativeAlphaVideoModule.js.map +1 -0
  88. package/lib/module/NativeAlphaVideoModule.web.js +9 -0
  89. package/lib/module/NativeAlphaVideoModule.web.js.map +1 -0
  90. package/lib/module/WebPlayer.js +12 -0
  91. package/lib/module/WebPlayer.js.map +1 -0
  92. package/lib/module/WebPlayer.web.js +92 -0
  93. package/lib/module/WebPlayer.web.js.map +1 -0
  94. package/lib/module/index.js +124 -0
  95. package/lib/module/index.js.map +1 -0
  96. package/lib/typescript/react-native/src/AlphaVideoView.d.ts +189 -0
  97. package/lib/typescript/react-native/src/AlphaVideoView.d.ts.map +1 -0
  98. package/lib/typescript/react-native/src/AlphaVideoViewNativeComponent.d.ts +109 -0
  99. package/lib/typescript/react-native/src/AlphaVideoViewNativeComponent.d.ts.map +1 -0
  100. package/lib/typescript/react-native/src/AlphaVideoViewNativeComponent.web.d.ts +5 -0
  101. package/lib/typescript/react-native/src/AlphaVideoViewNativeComponent.web.d.ts.map +1 -0
  102. package/lib/typescript/react-native/src/NativeAlphaVideoModule.d.ts +21 -0
  103. package/lib/typescript/react-native/src/NativeAlphaVideoModule.d.ts.map +1 -0
  104. package/lib/typescript/react-native/src/NativeAlphaVideoModule.web.d.ts +3 -0
  105. package/lib/typescript/react-native/src/NativeAlphaVideoModule.web.d.ts.map +1 -0
  106. package/lib/typescript/react-native/src/WebPlayer.d.ts +3 -0
  107. package/lib/typescript/react-native/src/WebPlayer.d.ts.map +1 -0
  108. package/lib/typescript/react-native/src/WebPlayer.web.d.ts +9 -0
  109. package/lib/typescript/react-native/src/WebPlayer.web.d.ts.map +1 -0
  110. package/lib/typescript/react-native/src/index.d.ts +77 -0
  111. package/lib/typescript/react-native/src/index.d.ts.map +1 -0
  112. package/licenses/dav1d-COPYING.txt +23 -0
  113. package/licenses/libvpx-LICENSE.txt +31 -0
  114. package/licenses/libvpx-PATENTS.txt +23 -0
  115. package/licenses/libwebm-LICENSE.txt +30 -0
  116. package/licenses/libwebm-PATENTS.txt +23 -0
  117. package/package.json +150 -4
  118. package/prebuilt/android/arm64-v8a/lib/libdav1d.a +0 -0
  119. package/prebuilt/android/arm64-v8a/lib/libvpx.a +0 -0
  120. package/prebuilt/android/armeabi-v7a/lib/libdav1d.a +0 -0
  121. package/prebuilt/android/armeabi-v7a/lib/libvpx.a +0 -0
  122. package/prebuilt/android/x86_64/lib/libdav1d.a +0 -0
  123. package/prebuilt/android/x86_64/lib/libvpx.a +0 -0
  124. package/prebuilt/include/dav1d/common.h +94 -0
  125. package/prebuilt/include/dav1d/data.h +117 -0
  126. package/prebuilt/include/dav1d/dav1d.h +329 -0
  127. package/prebuilt/include/dav1d/headers.h +440 -0
  128. package/prebuilt/include/dav1d/picture.h +157 -0
  129. package/prebuilt/include/dav1d/version.h +50 -0
  130. package/prebuilt/include/vpx/vp8.h +136 -0
  131. package/prebuilt/include/vpx/vp8dx.h +228 -0
  132. package/prebuilt/include/vpx/vpx_codec.h +475 -0
  133. package/prebuilt/include/vpx/vpx_decoder.h +365 -0
  134. package/prebuilt/include/vpx/vpx_frame_buffer.h +83 -0
  135. package/prebuilt/include/vpx/vpx_image.h +209 -0
  136. package/prebuilt/include/vpx/vpx_integer.h +40 -0
  137. package/prebuilt/ios/lib/libdav1d.a +0 -0
  138. package/prebuilt/ios/lib/libvpx.a +0 -0
  139. package/prebuilt/ios-simulator/include/dav1d/common.h +94 -0
  140. package/prebuilt/ios-simulator/include/dav1d/data.h +117 -0
  141. package/prebuilt/ios-simulator/include/dav1d/dav1d.h +329 -0
  142. package/prebuilt/ios-simulator/include/dav1d/headers.h +440 -0
  143. package/prebuilt/ios-simulator/include/dav1d/picture.h +157 -0
  144. package/prebuilt/ios-simulator/include/dav1d/version.h +50 -0
  145. package/prebuilt/ios-simulator/include/vpx/vp8.h +136 -0
  146. package/prebuilt/ios-simulator/include/vpx/vp8dx.h +228 -0
  147. package/prebuilt/ios-simulator/include/vpx/vpx_codec.h +475 -0
  148. package/prebuilt/ios-simulator/include/vpx/vpx_decoder.h +365 -0
  149. package/prebuilt/ios-simulator/include/vpx/vpx_frame_buffer.h +83 -0
  150. package/prebuilt/ios-simulator/include/vpx/vpx_image.h +209 -0
  151. package/prebuilt/ios-simulator/include/vpx/vpx_integer.h +40 -0
  152. package/prebuilt/ios-simulator/lib/libdav1d.a +0 -0
  153. package/prebuilt/ios-simulator/lib/libvpx.a +0 -0
  154. package/react-native/android/CMakeLists.txt +14 -0
  155. package/react-native/android/build.gradle +110 -0
  156. package/react-native/android/src/main/AndroidManifest.xml +1 -0
  157. package/react-native/android/src/main/java/dev/mediacache/alphavideo/reactnative/AlphaVideoModule.java +102 -0
  158. package/react-native/android/src/main/java/dev/mediacache/alphavideo/reactnative/AlphaVideoPackage.java +50 -0
  159. package/react-native/android/src/main/java/dev/mediacache/alphavideo/reactnative/AlphaVideoViewManager.java +317 -0
  160. package/react-native/android/src/newarch/java/dev/mediacache/alphavideo/reactnative/AlphaVideoModuleSpec.java +10 -0
  161. package/react-native/android/src/newarch/java/dev/mediacache/alphavideo/reactnative/AlphaVideoViewManagerSpec.java +23 -0
  162. package/react-native/android/src/norangesource/java/dev/mediacache/alphavideo/AppDataSource.java +15 -0
  163. package/react-native/android/src/oldarch/java/dev/mediacache/alphavideo/reactnative/AlphaVideoModuleSpec.java +30 -0
  164. package/react-native/android/src/oldarch/java/dev/mediacache/alphavideo/reactnative/AlphaVideoViewManagerSpec.java +61 -0
  165. package/react-native/ios/RNAlphaVideoViewComponentView.mm +173 -0
  166. package/react-native/ios/RNAlphaVideoViewManager.mm +147 -0
  167. package/react-native/src/AlphaVideoView.tsx +329 -0
  168. package/react-native/src/AlphaVideoViewNativeComponent.ts +132 -0
  169. package/react-native/src/AlphaVideoViewNativeComponent.web.ts +5 -0
  170. package/react-native/src/NativeAlphaVideoModule.ts +22 -0
  171. package/react-native/src/NativeAlphaVideoModule.web.ts +2 -0
  172. package/react-native/src/WebPlayer.tsx +7 -0
  173. package/react-native/src/WebPlayer.web.tsx +60 -0
  174. package/react-native/src/index.tsx +144 -0
  175. package/react-native-alpha-video.podspec +99 -0
  176. package/react-native.config.js +12 -0
  177. package/scripts/build_codecs_android.sh +246 -0
  178. package/scripts/build_codecs_ios.sh +31 -0
  179. package/scripts/build_codecs_ios_device.sh +210 -0
  180. package/scripts/build_codecs_ios_simulator.sh +256 -0
  181. package/scripts/build_wasm.sh +230 -0
  182. package/scripts/build_xcframework.sh +49 -0
  183. package/scripts/fetch-prebuilt.sh +48 -0
  184. package/scripts/package_release.sh +107 -0
  185. package/scripts/publish-android.sh +85 -0
  186. package/scripts/vendor_range_source.sh +47 -0
  187. package/third_party/libwebm/AUTHORS.TXT +5 -0
  188. package/third_party/libwebm/LICENSE.TXT +30 -0
  189. package/third_party/libwebm/PATENTS.TXT +23 -0
  190. package/third_party/libwebm/README.md +7 -0
  191. package/third_party/libwebm/webm_parser/include/webm/buffer_reader.h +138 -0
  192. package/third_party/libwebm/webm_parser/include/webm/callback.h +363 -0
  193. package/third_party/libwebm/webm_parser/include/webm/dom_types.h +1781 -0
  194. package/third_party/libwebm/webm_parser/include/webm/element.h +208 -0
  195. package/third_party/libwebm/webm_parser/include/webm/file_reader.h +103 -0
  196. package/third_party/libwebm/webm_parser/include/webm/id.h +1085 -0
  197. package/third_party/libwebm/webm_parser/include/webm/istream_reader.h +99 -0
  198. package/third_party/libwebm/webm_parser/include/webm/reader.h +94 -0
  199. package/third_party/libwebm/webm_parser/include/webm/status.h +166 -0
  200. package/third_party/libwebm/webm_parser/include/webm/webm_parser.h +133 -0
  201. package/third_party/libwebm/webm_parser/src/ancestory.cc +334 -0
  202. package/third_party/libwebm/webm_parser/src/ancestory.h +83 -0
  203. package/third_party/libwebm/webm_parser/src/audio_parser.h +84 -0
  204. package/third_party/libwebm/webm_parser/src/bit_utils.cc +26 -0
  205. package/third_party/libwebm/webm_parser/src/bit_utils.h +24 -0
  206. package/third_party/libwebm/webm_parser/src/block_additions_parser.h +30 -0
  207. package/third_party/libwebm/webm_parser/src/block_group_parser.h +71 -0
  208. package/third_party/libwebm/webm_parser/src/block_header_parser.cc +76 -0
  209. package/third_party/libwebm/webm_parser/src/block_header_parser.h +65 -0
  210. package/third_party/libwebm/webm_parser/src/block_more_parser.h +32 -0
  211. package/third_party/libwebm/webm_parser/src/block_parser.cc +285 -0
  212. package/third_party/libwebm/webm_parser/src/block_parser.h +126 -0
  213. package/third_party/libwebm/webm_parser/src/bool_parser.h +95 -0
  214. package/third_party/libwebm/webm_parser/src/buffer_reader.cc +110 -0
  215. package/third_party/libwebm/webm_parser/src/byte_parser.h +144 -0
  216. package/third_party/libwebm/webm_parser/src/callback.cc +155 -0
  217. package/third_party/libwebm/webm_parser/src/chapter_atom_parser.h +42 -0
  218. package/third_party/libwebm/webm_parser/src/chapter_display_parser.h +34 -0
  219. package/third_party/libwebm/webm_parser/src/chapters_parser.h +28 -0
  220. package/third_party/libwebm/webm_parser/src/cluster_parser.h +48 -0
  221. package/third_party/libwebm/webm_parser/src/colour_parser.h +55 -0
  222. package/third_party/libwebm/webm_parser/src/content_enc_aes_settings_parser.h +32 -0
  223. package/third_party/libwebm/webm_parser/src/content_encoding_parser.h +38 -0
  224. package/third_party/libwebm/webm_parser/src/content_encodings_parser.h +30 -0
  225. package/third_party/libwebm/webm_parser/src/content_encryption_parser.h +37 -0
  226. package/third_party/libwebm/webm_parser/src/cue_point_parser.h +38 -0
  227. package/third_party/libwebm/webm_parser/src/cue_track_positions_parser.h +39 -0
  228. package/third_party/libwebm/webm_parser/src/cues_parser.h +27 -0
  229. package/third_party/libwebm/webm_parser/src/date_parser.cc +72 -0
  230. package/third_party/libwebm/webm_parser/src/date_parser.h +64 -0
  231. package/third_party/libwebm/webm_parser/src/ebml_parser.h +48 -0
  232. package/third_party/libwebm/webm_parser/src/edition_entry_parser.h +35 -0
  233. package/third_party/libwebm/webm_parser/src/element_parser.h +102 -0
  234. package/third_party/libwebm/webm_parser/src/file_reader.cc +142 -0
  235. package/third_party/libwebm/webm_parser/src/float_parser.cc +77 -0
  236. package/third_party/libwebm/webm_parser/src/float_parser.h +65 -0
  237. package/third_party/libwebm/webm_parser/src/id_element_parser.cc +49 -0
  238. package/third_party/libwebm/webm_parser/src/id_element_parser.h +56 -0
  239. package/third_party/libwebm/webm_parser/src/id_parser.cc +75 -0
  240. package/third_party/libwebm/webm_parser/src/id_parser.h +45 -0
  241. package/third_party/libwebm/webm_parser/src/info_parser.h +44 -0
  242. package/third_party/libwebm/webm_parser/src/int_parser.h +121 -0
  243. package/third_party/libwebm/webm_parser/src/istream_reader.cc +133 -0
  244. package/third_party/libwebm/webm_parser/src/master_parser.cc +298 -0
  245. package/third_party/libwebm/webm_parser/src/master_parser.h +230 -0
  246. package/third_party/libwebm/webm_parser/src/master_value_parser.h +533 -0
  247. package/third_party/libwebm/webm_parser/src/mastering_metadata_parser.h +57 -0
  248. package/third_party/libwebm/webm_parser/src/parser.h +34 -0
  249. package/third_party/libwebm/webm_parser/src/parser_utils.cc +34 -0
  250. package/third_party/libwebm/webm_parser/src/parser_utils.h +64 -0
  251. package/third_party/libwebm/webm_parser/src/projection_parser.h +40 -0
  252. package/third_party/libwebm/webm_parser/src/recursive_parser.h +93 -0
  253. package/third_party/libwebm/webm_parser/src/seek_head_parser.h +27 -0
  254. package/third_party/libwebm/webm_parser/src/seek_parser.h +37 -0
  255. package/third_party/libwebm/webm_parser/src/segment_parser.cc +86 -0
  256. package/third_party/libwebm/webm_parser/src/segment_parser.h +53 -0
  257. package/third_party/libwebm/webm_parser/src/simple_tag_parser.h +37 -0
  258. package/third_party/libwebm/webm_parser/src/size_parser.cc +56 -0
  259. package/third_party/libwebm/webm_parser/src/size_parser.h +43 -0
  260. package/third_party/libwebm/webm_parser/src/skip_callback.h +63 -0
  261. package/third_party/libwebm/webm_parser/src/skip_parser.cc +59 -0
  262. package/third_party/libwebm/webm_parser/src/skip_parser.h +35 -0
  263. package/third_party/libwebm/webm_parser/src/slices_parser.h +30 -0
  264. package/third_party/libwebm/webm_parser/src/tag_parser.h +37 -0
  265. package/third_party/libwebm/webm_parser/src/tags_parser.h +27 -0
  266. package/third_party/libwebm/webm_parser/src/targets_parser.h +35 -0
  267. package/third_party/libwebm/webm_parser/src/time_slice_parser.h +30 -0
  268. package/third_party/libwebm/webm_parser/src/track_entry_parser.h +65 -0
  269. package/third_party/libwebm/webm_parser/src/tracks_parser.h +27 -0
  270. package/third_party/libwebm/webm_parser/src/unknown_parser.cc +49 -0
  271. package/third_party/libwebm/webm_parser/src/unknown_parser.h +38 -0
  272. package/third_party/libwebm/webm_parser/src/var_int_parser.cc +71 -0
  273. package/third_party/libwebm/webm_parser/src/var_int_parser.h +56 -0
  274. package/third_party/libwebm/webm_parser/src/video_parser.h +122 -0
  275. package/third_party/libwebm/webm_parser/src/virtual_block_parser.cc +72 -0
  276. package/third_party/libwebm/webm_parser/src/virtual_block_parser.h +69 -0
  277. package/third_party/libwebm/webm_parser/src/void_parser.cc +49 -0
  278. package/third_party/libwebm/webm_parser/src/void_parser.h +41 -0
  279. package/third_party/libwebm/webm_parser/src/webm_parser.cc +282 -0
  280. package/third_party/range-source/LICENSE +21 -0
  281. package/third_party/range-source/README.md +8 -0
  282. package/third_party/range-source/include/range_source.h +395 -0
  283. package/third_party/range-source/include/range_source_download.h +309 -0
@@ -0,0 +1,389 @@
1
+ /**
2
+ * WebMAlphaPlayer.h
3
+ * High-performance VP9+Alpha WebM player: libwebm's parser, libvpx and dav1d
4
+ * Fully self-contained C++ implementation for iOS and Android
5
+ *
6
+ * This is a complete native implementation - no delegation to Java/Swift layers.
7
+ * The decoders are linked directly as static libraries.
8
+ */
9
+
10
+ #ifndef WEBM_ALPHA_PLAYER_H
11
+ #define WEBM_ALPHA_PLAYER_H
12
+
13
+ #include <string>
14
+ #include <functional>
15
+ #include <memory>
16
+ #include <vector>
17
+ #include <mutex>
18
+ #include <atomic>
19
+ #include <thread>
20
+ #include <queue>
21
+ #include <condition_variable>
22
+ #include <unordered_map>
23
+ #include <list>
24
+ #include <chrono>
25
+
26
+ #include "RangeSourceClient.h"
27
+ #include "YuvToRgb.h"
28
+
29
+ namespace webm {
30
+
31
+ class AlphaDemuxer;
32
+ class ByteSource;
33
+ class VideoDecoder;
34
+
35
+ /**
36
+ * Represents a decoded YUVA frame with zero-copy plane access
37
+ */
38
+ struct YUVAFrame {
39
+ // Plane data pointers (owned by this struct)
40
+ uint8_t* yPlane;
41
+ uint8_t* uPlane;
42
+ uint8_t* vPlane;
43
+ uint8_t* aPlane;
44
+
45
+ // Plane sizes
46
+ int ySize;
47
+ int uSize;
48
+ int vSize;
49
+ int aSize;
50
+
51
+ // Frame dimensions
52
+ int width;
53
+ int height;
54
+
55
+ // Timing
56
+ int64_t pts; // Presentation timestamp, in microseconds
57
+ double timeBase; // Time base for pts conversion (1e-6)
58
+ int frameIndex; // Frame number in sequence
59
+
60
+ // Strides (bytes per row - may differ from width due to alignment)
61
+ int yStride;
62
+ int uvStride;
63
+ int aStride;
64
+
65
+ YUVAFrame();
66
+ ~YUVAFrame();
67
+
68
+ // Move semantics for efficient frame passing
69
+ YUVAFrame(YUVAFrame&& other) noexcept;
70
+ YUVAFrame& operator=(YUVAFrame&& other) noexcept;
71
+
72
+ // Delete copy to enforce zero-copy semantics
73
+ YUVAFrame(const YUVAFrame&) = delete;
74
+ YUVAFrame& operator=(const YUVAFrame&) = delete;
75
+
76
+ // Allocate plane memory
77
+ bool allocate(int w, int h);
78
+
79
+ // Get duration in milliseconds
80
+ double getDurationMs() const;
81
+ };
82
+
83
+ /**
84
+ * Callback type for frame delivery
85
+ * Called on decoder thread - implementations should be fast (just queue for GPU upload)
86
+ */
87
+ using FrameCallback = std::function<void(std::shared_ptr<YUVAFrame>)>;
88
+
89
+ /**
90
+ * Callback for playback events
91
+ */
92
+ using EventCallback = std::function<void(const std::string& event, const std::string& data)>;
93
+
94
+ /**
95
+ * LRU Cache for decoded frames
96
+ */
97
+ class FrameCache {
98
+ public:
99
+ explicit FrameCache(size_t maxFrames = 30);
100
+
101
+ void put(const std::string& key, std::shared_ptr<YUVAFrame> frame);
102
+ std::shared_ptr<YUVAFrame> get(const std::string& key);
103
+ void clear();
104
+ size_t size() const;
105
+ size_t maxSize() const;
106
+ bool contains(const std::string& key) const;
107
+ bool isFull() const;
108
+ void evict(const std::string& key); // Remove specific frame from cache
109
+ void setMaxSize(size_t maxFrames); // Dynamically adjust cache size
110
+ // Evict the frame furthest behind playbackPos in circular buffer (modulo totalFrames)
111
+ // Returns true if a frame was evicted
112
+ bool evictFurthestBehind(int playbackPos, int totalFrames, const std::string& keyPrefix);
113
+
114
+ private:
115
+ size_t maxSize_;
116
+ mutable std::mutex mutex_;
117
+ std::unordered_map<std::string, std::shared_ptr<YUVAFrame>> cache_;
118
+ std::list<std::string> lruList_;
119
+ std::unordered_map<std::string, std::list<std::string>::iterator> lruMap_;
120
+
121
+ void evictOldest();
122
+ };
123
+
124
+ /**
125
+ * Decoder statistics snapshot (for returning stats to callers)
126
+ */
127
+ struct DecoderStatsSnapshot {
128
+ int64_t framesDecoded{0};
129
+ int64_t framesCached{0};
130
+ int64_t cacheHits{0};
131
+ int64_t cacheMisses{0};
132
+ int64_t decodeTimeUs{0}; // Total decode time in microseconds
133
+ int64_t peakMemoryBytes{0};
134
+
135
+ double getAverageDecodeTimeMs() const {
136
+ return framesDecoded > 0 ? (decodeTimeUs / 1000.0) / framesDecoded : 0.0;
137
+ }
138
+ double getCacheHitRate() const {
139
+ int64_t total = cacheHits + cacheMisses;
140
+ return total > 0 ? (double)cacheHits / total : 0.0;
141
+ }
142
+ };
143
+
144
+ /**
145
+ * Decoder statistics for performance monitoring (thread-safe internal use)
146
+ */
147
+ struct DecoderStats {
148
+ std::atomic<int64_t> framesDecoded{0};
149
+ std::atomic<int64_t> framesCached{0};
150
+ std::atomic<int64_t> cacheHits{0};
151
+ std::atomic<int64_t> cacheMisses{0};
152
+ std::atomic<int64_t> decodeTimeUs{0}; // Total decode time in microseconds
153
+ std::atomic<int64_t> peakMemoryBytes{0};
154
+
155
+ double getAverageDecodeTimeMs() const;
156
+ double getCacheHitRate() const;
157
+ void reset();
158
+
159
+ // Create a copyable snapshot
160
+ DecoderStatsSnapshot snapshot() const {
161
+ DecoderStatsSnapshot s;
162
+ s.framesDecoded = framesDecoded.load();
163
+ s.framesCached = framesCached.load();
164
+ s.cacheHits = cacheHits.load();
165
+ s.cacheMisses = cacheMisses.load();
166
+ s.decodeTimeUs = decodeTimeUs.load();
167
+ s.peakMemoryBytes = peakMemoryBytes.load();
168
+ return s;
169
+ }
170
+ };
171
+
172
+ /**
173
+ * Playback mode for the player
174
+ */
175
+ enum class PlaybackMode {
176
+ Preload, // Decode all frames upfront before playback starts
177
+ Stream, // Decode on-demand with sliding window cache (frames evicted)
178
+ StreamAndPreload // Start playing immediately, cache all frames for smooth looping
179
+ };
180
+
181
+ /**
182
+ * Configuration for the player
183
+ */
184
+ struct PlayerConfig {
185
+ int decodeThreads = 4; // Threads per decoder (libvpx, dav1d)
186
+ bool useHardwareAccel = false; // Try hardware decode (rarely works for VP9+alpha)
187
+ int cacheSize = 250; // Max frames in cache (should be >= totalFrames when preloading)
188
+ bool preloadAllFrames = true; // Decode all frames upfront (ignored if mode != Preload)
189
+ bool loopPlayback = true; // Loop at end
190
+ double playbackSpeed = 1.0; // Playback speed multiplier
191
+
192
+ // Playback mode:
193
+ // - Preload: decode all frames upfront, then play
194
+ // - Stream (default): decode on-demand with sliding window cache (frames evicted on loop)
195
+ // - StreamAndPreload: start playing immediately, cache all frames for smooth looping
196
+ PlaybackMode mode = PlaybackMode::Stream;
197
+
198
+ // Legacy: kept for backwards compatibility, use mode instead
199
+ bool stream = true;
200
+
201
+ // Frames decoded ahead of the playhead while streaming. Stream mode keeps these and nothing
202
+ // behind the playhead, so it is also the cache size.
203
+ int streamPrefetchAhead = 8;
204
+ };
205
+
206
+ /**
207
+ * Main WebM Alpha Player class
208
+ * Thread-safe, supports concurrent access
209
+ */
210
+ class WebMAlphaPlayer {
211
+ public:
212
+ WebMAlphaPlayer();
213
+ ~WebMAlphaPlayer();
214
+
215
+ // Configuration
216
+ void setConfig(const PlayerConfig& config);
217
+ PlayerConfig getConfig() const;
218
+ // Info logging (lifecycle, decode/playback stats); errors are always logged
219
+ void setVerbose(bool verbose) { verbose_ = verbose; }
220
+
221
+ // Loading
222
+ bool loadFromFile(const std::string& path);
223
+ bool loadFromMemory(const uint8_t* data, size_t size);
224
+ // Reads bytes [offset, offset + length) of fd in place (APK assets, content:// streams).
225
+ // fd is duplicated; the caller keeps ownership. A negative length means "to end of file".
226
+ bool loadFromFileRange(int fd, int64_t offset, int64_t length);
227
+ // A URL or an app's data source, read by range: reads wait for bytes being fetched, so the
228
+ // stream modes start with the first frames. Preload decodes the whole clip before the first
229
+ // frame, so it waits for the whole file. The reader may be shared with a probe.
230
+ bool loadFromReader(std::shared_ptr<SourceReader> reader);
231
+ void unload();
232
+
233
+ // Playback control
234
+ void play();
235
+ void pause();
236
+ void stop();
237
+ // Both clamp to the clip and keep playing or paused; a paused player shows the new frame.
238
+ // In the stream modes the frame appears once it's decoded; read by range, a seek fetches the
239
+ // Cues and the cluster it lands in.
240
+ void seekTo(double timeSeconds); // To the frame showing at timeSeconds
241
+ void seekToFrame(int frameIndex);
242
+
243
+ /**
244
+ * Changes how fast the frames are shown, without moving the playhead.
245
+ *
246
+ * Safe to call at any time, including mid-playback, and that is the point: a UI that swaps
247
+ * speed on hover must not also jump position, or the change reads as a glitch rather than an
248
+ * acceleration. The playback loop only ever advances the frame index by one per tick, so
249
+ * altering the tick interval leaves `currentFrame_` exactly where it was.
250
+ *
251
+ * Clamped away from zero — a rate of 0 is a pause, and expressing it here would divide by
252
+ * zero and stall the loop forever rather than pausing it.
253
+ */
254
+ void setPlaybackSpeed(double speed);
255
+ double getPlaybackSpeed() const;
256
+
257
+ // State queries
258
+ bool isLoaded() const;
259
+ bool isPlaying() const;
260
+ int getCurrentFrame() const;
261
+ double getCurrentTime() const;
262
+ double getDuration() const;
263
+ int getTotalFrames() const;
264
+ int getWidth() const;
265
+ int getHeight() const;
266
+ double getFrameRate() const;
267
+ // The shader's YUV to RGB for the loaded clip (its range and matrix)
268
+ YuvToRgb getYuvToRgb() const;
269
+ // The loaded clip's decoder, "libvpx" (VP9) or "dav1d" (AV1); both, comma-separated, if the
270
+ // colour and alpha tracks differ
271
+ std::string getDecoderName() const;
272
+
273
+ // Callbacks
274
+ void setFrameCallback(FrameCallback callback);
275
+ void setEventCallback(EventCallback callback);
276
+ // The last "error" event's code: kErrorNoDecoder when no registered software decoder plays a
277
+ // track, else empty. Callable from the event callback.
278
+ std::string getLastErrorCode() const;
279
+
280
+ // Direct frame access (for single-frame rendering)
281
+ std::shared_ptr<YUVAFrame> getFrame(int index);
282
+ std::shared_ptr<YUVAFrame> getCachedFrame(int index); // Cache-only, thread-safe
283
+ std::shared_ptr<YUVAFrame> getCurrentFrameData();
284
+
285
+ // Statistics
286
+ DecoderStatsSnapshot getStats() const;
287
+ void resetStats();
288
+
289
+ // The registered decoders' versions, e.g. "libvpx v1.14.0, dav1d 1.5.1"; empty with none
290
+ static std::string getLibraryVersions();
291
+
292
+ static constexpr const char* kErrorNoDecoder = "NO_DECODER";
293
+
294
+ private:
295
+ std::unique_ptr<AlphaDemuxer> demuxer_;
296
+ std::unique_ptr<VideoDecoder> colorDecoder_;
297
+ std::unique_ptr<VideoDecoder> alphaDecoder_; // For an alpha track or BlockAdditional alpha
298
+ bool alphaAsIs_ = false; // BlockAdditional alpha says limited range but is 0-255
299
+
300
+ // Video info
301
+ int width_ = 0;
302
+ int height_ = 0;
303
+ int totalFrames_ = 0;
304
+ double duration_ = 0.0;
305
+ double frameRate_ = 30.0;
306
+ int64_t startUs_ = 0; // The colour stream's first timestamp
307
+ mutable std::mutex yuvToRgbMutex_; // Set on load, read from render threads
308
+ YuvToRgb yuvToRgb_{};
309
+ mutable std::mutex decoderNameMutex_; // Set on load, read from the platform layers
310
+ std::string decoderName_;
311
+
312
+ // State
313
+ std::atomic<bool> loaded_{false};
314
+ std::atomic<bool> playing_{false};
315
+ std::atomic<int> currentFrame_{0};
316
+
317
+ /**
318
+ * The live rate, separate from `config_.playbackSpeed`.
319
+ *
320
+ * `config_` is a plain struct read without a lock all over the playback thread; making one
321
+ * of its fields mutable from the outside would be a data race. This mirrors the config's
322
+ * value at construction and is the only copy the loop consults.
323
+ */
324
+ std::atomic<double> playbackSpeed_{1.0};
325
+ std::atomic<bool> verbose_{false};
326
+
327
+ // Threading
328
+ std::unique_ptr<std::thread> decodeThread_;
329
+ std::unique_ptr<std::thread> playbackThread_;
330
+ std::atomic<bool> playbackLoopRunning_{false};
331
+ std::atomic<bool> preloading_{false}; // decodeAllFrames() is running and holds the decoder
332
+ std::unique_ptr<std::thread> prefetchThread_; // Background prefetch for streaming mode
333
+ std::timed_mutex decodeMutex_; // timed_mutex allows try_lock_for() for non-blocking unload
334
+ std::mutex unloadMutex_; // Protects unload() from concurrent calls
335
+ std::condition_variable decodeCV_;
336
+ std::condition_variable_any prefetchCV_; // Signal prefetch thread
337
+ std::atomic<bool> shouldStop_{false};
338
+ AbortFlag ioAbort_{0}; // Set by unload() so reads waiting on the network return
339
+ std::shared_ptr<SourceReader> reader_; // For loadFromReader: why a read failed
340
+ std::atomic<int> prefetchTarget_{-1}; // Target frame for prefetch thread
341
+ std::atomic<int> streamDecodePos_{0}; // Current sequential decode position for streaming
342
+ std::atomic<bool> firstPassComplete_{false}; // For streamAndPreload: all frames cached after first pass
343
+ std::atomic<int> seekRequest_{-1}; // Stream mode: frame the prefetch thread must move the demuxer to
344
+ std::atomic<int> pendingSeekFrame_{-1}; // Stream modes: sought frame to show when decoded while paused
345
+
346
+ // Caching
347
+ std::unique_ptr<FrameCache> frameCache_;
348
+
349
+ // Callbacks
350
+ FrameCallback frameCallback_;
351
+ EventCallback eventCallback_;
352
+ std::mutex callbackMutex_;
353
+ std::string lastErrorCode_;
354
+ mutable std::mutex lastErrorCodeMutex_; // Not callbackMutex_, which the event callback runs under
355
+
356
+ // Config
357
+ PlayerConfig config_;
358
+ mutable std::mutex configMutex_;
359
+
360
+ // Stats
361
+ DecoderStats stats_;
362
+
363
+ // Private methods
364
+ bool finishLoad(std::unique_ptr<ByteSource> source); // Demuxer, codecs, "loaded", playback mode
365
+ bool openStreams();
366
+ bool initCodecs();
367
+ void closeCodecs();
368
+
369
+ std::shared_ptr<YUVAFrame> decodeFrame(int index);
370
+ int frameIndexForPts(int64_t ptsUs) const;
371
+ void decodeAllFrames();
372
+ void playbackLoop();
373
+ void streamingPrefetch(int currentFrame); // Prefetch frames ahead in streaming mode
374
+ void evictOldFrames(int currentFrame); // Remove frames behind playback position
375
+ void prefetchLoop(); // Background prefetch thread loop
376
+ void requestPrefetch(int currentFrame); // Request prefetch from playback thread (non-blocking)
377
+ void streamingDecodeLoop(); // Sequential decode loop for streaming mode
378
+ std::shared_ptr<YUVAFrame> decodeNextFrame(); // Decode next frame sequentially (no seeking)
379
+
380
+ void emitEvent(const std::string& event, const std::string& data = "");
381
+ void emitError(const std::string& code, const std::string& message);
382
+ void emitFrame(std::shared_ptr<YUVAFrame> frame);
383
+
384
+ std::string makeFrameKey(int index) const;
385
+ };
386
+
387
+ } // namespace webm
388
+
389
+ #endif // WEBM_ALPHA_PLAYER_H