|
1 | 1 | package com.example.trtc_api_example;
|
2 | 2 |
|
3 |
| -import android.content.Intent; |
4 |
| -import android.os.Bundle; |
5 | 3 | import io.flutter.embedding.android.FlutterActivity;
|
6 |
| -import io.flutter.embedding.engine.FlutterEngine; |
7 |
| -import io.flutter.plugin.common.MethodCall; |
8 |
| -import io.flutter.plugin.common.MethodChannel; |
9 |
| - |
10 |
| -import com.tencent.live.beauty.custom.ITXCustomBeautyProcesserFactory; |
11 |
| -import com.tencent.live.beauty.custom.ITXCustomBeautyProcesser; |
12 |
| -import static com.tencent.live.beauty.custom.TXCustomBeautyDef.TXCustomBeautyBufferType; |
13 |
| -import static com.tencent.live.beauty.custom.TXCustomBeautyDef.TXCustomBeautyPixelFormat; |
14 |
| -import static com.tencent.live.beauty.custom.TXCustomBeautyDef.TXCustomBeautyVideoFrame; |
15 |
| - |
16 |
| -import com.tencent.trtc.TRTCCloud; |
17 |
| -import com.tencent.trtc.TRTCCloudDef; |
18 |
| -import com.tencent.trtc.TRTCCloudListener; |
19 |
| -import com.tencent.trtc_demo.opengl.FrameBuffer; |
20 |
| -import com.tencent.trtc_demo.opengl.GpuImageGrayscaleFilter; |
21 |
| -import com.tencent.trtc_demo.opengl.OpenGlUtils; |
22 |
| -import com.tencent.trtc_demo.opengl.Rotation; |
23 |
| -import com.tencent.trtcplugin.TRTCCloudPlugin; |
24 |
| -import java.nio.FloatBuffer; |
25 |
| -import android.opengl.GLES20; |
26 |
| -import android.util.Log; |
27 |
| - |
28 |
| -import androidx.annotation.NonNull; |
29 | 4 |
|
30 | 5 | public class MainActivity extends FlutterActivity {
|
31 |
| - private static final String channelName = "TRCT_FLUTTER_EXAMPLE"; |
32 |
| - |
33 |
| - private MethodChannel channel; |
34 |
| - |
35 |
| - @Override |
36 |
| - public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) { |
37 |
| - super.configureFlutterEngine(flutterEngine); |
38 |
| - |
39 |
| - // Access the onCapturedAudioFrame interface Step 1: Use MethodChannel to turn on or off custom audio processing |
40 |
| - channel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), channelName); |
41 |
| - channel.setMethodCallHandler(((call, result) -> { |
42 |
| - switch (call.method) { |
43 |
| - case "enableTRTCAudioFrameDelegate": |
44 |
| - enableTRTCAudioFrameDelegate(call, result); |
45 |
| - break; |
46 |
| - case "disableTRTCAudioFrameDelegate": |
47 |
| - disableTRTCAudioFrameDelegate(call, result); |
48 |
| - break; |
49 |
| - default: |
50 |
| - break; |
51 |
| - } |
52 |
| - })); |
53 |
| - } |
54 |
| - |
55 |
| - // Access the onCapturedAudioFrame interface Step 2.1 : set AudioFrameDelegate |
56 |
| - void enableTRTCAudioFrameDelegate(MethodCall call, MethodChannel.Result result) { |
57 |
| - TRTCCloud.sharedInstance(getApplicationContext()).setAudioFrameListener(new AudioFrameListener()); |
58 |
| - result.success(""); |
59 |
| - } |
60 |
| - // Access the onCapturedAudioFrame interface Step 2.2 : remove AudioFrameDelegate |
61 |
| - void disableTRTCAudioFrameDelegate(MethodCall call, MethodChannel.Result result) { |
62 |
| - TRTCCloud.sharedInstance(getApplicationContext()).setAudioFrameListener(null); |
63 |
| - result.success(""); |
64 |
| - } |
65 |
| - |
66 |
| - @Override |
67 |
| - protected void onCreate(Bundle savedInstanceState) { |
68 |
| - super.onCreate(savedInstanceState); |
69 |
| - startService(new Intent(this, MediaService.class)); |
70 |
| - TUICallService.start(this); |
71 |
| - TRTCCloudPlugin.register(new TXThirdBeauty()); |
72 |
| - } |
73 |
| - |
74 |
| - @Override |
75 |
| - protected void onDestroy() { |
76 |
| - super.onDestroy(); |
77 |
| - TUICallService.stop(this); |
78 |
| - } |
79 |
| -} |
80 |
| - |
81 |
| -// Access the onCapturedAudioFrame interface Step 3: get audio frame & handle your business |
82 |
| -class AudioFrameListener implements TRTCCloudListener.TRTCAudioFrameListener { |
83 |
| - |
84 |
| - @Override |
85 |
| - public void onCapturedAudioFrame(TRTCCloudDef.TRTCAudioFrame trtcAudioFrame) { |
86 |
| - // TODO |
87 |
| - } |
88 |
| - |
89 |
| - @Override |
90 |
| - public void onLocalProcessedAudioFrame(TRTCCloudDef.TRTCAudioFrame trtcAudioFrame) { |
91 |
| - // TODO |
92 |
| - } |
93 |
| - |
94 |
| - @Override |
95 |
| - public void onRemoteUserAudioFrame(TRTCCloudDef.TRTCAudioFrame trtcAudioFrame, String s) { |
96 |
| - // TODO |
97 |
| - } |
98 |
| - |
99 |
| - @Override |
100 |
| - public void onMixedPlayAudioFrame(TRTCCloudDef.TRTCAudioFrame trtcAudioFrame) { |
101 |
| - // TODO |
102 |
| - } |
103 |
| - |
104 |
| - @Override |
105 |
| - public void onMixedAllAudioFrame(TRTCCloudDef.TRTCAudioFrame trtcAudioFrame) { |
106 |
| - // TODO |
107 |
| - } |
108 |
| - |
109 |
| - @Override |
110 |
| - public void onVoiceEarMonitorAudioFrame(TRTCCloudDef.TRTCAudioFrame trtcAudioFrame) { |
111 |
| - // TODO |
112 |
| - } |
113 |
| -} |
114 |
| - |
115 |
| -class TXThirdBeauty implements ITXCustomBeautyProcesserFactory { |
116 |
| - private BeautyProcessor customBeautyProcesser; |
117 |
| - @Override |
118 |
| - public ITXCustomBeautyProcesser createCustomBeautyProcesser() { |
119 |
| - customBeautyProcesser = new BeautyProcessor(); |
120 |
| - return customBeautyProcesser; |
121 |
| - } |
122 |
| - @Override |
123 |
| - public void destroyCustomBeautyProcesser() { |
124 |
| - if (null != customBeautyProcesser) { |
125 |
| - customBeautyProcesser.destroy(); |
126 |
| - customBeautyProcesser = null; |
127 |
| - } |
128 |
| - } |
129 |
| -} |
130 |
| -class BeautyProcessor implements ITXCustomBeautyProcesser { |
131 |
| - private FrameBuffer mFrameBuffer; |
132 |
| - private GpuImageGrayscaleFilter mGrayscaleFilter; |
133 |
| - private FloatBuffer mGLCubeBuffer; |
134 |
| - private FloatBuffer mGLTextureBuffer; |
135 |
| - |
136 |
| - @Override |
137 |
| - public void onProcessVideoFrame(TXCustomBeautyVideoFrame srcFrame, TXCustomBeautyVideoFrame dstFrame) { |
138 |
| - final int width = srcFrame.width; |
139 |
| - final int height = srcFrame.height; |
140 |
| - if (mFrameBuffer == null || mFrameBuffer.getWidth() != width || mFrameBuffer.getHeight() != height) { |
141 |
| - if (mFrameBuffer != null) { |
142 |
| - mFrameBuffer.uninitialize(); |
143 |
| - } |
144 |
| - mFrameBuffer = new FrameBuffer(width, height); |
145 |
| - mFrameBuffer.initialize(); |
146 |
| - } |
147 |
| - if (mGrayscaleFilter == null) { |
148 |
| - mGrayscaleFilter = new GpuImageGrayscaleFilter(); |
149 |
| - mGrayscaleFilter.init(); |
150 |
| - mGrayscaleFilter.onOutputSizeChanged(width, height); |
151 |
| - |
152 |
| - mGLCubeBuffer = OpenGlUtils.createNormalCubeVerticesBuffer(); |
153 |
| - mGLTextureBuffer = OpenGlUtils.createTextureCoordsBuffer(Rotation.NORMAL, false, false); |
154 |
| - } |
155 |
| - GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFrameBuffer.getFrameBufferId()); |
156 |
| - GLES20.glViewport(0, 0, width, height); |
157 |
| - mGrayscaleFilter.onDraw(srcFrame.texture.textureId, mGLCubeBuffer, mGLTextureBuffer); |
158 |
| - dstFrame.texture.textureId = mFrameBuffer.getTextureId(); |
159 |
| - GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0); |
160 |
| - } |
161 |
| - public void destroy() { |
162 |
| - if (mFrameBuffer != null) { |
163 |
| - mFrameBuffer.uninitialize(); |
164 |
| - mFrameBuffer = null; |
165 |
| - } |
166 |
| - if (mGrayscaleFilter != null) { |
167 |
| - mGrayscaleFilter.destroy(); |
168 |
| - mGrayscaleFilter = null; |
169 |
| - } |
170 |
| - } |
171 |
| - @Override |
172 |
| - public TXCustomBeautyPixelFormat getSupportedPixelFormat() { |
173 |
| - return TXCustomBeautyPixelFormat.TXCustomBeautyPixelFormatTexture2D; |
174 |
| - } |
175 |
| - @Override |
176 |
| - public TXCustomBeautyBufferType getSupportedBufferType() { |
177 |
| - return TXCustomBeautyBufferType.TXCustomBeautyBufferTypeTexture; |
178 |
| - } |
179 | 6 | }
|
0 commit comments