Skip to content

Commit b02e7b6

Browse files
committed
maintain video feed aspect ratio
1 parent 8fefb6e commit b02e7b6

File tree

2 files changed

+117
-129
lines changed

2 files changed

+117
-129
lines changed

app/src/main/java/sq/rogue/rosettadrone/MainActivity.java

+98-82
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import android.content.res.AssetManager;
1313
import android.graphics.Bitmap;
1414
import android.graphics.Color;
15+
import android.graphics.Matrix;
1516
import android.graphics.SurfaceTexture;
1617
import android.graphics.drawable.BitmapDrawable;
1718
import android.graphics.drawable.Drawable;
@@ -25,6 +26,7 @@
2526
import android.os.Handler;
2627
import android.os.IBinder;
2728
import android.os.Looper;
29+
import android.text.format.DateFormat;
2830
import android.util.Log;
2931
import android.util.TypedValue;
3032
import android.view.MenuInflater;
@@ -72,6 +74,7 @@
7274
import java.net.PortUnreachableException;
7375
import java.net.SocketTimeoutException;
7476
import java.util.ArrayList;
77+
import java.util.Date;
7578
import java.util.List;
7679
import java.util.Objects;
7780
import java.util.Timer;
@@ -118,7 +121,12 @@
118121
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
119122
private final static int RESULT_SETTINGS = 1001;
120123
private final static int RESULT_HELP = 1002;
121-
private static int compare_height = 0;
124+
125+
private enum FocusedView {
126+
VideoFeed,
127+
Map,
128+
}
129+
private static FocusedView focusedView = FocusedView.VideoFeed;
122130

123131
public static boolean FLAG_PREFS_CHANGED = false;
124132
public static List<String> changedSettings = new ArrayList<String>();
@@ -188,14 +196,12 @@ public class MainActivity extends AppCompatActivity implements OnMapReadyCallbac
188196
private VideoService videoService = null;
189197
private boolean mIsBound;
190198
private int m_videoMode = 1;
199+
private int mPrevVideoBufferSize = 0;
191200

192201
private VideoFeeder.VideoFeed standardVideoFeeder;
193202
protected VideoFeeder.VideoDataListener mReceivedVideoDataListener;
194203
private TextureView videostreamPreviewTtView;
195-
private TextureView videostreamPreviewTtViewSmall;
196204
public DJICodecManager mCodecManager;
197-
private int videoViewWidth;
198-
private int videoViewHeight;
199205
private boolean mIsTranscodedVideoFeedNeeded = false;
200206

201207
private int mMaptype = GoogleMap.MAP_TYPE_HYBRID;
@@ -534,11 +540,18 @@ private void updateDroneLocation() {
534540
BitmapDrawable bitmapdraw = (BitmapDrawable)getResources().getDrawable(R.drawable.pilot, null);
535541
Bitmap smallMarker;
536542

537-
if (compare_height == 0) {
538-
smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 32, 32, false);
539-
}else{
540-
smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 64, 64, false);
543+
switch (focusedView) {
544+
case Map: {
545+
smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 64, 64, false);
546+
break;
547+
}
548+
case VideoFeed:
549+
default: {
550+
smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 32, 32, false);
551+
break;
552+
}
541553
}
554+
542555
GCS_markerOptions.icon(BitmapDescriptorFactory.fromBitmap(smallMarker));
543556

544557
runOnUiThread(() -> {
@@ -568,10 +581,16 @@ private void updateDroneLocation() {
568581
BitmapDrawable bitmapdraw = (BitmapDrawable)getResources().getDrawable(R.drawable.drone_img, null);
569582
Bitmap smallMarker;
570583

571-
if (compare_height == 0) {
572-
smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 32, 32, false);
573-
}else{
574-
smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 64, 64, false);
584+
switch (focusedView) {
585+
case Map: {
586+
smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 64, 64, false);
587+
break;
588+
}
589+
case VideoFeed:
590+
default: {
591+
smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 32, 32, false);
592+
break;
593+
}
575594
}
576595
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(smallMarker));
577596

@@ -745,8 +764,6 @@ protected void onCreate(Bundle savedInstanceState) {
745764
}
746765

747766
videostreamPreviewTtView = findViewById(R.id.livestream_preview_ttv);
748-
videostreamPreviewTtViewSmall = findViewById(R.id.livestream_preview_ttv_small);
749-
videostreamPreviewTtView.setVisibility(View.VISIBLE);
750767

751768
deleteApplicationDirectory();
752769
initLogs();
@@ -840,6 +857,10 @@ private void notifyStatusChange() {
840857
if (mCodecManager != null) {
841858
// Render on screen
842859
mCodecManager.sendDataToDecoder(videoBuffer, size);
860+
if (mPrevVideoBufferSize != size && videostreamPreviewTtView.getSurfaceTexture() != null) {
861+
mPrevVideoBufferSize = size;
862+
mSurfaceTextureListener.onSurfaceTextureSizeChanged(videostreamPreviewTtView.getSurfaceTexture(), videostreamPreviewTtView.getWidth(), videostreamPreviewTtView.getHeight());
863+
}
843864
}
844865

845866
} else {
@@ -953,32 +974,43 @@ private int getVideoMode(Model model) {
953974
private TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
954975
@Override
955976
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
956-
Log.d(TAG, "real onSurfaceTextureAvailable: width " + width + " height " + height);
957-
if (compare_height == 1) {
958-
height = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 86, getResources().getDisplayMetrics()));
959-
width = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 164, getResources().getDisplayMetrics()));
960-
961-
}
962-
videoViewWidth = width;
963-
videoViewHeight = height;
964-
965-
Log.d(TAG, "real onSurfaceTextureAvailable: width " + videoViewWidth + " height " + videoViewHeight + " Mode: " + compare_height);
966977
if (mCodecManager == null) {
967978
mCodecManager = new DJICodecManager(getApplicationContext(), surface, width, height);
968979
pluginManager.onVideoChange();
969-
} else {
970-
if(useOutputSurface) {
971-
mCodecManager.changeOutputSurface(surface);
972-
mCodecManager.onSurfaceSizeChanged(width, height, 0);
973-
}
974980
}
981+
982+
Log.d(TAG, "real onSurfaceTextureAvailable: width " + width + " height " + height);
983+
984+
onSurfaceTextureSizeChanged(surface, width, height);
975985
}
976986

977987
@Override
978988
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
979-
videoViewWidth = width;
980-
videoViewHeight = height;
981-
Log.d(TAG, "real onSurfaceTextureAvailable2: width " + videoViewWidth + " height " + videoViewHeight);
989+
double aspectRatio = (double) mCodecManager.getVideoHeight() / mCodecManager.getVideoWidth();
990+
int newWidth, newHeight;
991+
if (height > (int) (width * aspectRatio)) {
992+
newWidth = width;
993+
newHeight = (int) (width * aspectRatio);
994+
} else {
995+
newWidth = (int) (height / aspectRatio);
996+
newHeight = height;
997+
}
998+
int xOffset = (width - newWidth) / 2;
999+
int yOffset = (height - newHeight) / 2;
1000+
1001+
Matrix transform = new Matrix();
1002+
TextureView textureView = videostreamPreviewTtView;
1003+
textureView.getTransform(transform);
1004+
transform.setScale((float) newWidth / width, (float) newHeight / height);
1005+
transform.postTranslate(xOffset, yOffset);
1006+
textureView.setTransform(transform);
1007+
1008+
Log.d(TAG, "real onSurfaceTextureAvailable2: width " + width + " height " + height);
1009+
1010+
if (useOutputSurface) {
1011+
mCodecManager.changeOutputSurface(surface);
1012+
mCodecManager.onSurfaceSizeChanged(width, height, 0);
1013+
}
9821014
}
9831015

9841016
@Override
@@ -1117,7 +1149,7 @@ private void downloadLogs() {
11171149
final int BUF_LEN = 2048;
11181150
byte[] buffer = new byte[BUF_LEN];
11191151

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

11231155
File directory = new File(Environment.getExternalStorageDirectory().getPath()
@@ -1283,64 +1315,48 @@ public boolean onMenuItemClick(MenuItem item) {
12831315
}
12841316

12851317
public void onSmallMapClick(View v) {
1286-
12871318
LinearLayout map_layout = findViewById(R.id.map_view);
1288-
FrameLayout video_layout_small = findViewById(R.id.fragment_container_small);
1289-
ViewGroup.LayoutParams map_para = map_layout.getLayoutParams();
1290-
1291-
if (compare_height == 0) {
1292-
logMessageDJI("Set Small screen...");
1293-
videostreamPreviewTtView.clearFocus();
1294-
videostreamPreviewTtView.setVisibility(View.GONE);
1295-
1296-
// safeSleep(200);
1297-
videostreamPreviewTtViewSmall.setSurfaceTextureListener(mSurfaceTextureListener);
1298-
videostreamPreviewTtViewSmall.setVisibility(View.VISIBLE);
1299-
video_layout_small.setZ(100.f);
1300-
1301-
// MAP ok...
1302-
map_layout.setZ(0.f);
1303-
map_para.height = LayoutParams.WRAP_CONTENT;
1304-
map_para.width = LayoutParams.WRAP_CONTENT;
1305-
map_layout.setLayoutParams(map_para);
1306-
1307-
changeOutputSurface(videostreamPreviewTtViewSmall);
1308-
1309-
compare_height = 1;
1319+
FrameLayout video_layout = findViewById(R.id.fragment_container);
1320+
1321+
ViewGroup focusedLayout;
1322+
ViewGroup previewLayout;
1323+
switch (focusedView) {
1324+
case Map: {
1325+
focusedLayout = map_layout;
1326+
previewLayout = video_layout;
1327+
focusedView = FocusedView.VideoFeed;
1328+
break;
1329+
}
1330+
case VideoFeed:
1331+
default: {
1332+
focusedLayout = video_layout;
1333+
previewLayout = map_layout;
1334+
focusedView = FocusedView.Map;
1335+
break;
1336+
}
1337+
}
13101338

1311-
} else {
1312-
logMessageDJI("Set Main screen...");
1313-
videostreamPreviewTtViewSmall.clearFocus();
1314-
videostreamPreviewTtViewSmall.setVisibility(View.GONE);
1339+
logMessageDJI("Swap the map and the video feed...");
13151340

1316-
//safeSleep(200);
1317-
videostreamPreviewTtView.setSurfaceTextureListener(mSurfaceTextureListener);
1318-
videostreamPreviewTtView.setVisibility(View.VISIBLE);
1319-
video_layout_small.setZ(0.f);
1341+
int previewWidth = previewLayout.getWidth();
1342+
int previewHeight = previewLayout.getHeight();
13201343

1321-
// MAP OK...
1322-
map_layout.setZ(100.f);
1323-
map_para.height = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 86, getResources().getDisplayMetrics()));
1324-
map_para.width = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 164, getResources().getDisplayMetrics()));
1325-
map_layout.setLayoutParams(map_para);
1326-
map_layout.setBottom(((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics())));
1327-
map_layout.setLeft(((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics())));
1344+
LayoutParams focusedLayoutParams = focusedLayout.getLayoutParams();
1345+
focusedLayoutParams.width = previewWidth;
1346+
focusedLayoutParams.height = previewHeight;
1347+
focusedLayout.setLayoutParams(focusedLayoutParams);
1348+
focusedLayout.setZ(100.f);
13281349

1329-
changeOutputSurface(videostreamPreviewTtView);
1350+
LayoutParams previewLayoutParams = previewLayout.getLayoutParams();
1351+
previewLayoutParams.height = LayoutParams.WRAP_CONTENT;
1352+
previewLayoutParams.width = LayoutParams.WRAP_CONTENT;
1353+
previewLayout.setLayoutParams(previewLayoutParams);
1354+
previewLayout.setZ(0.f);
13301355

1331-
compare_height = 0;
1332-
}
13331356
v.setZ(101.f);
13341357
//updateDroneLocation();
13351358
}
13361359

1337-
void changeOutputSurface(TextureView textureView) {
1338-
if (mCodecManager != null && useOutputSurface) {
1339-
mCodecManager.changeOutputSurface(textureView.getSurfaceTexture());
1340-
mCodecManager.onSurfaceSizeChanged(textureView.getWidth(), textureView.getHeight(), 0);
1341-
}
1342-
}
1343-
13441360
// Hmm is this ever called...
13451361
@Override
13461362
public boolean onContextItemSelected(MenuItem item) {
@@ -1783,7 +1799,7 @@ protected Integer doInBackground(Integer... ints2) {
17831799
timer.scheduleAtFixedRate(gcsSender, 0, 100);
17841800

17851801
for(MAVLinkConnection mavLinkConnection : mainActivityRef.mMavlinkReceiver.mavLinkConnections) {
1786-
Listener listener = new MainActivity.GCSCommunicatorAsyncTask.Listener(mavLinkConnection, mainActivityRef);
1802+
Listener listener = new Listener(mavLinkConnection, mainActivityRef);
17871803
mavLinkConnection.listen(listener);
17881804
}
17891805

0 commit comments

Comments
 (0)