Skip to content

Commit 3b4fbf0

Browse files
authored
feat: add a hardware video decoder factory, with Media Foundation on Windows (#315)
* feat: add a hardware video decoder factory, with Media Foundation on Windows HardwareVideoDecoderFactory puts the decoders of the GPU in front of the built-in ones where the platform has them, the way HardwareVideoEncoderFactory does for encoders. DefaultVideoDecoderFactory, and a PeerConnectionFactory without a decoder factory, decode as before. On Windows, H.264 and AV1 are decoded by the Media Foundation decoders of Windows on a Direct3D 11 device, so through DXVA on the GPU. A codec is offered only where the GPU has its DXVA decode profile and Windows has a decoder that uses Direct3D 11 (the AV1 Video Extension for AV1). The decoders run synchronously in low-latency mode; each decoded frame is copied from its texture into a staging texture, read back and converted from NV12 to I420, the size taken from the display aperture. A decoder whose output is not in GPU memory decodes in software, which WebRTC's own decoders do better, so it gives up then, as on any failure. FallbackVideoDecoder switches to the software decoder when the hardware one fails to configure or gives up while decoding; the software decoder starts with the frame at hand, and the receiver asks for a key frame if it cannot decode it. The hardware decoders offer the formats the software decoders offer, so negotiation does not change. Linux has no hardware decoders yet; on macOS both factories use VideoToolbox. * test: cover H.264 through VideoToolbox on macOS
1 parent e7fcc74 commit 3b4fbf0

26 files changed

Lines changed: 1855 additions & 10 deletions

‎docs/guide/advanced/video-codecs.md‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,27 @@ The hardware encoders take over H.264 Constrained Baseline and Baseline with pac
3838

3939
Which encoder a stream uses shows in the `encoderImplementation` statistic of its `outbound-rtp` stats, e.g. `NVENC (NVIDIA GeForce RTX 4070)`, `MediaFoundation (AMDav1Encoder)`, `VA-API (Intel iHD driver ...)`, `OpenH264` or `libaom`.
4040

41+
### Hardware Decoding
42+
43+
`HardwareVideoDecoderFactory` does the same for decoding:
44+
45+
```java
46+
PeerConnectionFactory factory = PeerConnectionFactory.builder()
47+
.setVideoEncoderFactory(new HardwareVideoEncoderFactory())
48+
.setVideoDecoderFactory(new HardwareVideoDecoderFactory())
49+
.build();
50+
```
51+
52+
| Platform | Hardware decoding |
53+
|---|---|
54+
| Windows | H.264 and AV1, on GPUs that decode them, through the Media Foundation decoders of Windows on Direct3D 11 (DXVA) |
55+
| Linux | Not yet; decoding is in software |
56+
| macOS | VideoToolbox, as with `DefaultVideoDecoderFactory` |
57+
58+
AV1 on Windows needs the *AV1 Video Extension*, which Windows 11 includes. Decoded frames are copied from GPU memory back to system memory, where WebRTC's frames are, so hardware decoding pays off mostly at high resolutions and with many streams; at low resolutions WebRTC's software decoders are about as cheap. A hardware decoder that fails, or turns out to decode in software, is replaced by the software decoder, which starts with the next key frame.
59+
60+
Which decoder a stream uses shows in the `decoderImplementation` statistic of its `inbound-rtp` stats, e.g. `MediaFoundation (Microsoft H264 Video Decoder MFT)` or `MediaFoundation (AV1VideoExtension)`.
61+
4162
### Native Codecs
4263

4364
The encoders and decoders these factories create are `NativeVideoEncoder`s and `NativeVideoDecoder`s. They are placeholders that make WebRTC create the built-in codec, which then runs entirely inside WebRTC, so their methods are not to be called from Java.
@@ -189,7 +210,7 @@ Whatever an encoder, decoder or factory method throws is logged and treated as `
189210
- `VideoEncoderFactory`, `VideoDecoderFactory` — create the codecs of a factory.
190211
- `VideoEncoder`, `VideoDecoder`, `EncodedImage` — codecs implemented in Java.
191212
- `DefaultVideoEncoderFactory`, `DefaultVideoDecoderFactory` — the built-in codecs.
192-
- `HardwareVideoEncoderFactory` — the built-in encoders, on the GPU where possible.
213+
- `HardwareVideoEncoderFactory`, `HardwareVideoDecoderFactory` — the built-in codecs, on the GPU where possible.
193214
- `RTCRtpTransceiver.setCodecPreferences()` — chooses among the negotiated codecs.
194215

195216
For the full API, see the JavaDoc of the `dev.onvoid.webrtc.media.video.codec` package.

‎webrtc-jni/src/main/cpp/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ elseif(LINUX)
135135
target_link_libraries(${PROJECT_NAME} ${CXX_LIBS})
136136
target_link_libraries(${PROJECT_NAME} dl)
137137
elseif(WIN32)
138-
target_link_libraries(${PROJECT_NAME} dwmapi.lib mf.lib mfreadwrite.lib mfplat.lib mfuuid.lib shcore.lib)
138+
target_link_libraries(${PROJECT_NAME} d3d11.lib dwmapi.lib dxguid.lib mf.lib mfreadwrite.lib mfplat.lib mfuuid.lib shcore.lib)
139139
endif()
140140

141141
install(TARGETS ${PROJECT_NAME}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2026 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <jni.h>
18+
/* Header for class dev_onvoid_webrtc_media_video_codec_HardwareVideoDecoderFactory */
19+
20+
#ifndef _Included_dev_onvoid_webrtc_media_video_codec_HardwareVideoDecoderFactory
21+
#define _Included_dev_onvoid_webrtc_media_video_codec_HardwareVideoDecoderFactory
22+
#ifdef __cplusplus
23+
extern "C" {
24+
#endif
25+
/*
26+
* Class: dev_onvoid_webrtc_media_video_codec_HardwareVideoDecoderFactory
27+
* Method: getSupportedCodecsInternal
28+
* Signature: ()[Ldev/onvoid/webrtc/media/video/codec/VideoCodecInfo;
29+
*/
30+
JNIEXPORT jobjectArray JNICALL Java_dev_onvoid_webrtc_media_video_codec_HardwareVideoDecoderFactory_getSupportedCodecsInternal
31+
(JNIEnv *, jclass);
32+
33+
#ifdef __cplusplus
34+
}
35+
#endif
36+
#endif

‎webrtc-jni/src/main/cpp/include/media/video/codec/DefaultVideoCodecFactories.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ namespace jni
3434
// macOS the default encoders already use VideoToolbox.
3535
std::unique_ptr<webrtc::VideoEncoderFactory> CreateHardwareVideoEncoderFactory();
3636
std::unique_ptr<webrtc::VideoDecoderFactory> CreateDefaultVideoDecoderFactory();
37+
38+
// The default decoders, with the decoders of the GPU in front of them
39+
// where the platform has them, falling back to the software ones. On
40+
// macOS the default decoders already use VideoToolbox.
41+
std::unique_ptr<webrtc::VideoDecoderFactory> CreateHardwareVideoDecoderFactory();
3742
}
3843

3944
#endif
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2026 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef JNI_WEBRTC_MEDIA_VIDEO_CODEC_FALLBACK_VIDEO_DECODER_H_
18+
#define JNI_WEBRTC_MEDIA_VIDEO_CODEC_FALLBACK_VIDEO_DECODER_H_
19+
20+
#include "api/video/encoded_image.h"
21+
#include "api/video_codecs/video_decoder.h"
22+
23+
#include <memory>
24+
#include <optional>
25+
26+
namespace jni
27+
{
28+
// Decodes with a hardware decoder, and switches to a software decoder of
29+
// the same codec when the hardware one fails to configure, or gives up
30+
// while decoding by returning WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE. The
31+
// switch is for good. The software decoder starts with the frame the
32+
// hardware one gave up on; if that is no key frame, it fails to decode
33+
// it, and the receiver asks the sender for a key frame.
34+
//
35+
// WebRTC has such a wrapper too, but it is not part of the WebRTC
36+
// library this library links.
37+
class FallbackVideoDecoder : public webrtc::VideoDecoder
38+
{
39+
public:
40+
FallbackVideoDecoder(std::unique_ptr<webrtc::VideoDecoder> hardware,
41+
std::unique_ptr<webrtc::VideoDecoder> software);
42+
~FallbackVideoDecoder() override = default;
43+
44+
using webrtc::VideoDecoder::Decode;
45+
46+
bool Configure(const Settings & settings) override;
47+
int32_t Decode(const webrtc::EncodedImage & image, int64_t renderTimeMs) override;
48+
int32_t RegisterDecodeCompleteCallback(webrtc::DecodedImageCallback * callback) override;
49+
int32_t Release() override;
50+
DecoderInfo GetDecoderInfo() const override;
51+
const char * ImplementationName() const override;
52+
53+
private:
54+
// Configures the software decoder with the settings the hardware
55+
// one was given. Returns whether it is ready.
56+
bool StartSoftware();
57+
58+
webrtc::VideoDecoder * Active() const;
59+
60+
private:
61+
const std::unique_ptr<webrtc::VideoDecoder> hardware;
62+
const std::unique_ptr<webrtc::VideoDecoder> software;
63+
64+
bool useSoftware;
65+
66+
std::optional<Settings> settings;
67+
webrtc::DecodedImageCallback * callback;
68+
};
69+
}
70+
71+
#endif
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2026 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef JNI_WEBRTC_MEDIA_VIDEO_CODEC_HARDWARE_VIDEO_DECODER_FACTORY_H_
18+
#define JNI_WEBRTC_MEDIA_VIDEO_CODEC_HARDWARE_VIDEO_DECODER_FACTORY_H_
19+
20+
#include "api/environment/environment.h"
21+
#include "api/video_codecs/sdp_video_format.h"
22+
#include "api/video_codecs/video_decoder.h"
23+
#include "api/video_codecs/video_decoder_factory.h"
24+
25+
#include <memory>
26+
#include <vector>
27+
28+
namespace jni
29+
{
30+
// Puts hardware decoders in front of software ones, the way
31+
// HardwareVideoEncoderFactory does with encoders: a codec is decoded by
32+
// the first hardware factory that has it, falling back to the next and
33+
// finally to the software decoder.
34+
class HardwareVideoDecoderFactory : public webrtc::VideoDecoderFactory
35+
{
36+
public:
37+
// The hardware factories are in order of preference.
38+
HardwareVideoDecoderFactory(std::vector<std::unique_ptr<webrtc::VideoDecoderFactory>> hardware,
39+
std::unique_ptr<webrtc::VideoDecoderFactory> software);
40+
~HardwareVideoDecoderFactory() override = default;
41+
42+
std::vector<webrtc::SdpVideoFormat> GetSupportedFormats() const override;
43+
std::unique_ptr<webrtc::VideoDecoder> Create(const webrtc::Environment & env,
44+
const webrtc::SdpVideoFormat & format) override;
45+
46+
private:
47+
const std::vector<std::unique_ptr<webrtc::VideoDecoderFactory>> hardware;
48+
const std::unique_ptr<webrtc::VideoDecoderFactory> software;
49+
};
50+
51+
// Returns the factories for the hardware decoders of the platform that are
52+
// available, in order of preference; none if the platform has none this
53+
// library supports, or no device that can decode. Defined per platform.
54+
std::vector<std::unique_ptr<webrtc::VideoDecoderFactory>> CreatePlatformHardwareVideoDecoderFactories();
55+
}
56+
57+
#endif

‎webrtc-jni/src/main/cpp/include/media/video/codec/VideoDecoderFactoryWrapper.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,15 @@ namespace jni
5959

6060
jclass nativeDecoderClass;
6161
jfieldID nativeDecoderCodecInfo;
62+
jfieldID nativeDecoderHardwareAcceleration;
6263
};
6364

6465
private:
6566
const JavaGlobalRef<jobject> factory;
6667
const std::shared_ptr<JavaVideoDecoderFactoryClass> javaClass;
68+
// The built-in decoders, and those with the GPU's in front.
6769
const std::unique_ptr<webrtc::VideoDecoderFactory> defaultFactory;
70+
const std::unique_ptr<webrtc::VideoDecoderFactory> hardwareFactory;
6871

6972
std::vector<webrtc::SdpVideoFormat> supportedFormats;
7073
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2026 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef JNI_WEBRTC_MEDIA_VIDEO_CODEC_MF_DECODER_UTILS_H_
18+
#define JNI_WEBRTC_MEDIA_VIDEO_CODEC_MF_DECODER_UTILS_H_
19+
20+
#include <d3d11.h>
21+
#include <mfapi.h>
22+
#include <mfidl.h>
23+
#include <mftransform.h>
24+
#include <wrl/client.h>
25+
26+
#include <string>
27+
#include <vector>
28+
29+
namespace jni
30+
{
31+
// Creates a Direct3D 11 device on the default adapter that video can be
32+
// decoded on, protected for use from several threads, as a decoder
33+
// transform and the thread reading its output both use it.
34+
HRESULT CreateVideoDevice(Microsoft::WRL::ComPtr<ID3D11Device> & device,
35+
Microsoft::WRL::ComPtr<ID3D11DeviceContext> & context);
36+
37+
// Whether the GPU of the device decodes the given DXVA profile, such as
38+
// D3D11_DECODER_PROFILE_H264_VLD_NOFGT, in hardware.
39+
bool SupportsDecoderProfile(ID3D11Device * device, const GUID & profile);
40+
41+
// Lists the decoder transforms for the given video format, such as
42+
// MFVideoFormat_H264, best first. These are the synchronous transforms of
43+
// Windows, which decode on the GPU through DXVA when given a Direct3D
44+
// device. Media Foundation has to be started.
45+
HRESULT EnumerateDecoders(const GUID & format, std::vector<Microsoft::WRL::ComPtr<IMFActivate>> & decoders);
46+
47+
// Activates the first of the decoders that can decode on a Direct3D 11
48+
// device, and gives it the device manager. The activation object of the
49+
// transform is returned with it, to shut it down with.
50+
HRESULT ActivateDirect3DDecoder(const std::vector<Microsoft::WRL::ComPtr<IMFActivate>> & decoders,
51+
IMFDXGIDeviceManager * manager, Microsoft::WRL::ComPtr<IMFActivate> & activate,
52+
Microsoft::WRL::ComPtr<IMFTransform> & transform);
53+
}
54+
55+
#endif

0 commit comments

Comments
 (0)