stream pushing ok without access permission of /dev/video0
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / MyService.java
index 768d95d..9053b35 100644 (file)
@@ -12,56 +12,44 @@ 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;
 
 public class MyService extends Service {
-    private static final String TAG = "MyService";
+
+    private Logger gLogger = Logger.getLogger("MyService");
+
     private Ffmpeg ffmpeg = Ffmpeg.getInstance();
     private  Boolean isRunning = false;
+    private FfmpegRunnable  runnable;
 
-    private Runnable runnable = new Runnable() {
+    private class FfmpegRunnable implements Runnable {
+        private String url;
+        public FfmpegRunnable(String _url){
+            this.url = _url;
+        }
         @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);
         }
-    };
-
-
-    /**
-     * id不可设置为0,否则不能设置为前台service
-     */
-    private static final int NOTIFICATION_DOWNLOAD_PROGRESS_ID = 0x0001;
-
-    //private boolean isRemove=false;//是否需要移除
+    }
 
-    /**
-     * Notification
-     */
+    private static final int NOTIFICATION_DOWNLOAD_PROGRESS_ID = 0x0001;                                        //id不可设置为0,否则不能设置为前台service
     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);
+        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;
@@ -70,14 +58,14 @@ public class MyService extends Service {
     @Override
     public void onCreate() {
         super.onCreate();
-        Log.e(TAG, "onCreate");
+        gLogger.error("onCreate");
     }
 
     @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 +73,26 @@ 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 = intent.getExtras().getString("url");
+        gLogger.error("onStartCommand: url is:" + url + ". isRunning: " + isRunning);
+        runnable = new FfmpegRunnable(url);
         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");
     }
-
 }