X-Git-Url: http://47.100.26.94:8080/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fai%2Fsuanzi%2Frtmpclient%2FMainActivity.java;h=6531b9f4ad090e909e1a0f593c43e86478d9384c;hb=f8fb13b9804d9b4b6fb227fbd07b80de9b092b55;hp=31fcbf30cbff3899c687698a98c59e9765e0fb3c;hpb=214619593e74f6a04fa25f3614a99cf6b9704da5;p=rtmpclient.git diff --git a/app/src/main/java/ai/suanzi/rtmpclient/MainActivity.java b/app/src/main/java/ai/suanzi/rtmpclient/MainActivity.java index 31fcbf3..6531b9f 100644 --- a/app/src/main/java/ai/suanzi/rtmpclient/MainActivity.java +++ b/app/src/main/java/ai/suanzi/rtmpclient/MainActivity.java @@ -1,18 +1,414 @@ package ai.suanzi.rtmpclient; +import android.app.Activity; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.IntentFilter; +import android.graphics.ImageFormat; +import android.graphics.SurfaceTexture; +import android.hardware.usb.UsbDevice; +import android.hardware.usb.UsbManager; +import android.os.Environment; +import android.support.design.widget.TextInputEditText; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.text.TextUtils; import android.util.Log; +import android.view.SurfaceHolder; +import android.hardware.Camera; +import android.view.SurfaceView; +import android.view.View; +import android.widget.Button; +import android.widget.Toast; +import android.content.Context; +import android.content.pm.PackageManager; +import android.os.AsyncTask; -public class MainActivity extends AppCompatActivity { +import java.io.File; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.io.IOException; +import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import android.content.Intent; +import java.io.OutputStream; +import android.hardware.usb.UsbManager; +import android.hardware.usb.UsbDeviceConnection; + +import de.mindpipe.android.logging.log4j.LogConfigurator; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import android.net.wifi.WifiManager; +import android.net.wifi.WifiInfo; +import android.content.ServiceConnection; +import android.content.ComponentName; + +import ai.suanzi.rtmpclient.MyService.LocalBinder; +import android.os.IBinder; + +//"rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid"; + +public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, Camera.PreviewCallback{ private static final String TAG = "PENG"; - private Ffmpeg ffmpeg = new Ffmpeg(); + private Ffmpeg ffmpeg; + private Camera mCamera ; + private StreamTask mStreamTask; + private SurfaceHolder mHolder; + private SurfaceView mSufaceView; + //private UVCCamera uvcCamera; + ExecutorService mExecutor = Executors.newSingleThreadExecutor(); + //Intent it = new Intent(getApplicationContext(), MyService.class); + Intent intent = new Intent(); + + private UsbManager usbManager; + private UsbDevice usbCamera; + + private Logger gLogger; + + private Button mBtnStart; + private TextInputEditText mTextServer; + private TextInputEditText mTextUser; + private TextInputEditText mTextCamera; + private String mMacAddr = ""; + private CameraView mCameraView; + + private void configLog(){ + try { + 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()); + } catch (Exception e){ + e.printStackTrace(); + } + } + + private void init(){ + configLog(); + gLogger.debug("#######################################"); + UserInfo.setConfigPath(getExternalFilesDir(null) + File.separator + "config"); + this.mMacAddr = getMacAddr(); + + ffmpeg = Ffmpeg.getInstance(); + //uvcCamera = new UVCCamera(); + + mBtnStart = findViewById(R.id.button); + mTextServer = findViewById(R.id.textServer); + mTextUser = findViewById(R.id.textUser); + mTextCamera = findViewById(R.id.textCamera); + loadConfig(); + + // init service + intent.setPackage(this.getPackageName()); + intent.setAction("ai.suanzi.rtmpclient.service"); + + // init surface view + mSufaceView = findViewById(R.id.surfaceView); + mHolder = mSufaceView.getHolder(); + mHolder.addCallback(this); + + // camera + //mCamera = getCameraInstance(); + //configCamera(mCamera); + + } + + + boolean mBounded; + MyService mServer; + ServiceConnection mConnection = new ServiceConnection() { + @Override + public void onServiceDisconnected(ComponentName name) { + Toast.makeText(MainActivity.this, "Service is disconnected", 1000).show(); + gLogger.error("onServiceDisconnected"); + + mBounded = false; + mServer = null; + } + + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + Toast.makeText(MainActivity.this, "Service is connected", 1000).show(); + gLogger.error("onServiceConnected"); + mBounded = true; + LocalBinder mLocalBinder = (LocalBinder)service; + mServer = mLocalBinder.getServiceInstance(); + mServer.onChange(mHolder); + } + }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - Log.e(TAG, "Ffmpeg Version: " + ffmpeg.getVersion()); + init(); + //mCameraView = new CameraView(this); + //intent.putExtra("url", "xxxxxxxxxxxxxxxxxx"); + //intent.putExtra("view", mCameraView); + + //startService(intent); + Intent mIntent = new Intent(this, MyService.class); + bindService(mIntent, mConnection, BIND_AUTO_CREATE); + + + + + + mBtnStart.setOnClickListener(new View.OnClickListener(){ + @Override + public void onClick(View view){ + String url = mTextServer.getText().toString() + "/" + mTextUser.getText().toString() + "_" + mMacAddr + "_" + mTextCamera.getText().toString(); + gLogger.error("----------> onClick, Url is: " + url); + intent.putExtra("url", url); + startService(intent); + saveConfig(); + } + }); + } + + @Override + protected void onPause(){ + super.onPause(); + gLogger.debug("OnPause"); + } + + @Override + protected void onResume() { + super.onResume(); + gLogger.debug("OnResume"); + } + + @Override + protected void onStop() { + super.onStop(); + gLogger.debug("onStop"); + } + + @Override + protected void onStart(){ + super.onStart(); + gLogger.debug("onStart"); + } + + @Override + protected void onDestroy(){ + super.onDestroy(); + gLogger.debug("onDestroy"); + } + + @Override + protected void onRestart(){ + super.onStart(); + gLogger.debug("onRestart"); + } + + + // class StreamTask AsyncTask + private class StreamTask extends AsyncTask{ + private byte[] data; + + StreamTask(byte[] data){ + this.data = data; + } + + @Override + protected Void doInBackground(Void... params) { + + if (this.data != null){ + Log.e(TAG, "fps: " + mCamera.getParameters().getPreviewFrameRate()); + ffmpeg.process(this.data); + } + return null; + } + } + + + + + // SurfaceHolder.Callback implementation + @Override + public void surfaceCreated(final SurfaceHolder holder){ + gLogger.error("SurfacedCreated"); + } + + @Override + public void surfaceChanged(SurfaceHolder holder, int format, int widht, int height){ + gLogger.error("surfaceChanged"); + mHolder = holder; + + if (mServer != null) { + mServer.onChange(holder); + } + + /* final SurfaceHolder hh = holder; + Thread tr = new Thread(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(3000); + } catch (Exception e){ + e.printStackTrace(); + } + if (mServer != null) { + gLogger.error("onChange"); + mServer.onChange(hh); + }else { + gLogger.error("service XXXXXXXXXXXXXXX"); + } + + } + }); + tr.run();*/ + + + /* + try { + mCamera.setPreviewDisplay(holder); + mCamera.startPreview(); + } catch (Exception e){ + gLogger.error("Error starting camera previewe: " + e.getMessage()); + Toast.makeText(this, "Camera Error: " + e.getMessage(), Toast.LENGTH_LONG).show(); + } + */ + } + + @Override + public void surfaceDestroyed(SurfaceHolder holder){ + Log.e(TAG, "surfaceDestroyed"); + } + + @Override + public void onPreviewFrame(final byte[] data, Camera camera){ + gLogger.error("onPreviewFrame"); + /*if (null != mStreamTask){ + switch (mStreamTask.getStatus()){ + case RUNNING: + Log.e(TAG, "onPreviewFrame Running"); + return; + case PENDING: + Log.e(TAG,"OnPreviewFrame Pending"); + mStreamTask.cancel(false); + break; + } + } + mStreamTask = new StreamTask(data); + mStreamTask.execute((Void)null); +*/ + +// ong endTime = System.currentTimeMillis(); +// mExecutor.execute(new Runnable() { +// @Override +// public void run() { +// //long encodeTime = System.currentTimeMillis(); +// ffmpeg.process(data); + //Log.e(TAG, "编码第:" + (encodeCount++) + "帧,耗时:" + (System.currentTimeMillis() - encodeTime)); +// } +// }); + //Log.e(TAG, "采集第:" + (++count) + "帧,距上一帧间隔时间:" +// + (endTime - previewTime) + " " + Thread.currentThread().getName()); +// previewTime = endTime;*/ + + } + + // private + private boolean checkCameraHardware(Context context) { + return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA); + } + + private static Camera getCameraInstance(){ + Camera c = null; + try { + Log.e(TAG, "Open Camera"); + c = Camera.open(1); + } catch (Exception e){ + e.printStackTrace(); + } + return c; + } + + private void configCamera(Camera camera){ + Camera.Parameters paras = camera.getParameters(); + gLogger.error("Supported Picture Sizes:"); + for (Camera.Size cc : paras.getSupportedPictureSizes()){ + gLogger.error(cc.width + "x" + cc.height); + } + gLogger.error("Supported Preview fps range:"); + for(int[] i : paras.getSupportedPreviewFpsRange()){ + gLogger.error("[" + i[0] + "," + i[1] + "]"); + } + gLogger.error("Set parameters"); + camera.setParameters(paras); + camera.setDisplayOrientation(0); + gLogger.error("Preview Format: " + paras.getPreviewFormat() + ". Size: " + paras.getPreviewSize().width + "x" + paras.getPreviewSize().height); + gLogger.error("Picture Format: " + paras.getPictureFormat() + ". Size: " + paras.getPictureSize().width + "x" + paras.getPictureSize().height); + + try { + camera.setPreviewDisplay(mHolder); + } catch (IOException e){ + e.printStackTrace(); + } + //camera.setPreviewCallback(this); + } + + private void switchToBackground(){ + Intent i = new Intent(); + i.setAction(Intent.ACTION_MAIN); + i.addCategory(Intent.CATEGORY_HOME); + this.startActivity(i); + } + + private String getMacAddr() { + WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); + WifiInfo info = manager.getConnectionInfo(); + return info.getMacAddress().replace(":", ""); //02:00:00:00:00:00 - 020000000000 + } + + private void loadConfig() { + UserInfo info = UserInfo.getConfig(); + mTextServer.setText(info.server.equals("") ? "rtmp://gpussh.suanzi.ai:1935/myapp" : info.server); + mTextUser.setText(info.user.equals("") ? "suanzi" : info.user ); + mTextCamera.setText(info.cameraId.equals("") ? "001" : info.cameraId); + mMacAddr = info.macAddr.equals("") ? this.mMacAddr : info.macAddr; + gLogger.debug("loadConfig " + info.toString()); + } + + private void saveConfig() { + UserInfo info = UserInfo.getConfig(); + info.update(mTextServer.getText().toString(), mTextUser.getText().toString(), mMacAddr, mTextCamera.getText().toString()); + if(info.saveConfig()) { + Toast.makeText(getApplicationContext(), "Config saved", Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(getApplicationContext(), "Error: config saved", Toast.LENGTH_LONG).show(); + } + gLogger.error("saveConfig: " + info.toString()); + } + + private void changePermission(){ + try { + Log.e(TAG, "change permission"); + //Process sh = Runtime.getRuntime().exec(new String[]{"su", "-c", "chmod 666 /dev/video0"}); + + Process sh = Runtime.getRuntime().exec("/system/xbin/su", null,null); + //Process sh = Runtime.getRuntime().exec("su", null,null); + + OutputStream os = sh.getOutputStream(); + os.write(("/system/bin/chmod 666 /dev/video0").getBytes("ASCII")); + //os.write(("/system/bin/echo 'wowo' >> /data/local/test").getBytes("ASCII")); + os.flush(); + os.close(); + sh.waitFor(); + + + }catch (Exception e){ + e.printStackTrace(); + } } }