|
| 1 | +package com.pedrolibrary; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.media.AudioManager; |
| 5 | +import android.support.annotation.NonNull; |
| 6 | +import android.util.Log; |
| 7 | +import android.view.SurfaceHolder; |
| 8 | +import android.view.SurfaceView; |
| 9 | +import android.view.View; |
| 10 | +import android.view.ViewGroup; |
| 11 | +import android.widget.FrameLayout; |
| 12 | + |
| 13 | +import com.facebook.react.uimanager.ThemedReactContext; |
| 14 | +import com.pedro.encoder.input.video.CameraOpenException; |
| 15 | +import com.pedro.rtplibrary.rtmp.RtmpCamera2; |
| 16 | +import net.ossrs.rtmp.ConnectCheckerRtmp; |
| 17 | + |
| 18 | +import static com.facebook.react.common.ReactConstants.TAG; |
| 19 | + |
| 20 | + |
| 21 | +public class RNBroadcastView extends FrameLayout implements |
| 22 | + ConnectCheckerRtmp, SurfaceHolder.Callback { |
| 23 | + private SurfaceView mCameraView; |
| 24 | + private ThemedReactContext mContext = null; |
| 25 | + private RtmpCamera2 rtmpCamera2; |
| 26 | + private Boolean isLive = false; |
| 27 | + private Boolean surfaceExists = false; |
| 28 | + private String streamUrl = ""; |
| 29 | + |
| 30 | + public RNBroadcastView(@NonNull ThemedReactContext context) { |
| 31 | + super(context); |
| 32 | + // context.addLifecycleEventListener(this); |
| 33 | + |
| 34 | + mCameraView = new SurfaceView(context); |
| 35 | + mCameraView.getHolder().addCallback(this); |
| 36 | +// mCameraView.setVisibility(View.VISIBLE); |
| 37 | +// mCameraView.setFullscreen(false, false); |
| 38 | +// mCameraView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); |
| 39 | + |
| 40 | + addView(mCameraView); |
| 41 | + |
| 42 | + rtmpCamera2 = new RtmpCamera2(mCameraView, this); |
| 43 | + rtmpCamera2.setReTries(10); |
| 44 | + |
| 45 | + Log.d(TAG, "About to return from RNBroadcastView"); |
| 46 | + } |
| 47 | + |
| 48 | + private void handleException(Exception e) { |
| 49 | + try { |
| 50 | + rtmpCamera2.stopStream(); |
| 51 | + } catch (Exception e1) { |
| 52 | + // |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public void setPublish(String publishUrl) { |
| 57 | + if (!publishUrl.isEmpty()) { |
| 58 | + AudioManager am = (AudioManager) this.mContext.getSystemService(Context.AUDIO_SERVICE); |
| 59 | + am.setSpeakerphoneOn(true); |
| 60 | + am.setMode(AudioManager.MODE_IN_COMMUNICATION); |
| 61 | + streamUrl = publishUrl; |
| 62 | + this.startStreaming(); |
| 63 | + } |
| 64 | + else if (publishUrl.isEmpty() && this.isLive){ |
| 65 | + AudioManager am = (AudioManager) this.mContext.getSystemService(Context.AUDIO_SERVICE); |
| 66 | + am.setSpeakerphoneOn(true); |
| 67 | + am.setMode(AudioManager.MODE_NORMAL); |
| 68 | + this.stopStreaming(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public void handleStopPublish() { |
| 73 | + AudioManager am = (AudioManager) this.mContext.getSystemService(Context.AUDIO_SERVICE); |
| 74 | + am.setSpeakerphoneOn(true); |
| 75 | + am.setMode(AudioManager.MODE_NORMAL); |
| 76 | + this.stopStreaming(); |
| 77 | + } |
| 78 | + |
| 79 | + public void startStreaming() { |
| 80 | + if (surfaceExists && !streamUrl.isEmpty()) { |
| 81 | + if (rtmpCamera2.isRecording() |
| 82 | + || rtmpCamera2.prepareAudio() && rtmpCamera2.prepareVideo()) { |
| 83 | + rtmpCamera2.startStream(streamUrl); |
| 84 | + isLive = true; |
| 85 | + } else { |
| 86 | +// Toast.makeText(this, "Error preparing stream, This device cant do it", |
| 87 | +// Toast.LENGTH_SHORT).show(); |
| 88 | + isLive = false; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public void stopStreaming() { |
| 94 | + rtmpCamera2.stopStream(); |
| 95 | + isLive = false; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void onConnectionSuccessRtmp() { |
| 100 | +// runOnUiThread(new Runnable() { |
| 101 | +// @Override |
| 102 | +// public void run() { |
| 103 | +// Toast.makeText(MainActivity.this, "Connection success", Toast.LENGTH_SHORT) |
| 104 | +// .show(); |
| 105 | +// } |
| 106 | +// }); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void onConnectionFailedRtmp(final String reason) { |
| 111 | +// runOnUiThread(new Runnable() { |
| 112 | +// @Override |
| 113 | +// public void run() { |
| 114 | +// if (rtmpCamera2.shouldRetry(reason)) { |
| 115 | +// Toast.makeText(MainActivity.this, "Retry", Toast.LENGTH_SHORT) |
| 116 | +// .show(); |
| 117 | +// rtmpCamera2.reTry(5000); //Wait 5s and retry connect stream |
| 118 | +// } else { |
| 119 | +// Toast.makeText(MainActivity.this, "Connection failed. " + reason, Toast.LENGTH_SHORT).show(); |
| 120 | +// //rtmpCamera2.stopStream(); |
| 121 | +// // button.setText(R.string.start_button); |
| 122 | +// stopClick(); |
| 123 | +// } |
| 124 | +// } |
| 125 | +// }); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void onNewBitrateRtmp(long bitrate) { |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void onDisconnectRtmp() { |
| 135 | +// runOnUiThread(new Runnable() { |
| 136 | +// @Override |
| 137 | +// public void run() { |
| 138 | +// Toast.makeText(MainActivity.this, "Disconnected", Toast.LENGTH_SHORT).show(); |
| 139 | +// } |
| 140 | +// }); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void onAuthErrorRtmp() { |
| 145 | +// runOnUiThread(new Runnable() { |
| 146 | +// @Override |
| 147 | +// public void run() { |
| 148 | +// Toast.makeText(MainActivity.this, "Auth error", Toast.LENGTH_SHORT).show(); |
| 149 | +// } |
| 150 | +// }); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void onAuthSuccessRtmp() { |
| 155 | +// runOnUiThread(new Runnable() { |
| 156 | +// @Override |
| 157 | +// public void run() { |
| 158 | +// Toast.makeText(MainActivity.this, "Auth success", Toast.LENGTH_SHORT).show(); |
| 159 | +// } |
| 160 | +// }); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public void surfaceCreated(SurfaceHolder surfaceHolder) { |
| 165 | + Log.d(TAG, "SurfaceCreated"); |
| 166 | + |
| 167 | + // Set the surface exists flag to true and |
| 168 | + // hit the startStreaming function in case we already have a URL |
| 169 | + surfaceExists = true; |
| 170 | + startStreaming(); |
| 171 | + |
| 172 | +// if (rtmpCamera2.isRecording() |
| 173 | +// || rtmpCamera2.prepareAudio() && rtmpCamera2.prepareVideo()) { |
| 174 | +// rtmpCamera2.startStream("rtmp://"+Constants.STREAMING_BASE_URL+":"+Constants.STREAMING_PORT+"/live/" + mDevicePath); |
| 175 | +// isServicePersisted = true; |
| 176 | +// dismissStreamDialog(); |
| 177 | +// } else { |
| 178 | +// Toast.makeText(this, "Error preparing stream, This device cant do it", |
| 179 | +// Toast.LENGTH_SHORT).show(); |
| 180 | +// isServicePersisted = false; |
| 181 | +// showStreamDialog(); |
| 182 | +// } |
| 183 | +// |
| 184 | +// updateServiceIndicator(); |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { |
| 189 | + Log.d(TAG, "SurfaceChanged"); |
| 190 | + rtmpCamera2.startPreview(); |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public void surfaceDestroyed(SurfaceHolder surfaceHolder) { |
| 195 | + surfaceExists = false; |
| 196 | + if (rtmpCamera2.isStreaming()) { |
| 197 | + stopStreaming(); |
| 198 | + } |
| 199 | + rtmpCamera2.stopPreview(); |
| 200 | + } |
| 201 | +} |
0 commit comments