Android-WebRTC-实现摄像头显示

  • EglBase是什么?

它提供了一个接口,用于在Android平台上创建和管理EGL(嵌入式系统图形库)上下文,以便在WebRTC中进行图像和视频的处理和渲染。

  • Capturer, Source, Track, Sink分别是什么?
  • Capturer(采集器)是指用于采集音频或视频数据的设备或软件。它可以是麦克风、摄像头或其他采集设备。
  • Source(源)是指从采集器获取的原始音频或视频数据。它是数据的起点,并可能包含噪音、干扰或其他不必要的信息。
  • Track(跟踪器)是指对源数据进行处理和分析的组件。它可以包括音频或视频的编码、滤波、分割、特征提取等算法和技术。
  • Sink(接收器)是指从跟踪器输出的经过处理的音频或视频数据的目标或目的地。它可以是扬声器、显示器或其他接收设备。
  • activity_rtc.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/dp_20"
    >

    <org.webrtc.SurfaceViewRenderer
        android:id="@+id/localView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintHeight_percent="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • RtcActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;

import org.webrtc.Camera1Enumerator;
import org.webrtc.EglBase;
import org.webrtc.PeerConnectionFactory;
import org.webrtc.SurfaceTextureHelper;
import org.webrtc.SurfaceViewRenderer;
import org.webrtc.VideoCapturer;
import org.webrtc.VideoSource;
import org.webrtc.VideoTrack;

public class RtcActivity extends Activity {

    private static final String TAG = "RtcRemoteActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rtc);

        //  factory static init
        PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions
                .builder(this)
                .createInitializationOptions());
        PeerConnectionFactory.Options options = new PeerConnectionFactory.Options();

        //  factory create
        PeerConnectionFactory peerConnectionFactory = PeerConnectionFactory.builder()
                .setOptions(options).createPeerConnectionFactory();

        // create videoCapturer
        Camera1Enumerator camera1Enumerator = new Camera1Enumerator(false);// false输出到surface
        String[] deviceNames = camera1Enumerator.getDeviceNames();
        VideoCapturer videoCapturer = null;
        for (String deviceName : deviceNames) {
            if (camera1Enumerator.isFrontFacing(deviceName)) {
                VideoCapturer capturer = camera1Enumerator.createCapturer(deviceName, null);
                if (capturer != null) {
                    videoCapturer = capturer;
                    break;
                }
                Log.e(TAG, "onCreate: create capturer fail");
                return;
            }
        }
        if (videoCapturer == null) {
            return;
        }

        //  create videoSource
        VideoSource videoSource = peerConnectionFactory.createVideoSource(videoCapturer.isScreencast());

        //  init videoCapturer
        EglBase.Context eglBaseContext = EglBase.create().getEglBaseContext();
        SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.create("surfaceTexture", eglBaseContext);
        videoCapturer.initialize(surfaceTextureHelper, this, videoSource.getCapturerObserver());
        videoCapturer.startCapture(480, 640, 30);// width, height, frame

        //  create videoTrack
        VideoTrack videoTrack = peerConnectionFactory.createVideoTrack("videoTrack-1", videoSource);

        //  get show view
        SurfaceViewRenderer localView = findViewById(R.id.localView);
        localView.setMirror(true);// 镜像
        localView.init(eglBaseContext, null);

        //  link track to view so that data show in view
        videoTrack.addSink(localView);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值