@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,442 @@
1
+ #include "mediafoundation_encoder.h"
2
+
3
+ #define WIN32_LEAN_AND_MEAN
4
+ #define NOMINMAX
5
+ #include <windows.h>
6
+ // clang-format off
7
+ #include <mfapi.h>
8
+ #include <mferror.h>
9
+ #include <mfidl.h>
10
+ #include <mftransform.h>
11
+ #include <strmif.h>
12
+ #include <codecapi.h>
13
+ #include <wrl/client.h>
14
+ // clang-format on
15
+
16
+ #include <algorithm>
17
+ #include <array>
18
+ #include <cmath>
19
+ #include <cstdint>
20
+ #include <cstdio>
21
+ #include <cstring>
22
+ #include <string>
23
+ #include <utility>
24
+
25
+ extern "C" {
26
+ #include <libavutil/frame.h>
27
+ #include <libavutil/log.h>
28
+ }
29
+
30
+ namespace media_proxy {
31
+
32
+ namespace {
33
+
34
+ using Microsoft::WRL::ComPtr;
35
+
36
+ constexpr int64_t kHundredNanosPerMicro = 10;
37
+ constexpr int64_t kHundredNanosPerSecond = 10'000'000;
38
+ constexpr double kRateScale = 1000;
39
+ constexpr size_t kLongStartCode = 4;
40
+ constexpr size_t kShortStartCode = 3;
41
+ constexpr uint8_t kNalTypeMask = 0x1f;
42
+ constexpr uint8_t kNalIdr = 5;
43
+ constexpr uint8_t kNalSps = 7;
44
+ constexpr uint8_t kNalPps = 8;
45
+ constexpr uint8_t kNalAud = 9;
46
+ constexpr size_t kHexDigits = 8;
47
+
48
+ PipelineError Failure(const char* what, HRESULT hr) {
49
+ std::array<char, kHexDigits + 1> code{};
50
+ std::snprintf(code.data(), code.size(), "%08lX", static_cast<unsigned long>(hr));
51
+ return {"MediaProxyError", std::string("Media Foundation ") + what + " failed (HRESULT 0x" + code.data() + ")"};
52
+ }
53
+
54
+ void Check(HRESULT hr, const char* what) {
55
+ if (FAILED(hr)) {
56
+ throw Failure(what, hr);
57
+ }
58
+ }
59
+
60
+ // COM is initialised per call: the encoder is created and destroyed on whichever thread the pipeline runs on.
61
+ class ComScope {
62
+ public:
63
+ ComScope() : initialized_(SUCCEEDED(CoInitializeEx(nullptr, COINIT_MULTITHREADED))) {}
64
+ ~ComScope() {
65
+ if (initialized_) {
66
+ CoUninitialize();
67
+ }
68
+ }
69
+ ComScope(const ComScope&) = delete;
70
+ ComScope& operator=(const ComScope&) = delete;
71
+
72
+ private:
73
+ bool initialized_;
74
+ };
75
+
76
+ struct NalRange {
77
+ const uint8_t* data;
78
+ size_t size;
79
+ };
80
+
81
+ // Splits an Annex-B stream (3- or 4-byte start codes) into NAL units without their start codes.
82
+ std::vector<NalRange> SplitNals(const uint8_t* bytes, size_t size) {
83
+ std::vector<std::pair<size_t, size_t>> starts; // payload offset, start code length
84
+ for (size_t i = 0; i + kShortStartCode <= size;) {
85
+ if (bytes[i] == 0 && bytes[i + 1] == 0 && bytes[i + 2] == 1) {
86
+ const bool longCode = i > 0 && bytes[i - 1] == 0;
87
+ starts.emplace_back(i + kShortStartCode, longCode ? kLongStartCode : kShortStartCode);
88
+ i += kShortStartCode;
89
+ } else {
90
+ ++i;
91
+ }
92
+ }
93
+ std::vector<NalRange> nals;
94
+ for (size_t n = 0; n < starts.size(); ++n) {
95
+ const size_t begin = starts[n].first;
96
+ size_t end = size;
97
+ if (n + 1 < starts.size()) {
98
+ end = starts[n + 1].first - starts[n + 1].second;
99
+ }
100
+ if (end > begin) {
101
+ nals.push_back({bytes + begin, end - begin});
102
+ }
103
+ }
104
+ return nals;
105
+ }
106
+
107
+ void AppendNal(std::vector<uint8_t>& out, const NalRange& nal) {
108
+ out.insert(out.end(), kAnnexBStartCode.begin(), kAnnexBStartCode.end());
109
+ out.insert(out.end(), nal.data, nal.data + nal.size);
110
+ }
111
+
112
+ VARIANT UInt32Variant(ULONG value) {
113
+ VARIANT var;
114
+ VariantInit(&var);
115
+ var.vt = VT_UI4;
116
+ var.ulVal = value;
117
+ return var;
118
+ }
119
+
120
+ VARIANT BoolVariant(bool value) {
121
+ VARIANT var;
122
+ VariantInit(&var);
123
+ var.vt = VT_BOOL;
124
+ var.boolVal = value ? VARIANT_TRUE : VARIANT_FALSE;
125
+ return var;
126
+ }
127
+
128
+ class MediaFoundationEncoder final : public H264Encoder {
129
+ public:
130
+ MediaFoundationEncoder(const H264Params& params, H264Encoder::Sink sink) : params_(params), sink_(std::move(sink)) {
131
+ const HRESULT started = MFStartup(MF_VERSION, MFSTARTUP_LITE);
132
+ if (FAILED(started)) {
133
+ throw PipelineError{kEncoderUnavailableError, Failure("startup", started).message};
134
+ }
135
+ try {
136
+ const ComScope com;
137
+ Open();
138
+ } catch (const PipelineError& err) { // whatever fails while opening means this encoder can't serve the request
139
+ Release();
140
+ throw PipelineError{kEncoderUnavailableError, err.message};
141
+ } catch (...) {
142
+ Release();
143
+ throw;
144
+ }
145
+ }
146
+
147
+ ~MediaFoundationEncoder() override { Release(); }
148
+ MediaFoundationEncoder(const MediaFoundationEncoder&) = delete;
149
+ MediaFoundationEncoder& operator=(const MediaFoundationEncoder&) = delete;
150
+
151
+ void Encode(const AVFrame& nv12, bool forceKeyFrame) override {
152
+ const ComScope com;
153
+ if (forceKeyFrame) {
154
+ VARIANT on = UInt32Variant(1);
155
+ Check(codecApi_->SetValue(&CODECAPI_AVEncVideoForceKeyFrame, &on), "forcing a key frame");
156
+ }
157
+ // The MFT needs increasing timestamps even if the source timeline jumped back.
158
+ lastPts_ = std::max(nv12.pts, lastPts_ + 1);
159
+ const ComPtr<IMFSample> sample = MakeSample(nv12, lastPts_);
160
+ HRESULT hr = transform_->ProcessInput(0, sample.Get(), 0);
161
+ if (hr == MF_E_NOTACCEPTING) {
162
+ PullOutput();
163
+ hr = transform_->ProcessInput(0, sample.Get(), 0);
164
+ }
165
+ Check(hr, "input");
166
+ PullOutput();
167
+ }
168
+
169
+ void Flush() override {
170
+ const ComScope com;
171
+ Check(transform_->ProcessMessage(MFT_MESSAGE_COMMAND_DRAIN, 0), "drain");
172
+ PullOutput();
173
+ }
174
+
175
+ private:
176
+ void Open() {
177
+ ComPtr<IMFTransform> transform = CreateTransform();
178
+ transform_ = transform;
179
+ Check(transform_.As(&codecApi_), "ICodecAPI");
180
+ Configure();
181
+ SetTypes();
182
+ Check(transform_->ProcessMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0), "begin streaming");
183
+ Check(transform_->ProcessMessage(MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0), "start of stream");
184
+ RefreshOutput();
185
+ }
186
+
187
+ // Re-reads everything that depends on the output type; the cached sample and buffer are rebuilt on demand.
188
+ void RefreshOutput() {
189
+ MFT_OUTPUT_STREAM_INFO info{};
190
+ Check(transform_->GetOutputStreamInfo(0, &info), "output stream info");
191
+ providesSamples_ =
192
+ (info.dwFlags & (MFT_OUTPUT_STREAM_PROVIDES_SAMPLES | MFT_OUTPUT_STREAM_CAN_PROVIDE_SAMPLES)) != 0;
193
+ outputSize_ = info.cbSize;
194
+ outputBuffer_.Reset();
195
+ outputSample_.Reset();
196
+ ReadSequenceHeader();
197
+ }
198
+
199
+ // Hardware MFTs are asynchronous (event driven) and left to the vendor encoders; this takes the first synchronous
200
+ // one.
201
+ static ComPtr<IMFTransform> CreateTransform() {
202
+ const MFT_REGISTER_TYPE_INFO input{MFMediaType_Video, MFVideoFormat_NV12};
203
+ const MFT_REGISTER_TYPE_INFO output{MFMediaType_Video, MFVideoFormat_H264};
204
+ IMFActivate** activates = nullptr;
205
+ UINT32 count = 0;
206
+ const HRESULT enumerated =
207
+ MFTEnumEx(MFT_CATEGORY_VIDEO_ENCODER, MFT_ENUM_FLAG_SYNCMFT | MFT_ENUM_FLAG_SORTANDFILTER, &input, &output,
208
+ &activates, &count);
209
+ if (FAILED(enumerated) || count == 0) {
210
+ throw PipelineError{kEncoderUnavailableError, "No Media Foundation H.264 encoder is installed"};
211
+ }
212
+ ComPtr<IMFTransform> transform;
213
+ for (UINT32 i = 0; i < count && !transform; ++i) {
214
+ activates[i]->ActivateObject(IID_PPV_ARGS(&transform));
215
+ }
216
+ for (UINT32 i = 0; i < count; ++i) {
217
+ activates[i]->Release();
218
+ }
219
+ CoTaskMemFree(activates);
220
+ if (!transform) {
221
+ throw PipelineError{kEncoderUnavailableError, "No Media Foundation H.264 encoder could be created"};
222
+ }
223
+ return transform;
224
+ }
225
+
226
+ // Settings that shape the promised output must be accepted; the rest are tuning (rejections are logged).
227
+ void Configure() {
228
+ const auto set = [&](const GUID& key, const char* name, VARIANT value, bool required) {
229
+ const HRESULT hr = codecApi_->SetValue(&key, &value);
230
+ if (SUCCEEDED(hr)) {
231
+ return;
232
+ }
233
+ if (required) {
234
+ throw PipelineError{kEncoderUnavailableError, Failure((std::string("setting ") + name).c_str(), hr).message};
235
+ }
236
+ av_log(nullptr, AV_LOG_WARNING, "Media Foundation rejected %s (HRESULT 0x%lx)\n", name,
237
+ static_cast<unsigned long>(hr));
238
+ };
239
+ set(CODECAPI_AVLowLatencyMode, "AVLowLatencyMode", BoolVariant(true), false);
240
+ set(CODECAPI_AVEncCommonRateControlMode, "AVEncCommonRateControlMode",
241
+ UInt32Variant(eAVEncCommonRateControlMode_CBR), false);
242
+ set(CODECAPI_AVEncMPVDefaultBPictureCount, "AVEncMPVDefaultBPictureCount", UInt32Variant(0), true);
243
+ set(CODECAPI_AVEncCommonMeanBitRate, "AVEncCommonMeanBitRate",
244
+ UInt32Variant(static_cast<ULONG>(std::min<int64_t>(params_.bitrate, UINT32_MAX))), true);
245
+ set(CODECAPI_AVEncMPVGOPSize, "AVEncMPVGOPSize", UInt32Variant(static_cast<ULONG>(params_.gopFrames)), true);
246
+ }
247
+
248
+ ComPtr<IMFMediaType> MakeType(const GUID& subtype) const {
249
+ ComPtr<IMFMediaType> type;
250
+ Check(MFCreateMediaType(&type), "media type");
251
+ type->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
252
+ type->SetGUID(MF_MT_SUBTYPE, subtype);
253
+ MFSetAttributeSize(type.Get(), MF_MT_FRAME_SIZE, static_cast<UINT32>(params_.width),
254
+ static_cast<UINT32>(params_.height));
255
+ MFSetAttributeRatio(type.Get(), MF_MT_FRAME_RATE, static_cast<UINT32>(std::lround(params_.fps * kRateScale)),
256
+ static_cast<UINT32>(kRateScale));
257
+ MFSetAttributeRatio(type.Get(), MF_MT_PIXEL_ASPECT_RATIO, 1, 1);
258
+ type->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive);
259
+ type->SetUINT32(MF_MT_VIDEO_PRIMARIES, MFVideoPrimaries_BT709);
260
+ type->SetUINT32(MF_MT_TRANSFER_FUNCTION, MFVideoTransFunc_709);
261
+ type->SetUINT32(MF_MT_YUV_MATRIX, MFVideoTransferMatrix_BT709);
262
+ type->SetUINT32(MF_MT_VIDEO_NOMINAL_RANGE, MFNominalRange_16_235);
263
+ return type;
264
+ }
265
+
266
+ void SetTypes() {
267
+ const ComPtr<IMFMediaType> output = MakeType(MFVideoFormat_H264);
268
+ output->SetUINT32(MF_MT_AVG_BITRATE, static_cast<UINT32>(std::min<int64_t>(params_.bitrate, UINT32_MAX)));
269
+ output->SetUINT32(MF_MT_MPEG2_PROFILE, eAVEncH264VProfile_High);
270
+ Check(transform_->SetOutputType(0, output.Get(), 0), "output type");
271
+ const ComPtr<IMFMediaType> input = MakeType(MFVideoFormat_NV12);
272
+ Check(transform_->SetInputType(0, input.Get(), 0), "input type");
273
+ }
274
+
275
+ // SPS/PPS from the negotiated output type, for key frames the MFT emits without them.
276
+ void ReadSequenceHeader() {
277
+ sequenceHeader_.clear();
278
+ ComPtr<IMFMediaType> type;
279
+ if (FAILED(transform_->GetOutputCurrentType(0, &type))) {
280
+ return;
281
+ }
282
+ UINT32 size = 0;
283
+ if (FAILED(type->GetBlobSize(MF_MT_MPEG_SEQUENCE_HEADER, &size)) || size == 0) {
284
+ return;
285
+ }
286
+ std::vector<uint8_t> blob(size);
287
+ if (FAILED(type->GetBlob(MF_MT_MPEG_SEQUENCE_HEADER, blob.data(), size, nullptr))) {
288
+ return;
289
+ }
290
+ for (const NalRange& nal : SplitNals(blob.data(), blob.size())) {
291
+ const uint8_t type = nal.data[0] & kNalTypeMask;
292
+ if (type == kNalSps || type == kNalPps) {
293
+ AppendNal(sequenceHeader_, nal);
294
+ }
295
+ }
296
+ }
297
+
298
+ ComPtr<IMFSample> MakeSample(const AVFrame& nv12, int64_t pts) const {
299
+ const auto width = static_cast<size_t>(params_.width);
300
+ const auto height = static_cast<size_t>(params_.height);
301
+ const size_t lumaSize = width * height;
302
+ ComPtr<IMFMediaBuffer> buffer;
303
+ Check(MFCreateMemoryBuffer(static_cast<DWORD>(lumaSize + (lumaSize / 2)), &buffer), "input buffer");
304
+ BYTE* dst = nullptr;
305
+ Check(buffer->Lock(&dst, nullptr, nullptr), "input buffer lock");
306
+ for (size_t row = 0; row < height; ++row) {
307
+ std::memcpy(dst + (row * width), nv12.data[0] + (row * static_cast<size_t>(nv12.linesize[0])), width);
308
+ }
309
+ for (size_t row = 0; row < height / 2; ++row) {
310
+ std::memcpy(dst + lumaSize + (row * width), nv12.data[1] + (row * static_cast<size_t>(nv12.linesize[1])), width);
311
+ }
312
+ buffer->Unlock();
313
+ Check(buffer->SetCurrentLength(static_cast<DWORD>(lumaSize + (lumaSize / 2))), "input buffer length");
314
+ ComPtr<IMFSample> sample;
315
+ Check(MFCreateSample(&sample), "input sample");
316
+ Check(sample->AddBuffer(buffer.Get()), "input sample buffer");
317
+ Check(sample->SetSampleTime(pts * kHundredNanosPerMicro), "sample time");
318
+ Check(sample->SetSampleDuration(static_cast<LONGLONG>(static_cast<double>(kHundredNanosPerSecond) / params_.fps)),
319
+ "sample duration");
320
+ return sample;
321
+ }
322
+
323
+ // Collects every unit the MFT has ready; "needs more input" ends the loop.
324
+ void PullOutput() {
325
+ for (;;) {
326
+ MFT_OUTPUT_DATA_BUFFER out{};
327
+ if (!providesSamples_) {
328
+ EnsureOutputSample();
329
+ out.pSample = outputSample_.Get();
330
+ }
331
+ DWORD status = 0;
332
+ const HRESULT hr = transform_->ProcessOutput(0, 1, &out, &status);
333
+ if (out.pEvents != nullptr) {
334
+ out.pEvents->Release();
335
+ }
336
+ ComPtr<IMFSample> provided;
337
+ if (providesSamples_) {
338
+ provided.Attach(out.pSample);
339
+ }
340
+ if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) {
341
+ return;
342
+ }
343
+ if (hr == MF_E_TRANSFORM_STREAM_CHANGE) {
344
+ ComPtr<IMFMediaType> type;
345
+ Check(transform_->GetOutputAvailableType(0, 0, &type), "output type renegotiation");
346
+ Check(transform_->SetOutputType(0, type.Get(), 0), "output type renegotiation");
347
+ RefreshOutput();
348
+ continue;
349
+ }
350
+ Check(hr, "output");
351
+ Deliver(providesSamples_ ? provided.Get() : outputSample_.Get());
352
+ }
353
+ }
354
+
355
+ void EnsureOutputSample() {
356
+ if (outputSample_) {
357
+ outputSample_->GetBufferByIndex(0, &outputBuffer_);
358
+ outputBuffer_->SetCurrentLength(0);
359
+ return;
360
+ }
361
+ Check(MFCreateSample(&outputSample_), "output sample");
362
+ Check(MFCreateMemoryBuffer(std::max<DWORD>(outputSize_, 1), &outputBuffer_), "output buffer");
363
+ Check(outputSample_->AddBuffer(outputBuffer_.Get()), "output sample buffer");
364
+ }
365
+
366
+ void Deliver(IMFSample* sample) {
367
+ ComPtr<IMFMediaBuffer> contiguous;
368
+ Check(sample->ConvertToContiguousBuffer(&contiguous), "output buffer");
369
+ BYTE* bytes = nullptr;
370
+ DWORD length = 0;
371
+ Check(contiguous->Lock(&bytes, nullptr, &length), "output buffer lock");
372
+ EncodedUnit unit;
373
+ try {
374
+ unit.data = ToAnnexB(bytes, length, unit.isKeyFrame);
375
+ } catch (...) {
376
+ contiguous->Unlock();
377
+ throw;
378
+ }
379
+ contiguous->Unlock();
380
+ LONGLONG time = 0;
381
+ unit.timestampMicros = SUCCEEDED(sample->GetSampleTime(&time)) ? time / kHundredNanosPerMicro : lastPts_;
382
+ sink_(std::move(unit));
383
+ }
384
+
385
+ // Normalises to 4-byte start codes without access unit delimiters, with SPS/PPS ahead of every IDR.
386
+ std::vector<uint8_t> ToAnnexB(const uint8_t* bytes, size_t size, bool& isKeyFrame) const {
387
+ const std::vector<NalRange> nals = SplitNals(bytes, size);
388
+ if (nals.empty()) {
389
+ throw PipelineError{"MediaProxyError", "Media Foundation produced a sample without NAL units"};
390
+ }
391
+ bool hasSps = false;
392
+ for (const NalRange& nal : nals) {
393
+ const uint8_t type = nal.data[0] & kNalTypeMask;
394
+ isKeyFrame = isKeyFrame || type == kNalIdr;
395
+ hasSps = hasSps || type == kNalSps;
396
+ }
397
+ std::vector<uint8_t> out;
398
+ out.reserve(size + sequenceHeader_.size() + kLongStartCode);
399
+ if (isKeyFrame && !hasSps) {
400
+ out = sequenceHeader_;
401
+ }
402
+ for (const NalRange& nal : nals) {
403
+ if ((nal.data[0] & kNalTypeMask) != kNalAud) {
404
+ AppendNal(out, nal);
405
+ }
406
+ }
407
+ return out;
408
+ }
409
+
410
+ void Release() {
411
+ {
412
+ const ComScope com;
413
+ outputBuffer_.Reset();
414
+ outputSample_.Reset();
415
+ codecApi_.Reset();
416
+ if (transform_) {
417
+ transform_->ProcessMessage(MFT_MESSAGE_NOTIFY_END_OF_STREAM, 0);
418
+ }
419
+ transform_.Reset();
420
+ }
421
+ MFShutdown();
422
+ }
423
+
424
+ H264Params params_;
425
+ H264Encoder::Sink sink_;
426
+ ComPtr<IMFTransform> transform_;
427
+ ComPtr<ICodecAPI> codecApi_;
428
+ ComPtr<IMFSample> outputSample_;
429
+ ComPtr<IMFMediaBuffer> outputBuffer_;
430
+ std::vector<uint8_t> sequenceHeader_;
431
+ DWORD outputSize_ = 0;
432
+ bool providesSamples_ = false;
433
+ int64_t lastPts_ = -1;
434
+ };
435
+
436
+ } // namespace
437
+
438
+ std::unique_ptr<H264Encoder> CreateMediaFoundationEncoder(const H264Params& params, const H264Encoder::Sink& sink) {
439
+ return std::make_unique<MediaFoundationEncoder>(params, sink);
440
+ }
441
+
442
+ } // 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
+ // Windows Media Foundation H.264 encoder MFT (synchronous ones: Microsoft's own, always present). FFmpeg's h264_mf
10
+ // buffers frames; calling the MFT directly returns each frame from the same Encode(). Throws PipelineError
11
+ // (MediaProxyEncoderUnavailableError) if no encoder opens.
12
+ std::unique_ptr<H264Encoder> CreateMediaFoundationEncoder(const H264Params& params, const H264Encoder::Sink& sink);
13
+
14
+ } // namespace media_proxy
@@ -0,0 +1,20 @@
1
+ #pragma once
2
+
3
+ #include <napi.h>
4
+
5
+ #include <string>
6
+
7
+ namespace media_proxy {
8
+
9
+ // A JS Error whose `name` is one of the typed errors in src/errors.ts, so wrapNativeError() can re-type it.
10
+ inline Napi::Error MakeTypedError(Napi::Env env, const char* name, const std::string& message) {
11
+ Napi::Error err = Napi::Error::New(env, message);
12
+ err.Set("name", Napi::String::New(env, name));
13
+ return err;
14
+ }
15
+
16
+ [[noreturn]] inline void ThrowTyped(Napi::Env env, const char* name, const std::string& message) {
17
+ throw MakeTypedError(env, name, message);
18
+ }
19
+
20
+ } // namespace media_proxy