-
Notifications
You must be signed in to change notification settings - Fork 1k
/
StreamingFragment.java
303 lines (266 loc) · 12.6 KB
/
StreamingFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package com.amazonaws.kinesisvideo.demoapp.fragment;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraCharacteristics;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.util.Size;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.amazonaws.kinesisvideo.common.exception.KinesisVideoException;
import com.amazonaws.kinesisvideo.demoapp.KinesisVideoDemoApp;
import com.amazonaws.kinesisvideo.demoapp.R;
import com.amazonaws.kinesisvideo.demoapp.activity.SimpleNavActivity;
import com.amazonaws.kinesisvideo.client.KinesisVideoClient;
import com.amazonaws.mobileconnectors.kinesisvideo.client.KinesisVideoAndroidClientFactory;
import com.amazonaws.mobileconnectors.kinesisvideo.mediasource.android.AndroidCameraMediaSource;
import com.amazonaws.mobileconnectors.kinesisvideo.mediasource.android.AndroidCameraMediaSourceConfiguration;
import com.amazonaws.mobileconnectors.kinesisvideo.util.CameraHardwareCapabilitiesHelper;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class StreamingFragment extends Fragment implements TextureView.SurfaceTextureListener {
public static final String KEY_MEDIA_SOURCE_CONFIGURATION = "mediaSourceConfiguration";
public static final String KEY_STREAM_NAME = "streamName";
public static final String KEY_ROTATION = "rotation";
public static final String KEY_SHOULD_MAINTAIN_ASPECT_RATIO = "maintainAspectRatio";
public static final String KEY_SHOULD_FILL_SCREEN = "shouldFillScreen";
public static final String KEY_IS_MIRRORED = "isMirrored";
private static final String TAG = StreamingFragment.class.getSimpleName();
private Button mStartStreamingButton;
private KinesisVideoClient mKinesisVideoClient;
private String mStreamName;
private AndroidCameraMediaSourceConfiguration mConfiguration;
private AndroidCameraMediaSource mCameraMediaSource;
private TextureView mTextureView;
private float mRotation;
private boolean mShouldMaintainAspectRatio;
private boolean mShouldFillScreen;
private boolean mIsMirrored;
private Size cameraPreviewSize;
private SimpleNavActivity navActivity;
public static StreamingFragment newInstance(SimpleNavActivity navActivity) {
StreamingFragment s = new StreamingFragment();
s.navActivity = navActivity;
return s;
}
@Override
public View onCreateView(final LayoutInflater inflater,
final ViewGroup container,
final Bundle savedInstanceState) {
getArguments().setClassLoader(AndroidCameraMediaSourceConfiguration.class.getClassLoader());
mStreamName = getArguments().getString(KEY_STREAM_NAME);
mConfiguration = getArguments().getParcelable(KEY_MEDIA_SOURCE_CONFIGURATION);
mRotation = getArguments().getFloat(KEY_ROTATION);
mShouldMaintainAspectRatio = getArguments().getBoolean(KEY_SHOULD_MAINTAIN_ASPECT_RATIO);
mShouldFillScreen = getArguments().getBoolean(KEY_SHOULD_FILL_SCREEN);
mIsMirrored = getArguments().getBoolean(KEY_IS_MIRRORED);
Log.d(TAG, "mStreamName=" + mStreamName);
Log.d(TAG, "mRotation=" + mRotation);
Log.d(TAG, "mShouldMaintainAspectRatio=" + mShouldMaintainAspectRatio);
Log.d(TAG, "mShouldFillScreen=" + mShouldFillScreen);
Log.d(TAG, "mIsMirrored=" + mIsMirrored);
final View view = inflater.inflate(R.layout.fragment_streaming, container, false);
mTextureView = (TextureView) view.findViewById(R.id.texture);
mTextureView.setSurfaceTextureListener(this);
cameraPreviewSize = null;
return view;
}
private void createClientAndStartStreaming(final SurfaceTexture previewTexture) {
try {
mKinesisVideoClient = KinesisVideoAndroidClientFactory.createKinesisVideoClient(
getActivity(),
KinesisVideoDemoApp.KINESIS_VIDEO_REGION,
KinesisVideoDemoApp.getCredentialsProvider());
mCameraMediaSource = (AndroidCameraMediaSource) mKinesisVideoClient
.createMediaSource(mStreamName, mConfiguration);
mCameraMediaSource.setPreviewSurfaces(new Surface(previewTexture));
resumeStreaming();
} catch (final KinesisVideoException e) {
Log.e(TAG, "unable to start streaming");
throw new RuntimeException("unable to start streaming", e);
}
}
@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
mStartStreamingButton = (Button) view.findViewById(R.id.start_streaming);
mStartStreamingButton.setOnClickListener(stopStreamingWhenClicked());
}
@Override
public void onResume() {
super.onResume();
resumeStreaming();
}
@Override
public void onPause() {
super.onPause();
pauseStreaming();
}
private View.OnClickListener stopStreamingWhenClicked() {
return new View.OnClickListener() {
@Override
public void onClick(final View view) {
pauseStreaming();
navActivity.startConfigFragment();
}
};
}
private void resumeStreaming() {
try {
if (mCameraMediaSource == null) {
return;
}
mCameraMediaSource.start();
Toast.makeText(getActivity(), "resumed streaming", Toast.LENGTH_SHORT).show();
mStartStreamingButton.setText(getActivity().getText(R.string.stop_streaming));
} catch (final KinesisVideoException e) {
Log.e(TAG, "unable to resume streaming", e);
Toast.makeText(getActivity(), "failed to resume streaming", Toast.LENGTH_SHORT).show();
}
}
private void pauseStreaming() {
try {
if (mCameraMediaSource == null) {
return;
}
mCameraMediaSource.stop();
Toast.makeText(getActivity(), "stopped streaming", Toast.LENGTH_SHORT).show();
mStartStreamingButton.setText(getActivity().getText(R.string.start_streaming));
} catch (final KinesisVideoException e) {
Log.e(TAG, "unable to pause streaming", e);
Toast.makeText(getActivity(), "failed to pause streaming", Toast.LENGTH_SHORT).show();
}
}
/**
* Updates the cameraPreviewSize. If the aspect ratio should be maintained, then choose from the
* pool of sizes that the camera device supports.
*
* @param width the width of the surface
* @param height the height of the surface
*/
private void updatePreviewSize(int width, int height) {
Log.d(TAG, "Choosing preview size...");
if (mShouldMaintainAspectRatio) {
cameraPreviewSize = chooseBestPreviewSize(width, height);
} else if (mRotation % 180 == 0) {
cameraPreviewSize = new Size(height, width);
} else {
cameraPreviewSize = new Size(width, height);
}
Log.d(TAG, "Preview size is: " + cameraPreviewSize.toString());
updateTransform(width, height);
}
/**
* Chooses the largest Size from the pool of possible sizes, where both dimensions are larger than
* the width and height.
*
* @param width the width of the surface
* @param height the height of the surface
*/
private Size chooseBestPreviewSize(int width, int height) {
List<Size> allSizes = CameraHardwareCapabilitiesHelper.getSupportedResolutionsForYUV420_888(getContext(), mConfiguration.getCameraId());
List<Size> possibleSizes = new ArrayList<>();
int longDimension = Math.max(width, height);
int shortDimension = Math.min(width, height);
for (Size possibility : allSizes) {
if (possibility.getWidth() > longDimension && possibility.getHeight() > shortDimension) {
possibleSizes.add(possibility);
}
}
if (possibleSizes.size() >= 1) {
return Collections.min(possibleSizes, new Comparator<Size>() {
@Override
public int compare(Size s1, Size s2) {
return Long.signum(s1.getWidth() * s1.getHeight() - s2.getWidth() * s2.getHeight());
}
});
}
return allSizes.get(0);
}
/**
* Sets up the transform matrix in order to rotate the video preview. This only changes what
* is displayed on the app screen. Video sent to AWS KVS is unaffected by this.
* The matrix will first map the pixels to fit the cameraPreviewSize.
* Then, the matrix will apply scaling to fit the screen.
* Finally, the matrix will perform a rotation, if necessary.
* If the option is checked, the preview will be mirrored after the rotation.
*
* @param width the width of the surface
* @param height the height of the surface
*/
private void updateTransform(int width, int height) {
if (width != 0 && height != 0 && mTextureView != null && cameraPreviewSize != null) {
Log.d(TAG, "Updating the matrix with width=" + width + ", height=" + height);
Matrix transformationMatrix = new Matrix();
RectF inputRectF = new RectF(0, 0, mTextureView.getWidth(), mTextureView.getHeight());
RectF outputRectF = new RectF(0, 0, cameraPreviewSize.getHeight(), cameraPreviewSize.getWidth()); // since it's offset by 90, the width and height are switched
// see https://source.android.com/compatibility/android-cdd#7_5_5_camera_orientation for more info
float centerX = inputRectF.centerX();
float centerY = inputRectF.centerY();
outputRectF.offset(centerX - outputRectF.centerX(),
centerY - outputRectF.centerY());
transformationMatrix.setRectToRect(inputRectF, outputRectF, Matrix.ScaleToFit.FILL);
Log.d(TAG, "inputRectF: " + inputRectF.toShortString());
Log.d(TAG, "outputRectF: " + outputRectF.toShortString());
if (mShouldMaintainAspectRatio) {
float scale1, scale2;
if (mRotation % 180 == 0) {
scale1 = (float) height / cameraPreviewSize.getWidth();
scale2 = (float) width / cameraPreviewSize.getHeight();
} else {
scale1 = (float) height / cameraPreviewSize.getHeight();
scale2 = (float) width / cameraPreviewSize.getWidth();
}
float scale = mShouldFillScreen ? Math.max(scale1, scale2) : Math.min(scale1, scale2);
transformationMatrix.postScale(scale, scale, centerX, centerY);
}
transformationMatrix.postRotate(mRotation, centerX, centerY);
mTextureView.setTransform(transformationMatrix);
if (mIsMirrored) {
// https://source.android.com/compatibility/android-cdd#7_5_2_front-facing_camera
// Front-facing camera is mirrored by default. You need to mirror it again to un-mirror it.
if ((mConfiguration.getCameraFacing() == CameraCharacteristics.LENS_FACING_FRONT) == (mRotation % 180 == 0)) {
mTextureView.setScaleX(-1);
} else {
mTextureView.setScaleY(-1);
}
}
}
}
////
// TextureView.SurfaceTextureListener methods
////
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
surfaceTexture.setDefaultBufferSize(1280, 720);
updatePreviewSize(width, height);
createClientAndStartStreaming(surfaceTexture);
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
try {
if (mCameraMediaSource != null)
mCameraMediaSource.stop();
if (mKinesisVideoClient != null)
mKinesisVideoClient.stopAllMediaSources();
KinesisVideoAndroidClientFactory.freeKinesisVideoClient();
} catch (final KinesisVideoException e) {
Log.e(TAG, "failed to release kinesis video client", e);
}
return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
}