1 package org.freedesktop.gstreamer.vulkan;
3 import android.content.Context;
4 import android.util.AttributeSet;
5 import android.util.Log;
6 import android.view.SurfaceView;
7 import android.view.View;
9 // A simple SurfaceView whose width and height can be set from the outside
10 public class GStreamerSurfaceView extends SurfaceView {
11 public int media_width = 320;
12 public int media_height = 240;
14 // Mandatory constructors, they do not do much
15 public GStreamerSurfaceView(Context context, AttributeSet attrs,
17 super(context, attrs, defStyle);
20 public GStreamerSurfaceView(Context context, AttributeSet attrs) {
21 super(context, attrs);
24 public GStreamerSurfaceView (Context context) {
28 // Called by the layout manager to find out our size and give us some rules.
29 // We will try to maximize our size, and preserve the media's aspect ratio if
30 // we are given the freedom to do so.
32 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
33 int width = 0, height = 0;
34 int wmode = View.MeasureSpec.getMode(widthMeasureSpec);
35 int hmode = View.MeasureSpec.getMode(heightMeasureSpec);
36 int wsize = View.MeasureSpec.getSize(widthMeasureSpec);
37 int hsize = View.MeasureSpec.getSize(heightMeasureSpec);
39 Log.i ("GStreamer", "onMeasure called with " + media_width + "x" + media_height);
42 case View.MeasureSpec.AT_MOST:
43 if (hmode == View.MeasureSpec.EXACTLY) {
44 width = Math.min(hsize * media_width / media_height, wsize);
47 case View.MeasureSpec.EXACTLY:
50 case View.MeasureSpec.UNSPECIFIED:
56 case View.MeasureSpec.AT_MOST:
57 if (wmode == View.MeasureSpec.EXACTLY) {
58 height = Math.min(wsize * media_height / media_width, hsize);
61 case View.MeasureSpec.EXACTLY:
64 case View.MeasureSpec.UNSPECIFIED:
65 height = media_height;
68 // Finally, calculate best size when both axis are free
69 if (hmode == View.MeasureSpec.AT_MOST && wmode == View.MeasureSpec.AT_MOST) {
70 int correct_height = width * media_height / media_width;
71 int correct_width = height * media_width / media_height;
73 if (correct_height < height)
74 height = correct_height;
76 width = correct_width;
80 width = Math.max (getSuggestedMinimumWidth(), width);
81 height = Math.max (getSuggestedMinimumHeight(), height);
82 setMeasuredDimension(width, height);