Skip to content

Commit

Permalink
maintain video feed aspect ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
Ugzuzg committed May 13, 2024
1 parent 84b921b commit 6eaa298
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 129 deletions.
175 changes: 93 additions & 82 deletions app/src/main/java/sq/rogue/rosettadrone/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.SurfaceTexture;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
Expand All @@ -25,6 +26,7 @@
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.text.format.DateFormat;
import android.util.Log;
import android.util.TypedValue;
import android.view.MenuInflater;
Expand Down Expand Up @@ -72,6 +74,7 @@
import java.net.PortUnreachableException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Timer;
Expand Down Expand Up @@ -118,7 +121,12 @@
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private final static int RESULT_SETTINGS = 1001;
private final static int RESULT_HELP = 1002;
private static int compare_height = 0;

private enum FocusedView {
VideoFeed,
Map,
}
private static FocusedView focusedView = FocusedView.VideoFeed;

public static boolean FLAG_PREFS_CHANGED = false;
public static List<String> changedSettings = new ArrayList<String>();
Expand Down Expand Up @@ -192,10 +200,7 @@ public class MainActivity extends AppCompatActivity implements OnMapReadyCallbac
private VideoFeeder.VideoFeed standardVideoFeeder;
protected VideoFeeder.VideoDataListener mReceivedVideoDataListener;
private TextureView videostreamPreviewTtView;
private TextureView videostreamPreviewTtViewSmall;
public DJICodecManager mCodecManager;
private int videoViewWidth;
private int videoViewHeight;
private boolean mIsTranscodedVideoFeedNeeded = false;

private int mMaptype = GoogleMap.MAP_TYPE_HYBRID;
Expand Down Expand Up @@ -534,11 +539,18 @@ private void updateDroneLocation() {
BitmapDrawable bitmapdraw = (BitmapDrawable)getResources().getDrawable(R.drawable.pilot, null);
Bitmap smallMarker;

if (compare_height == 0) {
smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 32, 32, false);
}else{
smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 64, 64, false);
switch (focusedView) {
case Map: {
smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 64, 64, false);
break;
}
case VideoFeed:
default: {
smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 32, 32, false);
break;
}
}

GCS_markerOptions.icon(BitmapDescriptorFactory.fromBitmap(smallMarker));

runOnUiThread(() -> {
Expand Down Expand Up @@ -568,10 +580,16 @@ private void updateDroneLocation() {
BitmapDrawable bitmapdraw = (BitmapDrawable)getResources().getDrawable(R.drawable.drone_img, null);
Bitmap smallMarker;

if (compare_height == 0) {
smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 32, 32, false);
}else{
smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 64, 64, false);
switch (focusedView) {
case Map: {
smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 64, 64, false);
break;
}
case VideoFeed:
default: {
smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 32, 32, false);
break;
}
}
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(smallMarker));

Expand Down Expand Up @@ -741,8 +759,6 @@ protected void onCreate(Bundle savedInstanceState) {
}

videostreamPreviewTtView = findViewById(R.id.livestream_preview_ttv);
videostreamPreviewTtViewSmall = findViewById(R.id.livestream_preview_ttv_small);
videostreamPreviewTtView.setVisibility(View.VISIBLE);

deleteApplicationDirectory();
initLogs();
Expand Down Expand Up @@ -949,32 +965,43 @@ private int getVideoMode(Model model) {
private TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Log.d(TAG, "real onSurfaceTextureAvailable: width " + width + " height " + height);
if (compare_height == 1) {
height = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 86, getResources().getDisplayMetrics()));
width = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 164, getResources().getDisplayMetrics()));

}
videoViewWidth = width;
videoViewHeight = height;

Log.d(TAG, "real onSurfaceTextureAvailable: width " + videoViewWidth + " height " + videoViewHeight + " Mode: " + compare_height);
if (mCodecManager == null) {
mCodecManager = new DJICodecManager(getApplicationContext(), surface, width, height);
pluginManager.onVideoChange();
} else {
if(useOutputSurface) {
mCodecManager.changeOutputSurface(surface);
mCodecManager.onSurfaceSizeChanged(width, height, 0);
}
}

Log.d(TAG, "real onSurfaceTextureAvailable: width " + width + " height " + height);

onSurfaceTextureSizeChanged(surface, width, height);
}

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
videoViewWidth = width;
videoViewHeight = height;
Log.d(TAG, "real onSurfaceTextureAvailable2: width " + videoViewWidth + " height " + videoViewHeight);
double aspectRatio = (double) mCodecManager.getVideoHeight() / mCodecManager.getVideoWidth();
int newWidth, newHeight;
if (height > (int) (width * aspectRatio)) {
newWidth = width;
newHeight = (int) (width * aspectRatio);
} else {
newWidth = (int) (height / aspectRatio);
newHeight = height;
}
int xOffset = (width - newWidth) / 2;
int yOffset = (height - newHeight) / 2;

Matrix transform = new Matrix();
TextureView textureView = videostreamPreviewTtView;
textureView.getTransform(transform);
transform.setScale((float) newWidth / width, (float) newHeight / height);
transform.postTranslate(xOffset, yOffset);
textureView.setTransform(transform);

Log.d(TAG, "real onSurfaceTextureAvailable2: width " + width + " height " + height);

if (useOutputSurface) {
mCodecManager.changeOutputSurface(surface);
mCodecManager.onSurfaceSizeChanged(width, height, 0);
}
}

@Override
Expand Down Expand Up @@ -1113,7 +1140,7 @@ private void downloadLogs() {
final int BUF_LEN = 2048;
byte[] buffer = new byte[BUF_LEN];

String zipName = "RD_LOG_" + android.text.format.DateFormat.format("yyyy-MM-dd-hh:mm:ss", new java.util.Date());
String zipName = "RD_LOG_" + DateFormat.format("yyyy-MM-dd-hh:mm:ss", new Date());
String[] fileNames = {"DJI_LOG", "OUTBOUND_LOG", "INBOUND_LOG"};

File directory = new File(Environment.getExternalStorageDirectory().getPath()
Expand Down Expand Up @@ -1279,64 +1306,48 @@ public boolean onMenuItemClick(MenuItem item) {
}

public void onSmallMapClick(View v) {

LinearLayout map_layout = findViewById(R.id.map_view);
FrameLayout video_layout_small = findViewById(R.id.fragment_container_small);
ViewGroup.LayoutParams map_para = map_layout.getLayoutParams();

if (compare_height == 0) {
logMessageDJI("Set Small screen...");
videostreamPreviewTtView.clearFocus();
videostreamPreviewTtView.setVisibility(View.GONE);

// safeSleep(200);
videostreamPreviewTtViewSmall.setSurfaceTextureListener(mSurfaceTextureListener);
videostreamPreviewTtViewSmall.setVisibility(View.VISIBLE);
video_layout_small.setZ(100.f);

// MAP ok...
map_layout.setZ(0.f);
map_para.height = LayoutParams.WRAP_CONTENT;
map_para.width = LayoutParams.WRAP_CONTENT;
map_layout.setLayoutParams(map_para);

changeOutputSurface(videostreamPreviewTtViewSmall);

compare_height = 1;
FrameLayout video_layout = findViewById(R.id.fragment_container);

ViewGroup focusedLayout;
ViewGroup previewLayout;
switch (focusedView) {
case Map: {
focusedLayout = map_layout;
previewLayout = video_layout;
focusedView = FocusedView.VideoFeed;
break;
}
case VideoFeed:
default: {
focusedLayout = video_layout;
previewLayout = map_layout;
focusedView = FocusedView.Map;
break;
}
}

} else {
logMessageDJI("Set Main screen...");
videostreamPreviewTtViewSmall.clearFocus();
videostreamPreviewTtViewSmall.setVisibility(View.GONE);
logMessageDJI("Swap the map and the video feed...");

//safeSleep(200);
videostreamPreviewTtView.setSurfaceTextureListener(mSurfaceTextureListener);
videostreamPreviewTtView.setVisibility(View.VISIBLE);
video_layout_small.setZ(0.f);
int previewWidth = previewLayout.getWidth();
int previewHeight = previewLayout.getHeight();

// MAP OK...
map_layout.setZ(100.f);
map_para.height = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 86, getResources().getDisplayMetrics()));
map_para.width = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 164, getResources().getDisplayMetrics()));
map_layout.setLayoutParams(map_para);
map_layout.setBottom(((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics())));
map_layout.setLeft(((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics())));
LayoutParams focusedLayoutParams = focusedLayout.getLayoutParams();
focusedLayoutParams.width = previewWidth;
focusedLayoutParams.height = previewHeight;
focusedLayout.setLayoutParams(focusedLayoutParams);
focusedLayout.setZ(100.f);

changeOutputSurface(videostreamPreviewTtView);
LayoutParams previewLayoutParams = previewLayout.getLayoutParams();
previewLayoutParams.height = LayoutParams.WRAP_CONTENT;
previewLayoutParams.width = LayoutParams.WRAP_CONTENT;
previewLayout.setLayoutParams(previewLayoutParams);
previewLayout.setZ(0.f);

compare_height = 0;
}
v.setZ(101.f);
//updateDroneLocation();
}

void changeOutputSurface(TextureView textureView) {
if (mCodecManager != null && useOutputSurface) {
mCodecManager.changeOutputSurface(textureView.getSurfaceTexture());
mCodecManager.onSurfaceSizeChanged(textureView.getWidth(), textureView.getHeight(), 0);
}
}

// Hmm is this ever called...
@Override
public boolean onContextItemSelected(MenuItem item) {
Expand Down Expand Up @@ -1779,7 +1790,7 @@ protected Integer doInBackground(Integer... ints2) {
timer.scheduleAtFixedRate(gcsSender, 0, 100);

for(MAVLinkConnection mavLinkConnection : mainActivityRef.mMavlinkReceiver.mavLinkConnections) {
Listener listener = new MainActivity.GCSCommunicatorAsyncTask.Listener(mavLinkConnection, mainActivityRef);
Listener listener = new Listener(mavLinkConnection, mainActivityRef);
mavLinkConnection.listen(listener);
}

Expand Down
66 changes: 19 additions & 47 deletions app/src/main/res/layout/activity_gui.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@

<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:textColor="@color/centerpoint_green"
android:background="@color/background_blue"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent">

Expand All @@ -28,43 +27,17 @@
android:layout_gravity="bottom|end"
android:alpha="40"
android:foregroundGravity="center_horizontal"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent" />

</FrameLayout>

<FrameLayout
android:id="@+id/fragment_container_small"
android:layout_width="164dp"
android:layout_height="86dp"
android:layout_gravity="bottom|end"
android:textColor="@color/centerpoint_green"
android:background="@color/background_blue"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent">

<TextureView
android:id="@+id/livestream_preview_ttv_small"
android:layout_width="164dp"
android:layout_height="86dp"
android:layout_gravity="bottom|end"
android:alpha="40"
android:foregroundGravity="center_horizontal"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent" />

</FrameLayout>

<LinearLayout
android:id="@+id/map_view"
android:layout_width="164dp"
android:layout_height="86dp"
android:layout_width="160dp"
android:layout_height="90dp"
android:gravity="bottom|end"
android:orientation="horizontal"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent">

Expand All @@ -89,6 +62,21 @@
-->
</LinearLayout>


<FrameLayout
android:id="@+id/tab_map_video"
android:layout_width="160dp"
android:layout_height="90dp"
android:background="#FF0000"
android:gravity="bottom|end"
android:onClick="onSmallMapClick"
android:clickable="true"
android:alpha="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent">
</FrameLayout>


<FrameLayout
android:id="@+id/compass_container"
android:layout_width="510dp"
Expand All @@ -111,22 +99,6 @@
</FrameLayout>


<FrameLayout
android:id="@+id/tab_map_video"
android:layout_width="164dp"
android:layout_height="86dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="10dp"
android:background="#FF0000"
android:gravity="bottom|end"
android:onClick="onSmallMapClick"
android:clickable="true"
android:alpha="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent">
</FrameLayout>


<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down

0 comments on commit 6eaa298

Please sign in to comment.