Skip to content

Commit

Permalink
fix: Fix flutter texture rendering downscale on Android (#1696)
Browse files Browse the repository at this point in the history
  • Loading branch information
littleGnAl committed Apr 11, 2024
1 parent af2605f commit eb8b74a
Show file tree
Hide file tree
Showing 7 changed files with 348 additions and 123 deletions.
12 changes: 10 additions & 2 deletions android/src/main/cpp/iris_rtc_rendering_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ class Texture2DRendering final : public RenderingOp {
CHECK_GL_ERROR()
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
CHECK_GL_ERROR()
glViewport(0, 0, video_frame->width, video_frame->height);
CHECK_GL_ERROR()

// Bind 2D texture
glActiveTexture(GL_TEXTURE0);
Expand Down Expand Up @@ -409,6 +411,8 @@ class OESTextureRendering final : public RenderingOp {
CHECK_GL_ERROR()
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
CHECK_GL_ERROR()
glViewport(0, 0, video_frame->width, video_frame->height);
CHECK_GL_ERROR()

// Bind external oes texture
glActiveTexture(GL_TEXTURE0);
Expand Down Expand Up @@ -528,6 +532,8 @@ class YUVRendering final : public RenderingOp {
CHECK_GL_ERROR()
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
CHECK_GL_ERROR()
glViewport(0, 0, width, height);
CHECK_GL_ERROR()

glEnableVertexAttribArray(aPositionLoc_);
CHECK_GL_ERROR()
Expand All @@ -538,8 +544,8 @@ class YUVRendering final : public RenderingOp {

// Adjust the tex coords to avoid green edge issue
float sFactor = 1.0f;
if (video_frame->width != video_frame->yStride) {
sFactor = (float) video_frame->width / (float) video_frame->yStride - 0.02f;
if (width != yStride) {
sFactor = (float) width / (float) yStride - 0.02f;
}

float fragment[] = {sFactor, 0.0f, 0.0f, 0.0f, sFactor, 1.0f, 0.0f, 1.0f};
Expand Down Expand Up @@ -704,6 +710,8 @@ class NativeTextureRenderer final
strcpy(config.channelId, "");
}
config.video_view_setup_mode = video_view_setup_mode;
config.observed_frame_position = agora::media::base::VIDEO_MODULE_POSITION::POSITION_POST_CAPTURER
| agora::media::base::VIDEO_MODULE_POSITION::POSITION_PRE_RENDERER;

if (iris_rtc_rendering_) {
delegate_id_ =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ typedef struct IrisRtcVideoFrameConfig {
int video_view_setup_mode;

/// int value of agora::media::base::VIDEO_MODULE_POSITION.
/// Default value is
/// Default value is
/// `agora::media::base::VIDEO_MODULE_POSITION::POSITION_PRE_ENCODER | agora::media::base::VIDEO_MODULE_POSITION::POSITION_PRE_RENDERER`
uint32_t observed_frame_position;
} IrisRtcVideoFrameConfig;
Expand Down
61 changes: 6 additions & 55 deletions android/src/main/java/io/agora/agora_rtc_ng/TextureRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
import android.os.Looper;
import android.view.Surface;

import androidx.annotation.NonNull;

import java.util.HashMap;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.view.TextureRegistry;

Expand All @@ -23,9 +20,6 @@ public class TextureRenderer {
private SurfaceTexture flutterSurfaceTexture;
private Surface renderSurface;

int width = 0;
int height = 0;

public TextureRenderer(
TextureRegistry textureRegistry,
BinaryMessenger binaryMessenger,
Expand All @@ -42,33 +36,6 @@ public TextureRenderer(
this.renderSurface = new Surface(this.flutterSurfaceTexture);

this.methodChannel = new MethodChannel(binaryMessenger, "agora_rtc_engine/texture_render_" + flutterTexture.id());
this.methodChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
if (call.method.equals("setSizeNative")) {
if (call.arguments() == null) {
result.success(false);
return;
}

int width = 0;
int height = 0;
if (call.hasArgument("width")) {
width = call.argument("width");
}
if (call.hasArgument("height")) {
height = call.argument("height");
}

startRendering(width, height);

result.success(true);
return;
}

result.notImplemented();
}
});

this.irisRenderer = new IrisRenderer(
irisRtcRenderingHandle,
Expand All @@ -79,6 +46,11 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
this.irisRenderer.setCallback(new IrisRenderer.Callback() {
@Override
public void onSizeChanged(int width, int height) {
final SurfaceTexture st = TextureRenderer.this.flutterSurfaceTexture;
if (null != st) {
st.setDefaultBufferSize(width, height);
}

handler.post(() -> {
methodChannel.invokeMethod(
"onSizeChanged",
Expand All @@ -89,29 +61,8 @@ public void onSizeChanged(int width, int height) {
});
}
});
}

private void startRendering(int width, int height) {
if (width == 0 && height == 0) {
return;
}

final SurfaceTexture st = TextureRenderer.this.flutterSurfaceTexture;
if (null == st) {
return;
}

if (this.width != width || this.height != height) {
st.setDefaultBufferSize(width, height);

// Only call `irisRenderer.startRenderingToSurface` in the first time.
if (this.width == 0 && this.height == 0) {
this.irisRenderer.startRenderingToSurface(renderSurface);
}

this.width = width;
this.height = height;
}
this.irisRenderer.startRenderingToSurface(renderSurface);
}

public long getTextureId() {
Expand Down
4 changes: 4 additions & 0 deletions example/lib/components/config_override.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ class ExampleConfigOverride {
void set(String name, String value) {
_overridedConfig[name] = value;
}

/// Internal testing flag
bool get isInternalTesting =>
const bool.fromEnvironment('INTERNAL_TESTING', defaultValue: false);
}
8 changes: 8 additions & 0 deletions example/lib/examples/basic/index.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:agora_rtc_engine_example/components/config_override.dart';
import 'package:agora_rtc_engine_example/examples/basic/join_channel_video/flutter_texture_android_internal_test.dart';
import 'package:agora_rtc_engine_example/examples/basic/string_uid/string_uid.dart';

import 'join_channel_audio/join_channel_audio.dart';
Expand All @@ -9,5 +11,11 @@ final basic = [
{'name': 'Basic'},
{'name': 'JoinChannelAudio', 'widget': const JoinChannelAudio()},
{'name': 'JoinChannelVideo', 'widget': const JoinChannelVideo()},
if (defaultTargetPlatform == TargetPlatform.android &&
ExampleConfigOverride().isInternalTesting)
{
'name': 'FlutterTextureAndroidTest',
'widget': const FlutterTextureAndroidTest()
},
{'name': 'StringUid', 'widget': const StringUid()}
];
Loading

0 comments on commit eb8b74a

Please sign in to comment.