pushing and previewing work when switch background/foreground
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / MyService.java
index c5ec172..1d9dc60 100644 (file)
@@ -2,82 +2,134 @@ package ai.suanzi.rtmpclient;
 
 import android.app.Service;
 import android.content.Intent;
+import android.graphics.SurfaceTexture;
+import android.hardware.Camera;
 import android.os.Handler;
 import android.os.HandlerThread;
 import android.os.IBinder;
 import android.os.Looper;
 import android.util.Log;
+import android.view.SurfaceHolder;
+import android.view.SurfaceView;
+import android.view.WindowManager;
 import android.widget.Toast;
 import android.support.v4.app.NotificationCompat;
 import android.graphics.BitmapFactory;
 import android.app.Notification;
 import android.os.Message;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import android.hardware.Camera.PreviewCallback;
+import android.os.IBinder;
+import android.os.Binder;
+import android.content.Context;
+import android.graphics.PixelFormat;
+import java.io.IOException;
+import android.view.Gravity;
+
+public class MyService extends Service  implements Camera.PreviewCallback {
+
+    private Logger gLogger = Logger.getLogger("MyService");
 
-public class MyService extends Service {
-    private static final String TAG = "MyService";
-    private Ffmpeg ffmpeg = new Ffmpeg();
+    private Ffmpeg ffmpeg = Ffmpeg.getInstance();
     private  Boolean isRunning = false;
+    private FfmpegRunnable  runnable;
+    private Camera mCamera = null;
+    IBinder mBinder = new LocalBinder();
+    private WindowManager mWindowManager;
+    private SurfaceView mOutComeVideoView;
+
+
+    public class LocalBinder extends Binder {
+        public MyService getServiceInstance(){
+            return MyService.this;
+        }
+    }
 
-    private Runnable runnable = new Runnable() {
+    private class FfmpegRunnable implements Runnable {
+        private String url;
+        private Camera.PreviewCallback cb;
+        public FfmpegRunnable(String _url, Camera.PreviewCallback _cb){
+            this.url = _url;
+            this.cb = _cb;
+        }
         @Override
-        public void run() {
-            Log.e(TAG, "Run ffmpeg");
+        public void run(){
+            gLogger.error("Run Ffmpeg url: " + url);
             isRunning = true;
-            ffmpeg.push(null);
+            //ffmpeg.push(null, this.url);
+            if (mCamera == null) {
+                gLogger.error("open camea");
+                try {
+                    mCamera = Camera.open(1);
+                }catch (Exception e){
+                    e.printStackTrace();
+                }
+            }
+            /*
+            SurfaceTexture st = new SurfaceTexture(0);
+            try {
+                mCamera.setPreviewTexture(st);
+            }catch (Exception e){
+                e.printStackTrace();
+            }*/
+            gLogger.error("start preview");
+            mCamera.setPreviewCallback(this.cb);
+            mCamera.startPreview();
+
         }
-    };
-
-
-    /**
-     * id不可设置为0,否则不能设置为前台service
-     */
-    private static final int NOTIFICATION_DOWNLOAD_PROGRESS_ID = 0x0001;
-
-    //private boolean isRemove=false;//是否需要移除
-
-    /**
-     * Notification
-     */
-    public void createNotification(){
-        Log.e(TAG, "create notification");
-        //使用兼容版本
-        NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
-        //设置状态栏的通知图标
-        builder.setSmallIcon(R.mipmap.ic_launcher);
-        //设置通知栏横条的图标
-        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_background));
-        //禁止用户点击删除按钮删除
-        builder.setAutoCancel(false);
-        //禁止滑动删除
-        builder.setOngoing(true);
-        //右上角的时间显示
-        builder.setShowWhen(true);
-        //设置通知栏的标题内容
-        builder.setContentTitle("Rtmp Foreground Service!!!");
-        //创建通知
-        Notification notification = builder.build();
-        //设置为前台服务
-        startForeground(NOTIFICATION_DOWNLOAD_PROGRESS_ID,notification);
     }
 
-
+    private static final int NOTIFICATION_DOWNLOAD_PROGRESS_ID = 0x0001;                                        //id不可设置为0,否则不能设置为前台service
+    private void createNotification(){
+        gLogger.error("createNotification");
+        NotificationCompat.Builder builder=new NotificationCompat.Builder(this);                        //使用兼容版本
+        builder.setSmallIcon(R.mipmap.ic_launcher);                                                             //设置状态栏的通知图标
+        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_background));   //设置通知栏横条的图标
+        builder.setAutoCancel(false);                                                                           //禁止用户点击删除按钮删除
+        builder.setOngoing(true);                                                                               //禁止滑动删除
+        builder.setShowWhen(true);                                                                              //右上角的时间显示
+        builder.setContentTitle("Rtmp Foreground Service!!!");                                                  //设置通知栏的标题内容
+        Notification notification = builder.build();                                                            //创建通知
+        startForeground(NOTIFICATION_DOWNLOAD_PROGRESS_ID,notification);                                        //设置为前台服务
+    }
 
     @Override
     public IBinder onBind(Intent intent) {
-        return null;
+        return mBinder;
     }
 
     @Override
     public void onCreate() {
         super.onCreate();
-        Log.e(TAG, "onCreate");
+        gLogger.error("onCreate");
+
+
+//        mWindowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
+//        mOutComeVideoView = new SurfaceView(this);
+//
+//
+//        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(1, 1, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
+//                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT);
+//        layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
+//        mWindowManager.addView(mOutComeVideoView, layoutParams);
+//        mOutComeVideoView.getHolder().addCallback(this);
+//
+//
+        createNotification();
+        runnable = new FfmpegRunnable("xxxxxx", this);
+        new Thread(runnable).start();
+
+
+
+
     }
 
     @Override
     public void onDestroy() {
         stopForeground(true);
         Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
-        Log.e(TAG, "onDestroy");
+        gLogger.error( "onDestroy");
         super.onDestroy();
 
     }
@@ -85,41 +137,77 @@ public class MyService extends Service {
     @Override
     public void onStart(Intent intent, int startid){
         super.onStart(intent, startid);
-        Log.e(TAG, "onStart");
+        gLogger.error("onStart");
 
     }
 
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
-        Log.e(TAG, "onStartCommand");
+        String url = "hahahahhah"; //intent.getExtras().getString("url");
+
+
+        gLogger.error("onStartCommand: url is:" + url + ". isRunning: " + isRunning);
+        runnable = new FfmpegRunnable(url, this);
         if (!isRunning) {
             createNotification();
-            Toast.makeText(this, "Ffmpeg started", Toast.LENGTH_LONG).show();
+            Toast.makeText(this, "Video stream pushed to " + url, Toast.LENGTH_LONG).show();
             new Thread(runnable).start();
         }
-
-//        int i=intent.getExtras().getInt("cmd");
-//        if(i==0){
-//            if(!isRemove) {
-//
-//                createNotification();
-//            }
-//            isRemove=true;
-//        }else {
-//            //移除前台服务
-//            if (isRemove) {
-//                stopForeground(true);
-//            }
-//            isRemove=false;
-//        }
-        //super.onStartCommand(intent, flags, startId);
         return START_STICKY;
     }
 
     @Override
     public void onLowMemory(){
         super.onLowMemory();
-        Log.e(TAG, "onLowMemory");
+        gLogger.error("onLowMemory");
+    }
+
+    // Camera.PreviewCallback
+    @Override
+    public void  onPreviewFrame(final byte[] data, Camera camera){
+        gLogger.error("onPreviewFrame");
+        ffmpeg.process(data);
     }
 
+    public void onChange (SurfaceHolder holder){
+        gLogger.error("onChange");
+        try {
+            if (holder == null){
+                gLogger.error("xxxx holder is null xxxxxx");
+            }
+            if (mCamera == null) {
+                gLogger.error("xxxx camera is null xxxx");
+            }
+
+            mCamera.setPreviewDisplay(holder);
+            mCamera.startPreview();
+        } catch (Exception e){
+            e.printStackTrace();
+        }
+    }
+
+
+
+//    // 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");
+//        try {
+//            mCamera.setPreviewDisplay(holder);
+//        } catch (Exception e){
+//            e.printStackTrace();
+//        }
+//        mCamera.startPreview();
+//
+//    }
+//
+//    @Override
+//    public void surfaceDestroyed(SurfaceHolder holder){
+//        gLogger.error("surfaceDestroyed");
+//    }
 }