AliVCSDK  4.5.1
阿里云音视频SDK,为视频开发者提供一站式接入服务
AlivcLivePusherBasicImpl.java
1 package com.alivc.live.pusher;
2 
6 import static com.alivc.live.pusher.AlivcLivePushError.ALIVC_FRAMEWORK_RENDER_FIRST_FRAME_PREVIEWED;
7 import static com.alivc.live.pusher.AlivcLivePushError.ALIVC_LIVE_ERROR_SYSTEM_RTMP_OOM;
8 import static com.alivc.live.pusher.AlivcLivePushError.ALIVC_PUSHER_ERROR_BGM_OPEN_FAILED;
9 import static com.alivc.live.pusher.AlivcLivePushError.ALIVC_PUSHER_ERROR_BGM_TIMEOUT;
10 import static com.alivc.live.pusher.AlivcLivePushError.ALIVC_PUSHER_ERROR_SDK_LIVE_PUSH_LOW_PERFORMANCE;
11 import static com.alivc.live.pusher.AlivcLivePushError.ALIVC_PUSHER_ERROR_SDK_LIVE_PUSH_NETWORK_TOO_POOR;
12 import static com.alivc.live.pusher.AlivcLivePushError.ALIVC_PUSHER_ERROR_SDK_PUSH_CONNECT;
13 import static com.alivc.live.pusher.AlivcLivePushError.ALIVC_PUSHER_ERROR_SDK_PUSH_CONNECT_STREAM;
14 import static com.alivc.live.pusher.AlivcLivePushError.ALIVC_PUSHER_ERROR_SDK_PUSH_RECONNECT_FAIL;
15 import static com.alivc.live.pusher.AlivcLivePushError.ALIVC_PUSHER_ERROR_SDK_PUSH_RTS_DOWN_TO_RTMP;
16 import static com.alivc.live.pusher.AlivcLivePushError.ALIVC_PUSHER_ERROR_SDK_PUSH_SEND_DATA_TIMEOUT;
17 import static com.alivc.live.pusher.AlivcLivePushError.ALIVC_PUSHER_ERROR_SDK_PUSH_SETUPURL;
18 import static com.alivc.live.pusher.AlivcLivePushError.ALIVC_PUSHER_ERROR_SDK_PUSH_START_PUSH_TIMEOUT;
19 import static com.alivc.live.pusher.AlivcLivePushError.getErrorByCode;
21 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_BGM_COMPLETED;
22 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_BGM_OPEN_SUCCESS;
23 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_BGM_PAUSE_SUCCESS;
24 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_BGM_PROGRESS;
25 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_BGM_RESUME_SUCCESS;
26 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_BGM_STOP_SUCCESS;
27 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_CAPTURE_CLOSE_CAMERA_SUCCESS;
29 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_CAPTURE_OPEN_CAMERA_SUCCESS;
30 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_CAPTURE_OPEN_MIC_SUCCESS;
32 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_GENERAL_MONITOR;
35 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_NATIVE_LIVE_PUSH_CHANGE_FPS;
39 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_NATIVE_LIVE_PUSH_INIT_SUCCESS;
43 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_NATIVE_LIVE_PUSH_PUSH_PAUSED;
45 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_NATIVE_LIVE_PUSH_PUSH_RESUMED;
46 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_PUSH_CONNECTION_LOST;
47 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_PUSH_CRASH;
48 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_PUSH_DELAY_INFO;
49 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_PUSH_DROP_FRAME;
50 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_PUSH_MEDIAINFO;
51 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_PUSH_NETWORK_RECOVERY;
52 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_PUSH_RECONNECT_START;
53 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_PUSH_RECONNECT_SUCCESS;
54 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_PUSH_SENDED_FIRST_AV;
55 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_PUSH_SENDED_SEI;
56 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_PUSH_STARTED;
57 import static com.alivc.live.pusher.LivePushNotification.ALIVC_PUSHER_EVENT_PUSH_STOPED;
58 import static com.alivc.live.utils.ParameterUtil.getInteger;
59 import static com.alivc.live.utils.ParameterUtil.getLong;
60 
61 import android.app.Application;
62 import android.app.Service;
63 import android.content.BroadcastReceiver;
64 import android.content.Context;
65 import android.content.Intent;
66 import android.content.IntentFilter;
67 import android.graphics.Bitmap;
68 import android.graphics.BitmapFactory;
69 import android.media.AudioManager;
70 import android.os.Handler;
71 import android.os.Looper;
72 import android.os.Message;
73 import android.telephony.PhoneStateListener;
74 import android.telephony.TelephonyManager;
75 import android.text.TextUtils;
76 import android.view.Surface;
77 import android.view.SurfaceHolder;
78 import android.view.SurfaceView;
79 import android.widget.FrameLayout;
80 
84 import com.alivc.component.player.BGMPlayerJNI;
85 import com.alivc.live.annotations.AlivcLiveStreamType;
87 import com.alivc.live.pusher.logreport.PusherAdjustFpsEvent;
89 import com.alivc.live.pusher.logreport.PusherDelayInfoEvent;
90 import com.alivc.live.pusher.logreport.PusherDropFrameEvent;
92 import com.alivc.live.pusher.logreport.PusherHeartBeatEvent;
93 import com.alivc.live.pusher.logreport.PusherLicenseEvent;
94 import com.alivc.live.pusher.logreport.PusherMuteOffEvent;
95 import com.alivc.live.pusher.logreport.PusherMuteOnEvent;
96 import com.alivc.live.pusher.logreport.PusherNetworkPoorEvent;
98 import com.alivc.live.pusher.logreport.PusherOnPauseEvent;
99 import com.alivc.live.pusher.logreport.PusherOnRestartEvent;
100 import com.alivc.live.pusher.logreport.PusherOnResumeEvent;
101 import com.alivc.live.pusher.logreport.PusherPauseEvent;
102 import com.alivc.live.pusher.logreport.PusherReconnectEvent;
106 import com.alivc.live.pusher.logreport.PusherRestartPushEvent;
107 import com.alivc.live.pusher.logreport.PusherResumeEvent;
108 import com.alivc.live.pusher.logreport.PusherSDKCrashEvent;
109 import com.alivc.live.pusher.logreport.PusherSdkErrorEvent;
112 import com.alivc.live.pusher.logreport.PusherSetBGMEvent;
113 import com.alivc.live.pusher.logreport.PusherSetBGMLoopEvent;
114 import com.alivc.live.pusher.logreport.PusherSetFlashEvent;
115 import com.alivc.live.pusher.logreport.PusherStartPreviewEvent;
116 import com.alivc.live.pusher.logreport.PusherStartPushEvent;
119 import com.alivc.live.pusher.logreport.PusherStopPreviewEvent;
120 import com.alivc.live.pusher.logreport.PusherStopPushEvent;
122 import com.alivc.live.pusher.logreport.PusherSwitchCameraEvent;
123 import com.alivc.live.pusher.logreport.PusherSystemErrorEvent;
124 import com.alivc.live.pusher.logreport.core.AliLiveInfoUtils;
125 import com.alivc.live.pusher.logreport.core.LiveEventReporter;
128 import com.alivc.live.utils.AlivcLiveURLTools;
129 import com.alivc.live.utils.AppFrontBackHelper;
130 import com.alivc.live.utils.BluetoothHeadsetUtils;
131 import com.alivc.live.utils.ParameterUtil;
132 import com.alivc.live.utils.SntpClient;
133 
134 import org.json.JSONException;
135 import org.json.JSONObject;
136 import org.webrtc.utils.AlivcLog;
137 
138 import java.lang.ref.WeakReference;
139 import java.util.ArrayList;
140 import java.util.HashMap;
141 import java.util.Iterator;
142 import java.util.Map;
143 import java.util.StringTokenizer;
144 import java.util.concurrent.LinkedBlockingQueue;
145 import java.util.concurrent.ScheduledExecutorService;
146 import java.util.concurrent.ScheduledThreadPoolExecutor;
147 import java.util.concurrent.ThreadFactory;
148 import java.util.concurrent.ThreadPoolExecutor;
149 import java.util.concurrent.TimeUnit;
150 import java.util.concurrent.atomic.AtomicInteger;
151 
152 @CalledByNative
153 class AlivcLivePusherBasicImpl implements ILivePusher {
154  private static final String TAG = "AlivcLivePusherBasicImpl";
155 
156  protected enum ScreenRecordStatus {
157  SCREEN_RECORD_NONE,
158  SCREEN_RECORD_NORMAL,
159  SCREEN_RECORD_CAMERA_START,
160  SCREEN_RECORD_CAMERA_MIX_START,
161  }
162 
163  private static LivePusherJNI mNativeAlivcLivePusher = null;
164  private WeakReference<AlivcLivePusher> mWeakAlivcLivePusher = null;
165 
166  private AlivcLivePushStats mPushStatus = AlivcLivePushStats.IDLE;
167  private AlivcLivePlayStats mPlayStats = AlivcLivePlayStats.IDLE;
168  private AlivcLivePushError mLastError = AlivcLivePushError.ALIVC_COMMON_RETURN_SUCCESS;
169 
170  private AlivcLivePushInfoListener mPushInfoListener = null;
171  private AlivcLivePushErrorListener mPushErrorListener = null;
172  private AlivcLivePushNetworkListener mPushNetworkListener = null;
173  private AlivcLivePushBGMListener mPushBGMListener = null;
174  private AlivcLivePusherRenderContextListener mRenderContextListener = null;
175 
176  private SurfaceStatus mSurfaceStatus = SurfaceStatus.UNINITED;
177 
178  private SurfaceView mPreviewView = null;
179 
180  private AudioManager mAudioManager = null;
181 
182  private Context mContext = null;
183  private AlivcLivePushConfig mAlivcLivePushConfig = null;
185  private static final int NTP_TIME_OUT_MILLISECOND = 1000;
186  private static final String AUTH_KEY = "auth_key=";
187  private static final int MAX_CHATS = 4000;
188  private static final float TEXTURE_RANGE_MIN = 0.0f;
189  private static final float TEXTURE_RANGE_MAX = 1.0f;
190  private static final int MAX_DYNAMIC_ADDSON_COUNT = 3;
191  private static final int AlivcLiveMaxWatermarkCount = 3; // 最多输入3个水印
192 
193  private Map<Integer, AlivcLivePushError> mErrorMap = new HashMap<>();
194  private String mPushUrl = null;
195 
196  private int mBGMVolume = 50;
197  private int mCaptureVolume = 50;
198  private boolean mMute = false;
199  private TelephonyManager manager = null;
200  private AlivcLivePushLogLevel mLogLevel = AlivcLivePushLogLevelWarn;
201 
202  private static final int MESSAGE_RECONNECT_SUCCESS = 0x12;
203  private boolean registeredCallback = false;
204  protected BluetoothHeadsetUtils mBluetoothHelper;
205  private boolean isReconnect = false;
206  private LiveEventReporter mLiveEventReporter = null;
207  private long mTimeStamp = -1;
208  private int mExpiryTime = -1;
209 
210  private int mDynamicAddsonCount = 0;
211 
212  private int mTimeCount = 0;
213 
214  private int mWatermarkCount = 0;
215 
216  private AlivcSnapshotListener mSnapshotListener = null;
217 
218  private ScreenRecordStatus mScreenStatus = ScreenRecordStatus.SCREEN_RECORD_NONE;
219 
220  private AppFrontBackHelper mAppFrontBackHelper;
221 
222  private ThreadPoolExecutor mThreadPoolExecutor = new ThreadPoolExecutor(3, 5, 1, TimeUnit.SECONDS,
223  new LinkedBlockingQueue<Runnable>(100));
224 
225  private Handler mHandler = new Handler(Looper.getMainLooper()) {
226  @Override
227  public void handleMessage(Message msg) {
228  AlivcLog.i(TAG, "AlivcLivePusher reconnect success");
229  isReconnect = false;
230  if (mPushNetworkListener != null) {
231  mThreadPoolExecutor.execute(new Runnable() {
232  @Override
233  public void run() {
234  mPushNetworkListener.onReconnectSucceed(getLivePusherReference());
235  }
236  });
237  }
238  }
239  };
240 
241  private SurfaceHolder.Callback mPreviewCallback = new SurfaceHolder.Callback() {
242  @Override
243  public void surfaceCreated(SurfaceHolder holder) {
244  AlivcLog.d(TAG, "LiveActivity-->Preview surface created");
245  if (mPreviewView == null) {
246  return;
247  }
248  //记录Surface的状态
249  if (mSurfaceStatus == SurfaceStatus.UNINITED) {
250  mSurfaceStatus = SurfaceStatus.CREATED;
251  } else if (mSurfaceStatus == SurfaceStatus.DESTROYED) {
252  mSurfaceStatus = SurfaceStatus.RECREATED;
253  if (mNativeAlivcLivePusher != null) {
254  mNativeAlivcLivePusher.notifySurfaceReCreate(mPreviewView.getHolder().getSurface());
255  }
256  }
257  }
258 
259  @Override
260  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
261  AlivcLog.d(TAG, "LiveActivity-->Preview surface changed");
262  if (mPreviewView == null) {
263  return;
264  }
265  mSurfaceStatus = SurfaceStatus.CHANGED;
266  if (mNativeAlivcLivePusher != null) {
267  mNativeAlivcLivePusher.notifySurfaceChange(holder.getSurface(), mAlivcLivePushConfig.getPreviewOrientation());
268  }
269  }
270 
271  @Override
272  public void surfaceDestroyed(SurfaceHolder holder) {
273  AlivcLog.d(TAG, "LiveActivity-->Preview surface destroyed");
274  if (mPreviewView == null) {
275  return;
276  }
277  if (mNativeAlivcLivePusher != null) {
278  mNativeAlivcLivePusher.notifySurfaceDestroy();
279  }
280  mSurfaceStatus = SurfaceStatus.DESTROYED;
281  }
282  };
283  //定时器
284  private ScheduledExecutorService scheduleExecutor = null;
285 
286  public AlivcLivePusherBasicImpl() {
287 
288  }
289 
290  @Override
291  public void init(final Context context, final AlivcLivePushConfig config) throws IllegalArgumentException, IllegalStateException {
292  AlivcLog.i(TAG, "init");
293  AlivcLog.i(TAG, AlivcLivePushInstance.getSdkBuildInfo());
294  mContext = context;
295  mAlivcLivePushConfig = config;
296  mLiveEventReporter = new LiveEventReporter(mContext, this, mAlivcLivePushConfig);
297 
298  if (!AlivcLiveLicenseManager.isLicenseRegistered()) {
299  String errorMessage = "License not registered! Get HELP from https://help.aliyun.com/document_detail/431730.htm";
300  AlivcLog.e(TAG, errorMessage);
301  throw new IllegalStateException(errorMessage);
302  }
303 
304  boolean isLicenseVerified = AlivcLiveLicenseManager.verifyLicense();
305 
306  AlivcLiveLicenseManager.AlivcLiveLicenseResult licenseResult = AlivcLiveLicenseManager.getLatestLicenseResult();
307  if (licenseResult != null) {
308  PusherLicenseEvent.Args args = new PusherLicenseEvent.Args();
309  args.result = licenseResult.result;
310  args.reason = licenseResult.msg;
311  args.key = licenseResult.licenseKey;
312  mLiveEventReporter.sendEvent(PusherLicenseEvent.kTopicType, PusherLicenseEvent.kTopicValue, PusherLicenseEvent.getArgsStr(args));
313  }
314 
315  if (!isLicenseVerified) {
316  String errorMessage = "License not verified! Get HELP from https://help.aliyun.com/document_detail/431730.htm";
317  AlivcLog.e(TAG, errorMessage);
318  throw new IllegalStateException(errorMessage);
319  }
320 
321  if (mPushStatus != AlivcLivePushStats.IDLE && mPushStatus != AlivcLivePushStats.INIT) {
322  throw new IllegalStateException("init state error, current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
323  }
324  mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
325  mAudioManager.setMode(AudioManager.STREAM_VOICE_CALL);
326  mAudioManager.setSpeakerphoneOn(true);
327 
328  registerHeadsetPlugReceiver();
329 
330  LogWhenGoBackOrFront((Application) context.getApplicationContext(), true);
331 
332  AliLiveInfoUtils.setSDKContext(mContext);
333 
334  mBluetoothHelper = new BluetoothHeadsetUtils(mContext);
335  mBluetoothHelper.start(new BluetoothHeadsetUtils.BlueToothListener() {
336  @Override
337  public void onBlueTooth(boolean on) {
338  LivePusherJNI.headSetOn = on;
339  if (!on) {
340  mAudioManager.setMode(AudioManager.STREAM_VOICE_CALL);
341  mAudioManager.setSpeakerphoneOn(true);
342  } else {
343  mAudioManager.setSpeakerphoneOn(false);
344  }
345  if (mNativeAlivcLivePusher != null) {
346  mNativeAlivcLivePusher.setHeadSet(on);
347  }
348  }
349  });
350 
351  checkConfig(config);
352 
353  res = mAlivcLivePushConfig.getResolution();
354 
355  for (AlivcLivePushError error : AlivcLivePushError.values()) {
356  mErrorMap.put(error.getCode(), error);
357  }
358  mLiveEventReporter = new LiveEventReporter(mContext, this, mAlivcLivePushConfig);
359  mNativeAlivcLivePusher = new LivePusherJNI(mContext, config, new LivePusherJNI.LivePusherListener() {
360  @Override
361  public void onNotify(final int what, String msg, final int param1, final int param2, int param3, int param4, int param5, long arg6, String arg7, String arg8) {
362  // 网络监听器
363  if (what == ALIVC_PUSHER_ERROR_SDK_LIVE_PUSH_NETWORK_TOO_POOR.getCode()) {
364  AlivcLog.i(TAG, "AlivcLivePusher Callback Network Too Poor");
365  if (mPushNetworkListener != null) {
366  mThreadPoolExecutor.execute(new Runnable() {
367  @Override
368  public void run() {
369  mPushNetworkListener.onNetworkPoor(getLivePusherReference());
370  }
371  });
372  }
373 
374  PusherNetworkPoorEvent.Args args = null;
375  mLiveEventReporter.sendEvent(PusherNetworkPoorEvent.kTopicType, PusherNetworkPoorEvent.kTopicValue, PusherNetworkPoorEvent.getArgsStr(args));
376  } else if (what == ALIVC_PUSHER_EVENT_PUSH_NETWORK_RECOVERY.getCode()) {
377  AlivcLog.i(TAG, "AlivcLivePusher Callback Network Recovery");
378  if (mPushNetworkListener != null) {
379  mThreadPoolExecutor.execute(new Runnable() {
380  @Override
381  public void run() {
382  mPushNetworkListener.onNetworkRecovery(getLivePusherReference());
383  }
384  });
385  }
386 
387  PusherNetworkRecoveryEvent.Args args = null;
388  mLiveEventReporter.sendEvent(PusherNetworkRecoveryEvent.kTopicType, PusherNetworkRecoveryEvent.kTopicValue, PusherNetworkRecoveryEvent.getArgsStr(args));
389  } else if (what == ALIVC_PUSHER_EVENT_PUSH_RECONNECT_START.getCode()) {
390  AlivcLog.i(TAG, "AlivcLivePusher Callback Reconnect Start");
391  if (!isReconnect) {
392  isReconnect = true;
393  if (mPushNetworkListener != null) {
394  mThreadPoolExecutor.execute(new Runnable() {
395  @Override
396  public void run() {
397  mPushNetworkListener.onReconnectStart(getLivePusherReference());
398  }
399  });
400  }
401  }
402 
403  PusherReconnectStartEvent.Args args = new PusherReconnectStartEvent.Args();
404  mLiveEventReporter.sendEvent(PusherReconnectStartEvent.kTopicType, PusherReconnectStartEvent.kTopicValue, PusherReconnectStartEvent.getArgsStr(args));
405  } else if (what == ALIVC_PUSHER_EVENT_PUSH_RECONNECT_SUCCESS.getCode()) {
406  AlivcLog.i(TAG, "AlivcLivePusher Callback Reconnect Success");
407  if (mHandler != null) {
408  mHandler.removeMessages(MESSAGE_RECONNECT_SUCCESS);
409  mHandler.sendEmptyMessageDelayed(MESSAGE_RECONNECT_SUCCESS, 500);
410  }
411  //todo ! keep paused status
412  if (mPushStatus != AlivcLivePushStats.PAUSED) {
413  mPushStatus = AlivcLivePushStats.PUSHED;
414  }
415 
416  PusherReconnectSuccessEvent.Args args = null;
417  mLiveEventReporter.sendEvent(PusherReconnectSuccessEvent.kTopicType, PusherReconnectSuccessEvent.kTopicValue, PusherReconnectSuccessEvent.getArgsStr(args));
418  } else if (what == ALIVC_PUSHER_EVENT_PUSH_CONNECTION_LOST.getCode()) {
419  AlivcLog.i(TAG, "AlivcLivePusher Callback Connection Lost");
420 
421  if (mPushNetworkListener != null) {
422  mThreadPoolExecutor.execute(new Runnable() {
423  @Override
424  public void run() {
425  mPushNetworkListener.onConnectionLost(getLivePusherReference());
426  }
427  });
428  }
429 
430  PusherConnectionLostEvent.Args args = new PusherConnectionLostEvent.Args();
431  if (param1 == 0) {
432  args.reason = "rtmp_";
433  } else if (param1 == 1) {
434  args.reason = "artc_";
435  }
436  args.reason += String.valueOf(param2);
437  mLiveEventReporter.sendEvent(PusherConnectionLostEvent.kTopicType, PusherConnectionLostEvent.kTopicValue, PusherConnectionLostEvent.getArgsStr(args));
438  } else if (what == ALIVC_PUSHER_ERROR_SDK_PUSH_RECONNECT_FAIL.getCode()) {
439  AlivcLog.i(TAG, "AlivcLivePusher Callback Reconnect Fail");
440 
441  isReconnect = false;
442  if (mHandler != null) {
443  mHandler.removeMessages(MESSAGE_RECONNECT_SUCCESS);
444  }
445  if (mPushNetworkListener != null) {
446  mThreadPoolExecutor.execute(new Runnable() {
447  @Override
448  public void run() {
449  mPushNetworkListener.onReconnectFail(getLivePusherReference());
450  }
451  });
452  }
453  mPushStatus = AlivcLivePushStats.ERROR;
454  mLastError = ALIVC_PUSHER_ERROR_SDK_PUSH_RECONNECT_FAIL;
455 
456  PusherReconnectFailedEvent.Args args = new PusherReconnectFailedEvent.Args();
457  args.errCode = what;
458  args.errMsg = msg;
459  mLiveEventReporter.sendEvent(PusherReconnectFailedEvent.kTopicType, PusherReconnectFailedEvent.kTopicValue, PusherReconnectFailedEvent.getArgsStr(args));
460  } else if (what == ALIVC_PUSHER_ERROR_SDK_PUSH_SEND_DATA_TIMEOUT.getCode()) {
461  AlivcLog.i(TAG, "AlivcLivePusher Callback Send Data Timeout");
462  if (mPushNetworkListener != null) {
463  mThreadPoolExecutor.execute(new Runnable() {
464  @Override
465  public void run() {
466  mPushNetworkListener.onSendDataTimeout(getLivePusherReference());
467  }
468  });
469  }
470 
471  PusherSendDataTimeoutEvent.Args args = null;
472  mLiveEventReporter.sendEvent(PusherSendDataTimeoutEvent.kTopicType, PusherSendDataTimeoutEvent.kTopicValue, PusherSendDataTimeoutEvent.getArgsStr(args));
473  } else if (what == ALIVC_PUSHER_ERROR_SDK_PUSH_CONNECT.getCode()
474  || what == ALIVC_LIVE_ERROR_SYSTEM_RTMP_OOM.getCode()
475  || what == ALIVC_PUSHER_ERROR_SDK_PUSH_SETUPURL.getCode()
476  || what == ALIVC_PUSHER_ERROR_SDK_PUSH_CONNECT_STREAM.getCode()
477  || what == ALIVC_PUSHER_ERROR_SDK_PUSH_START_PUSH_TIMEOUT.getCode()) {
478  AlivcLog.i(TAG, "AlivcLivePusher Callback Push Connect Fail");
479 
480  if (mPushNetworkListener != null) {
481  mThreadPoolExecutor.execute(new Runnable() {
482  @Override
483  public void run() {
484  mPushNetworkListener.onConnectFail(getLivePusherReference());
485  }
486  });
487  }
488  mPushStatus = AlivcLivePushStats.ERROR;
489  mLastError = getErrorByCode(what);
490 
491  PusherStartPushFailedEvent.Args args = new PusherStartPushFailedEvent.Args();
492  args.errCode = what;
493  args.errMsg = msg;
494  mLiveEventReporter.sendEvent(PusherStartPushFailedEvent.kTopicType, PusherStartPushFailedEvent.kTopicValue, PusherStartPushFailedEvent.getArgsStr(args));
495  } else if (what == ALIVC_PUSHER_EVENT_NATIVE_LIVE_PUSH_INIT_SUCCESS.getCode()) {
496 
497  } else if (what == ALIVC_PUSHER_EVENT_NATIVE_LIVE_PUSH_PREVIEW_STARTED.getCode()) {
498  AlivcLog.i(TAG, "AlivcLivePusher Callback Preview Started");
499 
500  if (mPushInfoListener != null) {
501  mThreadPoolExecutor.execute(new Runnable() {
502  @Override
503  public void run() {
504  mPushInfoListener.onPreviewStarted(getLivePusherReference());
505  }
506  });
507  }
508  mPushStatus = AlivcLivePushStats.PREVIEWED;
509  addWaterMark();
510  } else if (what == ALIVC_PUSHER_EVENT_NATIVE_LIVE_PUSH_PREVIEW_STOPED.getCode()) {
511  AlivcLog.i(TAG, "AlivcLivePusher Callback Preview Stoped");
512 
513  if (mPushInfoListener != null) {
514  mThreadPoolExecutor.execute(new Runnable() {
515  @Override
516  public void run() {
517  if (mPushInfoListener != null) {
518  mPushInfoListener.onPreviewStopped(getLivePusherReference());
519  }
520  }
521  });
522  }
523  mPushStatus = AlivcLivePushStats.INIT;
524  } else if (what == ALIVC_PUSHER_EVENT_PUSH_STARTED.getCode()) {
525  AlivcLog.i(TAG, "AlivcLivePusher Callback Push Started");
526 
527  mPushStatus = AlivcLivePushStats.PUSHED;
528  if (mPushInfoListener != null) {
529  mThreadPoolExecutor.execute(new Runnable() {
530  @Override
531  public void run() {
532  mPushInfoListener.onPushStarted(getLivePusherReference());
533  }
534  });
535  }
536 
537  PusherStartPushSuccessEvent.Args args = new PusherStartPushSuccessEvent.Args();
538  args.url = mPushUrl;
539  mLiveEventReporter.sendEvent(PusherStartPushSuccessEvent.kTopicType, PusherStartPushSuccessEvent.kTopicValue, PusherStartPushSuccessEvent.getArgsStr(args));
540  } else if (what == ALIVC_PUSHER_EVENT_PUSH_STOPED.getCode()) {
541  AlivcLog.i(TAG, "AlivcLivePusher Callback Push Stoped");
542 
543  if (mPushInfoListener != null) {
544  mThreadPoolExecutor.execute(new Runnable() {
545  @Override
546  public void run() {
547  if (mPushInfoListener != null) {
548  mPushInfoListener.onPushStopped(getLivePusherReference());
549  }
550  }
551  });
552  }
553  PusherStopPushSuccessEvent.Args args = new PusherStopPushSuccessEvent.Args();
554  args.url = mPushUrl;
555  mLiveEventReporter.sendEvent(PusherStopPushSuccessEvent.kTopicType, PusherStopPushSuccessEvent.kTopicValue, PusherStopPushSuccessEvent.getArgsStr(args));
556  } else if(what == ALIVC_PUSHER_EVENT_PUSH_CRASH.getCode()){
557  AlivcLog.i(TAG, "found SDK crashed!");
558  mLiveEventReporter.sendEvent(PusherSDKCrashEvent.kTopicType, PusherSDKCrashEvent.kTopicValue, PusherSDKCrashEvent.getArgsStr(arg7, arg8));
559  } else if(what == ALIVC_PUSHER_EVENT_GENERAL_MONITOR.getCode()) {
560  AlivcLivePushConstants.Topic topicType = AlivcLivePushConstants.Topic.event;
561  //kAction=0, kEvent=1, kError=2, kBiz=3
562  int topicTypeId = (int)arg6;
563  switch (topicTypeId) {
564  case 0: topicType = AlivcLivePushConstants.Topic.action; break;
565  case 1: topicType = AlivcLivePushConstants.Topic.event; break;
566  case 2: topicType = AlivcLivePushConstants.Topic.error; break;
567  case 3: topicType = AlivcLivePushConstants.Topic.biz; break;
568  default: assert(false);
569  }
570 
571  assert(!arg7.isEmpty());
572  String topicValue = arg7;
573  String argJsonStr = arg8;
574  Map<String, String> param = new HashMap<String, String>();
575  try {
576  JSONObject obj = new JSONObject(argJsonStr);
577  Iterator it = obj.keys();
578  while (it.hasNext()) {
579  String key = it.next().toString();
580  String value = (String)obj.get(key).toString();
581  param.put(key, value);
582  }
583  } catch(JSONException e) {
584  AlivcLog.e(TAG, "Invalid json object: " + arg8);
585  } catch(Exception e) {
586  AlivcLog.e(TAG, "Unknown exception: " + e.getMessage());
587  }
588  mLiveEventReporter.sendEvent(topicType, topicValue, param);
589  } else if(what == ALIVC_PUSHER_EVENT_PUSH_MEDIAINFO.getCode()) {
590 
591  } else if(what == ALIVC_PUSHER_EVENT_PUSH_DELAY_INFO.getCode()) {
592 
593  PusherDelayInfoEvent.Args args = new PusherDelayInfoEvent.Args();
594  args.totalDelay = param1;
595  args.captureDelay = param2;
596  args.encoderDelay = param3;
597  args.sendDelay = param4;
598 
599  AlivcLog.d(TAG, "AlivcLivePusher push delay");
600 
601  mLiveEventReporter.sendEvent(PusherDelayInfoEvent.kTopicType, PusherDelayInfoEvent.kTopicValue, PusherDelayInfoEvent.getArgsStr(args));
602 
603 
604  JSONObject gDelayObject = new JSONObject();
605 
606  try {
607  gDelayObject.put("gdelay", param1);
608 
609  sendMessageInternal(gDelayObject.toString(),10,0,false, true);
610 
611  } catch (Exception e) {
612  e.printStackTrace();
613  }
614 
615 
616  } else if (what == ALIVC_PUSHER_EVENT_NATIVE_LIVE_PUSH_PUSH_PAUSED.getCode()) {
617  AlivcLog.i(TAG, "AlivcLivePusher Callback Push Paused");
618 
619  if (mPushInfoListener != null) {
620  mThreadPoolExecutor.execute(new Runnable() {
621  @Override
622  public void run() {
623  mPushInfoListener.onPushPaused(getLivePusherReference());
624  }
625  });
626  }
627 
628  PusherOnPauseEvent.Args args = null;
629  mLiveEventReporter.sendEvent(PusherOnPauseEvent.kTopicType, PusherOnPauseEvent.kTopicValue, PusherOnPauseEvent.getArgsStr(args));
630  } else if (what == ALIVC_PUSHER_EVENT_NATIVE_LIVE_PUSH_PUSH_RESUMED.getCode()) {
631  AlivcLog.i(TAG, "AlivcLivePusher Callback Push Resumed");
632 
633  if (mNativeAlivcLivePusher.isPushing()) {
634  mPushStatus = AlivcLivePushStats.PUSHED;
635  } else {
636  mPushStatus = AlivcLivePushStats.PREVIEWED;
637  }
638  if (mPushInfoListener != null) {
639  mThreadPoolExecutor.execute(new Runnable() {
640  @Override
641  public void run() {
642  mPushInfoListener.onPushResumed(getLivePusherReference());
643  }
644  });
645  }
646 
647  PusherOnResumeEvent.Args args = null;
648  mLiveEventReporter.sendEvent(PusherOnResumeEvent.kTopicType, PusherOnResumeEvent.kTopicValue, PusherOnResumeEvent.getArgsStr(args));
649  } else if (what == ALIVC_FRAMEWORK_RENDER_FIRST_FRAME_PREVIEWED.getCode()) {
650  AlivcLog.i(TAG, "AlivcLivePusher Callback Preview First Frame");
651 
652  if (mPushInfoListener != null) {
653  mThreadPoolExecutor.execute(new Runnable() {
654  @Override
655  public void run() {
656  mPushInfoListener.onFirstFramePreviewed(getLivePusherReference());
657  }
658  });
659  }
660  } else if (what == ALIVC_PUSHER_EVENT_NATIVE_LIVE_PUSH_PUSH_RESTARTED.getCode()) {
661  AlivcLog.i(TAG, "AlivcLivePusher Callback Push Restarted");
662 
663  mPushStatus = AlivcLivePushStats.PUSHED;
664  if (mPushInfoListener != null) {
665  mThreadPoolExecutor.execute(new Runnable() {
666  @Override
667  public void run() {
668  mPushInfoListener.onPushRestarted(getLivePusherReference());
669  }
670  });
671  }
672 
673  PusherOnRestartEvent.Args args = null;
674  mLiveEventReporter.sendEvent(PusherOnRestartEvent.kTopicType, PusherOnRestartEvent.kTopicValue, PusherOnRestartEvent.getArgsStr(args));
675  } else if (what == ALIVC_PUSHER_EVENT_PUSH_DROP_FRAME.getCode()) {//丢帧
676  AlivcLog.d(TAG, "AlivcLivePusher Callback Drop Frame");
677 
678  PusherDropFrameEvent.PusherDropFrameArgs args = new PusherDropFrameEvent.PusherDropFrameArgs();
679  args.vpiubbd = param1;
680  args.vpiubad = param2;
681  args.apiubbd = param3;
682  args.apiubad = param4;
683  mLiveEventReporter.sendEvent(PusherDropFrameEvent.kTopicType, PusherDropFrameEvent.kTopicValue, PusherDropFrameEvent.getArgsStr(args));
684  if (mPushInfoListener != null) {
685  mThreadPoolExecutor.execute(new Runnable() {
686  @Override
687  public void run() {
688  mPushInfoListener.onDropFrame(getLivePusherReference(), param1, param2);
689 
690  if (mPushNetworkListener != null) {
691  mPushNetworkListener.onPacketsLost(getLivePusherReference());
692  }
693  }
694  });
695  }
696 
697  } else if (what == ALIVC_PUSHER_EVENT_NATIVE_LIVE_PUSH_ADJUST_BITRATE.getCode()) {
698  AlivcLog.d(TAG, "AlivcLivePusher Callback Adjust Bitrate");
699 
700  PusherAdjustBitrateEvent.Args args = new PusherAdjustBitrateEvent.Args();
701  args.oeb = param1 / 1000;
702  args.neb = param2 / 1000;
703  Map map = getPerformanceMap();
704  if (map != null) {
705  args.ceb = getInteger(map, "mVideoEncodeBitrate") + getInteger(map, "mAudioEncodeBitrate");
706  args.cub = getInteger(map, "mAudioUploadBitrate") + getInteger(map, "mVideoUploadBitrate");
707  }
708  mLiveEventReporter.sendEventWithLevel(PusherAdjustBitrateEvent.kTopicType, PusherAdjustBitrateEvent.kTopicValue, PusherAdjustBitrateEvent.getArgsStr(args), AlivcLivePushMonitorLevel.CUT);
709  if (mPushInfoListener != null) {
710  mThreadPoolExecutor.execute(new Runnable() {
711  @Override
712  public void run() {
713  mPushInfoListener.onAdjustBitrate(getLivePusherReference(), param1 / 1000, param2 / 1000);
714  }
715  });
716  }
717  } else if (what == ALIVC_PUSHER_EVENT_NATIVE_LIVE_PUSH_CHANGE_FPS.getCode()) {
718  AlivcLog.d(TAG, "AlivcLivePusher Callback Change FPS");
719 
720  PusherAdjustFpsEvent.Args args = new PusherAdjustFpsEvent.Args();
721  args.oldFps = param1;
722  args.newFps = param2;
723  mLiveEventReporter.sendEvent(PusherAdjustFpsEvent.kTopicType, PusherAdjustFpsEvent.kTopicValue, PusherAdjustFpsEvent.getArgsStr(args));
724  if (mPushInfoListener != null) {
725  mThreadPoolExecutor.execute(new Runnable() {
726  @Override
727  public void run() {
728  mPushInfoListener.onAdjustFps(getLivePusherReference(), param1, param2);
729  }
730  });
731  }
732  } else if (what == ALIVC_PUSHER_EVENT_NATIVE_LIVE_PUSH_CHANGE_RESOLUTION.getCode()) {
733 
734  } else if (what == ALIVC_PUSHER_ERROR_SDK_PUSH_RTS_DOWN_TO_RTMP.getCode()) {
735  //RTS降级到RTMP逻辑
736  //rtsDownToRtmp();
737  } else if (what == ALIVC_PUSHER_EVENT_PUSH_SENDED_FIRST_AV.getCode()) {
738  //开始推流
739  PusherFirstFrameSentEvent.Args args = new PusherFirstFrameSentEvent.Args();
740  args.vutMs = param2;
741  args.autMs = param1;
742  args.resolution = mAlivcLivePushConfig.getResolution().toString().substring(11);
743  args.st = mAlivcLivePushConfig.getAudioChannels() == 1 ? "single" : "dual";
744  args.ao = mAlivcLivePushConfig.isAudioOnly();
745  args.vo = mAlivcLivePushConfig.isVideoOnly();
746  args.he = mAlivcLivePushConfig.getVideoEncodeMode().equals(Encode_MODE_HARD);//是否使用硬编
747  args.wc = mAlivcLivePushConfig.getWaterMarkInfos().size();//水印
748  args.pum = mAlivcLivePushConfig.isPushMirror();
749  args.fps = mAlivcLivePushConfig.getFps();
750  args.ivb = mAlivcLivePushConfig.getInitialVideoBitrate();
751  args.mavb = mAlivcLivePushConfig.getTargetVideoBitrate();
752  args.mivb = mAlivcLivePushConfig.getMinVideoBitrate();
753  args.asr = mAlivcLivePushConfig.getAudioSampleRate().getAudioSampleRate();
754  args.po = mAlivcLivePushConfig.getPreviewOrientation();
755  args.ct = mAlivcLivePushConfig.getCameraType();
756  args.flash = mAlivcLivePushConfig.isFlash();
757  args.crmc = mAlivcLivePushConfig.getConnectRetryCount();
758  args.cri = mAlivcLivePushConfig.getConnectRetryInterval();
759  args.prm = mAlivcLivePushConfig.isPreviewMirror();
760  args.gop = mAlivcLivePushConfig.getVideoEncodeGop();
761  args.utm = (mAlivcLivePushConfig.getConnectRetryCount() * mAlivcLivePushConfig.getConnectRetryInterval()) / 1000;
762  mLiveEventReporter.sendEvent(PusherFirstFrameSentEvent.kTopicType, PusherFirstFrameSentEvent.kTopicValue, PusherFirstFrameSentEvent.getArgsStr(args));
763  if (mPushInfoListener != null) {
764  mThreadPoolExecutor.execute(new Runnable() {
765  @Override
766  public void run() {
767  mPushInfoListener.onFirstFramePushed(getLivePusherReference());
768  }
769  });
770  }
771  } else if (what == ALIVC_PUSHER_EVENT_CAPTURE_OPEN_CAMERA_SUCCESS.getCode()) {
772 
773  } else if (what == ALIVC_PUSHER_EVENT_CAPTURE_OPEN_MIC_SUCCESS.getCode()) {
774 
775  } else if (what == ALIVC_PUSHER_EVENT_CAPTURE_CLOSE_CAMERA_SUCCESS.getCode()) {
776 
777  } else if (what == ALIVC_PUSHER_EVENT_NATIVE_LIVE_PUSH_OPEN_VIDEO_ENCODER_SUCCESS.getCode()) {
778 
779  } else if (what == ALIVC_PUSHER_EVENT_NATIVE_LIVE_PUSH_CAPTURE_VIDEO_SAMPLES_OVERFLOW.getCode()) {
780  //render 处理不过来,capture丢帧
781  } else if (what == ALIVC_PUSHER_EVENT_PUSH_SENDED_SEI.getCode()) {
782  if (mPushNetworkListener != null) {
783  mThreadPoolExecutor.execute(new Runnable() {
784  @Override
785  public void run() {
786  mPushNetworkListener.onSendMessage(getLivePusherReference());
787  }
788  });
789  }
790  }
791 
792  //播放器监听
793  else if (what == ALIVC_PUSHER_ERROR_BGM_OPEN_FAILED.getCode()) {
794  mPlayStats = AlivcLivePlayStats.IDLE;
795  if (mPushBGMListener != null) {
796  mPushBGMListener.onOpenFailed();
797  }
798  if (mNativeAlivcLivePusher != null) {
799  mNativeAlivcLivePusher.stopBGM();
800  }
801  } else if (what == ALIVC_PUSHER_ERROR_BGM_TIMEOUT.getCode()) {
802  mPlayStats = AlivcLivePlayStats.IDLE;
803  if (mPushBGMListener != null) {
804  mPushBGMListener.onDownloadTimeout();
805  }
806  } else if (what == ALIVC_PUSHER_EVENT_BGM_OPEN_SUCCESS.getCode()) {
807  mPlayStats = AlivcLivePlayStats.STARTED;
808  if (mPushBGMListener != null) {
809  mPushBGMListener.onStarted();
810  }
811  } else if (what == ALIVC_PUSHER_EVENT_BGM_STOP_SUCCESS.getCode()) {
812  mPlayStats = AlivcLivePlayStats.STOPPED;
813  if (mPushBGMListener != null) {
814  mPushBGMListener.onStopped();
815  }
816  } else if (what == ALIVC_PUSHER_EVENT_BGM_PAUSE_SUCCESS.getCode()) {
817  mPlayStats = AlivcLivePlayStats.PAUSED;
818  if (mPushBGMListener != null) {
819  mPushBGMListener.onPaused();
820  }
821  } else if (what == ALIVC_PUSHER_EVENT_BGM_RESUME_SUCCESS.getCode()) {
822  mPlayStats = AlivcLivePlayStats.STARTED;
823  if (mPushBGMListener != null) {
824  mPushBGMListener.onResumed();
825  }
826  } else if (what == ALIVC_PUSHER_EVENT_BGM_COMPLETED.getCode()) {
827  if (mPushBGMListener != null) {
828  mPushBGMListener.onCompleted();
829  }
830  mPlayStats = AlivcLivePlayStats.IDLE;
831  } else if (what == ALIVC_PUSHER_EVENT_BGM_PROGRESS.getCode()) {
832  if (mPushBGMListener != null) {
833  mPushBGMListener.onProgress((long) param1, (long) param2);
834  }
835  } else if (what == ALIVC_PUSHER_EVENT_CAPTURE_OPEN_SCREENCAPTURE_SUCCESS.getCode()) {
836 
837  } else if (what == ALIVC_PUSHER_EVENT_CAPTURE_CLOSE_SCREENCAPTURE_SUCCESS.getCode()) {
838 
839  } else if (what == ALIVC_PUSHER_EVENT_NATIVE_LIVE_PUSH_GL_CONTEXT_CREATED.getCode()) {
840  if (mRenderContextListener != null) {
841  mRenderContextListener.onSharedContextCreated(arg6);
842  }
843  } else if (what == ALIVC_PUSHER_EVENT_NATIVE_LIVE_PUSH_GL_CONTEXT_DESTROYED.getCode()) {
844  if (mRenderContextListener != null) {
845  mRenderContextListener.onSharedContextDestroyed(arg6);
846  }
847  }
848  // 系统错误监听器
849  else if (what > 0x30010000 && what < 0x30020000) {
850  AlivcLog.e(TAG, "AlivcLivePusher SystemError Callback error: " + what);
851 
852  if (mErrorMap.get(what) != null) {
853  mErrorMap.get(what).setMsg(msg);
854  }
855  if (mPushErrorListener != null) {
856  mThreadPoolExecutor.execute(new Runnable() {
857  @Override
858  public void run() {
859  mPushErrorListener.onSystemError(getLivePusherReference(), mErrorMap.get(what));
860  }
861  });
862  }
863 
864  PusherSystemErrorEvent.Args args = new PusherSystemErrorEvent.Args();
865  Map map = getPerformanceMap();
866  if (map != null) {
867  args.totalUploadSize = getLong(map, "mTotalSizeOfUploadedPackets");
868  args.totalTime = getLong(map, "mTotalTimeOfPublishing");
869  args.error_code = what;
870  if (mErrorMap.get(what) != null) {
871  args.error_msg = msg;
872  }
873  mLiveEventReporter.sendEvent(PusherSystemErrorEvent.kTopicType, PusherSystemErrorEvent.kTopicValue, PusherSystemErrorEvent.getArgsStr(args));
874  }
875  mPushStatus = AlivcLivePushStats.ERROR;
876  mLastError = getErrorByCode(what);
877  }
878  else if (what == ALIVC_PUSHER_ERROR_SDK_LIVE_PUSH_LOW_PERFORMANCE.getCode()) {
879  // When callback is low performance, do not set the status into a forbidden state.
880  AlivcLog.i(TAG, "AlivcLivePusher low performance warning: 0x" + Integer.toHexString(what));
881  if (mErrorMap.get(what) != null) {
882  mErrorMap.get(what).setMsg(msg);
883  }
884  PusherSdkErrorEvent.Args args = new PusherSdkErrorEvent.Args();
885  Map map = getPerformanceMap();
886  if (map != null) {
887  args.totalUploadSize = ParameterUtil.getLong(map, "mTotalSizeOfUploadedPackets");
888  args.totalTime = ParameterUtil.getLong(map, "mTotalTimeOfPublishing");
889  args.error_code = what;
890  if (mErrorMap.get(what) != null) {
891  args.error_msg = msg;
892  }
893  mLiveEventReporter.sendEvent(PusherSdkErrorEvent.kTopicType, PusherSdkErrorEvent.kTopicValue, PusherSdkErrorEvent.getArgsStr(args));
894  }
895  }
896  // 其他
897  else {
898  AlivcLog.e(TAG, "AlivcLivePusher SDKError Callback error: 0x" + Integer.toHexString(what));
899 
900  if (mErrorMap.get(what) != null) {
901  mErrorMap.get(what).setMsg(msg);
902  }
903  if (mPushErrorListener != null) {
904  mThreadPoolExecutor.execute(new Runnable() {
905  @Override
906  public void run() {
907  mPushErrorListener.onSDKError(getLivePusherReference(), mErrorMap.get(what));
908  }
909  });
910  }
911 
912  PusherSdkErrorEvent.Args args = new PusherSdkErrorEvent.Args();
913  Map map = getPerformanceMap();
914  if (map != null) {
915  args.totalUploadSize = getLong(map, "mTotalSizeOfUploadedPackets");
916  args.totalTime = getLong(map, "mTotalTimeOfPublishing");
917  args.error_code = what;
918  if (mErrorMap.get(what) != null) {
919  args.error_msg = msg;
920  }
921  mLiveEventReporter.sendEvent(PusherSdkErrorEvent.kTopicType, PusherSdkErrorEvent.kTopicValue, PusherSdkErrorEvent.getArgsStr(args));
922  }
923  mPushStatus = AlivcLivePushStats.ERROR;
924  mLastError = getErrorByCode(what);
925  AlivcLog.e(TAG, "error is " + what + " msg is " + what);
926  }
927  }
928  });
929  mNativeAlivcLivePusher.init();
930  BGMPlayerJNI.setContext(mContext);
931  // AlivcLiveBase.setLogLevel(mLogLevel); // 如果用户之前设置,此处会覆盖用户设置, 暂时默认先不设置
932  mPushStatus = AlivcLivePushStats.INIT;
933  }
934 
935  @Override
936  public void setCustomDetect(AlivcLivePushCustomDetect customVideoDetect) {
937  AlivcLog.i(TAG, "setCustomDetect");
938  if (mNativeAlivcLivePusher == null) {
939  return;
940  }
941  mNativeAlivcLivePusher.setCustomDetect(customVideoDetect);
942  }
943 
944  @Override
945  public void setCustomFilter(AlivcLivePushCustomFilter customVideoFilter) {
946  AlivcLog.i(TAG, "setCustomFilter");
947  if (mNativeAlivcLivePusher == null) {
948  return;
949  }
950  mNativeAlivcLivePusher.setCustomFilter(customVideoFilter);
951  }
952 
953  @Override
954  public void setCustomAudioFilter(AlivcLivePushCustomAudioFilter customAudioFilter) {
955  AlivcLog.i(TAG, "setCustomAudioFilter");
956  if (mNativeAlivcLivePusher == null) {
957  return;
958  }
959  mNativeAlivcLivePusher.setCustomAudioFilter(customAudioFilter);
960  }
961 
962  @Override
963  public void destroy() throws IllegalStateException {
964  AlivcLog.i(TAG, "destroy");
965  LogWhenGoBackOrFront((Application) mContext.getApplicationContext(), false);
966 
967  if (mNativeAlivcLivePusher != null) {
968  mNativeAlivcLivePusher.release();
969  }
970 
971  mDynamicAddsonCount = 0;
972 
973  if (registeredCallback) {
974  try {
975  mContext.unregisterReceiver(mHeadsetPlugReceiver);
976  mContext.unregisterReceiver(mTelephoneReceiver);
977  } catch (Exception e) {
978  AlivcLog.e(TAG, "unregisterReceiver exception");
979  }
980  registeredCallback = false;
981  }
982  if (mBluetoothHelper != null) {
983  mBluetoothHelper.stop();
984  }
985  mPushStatus = AlivcLivePushStats.IDLE;
986 
987  stop5Interval();
988 
989  if (mThreadPoolExecutor != null && !mThreadPoolExecutor.isShutdown()) {
990  mThreadPoolExecutor.shutdown();
991  }
992  mThreadPoolExecutor = null;
993 
994  mNativeAlivcLivePusher = null;
995  mPushInfoListener = null;
996  mPushErrorListener = null;
997  mPushNetworkListener = null;
998  mPushBGMListener = null;
999 
1000  mPreviewView = null;
1001  mContext = null;
1002  mAlivcLivePushConfig = null;
1003 
1004  mErrorMap = null;
1005  mPreviewCallback = null;
1006  if (mHandler != null) {
1007  mHandler.removeMessages(MESSAGE_RECONNECT_SUCCESS);
1008  }
1009  mHandler = null;
1010 
1011  if (mWeakAlivcLivePusher != null) {
1012  mWeakAlivcLivePusher.clear();
1013  mWeakAlivcLivePusher = null;
1014  }
1015  BGMPlayerJNI.setContext(null);
1016  }
1017 
1018  @Override
1019  public void setLivePusherReference(AlivcLivePusher livePusher) {
1020  mWeakAlivcLivePusher = new WeakReference<>(livePusher);
1021  }
1022 
1023  private AlivcLivePusher getLivePusherReference() {
1024  return (mWeakAlivcLivePusher != null) ? mWeakAlivcLivePusher.get() : null;
1025  }
1026 
1027  @Override
1028  public void startPreview(SurfaceView surfaceView) throws IllegalArgumentException, IllegalStateException {
1029  AlivcLog.i(TAG, "startPreview");
1030  PusherStartPreviewEvent.Args args = new PusherStartPreviewEvent.Args();
1031  args.sync = true;
1032  if (mLiveEventReporter != null) {
1033  mLiveEventReporter.sendEvent(PusherStartPreviewEvent.kTopicType, PusherStartPreviewEvent.kTopicValue, PusherStartPreviewEvent.getArgsStr(args));
1034  }
1035 
1036  if (mNativeAlivcLivePusher == null) {
1037  AlivcLog.e(TAG, "Illegal State, you should init first");
1038  return;
1039  }
1040  if (mPushStatus != AlivcLivePushStats.INIT && mPushStatus != AlivcLivePushStats.PREVIEWED) {
1041  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1042  return;
1043  }
1044 
1045  this.mPreviewView = surfaceView;
1046  int result = 0;
1047  if (surfaceView == null) {
1048  result = mNativeAlivcLivePusher.startPreview(null, true);
1049  if (result == 0) {
1050  mPushStatus = AlivcLivePushStats.PREVIEWED;
1051  } else {
1052  AlivcLog.e(TAG, "start preview error");
1053  return;
1054  }
1055  } else {
1056  result = mNativeAlivcLivePusher.startPreview(surfaceView.getHolder().getSurface(), true);
1057  if (result == 0) {
1058  surfaceView.getHolder().addCallback(mPreviewCallback);
1059  mPushStatus = AlivcLivePushStats.PREVIEWED;
1060  } else {
1061  AlivcLog.e(TAG, "start preview error");
1062  }
1063  }
1064  }
1065 
1066  @Override
1067  public void startPreviewAsync(SurfaceView surfaceView) throws IllegalArgumentException, IllegalStateException {
1068  AlivcLog.i(TAG, "startPreviewAsync");
1069 
1070  PusherStartPreviewEvent.Args args = new PusherStartPreviewEvent.Args();
1071  args.sync = false;
1072  if (mLiveEventReporter != null) {
1073  mLiveEventReporter.sendEvent(PusherStartPreviewEvent.kTopicType, PusherStartPreviewEvent.kTopicValue, PusherStartPreviewEvent.getArgsStr(args));
1074  }
1075 
1076  if (mNativeAlivcLivePusher == null) {
1077  AlivcLog.e(TAG, "Illegal State, you should init first");
1078  return;
1079  }
1080 
1081  if (mPushStatus != AlivcLivePushStats.INIT && mPushStatus != AlivcLivePushStats.PREVIEWED) {
1082  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1083  return;
1084  }
1085 
1086  mPushStatus = AlivcLivePushStats.PREVIEWING;
1087  this.mPreviewView = surfaceView;
1088  int result = 0;
1089  if (surfaceView == null) {
1090  result = mNativeAlivcLivePusher.startPreview(null, false);
1091  if (result == 0) {
1092  mPushStatus = AlivcLivePushStats.PREVIEWED;
1093  } else {
1094  AlivcLog.e(TAG, "start preview async error");
1095  }
1096  } else {
1097  result = mNativeAlivcLivePusher.startPreview(surfaceView.getHolder().getSurface(), false);
1098  if (result == 0) {
1099  surfaceView.getHolder().addCallback(mPreviewCallback);
1100  mPushStatus = AlivcLivePushStats.PREVIEWED;
1101  } else {
1102  AlivcLog.e(TAG, "start preview async error");
1103  }
1104  }
1105  }
1106 
1107  @Override
1108  public void startPreview(Context context, FrameLayout frameLayout, boolean isAnchor) throws IllegalArgumentException, IllegalStateException {
1109 
1110  }
1111 
1112  @Override
1113  public void stopPreview() throws IllegalStateException {
1114  AlivcLog.i(TAG, "stopPreview");
1115  PusherStopPreviewEvent.Args args = null;
1116  if (mLiveEventReporter != null) {
1117  mLiveEventReporter.sendEvent(PusherStopPreviewEvent.kTopicType, PusherStopPreviewEvent.kTopicValue, PusherStopPreviewEvent.getArgsStr(args));
1118  }
1119 
1120  if (mNativeAlivcLivePusher == null) {
1121  AlivcLog.e(TAG, "Illegal State, you should init first");
1122  return;
1123  }
1124 
1125  mDynamicAddsonCount = 0;
1126 
1127  if (mPushStatus != AlivcLivePushStats.INIT && mPushStatus != AlivcLivePushStats.PREVIEWED) {
1128  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1129  return;
1130  }
1131 
1132  int result = mNativeAlivcLivePusher.stopPreview();
1133  if (result == 0) {
1134  mPushStatus = AlivcLivePushStats.INIT;
1135  } else {
1136  AlivcLog.e(TAG, "stop preview error");
1137  }
1138  }
1139 
1140  @Override
1141  public void startPush(String url) throws IllegalArgumentException, IllegalStateException {
1142  AlivcLog.i(TAG, "startPush");
1143  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
1144  AlivcLog.e(TAG, "Illegal State, you should init first");
1145  return;
1146  }
1147 
1148  url = AlivcLiveURLTools.trimUrl(url);
1149  if (!AlivcLiveURLTools.checkPushUrl(url)) {
1150  AlivcLog.e(TAG, "Illegal argument, push url error!");
1151  return;
1152  }
1153 
1154  if ((mAlivcLivePushConfig.isAudioOnly() || mAlivcLivePushConfig.isVideoOnly()) && url.startsWith(AlivcLiveStreamType.EnumType.AlivcLiveStreamRts.getPrefix())) {
1155  AlivcLog.e(TAG, "RTC protocol don't support audio/video only stream");
1156  return;
1157  }
1158 
1159  if ((mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.INIT)
1160  && mPushStatus != AlivcLivePushStats.PUSHED) {
1161  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1162  return;
1163  }
1164 
1165  if (mPushStatus.equals(AlivcLivePushStats.INIT)) {
1166  mPreviewView = null;
1167  int result = mNativeAlivcLivePusher.startPreview(null, true);
1168  if (result != 0) {
1169  AlivcLog.e(TAG, "start push error : create gl resource failed");
1170  return;
1171  }
1172  }
1173  mPushUrl = url;
1174  if (mPushUrl.contains(AUTH_KEY)) {
1175  getNetworkTime();
1176  }
1177 
1178  mLiveEventReporter.refreshPushId();
1179  int result = mNativeAlivcLivePusher.startPush(url, true);
1180  if (result == 0) {
1181  if (mPushStatus != AlivcLivePushStats.PUSHED) {
1182  mPushStatus = AlivcLivePushStats.PUSHED;
1183  if (mAlivcLivePushConfig.getMediaProjectionPermissionResultData() != null) {
1184  mScreenStatus = ScreenRecordStatus.SCREEN_RECORD_NORMAL;
1185  }
1186  }
1187  } else {
1188  AlivcLog.e(TAG, "start push error");
1189  return;
1190  }
1191 
1192  reportStartPushEvent(false, true);
1193 
1194  String pauseImage = mAlivcLivePushConfig.getPausePushImage() == null ? "" : mAlivcLivePushConfig.getPausePushImage();
1195  String networkImage = mAlivcLivePushConfig.getNetworkPoorPushImage() == null ? "" : mAlivcLivePushConfig.getNetworkPoorPushImage();
1196  mNativeAlivcLivePusher.addPushImage(pauseImage, networkImage);
1197  startScheduleExecutor();
1198  }
1199 
1200  @Override
1201  public void startPushAsync(String url) throws IllegalArgumentException, IllegalStateException {
1202  AlivcLog.i(TAG, "startPushAsync");
1203  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
1204  AlivcLog.e(TAG, "Illegal State, you should init first");
1205  return;
1206  }
1207 
1208  url = AlivcLiveURLTools.trimUrl(url);
1209  if (!AlivcLiveURLTools.checkPushUrl(url)) {
1210  AlivcLog.e(TAG, "Illegal argument, push url error!");
1211  return;
1212  }
1213 
1214  if ((mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.INIT)
1215  && mPushStatus != AlivcLivePushStats.PUSHED) {
1216  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1217  return;
1218  }
1219 
1220  if ((mAlivcLivePushConfig.isAudioOnly() || mAlivcLivePushConfig.isVideoOnly()) && url.startsWith(AlivcLiveStreamType.EnumType.AlivcLiveStreamRts.getPrefix())) {
1221  AlivcLog.e(TAG, "RTC protocol don't support audio/video only stream");
1222  return;
1223  }
1224 
1225  if (mPushStatus.equals(AlivcLivePushStats.INIT)) {
1226  mPreviewView = null;
1227  int result = mNativeAlivcLivePusher.startPreview(null, true);
1228  if (result != 0) {
1229  AlivcLog.e(TAG, "start push error : create gl resource failed");
1230  return;
1231  }
1232  }
1233 
1234  mPushUrl = url;
1235  if (mPushUrl.contains(AUTH_KEY)) {
1236  getNetworkTime();
1237  }
1238 
1239  mLiveEventReporter.refreshPushId();
1240  int result = mNativeAlivcLivePusher.startPush(url, false);
1241  if (result == 0) {
1242  if (mPushStatus != AlivcLivePushStats.PUSHED) {
1243  mPushStatus = AlivcLivePushStats.PUSHED;
1244  if (mAlivcLivePushConfig.getMediaProjectionPermissionResultData() != null) {
1245  mScreenStatus = ScreenRecordStatus.SCREEN_RECORD_NORMAL;
1246  }
1247  }
1248  } else {
1249  AlivcLog.e(TAG, "start push async error");
1250  return;
1251  }
1252 
1253  reportStartPushEvent(false, false);
1254 
1255  String pauseImage = mAlivcLivePushConfig.getPausePushImage() == null ? "" : mAlivcLivePushConfig.getPausePushImage();
1256  String networkImage = mAlivcLivePushConfig.getNetworkPoorPushImage() == null ? "" : mAlivcLivePushConfig.getNetworkPoorPushImage();
1257  mNativeAlivcLivePusher.addPushImage(pauseImage, networkImage);
1258  startScheduleExecutor();
1259  }
1260 
1261  @Override
1262  public void restartPush() throws IllegalStateException {
1263  AlivcLog.i(TAG, "restartPush");
1264  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
1265  AlivcLog.e(TAG, "Illegal State, you should init first");
1266  return;
1267  }
1268 
1269  if (mPreviewView == null && mAlivcLivePushConfig.getMediaProjectionPermissionResultData() == null
1270  && !mAlivcLivePushConfig.isAudioOnly()) {
1271  AlivcLog.e(TAG, "illegal argument");
1272  return;
1273  }
1274 
1275  if ((mAlivcLivePushConfig.isAudioOnly() || mAlivcLivePushConfig.isVideoOnly())
1276  && (!TextUtils.isEmpty(mPushUrl) && mPushUrl.startsWith(AlivcLiveStreamType.EnumType.AlivcLiveStreamRts.getPrefix()))) {
1277  AlivcLog.e(TAG, "RTC protocol don't support audio/video only stream");
1278  return;
1279  }
1280 
1281  if (mPushStatus != AlivcLivePushStats.PUSHED && mPushStatus != AlivcLivePushStats.ERROR) {
1282  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1283  return;
1284  }
1285 
1286  mDynamicAddsonCount = 0;
1287 
1288  stop5Interval();
1289 
1290  Surface surface = mPreviewView != null ? mPreviewView.getHolder().getSurface() : null;
1291  int result = mNativeAlivcLivePusher.restartPush(surface, true, 1000);
1292  if (result == 0) {
1293  mPushStatus = AlivcLivePushStats.PUSHED;
1294  if (mAlivcLivePushConfig.getMediaProjectionPermissionResultData() != null) {
1295  mScreenStatus = ScreenRecordStatus.SCREEN_RECORD_NORMAL;
1296  }
1297  } else {
1298  AlivcLog.e(TAG, "restart push error");
1299  return;
1300  }
1301  String pauseImage = mAlivcLivePushConfig.getPausePushImage() == null ? "" : mAlivcLivePushConfig.getPausePushImage();
1302  String networkImage = mAlivcLivePushConfig.getNetworkPoorPushImage() == null ? "" : mAlivcLivePushConfig.getNetworkPoorPushImage();
1303  mNativeAlivcLivePusher.addPushImage(pauseImage, networkImage);
1304 
1305  reportStartPushEvent(true, true);
1306 
1307  startScheduleExecutor();
1308 
1309  }
1310 
1311  @Override
1312  public void restartPushAsync() throws IllegalStateException {
1313  AlivcLog.i(TAG, "restartPushAsync");
1314  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
1315  AlivcLog.e(TAG, "Illegal State, you should init first");
1316  return;
1317  }
1318 
1319  if (mPreviewView == null && mAlivcLivePushConfig.getMediaProjectionPermissionResultData() == null
1320  && !mAlivcLivePushConfig.isAudioOnly()) {
1321  AlivcLog.e(TAG, "illegal argument");
1322  return;
1323  }
1324 
1325  if ((mAlivcLivePushConfig.isAudioOnly() || mAlivcLivePushConfig.isVideoOnly())
1326  && (!TextUtils.isEmpty(mPushUrl) && mPushUrl.startsWith(AlivcLiveStreamType.EnumType.AlivcLiveStreamRts.getPrefix()))) {
1327  AlivcLog.e(TAG, "RTC protocol don't support audio/video only stream");
1328  return;
1329  }
1330 
1331  if (mPushStatus != AlivcLivePushStats.PUSHED && mPushStatus != AlivcLivePushStats.ERROR) {
1332  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1333  return;
1334  }
1335 
1336  Surface surface = mPreviewView != null ? mPreviewView.getHolder().getSurface() : null;
1337  int result = mNativeAlivcLivePusher.restartPush(surface, false, 1000);
1338  if (result == 0) {
1339  mPushStatus = AlivcLivePushStats.RESTARTING;
1340  } else {
1341  AlivcLog.e(TAG, "restart push async error");
1342  return;
1343  }
1344  String pauseImage = mAlivcLivePushConfig.getPausePushImage() == null ? "" : mAlivcLivePushConfig.getPausePushImage();
1345  String networkImage = mAlivcLivePushConfig.getNetworkPoorPushImage() == null ? "" : mAlivcLivePushConfig.getNetworkPoorPushImage();
1346  mNativeAlivcLivePusher.addPushImage(pauseImage, networkImage);
1347 
1348  stop5Interval();
1349 
1350  reportStartPushEvent(true, false);
1351 
1352  startScheduleExecutor();
1353  }
1354 
1355  @Override
1356  public void reconnectPushAsync(String url) throws IllegalStateException {
1357  AlivcLog.i(TAG, "reconnectPushAsync");
1358  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
1359  AlivcLog.e(TAG, "Illegal State, you should init first");
1360  return;
1361  }
1362 
1363  url = AlivcLiveURLTools.trimUrl(url);
1364  if (!AlivcLiveURLTools.checkPushUrl(url)) {
1365  return;
1366  }
1367 
1368  if (mPushStatus != AlivcLivePushStats.PUSHED && mPushStatus != AlivcLivePushStats.PAUSED &&
1369  mPushStatus != AlivcLivePushStats.ERROR) {
1370  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1371  return;
1372  }
1373 
1374  if (isNetworkPushing()) {
1375  return;
1376  }
1377  mPushUrl = url;
1378  getNetworkTime();
1379 
1380  int result = mNativeAlivcLivePusher.reconnect(url, false);
1381  if (result == 0) {
1382  isReconnect = false;
1383  if (mHandler != null) {
1384  mHandler.removeMessages(MESSAGE_RECONNECT_SUCCESS);
1385  }
1386  mPushStatus = AlivcLivePushStats.PUSHED;
1387  if (mAlivcLivePushConfig.getMediaProjectionPermissionResultData() != null) {
1388  mScreenStatus = ScreenRecordStatus.SCREEN_RECORD_NORMAL;
1389  }
1390  } else {
1391  AlivcLog.e(TAG, "reconnect push async error");
1392  return;
1393  }
1394 
1395  //重新连接推流
1396  PusherReconnectEvent.Args args = new PusherReconnectEvent.Args();
1397  Map map = getPerformanceMap();
1398  if (map != null) {
1399  args.vutMs = getLong(map, "mVideoDurationFromeCaptureToUpload");
1400  args.autMs = getLong(map, "mAudioDurationFromeCaptureToUpload");
1401  if (mLiveEventReporter != null) {
1402  mLiveEventReporter.sendEvent(PusherReconnectEvent.kTopicType, PusherReconnectEvent.kTopicValue, PusherReconnectEvent.getArgsStr(args));
1403  }
1404  }
1405  }
1406 
1407  @Override
1408  public void stopPush() throws IllegalStateException {
1409  AlivcLog.i(TAG, "stopPush");
1410  PusherStopPushEvent.Args args = new PusherStopPushEvent.Args();
1411  Map map = getPerformanceMap();
1412  if (map != null) {
1413  args.totalUploadSize = ParameterUtil.getLong(map, "mTotalSizeOfUploadedPackets");
1414  args.totalTime = ParameterUtil.getLong(map, "mTotalTimeOfPublishing");
1415  if (mLiveEventReporter != null) {
1416  mLiveEventReporter.sendEvent(PusherStopPushEvent.kTopicType, PusherStopPushEvent.kTopicValue, PusherStopPushEvent.getArgsStr(args));
1417  }
1418  }
1419 
1420  if (mNativeAlivcLivePusher == null) {
1421  AlivcLog.e(TAG, "Illegal State, you should init first");
1422  return;
1423  }
1424  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED
1425  && mPushStatus != AlivcLivePushStats.PAUSED && mPushStatus != AlivcLivePushStats.ERROR) {
1426  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1427  return;
1428  }
1429 
1430  int result = mNativeAlivcLivePusher.stopPush();
1431  if (result == 0) {
1432  mPushStatus = AlivcLivePushStats.PREVIEWED;
1433  } else {
1434  AlivcLog.e(TAG, "stop push error");
1435  return;
1436  }
1437  if (mPreviewView == null) {
1438  mNativeAlivcLivePusher.stopPreview();
1439  mPushStatus = AlivcLivePushStats.INIT;
1440  }
1441  stop5Interval();
1442  }
1443 
1444  @Override
1445  public void pause() throws IllegalStateException {
1446  AlivcLog.i(TAG, "pause");
1447  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
1448  AlivcLog.e(TAG, "Illegal State, you should init first");
1449  return;
1450  }
1451  if (mAlivcLivePushConfig.isExternMainStream()) {
1452  return;
1453  }
1454  if (mPushStatus != AlivcLivePushStats.PUSHED && mPushStatus != AlivcLivePushStats.PAUSED
1455  && mPushStatus != AlivcLivePushStats.PREVIEWED) {
1456  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1457  return;
1458  }
1459  mPushStatus = AlivcLivePushStats.PAUSED;
1460 
1461  mNativeAlivcLivePusher.pause();
1462 
1463  //reclock pause time
1464  PusherPauseEvent.mLastPauseTime = System.currentTimeMillis();
1465  PusherPauseEvent.Args args = new PusherPauseEvent.Args();
1466  Map map = getPerformanceMap();
1467  if (map != null) {
1468  args.isPauseScreen = false;
1469  args.totalUploadSize = ParameterUtil.getLong(map, "mTotalSizeOfUploadedPackets");
1470  args.totalTime = ParameterUtil.getLong(map, "mTotalTimeOfPublishing");
1471  if (mLiveEventReporter != null) {
1472  mLiveEventReporter.sendEvent(PusherPauseEvent.kTopicType, PusherPauseEvent.kTopicValue, PusherPauseEvent.getArgsStr(args));
1473  }
1474  }
1475  }
1476 
1477  @Override
1478  public void resume() throws IllegalStateException {
1479  AlivcLog.i(TAG, "resume");
1480  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
1481  AlivcLog.e(TAG, "Illegal State, you should init first");
1482  return;
1483  }
1484  if (mAlivcLivePushConfig.isExternMainStream()) {
1485  return;
1486  }
1487  if (mPushStatus != AlivcLivePushStats.PAUSED && mPushStatus != AlivcLivePushStats.ERROR) {
1488  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1489  return;
1490  }
1491 
1492  mNativeAlivcLivePusher.resume(true);
1493  PusherResumeEvent.Args args = new PusherResumeEvent.Args();
1494  Map map = getPerformanceMap();
1495  if (map != null) {
1496  args.sync = true;
1497  args.isResumeScreen = false;
1498  args.totalUploadSize = ParameterUtil.getLong(map, "mTotalSizeOfUploadedPackets");
1499  args.totalTime = ParameterUtil.getLong(map, "mTotalTimeOfPublishing");
1500  if (PusherPauseEvent.mLastPauseTime == 0) {
1501  args.cost = 0;
1502  } else {
1503  args.cost = System.currentTimeMillis() - PusherPauseEvent.mLastPauseTime;
1504  }
1505  if (mLiveEventReporter != null) {
1506  mLiveEventReporter.sendEvent(PusherResumeEvent.kTopicType, PusherResumeEvent.kTopicValue, PusherResumeEvent.getArgsStr(args));
1507  }
1508  }
1509  //reset to 0
1510  PusherPauseEvent.mLastPauseTime = 0;
1511 
1512  if (mNativeAlivcLivePusher.isPushing()) {
1513  mPushStatus = AlivcLivePushStats.PUSHED;
1514  } else {
1515  mPushStatus = AlivcLivePushStats.PREVIEWED;
1516  }
1517  }
1518 
1519  @Override
1520  public void resumeAsync() throws IllegalStateException {
1521  AlivcLog.i(TAG, "resumeAsync");
1522  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
1523  AlivcLog.e(TAG, "Illegal State, you should init first");
1524  return;
1525  }
1526  if (mAlivcLivePushConfig.isExternMainStream()) {
1527  return;
1528  }
1529  if (mPushStatus != AlivcLivePushStats.PAUSED && mPushStatus != AlivcLivePushStats.ERROR) {
1530  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1531  return;
1532  }
1533  mPushStatus = AlivcLivePushStats.RESUMING;
1534  mNativeAlivcLivePusher.resume(false);
1535  PusherResumeEvent.Args args = new PusherResumeEvent.Args();
1536  Map map = getPerformanceMap();
1537  if (map != null) {
1538  args.sync = false;
1539  args.isResumeScreen = false;
1540  args.totalUploadSize = ParameterUtil.getLong(map, "mTotalSizeOfUploadedPackets");
1541  args.totalTime = ParameterUtil.getLong(map, "mTotalTimeOfPublishing");
1542  if (PusherPauseEvent.mLastPauseTime == 0) {
1543  args.cost = 0;
1544  } else {
1545  args.cost = System.currentTimeMillis() - PusherPauseEvent.mLastPauseTime;
1546  }
1547  if (mLiveEventReporter != null) {
1548  mLiveEventReporter.sendEvent(PusherResumeEvent.kTopicType, PusherResumeEvent.kTopicValue, PusherResumeEvent.getArgsStr(args));
1549  }
1550  }
1551  //reset to 0
1552  PusherPauseEvent.mLastPauseTime = 0;
1553  }
1554 
1555  @Override
1556  public void pauseScreenCapture() throws IllegalStateException {
1557  if (mNativeAlivcLivePusher == null) {
1558  AlivcLog.e(TAG, "Illegal State, you should init first");
1559  return;
1560  }
1561  if (mPushStatus != AlivcLivePushStats.PUSHED && mPushStatus != AlivcLivePushStats.PAUSED
1562  && mPushStatus != AlivcLivePushStats.PREVIEWED) {
1563  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1564  return;
1565  }
1566  mPushStatus = AlivcLivePushStats.PAUSED;
1567 
1568  //reclock pause time
1569  PusherPauseEvent.mLastPauseTime = System.currentTimeMillis();
1570 
1571  mNativeAlivcLivePusher.pauseScreenCapture();
1572  PusherPauseEvent.Args args = new PusherPauseEvent.Args();
1573  Map map = getPerformanceMap();
1574  if (map != null) {
1575  args.isPauseScreen = true;
1576  args.totalUploadSize = ParameterUtil.getLong(map, "mTotalSizeOfUploadedPackets");
1577  args.totalTime = ParameterUtil.getLong(map, "mTotalTimeOfPublishing");
1578  if (mLiveEventReporter != null) {
1579  mLiveEventReporter.sendEvent(PusherPauseEvent.kTopicType, PusherPauseEvent.kTopicValue, PusherPauseEvent.getArgsStr(args));
1580  }
1581  }
1582  }
1583 
1584  @Override
1585  public void resumeScreenCapture() throws IllegalStateException {
1586  if (mNativeAlivcLivePusher == null) {
1587  AlivcLog.e(TAG, "Illegal State, you should init first");
1588  return;
1589  }
1590  if (mPushStatus != AlivcLivePushStats.PAUSED) {
1591  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1592  return;
1593  }
1594 
1595  mNativeAlivcLivePusher.resumeScreenCapture();
1596  PusherResumeEvent.Args args = new PusherResumeEvent.Args();
1597  Map map = getPerformanceMap();
1598  if (map != null) {
1599  args.sync = true;
1600  args.isResumeScreen = true;
1601  args.totalUploadSize = ParameterUtil.getLong(map, "mTotalSizeOfUploadedPackets");
1602  args.totalTime = ParameterUtil.getLong(map, "mTotalTimeOfPublishing");
1603  args.cost = System.currentTimeMillis() - PusherPauseEvent.mLastPauseTime;
1604  if (mLiveEventReporter != null) {
1605  mLiveEventReporter.sendEvent(PusherResumeEvent.kTopicType, PusherResumeEvent.kTopicValue, PusherResumeEvent.getArgsStr(args));
1606  }
1607  }
1608  //reset to 0
1609  PusherPauseEvent.mLastPauseTime = 0;
1610 
1611  if (mNativeAlivcLivePusher.isPushing()) {
1612  mPushStatus = AlivcLivePushStats.PUSHED;
1613  } else {
1614  mPushStatus = AlivcLivePushStats.PREVIEWED;
1615  }
1616  }
1617 
1618  @Override
1619  public void switchCamera() throws IllegalStateException {
1620  AlivcLog.i(TAG, "switchCamera");
1621  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
1622  AlivcLog.e(TAG, "Illegal State, you should init first");
1623  return;
1624  }
1625  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED) {
1626  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1627  return;
1628  }
1629  mNativeAlivcLivePusher.switchCamera();
1630 
1631  if (mAlivcLivePushConfig.getCameraType() == CAMERA_TYPE_FRONT.getCameraId()) {
1632  mAlivcLivePushConfig.setCameraType(CAMERA_TYPE_BACK);
1633  } else {
1634  mAlivcLivePushConfig.setCameraType(CAMERA_TYPE_FRONT);
1635  }
1636 
1637  //send switch camera
1638  PusherSwitchCameraEvent.Args args = new PusherSwitchCameraEvent.Args();
1639  args.isFrontCamera = mAlivcLivePushConfig.getCameraType() == CAMERA_TYPE_FRONT.getCameraId();
1640  if (mLiveEventReporter != null) {
1641  mLiveEventReporter.sendEvent(PusherSwitchCameraEvent.kTopicType, PusherSwitchCameraEvent.kTopicValue, PusherSwitchCameraEvent.getArgsStr(args));
1642  }
1643  }
1644 
1645  @Override
1646  public void setAutoFocus(boolean autoFocus) throws IllegalStateException {
1647  AlivcLog.i(TAG, "setAutoFocus auto: " + autoFocus);
1648  if (mNativeAlivcLivePusher == null) {
1649  AlivcLog.e(TAG, "Illegal State, you should init first");
1650  return;
1651  }
1652  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED) {
1653  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1654  return;
1655  }
1656  mNativeAlivcLivePusher.setCameraFocus(autoFocus, 0, 0);
1657  }
1658 
1659  @Override
1660  public void focusCameraAtAdjustedPoint(float x, float y, boolean autoFocus) throws IllegalStateException {
1661  AlivcLog.i(TAG, "focusCameraAtAdjustedPoint x: " + x + " y: " + y + " auto: " + autoFocus);
1662  if (mNativeAlivcLivePusher == null) {
1663  AlivcLog.e(TAG, "Illegal State, you should init first");
1664  return;
1665  }
1666  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED) {
1667  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1668  return;
1669  }
1670  mNativeAlivcLivePusher.setCameraFocus(autoFocus, x, y);
1671  }
1672 
1673  @Override
1674  public void setExposure(int exposure) {
1675  AlivcLog.i(TAG, "setExposure exposure: " + exposure);
1676  if (mNativeAlivcLivePusher == null) {
1677  AlivcLog.e(TAG, "Illegal State, you should init first");
1678  return;
1679  }
1680  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED) {
1681  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1682  return;
1683  }
1684  mNativeAlivcLivePusher.setExposureCompensation(exposure);
1685  }
1686 
1687  @Override
1688  public int getCurrentExposure() {
1689  AlivcLog.i(TAG, "getCurrentExposure");
1690  if (mNativeAlivcLivePusher == null) {
1691  AlivcLog.e(TAG, "Illegal State, you should init first");
1692  return -1;
1693  }
1694  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED) {
1695  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1696  return -1;
1697  }
1698  return mNativeAlivcLivePusher.getCurrentExposureCompensation();
1699  }
1700 
1701  @Override
1702  public int getSupportedMinExposure() {
1703  AlivcLog.i(TAG, "getSupportedMinExposure");
1704  if (mNativeAlivcLivePusher == null) {
1705  AlivcLog.e(TAG, "Illegal State, you should init first");
1706  return -1;
1707  }
1708  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED) {
1709  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1710  return -1;
1711  }
1712  return mNativeAlivcLivePusher.getMinExposureCompensation();
1713  }
1714 
1715  @Override
1716  public int getSupportedMaxExposure() {
1717  AlivcLog.i(TAG, "getSupportedMaxExposure");
1718  if (mNativeAlivcLivePusher == null) {
1719  AlivcLog.e(TAG, "Illegal State, you should init first");
1720  return -1;
1721  }
1722  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED) {
1723  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1724  return -1;
1725  }
1726  return mNativeAlivcLivePusher.getMaxExposureCompensation();
1727  }
1728 
1729  @Override
1730  public int setLiveMixTranscodingConfig(AlivcLiveTranscodingConfig config) {
1731  return -1;
1732  }
1733 
1734  @Override
1735  public int muteLocalCamera(boolean mute) {
1736  // TODO need implement
1737  return -1;
1738  }
1739 
1740  @Override
1741  public int enableSpeakerphone(boolean enable) {
1742  // TODO need implement
1743  return -1;
1744  }
1745 
1746  @Override
1747  public boolean isSpeakerphoneOn() {
1748  // TODO need implement
1749  return false;
1750  }
1751 
1752  @Override
1753  public void setZoom(int zoom) throws IllegalStateException {
1754  AlivcLog.i(TAG, "setZoom zoom: " + zoom);
1755  if (mNativeAlivcLivePusher == null) {
1756  AlivcLog.e(TAG, "Illegal State, you should init first");
1757  return;
1758  }
1759  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED) {
1760  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1761  return;
1762  }
1763  mNativeAlivcLivePusher.setCameraZoom(zoom);
1764  }
1765 
1766  @Override
1767  public int getMaxZoom() throws IllegalStateException {
1768  AlivcLog.i(TAG, "getMaxZoom");
1769  if (mNativeAlivcLivePusher == null) {
1770  AlivcLog.e(TAG, "Illegal State, you should init first");
1771  return -1;
1772  }
1773  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED) {
1774  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1775  return -1;
1776  }
1777  return mNativeAlivcLivePusher.getCameraMaxZoom();
1778  }
1779 
1780  @Override
1781  public int getCurrentZoom() throws IllegalStateException {
1782  AlivcLog.i(TAG, "getCurrentZoom");
1783  if (mNativeAlivcLivePusher == null) {
1784  AlivcLog.e(TAG, "Illegal State, you should init first");
1785  return -1;
1786  }
1787  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED) {
1788  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1789  return -1;
1790  }
1791 
1792  return mNativeAlivcLivePusher.getCameraCurrentZoom();
1793  }
1794 
1795  @Override
1796  public void setMute(boolean mute) throws IllegalStateException {
1797  AlivcLog.i(TAG, "setMute mute: " + mute);
1798  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
1799  AlivcLog.e(TAG, "Illegal State, you should init first");
1800  return;
1801  }
1802  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED && mPushStatus != AlivcLivePushStats.PAUSED) {
1803  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1804  return;
1805  }
1806  mNativeAlivcLivePusher.setMute(mute);
1807 
1808  mMute = mute;
1809  //send mute event
1810  if (mLiveEventReporter != null) {
1811  if (mute) {
1812  PusherMuteOnEvent.Args args = new PusherMuteOnEvent.Args();
1813  mLiveEventReporter.sendEvent(PusherMuteOffEvent.kTopicType, PusherMuteOffEvent.kTopicValue, PusherMuteOnEvent.getArgsStr(args));
1814  } else {
1815  PusherMuteOffEvent.Args args = new PusherMuteOffEvent.Args();
1816  mLiveEventReporter.sendEvent(PusherMuteOffEvent.kTopicType, PusherMuteOffEvent.kTopicValue, PusherMuteOffEvent.getArgsStr(args));
1817  }
1818  }
1819  }
1820 
1821  @Override
1822  public void setFlash(boolean flash) throws IllegalStateException {
1823  AlivcLog.i(TAG, "setFlash flash: " + flash);
1824  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
1825  AlivcLog.e(TAG, "Illegal State, you should init first");
1826  return;
1827  }
1828  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED) {
1829  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1830  return;
1831  }
1832  mNativeAlivcLivePusher.setFlash(flash);
1833  mAlivcLivePushConfig.setFlash(flash);
1834  //send flash event
1835  PusherSetFlashEvent.Args args = new PusherSetFlashEvent.Args();
1836  args.isOpen = flash;
1837  if (mLiveEventReporter != null) {
1838  mLiveEventReporter.sendEvent(PusherSetFlashEvent.kTopicType, PusherSetFlashEvent.kTopicValue, PusherSetFlashEvent.getArgsStr(args));
1839  }
1840  }
1841 
1842  @Override
1843  public void setPushMirror(boolean mirror) throws IllegalStateException {
1844  AlivcLog.i(TAG, "setPushMirror pushMirror: " + mirror);
1845  if (mNativeAlivcLivePusher == null) {
1846  AlivcLog.e(TAG, "Illegal State, you should init first");
1847  return;
1848  }
1849  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED) {
1850  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1851  return;
1852  }
1853  mNativeAlivcLivePusher.setPusherMirror(mirror);
1854  }
1855 
1856  @Override
1857  public void setPreviewMirror(boolean mirror) throws IllegalStateException {
1858  AlivcLog.i(TAG, "setPreviewMirror previewMirror: " + mirror);
1859  if (mNativeAlivcLivePusher == null) {
1860  AlivcLog.e(TAG, "Illegal State, you should init first");
1861  return;
1862  }
1863  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED) {
1864  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1865  return;
1866  }
1867  mNativeAlivcLivePusher.setPreviewMirror(mirror);
1868  }
1869 
1870  @Override
1871  public void setTargetVideoBitrate(int targetVideoBitrate) throws IllegalArgumentException, IllegalStateException {
1872  AlivcLog.i(TAG, "setTargetVideoBitrate targetVideoBitrate: " + targetVideoBitrate);
1873  if (mNativeAlivcLivePusher == null) {
1874  AlivcLog.e(TAG, "Illegal State, you should init first");
1875  return;
1876  }
1877  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED) {
1878  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1879  return;
1880  }
1881  mNativeAlivcLivePusher.setVideoBitrateRange(0, targetVideoBitrate, targetVideoBitrate);
1882  }
1883 
1884  @Override
1885  public void setMinVideoBitrate(int minVideoBitrate) throws IllegalArgumentException, IllegalStateException {
1886  AlivcLog.i(TAG, "setMinVideoBitrate minVideoBitrate: " + minVideoBitrate);
1887  if (mNativeAlivcLivePusher == null) {
1888  AlivcLog.e(TAG, "Illegal State, you should init first");
1889  return;
1890  }
1891  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED) {
1892  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1893  return;
1894  }
1895  mNativeAlivcLivePusher.setVideoBitrateRange(minVideoBitrate, 0, 0);
1896  }
1897 
1898  @Override
1899  public boolean isCameraSupportAutoFocus() {
1900  AlivcLog.i(TAG, "isCameraSupportAutoFocus");
1901  if (mNativeAlivcLivePusher == null) {
1902  AlivcLog.e(TAG, "Illegal State, you should init first");
1903  return false;
1904  }
1905  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED) {
1906  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1907  return false;
1908  }
1909  return mNativeAlivcLivePusher.isCameraSupportAutoFocus();
1910  }
1911 
1912  @Override
1913  public boolean isCameraSupportFlash() {
1914  AlivcLog.i(TAG, "isCameraSupportFlash");
1915 
1916  if (mNativeAlivcLivePusher == null) {
1917  AlivcLog.e(TAG, "Illegal State, you should init first");
1918  return false;
1919  }
1920  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED) {
1921  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
1922  return false;
1923  }
1924  return mNativeAlivcLivePusher.isCameraSupportFlash();
1925  }
1926 
1927  @Override
1928  public boolean isPushing() throws IllegalStateException {
1929  // AlivcLog.d(TAG, "isPushing");
1930 
1931  if (mNativeAlivcLivePusher != null) {
1932  return mNativeAlivcLivePusher.isPushing();
1933  }
1934  return false;
1935  }
1936 
1937  @Override
1938  public AlivcLivePushStats getCurrentStatus() {
1939  return mPushStatus;
1940  }
1941 
1942  @Override
1943  public AlivcLivePushError getLastError() {
1944  return mLastError;
1945  }
1946 
1947  @Override
1948  public void snapshot(int count, int interval, AlivcSnapshotListener listener) {
1949  mSnapshotListener = listener;
1950  if (mNativeAlivcLivePusher != null) {
1951  mNativeAlivcLivePusher.snapshot(count, interval, mInnerSnapshotListener);
1952  }
1953  }
1954 
1955  @Override
1956  public boolean isNetworkPushing() throws IllegalStateException {
1957  // AlivcLog.d(TAG, "isNetworkPushing");
1958 
1959  if (mNativeAlivcLivePusher != null) {
1960  return mNativeAlivcLivePusher.isNetworkPushing();
1961  }
1962  return false;
1963  }
1964 
1965  @Override
1966  public void setLivePushErrorListener(AlivcLivePushErrorListener errorListener) {
1967  this.mPushErrorListener = errorListener;
1968  }
1969 
1970  @Override
1971  public void setLivePushInfoListener(AlivcLivePushInfoListener infoListener) {
1972  this.mPushInfoListener = infoListener;
1973  }
1974 
1975  @Override
1976  public void setLivePushNetworkListener(AlivcLivePushNetworkListener networkListner) {
1977  this.mPushNetworkListener = networkListner;
1978  }
1979 
1980  @Override
1981  public void setLivePushBGMListener(AlivcLivePushBGMListener pushBGMListener) {
1982  this.mPushBGMListener = pushBGMListener;
1983  }
1984 
1985  @Override
1986  public void setLivePushRenderContextListener(AlivcLivePusherRenderContextListener renderCtxListener) {
1987  this.mRenderContextListener = renderCtxListener;
1988  }
1989 
1990  @Override
1991  public AlivcLivePushStatsInfo getLivePushStatsInfo() throws IllegalStateException {
1992  // AlivcLog.d(TAG, "getLivePushStatsInfo");
1993 
1994  if (mNativeAlivcLivePusher == null) {
1995  return null;
1996  }
1997  AlivcLivePushStatsInfo info = new AlivcLivePushStatsInfo();
1998  String mapStr = mNativeAlivcLivePusher.getPerformanceInfo();
1999  if (mapStr == null || "".equals(mapStr)) {
2000  return null;
2001  }
2002  Map map = new HashMap<String, String>();
2003  StringTokenizer items;
2004  for (StringTokenizer entries = new StringTokenizer(mapStr, "|"); entries.hasMoreTokens();
2005  map.put(items.nextToken(), items.hasMoreTokens() ? ((String) (items.nextToken())) : null)) {
2006  items = new StringTokenizer(entries.nextToken(), ":");
2007  }
2008  try {
2009  info.setVideoCaptureFps(getInteger(map, "mVideoCaptureFps"));
2010  info.setAudioEncodeBitrate(getInteger(map, "mAudioEncodeBitrate"));
2011  info.setVideoRenderFps(getInteger(map, "mVideoRenderingFPS"));
2012  info.setAudioEncodeFps(getInteger(map, "mAudioEncodeFps"));
2013  info.setVideoEncodeMode(AlivcEncodeModeEnum.values()[getInteger(map, "mPresetVideoEncoderMode")]);
2014  info.setVideoEncodeBitrate(getInteger(map, "mVideoEncodeBitrate"));
2015  info.setVideoEncodeFps(getInteger(map, "mVideoEncodedFps"));
2016  info.setTotalFramesOfEncodedVideo(getLong(map, "mTotalFramesOfEncodedVideo"));
2017  info.setTotalTimeOfEncodedVideo(getLong(map, "mTotalTimeOfEncodedVideo"));
2018  info.setVideoEncodeParam(getInteger(map, "mPresetVideoEncoderBitrate"));
2019  info.setAudioUploadBitrate(getInteger(map, "mAudioUploadBitrate"));
2020  info.setVideoUploadBitrate(getInteger(map, "mVideoUploadBitrate"));
2021  info.setAudioPacketsInUploadBuffer(getInteger(map, "mAudioPacketsInBuffer"));
2022  info.setVideoPacketsInUploadBuffer(getInteger(map, "mVideoPacketsInBuffer"));
2023  info.setVideoUploadeFps(getInteger(map, "mVideoUploadedFps"));
2024  info.setAudioUploadFps(getInteger(map, "mAudioUploadingPacketsPerSecond"));
2025  info.setCurrentlyUploadedVideoFramePts(getLong(map, "mCurrentlyUploadedVideoFramePts"));
2026  info.setCurrentlyUploadedAudioFramePts(getLong(map, "mCurrentlyUploadedAudioFramePts"));
2027  info.setPreviousVideoKeyFramePts(getLong(map, "mPreviousKeyFramePts"));
2028  info.setLastVideoPtsInBuffer(getLong(map, "mLastVideoFramePTSInQueue"));
2029  info.setLastAudioPtsInBuffer(getLong(map, "mLastAudioFramePTSInQueue"));
2030  info.setTotalSizeOfUploadedPackets(getLong(map, "mTotalSizeOfUploadedPackets"));
2031  info.setTotalTimeOfUploading(getLong(map, "mTotalTimeOfPublishing"));
2032  info.setTotalFramesOfUploadedVideo(getLong(map, "mTotalFramesOfVideoUploaded"));
2033  info.setTotalDurationOfDropingVideoFrames(getLong(map, "mDropDurationOfVideoFrames"));
2034  info.setTotalTimesOfDropingVideoFrames(getLong(map, "mTotalDroppedTimes"));
2035  info.setTotalTimesOfDisconnect(getInteger(map, "mTotalNetworkDisconnectedTimes"));
2036  info.setTotalTimesOfReconnect(getInteger(map, "mTotalNetworkReconnectedTimes"));
2037  info.setVideoDurationFromeCaptureToUpload(getInteger(map, "mVideoDurationFromeCaptureToUpload"));
2038  info.setAudioDurationFromeCaptureToUpload(getInteger(map, "mAudioDurationFromeCaptureToUpload"));
2039  info.setCurrentUploadPacketSize(getInteger(map, "mCurrentUploadingPacketSize"));
2040  info.setMaxSizeOfVideoPacketsInBuffer(getInteger(map, "mMaxVideoPacketSize"));
2041  info.setMaxSizeOfAudioPacketsInBuffer(getInteger(map, "mMaxAudioPacketSize"));
2042  info.setLastVideoFramePTSInQueue(getLong(map, "mLastVideoFramePTSInQueue"));
2043  info.setLastAudioFramePTSInQueue(getLong(map, "mLastAudioFramePTSInQueue"));
2044  info.setAvPTSInterval(getLong(map, "mAvPTSInterval"));
2045  info.setAudioFrameInEncodeBuffer(getInteger(map, "mAudioFramesInEncoderQueue"));
2046  info.setVideoFramesInEncodeBuffer(getInteger(map, "mVideoFramesInEncoderQueue"));
2047  info.setVideoFramesInRenderBuffer(getInteger(map, "mVideoFramesInRenderQueue"));
2048  info.setVideoRenderConsumingTimePerFrame(getInteger(map, "mVideoRenderConsumingTimePerFrame"));
2049  info.setTotalDroppedAudioFrames(getInteger(map, "mTotalDroppedAudioFrames"));
2050  info.setRtt(getInteger(map, "mRtt"));
2051  info.setVideoLostRate(getInteger(map, "mVideoLostRate"));
2052  info.setAudioLostRate(getInteger(map, "mAudioLostRate"));
2053  info.setVideoReSendBitRate(getInteger(map, "mVideoReSendBitRate"));
2054  info.setAudioReSendBitRate(getInteger(map, "mAudioReSendBitRate"));
2055  info.setVideoEncodingWidth(getInteger(map, "mVideoEncodingWidth"));
2056  info.setVideoEncodingHeight(getInteger(map, "mVideoEncodingHeight"));
2057  info.setVideoEncodingGopSize(getInteger(map, "mVideoEncodingGopSize"));
2058  info.setAudioCapturingSampleRate(getInteger(map, "mAudioCapturingSampleRate"));
2059  info.setAudioCaptureVolume(getInteger(map, "mAudioCaptureVolume"));
2060 
2061  info.setCpu((float)AliLiveInfoUtils.getCPUUsageRatio());
2062  info.setMemory(ParameterUtil.getRunningAppProcessInfo(mContext));
2063  if (!TextUtils.isEmpty(mPushUrl)) {
2064  if (mPushUrl.startsWith(AlivcLiveStreamType.EnumType.AlivcLiveStreamRtmp.getPrefix())) {
2065  info.setAlivcLivePushPublishType(AlivcLiveStreamType.EnumType.AlivcLiveStreamRtmp.getValue());
2066  } else if (mPushUrl.startsWith(AlivcLiveStreamType.EnumType.AlivcLiveStreamRts.getPrefix())) {
2067  info.setAlivcLivePushPublishType(AlivcLiveStreamType.EnumType.AlivcLiveStreamRts.getValue());
2068  }
2069  }
2070  } catch (IllegalStateException e) {
2071  e.printStackTrace();
2072  } catch (Exception e) {
2073  e.printStackTrace();
2074  }
2075  return info;
2076  }
2077 
2078  @Override
2079  public String getPushUrl() {
2080  return AlivcLiveURLTools.getUrlWithoutParameters(mPushUrl);
2081  }
2082 
2083  private void checkConfig(AlivcLivePushConfig livePushConfig) throws IllegalStateException {
2084  if (livePushConfig == null) {
2085  throw new IllegalArgumentException("Invalid parameter, config is null.");
2086  }
2087 
2088  if (livePushConfig.isVideoOnly() && livePushConfig.isAudioOnly()) {
2089  throw new IllegalStateException("cannot set video only and audio only simultaneously");
2090  }
2091 
2092  if (livePushConfig.getTargetVideoBitrate() < 100 || livePushConfig.getTargetVideoBitrate() > 5000) {
2093  throw new IllegalStateException("video target bitrate error, Range:[100 5000]");
2094  }
2095  if (livePushConfig.getMinVideoBitrate() < 100 || livePushConfig.getMinVideoBitrate() > 5000) {
2096  throw new IllegalStateException("video min bitrate error, Range:[100 5000]");
2097  }
2098  if (livePushConfig.getInitialVideoBitrate() < livePushConfig.getMinVideoBitrate() || livePushConfig.getInitialVideoBitrate() < 100
2099  || livePushConfig.getInitialVideoBitrate() > 5000) {
2100  throw new IllegalStateException("init bitrate error");
2101  }
2102  if (livePushConfig.getConnectRetryCount() <= 0 || livePushConfig.getConnectRetryCount() > 100) {
2103  throw new IllegalStateException("connect retry count error, Range:[0 100]");
2104  }
2105  if (livePushConfig.getConnectRetryInterval() <= 0 || livePushConfig.getConnectRetryInterval() > 10000) {
2106  throw new IllegalStateException("connect retry interval error, Range:[0 10000]");
2107  }
2108 
2109  if (livePushConfig.getMinFps() <= 0 || livePushConfig.getMinFps() > livePushConfig.getFps()) {
2110  throw new IllegalStateException("fps error");
2111  }
2112 
2113  for (int i = 0; i < livePushConfig.getWaterMarkInfos().size(); i++) {
2114 
2115  if (livePushConfig.getWaterMarkInfos().get(i).mWaterMarkCoordX > 1 || livePushConfig.getWaterMarkInfos().get(i).mWaterMarkWidth > 1
2116  || livePushConfig.getWaterMarkInfos().get(i).mWaterMarkCoordY > 1 || livePushConfig.getWaterMarkInfos().get(i).mWaterMarkHeight > 1) {
2117  throw new IllegalStateException("watermark param error");
2118  }
2119 
2120  if (livePushConfig.getWaterMarkInfos().get(i).mWaterMarkCoordX < 0 || livePushConfig.getWaterMarkInfos().get(i).mWaterMarkWidth <= 0
2121  || livePushConfig.getWaterMarkInfos().get(i).mWaterMarkCoordY < 0 || livePushConfig.getWaterMarkInfos().get(i).mWaterMarkHeight <= 0) {
2122  throw new IllegalStateException("watermark param error");
2123  }
2124  }
2125  }
2126 
2127  @Override
2128  public void setPreviewOrientation(AlivcPreviewOrientationEnum orientation) {
2129  AlivcLog.i(TAG, "setPreviewOrientation");
2130  if (mNativeAlivcLivePusher == null) {
2131  AlivcLog.e(TAG, "Illegal State, you should init first");
2132  return;
2133  }
2134  if (mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.PUSHED) {
2135  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
2136  return;
2137  }
2138 
2139  mNativeAlivcLivePusher.setOrientaion(orientation.getOrientation());
2140  }
2141 
2142  @Override
2143  public int setAudioEffectVoiceChangeMode(AlivcLivePushAudioEffectVoiceChangeMode mode) {
2144  AlivcLog.i(TAG, "setAudioEffectVoiceChangeMode: " + mode);
2145 
2146  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
2147  AlivcLog.e(TAG, "Illegal State, you should init first");
2148  return -1;
2149  }
2150 
2151  if (mAlivcLivePushConfig.isVideoOnly()) {
2152  AlivcLog.e(TAG, "Sound effect can not be used on the video only mode!");
2153  return -1;
2154  }
2155 
2156  return mNativeAlivcLivePusher.setNativeAudioEffectVoiceMode(mode.getValue());
2157  }
2158 
2159  @Override
2160  public int setAudioEffectReverbMode(AlivcLivePushAudioEffectReverbMode mode) {
2161  AlivcLog.i(TAG, "setAudioEffectReverbMode: " + mode);
2162 
2163  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
2164  AlivcLog.e(TAG, "Illegal State, you should init first");
2165  return -1;
2166  }
2167 
2168  if (mAlivcLivePushConfig.isVideoOnly()) {
2169  AlivcLog.e(TAG, "Sound effect can not be used on the video only mode!");
2170  return -1;
2171  }
2172 
2173  return mNativeAlivcLivePusher.setNativeAudioEffectReverbMode(mode.getValue());
2174  }
2175 
2176  private Map<String, String> getPerformanceMap() {
2177  if (mNativeAlivcLivePusher == null) {
2178  return null;
2179  }
2180  String mapStr = mNativeAlivcLivePusher.getPerformanceInfo();
2181  if (mapStr == null || "".equals(mapStr)) {
2182  return null;
2183  }
2184  Map map = new HashMap<String, String>();
2185  StringTokenizer items;
2186  for (StringTokenizer entries = new StringTokenizer(mapStr, "|"); entries.hasMoreTokens(); map
2187  .put(items.nextToken(), items.hasMoreTokens() ? ((String) (items.nextToken())) : null)) {
2188  items = new StringTokenizer(entries.nextToken(), ":");
2189  }
2190  return map;
2191  }
2192 
2193  private String getHeartBeatInfo() {
2194  if (mNativeAlivcLivePusher == null) {
2195  return null;
2196  }
2197 
2198  String heartBeatInfo = mNativeAlivcLivePusher.getPusherInfo();
2199  heartBeatInfo += ",cpu:" + AliLiveInfoUtils.getCPUUsageRatio();
2200  heartBeatInfo += ",mem:" + AliLiveInfoUtils.getMemoryUsageRatio();
2201 
2202  return heartBeatInfo;
2203  }
2204 
2205  @Override
2206  public String getLiveTraceId() {
2207  if (mNativeAlivcLivePusher == null) {
2208  return null;
2209  }
2210  return mNativeAlivcLivePusher.getPusherTraceId();
2211  }
2212 
2213  @Override
2214  public void sendMessage(String info, int repeat, int delay, boolean isKeyFrame) {
2215  AlivcLog.i(TAG, "sendMessage: " + info + ", repeat:" + repeat + ", delay:" + delay + ", isKeyFrame:" + isKeyFrame);
2216 
2217  try {
2218  sendMessageInternal(info, repeat, delay, isKeyFrame, false);
2219  } catch (Exception e) {
2220  e.printStackTrace();
2221  }
2222  }
2223 
2224  private void sendMessageInternal(String info, int repeat, int delay, boolean isKeyFrame, boolean isGDelayInfo) throws IllegalStateException, IllegalArgumentException {
2225  if (mNativeAlivcLivePusher == null) {
2226  AlivcLog.e(TAG, "Illegal State, you should init first");
2227  return;
2228  }
2229  if (!mNativeAlivcLivePusher.isPushing()) {
2230  AlivcLog.e(TAG, "Status error, status should be PUSHED");
2231  return;
2232  }
2233  if (TextUtils.isEmpty(info) || info.length() > MAX_CHATS) {
2234  AlivcLog.e(TAG, "The maximum length is 4000");
2235  return;
2236  }
2237  mNativeAlivcLivePusher.addSeiInfo(info, repeat, delay, isKeyFrame, isGDelayInfo);
2238  }
2239 
2240  private int getBGMVolume() {
2241  if (mMute) {
2242  return 0;
2243  }
2244  return mBGMVolume;
2245  }
2246 
2247  private int getCaptureVolume() {
2248  if (mMute) {
2249  return 0;
2250  }
2251  return mCaptureVolume;
2252  }
2253 
2254  private boolean getHeadSetPlugOn() {
2255  return LivePusherJNI.headSetOn;
2256  }
2257 
2258  private boolean getBlueToothHeadSetOn() {
2259  if (mBluetoothHelper != null) {
2260  return mBluetoothHelper.isOnHeadsetSco();
2261  }
2262  return false;
2263  }
2264 
2265  @Override
2266  public void startBGMAsync(String path) throws IllegalStateException {
2267  AlivcLog.i(TAG, "startBGMAsync");
2268  if (mNativeAlivcLivePusher == null) {
2269  AlivcLog.e(TAG, "Illegal State, you should init first");
2270  return;
2271  }
2272  if (mPushStatus == AlivcLivePushStats.IDLE || mPushStatus == AlivcLivePushStats.ERROR) {
2273  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
2274  return;
2275  }
2276 
2277  if (mPlayStats == AlivcLivePlayStats.IDLE || mPlayStats == AlivcLivePlayStats.STOPPED) {
2278  PusherSetBGMEvent.Args args = new PusherSetBGMEvent.Args();
2279  args.isOpen = true;
2280  args.music = getBGMVolume();
2281  args.voice = getCaptureVolume();
2282  if (mLiveEventReporter != null) {
2283  mLiveEventReporter.sendEvent(PusherSetBGMEvent.kTopicType, PusherSetBGMEvent.kTopicValue, PusherSetBGMEvent.getArgsStr(args));
2284  }
2285  }
2286 
2287  //mNativeAlivcLivePusher.resumeBGM();
2288  mNativeAlivcLivePusher.stopBGM();
2289  mNativeAlivcLivePusher.startBGMAsync(path);
2290  }
2291 
2292  @Override
2293  public void stopBGMAsync() throws IllegalStateException {
2294  AlivcLog.i(TAG, "stopBGMAsync");
2295  if (mNativeAlivcLivePusher == null) {
2296  AlivcLog.e(TAG, "Illegal State, you should init first");
2297  return;
2298  }
2299  if (mPlayStats == AlivcLivePlayStats.IDLE) {
2300  AlivcLog.e(TAG, "status error current state is " + AlivcLivePlayStats.values()[mPlayStats.ordinal()]);
2301  return;
2302  }
2303 
2304  mNativeAlivcLivePusher.stopBGM();
2305  PusherSetBGMEvent.Args args = new PusherSetBGMEvent.Args();
2306  args.isOpen = false;
2307  args.music = getBGMVolume();
2308  args.voice = getCaptureVolume();
2309  if (mLiveEventReporter != null) {
2310  mLiveEventReporter.sendEvent(PusherSetBGMEvent.kTopicType, PusherSetBGMEvent.kTopicValue, PusherSetBGMEvent.getArgsStr(args));
2311  }
2312  }
2313 
2314  @Override
2315  public void pauseBGM() throws IllegalStateException {
2316  AlivcLog.i(TAG, "pauseBGM");
2317  if (mNativeAlivcLivePusher == null) {
2318  AlivcLog.e(TAG, "Illegal State, you should init first");
2319  return;
2320  }
2321  if (mPlayStats != AlivcLivePlayStats.STARTED) {
2322  AlivcLog.e(TAG, "status error current state is " + AlivcLivePlayStats.values()[mPlayStats.ordinal()]);
2323  return;
2324  }
2325 
2326  mNativeAlivcLivePusher.pauseBGM();
2327  }
2328 
2329  @Override
2330  public void resumeBGM() throws IllegalStateException {
2331  AlivcLog.i(TAG, "resumeBGM");
2332  if (mNativeAlivcLivePusher == null) {
2333  AlivcLog.e(TAG, "Illegal State, you should init first");
2334  return;
2335  }
2336  if (mPlayStats != AlivcLivePlayStats.PAUSED) {
2337  AlivcLog.e(TAG, "status error current state is " + AlivcLivePlayStats.values()[mPlayStats.ordinal()]);
2338  return;
2339  }
2340 
2341  mNativeAlivcLivePusher.resumeBGM();
2342  }
2343 
2344  @Override
2345  public void setBGMLoop(boolean isLoop) throws IllegalStateException {
2346  if (mNativeAlivcLivePusher != null) {
2347  mNativeAlivcLivePusher.setBGMLoop(isLoop);
2348  }
2349  PusherSetBGMLoopEvent.Args args = new PusherSetBGMLoopEvent.Args();
2350  args.value = isLoop;
2351  if (mLiveEventReporter != null) {
2352  mLiveEventReporter.sendEvent(PusherSetBGMLoopEvent.kTopicType, PusherSetBGMLoopEvent.kTopicValue, PusherSetBGMLoopEvent.getArgsStr(args));
2353  }
2354  }
2355 
2356  @Override
2357  public void setBGMEarsBack(boolean isOpen) throws IllegalStateException {
2358  if (mNativeAlivcLivePusher != null) {
2359  mNativeAlivcLivePusher.setEarsBack(isOpen);
2360  }
2361  PusherSetBGMEarsBackEvent.Args args = new PusherSetBGMEarsBackEvent.Args();
2362  args.value = isOpen;
2363  args.bh = getBlueToothHeadSetOn() ? 1 : 0;
2364  args.eh = getHeadSetPlugOn() ? 1 : 0;
2365  if (mLiveEventReporter != null) {
2366  mLiveEventReporter.sendEvent(PusherSetBGMEarsBackEvent.kTopicType, PusherSetBGMEarsBackEvent.kTopicValue, PusherSetBGMEarsBackEvent.getArgsStr(args));
2367  }
2368  }
2369 
2370  @Override
2371  public void setBGMVolume(int volume) throws IllegalStateException, IllegalArgumentException {
2372  if (mNativeAlivcLivePusher == null) {
2373  AlivcLog.e(TAG, "Illegal State, you should init first");
2374  return;
2375  }
2376  mBGMVolume = (volume > 0 && volume < 10) ? 10 : volume;
2377  mNativeAlivcLivePusher.setBackgroundMusicVolume(mBGMVolume / 10);
2378  }
2379 
2380  @Override
2381  public void setCaptureVolume(int volume) throws IllegalStateException, IllegalArgumentException {
2382  if (mNativeAlivcLivePusher == null) {
2383  AlivcLog.e(TAG, "Illegal State, you should init first");
2384  return;
2385  }
2386  mCaptureVolume = (volume > 0 && volume < 10) ? 10 : volume;
2387  mNativeAlivcLivePusher.setAudioVolume(mCaptureVolume / 10);
2388  }
2389 
2390  @Override
2391  public void setAudioDenoise(boolean on) {
2392  AlivcLog.i(TAG, "setAudioDenoise, " + on);
2393 
2394  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
2395  AlivcLog.e(TAG, "Illegal State, you should init first");
2396  return;
2397  }
2398 
2399  if (mAlivcLivePushConfig.isVideoOnly()) {
2400  AlivcLog.e(TAG, "Audio denoise can not be used on the video only mode!");
2401  return;
2402  }
2403 
2404  mNativeAlivcLivePusher.setAudioDenoise(on);
2405  }
2406 
2407  @Override
2408  public int startIntelligentDenoise() {
2409  AlivcLog.i(TAG, "startIntelligentDenoise");
2410  return setIntelligentDenoiseInternal(true);
2411  }
2412 
2413  @Override
2414  public int stopIntelligentDenoise() {
2415  AlivcLog.i(TAG, "stopIntelligentDenoise");
2416  return setIntelligentDenoiseInternal(false);
2417  }
2418 
2419  private int setIntelligentDenoiseInternal(boolean flag) {
2420  if (!AlivcLivePushIntelligentDenoiseManager.isFeatureWorked(mContext)) {
2421  AlivcLog.e(TAG, "Intelligent denoise feature not worked due to the lack of native library file! " +
2422  "You can try to read the API doc to find the solution!");
2423  return -1;
2424  }
2425 
2426  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
2427  AlivcLog.e(TAG, "Illegal State, you should init first");
2428  return -1;
2429  }
2430 
2431  if (mAlivcLivePushConfig.isVideoOnly()) {
2432  AlivcLog.e(TAG, "Audio denoise can not be used on the video only mode!");
2433  return -1;
2434  }
2435 
2436  int result = 0;
2437  if (flag) {
2438  result = mNativeAlivcLivePusher.startNativeIntelligentDenoise();
2439  } else {
2440  result = mNativeAlivcLivePusher.stopNativeIntelligentDenoise();
2441  }
2442 
2443  AlivcLog.i(TAG, "setIntelligentDenoiseInternal " + flag + ", return " + result);
2444  return result;
2445  }
2446 
2447  @Override
2448  public void setQualityMode(AlivcQualityModeEnum mode) throws IllegalStateException {
2449  if (mNativeAlivcLivePusher == null) {
2450  AlivcLog.e(TAG, "Illegal State, you should init first");
2451  return;
2452  }
2453  if (AlivcQualityModeEnum.QM_CUSTOM.equals(mode)) {
2454  AlivcLog.e(TAG, "Cannot set QM_CUSTOM dynamically");
2455  return;
2456  }
2457  mNativeAlivcLivePusher.setQualityMode(mode.getQualityMode());
2458  }
2459 
2460  @Override
2461  public void setPreviewMode(AlivcPreviewDisplayMode mode) throws IllegalStateException {
2462  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
2463  AlivcLog.e(TAG, "Illegal State, you should init first");
2464  return;
2465  }
2466  if (mAlivcLivePushConfig.getMediaProjectionPermissionResultData() == null) {
2467  mNativeAlivcLivePusher.setDisplayMode(mode.getPreviewDisplayMode());
2468  }
2469  }
2470 
2471  private void startScheduleExecutor() {
2472  if (scheduleExecutor != null) {
2473  return;
2474  }
2475  if (scheduleExecutor == null || scheduleExecutor.isShutdown()) {
2476  scheduleExecutor = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
2477  private AtomicInteger atoInteger = new AtomicInteger(0);
2478 
2479  @Override
2480  public Thread newThread(Runnable r) {
2481  Thread t = new Thread(r);
2482  t.setName("LivePusher-report-Thread " + atoInteger.getAndIncrement());
2483  return t;
2484  }
2485  });
2486  }
2487  scheduleExecutor.scheduleAtFixedRate(new Runnable() {
2488 
2489  @Override
2490  public void run() {
2491  mTimeCount = mTimeCount + 1;
2492  if(mTimeCount == 5) {
2493  if (mTimeStamp != -1) {
2494  if (mExpiryTime < 60) {
2495  if (mPushNetworkListener != null) {
2496  String url = mPushNetworkListener.onPushURLAuthenticationOverdue(getLivePusherReference());
2497  if (!TextUtils.isEmpty(url) && !url.equals(mPushUrl)) {
2498  reconnectPushAsync(url);
2499  }
2500  }
2501  } else {
2502  mExpiryTime = mExpiryTime - 5;
2503  }
2504  }
2505 
2506  PusherHeartBeatEvent.Args args = new PusherHeartBeatEvent.Args();
2507  args.mediaInfo = getHeartBeatInfo();
2508  mLiveEventReporter.sendEvent(PusherHeartBeatEvent.kTopicType, PusherHeartBeatEvent.kTopicValue, PusherHeartBeatEvent.getArgsStr(args));
2509  mTimeCount = 0;
2510  }
2511  mPushInfoListener.onPushStatistics(getLivePusherReference(),getLivePushStatsInfo());
2512  }
2513  }, 0, 2 * 1000, TimeUnit.MILLISECONDS);
2514 
2515  }
2516 
2517  private int[] getImageWidthHeight(String path) {
2518  BitmapFactory.Options options = new BitmapFactory.Options();
2519 
2520  options.inJustDecodeBounds = true;
2521  try {
2522  Bitmap bitmap = BitmapFactory.decodeFile(path, options); // 此时返回的bitmap为null
2523  } catch (Exception e) {
2524  return new int[]{0, 0};
2525  }
2526 
2527  return new int[]{options.outWidth, options.outHeight};
2528  }
2529 
2530 
2531  private void addWaterMark() {
2532  if (mAlivcLivePushConfig == null) {
2533  AlivcLog.e(TAG, "Illegal State, you should init first");
2534  return;
2535  }
2536  ArrayList<WaterMarkInfo> waterMarkInfos = mAlivcLivePushConfig.getWaterMarkInfos();
2537  int size = waterMarkInfos.size();
2538  for (int i = 0; i < size; i++) {
2539  addWaterMark(waterMarkInfos.get(i).mWaterMarkPath, waterMarkInfos.get(i).mWaterMarkCoordX, waterMarkInfos.get(i).mWaterMarkCoordY,
2540  waterMarkInfos.get(i).mWaterMarkWidth);
2541  }
2542  }
2543 
2544  private void stop5Interval() {
2545  if (scheduleExecutor != null && !scheduleExecutor.isShutdown()) {
2546  scheduleExecutor.shutdown();
2547  }
2548  scheduleExecutor = null;
2549  }
2550 
2551  // RTS降级到RTMP逻辑
2552  private void rtsDownToRtmp() {
2553  AlivcLog.w(TAG, "rts downgrade to rtmp");
2554  if (TextUtils.isEmpty(mPushUrl) || !mPushUrl.startsWith(AlivcLiveStreamType.EnumType.AlivcLiveStreamRts.getPrefix())) {
2555  return;
2556  }
2557 
2558  AlivcLog.w(TAG, "pre url: " + mPushUrl);
2559  mPushUrl = mPushUrl.replaceFirst(AlivcLiveStreamType.EnumType.AlivcLiveStreamRts.getPrefix(), AlivcLiveStreamType.EnumType.AlivcLiveStreamRtmp.getPrefix());
2560  AlivcLog.w(TAG, "after url: " + mPushUrl);
2561  mPushStatus = AlivcLivePushStats.ERROR;
2562 
2563  stopPush();
2564  if (mNativeAlivcLivePusher != null) {
2565  mNativeAlivcLivePusher.release();
2566  mNativeAlivcLivePusher.init();
2567  }
2568 
2569  startPushAsync(mPushUrl);
2570  }
2571 
2572  private void reportStartPushEvent(boolean isRestart, boolean isSync) {
2573  PusherStartPushEvent.Args args = new PusherStartPushEvent.Args();
2574  Map map = getPerformanceMap();
2575  if (map != null) {
2576  args.sync = isSync;
2577  args.vutMs = getInteger(map, "mVideoDurationFromeCaptureToUpload");
2578  args.autMs = getInteger(map, "mAudioDurationFromeCaptureToUpload");
2579  args.resolution = mAlivcLivePushConfig.getResolution().toString().substring(11);
2580  args.st = mAlivcLivePushConfig.getAudioChannels() == 1 ? "single" : "dual";
2581  args.ao = mAlivcLivePushConfig.isAudioOnly();
2582  args.vo = mAlivcLivePushConfig.isVideoOnly();
2583  args.he = mAlivcLivePushConfig.getVideoEncodeMode().equals(Encode_MODE_HARD);//是否使用硬编
2584  args.wc = mAlivcLivePushConfig.getWaterMarkInfos().size();//水印
2585  args.pum = mAlivcLivePushConfig.isPushMirror();
2586  args.fps = mAlivcLivePushConfig.getFps();
2587  args.ivb = mAlivcLivePushConfig.getInitialVideoBitrate();
2588  args.mavb = mAlivcLivePushConfig.getTargetVideoBitrate();
2589  args.mivb = mAlivcLivePushConfig.getMinVideoBitrate();
2590  args.asr = mAlivcLivePushConfig.getAudioSampleRate().getAudioSampleRate();
2591  args.po = mAlivcLivePushConfig.getPreviewOrientation();
2592  args.ct = mAlivcLivePushConfig.getCameraType();
2593  args.flash = mAlivcLivePushConfig.isFlash();
2594  args.crmc = mAlivcLivePushConfig.getConnectRetryCount();
2595  args.cri = mAlivcLivePushConfig.getConnectRetryInterval();
2596  args.prm = mAlivcLivePushConfig.isPreviewMirror();
2597  args.gop = mAlivcLivePushConfig.getVideoEncodeGop();
2598  args.utm = (mAlivcLivePushConfig.getConnectRetryCount() * mAlivcLivePushConfig.getConnectRetryInterval()) / 1000;
2599 
2600  if (isRestart) {
2601  mLiveEventReporter.sendEvent(PusherRestartPushEvent.kTopicType, PusherRestartPushEvent.kTopicValue, PusherRestartPushEvent.getArgsStr(args));
2602  } else {
2603  mLiveEventReporter.sendEvent(PusherStartPushEvent.kTopicType, PusherStartPushEvent.kTopicValue, PusherStartPushEvent.getArgsStr(args));
2604  }
2605  }
2606  }
2607 
2608  private BroadcastReceiver mHeadsetPlugReceiver = new BroadcastReceiver() {
2609 
2610  @Override
2611  public void onReceive(Context context, Intent intent) {
2612 
2613  String action = intent.getAction();
2614  if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(action)) {
2615  AlivcLog.d("BluetoothHeadsetUtils", "ACTION_AUDIO_BECOMING_NOISY headset out");
2616  //拔出耳机
2617  mAudioManager.setMode(AudioManager.STREAM_VOICE_CALL);
2618  mAudioManager.setSpeakerphoneOn(true);
2619 
2620  LivePusherJNI.headSetOn = false;
2621  if (mNativeAlivcLivePusher != null) {
2622  mNativeAlivcLivePusher.setHeadSet(false);
2623  }
2624  } else if ("android.intent.action.HEADSET_PLUG".equals(action)) {// extra
2625  if (intent.hasExtra("state")) {
2626  if (intent.getIntExtra("state", 0) == 0) {
2627  AlivcLog.d("BluetoothHeadsetUtils", "headset out");
2628  //拔出耳机
2629  mAudioManager.setMode(AudioManager.STREAM_VOICE_CALL);
2630  mAudioManager.setSpeakerphoneOn(true);
2631 
2632  if (LivePusherJNI.headSetOn) {
2633  LivePusherJNI.headSetOn = false;
2634  if (mNativeAlivcLivePusher != null) {
2635  mNativeAlivcLivePusher.setHeadSet(false);
2636  }
2637  }
2638  } else if (intent.getIntExtra("state", 0) == 1) {
2639  //插上耳机
2640  AlivcLog.d("BluetoothHeadsetUtils", "headset in");
2641  mAudioManager.setSpeakerphoneOn(false);//
2642  LivePusherJNI.headSetOn = true;
2643  if (mNativeAlivcLivePusher != null) {
2644  mNativeAlivcLivePusher.setHeadSet(true);
2645  }
2646  }
2647  }
2648  }
2649  }
2650 
2651  };
2652 
2653  private BroadcastReceiver mTelephoneReceiver = new BroadcastReceiver() {
2654  @Override
2655  public void onReceive(Context context, Intent intent) {
2656  if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
2657 
2658  } else {
2659  if (manager == null) {
2660  if (Looper.myLooper() == null) {
2661  Looper.prepare();
2662  }
2663  manager = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
2664  manager.listen(new PhoneStateListener() {
2665  @Override
2666  public void onCallStateChanged(int state, String incomingNumber) {
2667  super.onCallStateChanged(state, incomingNumber);
2668  switch (state) {
2669  case TelephonyManager.CALL_STATE_IDLE:
2670  //挂断
2671  AlivcLog.i(TAG, "PHONE: CALL_STATE_IDLE");
2672  if (mNativeAlivcLivePusher != null) {
2673  mNativeAlivcLivePusher.setMute(false);
2674  }
2675  break;
2676  case TelephonyManager.CALL_STATE_OFFHOOK:
2677  //接听
2678  AlivcLog.i(TAG, "PHONE: CALL_STATE_OFFHOOK");
2679  if (mNativeAlivcLivePusher != null) {
2680  mNativeAlivcLivePusher.setMute(true);
2681  }
2682  break;
2683  case TelephonyManager.CALL_STATE_RINGING:
2684  //响铃
2685  AlivcLog.i(TAG, "PHONE: CALL_STATE_RINGING");
2686  if (mNativeAlivcLivePusher != null) {
2687  mNativeAlivcLivePusher.setMute(true);
2688  }
2689  break;
2690  default:
2691  break;
2692  }
2693  }
2694  }, PhoneStateListener.LISTEN_CALL_STATE);
2695  Looper.loop();
2696  }
2697  }
2698  }
2699  };
2700 
2701  private void registerHeadsetPlugReceiver() {
2702  //for ear headset receiver, extra
2703  IntentFilter intentFilter = new IntentFilter();
2704  intentFilter.addAction("android.intent.action.HEADSET_PLUG");
2705  mContext.registerReceiver(mHeadsetPlugReceiver, intentFilter);
2706 
2707  IntentFilter intentFilter1 = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
2708  mContext.registerReceiver(mHeadsetPlugReceiver, intentFilter1);
2709 
2710 
2711  IntentFilter intentTelephony = new IntentFilter();
2712  intentTelephony.addAction("android.intent.action.PHONE_STATE");
2713  mContext.registerReceiver(mTelephoneReceiver, intentTelephony);
2714 
2715  registeredCallback = true;
2716  }
2717 
2718  private void LogWhenGoBackOrFront(Application app, boolean bind) {
2719  if(bind){
2720  mAppFrontBackHelper = new AppFrontBackHelper();
2721  mAppFrontBackHelper.bindApplication(app, new AppFrontBackHelper.OnAppStatusListener() {
2722  @Override
2723  public void onFront() {
2724  if (mNativeAlivcLivePusher != null) {
2725  AlivcLog.i(TAG, "applicationWillBecomeActive");
2726  mNativeAlivcLivePusher.setAppBackgroundState(false);
2727  }
2728  }
2729 
2730  @Override
2731  public void onBack() {
2732  if (mNativeAlivcLivePusher != null) {
2733  AlivcLog.i(TAG, "applicationWillResignActive");
2734  mNativeAlivcLivePusher.setAppBackgroundState(true);
2735  }
2736  }
2737  });
2738  } else {
2739  if(mAppFrontBackHelper != null){
2740  mAppFrontBackHelper.unBindApplication(app);
2741  mAppFrontBackHelper = null;
2742  }
2743  }
2744  }
2745 
2746  private void getNetworkTime() {
2747  new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
2748  private AtomicInteger atoInteger = new AtomicInteger(0);
2749 
2750  @Override
2751  public Thread newThread(Runnable r) {
2752  Thread t = new Thread(r);
2753  t.setName("LivePusher-NTP-Time-Thread " + atoInteger.getAndIncrement());
2754  return t;
2755  }
2756  }).execute(new Runnable() {
2757  @Override
2758  public void run() {
2759  for (int i = 0; i < 3; i++) {
2760  mTimeStamp = getTimeFromNtpServer("time.pool.aliyun.com");
2761  if (mTimeStamp > 0) {
2762  mTimeStamp = mTimeStamp / 1000;
2763  break;
2764  }
2765  }
2766  if (mTimeStamp < 0) {
2767  mTimeStamp = System.currentTimeMillis() / 1000;
2768  }
2769  if (getTimestamp(mPushUrl) != -1) {
2770  mExpiryTime = (int) ((getTimestamp(mPushUrl) - mTimeStamp));
2771  }
2772  }
2773  });
2774  }
2775 
2776  private long getTimeFromNtpServer(String ntpHost) {
2777  AlivcLog.d("", "get time from " + ntpHost);
2778  SntpClient client = new SntpClient();
2779  boolean isSuccessful = client.requestTime(ntpHost, NTP_TIME_OUT_MILLISECOND);
2780  if (isSuccessful) {
2781  return client.getNtpTime();
2782  }
2783  return -1;
2784  }
2785 
2786  @Override
2787  public void addWaterMark(String path, float x, float y, float width) {
2788  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
2789  AlivcLog.e(TAG, "Illegal State, you should init first");
2790  return;
2791  }
2792  int[] widthHeight = getImageWidthHeight(path);
2793  if (mWatermarkCount >= AlivcLiveMaxWatermarkCount || widthHeight == null || widthHeight[0] == 0 || widthHeight[1] == 0) {
2794  return;
2795  }
2796 
2797  float w = width;
2798  float h = width * widthHeight[1] / widthHeight[0];
2799  if (mAlivcLivePushConfig.getPreviewOrientation() == AlivcPreviewOrientationEnum.ORIENTATION_LANDSCAPE_HOME_LEFT.getOrientation() ||
2800  mAlivcLivePushConfig.getPreviewOrientation() == AlivcPreviewOrientationEnum.ORIENTATION_LANDSCAPE_HOME_RIGHT.getOrientation()) {
2801  h = h * mAlivcLivePushConfig.getHeight() / mAlivcLivePushConfig.getWidth();
2802  } else {
2803  h = h * mAlivcLivePushConfig.getWidth() / mAlivcLivePushConfig.getHeight();
2804  }
2805  // 逻辑与IOS端同步
2806  mNativeAlivcLivePusher.addWaterMark(path, w, h, x + w / 2, y + h / 2);
2807  mWatermarkCount++;
2808  }
2809 
2810  @Override
2811  public void setWatermarkVisible(boolean visible) {
2812  if (mNativeAlivcLivePusher == null) {
2813  AlivcLog.e(TAG, "Illegal State, you should init first");
2814  return;
2815  }
2816  if (mPushStatus != AlivcLivePushStats.PREVIEWED) {
2817  AlivcLog.e(TAG, "status error current state is " + AlivcLivePushStats.values()[mPushStatus.ordinal()]);
2818  return;
2819  }
2820  if (mWatermarkCount <= 0) {
2821  return;
2822  }
2823  mNativeAlivcLivePusher.setWaterMarkVisible(visible);
2824  }
2825 
2826  @Override
2827  public int startCamera(SurfaceView surfaceView) throws IllegalStateException {
2828  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
2829  AlivcLog.e(TAG, "Please execute init first ");
2830  return -1;
2831  }
2832  if (mAlivcLivePushConfig.getMediaProjectionPermissionResultData() == null) {
2833  AlivcLog.e(TAG, "Not in ScreenCapture Mode");
2834  return -1;
2835  }
2836  if (!ScreenRecordStatus.SCREEN_RECORD_NORMAL.equals(mScreenStatus)) {
2837  AlivcLog.e(TAG, "you should start push screen first");
2838  return -1;
2839  }
2840  Surface surface = null;
2841  if (surfaceView != null) {
2842  surface = surfaceView.getHolder().getSurface();
2843  }
2844  int ret = mNativeAlivcLivePusher.startCamera(surface);
2845  mScreenStatus = ScreenRecordStatus.SCREEN_RECORD_CAMERA_START;
2846  return ret;
2847  }
2848 
2849  @Override
2850  public void setScreenOrientation(int orientation) {
2851  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
2852  AlivcLog.e(TAG, "Please execute init first ");
2853  return;
2854  }
2855  if (mAlivcLivePushConfig.getMediaProjectionPermissionResultData() == null) {
2856  AlivcLog.e(TAG, "Not in ScreenCapture Mode");
2857  return;
2858  }
2859  mNativeAlivcLivePusher.setScreenOrientation(orientation);
2860  }
2861 
2862  @Override
2863  public void stopCamera() {
2864  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
2865  AlivcLog.e(TAG, "Please execute init first ");
2866  return;
2867  }
2868  if (mAlivcLivePushConfig.getMediaProjectionPermissionResultData() == null) {
2869  AlivcLog.e(TAG, "Not in ScreenCapture Mode");
2870  return;
2871  }
2872  mNativeAlivcLivePusher.stopCamera();
2873  mScreenStatus = ScreenRecordStatus.SCREEN_RECORD_NORMAL;
2874  }
2875 
2876  @Override
2877  public int startCameraMix(float x, float y, float w, float h) {
2878  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
2879  AlivcLog.e(TAG, "Please execute init first ");
2880  return -1;
2881  }
2882  if (mAlivcLivePushConfig.getMediaProjectionPermissionResultData() == null) {
2883  AlivcLog.e(TAG, "Not in ScreenCapture Mode");
2884  return -1;
2885  }
2886  if (!mScreenStatus.equals(ScreenRecordStatus.SCREEN_RECORD_CAMERA_START)) {
2887  AlivcLog.e(TAG, "You should start camera first");
2888  return -1;
2889  }
2890  int ret = mNativeAlivcLivePusher.startCameraMix(x, y, w, h);
2891  mScreenStatus = ScreenRecordStatus.SCREEN_RECORD_CAMERA_MIX_START;
2892  return ret;
2893  }
2894 
2895  @Override
2896  public void stopCameraMix() {
2897  if (mNativeAlivcLivePusher == null || mAlivcLivePushConfig == null) {
2898  AlivcLog.e(TAG, "Please execute init first ");
2899  return;
2900  }
2901  if (mAlivcLivePushConfig.getMediaProjectionPermissionResultData() == null) {
2902  AlivcLog.e(TAG, "Not in ScreenCapture Mode");
2903  return;
2904  }
2905  mNativeAlivcLivePusher.stopCameraMix();
2906  mScreenStatus = ScreenRecordStatus.SCREEN_RECORD_CAMERA_START;
2907  }
2908 
2909  @Override
2910  public void changeResolution(AlivcResolutionEnum resolutionEnum) {
2911  if (mNativeAlivcLivePusher == null) {
2912  AlivcLog.e(TAG, "Please execute init first!");
2913  return;
2914  }
2915  if (mPushStatus != AlivcLivePushStats.PUSHED && mPushStatus != AlivcLivePushStats.PREVIEWED && mPushStatus != AlivcLivePushStats.INIT) {
2916  AlivcLog.e(TAG, "changeResolution can only be called in inited or previewed status");
2917  return;
2918  }
2919  mNativeAlivcLivePusher.changeResolution(AlivcResolutionEnum.GetResolutionWidth(resolutionEnum), AlivcResolutionEnum.GetResolutionHeight(resolutionEnum));
2920  }
2921 
2922  public int mixStreamRequireMain(int vid, boolean require) {
2923  if (mNativeAlivcLivePusher == null) {
2924  return -1;
2925  }
2926  return mNativeAlivcLivePusher.mixStreamRequireMain(vid, require);
2927  }
2928 
2929  public int mixStreamChangePosition(int vid, float x, float y, float w, float h) {
2930  if (mNativeAlivcLivePusher == null) {
2931  return -1;
2932  }
2933  return mNativeAlivcLivePusher.mixStreamChangePosition(vid, x, y, w, h);
2934  }
2935 
2936  public void mixStreamMirror(int vid, boolean mirror) {
2937  if (mNativeAlivcLivePusher == null) {
2938  return;
2939  }
2940  mNativeAlivcLivePusher.setMixStreamMirror(vid, mirror);
2941  }
2942 
2943  public int addMixVideo(AlivcImageFormat format, int width, int height, int rotation, float displayX, float displayY, float displayW, float displayH, boolean adjustHeight) {
2944  if (mNativeAlivcLivePusher == null) {
2945  return -1;
2946  }
2947  return mNativeAlivcLivePusher.addMixVideo(format.getAlivcImageFormat(), width, height, rotation, displayX, displayY, displayW, displayH, adjustHeight);
2948  }
2949 
2950  public void removeMixVideo(int handler) {
2951  if (mNativeAlivcLivePusher == null) {
2952  return;
2953  }
2954  mNativeAlivcLivePusher.removeMixVideo(handler);
2955  }
2956 
2957  public void inputMixTexture(int handler, int texture, int width, int height, long pts, int rotation) {
2958  if (mNativeAlivcLivePusher == null) {
2959  return;
2960  }
2961  mNativeAlivcLivePusher.inputMixTexture(handler, texture, width, height, pts, rotation);
2962  }
2963 
2964  public void inputMixVideoPtr(int handler, long dataptr, int width, int height, int stride, int size, long pts, int rotation) {
2965  if (mNativeAlivcLivePusher == null) {
2966  return;
2967  }
2968  mNativeAlivcLivePusher.inputMixVideoPtr(handler, dataptr, width, height, stride, size, pts, rotation);
2969  }
2970 
2971  public void inputMixVideoData(int handler, byte[] data, int width, int height, int stride, int size, long pts, int rotation) {
2972  if (mNativeAlivcLivePusher == null) {
2973  return;
2974  }
2975  mNativeAlivcLivePusher.inputMixVideoData(handler, data, width, height, stride, size, pts, rotation);
2976  }
2977 
2978  public int addMixAudio(int channels, AlivcSoundFormat format, int audioSample) {
2979  if (mNativeAlivcLivePusher == null) {
2980  return -1;
2981  }
2982  return mNativeAlivcLivePusher.addMixAudio(channels, format.getAlivcSoundFormat(), audioSample);
2983  }
2984 
2985  public void removeMixAudio(int handler) {
2986  if (mNativeAlivcLivePusher == null) {
2987  return;
2988  }
2989  mNativeAlivcLivePusher.removeMixAudio(handler);
2990  }
2991 
2992  public boolean inputMixAudioPtr(int handler, long dataptr, int size, long pts) {
2993  if (mNativeAlivcLivePusher == null) {
2994  return false;
2995  }
2996  return mNativeAlivcLivePusher.inputMixAudioPtr(handler, dataptr, size, pts);
2997  }
2998 
2999  public boolean inputMixAudioData(int handler, byte[] data, int size, long pts) {
3000  if (mNativeAlivcLivePusher == null) {
3001  return false;
3002  }
3003  return mNativeAlivcLivePusher.inputMixAudioData(handler, data, size, pts);
3004  }
3005 
3006  @Override
3007  public void inputStreamVideoPtr(long dataptr, int width, int height, int stride, int size, long pts, int rotation) {
3008  if (mNativeAlivcLivePusher == null) {
3009  return;
3010  }
3011  mNativeAlivcLivePusher.inputStreamVideoPtr(dataptr, width, height, stride, size, pts, rotation);
3012  }
3013 
3014  public void inputStreamTexture(int textureId, int width, int height, int stride, long pts, int rotation, long extra) {
3015  if (mNativeAlivcLivePusher == null) {
3016  return;
3017  }
3018  mNativeAlivcLivePusher.inputStreamTexture(textureId, width, height, stride, pts, rotation, extra);
3019  }
3020 
3021  @Override
3022  public void inputStreamVideoData(byte[] data, int width, int height, int stride, int size, long pts, int rotation) {
3023  if (mNativeAlivcLivePusher == null) {
3024  return;
3025  }
3026  mNativeAlivcLivePusher.inputStreamVideoData(data, width, height, stride, size, pts, rotation);
3027  }
3028 
3029  @Override
3030  public void inputStreamAudioPtr(long dataPtr, int size, int sampleRate, int channels, long pts) {
3031  if (mNativeAlivcLivePusher == null) {
3032  return;
3033  }
3034  mNativeAlivcLivePusher.inputStreamAudioPtr(dataPtr, size, sampleRate, channels, pts);
3035  }
3036 
3037  @Override
3038  public void inputStreamAudioData(byte[] data, int size, int sampleRate, int channels, long pts) {
3039  if (mNativeAlivcLivePusher == null) {
3040  return;
3041  }
3042  mNativeAlivcLivePusher.inputStreamAudioData(data, size, sampleRate, channels, pts);
3043  }
3044 
3045  @Override
3046  public int addDynamicsAddons(String path, float x, float y, float w, float h) throws IllegalArgumentException {
3047  if (mNativeAlivcLivePusher == null) {
3048  return -1;
3049  }
3050  if (x < TEXTURE_RANGE_MIN || x > TEXTURE_RANGE_MAX || y < TEXTURE_RANGE_MIN || y > TEXTURE_RANGE_MAX || w < TEXTURE_RANGE_MIN || w > TEXTURE_RANGE_MAX || h < TEXTURE_RANGE_MIN || h > TEXTURE_RANGE_MAX) {
3051  AlivcLog.e(TAG, "addDynamicsAddons failed! x, y, w, h should in range [0~1.0f]");
3052  return -1;
3053  }
3054  if (mDynamicAddsonCount >= MAX_DYNAMIC_ADDSON_COUNT) {
3055  return -1;
3056  }
3057  mDynamicAddsonCount++;
3058  return mNativeAlivcLivePusher.addDynamicsAddons(path, System.currentTimeMillis() * 1000, 0, x, y, w, h, 0, false);
3059  }
3060 
3061  @Override
3062  public void removeDynamicsAddons(int id) {
3063  if (mNativeAlivcLivePusher == null) {
3064  return;
3065  }
3066  mDynamicAddsonCount--;
3067  mNativeAlivcLivePusher.removeDynamicsAddons(id);
3068  }
3069 
3070  public void setMainStreamPosition(float startX, float startY, float w, float h) {
3071  if (mNativeAlivcLivePusher == null) {
3072  return;
3073  }
3074  mNativeAlivcLivePusher.setMainStreamPosition(startX, startY, w, h);
3075  }
3076 
3077  private String getFormatString(String string) {
3078  String result = "";
3079  int index = string.indexOf(AUTH_KEY);
3080  if (index > 0) {
3081  String temp = string.substring(index);
3082  if (temp.indexOf("-") > 0) {
3083  result = temp.substring(AUTH_KEY.length(), temp.indexOf("-"));
3084  } else {
3085  result = temp.substring(AUTH_KEY.length());
3086  }
3087  }
3088  return result;
3089  }
3090 
3091  private int getTimestamp(String string) {
3092  int result = -1;
3093  String tempStamp = getFormatString(string);
3094  if (tempStamp.length() < 10) {
3095  return -1;
3096  }
3097  if (TextUtils.isDigitsOnly(tempStamp)) {
3098  return new Long(Long.valueOf(tempStamp)).intValue();
3099  }
3100 
3101  return result;
3102  }
3103 
3104  private AlivcSnapshotListener mInnerSnapshotListener = new AlivcSnapshotListener() {
3105  @Override
3106  public void onSnapshot(final Bitmap bmp) {
3107  if (mSnapshotListener != null) {
3108  mThreadPoolExecutor.execute(new Runnable() {
3109  @Override
3110  public void run() {
3111  mSnapshotListener.onSnapshot(bmp);
3112  }
3113  });
3114  }
3115  }
3116  };
3117 }
void onReconnectStart(AlivcLivePusher pusher)
void onFirstFramePreviewed(AlivcLivePusher pusher)
void onPushPaused(AlivcLivePusher pusher)
Live push surface status.
void onNetworkRecovery(AlivcLivePusher pusher)
void onPushStarted(AlivcLivePusher pusher)
void onConnectionLost(AlivcLivePusher pusher)
void onReconnectFail(AlivcLivePusher pusher)
void onPreviewStarted(AlivcLivePusher pusher)
void onPushRestarted(AlivcLivePusher pusher)
void onDropFrame(AlivcLivePusher pusher, int beforeCount, int afterCount)
String onPushURLAuthenticationOverdue(AlivcLivePusher pusher)
void setCameraType(AlivcLivePushCameraTypeEnum cameraType)
Enumeration of streaming encode mode.
void onReconnectSucceed(AlivcLivePusher pusher)
void onPushStopped(AlivcLivePusher pusher)
void onFirstFramePushed(AlivcLivePusher pusher)
void onSDKError(AlivcLivePusher livePusher, AlivcLivePushError error)
void onAdjustFps(AlivcLivePusher pusher, int currentFps, int targetFps)
void onSystemError(AlivcLivePusher livePusher, AlivcLivePushError error)
void onPushStatistics(AlivcLivePusher pusher, AlivcLivePushStatsInfo statistics)
void onSendDataTimeout(AlivcLivePusher pusher)
void onAdjustBitrate(AlivcLivePusher pusher, int currentBitrate, int targetBitrate)
Enumeration of streaming video camera type.
void onPreviewStopped(AlivcLivePusher pusher)
void onPushResumed(AlivcLivePusher pusher)
void onProgress(long progress, long duration)