Merging gst-examples
[platform/upstream/gstreamer.git] / subprojects / gst-examples / webrtc / android / app / src / main / java / org / freedesktop / gstreamer / webrtc / GStreamerSurfaceView.java
1 /* GStreamer
2  *
3  * Copyright (C) 2014 Sebastian Dröge <sebastian@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 package org.freedesktop.gstreamer.webrtc;
22
23 import android.content.Context;
24 import android.util.AttributeSet;
25 import android.util.Log;
26 import android.view.SurfaceView;
27 import android.view.View;
28
29 // A simple SurfaceView whose width and height can be set from the outside
30 public class GStreamerSurfaceView extends SurfaceView {
31     public int media_width = 320;
32     public int media_height = 240;
33
34     // Mandatory constructors, they do not do much
35     public GStreamerSurfaceView(Context context, AttributeSet attrs,
36             int defStyle) {
37         super(context, attrs, defStyle);
38     }
39
40     public GStreamerSurfaceView(Context context, AttributeSet attrs) {
41         super(context, attrs);
42     }
43
44     public GStreamerSurfaceView (Context context) {
45         super(context);
46     }
47
48     // Called by the layout manager to find out our size and give us some rules.
49     // We will try to maximize our size, and preserve the media's aspect ratio if
50     // we are given the freedom to do so.
51     @Override
52     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
53         if (media_width == 0 || media_height == 0) {
54             super.onMeasure(widthMeasureSpec, heightMeasureSpec);
55             return;
56         }
57
58         int width = 0, height = 0;
59         int wmode = View.MeasureSpec.getMode(widthMeasureSpec);
60         int hmode = View.MeasureSpec.getMode(heightMeasureSpec);
61         int wsize = View.MeasureSpec.getSize(widthMeasureSpec);
62         int hsize = View.MeasureSpec.getSize(heightMeasureSpec);
63
64         Log.i ("GStreamer", "onMeasure called with " + media_width + "x" + media_height);
65         // Obey width rules
66         switch (wmode) {
67         case View.MeasureSpec.AT_MOST:
68             if (hmode == View.MeasureSpec.EXACTLY) {
69                 width = Math.min(hsize * media_width / media_height, wsize);
70                 break;
71             }
72         case View.MeasureSpec.EXACTLY:
73             width = wsize;
74             break;
75         case View.MeasureSpec.UNSPECIFIED:
76             width = media_width;
77         }
78
79         // Obey height rules
80         switch (hmode) {
81         case View.MeasureSpec.AT_MOST:
82             if (wmode == View.MeasureSpec.EXACTLY) {
83                 height = Math.min(wsize * media_height / media_width, hsize);
84                 break;
85             }
86         case View.MeasureSpec.EXACTLY:
87             height = hsize;
88             break;
89         case View.MeasureSpec.UNSPECIFIED:
90             height = media_height;
91         }
92
93         // Finally, calculate best size when both axis are free
94         if (hmode == View.MeasureSpec.AT_MOST && wmode == View.MeasureSpec.AT_MOST) {
95             int correct_height = width * media_height / media_width;
96             int correct_width = height * media_width / media_height;
97
98             if (correct_height < height)
99                 height = correct_height;
100             else
101                 width = correct_width;
102         }
103
104         // Obey minimum size
105         width = Math.max (getSuggestedMinimumWidth(), width);
106         height = Math.max (getSuggestedMinimumHeight(), height);
107         setMeasuredDimension(width, height);
108     }
109
110 }