compat camera1 and camera2

pull/209/head
xufuji456 3 years ago
parent 35ad5b2763
commit c3a66f2571
  1. 34
      Live/src/main/java/com/frank/live/LivePusherNew.java
  2. 6
      Live/src/main/java/com/frank/live/stream/CameraType.java
  3. 63
      Live/src/main/java/com/frank/live/stream/VideoStream.java
  4. 22
      Live/src/main/java/com/frank/live/stream/VideoStreamBase.java
  5. 23
      Live/src/main/java/com/frank/live/stream/VideoStreamNew.java
  6. 25
      app/src/main/java/com/frank/ffmpeg/activity/LiveActivity.kt
  7. 2
      app/src/main/res/layout/activity_live.xml

@ -2,14 +2,16 @@ package com.frank.live;
import android.app.Activity;
import android.view.SurfaceHolder;
import android.view.TextureView;
import android.view.View;
import com.frank.live.listener.LiveStateChangeListener;
import com.frank.live.listener.OnFrameDataCallback;
import com.frank.live.param.AudioParam;
import com.frank.live.param.VideoParam;
import com.frank.live.stream.AudioStream;
import com.frank.live.stream.CameraType;
import com.frank.live.stream.VideoStream;
import com.frank.live.stream.VideoStreamBase;
import com.frank.live.stream.VideoStreamNew;
public class LivePusherNew implements OnFrameDataCallback {
@ -34,25 +36,25 @@ public class LivePusherNew implements OnFrameDataCallback {
}
private final AudioStream audioStream;
private VideoStream videoStream;
// private VideoStreamNew videoStream;
private VideoStreamBase videoStream;
private LiveStateChangeListener liveStateChangeListener;
private Activity activity;
private final Activity activity;
public LivePusherNew(Activity activity, VideoParam videoParam, AudioParam audioParam) {
public LivePusherNew(Activity activity,
VideoParam videoParam,
AudioParam audioParam,
View view,
CameraType cameraType) {
this.activity = activity;
native_init();
videoStream = new VideoStream(this, activity, videoParam.getWidth(), videoParam.getHeight(),
videoParam.getBitRate(), videoParam.getFrameRate(), videoParam.getCameraId());
audioStream = new AudioStream(this, audioParam);
}
public LivePusherNew(Activity activity, VideoParam videoParam, AudioParam audioParam, TextureView textureView) {
native_init();
// videoStream = new VideoStreamNew(this, textureView, videoParam, activity);
audioStream = new AudioStream(this, audioParam);
if (cameraType == CameraType.CAMERA1) {
videoStream = new VideoStream(this, view, videoParam, activity);
} else if (cameraType == CameraType.CAMERA2) {
videoStream = new VideoStreamNew(this, view, videoParam, activity);
}
}
public void setPreviewDisplay(SurfaceHolder surfaceHolder) {
@ -130,15 +132,11 @@ public class LivePusherNew implements OnFrameDataCallback {
}
}
public void start(String path) {
native_start(path);
}
private int getInputSamplesFromNative() {
return native_getInputSamples();
}
public void setVideoCodecInfo(int width, int height, int frameRate, int bitrate) {
private void setVideoCodecInfo(int width, int height, int frameRate, int bitrate) {
native_setVideoCodecInfo(width, height, frameRate, bitrate);
}

@ -0,0 +1,6 @@
package com.frank.live.stream;
public enum CameraType {
CAMERA1,
CAMERA2
}

@ -1,13 +1,17 @@
package com.frank.live.stream;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.View;
import com.frank.live.listener.OnFrameDataCallback;
import com.frank.live.param.VideoParam;
public class VideoStream implements Camera.PreviewCallback, CameraHelper.OnChangedSizeListener {
public class VideoStream extends VideoStreamBase implements Camera.PreviewCallback,
CameraHelper.OnChangedSizeListener {
private final OnFrameDataCallback mCallback;
@ -17,58 +21,57 @@ public class VideoStream implements Camera.PreviewCallback, CameraHelper.OnChang
private boolean isLiving;
public VideoStream(OnFrameDataCallback callback,
Activity activity,
int width,
int height,
int bitrate,
int frameRate,
int cameraId) {
mCallback = callback;
mBitrate = bitrate;
mFrameRate = frameRate;
cameraHelper = new CameraHelper(activity, cameraId, width, height);
View view,
VideoParam videoParam,
Context context) {
mCallback = callback;
mBitrate = videoParam.getBitRate();
mFrameRate = videoParam.getFrameRate();
cameraHelper = new CameraHelper((Activity) context,
videoParam.getCameraId(),
videoParam.getWidth(),
videoParam.getHeight());
cameraHelper.setPreviewCallback(this);
cameraHelper.setOnChangedSizeListener(this);
}
@Override
public void setPreviewDisplay(SurfaceHolder surfaceHolder) {
cameraHelper.setPreviewDisplay(surfaceHolder);
}
/**
* preview data
*
* @param data data
* @param camera camera
*/
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if (isLiving && mCallback != null) {
mCallback.onVideoFrame(data, null, null, null);
}
}
public void switchCamera() {
cameraHelper.switchCamera();
}
@Override
public void onChanged(int w, int h) {
if (mCallback != null) {
mCallback.onVideoCodecInfo(w, h, mFrameRate, mBitrate);
}
}
public void startLive() {
isLiving = true;
}
@Override
public void stopLive() {
isLiving = false;
}
@Override
public void release() {
cameraHelper.release();
}
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if (isLiving && mCallback != null) {
mCallback.onVideoFrame(data, null, null, null);
}
}
@Override
public void onChanged(int w, int h) {
if (mCallback != null) {
mCallback.onVideoCodecInfo(w, h, mFrameRate, mBitrate);
}
}
}

@ -0,0 +1,22 @@
package com.frank.live.stream;
import android.view.SurfaceHolder;
/**
* Base of VideoStream
* Created by frank on 2022/01/27.
*/
public abstract class VideoStreamBase {
public abstract void startLive();
public abstract void setPreviewDisplay(SurfaceHolder surfaceHolder);
public abstract void switchCamera();
public abstract void stopLive();
public abstract void release();
}

@ -8,6 +8,7 @@ import android.util.Log;
import android.util.Size;
import android.view.SurfaceHolder;
import android.view.TextureView;
import android.view.View;
import com.frank.live.camera2.Camera2Helper;
import com.frank.live.camera2.Camera2Listener;
@ -18,7 +19,8 @@ import com.frank.live.param.VideoParam;
* Pushing video stream: using Camera2
* Created by frank on 2020/02/12.
*/
public class VideoStreamNew implements TextureView.SurfaceTextureListener, Camera2Listener {
public class VideoStreamNew extends VideoStreamBase
implements TextureView.SurfaceTextureListener, Camera2Listener {
private static final String TAG = VideoStreamNew.class.getSimpleName();
@ -30,20 +32,17 @@ public class VideoStreamNew implements TextureView.SurfaceTextureListener, Camer
private final OnFrameDataCallback mCallback;
public VideoStreamNew(OnFrameDataCallback callback,
TextureView textureView,
View view,
VideoParam videoParam,
Context context) {
this.mCallback = callback;
this.mTextureView = textureView;
// just support TextureView now
this.mTextureView = (TextureView) view;
this.mVideoParam = videoParam;
this.mContext = context;
mTextureView.setSurfaceTextureListener(this);
}
public void setPreviewDisplay(SurfaceHolder surfaceHolder) {
// cameraHelper.setPreviewDisplay(surfaceHolder);
}
/**
* start previewing
*/
@ -52,7 +51,6 @@ public class VideoStreamNew implements TextureView.SurfaceTextureListener, Camer
if (mContext instanceof Activity) {
rotateDegree = ((Activity) mContext).getWindowManager().getDefaultDisplay().getRotation();
}
Log.e(TAG, "preview width=" + mTextureView.getWidth() + "--height=" + mTextureView.getHeight());
camera2Helper = new Camera2Helper.Builder()
.cameraListener(this)
.specificCameraId(Camera2Helper.CAMERA_ID_BACK)
@ -64,20 +62,29 @@ public class VideoStreamNew implements TextureView.SurfaceTextureListener, Camer
camera2Helper.start();
}
@Override
public void setPreviewDisplay(SurfaceHolder surfaceHolder) {
}
@Override
public void switchCamera() {
if (camera2Helper != null) {
camera2Helper.switchCamera();
}
}
@Override
public void startLive() {
isLiving = true;
}
@Override
public void stopLive() {
isLiving = false;
}
@Override
public void release() {
if (camera2Helper != null) {
camera2Helper.stop();

@ -9,6 +9,7 @@ import android.os.Handler
import android.os.Message
import android.text.TextUtils
import android.util.Log
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
import android.widget.CompoundButton
@ -23,6 +24,7 @@ import com.frank.live.listener.LiveStateChangeListener
import com.frank.live.param.AudioParam
import com.frank.live.param.VideoParam
import com.frank.live.LivePusherNew
import com.frank.live.stream.CameraType
/**
* Realtime living with rtmp stream
@ -30,7 +32,7 @@ import com.frank.live.LivePusherNew
*/
open class LiveActivity : BaseActivity(), CompoundButton.OnCheckedChangeListener, LiveStateChangeListener, OnNetworkChangeListener {
private var textureView: SurfaceView? = null
private var liveView: View? = null
private var livePusher: LivePusherNew? = null
private var isPushing = false
private var connectionReceiver: ConnectionReceiver? = null
@ -64,23 +66,28 @@ open class LiveActivity : BaseActivity(), CompoundButton.OnCheckedChangeListener
initViewsWithClick(R.id.btn_swap)
(findViewById<View>(R.id.btn_live) as ToggleButton).setOnCheckedChangeListener(this)
(findViewById<View>(R.id.btn_mute) as ToggleButton).setOnCheckedChangeListener(this)
textureView = getView(R.id.surface_camera)
liveView = getView(R.id.surface_camera)
}
private fun initPusher() {
val width = 640//resolution
val width = 640
val height = 480
val videoBitRate = 800000//kb/s
val videoFrameRate = 10//fps
val videoBitRate = 800000 // kb/s
val videoFrameRate = 10 // fps
val videoParam = VideoParam(width, height,
Integer.valueOf(Camera2Helper.CAMERA_ID_BACK), videoBitRate, videoFrameRate)
val sampleRate = 44100//sample rate: Hz
val sampleRate = 44100
val channelConfig = AudioFormat.CHANNEL_IN_STEREO
val audioFormat = AudioFormat.ENCODING_PCM_16BIT
val numChannels = 2//channel number
val numChannels = 2
val audioParam = AudioParam(sampleRate, channelConfig, audioFormat, numChannels)
livePusher = LivePusherNew(this, videoParam, audioParam)
livePusher!!.setPreviewDisplay(textureView!!.holder)
// Camera1: SurfaceView Camera2: TextureView
livePusher = LivePusherNew(this, videoParam, audioParam, liveView, CameraType.CAMERA2)
var holder :SurfaceHolder ?= null
if (liveView is SurfaceView) {
holder = (liveView as SurfaceView).holder
}
livePusher!!.setPreviewDisplay(holder)
}
private fun registerBroadcast(networkChangeListener: OnNetworkChangeListener) {

@ -6,7 +6,7 @@
android:layout_height="match_parent"
tools:context="com.frank.ffmpeg.activity.LiveActivity">
<SurfaceView
<TextureView
android:id="@+id/surface_camera"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Loading…
Cancel
Save