@appium/media-proxy 0.0.1 → 1.1.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 (123) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/LICENSE +201 -0
  3. package/README.md +183 -2
  4. package/THIRD_PARTY_NOTICES.md +29 -0
  5. package/binding.gyp +90 -0
  6. package/lib/src/background.d.ts +5 -0
  7. package/lib/src/background.d.ts.map +1 -0
  8. package/lib/src/background.js +16 -0
  9. package/lib/src/background.js.map +1 -0
  10. package/lib/src/capabilities.d.ts +9 -0
  11. package/lib/src/capabilities.d.ts.map +1 -0
  12. package/lib/src/capabilities.js +11 -0
  13. package/lib/src/capabilities.js.map +1 -0
  14. package/lib/src/errors.d.ts +26 -0
  15. package/lib/src/errors.d.ts.map +1 -0
  16. package/lib/src/errors.js +40 -0
  17. package/lib/src/errors.js.map +1 -0
  18. package/lib/src/framing/apst.d.ts +43 -0
  19. package/lib/src/framing/apst.d.ts.map +1 -0
  20. package/lib/src/framing/apst.js +126 -0
  21. package/lib/src/framing/apst.js.map +1 -0
  22. package/lib/src/framing/mjpeg-http.d.ts +30 -0
  23. package/lib/src/framing/mjpeg-http.d.ts.map +1 -0
  24. package/lib/src/framing/mjpeg-http.js +148 -0
  25. package/lib/src/framing/mjpeg-http.js.map +1 -0
  26. package/lib/src/framing/mjpeg-multipart.d.ts +11 -0
  27. package/lib/src/framing/mjpeg-multipart.d.ts.map +1 -0
  28. package/lib/src/framing/mjpeg-multipart.js +26 -0
  29. package/lib/src/framing/mjpeg-multipart.js.map +1 -0
  30. package/lib/src/index.d.ts +10 -0
  31. package/lib/src/index.d.ts.map +1 -0
  32. package/lib/src/index.js +7 -0
  33. package/lib/src/index.js.map +1 -0
  34. package/lib/src/native.d.ts +12 -0
  35. package/lib/src/native.d.ts.map +1 -0
  36. package/lib/src/native.js +36 -0
  37. package/lib/src/native.js.map +1 -0
  38. package/lib/src/pkg-root.d.ts +6 -0
  39. package/lib/src/pkg-root.d.ts.map +1 -0
  40. package/lib/src/pkg-root.js +12 -0
  41. package/lib/src/pkg-root.js.map +1 -0
  42. package/lib/src/queue.d.ts +34 -0
  43. package/lib/src/queue.d.ts.map +1 -0
  44. package/lib/src/queue.js +123 -0
  45. package/lib/src/queue.js.map +1 -0
  46. package/lib/src/transcoder.d.ts +81 -0
  47. package/lib/src/transcoder.d.ts.map +1 -0
  48. package/lib/src/transcoder.js +422 -0
  49. package/lib/src/transcoder.js.map +1 -0
  50. package/lib/src/types.d.ts +147 -0
  51. package/lib/src/types.d.ts.map +1 -0
  52. package/lib/src/types.js +2 -0
  53. package/lib/src/types.js.map +1 -0
  54. package/package.json +53 -4
  55. package/prebuilds/darwin-arm64/@appium+media-proxy.node +0 -0
  56. package/prebuilds/darwin-arm64/ffmpeg-LICENSE-LGPL-2.1.txt +502 -0
  57. package/prebuilds/darwin-arm64/ffmpeg-build-info.txt +3 -0
  58. package/prebuilds/darwin-arm64/libavcodec-appium.62.dylib +0 -0
  59. package/prebuilds/darwin-arm64/libavutil-appium.60.dylib +0 -0
  60. package/prebuilds/darwin-arm64/libswscale-appium.9.dylib +0 -0
  61. package/prebuilds/darwin-x64/@appium+media-proxy.node +0 -0
  62. package/prebuilds/darwin-x64/ffmpeg-LICENSE-LGPL-2.1.txt +502 -0
  63. package/prebuilds/darwin-x64/ffmpeg-build-info.txt +3 -0
  64. package/prebuilds/darwin-x64/libavcodec-appium.62.dylib +0 -0
  65. package/prebuilds/darwin-x64/libavutil-appium.60.dylib +0 -0
  66. package/prebuilds/darwin-x64/libswscale-appium.9.dylib +0 -0
  67. package/prebuilds/linux-arm64/@appium+media-proxy.node +0 -0
  68. package/prebuilds/linux-arm64/ffmpeg-LICENSE-LGPL-2.1.txt +502 -0
  69. package/prebuilds/linux-arm64/ffmpeg-build-info.txt +3 -0
  70. package/prebuilds/linux-arm64/libavcodec-appium.so.62 +0 -0
  71. package/prebuilds/linux-arm64/libavutil-appium.so.60 +0 -0
  72. package/prebuilds/linux-arm64/libswscale-appium.so.9 +0 -0
  73. package/prebuilds/linux-x64/@appium+media-proxy.node +0 -0
  74. package/prebuilds/linux-x64/ffmpeg-LICENSE-LGPL-2.1.txt +502 -0
  75. package/prebuilds/linux-x64/ffmpeg-build-info.txt +3 -0
  76. package/prebuilds/linux-x64/libavcodec-appium.so.62 +0 -0
  77. package/prebuilds/linux-x64/libavutil-appium.so.60 +0 -0
  78. package/prebuilds/linux-x64/libswscale-appium.so.9 +0 -0
  79. package/prebuilds/win32-arm64/@appium+media-proxy.node +0 -0
  80. package/prebuilds/win32-arm64/avcodec-appium-62.dll +0 -0
  81. package/prebuilds/win32-arm64/avutil-appium-60.dll +0 -0
  82. package/prebuilds/win32-arm64/ffmpeg-LICENSE-LGPL-2.1.txt +502 -0
  83. package/prebuilds/win32-arm64/ffmpeg-build-info.txt +3 -0
  84. package/prebuilds/win32-arm64/swscale-appium-9.dll +0 -0
  85. package/prebuilds/win32-x64/@appium+media-proxy.node +0 -0
  86. package/prebuilds/win32-x64/avcodec-appium-62.dll +0 -0
  87. package/prebuilds/win32-x64/avutil-appium-60.dll +0 -0
  88. package/prebuilds/win32-x64/ffmpeg-LICENSE-LGPL-2.1.txt +502 -0
  89. package/prebuilds/win32-x64/ffmpeg-build-info.txt +3 -0
  90. package/prebuilds/win32-x64/swscale-appium-9.dll +0 -0
  91. package/scripts/install.mjs +29 -0
  92. package/src/background.ts +15 -0
  93. package/src/capabilities.ts +12 -0
  94. package/src/errors.ts +45 -0
  95. package/src/framing/apst.ts +158 -0
  96. package/src/framing/mjpeg-http.ts +186 -0
  97. package/src/framing/mjpeg-multipart.ts +38 -0
  98. package/src/index.ts +43 -0
  99. package/src/native/addon.cc +402 -0
  100. package/src/native/audio_pipeline.cc +426 -0
  101. package/src/native/audio_pipeline.h +63 -0
  102. package/src/native/ffmpeg_util.h +37 -0
  103. package/src/native/h264_encoder.cc +133 -0
  104. package/src/native/h264_encoder.h +48 -0
  105. package/src/native/mediafoundation_encoder.cc +442 -0
  106. package/src/native/mediafoundation_encoder.h +14 -0
  107. package/src/native/napi_util.h +20 -0
  108. package/src/native/openh264_encoder.cc +285 -0
  109. package/src/native/openh264_encoder.h +14 -0
  110. package/src/native/pipeline.cc +555 -0
  111. package/src/native/pipeline.h +108 -0
  112. package/src/native/vendor/openh264/LICENSE +23 -0
  113. package/src/native/vendor/openh264/codec_api.h +592 -0
  114. package/src/native/vendor/openh264/codec_app_def.h +820 -0
  115. package/src/native/vendor/openh264/codec_def.h +216 -0
  116. package/src/native/vendor/openh264/codec_ver.h +15 -0
  117. package/src/native/videotoolbox_encoder.cc +314 -0
  118. package/src/native/videotoolbox_encoder.h +13 -0
  119. package/src/native.ts +51 -0
  120. package/src/pkg-root.ts +14 -0
  121. package/src/queue.ts +133 -0
  122. package/src/transcoder.ts +480 -0
  123. package/src/types.ts +158 -0
@@ -0,0 +1,285 @@
1
+ #include "openh264_encoder.h"
2
+
3
+ #include <algorithm>
4
+ #include <cmath>
5
+ #include <cstdint>
6
+ #include <cstdlib>
7
+ #include <limits>
8
+ #include <mutex>
9
+ #include <string>
10
+ #include <thread>
11
+ #include <utility>
12
+ #include <vector>
13
+
14
+ #ifdef _WIN32
15
+ #ifndef NOMINMAX
16
+ #define NOMINMAX
17
+ #endif
18
+ #include <windows.h>
19
+ #else
20
+ #include <dlfcn.h>
21
+ #endif
22
+
23
+ #include "vendor/openh264/codec_api.h"
24
+ #include "vendor/openh264/codec_ver.h"
25
+
26
+ extern "C" {
27
+ #include <libavutil/frame.h>
28
+ }
29
+
30
+ namespace media_proxy {
31
+
32
+ namespace {
33
+
34
+ constexpr const char* kPathVariable = "MEDIA_PROXY_OPENH264_PATH";
35
+
36
+ using CreateFn = int (*)(ISVCEncoder**);
37
+ using DestroyFn = void (*)(ISVCEncoder*);
38
+ using VersionFn = void (*)(OpenH264Version*);
39
+
40
+ struct Library {
41
+ CreateFn create = nullptr;
42
+ DestroyFn destroy = nullptr;
43
+ };
44
+
45
+ std::mutex g_mutex;
46
+ Library g_library; // loaded once, never unloaded: encoders may outlive any single caller
47
+
48
+ PipelineError Unavailable(const std::string& message) { return {kEncoderUnavailableError, message}; }
49
+
50
+ void* OpenLibrary(const std::string& path) {
51
+ #ifdef _WIN32
52
+ const int length = MultiByteToWideChar(CP_UTF8, 0, path.c_str(), -1, nullptr, 0);
53
+ if (length <= 0) {
54
+ return nullptr;
55
+ }
56
+ std::wstring wide(static_cast<size_t>(length), L'\0');
57
+ MultiByteToWideChar(CP_UTF8, 0, path.c_str(), -1, wide.data(), length);
58
+ return reinterpret_cast<void*>(LoadLibraryW(wide.c_str()));
59
+ #else
60
+ return dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
61
+ #endif
62
+ }
63
+
64
+ void* Symbol(void* handle, const char* name) {
65
+ #ifdef _WIN32
66
+ return reinterpret_cast<void*>(GetProcAddress(static_cast<HMODULE>(handle), name));
67
+ #else
68
+ return dlsym(handle, name);
69
+ #endif
70
+ }
71
+
72
+ void CloseLibrary(void* handle) {
73
+ #ifdef _WIN32
74
+ FreeLibrary(static_cast<HMODULE>(handle));
75
+ #else
76
+ dlclose(handle);
77
+ #endif
78
+ }
79
+
80
+ // Same major.minor as the vendored headers: the C++ vtable layout is only stable within one.
81
+ Library Bind(void* handle, const std::string& path) {
82
+ Library library;
83
+ library.create = reinterpret_cast<CreateFn>(Symbol(handle, "WelsCreateSVCEncoder"));
84
+ library.destroy = reinterpret_cast<DestroyFn>(Symbol(handle, "WelsDestroySVCEncoder"));
85
+ const auto version = reinterpret_cast<VersionFn>(Symbol(handle, "WelsGetCodecVersionEx"));
86
+ if (library.create == nullptr || library.destroy == nullptr || version == nullptr) {
87
+ throw Unavailable(path + " is not an OpenH264 library");
88
+ }
89
+ OpenH264Version found{};
90
+ version(&found);
91
+ if (found.uMajor != OPENH264_MAJOR || found.uMinor != OPENH264_MINOR) {
92
+ throw Unavailable("Unsupported OpenH264 " + std::to_string(found.uMajor) + "." + std::to_string(found.uMinor) +
93
+ " (expected " + std::to_string(OPENH264_MAJOR) + "." + std::to_string(OPENH264_MINOR) + ")");
94
+ }
95
+ return library;
96
+ }
97
+
98
+ // Loads the library named by $MEDIA_PROXY_OPENH264_PATH once; a rejected one is closed again.
99
+ Library LoadLibraryOnce() {
100
+ const std::scoped_lock lock(g_mutex);
101
+ if (g_library.create != nullptr) {
102
+ return g_library;
103
+ }
104
+ const char* env = std::getenv(kPathVariable);
105
+ if (env == nullptr || *env == '\0') {
106
+ throw Unavailable(std::string("OpenH264 is not configured: set ") + kPathVariable + " to its library");
107
+ }
108
+ const std::string path = env;
109
+ void* handle = OpenLibrary(path);
110
+ if (handle == nullptr) {
111
+ throw Unavailable("Could not load OpenH264 from " + path);
112
+ }
113
+ try {
114
+ g_library = Bind(handle, path);
115
+ } catch (...) {
116
+ CloseLibrary(handle); // keep nothing loaded (and no DLL locked) for a library we reject
117
+ throw;
118
+ }
119
+ return g_library;
120
+ }
121
+
122
+ // OpenH264 only takes planar input, so the NV12 chroma is de-interleaved.
123
+ class OpenH264Encoder final : public H264Encoder {
124
+ public:
125
+ OpenH264Encoder(const H264Params& params, Sink sink)
126
+ : width_(params.width), height_(params.height), sink_(std::move(sink)), library_(LoadLibraryOnce()) {
127
+ if (library_.create(&encoder_) != 0 || encoder_ == nullptr) {
128
+ throw Unavailable("OpenH264 could not create an encoder");
129
+ }
130
+ try {
131
+ Open(params);
132
+ } catch (...) {
133
+ library_.destroy(encoder_);
134
+ throw;
135
+ }
136
+ }
137
+
138
+ ~OpenH264Encoder() override {
139
+ encoder_->Uninitialize();
140
+ library_.destroy(encoder_);
141
+ }
142
+
143
+ void Encode(const AVFrame& nv12, bool forceKeyFrame) override {
144
+ if (forceKeyFrame) {
145
+ encoder_->ForceIntraFrame(true);
146
+ }
147
+ SSourcePicture picture{};
148
+ picture.iColorFormat = videoFormatI420;
149
+ picture.iPicWidth = width_;
150
+ picture.iPicHeight = height_;
151
+ PackPlanes(nv12, picture);
152
+ picture.uiTimeStamp = nv12.pts / kMicrosPerMilli;
153
+
154
+ SFrameBSInfo info{};
155
+ if (encoder_->EncodeFrame(&picture, &info) != cmResultSuccess) {
156
+ throw PipelineError{"MediaProxyError", "OpenH264 failed to encode a frame"};
157
+ }
158
+ if (info.eFrameType == videoFrameTypeSkip || info.eFrameType == videoFrameTypeInvalid) {
159
+ return;
160
+ }
161
+ EncodedUnit unit;
162
+ unit.isKeyFrame = info.eFrameType == videoFrameTypeIDR;
163
+ unit.timestampMicros = nv12.pts;
164
+ for (int layer = 0; layer < info.iLayerNum; ++layer) {
165
+ const SLayerBSInfo& bs = info.sLayerInfo[layer];
166
+ size_t size = 0;
167
+ for (int nal = 0; nal < bs.iNalCount; ++nal) {
168
+ size += static_cast<size_t>(bs.pNalLengthInByte[nal]);
169
+ }
170
+ unit.data.insert(unit.data.end(), bs.pBsBuf, bs.pBsBuf + size);
171
+ }
172
+ if (!unit.data.empty()) {
173
+ sink_(std::move(unit));
174
+ }
175
+ }
176
+
177
+ void Flush() override {} // synchronous: nothing is held back
178
+
179
+ private:
180
+ static constexpr int64_t kMicrosPerMilli = 1000;
181
+ static constexpr int kMaxThreads = 4;
182
+ static constexpr int kMinSide = 16; // smaller frames initialise but fail every encode
183
+ static constexpr int64_t kMaxBitrate = 288'000'000; // the highest rate OpenH264 accepts (level 5.2)
184
+
185
+ // One slice per worker thread: OpenH264 can only parallelise across slices.
186
+ static int SliceThreads() {
187
+ return static_cast<int>(
188
+ std::clamp(std::thread::hardware_concurrency(), 1U, static_cast<unsigned int>(kMaxThreads)));
189
+ }
190
+
191
+ void Open(const H264Params& params) {
192
+ if (width_ < kMinSide || height_ < kMinSide) {
193
+ throw Unavailable("OpenH264 cannot encode frames smaller than 16x16 pixels");
194
+ }
195
+ int logLevel = WELS_LOG_QUIET;
196
+ encoder_->SetOption(ENCODER_OPTION_TRACE_LEVEL, &logLevel);
197
+ if (Initialize(params, SliceThreads())) {
198
+ return;
199
+ }
200
+ // Rate control needs room for a few macroblock rows per slice: a short frame only encodes as one slice.
201
+ encoder_->Uninitialize();
202
+ if (!Initialize(params, 1)) {
203
+ throw Unavailable("OpenH264 rejected the encoder parameters");
204
+ }
205
+ }
206
+
207
+ bool Initialize(const H264Params& params, int threads) {
208
+ SEncParamExt ext{};
209
+ if (encoder_->GetDefaultParams(&ext) != cmResultSuccess) {
210
+ throw Unavailable("OpenH264 rejected its default parameters");
211
+ }
212
+ const auto lowest = static_cast<int64_t>(std::ceil(params.fps));
213
+ const auto bitrate = static_cast<int>(std::clamp<int64_t>(params.bitrate, lowest, kMaxBitrate));
214
+ ext.iUsageType = SCREEN_CONTENT_REAL_TIME;
215
+ ext.iPicWidth = width_;
216
+ ext.iPicHeight = height_;
217
+ ext.iTargetBitrate = bitrate;
218
+ ext.iMaxBitrate = bitrate;
219
+ ext.iRCMode = RC_BITRATE_MODE;
220
+ ext.fMaxFrameRate = static_cast<float>(params.fps);
221
+ ext.uiIntraPeriod = static_cast<unsigned int>(params.gopFrames);
222
+ ext.iTemporalLayerNum = 1;
223
+ ext.iSpatialLayerNum = 1;
224
+ ext.iComplexityMode = LOW_COMPLEXITY;
225
+ ext.eSpsPpsIdStrategy = CONSTANT_ID;
226
+ ext.bEnableFrameSkip = false;
227
+ ext.bEnableDenoise = false;
228
+ ext.iMultipleThreadIdc = static_cast<unsigned short>(threads);
229
+ SSpatialLayerConfig& layer = ext.sSpatialLayers[0];
230
+ layer.iVideoWidth = width_;
231
+ layer.iVideoHeight = height_;
232
+ layer.fFrameRate = static_cast<float>(params.fps);
233
+ layer.iSpatialBitrate = bitrate;
234
+ layer.iMaxSpatialBitrate = bitrate;
235
+ layer.sSliceArgument.uiSliceMode = threads > 1 ? SM_FIXEDSLCNUM_SLICE : SM_SINGLE_SLICE;
236
+ layer.sSliceArgument.uiSliceNum = static_cast<unsigned int>(threads);
237
+ // The pipeline converts to limited-range BT.709 (same tags as the VideoToolbox and Media Foundation encoders).
238
+ layer.bVideoSignalTypePresent = true;
239
+ layer.uiVideoFormat = VF_UNDEF;
240
+ layer.bFullRange = false;
241
+ layer.bColorDescriptionPresent = true;
242
+ layer.uiColorPrimaries = CP_BT709;
243
+ layer.uiTransferCharacteristics = TRC_BT709;
244
+ layer.uiColorMatrix = CM_BT709;
245
+ return encoder_->InitializeExt(&ext) == cmResultSuccess;
246
+ }
247
+
248
+ void PackPlanes(const AVFrame& nv12, SSourcePicture& picture) {
249
+ const size_t chromaWidth = (static_cast<size_t>(width_) + 1) / 2;
250
+ const size_t chromaHeight = (static_cast<size_t>(height_) + 1) / 2;
251
+ planes_.resize(chromaWidth * chromaHeight * 2);
252
+ uint8_t* u = planes_.data();
253
+ uint8_t* v = u + (chromaWidth * chromaHeight);
254
+ for (size_t row = 0; row < chromaHeight; ++row) {
255
+ const uint8_t* src = nv12.data[1] + (row * static_cast<size_t>(nv12.linesize[1]));
256
+ uint8_t* dstU = u + (row * chromaWidth);
257
+ uint8_t* dstV = v + (row * chromaWidth);
258
+ for (size_t col = 0; col < chromaWidth; ++col) {
259
+ dstU[col] = src[col * 2];
260
+ dstV[col] = src[(col * 2) + 1];
261
+ }
262
+ }
263
+ picture.pData[0] = nv12.data[0];
264
+ picture.pData[1] = u;
265
+ picture.pData[2] = v;
266
+ picture.iStride[0] = nv12.linesize[0];
267
+ picture.iStride[1] = static_cast<int>(chromaWidth);
268
+ picture.iStride[2] = static_cast<int>(chromaWidth);
269
+ }
270
+
271
+ int width_;
272
+ int height_;
273
+ Sink sink_;
274
+ Library library_;
275
+ ISVCEncoder* encoder_ = nullptr;
276
+ std::vector<uint8_t> planes_;
277
+ };
278
+
279
+ } // namespace
280
+
281
+ std::unique_ptr<H264Encoder> CreateOpenH264Encoder(const H264Params& params, const H264Encoder::Sink& sink) {
282
+ return std::make_unique<OpenH264Encoder>(params, sink);
283
+ }
284
+
285
+ } // namespace media_proxy
@@ -0,0 +1,14 @@
1
+ #pragma once
2
+
3
+ #include <memory>
4
+
5
+ #include "h264_encoder.h"
6
+
7
+ namespace media_proxy {
8
+
9
+ // Software H.264 through Cisco's OpenH264, loaded at runtime from the library named by $MEDIA_PROXY_OPENH264_PATH
10
+ // (installed by the client package, never bundled). Without that variable OpenH264 is not offered.
11
+ // Throws PipelineError (MediaProxyEncoderUnavailableError) if the library is missing, mismatched or fails to open.
12
+ std::unique_ptr<H264Encoder> CreateOpenH264Encoder(const H264Params& params, const H264Encoder::Sink& sink);
13
+
14
+ } // namespace media_proxy