• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2017 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 package org.webrtc;
12 
13 import androidx.annotation.Nullable;
14 import java.util.Arrays;
15 import java.util.LinkedHashSet;
16 
17 /**
18  * Helper class that combines HW and SW decoders.
19  */
20 public class DefaultVideoDecoderFactory implements VideoDecoderFactory {
21   private final VideoDecoderFactory hardwareVideoDecoderFactory;
22   private final VideoDecoderFactory softwareVideoDecoderFactory = new SoftwareVideoDecoderFactory();
23   private final @Nullable VideoDecoderFactory platformSoftwareVideoDecoderFactory;
24 
25   /**
26    * Create decoder factory using default hardware decoder factory.
27    */
DefaultVideoDecoderFactory(@ullable EglBase.Context eglContext)28   public DefaultVideoDecoderFactory(@Nullable EglBase.Context eglContext) {
29     this.hardwareVideoDecoderFactory = new HardwareVideoDecoderFactory(eglContext);
30     this.platformSoftwareVideoDecoderFactory = new PlatformSoftwareVideoDecoderFactory(eglContext);
31   }
32 
33   /**
34    * Create decoder factory using explicit hardware decoder factory.
35    */
DefaultVideoDecoderFactory(VideoDecoderFactory hardwareVideoDecoderFactory)36   DefaultVideoDecoderFactory(VideoDecoderFactory hardwareVideoDecoderFactory) {
37     this.hardwareVideoDecoderFactory = hardwareVideoDecoderFactory;
38     this.platformSoftwareVideoDecoderFactory = null;
39   }
40 
41   @Override
createDecoder(VideoCodecInfo codecType)42   public @Nullable VideoDecoder createDecoder(VideoCodecInfo codecType) {
43     VideoDecoder softwareDecoder = softwareVideoDecoderFactory.createDecoder(codecType);
44     final VideoDecoder hardwareDecoder = hardwareVideoDecoderFactory.createDecoder(codecType);
45     if (softwareDecoder == null && platformSoftwareVideoDecoderFactory != null) {
46       softwareDecoder = platformSoftwareVideoDecoderFactory.createDecoder(codecType);
47     }
48     if (hardwareDecoder != null && softwareDecoder != null) {
49       // Both hardware and software supported, wrap it in a software fallback
50       return new VideoDecoderFallback(
51           /* fallback= */ softwareDecoder, /* primary= */ hardwareDecoder);
52     }
53     return hardwareDecoder != null ? hardwareDecoder : softwareDecoder;
54   }
55 
56   @Override
getSupportedCodecs()57   public VideoCodecInfo[] getSupportedCodecs() {
58     LinkedHashSet<VideoCodecInfo> supportedCodecInfos = new LinkedHashSet<VideoCodecInfo>();
59 
60     supportedCodecInfos.addAll(Arrays.asList(softwareVideoDecoderFactory.getSupportedCodecs()));
61     supportedCodecInfos.addAll(Arrays.asList(hardwareVideoDecoderFactory.getSupportedCodecs()));
62     if (platformSoftwareVideoDecoderFactory != null) {
63       supportedCodecInfos.addAll(
64           Arrays.asList(platformSoftwareVideoDecoderFactory.getSupportedCodecs()));
65     }
66 
67     return supportedCodecInfos.toArray(new VideoCodecInfo[supportedCodecInfos.size()]);
68   }
69 }
70