Skip to content

Commit

Permalink
Support for simulcast in Android SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
davidzhao committed Sep 10, 2021
1 parent 2d3ba08 commit aeee3a0
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
18 changes: 18 additions & 0 deletions sdk/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ if (is_android) {
sources = [
"api/org/webrtc/SoftwareVideoDecoderFactory.java",
"api/org/webrtc/SoftwareVideoEncoderFactory.java",
"api/org/webrtc/SimulcastVideoEncoder.java",
"api/org/webrtc/SimulcastVideoEncoderFactory.java",
]

deps = [
Expand Down Expand Up @@ -854,13 +856,29 @@ if (current_os == "linux" || is_android) {
]
}

rtc_library("simulcast_jni") {
visibility = [ "*" ]
allow_poison = [ "software_video_codecs" ]
sources = [
"src/jni/simulcast_video_encoder.cc",
"src/jni/simulcast_video_encoder.h"
]
deps = [
":base_jni",
":video_jni",
":native_api_codecs",
"../../media:rtc_simulcast_encoder_adapter"
]
}

rtc_library("swcodecs_jni") {
visibility = [ "*" ]
allow_poison = [ "software_video_codecs" ]
deps = [
":libaom_av1_jni",
":libvpx_vp8_jni",
":libvpx_vp9_jni",
":simulcast_jni",
]
}

Expand Down
28 changes: 28 additions & 0 deletions sdk/android/api/org/webrtc/SimulcastVideoEncoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.webrtc;

public class SimulcastVideoEncoder extends WrappedNativeVideoEncoder {

static native long nativeCreateEncoder(VideoEncoderFactory primary, VideoEncoderFactory fallback, VideoCodecInfo info);

VideoEncoderFactory primary;
VideoEncoderFactory fallback;
VideoCodecInfo info;

public SimulcastVideoEncoder(VideoEncoderFactory primary, VideoEncoderFactory fallback, VideoCodecInfo info) {
this.primary = primary;
this.fallback = fallback;
this.info = info;
}

@Override
public long createNativeVideoEncoder() {
return nativeCreateEncoder(primary, fallback, info);
}

@Override
public boolean isHardwareEncoder() {
return false;
}

}

43 changes: 43 additions & 0 deletions sdk/android/api/org/webrtc/SimulcastVideoEncoderFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2017 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/

package org.webrtc;

import android.support.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Arrays;

public class SimulcastVideoEncoderFactory implements VideoEncoderFactory {

VideoEncoderFactory primary;
VideoEncoderFactory fallback;

public SimulcastVideoEncoderFactory(VideoEncoderFactory primary, VideoEncoderFactory fallback) {
this.primary = primary;
this.fallback = fallback;
}

@Nullable
@Override
public VideoEncoder createEncoder(VideoCodecInfo info) {
return new SimulcastVideoEncoder(primary, fallback, info);
}

@Override
public VideoCodecInfo[] getSupportedCodecs() {
List<VideoCodecInfo> codecs = new ArrayList<VideoCodecInfo>();
codecs.addAll(Arrays.asList(primary.getSupportedCodecs()));
codecs.addAll(Arrays.asList(fallback.getSupportedCodecs()));
return codecs.toArray(new VideoCodecInfo[codecs.size()]);
}

}
34 changes: 34 additions & 0 deletions sdk/android/src/jni/simulcast_video_encoder.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <jni.h>

#include "sdk/android/src/jni/jni_helpers.h"
#include "sdk/android/src/jni/video_encoder_factory_wrapper.h"
#include "sdk/android/src/jni/video_codec_info.h"
#include "sdk/android/native_api/codecs/wrapper.h"
#include "media/engine/simulcast_encoder_adapter.h"
#include "rtc_base/logging.h"

using namespace webrtc;
using namespace webrtc::jni;

#ifdef __cplusplus
extern "C" {
#endif

// (VideoEncoderFactory primary, VideoEncoderFactory fallback, VideoCodecInfo info)
JNIEXPORT jlong JNICALL Java_org_webrtc_SimulcastVideoEncoder_nativeCreateEncoder(JNIEnv *env, jclass klass, jobject primary, jobject fallback, jobject info) {
RTC_LOG(LS_INFO) << "Create simulcast video encoder";
JavaParamRef<jobject> info_ref(info);
SdpVideoFormat format = VideoCodecInfoToSdpVideoFormat(env, info_ref);

// TODO: 影響は軽微だが、リークする可能性があるので将来的に修正したい
// https://github.com/shiguredo-webrtc-build/webrtc-build/pull/16#pullrequestreview-600675795
return NativeToJavaPointer(std::make_unique<SimulcastEncoderAdapter>(
JavaToNativeVideoEncoderFactory(env, primary).release(),
JavaToNativeVideoEncoderFactory(env, fallback).release(),
format).release());
}


#ifdef __cplusplus
}
#endif
22 changes: 22 additions & 0 deletions sdk/android/src/jni/simulcast_video_encoder.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 comments on commit aeee3a0

@voluntas
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may use our patches, but please use them in compliance with the Apache License 2.0 rules.

@cloudwebrtc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@voluntas Sorry for this, Thank you very much for creating these patches for WebRTC in your project. we will create README.md to create and use patches for your community, We can make any modifications to comply with the APL agreement.
@davidzhao, @hiroshihorie Maybe we should add a license for the patched Apache 2 in this repo, and add that the patch comes from https://github.com/shiguredo-webrtc-build/webrtc-build/tree/master/patches

Please sign in to comment.