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