Skip to content

Commit

Permalink
Working plugin v1
Browse files Browse the repository at this point in the history
  • Loading branch information
dipak140 committed Jun 14, 2022
1 parent fbe228b commit af26343
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 171 deletions.
2 changes: 1 addition & 1 deletion Source/WebCameraFeed/AndroidCamera_APL.xml
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ import io.sariska.sdk.SariskaMediaTransport;
SariskaMediaTransport.initializeSdk(_activity.getApplication());
System.out.println("Sariska Initialized");
this.setupLocalStream();
String token = "eyJhbGciOiJSUzI1NiIsImtpZCI6IjNmYjc1MTJjZjgzYzdkYTRjMjM0Y2QzYWEyYWViOTUzMGNlZmUwMDg1YzRiZjljYzgwY2U5YmQ5YmRiNjA3ZjciLCJ0eXAiOiJKV1QifQ.eyJjb250ZXh0Ijp7InVzZXIiOnsiaWQiOiJ6bmJ4M3Y1aCIsImF2YXRhciI6IiNFMDNEQTAiLCJuYW1lIjoidW5yZWFsRCJ9LCJncm91cCI6IjEifSwic3ViIjoidWF0bmNvZTV2NzJsbm9obG53Z3F2OCIsInJvb20iOiIqIiwiaWF0IjoxNjU1MDMxNTg1LCJuYmYiOjE2NTUwMzE1ODUsImlzcyI6InNhcmlza2EiLCJhdWQiOiJtZWRpYV9tZXNzYWdpbmdfY28tYnJvd3NpbmciLCJleHAiOjE2NTUyMDQzODV9.OoqZc3PKER3M7kOMuzJT7LYvUSYcGaYV5tR1rhChGPJ5fIS47Mpf-j09u9jI69-hl45hTzQS1xvWAnwqvopTgTJ35yulI2rYye0zGTed51ljHw4R7BVlhymvNOMW6mq6klBsRsmZgqLnEQIWPdC7Gdy2rYMs_daGYNI6POqUte3HoeTGwAwr_IIDe3jnzoA2LZ7bJ_59dGaUAjBjscyNW9IGwHNXnChEpDXKw0ocKp8hwUTKuLb2F-76auzzr5aQ_HBtj0-eMfeXt3TVINtLFDwZeW8sFIRCYqqyNimaHyMqDyi7vdYjb7oz_uaYAwVvjDRc2X8ieyDnw598-I3gZA";
String token = "eyJhbGciOiJSUzI1NiIsImtpZCI6IjNmYjc1MTJjZjgzYzdkYTRjMjM0Y2QzYWEyYWViOTUzMGNlZmUwMDg1YzRiZjljYzgwY2U5YmQ5YmRiNjA3ZjciLCJ0eXAiOiJKV1QifQ.eyJjb250ZXh0Ijp7InVzZXIiOnsiaWQiOiJtZXhsMnBzcSIsImF2YXRhciI6IiM3RTkyOTAiLCJuYW1lIjoidW5yZWFsVVNlciJ9LCJncm91cCI6IjEifSwic3ViIjoidWF0bmNvZTV2NzJsbm9obG53Z3F2OCIsInJvb20iOiIqIiwiaWF0IjoxNjU1MTk5NTAzLCJuYmYiOjE2NTUxOTk1MDMsImlzcyI6InNhcmlza2EiLCJhdWQiOiJtZWRpYV9tZXNzYWdpbmdfY28tYnJvd3NpbmciLCJleHAiOjE2NTUzNzIzMDN9.MhF3Jfcxg5fj5roBlUBfhkI4LkqwEPBP6_Pmts3Z5CY9xUPq9ijoWULCwA7hyGrvdDtVIlItKpSA8KkGv5PaNUcMFzseelhFfeWXeNUjsDdrPH5XtXcrKG3GRYA8Wcj7aciAes3wWtxC-xnmjocAPZuJiJePDpaiA5Q3paFldav8wNaduUaNibS5CwkWvTdbbjyU97UKG1RdQ0ehU-sOA4AgXJ34_B442-npsN7eQ5onLn001kUKZFE0c67TOhzphJY-3g8L-dWs9zM5Cl-eDA1gBsvfl_qnmmIhTnL377jZNFNWqHXJGQ-B0PVrBsWyJB3MNW19ChorI0IN8hDcvQ";

connection = SariskaMediaTransport.JitsiConnection(token, "dipak", false);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
//
// AndroidVideoRemote.cpp
// AndroidRemoteVideo.cpp
// SariskaUnrealDemo_Index
//
// Created by Dipak Sisodiya on 12/06/22.
// Created by Dipak Sisodiya on 14/06/22.
// Copyright © 2022 Epic Games, Inc. All rights reserved.
//

#include "AndroidVideoRemote.h"
#include "AndroidRemoteVideo.h"
#include <Async/Async.h>
#if PLATFORM_ANDROID
#include <Engine/Texture2D.h>
#include "Android/AndroidApplication.h"
#include <Runtime/Launch/Public/Android/AndroidJNI.h>
#include <android/log.h>
#include "WebCameraComponent.h"
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>

#define LOG_TAG "CameraLOG"

static long int crv_tab_Remote[256];
static long int cbu_tab_Remote[256];
static long int cgu_tab_Remote[256];
static long int cgv_tab_Remote[256];
static long int tab_76309_Remote[256];
static unsigned char clp_Remote[1024];

static AndroidVideoRemote* currentGrabberRemote = NULL;
int SetupJNICamera(JNIEnv* env);
//JNIEnv* ENV = NULL;
static AndroidRemoteVideo* currentGrabberRemote = NULL;

void InitConverTable(){
void InitConvertTable_Remote()
{
static bool inited = false;
if (inited) return;
long int crv, cbu, cgu, cgv;
Expand Down Expand Up @@ -57,87 +56,47 @@ void InitConverTable(){
inited = true;
}

int AndroidVideoRemote::returnNumber(){
return 21;
TArray<FVideoDevice> AndroidRemoteVideo::listDevices() const {
TArray<FVideoDevice> devices;
return devices;
}

AndroidVideoRemote* AndroidVideoRemote::get_instance(){
return new AndroidVideoRemote();
bool AndroidRemoteVideo::isFrameNew() const {
return newFrame;
}

void AndroidVideoRemote::allocateDataRemote(int w, int h, EPixelFormat InFormat){

if(isTextureGenerated == true){
return;
}

frwLock.WriteLock();

uint32 MemorySize = w * h * 4;
pixels.Reset();
pixels.AddUninitialized(MemorySize);
FMemory::Memzero(pixels.GetData(), MemorySize);

remoteTexture = UTexture2D::CreateTransient(w, h, InFormat);
FTexture2DMipMap& Mip = remoteTexture->PlatformData->Mips[0];
void* Data = Mip.BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(Data, pixels.GetData(), MemorySize);
Mip.BulkData.Unlock();
remoteTexture->UpdateResource();
isTextureGenerated = true;
frwLock.WriteUnlock();
int AndroidRemoteVideo::getHeight() const {
return height;
}

UTexture* AndroidVideoRemote::returnTexture(){
return remoteTexture.Get();
int AndroidRemoteVideo::getWidth() const {
return width;
}

AndroidVideoRemote::AndroidVideoRemote(){
bIsInit = false;
number = 1;
InitConvertTable();
bIsInit = false;
textureID = 0;
_mirror = false;
bool AndroidRemoteVideo::switchBackAndFront() {
return true;
}

AndroidVideoRemote:: ~AndroidVideoRemote() {
//Do nothing for now
// add a close function here
void AndroidRemoteVideo::pause() {
// Do nothing
}

bool AndroidVideoRemote::setup(int w, int h, bool mirrored) {
loadTexture();
bIsInit = true;
__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "Called setup for remote");
currentGrabberRemote = this;

//allocateData(width, height, PF_B8G8R8A8); // Part of BaseVideoGrabber
//startThread(); // Part of BaseVideoGrabber
//registerDelegates(); //Part of BaseVideoGrabber
//currentGrabber = this; // Not needed for now
return true;

void AndroidRemoteVideo::resume() {
//Do nothing
}

int AndroidRemoteVideo::getBackCamera() const {
return 1;
}

void AndroidVideoRemote::loadTexture() {

GLuint TextureID = 0;
glGenTextures(1, &TextureID);
glEnable(GL_TEXTURE_EXTERNAL_OES);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, TextureID);
glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glDisable(GL_TEXTURE_EXTERNAL_OES);
textureID = TextureID;
int AndroidRemoteVideo::getFrontCamera() const {
return 2;
}

void AndroidVideoRemote::ConvertYUV2RGBA(unsigned char *src0, unsigned char *src1, unsigned char *dst_ori,

void ConvertYUV2RGBA_Remote(unsigned char *src0, unsigned char *src1, unsigned char *dst_ori,
int width, int height, bool mirror)
{
__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "Called YUV");
register int y1, y2, u, v;
register unsigned char *py1, *py2;
register int i, j, c1, c2, c3, c4;
Expand Down Expand Up @@ -237,37 +196,183 @@ void AndroidVideoRemote::ConvertYUV2RGBA(unsigned char *src0, unsigned char *src
}


void AndroidVideoRemote::deleteTexture () {
AndroidRemoteVideo::AndroidRemoteVideo() {
InitConvertTable_Remote();
fps = -1;
width = 0;
height = 0;
bIsInit = false;
newFrame = false;
bHavePixelsChanged = false;
textureID = 0;
_mirror = false;
}

AndroidRemoteVideo:: ~AndroidRemoteVideo() {
close();
}


void AndroidRemoteVideo::loadTexture() {

GLuint TextureID = 0;
glGenTextures(1, &TextureID);

glEnable(GL_TEXTURE_EXTERNAL_OES);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, TextureID);

glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);



glDisable(GL_TEXTURE_EXTERNAL_OES);

textureID = TextureID;

}

void AndroidRemoteVideo::deleteTexture () {
if ( textureID != 0) {
GLuint TextureID = TextureID;
glDeleteTextures(1, &TextureID);
textureID = 0;
}
}

void AndroidVideoRemote::updatePixelsCB(unsigned char *isrc, int w, int h) {
//Do nothing for now
ConvertYUV2RGBA(isrc, // y component
isrc + (w * h), // uv components
copyPixels.GetData(), w, h, _mirror);
void AndroidRemoteVideo::setDeviceID(int deviceID) {

this->deviceID = deviceID;
}

void AndroidVideoRemote::close() {

bool AndroidRemoteVideo::setup(int w, int h, bool mirrored) {
__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "Called setup");
loadTexture();


setVideoMirrored(false);
_mirror = mirrored;
width = w;
height = h;
allocateData(width, height, PF_B8G8R8A8);
startThread();
bIsInit = true;
registerDelegates();
currentGrabberRemote = this;

return true;
}

void AndroidRemoteVideo::close() {
currentGrabberRemote = NULL;
bIsInit = false;
stopThread();
deleteTexture();
unRegisterDelegates();
bIsInit = false;
width = 0;
height = 0;
fps = -1;
newFrame = false;
bHavePixelsChanged = false;
}

extern "C" bool Java_com_epicgames_ue4_GameActivity_nativeGetFrameDataRemote(JNIEnv* LocalJNIEnv, jobject LocalThiz, jint frameWidth, jint frameHeight, jbyteArray data){
__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "Called from java");
int length = LocalJNIEnv->GetArrayLength(data);
jboolean isCopy;
auto currentFrame = (unsigned char*)LocalJNIEnv->GetByteArrayElements(data, &isCopy);
if(currentFrame){
currentGrabberRemote->updatePixelsCB(currentFrame, frameWidth, frameHeight);
LocalJNIEnv->ReleaseByteArrayElements(data, (jbyte*)currentFrame, 0);
void AndroidRemoteVideo::update() {

if ( !bIsInit) return;

newFrame = false;

if ( currentGrabberRemote == NULL) {
currentGrabberRemote = this;
}

if (bHavePixelsChanged ) {
bHavePixelsChanged = false;
frwLock.WriteLock();

if (cameraTexture.IsValid()) {
FUpdateTextureRegion2D region(0, 0, 0, 0, width, height);
cameraTexture->UpdateTextureRegions(0, 1, &region, width*4, 4, pixels.GetData());
}
frwLock.WriteUnlock();
newFrame = true;
}
}

void AndroidRemoteVideo::updatePixelsCB(unsigned char *isrc, int w, int h) {
if (!bIsInit) return;

if (w != width || h != height || !cameraTexture.IsValid()) {
if (!cameraTexture.IsValid()) {
UE_LOG(LogVideoGrabber, Warning, TEXT("The texture is invalid, reallocating"));
__android_log_print(ANDROID_LOG_WARN, LOG_TAG, "The texture is invalid, reallocating");
}
else {
UE_LOG(LogVideoGrabber, Warning, TEXT("The incoming image dimensions %d by %d don't match with the current dimensions %d by %d"), w, h, width, height);
__android_log_print(ANDROID_LOG_WARN, LOG_TAG, "The incoming image dimensions %d by %d don't match with the current dimensions %d by %d", w, h, width, height );
}

FEvent* fSemaphore = FGenericPlatformProcess::GetSynchEventFromPool(false);
AsyncTask(ENamedThreads::GameThread, [this, w, h, fSemaphore]() {
this->resizeData(w, h, PF_B8G8R8A8);
__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "Resized data");
fSemaphore->Trigger();
});

fSemaphore->Wait();
FGenericPlatformProcess::ReturnSynchEventToPool(fSemaphore);
width = w;
height = h;

}
frwLock.ReadLock();
if (pixels.Num() > 0) {

if ( copyPixels.Num() != pixels.Num()) {
copyPixels.Reset(pixels.Num());
}
frwLock.ReadUnlock();

ConvertYUV2RGBA_Remote(isrc, // y component
isrc + (w * h), // uv components
copyPixels.GetData(), w, h, _mirror);

frwLock.WriteLock();
uint32 MemorySize = w*h*4;
FMemory::Memcpy(pixels.GetData(), copyPixels.GetData(), MemorySize);
bHavePixelsChanged = true;
frwLock.WriteUnlock();
} else {
frwLock.ReadUnlock();
}

}


int SetupJNICamera_Remote(JNIEnv* env)
{
return JNI_OK;
}



extern "C" bool Java_com_epicgames_ue4_GameActivity_nativeGetFrameDataRemote(JNIEnv* LocalJNIEnv, jobject LocalThiz, jint frameWidth, jint frameHeight, jbyteArray data) {
__android_log_print(ANDROID_LOG_WARN, LOG_TAG, "Called Native Get Frame Remote");
if ( currentGrabberRemote != NULL) {
//get the new frame
int length = LocalJNIEnv->GetArrayLength(data);
jboolean isCopy;
__android_log_print(ANDROID_LOG_WARN, LOG_TAG, "currentGrabberIs not null");
auto currentFrame = (unsigned char *)LocalJNIEnv->GetByteArrayElements(data, &isCopy);
if (currentFrame) {
currentGrabberRemote->updatePixelsCB(currentFrame, frameWidth, frameHeight);
LocalJNIEnv->ReleaseByteArrayElements(data, (jbyte*)currentFrame, 0);
}
}
__android_log_print(ANDROID_LOG_WARN, LOG_TAG, "currentGrabberIs null");
return JNI_TRUE;
}

Expand Down
Loading

0 comments on commit af26343

Please sign in to comment.