react-native-alpha-video-gpu 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 (54) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +949 -2
  3. package/THIRD_PARTY_NOTICES.md +14 -0
  4. package/android/src/main/AndroidManifest.xml +9 -0
  5. package/android/src/main/cpp/CMakeLists.txt +41 -0
  6. package/android/src/main/cpp/alphavideogpu_jni.cpp +1168 -0
  7. package/android/src/main/cpp/astc_decomp.cpp +1550 -0
  8. package/android/src/main/cpp/astc_decomp.h +41 -0
  9. package/android/src/main/cpp/source_jni.cpp +242 -0
  10. package/android/src/main/cpp/source_jni.h +13 -0
  11. package/android/src/main/java/dev/mediacache/alphavideo/gpu/AlphaVideoGpuNative.java +43 -0
  12. package/android/src/main/java/dev/mediacache/alphavideo/gpu/AlphaVideoGpuReader.java +209 -0
  13. package/android/src/main/java/dev/mediacache/alphavideo/gpu/AlphaVideoGpuSource.java +328 -0
  14. package/android/src/main/java/dev/mediacache/alphavideo/gpu/AlphaVideoGpuView.java +2126 -0
  15. package/android/src/main/java/dev/mediacache/alphavideo/gpu/Fetcher.java +37 -0
  16. package/android/src/main/java/dev/mediacache/alphavideo/gpu/HttpFetcher.java +156 -0
  17. package/android/src/main/java/dev/mediacache/alphavideo/gpu/Ktx2Metadata.java +78 -0
  18. package/android/src/main/java/dev/mediacache/alphavideo/gpu/RangeSourceLink.java +54 -0
  19. package/android/src/main/java/dev/mediacache/alphavideo/gpu/ZstdSeekTable.java +120 -0
  20. package/android/src/rangesource/java/dev/mediacache/alphavideo/gpu/AppDataSource.java +50 -0
  21. package/ios/Sources/AlphaVideoGPU/AlphaVideoGpuSource.h +63 -0
  22. package/ios/Sources/AlphaVideoGPU/AlphaVideoGpuSource.mm +522 -0
  23. package/ios/Sources/AlphaVideoGPU/AlphaVideoGpuView.mm +2722 -0
  24. package/ios/Sources/AlphaVideoGPU/astc_decomp.cpp +1550 -0
  25. package/ios/Sources/AlphaVideoGPU/astc_decomp.h +41 -0
  26. package/ios/Sources/AlphaVideoGPU/include/AlphaVideoGPU.h +110 -0
  27. package/ios/Sources/AlphaVideoGPU/zstd_seek.h +250 -0
  28. package/package.json +77 -4
  29. package/react-native/android/build.gradle +92 -0
  30. package/react-native/android/src/main/AndroidManifest.xml +9 -0
  31. package/react-native/android/src/main/java/dev/mediacache/alphavideo/gpu/reactnative/AlphaVideoGpuPackage.java +26 -0
  32. package/react-native/android/src/main/java/dev/mediacache/alphavideo/gpu/reactnative/AlphaVideoGpuViewManager.java +187 -0
  33. package/react-native/android/src/main/java/dev/mediacache/alphavideo/gpu/reactnative/PlayerEventForwarder.java +107 -0
  34. package/react-native/android/src/newarch/java/dev/mediacache/alphavideo/gpu/reactnative/AlphaVideoGpuViewManagerSpec.java +23 -0
  35. package/react-native/android/src/norangesource/java/dev/mediacache/alphavideo/gpu/AppDataSource.java +15 -0
  36. package/react-native/android/src/oldarch/java/dev/mediacache/alphavideo/gpu/reactnative/AlphaVideoGpuViewManagerSpec.java +56 -0
  37. package/react-native/ios/RNAlphaVideoGpuViewComponentView.mm +176 -0
  38. package/react-native/ios/RNAlphaVideoGpuViewManager.mm +175 -0
  39. package/react-native/src/AlphaVideoGpuView.js +2 -0
  40. package/react-native/src/AlphaVideoGpuView.web.tsx +264 -0
  41. package/react-native/src/AlphaVideoGpuViewNativeComponent.ts +128 -0
  42. package/react-native/src/AlphaVideoGpuViewNativeComponent.web.ts +2 -0
  43. package/react-native/src/index.d.ts +95 -0
  44. package/react-native/src/index.js +162 -0
  45. package/react-native-alpha-video-gpu.podspec +37 -0
  46. package/react-native.config.js +12 -0
  47. package/third_party/licenses/Apache-2.0.txt +208 -0
  48. package/third_party/range-source/LICENSE +21 -0
  49. package/third_party/range-source/README.md +10 -0
  50. package/third_party/range-source/include/range_source.h +395 -0
  51. package/third_party/range-source/include/range_source_download.h +309 -0
  52. package/third_party/zstd/LICENSE +30 -0
  53. package/third_party/zstd/zstd.c +38717 -0
  54. package/third_party/zstd/zstd.h +2450 -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 */
@@ -0,0 +1,30 @@
1
+ BSD License
2
+
3
+ For Zstandard software
4
+
5
+ Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
6
+
7
+ Redistribution and use in source and binary forms, with or without modification,
8
+ are permitted provided that the following conditions are met:
9
+
10
+ * Redistributions of source code must retain the above copyright notice, this
11
+ list of conditions and the following disclaimer.
12
+
13
+ * Redistributions in binary form must reproduce the above copyright notice,
14
+ this list of conditions and the following disclaimer in the documentation
15
+ and/or other materials provided with the distribution.
16
+
17
+ * Neither the name Facebook nor the names of its contributors may be used to
18
+ endorse or promote products derived from this software without specific
19
+ prior written permission.
20
+
21
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
25
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.