|
1 | 1 | package com.dotengine.linsir.lin_webrtc_demo;
|
2 | 2 |
|
3 |
| -import android.support.v7.app.AppCompatActivity; |
| 3 | +import android.content.Intent; |
| 4 | +import android.graphics.Point; |
| 5 | +import android.opengl.GLSurfaceView; |
4 | 6 | import android.os.Bundle;
|
| 7 | +import android.support.v7.app.AppCompatActivity; |
| 8 | +import android.widget.Toast; |
| 9 | + |
| 10 | +import org.json.JSONException; |
| 11 | +import org.webrtc.MediaStream; |
| 12 | +import org.webrtc.VideoRenderer; |
| 13 | +import org.webrtc.VideoRendererGui; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +public class MainActivity extends AppCompatActivity implements WebRtcClient.RtcListener { |
| 18 | + |
| 19 | + |
| 20 | + private final static int VIDEO_CALL_SENT = 666; |
| 21 | + private static final String VIDEO_CODEC_VP9 = "VP9"; |
| 22 | + private static final String AUDIO_CODEC_OPUS = "opus"; |
| 23 | + |
| 24 | + |
| 25 | + private static final int LOCAL_X_CONNECTING = 0; |
| 26 | + private static final int LOCAL_Y_CONNECTING = 0; |
| 27 | + private static final int LOCAL_WIDTH_CONNECTING = 100; |
| 28 | + private static final int LOCAL_HEIGHT_CONNECTING = 100; |
| 29 | + |
| 30 | + private static final int LOCAL_X_CONNECTED = 72; |
| 31 | + private static final int LOCAL_Y_CONNECTED = 72; |
| 32 | + private static final int LOCAL_WIDTH_CONNECTED = 25; |
| 33 | + private static final int LOCAL_HEIGHT_CONNECTED = 25; |
| 34 | + |
| 35 | + private static final int REMOTE_X = 0; |
| 36 | + private static final int REMOTE_Y = 0; |
| 37 | + private static final int REMOTE_WIDTH = 100; |
| 38 | + private static final int REMOTE_HEIGHT = 100; |
| 39 | + private VideoRendererGui.ScalingType scalingType = VideoRendererGui.ScalingType.SCALE_ASPECT_FILL; |
| 40 | + |
| 41 | + private GLSurfaceView vsv; |
| 42 | + private VideoRenderer.Callbacks localRender; |
| 43 | + private VideoRenderer.Callbacks remoteRender; |
| 44 | + private WebRtcClient client; |
| 45 | + private String mSocketAddress; |
| 46 | + private String callerId; |
5 | 47 |
|
6 |
| -public class MainActivity extends AppCompatActivity { |
7 | 48 |
|
8 | 49 | @Override
|
9 | 50 | protected void onCreate(Bundle savedInstanceState) {
|
10 | 51 | super.onCreate(savedInstanceState);
|
11 | 52 | setContentView(R.layout.activity_main);
|
| 53 | + |
| 54 | + |
| 55 | + mSocketAddress = "http://" + getResources().getString(R.string.host); |
| 56 | + mSocketAddress += (":" + getResources().getString(R.string.port) + "/"); |
| 57 | + |
| 58 | + vsv = (GLSurfaceView) findViewById(R.id.glview_call); |
| 59 | + vsv.setPreserveEGLContextOnPause(true); |
| 60 | + vsv.setKeepScreenOn(true); |
| 61 | + VideoRendererGui.setView(vsv, new Runnable() { |
| 62 | + @Override public void run() { |
| 63 | + init(); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + remoteRender = VideoRendererGui.create(REMOTE_X, |
| 68 | + REMOTE_Y, |
| 69 | + REMOTE_WIDTH, |
| 70 | + REMOTE_HEIGHT, |
| 71 | + scalingType, |
| 72 | + false); |
| 73 | + |
| 74 | + localRender = VideoRendererGui.create(LOCAL_X_CONNECTING, |
| 75 | + LOCAL_Y_CONNECTING, |
| 76 | + LOCAL_WIDTH_CONNECTING, |
| 77 | + LOCAL_HEIGHT_CONNECTING, |
| 78 | + scalingType, |
| 79 | + true); |
| 80 | + |
| 81 | + final Intent intent = getIntent(); |
| 82 | + final String action = intent.getAction(); |
| 83 | + |
| 84 | + if (Intent.ACTION_VIEW.equals(action)) { |
| 85 | + final List<String> segments = intent.getData().getPathSegments(); |
| 86 | + callerId = segments.get(0); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + private void init() { |
| 93 | + Point displaySize = new Point(); |
| 94 | + getWindowManager().getDefaultDisplay().getSize(displaySize); |
| 95 | + PeerConnectionParameters params = new PeerConnectionParameters( |
| 96 | + true, false, displaySize.x, displaySize.y, 30, 1, VIDEO_CODEC_VP9, true, 1, AUDIO_CODEC_OPUS, true); |
| 97 | + |
| 98 | + client = new WebRtcClient(this, mSocketAddress, params, VideoRendererGui.getEGLContext()); |
| 99 | + |
| 100 | + |
| 101 | + startCam(); |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + @Override protected void onPause() { |
| 106 | + super.onPause(); |
| 107 | + vsv.onPause(); |
| 108 | + if (client != null) { |
| 109 | + client.onPause(); |
| 110 | + } |
12 | 111 | }
|
| 112 | + |
| 113 | + |
| 114 | + @Override protected void onResume() { |
| 115 | + super.onResume(); |
| 116 | + vsv.onResume(); |
| 117 | + if (client != null) { |
| 118 | + client.onResume(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + @Override protected void onDestroy() { |
| 124 | + super.onDestroy(); |
| 125 | + if (client != null) { |
| 126 | + client.onResume(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Override public void onCallReady(String callId) { |
| 131 | + if (client != null) { |
| 132 | + try { |
| 133 | + answer(callId); |
| 134 | + } catch (JSONException e) { |
| 135 | + e.printStackTrace(); |
| 136 | + } |
| 137 | + |
| 138 | + }else { |
| 139 | + call(callId); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + public void answer(String callerId) throws JSONException { |
| 145 | + client.sendMessage(callerId,"init",null); |
| 146 | + startCam(); |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | + public void call(String callId){ |
| 152 | + Intent msg = new Intent(Intent.ACTION_SEND); |
| 153 | + msg.putExtra(Intent.EXTRA_TEXT,mSocketAddress+callerId); |
| 154 | + msg.setType("text/plain"); |
| 155 | + startActivityForResult(Intent.createChooser(msg,"Call someone :"),VIDEO_CALL_SENT); |
| 156 | + |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + @Override public void onStatusChanged(final String newStatus) { |
| 161 | + runOnUiThread(new Runnable() { |
| 162 | + @Override public void run() { |
| 163 | + Toast.makeText(getApplicationContext(), newStatus, Toast.LENGTH_SHORT).show(); |
| 164 | + } |
| 165 | + }); |
| 166 | + } |
| 167 | + |
| 168 | + @Override public void onLocalStream(MediaStream localStream) { |
| 169 | + localStream.videoTracks.get(0).addRenderer(new VideoRenderer(localRender)); |
| 170 | + VideoRendererGui.update(localRender, |
| 171 | + LOCAL_X_CONNECTED, |
| 172 | + LOCAL_Y_CONNECTING, |
| 173 | + LOCAL_WIDTH_CONNECTING, |
| 174 | + LOCAL_HEIGHT_CONNECTING, |
| 175 | + scalingType); |
| 176 | + } |
| 177 | + |
| 178 | + @Override public void onAddRemoteStream(MediaStream romoteStream, int endPoint) { |
| 179 | + VideoRendererGui.update(remoteRender, |
| 180 | + REMOTE_X, |
| 181 | + REMOTE_Y, |
| 182 | + REMOTE_WIDTH, |
| 183 | + REMOTE_HEIGHT, |
| 184 | + scalingType |
| 185 | + ); |
| 186 | + |
| 187 | + |
| 188 | + VideoRendererGui.update(localRender, |
| 189 | + LOCAL_X_CONNECTED, |
| 190 | + LOCAL_Y_CONNECTED, |
| 191 | + LOCAL_WIDTH_CONNECTED, |
| 192 | + LOCAL_HEIGHT_CONNECTED, |
| 193 | + scalingType); |
| 194 | + |
| 195 | + |
| 196 | + } |
| 197 | + |
| 198 | + @Override public void onRemoveRemoteStream(int endPoint) { |
| 199 | + VideoRendererGui.update(localRender, |
| 200 | + LOCAL_X_CONNECTING, LOCAL_Y_CONNECTING, |
| 201 | + LOCAL_WIDTH_CONNECTING, LOCAL_HEIGHT_CONNECTING, |
| 202 | + scalingType); |
| 203 | + } |
| 204 | + |
| 205 | + |
| 206 | + public void startCam() { |
| 207 | + |
| 208 | + client.start("android_test"); |
| 209 | + } |
| 210 | + |
| 211 | + |
13 | 212 | }
|
0 commit comments