import android.content.Context;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
+import android.widget.FrameLayout;
import org.apache.log4j.Logger;
private Context mContext;
private SurfaceHolder mHolder;
private Callback mCallback;
+ private int width;
+ private int height;
public CameraView (Context context, Callback cb) {
super(context);
this.mCallback = cb;
}
+ public void setLayout(int width, int heitht){
+ this.height = heitht;
+ this.width = width;
+ gLogger.error("SurfacedCreated, screen size w: " + SCREEN_WIDTH + ", h:" + SCREEN_HEIGHT + ". preview size, w: " + width + ", h: " + height);
+
+ float ratio = (float) height / width; // 3/4,,, 640x480 1008x1344
+ FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)getLayoutParams();
+ gLogger.error("ratio = " + ratio + ". xxx=" + (float)(SCREEN_HEIGHT / SCREEN_HEIGHT));
+ if(ratio > (float)(SCREEN_HEIGHT / SCREEN_WIDTH) ){
+ lp.width = SCREEN_HEIGHT * width / height;
+ lp.height = SCREEN_HEIGHT;
+ lp.leftMargin = (SCREEN_WIDTH - lp.width ) / 2;
+ lp.topMargin = 0;
+ } else {
+ lp.width = SCREEN_WIDTH;
+ lp.height = SCREEN_WIDTH * height / width;
+ lp.leftMargin = 0;
+ lp.topMargin = (SCREEN_HEIGHT - lp.height) / 2;
+ }
+ this.setLayoutParams(lp);
+ }
+
// SurfaceHolder.Callback implementation
@Override
public void surfaceCreated(final SurfaceHolder holder){
- gLogger.error("SurfacedCreated");
+ gLogger.error("SurfacedCreated, screen size w: " + SCREEN_WIDTH + ", h:" + SCREEN_HEIGHT + ". preview size, w: " + width + ", h: " + height);
}
@Override
package ai.suanzi.rtmpclient;
import android.content.IntentFilter;
+import android.hardware.Camera;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import ai.suanzi.rtmpclient.MyService.LocalBinder;
import android.os.IBinder;
import android.net.ConnectivityManager;
+import android.view.ViewGroup;
//"rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90";
public class MainActivity extends AppCompatActivity implements MyService.MyServiceEventListener, CameraView.Callback {
- private static final String TAG = "MainActivity";
+ //private static final String TAG = "MainActivity";
private Logger gLogger;
private String mMacAddr = "";
final LogConfigurator logConfigurator = new LogConfigurator();
String fname = getExternalFilesDir(null) + File.separator + "log.txt";
logConfigurator.setFileName(fname);
- Log.e(TAG, "Log file is located at: " + fname);
logConfigurator.setRootLevel(Level.DEBUG);
logConfigurator.setLevel("org.apache", Level.ERROR);
logConfigurator.setMaxFileSize(1024 * 1024 * 10);
logConfigurator.configure();
gLogger = Logger.getLogger(getClass());
+ gLogger.error("#######################################");
+ gLogger.debug("Log file is located at: " + fname);
+
} catch (Exception e){
e.printStackTrace();
}
}
private void init(){
- configLog();
- gLogger.debug("#######################################");
// set config file
UserInfo.setConfigPath(getExternalFilesDir(null) + File.separator + "config");
if(mServer.setRtmpUrl(UserInfo.getConfig().toUrl(mMacAddr))){
//mServer.startPreview(mHolder);
mServer.startPreview(mCameraView.getHolder());
+ Camera.Size cs = mServer.getBestPictureSize();
+ mCameraView.setLayout(cs.width, cs.height);
}
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
///setContentView(R.layout.activity_main);
-
- DisplayMetrics outMetrics = new DisplayMetrics();
- this.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
-
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
+ DisplayMetrics outMetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
CameraView.SCREEN_WIDTH = outMetrics.widthPixels;
CameraView.SCREEN_HEIGHT = outMetrics.heightPixels;
+ gLogger.debug("Screen size is w: " + CameraView.SCREEN_WIDTH + ", h: " + CameraView.SCREEN_HEIGHT);
+
+
+
mCameraView = new CameraView(getApplicationContext(), this);
- setContentView(mCameraView);
+ setContentView(R.layout.activity_main);
+
+ ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(640, 480);
+ addContentView(mCameraView, lp);
+ //setContentView(mCameraView);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
+ configLog();
+ //gLogger.debug("#######################################");
initCameraView();
-
-
init();
loadConfig();
@Override
public void onCameraError(String msg){
gLogger.error("onCameraEvent " + msg);
- if(mUsbMonitor.hasUsbCamera()){
+ //if(mUsbMonitor.hasUsbCamera()){
//mServer.reopenCamera();
- }
+ //}
}
@Override
private long frameCount = 0;
// Preferred picture Size of the camera;
- private int width = 0;
- private int height = 0;
+ private Camera.Size mPreferredSize;
private MyServiceEventListener mListener;
public void startPreview (SurfaceHolder holder){
gLogger.debug("startPreview");
+ if(mCamera == null){
+ gLogger.error("startPreview - error: camera is null");
+ return;
+ }
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
public boolean setRtmpUrl (String url){
//this.rtmpUrl = url;
- if(mCamera == null){
- gLogger.error("setRtmpUrl, error mCamera is null");
+ if(mCamera == null || mPreferredSize == null){
+ gLogger.error("setRtmpUrl, error mCamera or PreferredSize is null");
return false;
}
- gLogger.error("setRtmpUrl - size: " + width + "x" + height + ". url: " + url);
- int ret = FfmpegHelper.initEncoder(width, height, url);
+ gLogger.error("setRtmpUrl - size: " + mPreferredSize.width + "x" + mPreferredSize.height + ". url: " + url);
+ int ret = FfmpegHelper.initEncoder(mPreferredSize.width, mPreferredSize.height, url);
if(ret != 0){
gLogger.error("setRtmpUrl, initEncoder error");
}
for(int[] i : paras.getSupportedPreviewFpsRange()){
gLogger.error("[" + i[0] + "," + i[1] + "]");
}
- width = preferredSize.width;
- height = preferredSize.height;
+ mPreferredSize = preferredSize;
paras.setPictureSize(preferredSize.width, preferredSize.height); // use 640x480 preferred
camera.setParameters(paras);
camera.setDisplayOrientation(0);
camera.setPreviewCallback(this);
}
+ public Camera.Size getBestPictureSize(){
+ return mPreferredSize;
+ }
+
// Camaer.onError callback
@Override
public void onError(int error, Camera camera){
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
+ android:background="@android:color/background_dark"
tools:context=".MainActivity">
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="18dp"
- android:layout_marginEnd="149dp"
- android:layout_marginStart="147dp"
- android:text="@string/btn"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent" />
-
- <SurfaceView
- android:id="@+id/surfaceView"
- android:layout_width="335dp"
- android:layout_height="178dp"
- android:layout_marginEnd="5dp"
- android:layout_marginStart="5dp"
- android:layout_marginTop="248dp"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.512"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- <android.support.design.widget.TextInputLayout
- android:id="@+id/textInputLayout"
- android:layout_width="0dp"
- android:layout_height="59dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="16dp"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent">
-
- <android.support.design.widget.TextInputEditText
- android:id="@+id/textServer"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="@string/labelServer"
- android:text="@string/rtmpServer" />
- </android.support.design.widget.TextInputLayout>
-
- <android.support.design.widget.TextInputLayout
- android:id="@+id/textInputLayout2"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_margin="5dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="15dp"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/textInputLayout"
- app:layout_goneMarginTop="10dp">
-
- <android.support.design.widget.TextInputEditText
- android:id="@+id/textUser"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="@string/labelUser"
- android:text="@string/user" />
- </android.support.design.widget.TextInputLayout>
-
- <android.support.design.widget.TextInputLayout
- android:id="@+id/textInputLayout3"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_margin="5dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="18dp"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/textInputLayout2">
-
- </android.support.design.widget.TextInputLayout>
-
</android.support.constraint.ConstraintLayout>
\ No newline at end of file