forked from react-native-webrtc/react-native-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React native vb #1
Open
yavuzcakir
wants to merge
10
commits into
master
Choose a base branch
from
reactNativeVB
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f40c4b1
add log getusermedia
dae4287
vb first commit
9809afc
virtual backgorund image used from project instead of url
f3d55e1
prevent pink screen on vb
5e77caf
changed throttle durations reworded logs
d235947
added framework search paths
0381210
apply throttle only for vb case
cc706f3
added android vb feature
919f99d
fixed mirror and rotation issue
32a9603
process every 3 frames in vb
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
android/src/main/java/com/oney/WebRTCModule/VirtualBackgroundVideoProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package com.oney.WebRTCModule; | ||
|
||
import static android.graphics.Color.argb; | ||
import static android.graphics.PorterDuff.Mode.DST_OVER; | ||
import static android.graphics.PorterDuff.Mode.SRC_IN; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.graphics.Canvas; | ||
import android.graphics.Matrix; | ||
import android.graphics.Paint; | ||
import android.graphics.PorterDuffXfermode; | ||
import android.opengl.GLES20; | ||
import android.opengl.GLUtils; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.google.android.gms.tasks.OnSuccessListener; | ||
import com.google.android.gms.tasks.Task; | ||
import com.google.mlkit.vision.common.InputImage; | ||
import com.google.mlkit.vision.segmentation.Segmentation; | ||
import com.google.mlkit.vision.segmentation.SegmentationMask; | ||
import com.google.mlkit.vision.segmentation.Segmenter; | ||
import com.google.mlkit.vision.segmentation.selfie.SelfieSegmenterOptions; | ||
|
||
import org.webrtc.SurfaceTextureHelper; | ||
import org.webrtc.TextureBufferImpl; | ||
import org.webrtc.VideoFrame; | ||
import org.webrtc.VideoProcessor; | ||
import org.webrtc.VideoSink; | ||
import org.webrtc.YuvConverter; | ||
|
||
public class VirtualBackgroundVideoProcessor implements VideoProcessor { | ||
|
||
private VideoSink target; | ||
private final SurfaceTextureHelper surfaceTextureHelper; | ||
final YuvConverter yuvConverter = new YuvConverter(); | ||
|
||
private YuvFrame yuvFrame; | ||
private Bitmap inputFrameBitmap; | ||
private int frameCounter = 0; | ||
|
||
final Bitmap backgroundImage; | ||
final Bitmap scaled; | ||
|
||
final SelfieSegmenterOptions options = | ||
new SelfieSegmenterOptions.Builder() | ||
.setDetectorMode(SelfieSegmenterOptions.STREAM_MODE) | ||
.build(); | ||
final Segmenter segmenter = Segmentation.getClient(options); | ||
|
||
public VirtualBackgroundVideoProcessor(ReactApplicationContext context, SurfaceTextureHelper surfaceTextureHelper) { | ||
super(); | ||
|
||
this.surfaceTextureHelper = surfaceTextureHelper; | ||
|
||
backgroundImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.portrait_background); | ||
scaled = Bitmap.createScaledBitmap(backgroundImage, 640, 640, false ); | ||
} | ||
|
||
@Override | ||
public void setSink(@Nullable VideoSink videoSink) { | ||
target = videoSink; | ||
} | ||
|
||
@Override | ||
public void onCapturerStarted(boolean b) { | ||
|
||
} | ||
|
||
@Override | ||
public void onCapturerStopped() { | ||
|
||
} | ||
|
||
@Override | ||
public void onFrameCaptured(VideoFrame videoFrame) { | ||
|
||
if(frameCounter == 0) { | ||
yuvFrame = new YuvFrame(videoFrame); | ||
inputFrameBitmap = yuvFrame.getBitmap(); | ||
|
||
InputImage image = InputImage.fromBitmap(inputFrameBitmap, 0); | ||
|
||
Task<SegmentationMask> result = | ||
segmenter.process(image) | ||
.addOnSuccessListener( | ||
new OnSuccessListener<SegmentationMask>() { | ||
@Override | ||
public void onSuccess(SegmentationMask mask) { | ||
|
||
mask.getBuffer().rewind(); | ||
int[] arr = maskColorsFromByteBuffer(mask); | ||
Bitmap segmentedBitmap = Bitmap.createBitmap( | ||
arr, mask.getWidth(), mask.getHeight(), Bitmap.Config.ARGB_8888 | ||
); | ||
arr = null; | ||
|
||
Bitmap segmentedBitmapMutable = segmentedBitmap.copy(Bitmap.Config.ARGB_8888, true); | ||
segmentedBitmap.recycle(); | ||
Canvas canvas = new Canvas(segmentedBitmapMutable); | ||
|
||
Paint paint = new Paint(); | ||
paint.setXfermode(new PorterDuffXfermode(SRC_IN)); | ||
canvas.drawBitmap(scaled, 0, 0, paint); | ||
paint.setXfermode(new PorterDuffXfermode(DST_OVER)); | ||
canvas.drawBitmap(inputFrameBitmap, 0, 0, paint); | ||
|
||
surfaceTextureHelper.getHandler().post(new Runnable() { | ||
@Override | ||
public void run() { | ||
|
||
GLES20.glActiveTexture(GLES20.GL_TEXTURE0); | ||
TextureBufferImpl buffer = new TextureBufferImpl(segmentedBitmapMutable.getWidth(), | ||
segmentedBitmapMutable.getHeight(), VideoFrame.TextureBuffer.Type.RGB, | ||
GLES20.GL_TEXTURE0, new Matrix(), surfaceTextureHelper.getHandler(), yuvConverter, null); | ||
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE0); | ||
|
||
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST); | ||
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST); | ||
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, segmentedBitmapMutable, 0); | ||
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0); | ||
|
||
VideoFrame.I420Buffer i420Buf = yuvConverter.convert(buffer); | ||
VideoFrame out = new VideoFrame(i420Buf, 180, videoFrame.getTimestampNs()); | ||
|
||
buffer.release(); | ||
//yuvFrame.dispose(); | ||
|
||
target.onFrame(out); | ||
out.release(); | ||
} | ||
}); | ||
|
||
} | ||
}); | ||
} | ||
updateFrameCounter(); | ||
} | ||
|
||
private void updateFrameCounter() { | ||
frameCounter++; | ||
if(frameCounter == 3) { | ||
frameCounter = 0; | ||
} | ||
} | ||
|
||
private int[] maskColorsFromByteBuffer(SegmentationMask mask) { | ||
int[] colors = new int[mask.getHeight() * mask.getWidth()]; | ||
for (int i = 0; i < mask.getHeight() * mask.getWidth(); i++) { | ||
float backgroundLikelihood = 1 - mask.getBuffer().getFloat(); | ||
if (backgroundLikelihood > 0.9) { | ||
colors[i] = argb(255, 255, 0, 255); | ||
} else if (backgroundLikelihood > 0.2) { | ||
// Linear interpolation to make sure when backgroundLikelihood is 0.2, the alpha is 0 and | ||
// when backgroundLikelihood is 0.9, the alpha is 128. | ||
// +0.5 to round the float value to the nearest int. | ||
double d = 182.9 * backgroundLikelihood - 36.6 + 0.5; | ||
int alpha = (int) d; | ||
colors[i] = argb(alpha, 255, 0, 255); | ||
} | ||
} | ||
return colors; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please check latest jitsi webrtc version