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.
- package/LICENSE +21 -0
- package/README.md +903 -2
- package/THIRD_PARTY_NOTICES.md +30 -0
- package/android/av1/build.gradle +67 -0
- package/android/av1/consumer-rules.pro +4 -0
- package/android/av1/src/main/cpp/Av1Codec_jni.cpp +8 -0
- package/android/av1/src/main/cpp/CMakeLists.txt +34 -0
- package/android/av1/src/main/java/dev/mediacache/alphavideo/av1/Av1Codec.java +21 -0
- package/android/build.gradle +85 -0
- package/android/consumer-rules.pro +2 -0
- package/android/gradle.properties +1 -0
- package/android/publish.gradle +82 -0
- package/android/settings.gradle +5 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/cpp/CMakeLists.txt +59 -0
- package/android/src/main/java/dev/mediacache/alphavideo/AlphaVideoCodecs.java +73 -0
- package/android/src/main/java/dev/mediacache/alphavideo/AlphaVideoView.java +1966 -0
- package/android/src/main/java/dev/mediacache/alphavideo/BufferedDualStreamPlayer.java +978 -0
- package/android/src/main/java/dev/mediacache/alphavideo/Fetcher.java +37 -0
- package/android/src/main/java/dev/mediacache/alphavideo/HttpFetcher.java +156 -0
- package/android/src/main/java/dev/mediacache/alphavideo/MediaCodecColor.java +47 -0
- package/android/src/main/java/dev/mediacache/alphavideo/MediaCodecDecoders.java +135 -0
- package/android/src/main/java/dev/mediacache/alphavideo/RangeSourceLink.java +54 -0
- package/android/src/main/java/dev/mediacache/alphavideo/SourceReader.java +174 -0
- package/android/src/main/java/dev/mediacache/alphavideo/SurfaceTextureVideoDecoder.java +802 -0
- package/android/src/main/java/dev/mediacache/alphavideo/WebMAlphaPlayerNative.java +95 -0
- package/android/src/main/java/dev/mediacache/alphavideo/WebMFrame.java +54 -0
- package/android/src/main/java/dev/mediacache/alphavideo/WebMSource.java +160 -0
- package/android/src/rangesource/java/dev/mediacache/alphavideo/AppDataSource.java +50 -0
- package/android/vpx/build.gradle +67 -0
- package/android/vpx/consumer-rules.pro +4 -0
- package/android/vpx/src/main/cpp/CMakeLists.txt +34 -0
- package/android/vpx/src/main/cpp/VpxCodec_jni.cpp +8 -0
- package/android/vpx/src/main/java/dev/mediacache/alphavideo/vpx/VpxCodec.java +21 -0
- package/codecs/av1/Dav1dDecoderPlugin.cpp +157 -0
- package/codecs/av1/Dav1dDecoderPlugin.h +17 -0
- package/codecs/vpx/VpxDecoderPlugin.cpp +103 -0
- package/codecs/vpx/VpxDecoderPlugin.h +17 -0
- package/cpp/AlphaDemuxer.cpp +984 -0
- package/cpp/AlphaDemuxer.h +123 -0
- package/cpp/AlphaVideoDecoderPlugin.h +84 -0
- package/cpp/Av1SequenceHeader.cpp +175 -0
- package/cpp/Av1SequenceHeader.h +25 -0
- package/cpp/ByteSource.cpp +116 -0
- package/cpp/ByteSource.h +59 -0
- package/cpp/RangeSourceClient.h +40 -0
- package/cpp/VideoDecoder.cpp +157 -0
- package/cpp/VideoDecoder.h +83 -0
- package/cpp/WebMAlphaPlayer.cpp +1666 -0
- package/cpp/WebMAlphaPlayer.h +389 -0
- package/cpp/WebMAlphaPlayer_jni.cpp +1380 -0
- package/cpp/YuvToRgb.cpp +39 -0
- package/cpp/YuvToRgb.h +40 -0
- package/ios/Sources/AlphaVideo/AlphaVideoCodecs.mm +63 -0
- package/ios/Sources/AlphaVideo/AlphaVideoInternal.h +70 -0
- package/ios/Sources/AlphaVideo/AlphaVideoPlayer.mm +994 -0
- package/ios/Sources/AlphaVideo/AlphaVideoSource.mm +476 -0
- package/ios/Sources/AlphaVideo/AlphaVideoView.mm +374 -0
- package/ios/Sources/AlphaVideo/include/AlphaVideo.h +303 -0
- package/ios/Sources/AlphaVideoAV1/AlphaVideoAV1Codec.mm +16 -0
- package/ios/Sources/AlphaVideoAV1/include/AlphaVideoAV1.h +18 -0
- package/ios/Sources/AlphaVideoVPX/AlphaVideoVPXCodec.mm +16 -0
- package/ios/Sources/AlphaVideoVPX/include/AlphaVideoVPX.h +17 -0
- package/lib/commonjs/AlphaVideoView.js +187 -0
- package/lib/commonjs/AlphaVideoView.js.map +1 -0
- package/lib/commonjs/AlphaVideoViewNativeComponent.js +47 -0
- package/lib/commonjs/AlphaVideoViewNativeComponent.js.map +1 -0
- package/lib/commonjs/AlphaVideoViewNativeComponent.web.js +11 -0
- package/lib/commonjs/AlphaVideoViewNativeComponent.web.js.map +1 -0
- package/lib/commonjs/NativeAlphaVideoModule.js +10 -0
- package/lib/commonjs/NativeAlphaVideoModule.js.map +1 -0
- package/lib/commonjs/NativeAlphaVideoModule.web.js +9 -0
- package/lib/commonjs/NativeAlphaVideoModule.web.js.map +1 -0
- package/lib/commonjs/WebPlayer.js +12 -0
- package/lib/commonjs/WebPlayer.js.map +1 -0
- package/lib/commonjs/WebPlayer.web.js +92 -0
- package/lib/commonjs/WebPlayer.web.js.map +1 -0
- package/lib/commonjs/index.js +124 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/module/AlphaVideoView.js +187 -0
- package/lib/module/AlphaVideoView.js.map +1 -0
- package/lib/module/AlphaVideoViewNativeComponent.js +47 -0
- package/lib/module/AlphaVideoViewNativeComponent.js.map +1 -0
- package/lib/module/AlphaVideoViewNativeComponent.web.js +11 -0
- package/lib/module/AlphaVideoViewNativeComponent.web.js.map +1 -0
- package/lib/module/NativeAlphaVideoModule.js +10 -0
- package/lib/module/NativeAlphaVideoModule.js.map +1 -0
- package/lib/module/NativeAlphaVideoModule.web.js +9 -0
- package/lib/module/NativeAlphaVideoModule.web.js.map +1 -0
- package/lib/module/WebPlayer.js +12 -0
- package/lib/module/WebPlayer.js.map +1 -0
- package/lib/module/WebPlayer.web.js +92 -0
- package/lib/module/WebPlayer.web.js.map +1 -0
- package/lib/module/index.js +124 -0
- package/lib/module/index.js.map +1 -0
- package/lib/typescript/react-native/src/AlphaVideoView.d.ts +189 -0
- package/lib/typescript/react-native/src/AlphaVideoView.d.ts.map +1 -0
- package/lib/typescript/react-native/src/AlphaVideoViewNativeComponent.d.ts +109 -0
- package/lib/typescript/react-native/src/AlphaVideoViewNativeComponent.d.ts.map +1 -0
- package/lib/typescript/react-native/src/AlphaVideoViewNativeComponent.web.d.ts +5 -0
- package/lib/typescript/react-native/src/AlphaVideoViewNativeComponent.web.d.ts.map +1 -0
- package/lib/typescript/react-native/src/NativeAlphaVideoModule.d.ts +21 -0
- package/lib/typescript/react-native/src/NativeAlphaVideoModule.d.ts.map +1 -0
- package/lib/typescript/react-native/src/NativeAlphaVideoModule.web.d.ts +3 -0
- package/lib/typescript/react-native/src/NativeAlphaVideoModule.web.d.ts.map +1 -0
- package/lib/typescript/react-native/src/WebPlayer.d.ts +3 -0
- package/lib/typescript/react-native/src/WebPlayer.d.ts.map +1 -0
- package/lib/typescript/react-native/src/WebPlayer.web.d.ts +9 -0
- package/lib/typescript/react-native/src/WebPlayer.web.d.ts.map +1 -0
- package/lib/typescript/react-native/src/index.d.ts +77 -0
- package/lib/typescript/react-native/src/index.d.ts.map +1 -0
- package/licenses/dav1d-COPYING.txt +23 -0
- package/licenses/libvpx-LICENSE.txt +31 -0
- package/licenses/libvpx-PATENTS.txt +23 -0
- package/licenses/libwebm-LICENSE.txt +30 -0
- package/licenses/libwebm-PATENTS.txt +23 -0
- package/package.json +150 -4
- package/prebuilt/android/arm64-v8a/lib/libdav1d.a +0 -0
- package/prebuilt/android/arm64-v8a/lib/libvpx.a +0 -0
- package/prebuilt/android/armeabi-v7a/lib/libdav1d.a +0 -0
- package/prebuilt/android/armeabi-v7a/lib/libvpx.a +0 -0
- package/prebuilt/android/x86_64/lib/libdav1d.a +0 -0
- package/prebuilt/android/x86_64/lib/libvpx.a +0 -0
- package/prebuilt/include/dav1d/common.h +94 -0
- package/prebuilt/include/dav1d/data.h +117 -0
- package/prebuilt/include/dav1d/dav1d.h +329 -0
- package/prebuilt/include/dav1d/headers.h +440 -0
- package/prebuilt/include/dav1d/picture.h +157 -0
- package/prebuilt/include/dav1d/version.h +50 -0
- package/prebuilt/include/vpx/vp8.h +136 -0
- package/prebuilt/include/vpx/vp8dx.h +228 -0
- package/prebuilt/include/vpx/vpx_codec.h +475 -0
- package/prebuilt/include/vpx/vpx_decoder.h +365 -0
- package/prebuilt/include/vpx/vpx_frame_buffer.h +83 -0
- package/prebuilt/include/vpx/vpx_image.h +209 -0
- package/prebuilt/include/vpx/vpx_integer.h +40 -0
- package/prebuilt/ios/lib/libdav1d.a +0 -0
- package/prebuilt/ios/lib/libvpx.a +0 -0
- package/prebuilt/ios-simulator/include/dav1d/common.h +94 -0
- package/prebuilt/ios-simulator/include/dav1d/data.h +117 -0
- package/prebuilt/ios-simulator/include/dav1d/dav1d.h +329 -0
- package/prebuilt/ios-simulator/include/dav1d/headers.h +440 -0
- package/prebuilt/ios-simulator/include/dav1d/picture.h +157 -0
- package/prebuilt/ios-simulator/include/dav1d/version.h +50 -0
- package/prebuilt/ios-simulator/include/vpx/vp8.h +136 -0
- package/prebuilt/ios-simulator/include/vpx/vp8dx.h +228 -0
- package/prebuilt/ios-simulator/include/vpx/vpx_codec.h +475 -0
- package/prebuilt/ios-simulator/include/vpx/vpx_decoder.h +365 -0
- package/prebuilt/ios-simulator/include/vpx/vpx_frame_buffer.h +83 -0
- package/prebuilt/ios-simulator/include/vpx/vpx_image.h +209 -0
- package/prebuilt/ios-simulator/include/vpx/vpx_integer.h +40 -0
- package/prebuilt/ios-simulator/lib/libdav1d.a +0 -0
- package/prebuilt/ios-simulator/lib/libvpx.a +0 -0
- package/react-native/android/CMakeLists.txt +14 -0
- package/react-native/android/build.gradle +110 -0
- package/react-native/android/src/main/AndroidManifest.xml +1 -0
- package/react-native/android/src/main/java/dev/mediacache/alphavideo/reactnative/AlphaVideoModule.java +102 -0
- package/react-native/android/src/main/java/dev/mediacache/alphavideo/reactnative/AlphaVideoPackage.java +50 -0
- package/react-native/android/src/main/java/dev/mediacache/alphavideo/reactnative/AlphaVideoViewManager.java +317 -0
- package/react-native/android/src/newarch/java/dev/mediacache/alphavideo/reactnative/AlphaVideoModuleSpec.java +10 -0
- package/react-native/android/src/newarch/java/dev/mediacache/alphavideo/reactnative/AlphaVideoViewManagerSpec.java +23 -0
- package/react-native/android/src/norangesource/java/dev/mediacache/alphavideo/AppDataSource.java +15 -0
- package/react-native/android/src/oldarch/java/dev/mediacache/alphavideo/reactnative/AlphaVideoModuleSpec.java +30 -0
- package/react-native/android/src/oldarch/java/dev/mediacache/alphavideo/reactnative/AlphaVideoViewManagerSpec.java +61 -0
- package/react-native/ios/RNAlphaVideoViewComponentView.mm +173 -0
- package/react-native/ios/RNAlphaVideoViewManager.mm +147 -0
- package/react-native/src/AlphaVideoView.tsx +329 -0
- package/react-native/src/AlphaVideoViewNativeComponent.ts +132 -0
- package/react-native/src/AlphaVideoViewNativeComponent.web.ts +5 -0
- package/react-native/src/NativeAlphaVideoModule.ts +22 -0
- package/react-native/src/NativeAlphaVideoModule.web.ts +2 -0
- package/react-native/src/WebPlayer.tsx +7 -0
- package/react-native/src/WebPlayer.web.tsx +60 -0
- package/react-native/src/index.tsx +144 -0
- package/react-native-alpha-video.podspec +99 -0
- package/react-native.config.js +12 -0
- package/scripts/build_codecs_android.sh +246 -0
- package/scripts/build_codecs_ios.sh +31 -0
- package/scripts/build_codecs_ios_device.sh +210 -0
- package/scripts/build_codecs_ios_simulator.sh +256 -0
- package/scripts/build_wasm.sh +230 -0
- package/scripts/build_xcframework.sh +49 -0
- package/scripts/fetch-prebuilt.sh +48 -0
- package/scripts/package_release.sh +107 -0
- package/scripts/publish-android.sh +85 -0
- package/scripts/vendor_range_source.sh +47 -0
- package/third_party/libwebm/AUTHORS.TXT +5 -0
- package/third_party/libwebm/LICENSE.TXT +30 -0
- package/third_party/libwebm/PATENTS.TXT +23 -0
- package/third_party/libwebm/README.md +7 -0
- package/third_party/libwebm/webm_parser/include/webm/buffer_reader.h +138 -0
- package/third_party/libwebm/webm_parser/include/webm/callback.h +363 -0
- package/third_party/libwebm/webm_parser/include/webm/dom_types.h +1781 -0
- package/third_party/libwebm/webm_parser/include/webm/element.h +208 -0
- package/third_party/libwebm/webm_parser/include/webm/file_reader.h +103 -0
- package/third_party/libwebm/webm_parser/include/webm/id.h +1085 -0
- package/third_party/libwebm/webm_parser/include/webm/istream_reader.h +99 -0
- package/third_party/libwebm/webm_parser/include/webm/reader.h +94 -0
- package/third_party/libwebm/webm_parser/include/webm/status.h +166 -0
- package/third_party/libwebm/webm_parser/include/webm/webm_parser.h +133 -0
- package/third_party/libwebm/webm_parser/src/ancestory.cc +334 -0
- package/third_party/libwebm/webm_parser/src/ancestory.h +83 -0
- package/third_party/libwebm/webm_parser/src/audio_parser.h +84 -0
- package/third_party/libwebm/webm_parser/src/bit_utils.cc +26 -0
- package/third_party/libwebm/webm_parser/src/bit_utils.h +24 -0
- package/third_party/libwebm/webm_parser/src/block_additions_parser.h +30 -0
- package/third_party/libwebm/webm_parser/src/block_group_parser.h +71 -0
- package/third_party/libwebm/webm_parser/src/block_header_parser.cc +76 -0
- package/third_party/libwebm/webm_parser/src/block_header_parser.h +65 -0
- package/third_party/libwebm/webm_parser/src/block_more_parser.h +32 -0
- package/third_party/libwebm/webm_parser/src/block_parser.cc +285 -0
- package/third_party/libwebm/webm_parser/src/block_parser.h +126 -0
- package/third_party/libwebm/webm_parser/src/bool_parser.h +95 -0
- package/third_party/libwebm/webm_parser/src/buffer_reader.cc +110 -0
- package/third_party/libwebm/webm_parser/src/byte_parser.h +144 -0
- package/third_party/libwebm/webm_parser/src/callback.cc +155 -0
- package/third_party/libwebm/webm_parser/src/chapter_atom_parser.h +42 -0
- package/third_party/libwebm/webm_parser/src/chapter_display_parser.h +34 -0
- package/third_party/libwebm/webm_parser/src/chapters_parser.h +28 -0
- package/third_party/libwebm/webm_parser/src/cluster_parser.h +48 -0
- package/third_party/libwebm/webm_parser/src/colour_parser.h +55 -0
- package/third_party/libwebm/webm_parser/src/content_enc_aes_settings_parser.h +32 -0
- package/third_party/libwebm/webm_parser/src/content_encoding_parser.h +38 -0
- package/third_party/libwebm/webm_parser/src/content_encodings_parser.h +30 -0
- package/third_party/libwebm/webm_parser/src/content_encryption_parser.h +37 -0
- package/third_party/libwebm/webm_parser/src/cue_point_parser.h +38 -0
- package/third_party/libwebm/webm_parser/src/cue_track_positions_parser.h +39 -0
- package/third_party/libwebm/webm_parser/src/cues_parser.h +27 -0
- package/third_party/libwebm/webm_parser/src/date_parser.cc +72 -0
- package/third_party/libwebm/webm_parser/src/date_parser.h +64 -0
- package/third_party/libwebm/webm_parser/src/ebml_parser.h +48 -0
- package/third_party/libwebm/webm_parser/src/edition_entry_parser.h +35 -0
- package/third_party/libwebm/webm_parser/src/element_parser.h +102 -0
- package/third_party/libwebm/webm_parser/src/file_reader.cc +142 -0
- package/third_party/libwebm/webm_parser/src/float_parser.cc +77 -0
- package/third_party/libwebm/webm_parser/src/float_parser.h +65 -0
- package/third_party/libwebm/webm_parser/src/id_element_parser.cc +49 -0
- package/third_party/libwebm/webm_parser/src/id_element_parser.h +56 -0
- package/third_party/libwebm/webm_parser/src/id_parser.cc +75 -0
- package/third_party/libwebm/webm_parser/src/id_parser.h +45 -0
- package/third_party/libwebm/webm_parser/src/info_parser.h +44 -0
- package/third_party/libwebm/webm_parser/src/int_parser.h +121 -0
- package/third_party/libwebm/webm_parser/src/istream_reader.cc +133 -0
- package/third_party/libwebm/webm_parser/src/master_parser.cc +298 -0
- package/third_party/libwebm/webm_parser/src/master_parser.h +230 -0
- package/third_party/libwebm/webm_parser/src/master_value_parser.h +533 -0
- package/third_party/libwebm/webm_parser/src/mastering_metadata_parser.h +57 -0
- package/third_party/libwebm/webm_parser/src/parser.h +34 -0
- package/third_party/libwebm/webm_parser/src/parser_utils.cc +34 -0
- package/third_party/libwebm/webm_parser/src/parser_utils.h +64 -0
- package/third_party/libwebm/webm_parser/src/projection_parser.h +40 -0
- package/third_party/libwebm/webm_parser/src/recursive_parser.h +93 -0
- package/third_party/libwebm/webm_parser/src/seek_head_parser.h +27 -0
- package/third_party/libwebm/webm_parser/src/seek_parser.h +37 -0
- package/third_party/libwebm/webm_parser/src/segment_parser.cc +86 -0
- package/third_party/libwebm/webm_parser/src/segment_parser.h +53 -0
- package/third_party/libwebm/webm_parser/src/simple_tag_parser.h +37 -0
- package/third_party/libwebm/webm_parser/src/size_parser.cc +56 -0
- package/third_party/libwebm/webm_parser/src/size_parser.h +43 -0
- package/third_party/libwebm/webm_parser/src/skip_callback.h +63 -0
- package/third_party/libwebm/webm_parser/src/skip_parser.cc +59 -0
- package/third_party/libwebm/webm_parser/src/skip_parser.h +35 -0
- package/third_party/libwebm/webm_parser/src/slices_parser.h +30 -0
- package/third_party/libwebm/webm_parser/src/tag_parser.h +37 -0
- package/third_party/libwebm/webm_parser/src/tags_parser.h +27 -0
- package/third_party/libwebm/webm_parser/src/targets_parser.h +35 -0
- package/third_party/libwebm/webm_parser/src/time_slice_parser.h +30 -0
- package/third_party/libwebm/webm_parser/src/track_entry_parser.h +65 -0
- package/third_party/libwebm/webm_parser/src/tracks_parser.h +27 -0
- package/third_party/libwebm/webm_parser/src/unknown_parser.cc +49 -0
- package/third_party/libwebm/webm_parser/src/unknown_parser.h +38 -0
- package/third_party/libwebm/webm_parser/src/var_int_parser.cc +71 -0
- package/third_party/libwebm/webm_parser/src/var_int_parser.h +56 -0
- package/third_party/libwebm/webm_parser/src/video_parser.h +122 -0
- package/third_party/libwebm/webm_parser/src/virtual_block_parser.cc +72 -0
- package/third_party/libwebm/webm_parser/src/virtual_block_parser.h +69 -0
- package/third_party/libwebm/webm_parser/src/void_parser.cc +49 -0
- package/third_party/libwebm/webm_parser/src/void_parser.h +41 -0
- package/third_party/libwebm/webm_parser/src/webm_parser.cc +282 -0
- package/third_party/range-source/LICENSE +21 -0
- package/third_party/range-source/README.md +8 -0
- package/third_party/range-source/include/range_source.h +395 -0
- package/third_party/range-source/include/range_source_download.h +309 -0
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The basic input, for a player running without the range-source add-on: a file downloaded whole,
|
|
3
|
+
* in one read from the start, into a temporary file, and read as it grows.
|
|
4
|
+
*
|
|
5
|
+
* Header-only, so that a player carries it with range_source.h and builds it in. Needs
|
|
6
|
+
* RANGE_SOURCE_CLIENT_NAMESPACE, as for range_source.h's C++ wrapper.
|
|
7
|
+
*/
|
|
8
|
+
#ifndef RANGE_SOURCE_DOWNLOAD_H
|
|
9
|
+
#define RANGE_SOURCE_DOWNLOAD_H
|
|
10
|
+
|
|
11
|
+
#include "range_source.h"
|
|
12
|
+
|
|
13
|
+
#ifndef RANGE_SOURCE_CLIENT_NAMESPACE
|
|
14
|
+
#error "Define RANGE_SOURCE_CLIENT_NAMESPACE before including range_source_download.h"
|
|
15
|
+
#endif
|
|
16
|
+
|
|
17
|
+
#include <algorithm>
|
|
18
|
+
#include <atomic>
|
|
19
|
+
#include <cerrno>
|
|
20
|
+
#include <chrono>
|
|
21
|
+
#include <condition_variable>
|
|
22
|
+
#include <cstdlib>
|
|
23
|
+
#include <memory>
|
|
24
|
+
#include <mutex>
|
|
25
|
+
#include <string>
|
|
26
|
+
#include <thread>
|
|
27
|
+
#include <unistd.h>
|
|
28
|
+
#include <vector>
|
|
29
|
+
|
|
30
|
+
namespace RANGE_SOURCE_CLIENT_NAMESPACE {
|
|
31
|
+
|
|
32
|
+
struct DownloadOptions {
|
|
33
|
+
/** Reopens after a failed read, each after twice the delay of the one before */
|
|
34
|
+
int retries = 3;
|
|
35
|
+
int64_t retryDelayMs = 500;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Downloads a file into a temporary file, from one open of the upstream at 0, and reads it as it
|
|
40
|
+
* arrives: a read past what has arrived waits for it. A dropped connection is reopened where it
|
|
41
|
+
* stopped, a few times. The file is unlinked as soon as it's made, so nothing stays behind, even
|
|
42
|
+
* after a crash; its space is freed when the reader is released.
|
|
43
|
+
*
|
|
44
|
+
* Nothing is kept between plays, and the upstream is never asked for anything but what follows.
|
|
45
|
+
*/
|
|
46
|
+
class DownloadReader : public Reader {
|
|
47
|
+
public:
|
|
48
|
+
using Options = DownloadOptions;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Opens `upstream` at 0 and starts the download into a new file in `dir`. Blocks for the
|
|
52
|
+
* open. Returns nullptr, with `error` set, if the file can't be made or the open fails.
|
|
53
|
+
*/
|
|
54
|
+
static std::shared_ptr<DownloadReader> open(std::unique_ptr<Upstream> upstream, const std::string& dir,
|
|
55
|
+
std::string& error, Options options = Options()) {
|
|
56
|
+
if (!upstream) {
|
|
57
|
+
error = "no source";
|
|
58
|
+
return nullptr;
|
|
59
|
+
}
|
|
60
|
+
std::string path = dir + "/range-source-download-XXXXXX";
|
|
61
|
+
std::vector<char> name(path.begin(), path.end());
|
|
62
|
+
name.push_back('\0');
|
|
63
|
+
const int fd = mkstemp(name.data());
|
|
64
|
+
if (fd < 0) {
|
|
65
|
+
error = "Cannot make a temporary file in " + dir;
|
|
66
|
+
return nullptr;
|
|
67
|
+
}
|
|
68
|
+
unlink(name.data());
|
|
69
|
+
|
|
70
|
+
auto state = std::make_shared<State>();
|
|
71
|
+
state->fd = fd;
|
|
72
|
+
state->options = options;
|
|
73
|
+
state->upstream = std::move(upstream);
|
|
74
|
+
if (!state->upstream->open(0, state->info, error)) {
|
|
75
|
+
if (error.empty()) error = "the source couldn't be opened";
|
|
76
|
+
return nullptr;
|
|
77
|
+
}
|
|
78
|
+
state->connected = true;
|
|
79
|
+
std::thread([state] { state->run(); }).detach();
|
|
80
|
+
return std::shared_ptr<DownloadReader>(new DownloadReader(state));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
~DownloadReader() override { close(); }
|
|
84
|
+
DownloadReader(const DownloadReader&) = delete;
|
|
85
|
+
DownloadReader& operator=(const DownloadReader&) = delete;
|
|
86
|
+
|
|
87
|
+
int64_t read(int64_t pos, uint8_t* out, int64_t size, const std::atomic<int32_t>* abort = nullptr) override {
|
|
88
|
+
return state_->read(pos, out, size, abort, true);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
int64_t tryRead(int64_t pos, uint8_t* out, int64_t size) override {
|
|
92
|
+
return state_->read(pos, out, size, nullptr, false);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
bool fetchAll(const std::atomic<int32_t>* abort = nullptr) override {
|
|
96
|
+
std::unique_lock<std::mutex> lock(state_->mutex);
|
|
97
|
+
for (;;) {
|
|
98
|
+
if (state_->closed || state_->failed || (abort && abort->load())) return false;
|
|
99
|
+
if (state_->done) return true;
|
|
100
|
+
state_->changed.wait_for(lock, kAbortPoll);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
int64_t size() const override {
|
|
105
|
+
std::lock_guard<std::mutex> lock(state_->mutex);
|
|
106
|
+
return state_->info.size;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
std::string lastError() const override {
|
|
110
|
+
std::lock_guard<std::mutex> lock(state_->mutex);
|
|
111
|
+
return state_->error;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
void close() override {
|
|
115
|
+
{
|
|
116
|
+
std::lock_guard<std::mutex> lock(state_->mutex);
|
|
117
|
+
if (state_->closed) return;
|
|
118
|
+
state_->closed = true;
|
|
119
|
+
state_->changed.notify_all();
|
|
120
|
+
}
|
|
121
|
+
state_->upstream->cancel();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Bytes downloaded so far. */
|
|
125
|
+
int64_t downloaded() const {
|
|
126
|
+
std::lock_guard<std::mutex> lock(state_->mutex);
|
|
127
|
+
return state_->have;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private:
|
|
131
|
+
// How often a waiting read checks its abort flag; nothing signals when it changes
|
|
132
|
+
static constexpr std::chrono::milliseconds kAbortPoll{50};
|
|
133
|
+
static constexpr int64_t kChunkBytes = 64 * 1024;
|
|
134
|
+
|
|
135
|
+
struct State {
|
|
136
|
+
std::mutex mutex;
|
|
137
|
+
std::condition_variable changed;
|
|
138
|
+
Options options;
|
|
139
|
+
int fd = -1;
|
|
140
|
+
std::unique_ptr<Upstream> upstream;
|
|
141
|
+
// Guarded by mutex. info.size is learnt at the end if the source didn't give it.
|
|
142
|
+
SourceInfo info;
|
|
143
|
+
int64_t have = 0;
|
|
144
|
+
bool done = false;
|
|
145
|
+
bool failed = false;
|
|
146
|
+
bool closed = false;
|
|
147
|
+
std::string error;
|
|
148
|
+
// The download thread's
|
|
149
|
+
bool connected = false;
|
|
150
|
+
|
|
151
|
+
~State() {
|
|
152
|
+
if (fd >= 0) ::close(fd);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
void run() {
|
|
156
|
+
std::vector<uint8_t> buffer(static_cast<size_t>(kChunkBytes));
|
|
157
|
+
int attempts = 0;
|
|
158
|
+
std::string message;
|
|
159
|
+
for (;;) {
|
|
160
|
+
if (isClosed()) break;
|
|
161
|
+
if (!connected && !reconnect(attempts, message)) break;
|
|
162
|
+
const int64_t n = upstream->read(buffer.data(), kChunkBytes, message);
|
|
163
|
+
if (n > 0) {
|
|
164
|
+
if (!write(buffer.data(), n)) break;
|
|
165
|
+
attempts = 0;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (n == 0) {
|
|
169
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
170
|
+
if (info.size < 0 || have >= info.size) {
|
|
171
|
+
info.size = have;
|
|
172
|
+
done = true;
|
|
173
|
+
changed.notify_all();
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
message = "the source ended at " + std::to_string(have) + " of " + std::to_string(info.size) +
|
|
177
|
+
" bytes";
|
|
178
|
+
}
|
|
179
|
+
upstream->close();
|
|
180
|
+
connected = false;
|
|
181
|
+
if (isClosed()) break;
|
|
182
|
+
if (attempts >= options.retries) {
|
|
183
|
+
fail(message);
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
if (!waitRetry(++attempts)) break;
|
|
187
|
+
}
|
|
188
|
+
if (connected) upstream->close();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
bool isClosed() {
|
|
192
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
193
|
+
return closed;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Opens again where the download stopped, for the same version of the file
|
|
197
|
+
bool reconnect(int& attempts, std::string& message) {
|
|
198
|
+
for (;;) {
|
|
199
|
+
int64_t from;
|
|
200
|
+
{
|
|
201
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
202
|
+
if (closed) return false;
|
|
203
|
+
from = have;
|
|
204
|
+
}
|
|
205
|
+
SourceInfo opened;
|
|
206
|
+
message.clear();
|
|
207
|
+
if (upstream->open(from, opened, message)) {
|
|
208
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
209
|
+
if (!sameVersion(info, opened)) {
|
|
210
|
+
upstream->close();
|
|
211
|
+
failLocked("the source changed while it was read");
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
connected = true;
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
if (attempts >= options.retries) {
|
|
218
|
+
fail(message.empty() ? "the source couldn't be opened" : message);
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
if (!waitRetry(++attempts)) return false;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
bool waitRetry(int attempt) {
|
|
226
|
+
const auto delay = std::chrono::milliseconds(options.retryDelayMs << std::min(attempt - 1, 10));
|
|
227
|
+
std::unique_lock<std::mutex> lock(mutex);
|
|
228
|
+
changed.wait_for(lock, delay, [this] { return closed; });
|
|
229
|
+
return !closed;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
bool write(const uint8_t* data, int64_t n) {
|
|
233
|
+
int64_t at;
|
|
234
|
+
{
|
|
235
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
236
|
+
at = have;
|
|
237
|
+
}
|
|
238
|
+
for (int64_t written = 0; written < n;) {
|
|
239
|
+
const ssize_t w = pwrite(fd, data + written, static_cast<size_t>(n - written), at + written);
|
|
240
|
+
if (w < 0 && errno == EINTR) continue;
|
|
241
|
+
if (w <= 0) {
|
|
242
|
+
fail("Cannot write the download to its temporary file (" + std::string(errno == ENOSPC ? "the disk is full" : "error " + std::to_string(errno)) + ")");
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
written += w;
|
|
246
|
+
}
|
|
247
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
248
|
+
have += n;
|
|
249
|
+
changed.notify_all();
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
void fail(const std::string& message) {
|
|
254
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
255
|
+
failLocked(message);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
void failLocked(const std::string& message) {
|
|
259
|
+
if (closed) return;
|
|
260
|
+
failed = true;
|
|
261
|
+
error = message;
|
|
262
|
+
changed.notify_all();
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
int64_t read(int64_t pos, uint8_t* out, int64_t size, const std::atomic<int32_t>* abort, bool wait) {
|
|
266
|
+
if (pos < 0 || size < 0) return -1;
|
|
267
|
+
if (size == 0) return 0;
|
|
268
|
+
int64_t n;
|
|
269
|
+
{
|
|
270
|
+
std::unique_lock<std::mutex> lock(mutex);
|
|
271
|
+
for (;;) {
|
|
272
|
+
if (closed || (abort && abort->load())) return -1;
|
|
273
|
+
if (info.size >= 0 && pos >= info.size) return 0;
|
|
274
|
+
if (pos < have) break;
|
|
275
|
+
if (done) return 0;
|
|
276
|
+
if (failed) return -1;
|
|
277
|
+
if (!wait) return kPending;
|
|
278
|
+
changed.wait_for(lock, kAbortPoll);
|
|
279
|
+
}
|
|
280
|
+
n = std::min(size, have - pos);
|
|
281
|
+
}
|
|
282
|
+
for (;;) {
|
|
283
|
+
const ssize_t r = pread(fd, out, static_cast<size_t>(n), pos);
|
|
284
|
+
if (r < 0 && errno == EINTR) continue;
|
|
285
|
+
if (r < 0) {
|
|
286
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
287
|
+
error = "Cannot read the download's temporary file (error " + std::to_string(errno) + ")";
|
|
288
|
+
return -1;
|
|
289
|
+
}
|
|
290
|
+
return r;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
static bool sameVersion(const SourceInfo& a, const SourceInfo& b) {
|
|
296
|
+
if (a.size >= 0 && b.size >= 0 && a.size != b.size) return false;
|
|
297
|
+
if (!a.etag.empty() && !b.etag.empty() && a.etag != b.etag) return false;
|
|
298
|
+
if (!a.lastModified.empty() && !b.lastModified.empty() && a.lastModified != b.lastModified) return false;
|
|
299
|
+
return true;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
explicit DownloadReader(std::shared_ptr<State> state) : state_(std::move(state)) {}
|
|
303
|
+
|
|
304
|
+
const std::shared_ptr<State> state_;
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
} // namespace RANGE_SOURCE_CLIENT_NAMESPACE
|
|
308
|
+
|
|
309
|
+
#endif /* RANGE_SOURCE_DOWNLOAD_H */
|