X-Git-Url: http://47.100.26.94:8080/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fai%2Fsuanzi%2Frtmpclient%2FMyService.java;h=a9ef972efd35e53b25660a2a04cd7114a11f5328;hb=7a99b2b0d2cf8048e1d9dd7fae5ccf984a865b1e;hp=9053b35ef3ba234b26328425d3e4fef018432d24;hpb=831cc09829bc6e18d8d0d8bb78063e89ea565ce9;p=rtmpclient.git diff --git a/app/src/main/java/ai/suanzi/rtmpclient/MyService.java b/app/src/main/java/ai/suanzi/rtmpclient/MyService.java index 9053b35..a9ef972 100644 --- a/app/src/main/java/ai/suanzi/rtmpclient/MyService.java +++ b/app/src/main/java/ai/suanzi/rtmpclient/MyService.java @@ -2,11 +2,16 @@ 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; @@ -14,31 +19,89 @@ 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 { +public class MyService extends Service implements Camera.PreviewCallback { - private Logger gLogger = Logger.getLogger("MyService"); + private static Logger gLogger = Logger.getLogger("MyService"); + private static String TAG = "MyService"; - private Ffmpeg ffmpeg = Ffmpeg.getInstance(); + //private Ffmpeg ffmpeg = Ffmpeg.getInstance(); + private FfmpegHelper helper; private Boolean isRunning = false; - private FfmpegRunnable runnable; + private Camera mCamera = null; + IBinder mBinder = new LocalBinder(); + private String rtmpUrl; + private long frameCount = 0; + + + public class LocalBinder extends Binder { + public MyService getServiceInstance(){ + return MyService.this; + } + } + + public Camera getCameraInstance() { + if (mCamera == null) { + CameraHandlerThread mThread = new CameraHandlerThread("camera thread"); + synchronized (mThread) { + mThread.openCamera(); + } + } + if (mCamera == null){ + gLogger.error("getCameraInstance, camera is null"); + } + return mCamera; + } + + private void openCameraOriginal() { + try { + gLogger.error("openCameraOriginal"); + mCamera = Camera.open(1); + } catch (Exception e) { + gLogger.error("camera is not available. error: " + e.getMessage()); + } + } + + private class CameraHandlerThread extends HandlerThread { + Handler mHandler; + + public CameraHandlerThread(String name) { + super(name); + gLogger.error("CameraHandlerThread: " + name); + start(); + mHandler = new Handler(getLooper()); + } - private class FfmpegRunnable implements Runnable { - private String url; - public FfmpegRunnable(String _url){ - this.url = _url; + synchronized void notifyCameraOpened() { + notify(); } - @Override - public void run(){ - gLogger.error("Run Ffmpeg url: " + url); - isRunning = true; - ffmpeg.push(null, this.url); + + void openCamera() { + mHandler.post(new Runnable() { + @Override + public void run() { + openCameraOriginal(); + notifyCameraOpened(); + } + }); + try { + wait(); + } catch (InterruptedException e) { + gLogger.error("wait was interrupted"); + } } } private static final int NOTIFICATION_DOWNLOAD_PROGRESS_ID = 0x0001; //id不可设置为0,否则不能设置为前台service - public void createNotification(){ - gLogger.error("createNotification"); + private void createNotification(){ + gLogger.debug("createNotification"); NotificationCompat.Builder builder=new NotificationCompat.Builder(this); //使用兼容版本 builder.setSmallIcon(R.mipmap.ic_launcher); //设置状态栏的通知图标 builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_background)); //设置通知栏横条的图标 @@ -52,41 +115,37 @@ public class MyService extends Service { @Override public IBinder onBind(Intent intent) { - return null; + return mBinder; } @Override public void onCreate() { super.onCreate(); - gLogger.error("onCreate"); + gLogger.error("onCreate ---> "); + createNotification(); + Toast.makeText(this, "Video stream pushed to " + this.rtmpUrl, Toast.LENGTH_LONG).show(); + mCamera = getCameraInstance(); + configCamera(mCamera); } @Override public void onDestroy() { stopForeground(true); - Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show(); - gLogger.error( "onDestroy"); super.onDestroy(); - + gLogger.error( "onDestroy --------->"); + Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show(); + if(mCamera != null){ + mCamera.stopPreview(); + mCamera.release(); + } } - @Override - public void onStart(Intent intent, int startid){ - super.onStart(intent, startid); - gLogger.error("onStart"); - - } @Override public int onStartCommand(Intent intent, int flags, int startId) { - String url = intent.getExtras().getString("url"); - gLogger.error("onStartCommand: url is:" + url + ". isRunning: " + isRunning); - runnable = new FfmpegRunnable(url); - if (!isRunning) { - createNotification(); - Toast.makeText(this, "Video stream pushed to " + url, Toast.LENGTH_LONG).show(); - new Thread(runnable).start(); - } + gLogger.error("onStartCommand"); + //nrunnable = new FfmpegRunnable("xxx", this); + //new Thread(runnable).start(); return START_STICKY; } @@ -95,4 +154,65 @@ public class MyService extends Service { super.onLowMemory(); gLogger.error("onLowMemory"); } + + // Camera.PreviewCallback + @Override + public void onPreviewFrame(final byte[] data, Camera camera){ + if(frameCount % (15 * 60) == 0) { + gLogger.error("onPreviewFrame"); + } + frameCount++; + //ffmpeg.process(data); + } + + public void startPreview (SurfaceHolder holder){ + gLogger.error("startPreview"); + if (mCamera == null){ + gLogger.error("startPreview - error: camera is null"); + return; + } + try { + mCamera.setPreviewDisplay(holder); + mCamera.startPreview(); + } catch (Exception e){ + gLogger.error("startPreview - error: " + e.getMessage()); + e.printStackTrace(); + } + } + + public boolean setRtmpUrl (String url){ + this.rtmpUrl = url; + Camera.Parameters param = mCamera.getParameters(); + int width = param.getPictureSize().width; + int height = param.getPictureSize().height; + gLogger.error("setRtmpUrl - size: " + width + "x" + height + ". url: " + url); + //int ret = ffmpeg.initnew(width, height, url); + int ret = FfmpegHelper.initialEncoder(width, height, url); + return ret == 0 ? true : false; + } + + private void configCamera(Camera camera){ + if(mCamera == null){ + gLogger.error("configCamera - camera is null"); + return; + } + Camera.Parameters paras = camera.getParameters(); + gLogger.error("Supported Picture Sizes:"); + Camera.Size preferredSize = paras.getSupportedPictureSizes().get(0); + for (Camera.Size cc : paras.getSupportedPictureSizes()){ + if (cc.width == 640) + preferredSize = cc; + gLogger.error(cc.width + "x" + cc.height); + } + gLogger.error("Supported Preview fps range:"); + for(int[] i : paras.getSupportedPreviewFpsRange()){ + gLogger.error("[" + i[0] + "," + i[1] + "]"); + } + paras.setPictureSize(preferredSize.width, preferredSize.height); // use 640x480 preferred + 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); + camera.setPreviewCallback(this); + } }