From 446a9a27865acb3b203fba434f3c840e22001db0 Mon Sep 17 00:00:00 2001 From: niyatim23 Date: Mon, 8 Apr 2024 17:17:00 -0700 Subject: [PATCH] more cleanup --- .../demoapp/activity/WebRtcActivity.java | 147 ++++++++---------- .../StreamWebRtcConfigurationFragment.java | 6 +- .../kinesisvideo/signaling/model/Event.java | 3 + 3 files changed, 71 insertions(+), 85 deletions(-) diff --git a/app/src/main/java/com/amazonaws/kinesisvideo/demoapp/activity/WebRtcActivity.java b/app/src/main/java/com/amazonaws/kinesisvideo/demoapp/activity/WebRtcActivity.java index 366a41b..21a04d5 100644 --- a/app/src/main/java/com/amazonaws/kinesisvideo/demoapp/activity/WebRtcActivity.java +++ b/app/src/main/java/com/amazonaws/kinesisvideo/demoapp/activity/WebRtcActivity.java @@ -20,7 +20,6 @@ import android.content.Intent; import android.graphics.BitmapFactory; import android.media.AudioManager; -import android.os.Build; import android.os.Bundle; import android.util.Base64; import android.util.DisplayMetrics; @@ -277,54 +276,52 @@ public void onException(final Exception e) { // Step 11. Create SignalingServiceWebSocketClient. // This is the actual client that is used to send messages over the signaling channel. // SignalingServiceWebSocketClient will attempt to open the connection in its constructor. - if (wsHost != null) { - try { - client = new SignalingServiceWebSocketClient(wsHost, signalingListener, Executors.newFixedThreadPool(10)); - - Log.d(TAG, "Client connection " + (client.isOpen() ? "Successful" : "Failed")); - } catch (final Exception e) { - Log.e(TAG, "Exception with websocket client: " + e); - gotException = true; - return; - } + try { + client = new SignalingServiceWebSocketClient(wsHost, signalingListener, Executors.newFixedThreadPool(10)); - if (isValidClient()) { - - Log.d(TAG, "Client connected to Signaling service " + client.isOpen()); - - if (master) { - - // If webrtc endpoint is non-null ==> Ingest media was checked - if (webrtcEndpoint != null) { - new Thread(() -> { - try { - final AWSKinesisVideoWebRTCStorageClient storageClient = - new AWSKinesisVideoWebRTCStorageClient( - KinesisVideoWebRtcDemoApp.getCredentialsProvider().getCredentials()); - storageClient.setRegion(Region.getRegion(mRegion)); - storageClient.setSignerRegionOverride(mRegion); - storageClient.setServiceNameIntern("kinesisvideo"); - storageClient.setEndpoint(webrtcEndpoint); - - Log.i(TAG, "Channel ARN is: " + mChannelArn); - storageClient.joinStorageSession(new JoinStorageSessionRequest() - .withChannelArn(mChannelArn)); - Log.i(TAG, "Join storage session request sent!"); - } catch (Exception ex) { - Log.e(TAG, "Error sending join storage session request!", ex); - } - }).start(); - } - } else { - Log.d(TAG, "Signaling service is connected: " + - "Sending offer as viewer to remote peer"); // Viewer + Log.d(TAG, "Client connection " + (client.isOpen() ? "Successful" : "Failed")); + } catch (final Exception e) { + Log.e(TAG, "Exception with websocket client: " + e); + gotException = true; + return; + } - createSdpOffer(); + if (isValidClient()) { + + Log.d(TAG, "Client connected to Signaling service " + client.isOpen()); + + if (master) { + + // If webrtc endpoint is non-null ==> Ingest media was checked + if (webrtcEndpoint != null) { + new Thread(() -> { + try { + final AWSKinesisVideoWebRTCStorageClient storageClient = + new AWSKinesisVideoWebRTCStorageClient( + KinesisVideoWebRtcDemoApp.getCredentialsProvider().getCredentials()); + storageClient.setRegion(Region.getRegion(mRegion)); + storageClient.setSignerRegionOverride(mRegion); + storageClient.setServiceNameIntern("kinesisvideo"); + storageClient.setEndpoint(webrtcEndpoint); + + Log.i(TAG, "Channel ARN is: " + mChannelArn); + storageClient.joinStorageSession(new JoinStorageSessionRequest() + .withChannelArn(mChannelArn)); + Log.i(TAG, "Join storage session request sent!"); + } catch (Exception ex) { + Log.e(TAG, "Error sending join storage session request!", ex); + } + }).start(); } } else { - Log.e(TAG, "Error in connecting to signaling service"); - gotException = true; + Log.d(TAG, "Signaling service is connected: " + + "Sending offer as viewer to remote peer"); // Viewer + + createSdpOffer(); } + } else { + Log.e(TAG, "Error in connecting to signaling service"); + gotException = true; } } @@ -504,17 +501,15 @@ protected void onCreate(final Bundle savedInstanceState) { if (mUrisList != null) { for (int i = 0; i < mUrisList.size(); i++) { final String turnServer = mUrisList.get(i).toString(); - if (turnServer != null) { - assert mUserNames != null; - assert mPasswords != null; - final IceServer iceServer = IceServer.builder(turnServer.replace("[", "").replace("]", "")) - .setUsername(mUserNames.get(i)) - .setPassword(mPasswords.get(i)) - .createIceServer(); - - Log.d(TAG, "IceServer details (TURN) = " + iceServer.toString()); - peerIceServers.add(iceServer); - } + assert mUserNames != null; + assert mPasswords != null; + final IceServer iceServer = IceServer.builder(turnServer.replace("[", "").replace("]", "")) + .setUsername(mUserNames.get(i)) + .setPassword(mPasswords.get(i)) + .createIceServer(); + + Log.d(TAG, "IceServer details (TURN) = " + iceServer.toString()); + peerIceServers.add(iceServer); } } @@ -783,11 +778,7 @@ public void onStateChange() { if (sendDataChannelButton != null) { runOnUiThread(() -> { - if (localDataChannel.state() == DataChannel.State.OPEN) { - sendDataChannelButton.setEnabled(true); - } else { - sendDataChannelButton.setEnabled(false); - } + sendDataChannelButton.setEnabled(localDataChannel.state() == DataChannel.State.OPEN); }); } } @@ -991,30 +982,26 @@ public boolean onTouch(View view, MotionEvent motionEvent) { } private void resizeRemoteView() { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { - final DisplayMetrics displayMetrics = new DisplayMetrics(); - final ViewGroup.LayoutParams lp = remoteView.getLayoutParams(); - getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); - lp.height = (int) (displayMetrics.heightPixels * 0.75); - lp.width = (int) (displayMetrics.widthPixels * 0.75); - remoteView.setLayoutParams(lp); - localView.bringToFront(); - } + final DisplayMetrics displayMetrics = new DisplayMetrics(); + final ViewGroup.LayoutParams lp = remoteView.getLayoutParams(); + getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); + lp.height = (int) (displayMetrics.heightPixels * 0.75); + lp.width = (int) (displayMetrics.widthPixels * 0.75); + remoteView.setLayoutParams(lp); + localView.bringToFront(); } private void createNotificationChannel() { // Create the NotificationChannel, but only on API 26+ because // the NotificationChannel class is new and not in the support library - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - final CharSequence name = getString(R.string.data_channel_notification); - final String description = getString(R.string.data_channel_notification_description); - final int importance = NotificationManager.IMPORTANCE_HIGH; - final NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); - channel.setDescription(description); - // Register the channel with the system; you can't change the importance - // or other notification behaviors after this - final NotificationManager notificationManager = getSystemService(NotificationManager.class); - notificationManager.createNotificationChannel(channel); - } + final CharSequence name = getString(R.string.data_channel_notification); + final String description = getString(R.string.data_channel_notification_description); + final int importance = NotificationManager.IMPORTANCE_HIGH; + final NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); + channel.setDescription(description); + // Register the channel with the system; you can't change the importance + // or other notification behaviors after this + final NotificationManager notificationManager = getSystemService(NotificationManager.class); + notificationManager.createNotificationChannel(channel); } } diff --git a/app/src/main/java/com/amazonaws/kinesisvideo/demoapp/fragment/StreamWebRtcConfigurationFragment.java b/app/src/main/java/com/amazonaws/kinesisvideo/demoapp/fragment/StreamWebRtcConfigurationFragment.java index 3b4f8e7..824e553 100644 --- a/app/src/main/java/com/amazonaws/kinesisvideo/demoapp/fragment/StreamWebRtcConfigurationFragment.java +++ b/app/src/main/java/com/amazonaws/kinesisvideo/demoapp/fragment/StreamWebRtcConfigurationFragment.java @@ -1,7 +1,6 @@ package com.amazonaws.kinesisvideo.demoapp.fragment; import android.Manifest; -import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.os.AsyncTask; @@ -11,7 +10,6 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.view.inputmethod.InputMethodManager; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; @@ -19,7 +17,6 @@ import android.widget.EditText; import android.widget.ListView; import android.widget.Spinner; -import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -55,7 +52,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Optional; public class StreamWebRtcConfigurationFragment extends Fragment { private static final String TAG = StreamWebRtcConfigurationFragment.class.getSimpleName(); @@ -263,7 +259,7 @@ private Bundle setExtras(boolean isMaster) { extras.putString(KEY_STREAM_ARN, mStreamArn); extras.putBoolean(KEY_IS_MASTER, isMaster); - if (mIceServerList.size() > 0) { + if (!mIceServerList.isEmpty()) { ArrayList userNames = new ArrayList<>(mIceServerList.size()); ArrayList passwords = new ArrayList<>(mIceServerList.size()); ArrayList ttls = new ArrayList<>(mIceServerList.size()); diff --git a/app/src/main/java/com/amazonaws/kinesisvideo/signaling/model/Event.java b/app/src/main/java/com/amazonaws/kinesisvideo/signaling/model/Event.java index 65eceac..260bde3 100644 --- a/app/src/main/java/com/amazonaws/kinesisvideo/signaling/model/Event.java +++ b/app/src/main/java/com/amazonaws/kinesisvideo/signaling/model/Event.java @@ -3,6 +3,8 @@ import android.util.Base64; import android.util.Log; +import androidx.annotation.NonNull; + import com.google.common.base.Charsets; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -143,6 +145,7 @@ public static String parseOfferEvent(Event offerEvent) { .orElse(""); } + @NonNull @Override public String toString() { return "Event(" +