1.首先安装 android studio软件

安装方法网上有很多,这里不详细说。

2.android studio打开项目

在目录下载源码 android 客户端源码:
https://github.com/Computician/janus-gateway-android

如果android studio中已经打开了某个项目,则在菜单的File - Open,然后在弹出的目录选择对话框中选中项目的主目录(janus-gateway-android 解压目录),点‘OK’。
如果android studio中没有已经打开的项目,则直接在欢迎对话框里选‘Open an existing android studio project’。

3.安装时确保环境变量已设置好

系统变量下找到Path这个变量
C:\Program Files\Java\jdk1.8.0_91

另外我的android studio 必须配置Jdk8以上,否则无法设置成功。

4.设置android studio JDK

这里写图片描述
这里写图片描述
必须要设置C:\Program Files\Java\jdk1.8.0_91

4.常见错误

例如这样的错误,Error:The SDK Build Tools revision (23.0.3)
一般是工程的SDK版与已下载不一致,所以可以下载需要的版本,也可以修改项目。我是直接下载了。
然后生成成功。

5.查找janus服务器IP

在当前窗口查找文本【Ctrl+F】
在当前工程内查找文本【Ctrl+Shift+F】
找到下面的内容
private final String JANUS_URI = “ws://192.168.1.197:8188”;

默认运行的是echotest插件,所以在文件EchoTest.java文件中。

把上面的IP修改成你自己搭建的janus服务器IP,怎么搭建请参考我前面的博文。这样就可以进行通信了。

只有设置好正确的 IP之后就可以看到视频,如果不修改无法运行。

输出的apk目录在 项目目录\janus-gateway-android-master\janus-gateway-android-master\app\build\outputs\apk

6.测试结果

我把对应的akp安装在手机上进行测试,效果如下:
这里写图片描述
其中主要内容还是实现webrtc相关的接口函数,例EchoTest

package computician.janusclient;

import android.content.Context;
import android.opengl.EGLContext;
import android.util.Log;

import org.json.JSONObject;
import org.webrtc.MediaStream;
import org.webrtc.PeerConnection;
import org.webrtc.VideoRenderer;
import org.webrtc.VideoRendererGui;

import java.util.ArrayList;
import java.util.List;

import computician.janusclientapi.IJanusGatewayCallbacks;
import computician.janusclientapi.IJanusPluginCallbacks;
import computician.janusclientapi.IPluginHandleWebRTCCallbacks;
import computician.janusclientapi.JanusMediaConstraints;
import computician.janusclientapi.JanusPluginHandle;
import computician.janusclientapi.JanusServer;
import computician.janusclientapi.JanusSupportedPluginPackages;
import computician.janusclientapi.PluginHandleSendMessageCallbacks;

/**
 * Created by ben.trent on 7/24/2015.
 */

//TODO create message classes unique to this plugin

public class EchoTest {


    //private final String JANUS_URI = "ws://192.168.1.197:8188";
    private final String JANUS_URI = "ws://39.105.105.159:8188";
    //private final String JANUS_URI = "ws://140.82.15.14:8188";

    private JanusPluginHandle handle = null;
    private final VideoRenderer.Callbacks localRender, remoteRender;
    private final JanusServer janusServer;

    public class JanusGlobalCallbacks implements IJanusGatewayCallbacks {

        @Override
        public void onSuccess() {
            janusServer.Attach(new JanusPluginCallbacks());
        }

        @Override
        public void onDestroy() {
        }

        @Override
        public String getServerUri() {
            return JANUS_URI;
        }

        @Override
        public List<PeerConnection.IceServer> getIceServers() {
            return new ArrayList<PeerConnection.IceServer>();
        }

        @Override
        public Boolean getIpv6Support() {
            return Boolean.FALSE;
        }

        @Override
        public Integer getMaxPollEvents() {
            return 0;
        }

        @Override
        public void onCallbackError(String error) {

        }
    }

    public class JanusPluginCallbacks implements IJanusPluginCallbacks {

        @Override
        public void success(JanusPluginHandle pluginHandle) {
            EchoTest.this.handle = pluginHandle;

                JSONObject msg = new JSONObject();
                JSONObject obj = new JSONObject();
                try {
                    obj.put("audio", true);
                    obj.put("video", true);
                    msg.put("message", obj);
                    handle.sendMessage(new PluginHandleSendMessageCallbacks(msg));
                } catch (Exception ex) {

                }

            handle.createOffer(new IPluginHandleWebRTCCallbacks() {
                @Override
                public JSONObject getJsep() {
                    return null;
                }

                @Override
                public void onCallbackError(String error) {

                }

                @Override
                public Boolean getTrickle() {
                    return true;
                }

                @Override
                public JanusMediaConstraints getMedia() {
                    return new JanusMediaConstraints();
                }

                @Override
                public void onSuccess(JSONObject obj) {
                    Log.d("JANUSCLIENT", "OnSuccess for CreateOffer called");
                    try {
                        JSONObject body = new JSONObject();
                        JSONObject msg = new JSONObject();
                        body.put("audio", true);
                        body.put("video", true);
                        msg.put("message", body);
                        msg.put("jsep", obj);
                        handle.sendMessage(new PluginHandleSendMessageCallbacks(msg));
                    } catch (Exception ex) {

                    }
                }
            });

        }

        @Override
        public void onMessage(JSONObject msg, final JSONObject jsepLocal) {
            if(jsepLocal != null)
            {
                handle.handleRemoteJsep(new IPluginHandleWebRTCCallbacks() {
                    final JSONObject myJsep = jsepLocal;
                    @Override
                    public void onSuccess(JSONObject obj) {

                    }

                    @Override
                    public JSONObject getJsep() {
                        return myJsep;
                    }

                    @Override
                    public JanusMediaConstraints getMedia() {
                        return null;
                    }

                    @Override
                    public Boolean getTrickle() {
                        return Boolean.FALSE;
                    }

                    @Override
                    public void onCallbackError(String error) {

                    }
                });
            }
        }

        @Override
        public void onLocalStream(MediaStream stream) {
            stream.videoTracks.get(0).addRenderer(new VideoRenderer(localRender));
            VideoRendererGui.update(localRender, 0, 0, 25, 25, VideoRendererGui.ScalingType.SCALE_ASPECT_FILL, false);
        }

        @Override
        public void onRemoteStream(MediaStream stream) {
            stream.videoTracks.get(0).setEnabled(true);
            if(stream.videoTracks.get(0).enabled())
                Log.d("JANUSCLIENT", "video tracks enabled");
            stream.videoTracks.get(0).addRenderer(new VideoRenderer(remoteRender));
            VideoRendererGui.update(remoteRender, 0, 0, 25, 25, VideoRendererGui.ScalingType.SCALE_ASPECT_FILL, true);
            VideoRendererGui.update(localRender, 72, 72, 25, 25, VideoRendererGui.ScalingType.SCALE_ASPECT_FILL, false);
        }

        @Override
        public void onDataOpen(Object data) {

        }

        @Override
        public void onData(Object data) {

        }

        @Override
        public void onCleanup() {

        }

        @Override
        public JanusSupportedPluginPackages getPlugin() {
            return JanusSupportedPluginPackages.JANUS_ECHO_TEST;
        }

        @Override
        public void onCallbackError(String error) {

        }

        @Override
        public void onDetached() {

        }

    }

    public EchoTest(VideoRenderer.Callbacks localRender, VideoRenderer.Callbacks remoteRender) {
        this.localRender = localRender;
        this.remoteRender = remoteRender;
        janusServer = new JanusServer(new JanusGlobalCallbacks());
    }

    public boolean initializeMediaContext(Context context, boolean audio, boolean video, boolean videoHwAcceleration, EGLContext eglContext){
        return janusServer.initializeMediaContext(context, audio, video, videoHwAcceleration, eglContext);
    }

    public void Start() {
        janusServer.Connect();
    }
    }
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐