Skip to content

Commit b72f4d5

Browse files
kanatcloudwebrtc
andauthored
fix: add WrappedVideoDecoderFactory.java (#18)
* fix: add WrappedVideoDecoderFactory.java. * fix compile for android. --------- Co-authored-by: cloudwebrtc <[email protected]>
1 parent 01ed14c commit b72f4d5

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

sdk/android/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ if (is_android) {
361361
sources = [
362362
"api/org/webrtc/DefaultVideoDecoderFactory.java",
363363
"api/org/webrtc/DefaultVideoEncoderFactory.java",
364+
"api/org/webrtc/WrappedVideoDecoderFactory.java",
364365
]
365366

366367
deps = [
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2023 LiveKit
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+
package org.webrtc;
18+
19+
import android.media.MediaCodecInfo;
20+
import androidx.annotation.Nullable;
21+
22+
import java.util.Arrays;
23+
import java.util.LinkedHashSet;
24+
25+
public class WrappedVideoDecoderFactory implements VideoDecoderFactory {
26+
public WrappedVideoDecoderFactory(@Nullable EglBase.Context eglContext) {
27+
this.hardwareVideoDecoderFactory = new HardwareVideoDecoderFactory(eglContext);
28+
this.platformSoftwareVideoDecoderFactory = new PlatformSoftwareVideoDecoderFactory(eglContext);
29+
}
30+
31+
private final VideoDecoderFactory hardwareVideoDecoderFactory;
32+
private final VideoDecoderFactory hardwareVideoDecoderFactoryWithoutEglContext = new HardwareVideoDecoderFactory(null) ;
33+
private final VideoDecoderFactory softwareVideoDecoderFactory = new SoftwareVideoDecoderFactory();
34+
@Nullable
35+
private final VideoDecoderFactory platformSoftwareVideoDecoderFactory;
36+
37+
@Override
38+
public VideoDecoder createDecoder(VideoCodecInfo codecType) {
39+
VideoDecoder softwareDecoder = this.softwareVideoDecoderFactory.createDecoder(codecType);
40+
VideoDecoder hardwareDecoder = this.hardwareVideoDecoderFactory.createDecoder(codecType);
41+
if (softwareDecoder == null && this.platformSoftwareVideoDecoderFactory != null) {
42+
softwareDecoder = this.platformSoftwareVideoDecoderFactory.createDecoder(codecType);
43+
}
44+
45+
if(hardwareDecoder != null && disableSurfaceTextureFrame(hardwareDecoder.getImplementationName())) {
46+
hardwareDecoder.release();
47+
hardwareDecoder = this.hardwareVideoDecoderFactoryWithoutEglContext.createDecoder(codecType);
48+
}
49+
50+
if (hardwareDecoder != null && softwareDecoder != null) {
51+
return new VideoDecoderFallback(softwareDecoder, hardwareDecoder);
52+
} else {
53+
return hardwareDecoder != null ? hardwareDecoder : softwareDecoder;
54+
}
55+
}
56+
57+
private boolean disableSurfaceTextureFrame(String name) {
58+
if (name.startsWith("OMX.qcom.") || name.startsWith("OMX.hisi.")) {
59+
return true;
60+
}
61+
return false;
62+
}
63+
64+
@Override
65+
public VideoCodecInfo[] getSupportedCodecs() {
66+
LinkedHashSet<VideoCodecInfo> supportedCodecInfos = new LinkedHashSet();
67+
supportedCodecInfos.addAll(Arrays.asList(this.softwareVideoDecoderFactory.getSupportedCodecs()));
68+
supportedCodecInfos.addAll(Arrays.asList(this.hardwareVideoDecoderFactory.getSupportedCodecs()));
69+
if (this.platformSoftwareVideoDecoderFactory != null) {
70+
supportedCodecInfos.addAll(Arrays.asList(this.platformSoftwareVideoDecoderFactory.getSupportedCodecs()));
71+
}
72+
73+
return (VideoCodecInfo[])supportedCodecInfos.toArray(new VideoCodecInfo[supportedCodecInfos.size()]);
74+
}
75+
}

0 commit comments

Comments
 (0)