1 package ai.suanzi.rtmpclient;
3 import android.app.Activity;
4 import android.app.PendingIntent;
5 import android.content.BroadcastReceiver;
6 import android.content.IntentFilter;
7 import android.graphics.ImageFormat;
8 import android.graphics.SurfaceTexture;
9 import android.hardware.usb.UsbDevice;
10 import android.hardware.usb.UsbManager;
11 import android.os.Environment;
12 import android.support.design.widget.TextInputEditText;
13 import android.support.v7.app.AppCompatActivity;
14 import android.os.Bundle;
15 import android.text.TextUtils;
16 import android.util.Log;
17 import android.view.SurfaceHolder;
18 import android.hardware.Camera;
19 import android.view.SurfaceView;
20 import android.view.View;
21 import android.widget.Button;
22 import android.widget.Toast;
23 import android.content.Context;
24 import android.content.pm.PackageManager;
25 import android.os.AsyncTask;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.io.IOException;
32 import java.util.concurrent.Executor;
33 import java.util.concurrent.ExecutorService;
34 import java.util.concurrent.Executors;
35 import android.content.Intent;
36 import java.io.OutputStream;
37 import android.hardware.usb.UsbManager;
38 import android.hardware.usb.UsbDeviceConnection;
40 import de.mindpipe.android.logging.log4j.LogConfigurator;
41 import org.apache.log4j.Level;
42 import org.apache.log4j.Logger;
43 import android.net.wifi.WifiManager;
44 import android.net.wifi.WifiInfo;
45 import android.content.ServiceConnection;
46 import android.content.ComponentName;
48 import ai.suanzi.rtmpclient.MyService.LocalBinder;
49 import android.os.IBinder;
51 //"rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid";
53 public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, Camera.PreviewCallback{
55 private static final String TAG = "PENG";
56 private Ffmpeg ffmpeg;
57 private Camera mCamera ;
58 private StreamTask mStreamTask;
59 private SurfaceHolder mHolder;
60 private SurfaceView mSufaceView;
61 //private UVCCamera uvcCamera;
62 ExecutorService mExecutor = Executors.newSingleThreadExecutor();
63 //Intent it = new Intent(getApplicationContext(), MyService.class);
64 Intent intent = new Intent();
66 private UsbManager usbManager;
67 private UsbDevice usbCamera;
69 private Logger gLogger;
71 private Button mBtnStart;
72 private TextInputEditText mTextServer;
73 private TextInputEditText mTextUser;
74 private TextInputEditText mTextCamera;
75 private String mMacAddr = "";
76 private CameraView mCameraView;
78 private void configLog(){
80 final LogConfigurator logConfigurator = new LogConfigurator();
81 String fname = getExternalFilesDir(null) + File.separator + "log.txt";
82 logConfigurator.setFileName(fname);
83 Log.e(TAG, "Log file is located at: " + fname);
84 logConfigurator.setRootLevel(Level.DEBUG);
85 logConfigurator.setLevel("org.apache", Level.ERROR);
86 logConfigurator.setMaxFileSize(1024 * 1024 * 10);
87 logConfigurator.configure();
88 gLogger = Logger.getLogger(getClass());
89 } catch (Exception e){
96 gLogger.debug("#######################################");
97 UserInfo.setConfigPath(getExternalFilesDir(null) + File.separator + "config");
98 this.mMacAddr = getMacAddr();
100 ffmpeg = Ffmpeg.getInstance();
101 //uvcCamera = new UVCCamera();
103 mBtnStart = findViewById(R.id.button);
104 mTextServer = findViewById(R.id.textServer);
105 mTextUser = findViewById(R.id.textUser);
106 mTextCamera = findViewById(R.id.textCamera);
110 intent.setPackage(this.getPackageName());
111 intent.setAction("ai.suanzi.rtmpclient.service");
114 mSufaceView = findViewById(R.id.surfaceView);
115 mHolder = mSufaceView.getHolder();
116 mHolder.addCallback(this);
119 //mCamera = getCameraInstance();
120 //configCamera(mCamera);
127 ServiceConnection mConnection = new ServiceConnection() {
129 public void onServiceDisconnected(ComponentName name) {
130 Toast.makeText(MainActivity.this, "Service is disconnected", 1000).show();
131 gLogger.error("onServiceDisconnected");
138 public void onServiceConnected(ComponentName name, IBinder service) {
139 Toast.makeText(MainActivity.this, "Service is connected", 1000).show();
140 gLogger.error("onServiceConnected");
142 LocalBinder mLocalBinder = (LocalBinder)service;
143 mServer = mLocalBinder.getServiceInstance();
144 mServer.onChange(mHolder);
149 protected void onCreate(Bundle savedInstanceState) {
150 super.onCreate(savedInstanceState);
151 setContentView(R.layout.activity_main);
153 //mCameraView = new CameraView(this);
154 //intent.putExtra("url", "xxxxxxxxxxxxxxxxxx");
155 //intent.putExtra("view", mCameraView);
157 //startService(intent);
158 Intent mIntent = new Intent(this, MyService.class);
159 bindService(mIntent, mConnection, BIND_AUTO_CREATE);
165 mBtnStart.setOnClickListener(new View.OnClickListener(){
167 public void onClick(View view){
168 String url = mTextServer.getText().toString() + "/" + mTextUser.getText().toString() + "_" + mMacAddr + "_" + mTextCamera.getText().toString();
169 gLogger.error("----------> onClick, Url is: " + url);
170 intent.putExtra("url", url);
171 startService(intent);
178 protected void onPause(){
180 gLogger.debug("OnPause");
184 protected void onResume() {
186 gLogger.debug("OnResume");
190 protected void onStop() {
192 gLogger.debug("onStop");
196 protected void onStart(){
198 gLogger.debug("onStart");
202 protected void onDestroy(){
204 gLogger.debug("onDestroy");
208 protected void onRestart(){
210 gLogger.debug("onRestart");
214 // class StreamTask AsyncTask
215 private class StreamTask extends AsyncTask<Void, Void, Void>{
218 StreamTask(byte[] data){
223 protected Void doInBackground(Void... params) {
225 if (this.data != null){
226 Log.e(TAG, "fps: " + mCamera.getParameters().getPreviewFrameRate());
227 ffmpeg.process(this.data);
236 // SurfaceHolder.Callback implementation
238 public void surfaceCreated(final SurfaceHolder holder){
239 gLogger.error("SurfacedCreated");
243 public void surfaceChanged(SurfaceHolder holder, int format, int widht, int height){
244 gLogger.error("surfaceChanged");
247 if (mServer != null) {
248 mServer.onChange(holder);
251 /* final SurfaceHolder hh = holder;
252 Thread tr = new Thread(new Runnable() {
257 } catch (Exception e){
260 if (mServer != null) {
261 gLogger.error("onChange");
262 mServer.onChange(hh);
264 gLogger.error("service XXXXXXXXXXXXXXX");
274 mCamera.setPreviewDisplay(holder);
275 mCamera.startPreview();
276 } catch (Exception e){
277 gLogger.error("Error starting camera previewe: " + e.getMessage());
278 Toast.makeText(this, "Camera Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
284 public void surfaceDestroyed(SurfaceHolder holder){
285 Log.e(TAG, "surfaceDestroyed");
289 public void onPreviewFrame(final byte[] data, Camera camera){
290 gLogger.error("onPreviewFrame");
291 /*if (null != mStreamTask){
292 switch (mStreamTask.getStatus()){
294 Log.e(TAG, "onPreviewFrame Running");
297 Log.e(TAG,"OnPreviewFrame Pending");
298 mStreamTask.cancel(false);
302 mStreamTask = new StreamTask(data);
303 mStreamTask.execute((Void)null);
306 // ong endTime = System.currentTimeMillis();
307 // mExecutor.execute(new Runnable() {
309 // public void run() {
310 // //long encodeTime = System.currentTimeMillis();
311 // ffmpeg.process(data);
312 //Log.e(TAG, "编码第:" + (encodeCount++) + "帧,耗时:" + (System.currentTimeMillis() - encodeTime));
315 //Log.e(TAG, "采集第:" + (++count) + "帧,距上一帧间隔时间:"
316 // + (endTime - previewTime) + " " + Thread.currentThread().getName());
317 // previewTime = endTime;*/
322 private boolean checkCameraHardware(Context context) {
323 return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
326 private static Camera getCameraInstance(){
329 Log.e(TAG, "Open Camera");
331 } catch (Exception e){
337 private void configCamera(Camera camera){
338 Camera.Parameters paras = camera.getParameters();
339 gLogger.error("Supported Picture Sizes:");
340 for (Camera.Size cc : paras.getSupportedPictureSizes()){
341 gLogger.error(cc.width + "x" + cc.height);
343 gLogger.error("Supported Preview fps range:");
344 for(int[] i : paras.getSupportedPreviewFpsRange()){
345 gLogger.error("[" + i[0] + "," + i[1] + "]");
347 gLogger.error("Set parameters");
348 camera.setParameters(paras);
349 camera.setDisplayOrientation(0);
350 gLogger.error("Preview Format: " + paras.getPreviewFormat() + ". Size: " + paras.getPreviewSize().width + "x" + paras.getPreviewSize().height);
351 gLogger.error("Picture Format: " + paras.getPictureFormat() + ". Size: " + paras.getPictureSize().width + "x" + paras.getPictureSize().height);
354 camera.setPreviewDisplay(mHolder);
355 } catch (IOException e){
358 //camera.setPreviewCallback(this);
361 private void switchToBackground(){
362 Intent i = new Intent();
363 i.setAction(Intent.ACTION_MAIN);
364 i.addCategory(Intent.CATEGORY_HOME);
365 this.startActivity(i);
368 private String getMacAddr() {
369 WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
370 WifiInfo info = manager.getConnectionInfo();
371 return info.getMacAddress().replace(":", ""); //02:00:00:00:00:00 - 020000000000
374 private void loadConfig() {
375 UserInfo info = UserInfo.getConfig();
376 mTextServer.setText(info.server.equals("") ? "rtmp://gpussh.suanzi.ai:1935/myapp" : info.server);
377 mTextUser.setText(info.user.equals("") ? "suanzi" : info.user );
378 mTextCamera.setText(info.cameraId.equals("") ? "001" : info.cameraId);
379 mMacAddr = info.macAddr.equals("") ? this.mMacAddr : info.macAddr;
380 gLogger.debug("loadConfig " + info.toString());
383 private void saveConfig() {
384 UserInfo info = UserInfo.getConfig();
385 info.update(mTextServer.getText().toString(), mTextUser.getText().toString(), mMacAddr, mTextCamera.getText().toString());
386 if(info.saveConfig()) {
387 Toast.makeText(getApplicationContext(), "Config saved", Toast.LENGTH_LONG).show();
389 Toast.makeText(getApplicationContext(), "Error: config saved", Toast.LENGTH_LONG).show();
391 gLogger.error("saveConfig: " + info.toString());
394 private void changePermission(){
396 Log.e(TAG, "change permission");
397 //Process sh = Runtime.getRuntime().exec(new String[]{"su", "-c", "chmod 666 /dev/video0"});
399 Process sh = Runtime.getRuntime().exec("/system/xbin/su", null,null);
400 //Process sh = Runtime.getRuntime().exec("su", null,null);
402 OutputStream os = sh.getOutputStream();
403 os.write(("/system/bin/chmod 666 /dev/video0").getBytes("ASCII"));
404 //os.write(("/system/bin/echo 'wowo' >> /data/local/test").getBytes("ASCII"));
410 }catch (Exception e){