3 * Copyright (C) 2014 Sebastian Dröge <sebastian@centricular.com>
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.
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.
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.
21 package org.freedesktop.gstreamer.webrtc;
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;
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;
34 // Mandatory constructors, they do not do much
35 public GStreamerSurfaceView(Context context, AttributeSet attrs,
37 super(context, attrs, defStyle);
40 public GStreamerSurfaceView(Context context, AttributeSet attrs) {
41 super(context, attrs);
44 public GStreamerSurfaceView (Context context) {
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.
52 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
53 if (media_width == 0 || media_height == 0) {
54 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
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);
64 Log.i ("GStreamer", "onMeasure called with " + media_width + "x" + media_height);
67 case View.MeasureSpec.AT_MOST:
68 if (hmode == View.MeasureSpec.EXACTLY) {
69 width = Math.min(hsize * media_width / media_height, wsize);
72 case View.MeasureSpec.EXACTLY:
75 case View.MeasureSpec.UNSPECIFIED:
81 case View.MeasureSpec.AT_MOST:
82 if (wmode == View.MeasureSpec.EXACTLY) {
83 height = Math.min(wsize * media_height / media_width, hsize);
86 case View.MeasureSpec.EXACTLY:
89 case View.MeasureSpec.UNSPECIFIED:
90 height = media_height;
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;
98 if (correct_height < height)
99 height = correct_height;
101 width = correct_width;
105 width = Math.max (getSuggestedMinimumWidth(), width);
106 height = Math.max (getSuggestedMinimumHeight(), height);
107 setMeasuredDimension(width, height);