From 14cb51a9b528b02c1b0baf277fdbaa64481a24b9 Mon Sep 17 00:00:00 2001 From: garyxgwang Date: Fri, 15 Jan 2021 19:34:59 +0800 Subject: [PATCH] Fix bug, update sdk. --- Android/Demo/app/build.gradle | 4 +- .../com/tencent/liteav/demo/MainActivity.java | 2 +- .../demo/liveplayer/model/Constants.java | 62 -- .../demo/liveplayer/model/LivePlayer.java | 133 --- .../demo/liveplayer/model/LivePlayerImpl.java | 421 --------- .../camerapush/model/CameraPush.java | 195 ----- .../camerapush/model/CameraPushImpl.java | 812 ------------------ .../camerapush/model/Constants.java | 32 - 8 files changed, 3 insertions(+), 1658 deletions(-) delete mode 100644 Android/Demo/liveplayerdemo/src/main/java/com/tencent/liteav/demo/liveplayer/model/Constants.java delete mode 100644 Android/Demo/liveplayerdemo/src/main/java/com/tencent/liteav/demo/liveplayer/model/LivePlayer.java delete mode 100644 Android/Demo/liveplayerdemo/src/main/java/com/tencent/liteav/demo/liveplayer/model/LivePlayerImpl.java delete mode 100644 Android/Demo/livepusherdemo/src/main/java/com/tencent/liteav/demo/livepusher/camerapush/model/CameraPush.java delete mode 100644 Android/Demo/livepusherdemo/src/main/java/com/tencent/liteav/demo/livepusher/camerapush/model/CameraPushImpl.java delete mode 100644 Android/Demo/livepusherdemo/src/main/java/com/tencent/liteav/demo/livepusher/camerapush/model/Constants.java diff --git a/Android/Demo/app/build.gradle b/Android/Demo/app/build.gradle index 03f1cad4..61b2ce0d 100644 --- a/Android/Demo/app/build.gradle +++ b/Android/Demo/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "com.tencent.liteav.demo" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion - versionCode 12 - versionName "8.2.638" + versionCode 13 + versionName "8.3.645" multiDexEnabled true ndk { diff --git a/Android/Demo/app/src/main/java/com/tencent/liteav/demo/MainActivity.java b/Android/Demo/app/src/main/java/com/tencent/liteav/demo/MainActivity.java index 96189b36..ca8ae119 100644 --- a/Android/Demo/app/src/main/java/com/tencent/liteav/demo/MainActivity.java +++ b/Android/Demo/app/src/main/java/com/tencent/liteav/demo/MainActivity.java @@ -56,7 +56,7 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); mTvVersion = (TextView) findViewById(R.id.main_tv_version); - mTvVersion.setText("Smart版本 v" + TXLiveBase.getSDKVersionStr()+"(8.2.638)"); + mTvVersion.setText("Smart版本 v" + TXLiveBase.getSDKVersionStr()+"(8.3.645)"); mMainTitle = (TextView) findViewById(R.id.main_title); mMainTitle.setOnLongClickListener(new View.OnLongClickListener() { diff --git a/Android/Demo/liveplayerdemo/src/main/java/com/tencent/liteav/demo/liveplayer/model/Constants.java b/Android/Demo/liveplayerdemo/src/main/java/com/tencent/liteav/demo/liveplayer/model/Constants.java deleted file mode 100644 index 26d57247..00000000 --- a/Android/Demo/liveplayerdemo/src/main/java/com/tencent/liteav/demo/liveplayer/model/Constants.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.tencent.liteav.demo.liveplayer.model; - -public class Constants { - /** - * 腾讯云直播拉流Demo提供的默认URL - */ - public static final String NORMAL_PLAY_URL = "http://liteavapp.qcloud.com/live/liteavdemoplayerstreamid.flv"; - - /** - * 腾讯云直播拉流文档URL - */ - public static final String LIVE_PLAYER_DOCUMENT_URL = "https://cloud.tencent.com/document/product/454/7886"; - - /** - * 腾讯云直播拉流超低时延播放文档URL - */ - public static final String LIVE_PLAYER_REAL_TIME_PLAY_DOCUMENT_URL = "https://cloud.tencent.com/document/product/454/7886#RealTimePlay"; - - /** - * 超低时延测试RTMP URL - */ - public static final String RTMP_ACC_TEST_URL = "https://lvb.qcloud.com/weapp/utils/get_test_rtmpaccurl"; - - /** - * MainActivity启动LivePlayerActivity时传递的Activity Type的KEY - */ - public static final String INTENT_ACTIVITY_TYPE = "TYPE"; - - /** - * QRCodeScanActivity完成扫描后,传递过来的结果的KEY - */ - public static final String INTENT_SCAN_RESULT = "SCAN_RESULT"; - - /** - * LivePlayerURLActivity设置页面传递给LivePlayerActivity的直播地址 - */ - public static final String INTENT_URL = "intent_url"; - - public static final String URL_PREFIX_HTTP = "http://"; - public static final String URL_PREFIX_HTTPS = "https://"; - public static final String URL_PREFIX_RTMP = "rtmp://"; - public static final String URL_SUFFIX_FLV = ".flv"; - public static final String URL_TX_SECRET = "txSecret"; - - - public static final int ACTIVITY_TYPE_LIVE_PLAY = 1; // 标准直播播放 - public static final int ACTIVITY_TYPE_REALTIME_PLAY = 2; // 低延时直播播放 - - public static final float CACHE_TIME_FAST = 1.0f; - public static final float CACHE_TIME_SMOOTH = 5.0f; - - public static final int CACHE_STRATEGY_FAST = 0; //极速 - public static final int CACHE_STRATEGY_SMOOTH = 1; //流畅 - public static final int CACHE_STRATEGY_AUTO = 2; //自动 - - public static final int PLAY_STATUS_SUCCESS = 0; - public static final int PLAY_STATUS_EMPTY_URL = -1; - public static final int PLAY_STATUS_INVALID_URL = -2; - public static final int PLAY_STATUS_INVALID_PLAY_TYPE = -3; - public static final int PLAY_STATUS_INVALID_RTMP_URL = -4; - public static final int PLAY_STATUS_INVALID_SECRET_RTMP_URL = -5; -} diff --git a/Android/Demo/liveplayerdemo/src/main/java/com/tencent/liteav/demo/liveplayer/model/LivePlayer.java b/Android/Demo/liveplayerdemo/src/main/java/com/tencent/liteav/demo/liveplayer/model/LivePlayer.java deleted file mode 100644 index e8f0b6d0..00000000 --- a/Android/Demo/liveplayerdemo/src/main/java/com/tencent/liteav/demo/liveplayer/model/LivePlayer.java +++ /dev/null @@ -1,133 +0,0 @@ -package com.tencent.liteav.demo.liveplayer.model; - -import android.os.Bundle; - -import com.tencent.rtmp.TXLivePlayer; - -/** - * 拉流常用接口 - */ -public interface LivePlayer { - - /** - * 开始播放 - */ - void startPlay(); - - /** - * 停止播放 - */ - void stopPlay(); - - /** - * 当前正在播放则停止;当前停止则开始播放 - */ - void togglePlay(); - - /** - * 是否正在播放 - * - * @return - */ - boolean isAcc(); - - /** - * 测试专用 - */ - void startAcc(); - - /** - * 测试专用 - */ - void stopAcc(); - - /** - * 测试专用 - */ - void toggleAcc(); - - /** - * 设置URL - * - * @param url - */ - void setPlayURL(String url); - - /** - * 设置播放模式和URL - * - * @param activityPlayType - * @param url - */ - void setPlayURL(int activityPlayType, String url); - - /** - * 获取播放URL - */ - void fetchPlayURL(); - - /** - * 设置缓存策略 - * - * @param cacheStrategy - */ - void setCacheStrategy(int cacheStrategy); - - /** - * 设置渲染模式 - * - * @param renderMode TXLiveConstants.RENDER_MODE_FULL_FILL_SCREEN:全屏 - * TXLiveConstants.RENDER_MODE_ADJUST_RESOLUTION:自适应 - */ - void setRenderMode(int renderMode); - - int getRenderMode(); - - /** - * 设置视频方向 - * - * @param renderRotation TXLiveConstants.RENDER_ROTATION_LANDSCAPE:横屏 - * TXLiveConstants.RENDER_ROTATION_PORTRAIT: 竖屏 - */ - void setRenderRotation(int renderRotation); - - int getRenderRotation(); - - /** - * 设置解码方式 - * - * @param mode 0:软解,1:硬解 - */ - void setHWDecode(int mode); - - int getHWDecode(); - - /** - * 显示播放器中 log 信息 - */ - void showVideoLog(boolean enable); - - void setOnLivePlayerCallback(OnLivePlayerCallback callback); - - void destroy(); - - interface OnLivePlayerCallback { - /** - * result返回值: - * 0 success; -1 empty url; -2 invalid url; -3 invalid playType; -4 invalid rtmp url; -5 invalid secret rtmp url - */ - void onPlayStart(int code); - - void onPlayStop(); - - void onPlayEvent(int event, Bundle param); - - void onNetStatus(Bundle bundle); - - void onFetchURLStart(); - - void onFetchURLFailure(); - - void onFetchURLSuccess(String url); - } -} diff --git a/Android/Demo/liveplayerdemo/src/main/java/com/tencent/liteav/demo/liveplayer/model/LivePlayerImpl.java b/Android/Demo/liveplayerdemo/src/main/java/com/tencent/liteav/demo/liveplayer/model/LivePlayerImpl.java deleted file mode 100644 index b4ffec03..00000000 --- a/Android/Demo/liveplayerdemo/src/main/java/com/tencent/liteav/demo/liveplayer/model/LivePlayerImpl.java +++ /dev/null @@ -1,421 +0,0 @@ -package com.tencent.liteav.demo.liveplayer.model; - -import android.app.Activity; -import android.content.Context; -import android.os.Bundle; -import android.os.Looper; -import android.text.TextUtils; -import android.util.Log; - -import com.tencent.rtmp.ITXLivePlayListener; -import com.tencent.rtmp.TXLiveConstants; -import com.tencent.rtmp.TXLivePlayConfig; -import com.tencent.rtmp.TXLivePlayer; -import com.tencent.rtmp.ui.TXCloudVideoView; - -import org.json.JSONObject; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; - -import okhttp3.Call; -import okhttp3.Callback; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; - -/** - * 腾讯云 {@link TXLivePlayer} 直播播放器使用参考 Demo - * 有以下功能参考 : - * - 基本功能参考: 启动推流 {@link #startPlay()}与 结束推流 {@link #stopPlay()} - * - 硬件加速: 使用硬解码 - * - 性能数据查看参考: {@link #onNetStatus(Bundle)} - * - 处理 SDK 回调事件参考: {@link #onPlayEvent(int, Bundle)} - * - 渲染角度、渲染模式切换: 横竖屏渲染、铺满与自适应渲染 - * - 缓存策略选择:{@link #setCacheStrategy} 缓存策略:自动、极速、流畅。 极速模式:时延会尽可能低、但抗网络抖动效果不佳;流畅模式:时延较高、抗抖动能力较强 - */ -public class LivePlayerImpl implements LivePlayer, ITXLivePlayListener { - - private static final String TAG = "LivePlayerImpl"; - - private Context mContext; - - private TXLivePlayer mLivePlayer; //直播拉流的视频播放器 - private TXLivePlayConfig mPlayerConfig; //TXLivePlayer 播放配置项 - private TXCloudVideoView mVideoView; - - private String mPlayURL = ""; - private String mAccPlayURL = ""; - - private boolean mIsPlaying = false; - private boolean mFetching = false; //是否正在获取视频源,测试专用 - private boolean mIsAcc = false; //是否播放超低时延视频,测试专用 - private boolean mHWDecode = false; //是否启用了硬解码 - - private int mCacheStrategy = Constants.CACHE_STRATEGY_AUTO; //Player缓存策略 - private int mActivityPlayType = Constants.ACTIVITY_TYPE_LIVE_PLAY; // 播放类型 - private int mCurrentPlayURLType = TXLivePlayer.PLAY_TYPE_LIVE_RTMP; //Player 当前播放链接类型 - private int mRenderMode = TXLiveConstants.RENDER_MODE_ADJUST_RESOLUTION; //Player 当前渲染模式 - private int mRenderRotation = TXLiveConstants.RENDER_ROTATION_PORTRAIT; //Player 当前渲染角度 - - private long mStartPlayTS = 0; //保存开始播放的时间戳,测试专用 - - private OnLivePlayerCallback mOnLivePlayerCallback; - - public LivePlayerImpl(Context context, TXCloudVideoView videoView) { - initialize(context, videoView); - } - - @Override - public void startPlay() { - String playURL = mIsAcc ? mAccPlayURL : mPlayURL; - int code = checkPlayURL(playURL); - if (code != Constants.PLAY_STATUS_SUCCESS) { - mIsPlaying = false; - } else { - mLivePlayer.setPlayerView(mVideoView); - mLivePlayer.setPlayListener(this); - - /** - * 硬件加速在1080p解码场景下效果显著,但细节之处并不如想象的那么美好: - * - 只有 4.3 以上android系统才支持 - * - 兼容性我们目前还仅过了小米华为等常见机型,故这里的返回值您先不要太当真 - * - */ - mLivePlayer.enableHardwareDecode(mHWDecode); - mLivePlayer.setRenderRotation(mRenderRotation); - mLivePlayer.setRenderMode(mRenderMode); - mPlayerConfig.setEnableMessage(true); - mLivePlayer.setConfig(mPlayerConfig); - - /** - * result返回值: - * 0 success; -1 empty url; -2 invalid url; -3 invalid playType; - */ - code = mLivePlayer.startPlay(playURL, mCurrentPlayURLType); - mIsPlaying = code == 0; - - Log.d("video render", "timetrack start play"); - mStartPlayTS = System.currentTimeMillis(); - } - if (mOnLivePlayerCallback != null) { - mOnLivePlayerCallback.onPlayStart(code); - } - } - - @Override - public void stopPlay() { - if (!mIsPlaying) { - return; - } - if (mLivePlayer != null) { - mLivePlayer.stopRecord(); - mLivePlayer.setPlayListener(null); - mLivePlayer.stopPlay(true); - } - mIsPlaying = false; - if (mOnLivePlayerCallback != null) { - mOnLivePlayerCallback.onPlayStop(); - } - } - - @Override - public void togglePlay() { - Log.d(TAG, "togglePlay: mIsPlaying:" + mIsPlaying + ", mCurrentPlayType:" + mActivityPlayType); - if (mIsPlaying) { - stopPlay(); - } else { - startPlay(); - } - } - - @Override - public boolean isAcc() { - return mIsAcc; - } - - @Override - public void startAcc() { - mIsAcc = true; - mActivityPlayType = Constants.ACTIVITY_TYPE_REALTIME_PLAY; - stopPlay(); - fetchPlayURL(); - } - - @Override - public void stopAcc() { - mIsAcc = false; - mActivityPlayType = Constants.ACTIVITY_TYPE_LIVE_PLAY; - // 停止 Acc 播放之后,自动开始普通播放 - stopPlay(); - startPlay(); - } - - @Override - public void toggleAcc() { - if (mIsAcc) { - stopAcc(); - } else { - startAcc(); - } - } - - @Override - public void setPlayURL(String url) { - setPlayURL(Constants.ACTIVITY_TYPE_LIVE_PLAY, url); - } - - @Override - public void setPlayURL(int activityPlayType, String url) { - mActivityPlayType = activityPlayType; - mPlayURL = url; - } - - @Override - public void fetchPlayURL() { - if (mFetching) { - return; - } - mFetching = true; - if (mOnLivePlayerCallback != null) { - mOnLivePlayerCallback.onFetchURLStart(); - } - OkHttpClient mOkHttpClient = new OkHttpClient().newBuilder() - .connectTimeout(10, TimeUnit.SECONDS) - .readTimeout(10, TimeUnit.SECONDS) - .writeTimeout(10, TimeUnit.SECONDS) - .build(); - - Request request = new Request.Builder() - .url(Constants.RTMP_ACC_TEST_URL) - .addHeader("Content-Type", "application/json; charset=utf-8") - .build(); - - Log.d(TAG, "start fetch push url"); - mOkHttpClient.newCall(request).enqueue(new Callback() { - @Override - public void onFailure(Call call, IOException e) { - Log.e(TAG, "fetch push url error.", e); - mFetching = false; - onFailure(); - } - - @Override - public void onResponse(Call call, Response response) throws IOException { - mFetching = false; - if (response.isSuccessful()) { - String rspString = response.body().string(); - try { - JSONObject jsonRsp = new JSONObject(rspString); - final String playURL = jsonRsp.optString("url_rtmpacc"); - mAccPlayURL = playURL; - onSuccess(playURL); - } catch (Exception e) { - Log.e(TAG, "fetch push url error.", e); - onFailure(); - } - } else { - onFailure(); - } - } - - void onSuccess(final String url) { - runMainThread(new Runnable() { - @Override - public void run() { - if (mOnLivePlayerCallback != null) { - mOnLivePlayerCallback.onFetchURLSuccess(url); - } - // 低延时拉流地址获取成功后自动开始播放 - startPlay(); - } - }); - } - - void onFailure() { - runMainThread(new Runnable() { - @Override - public void run() { - if (mOnLivePlayerCallback != null) { - mOnLivePlayerCallback.onFetchURLFailure(); - } - } - }); - } - - }); - } - - @Override - public void setCacheStrategy(int cacheStrategy) { - if (mCacheStrategy == cacheStrategy) return; - mCacheStrategy = cacheStrategy; - switch (cacheStrategy) { - case Constants.CACHE_STRATEGY_FAST: - mPlayerConfig.setAutoAdjustCacheTime(true); - mPlayerConfig.setMaxAutoAdjustCacheTime(Constants.CACHE_TIME_FAST); - mPlayerConfig.setMinAutoAdjustCacheTime(Constants.CACHE_TIME_FAST); - mLivePlayer.setConfig(mPlayerConfig); - break; - case Constants.CACHE_STRATEGY_SMOOTH: - mPlayerConfig.setAutoAdjustCacheTime(false); - mPlayerConfig.setMaxAutoAdjustCacheTime(Constants.CACHE_TIME_SMOOTH); - mPlayerConfig.setMinAutoAdjustCacheTime(Constants.CACHE_TIME_SMOOTH); - mLivePlayer.setConfig(mPlayerConfig); - break; - case Constants.CACHE_STRATEGY_AUTO: - mPlayerConfig.setAutoAdjustCacheTime(true); - mPlayerConfig.setMaxAutoAdjustCacheTime(Constants.CACHE_TIME_SMOOTH); - mPlayerConfig.setMinAutoAdjustCacheTime(Constants.CACHE_TIME_FAST); - mLivePlayer.setConfig(mPlayerConfig); - break; - default: - break; - } - } - - @Override - public void setRenderMode(int renderMode) { - mRenderMode = renderMode; - mLivePlayer.setRenderMode(renderMode); - } - - @Override - public int getRenderMode() { - return mRenderMode; - } - - @Override - public void setRenderRotation(int renderRotation) { - mRenderRotation = renderRotation; - mLivePlayer.setRenderRotation(renderRotation); - } - - @Override - public int getRenderRotation() { - return mRenderRotation; - } - - @Override - public void setHWDecode(int mode) { - mHWDecode = mode == 0; - if (mIsPlaying) { - stopPlay(); - startPlay(); - } - } - - @Override - public int getHWDecode() { - return mHWDecode ? 1 : 0; - } - - @Override - public void showVideoLog(boolean enable) { - mVideoView.showLog(enable); - } - - @Override - public void setOnLivePlayerCallback(OnLivePlayerCallback callback) { - mOnLivePlayerCallback = callback; - } - - @Override - public void destroy() { - if (mLivePlayer != null) { - mLivePlayer.stopPlay(true); - mLivePlayer = null; - } - if (mVideoView != null) { - mVideoView.onDestroy(); - mVideoView = null; - } - mPlayerConfig = null; - } - - @Override - public void onPlayEvent(int event, Bundle param) { - Log.d(TAG, "receive event: " + event + ", " + param.getString(TXLiveConstants.EVT_DESCRIPTION)); - switch (event) { - case TXLiveConstants.PLAY_EVT_PLAY_BEGIN: - Log.d("AutoMonitor", "PlayFirstRender,cost=" + (System.currentTimeMillis() - mStartPlayTS)); - break; - case TXLiveConstants.PLAY_ERR_NET_DISCONNECT: - case TXLiveConstants.PLAY_EVT_PLAY_END: - stopPlay(); - break; - default: - break; - } - if (mOnLivePlayerCallback != null) { - mOnLivePlayerCallback.onPlayEvent(event, param); - } - } - - @Override - public void onNetStatus(Bundle bundle) { - Log.d(TAG, "Current status, CPU:" + bundle.getString(TXLiveConstants.NET_STATUS_CPU_USAGE) + - ", RES:" + bundle.getInt(TXLiveConstants.NET_STATUS_VIDEO_WIDTH) + "*" + bundle.getInt(TXLiveConstants.NET_STATUS_VIDEO_HEIGHT) + - ", SPD:" + bundle.getInt(TXLiveConstants.NET_STATUS_NET_SPEED) + "Kbps" + - ", FPS:" + bundle.getInt(TXLiveConstants.NET_STATUS_VIDEO_FPS) + - ", ARA:" + bundle.getInt(TXLiveConstants.NET_STATUS_AUDIO_BITRATE) + "Kbps" + - ", VRA:" + bundle.getInt(TXLiveConstants.NET_STATUS_VIDEO_BITRATE) + "Kbps"); - if (mOnLivePlayerCallback != null) { - mOnLivePlayerCallback.onNetStatus(bundle); - } - } - - private void initialize(Context context, TXCloudVideoView videoView) { - mContext = context; - mVideoView = videoView; - mPlayerConfig = new TXLivePlayConfig(); - mLivePlayer = new TXLivePlayer(mContext); - showVideoLog(false); - } - - private int checkPlayURL(final String playURL) { - if (TextUtils.isEmpty(playURL)) { - return Constants.PLAY_STATUS_EMPTY_URL; - } - - if (!playURL.startsWith(Constants.URL_PREFIX_HTTP) && !playURL.startsWith(Constants.URL_PREFIX_HTTPS) - && !playURL.startsWith(Constants.URL_PREFIX_RTMP) && !playURL.startsWith("/")) { - return Constants.PLAY_STATUS_INVALID_URL; - } - - boolean isLiveRTMP = playURL.startsWith(Constants.URL_PREFIX_RTMP); - boolean isLiveFLV = (playURL.startsWith(Constants.URL_PREFIX_HTTP) || playURL.startsWith(Constants.URL_PREFIX_HTTPS)) && playURL.contains(Constants.URL_SUFFIX_FLV); - - if (mActivityPlayType == Constants.ACTIVITY_TYPE_LIVE_PLAY) { - if (isLiveRTMP) { - mCurrentPlayURLType = TXLivePlayer.PLAY_TYPE_LIVE_RTMP; - return Constants.PLAY_STATUS_SUCCESS; - } - if (isLiveFLV) { - mCurrentPlayURLType = TXLivePlayer.PLAY_TYPE_LIVE_FLV; - return Constants.PLAY_STATUS_SUCCESS; - } - return Constants.PLAY_STATUS_INVALID_URL; - } - - if (mActivityPlayType == Constants.ACTIVITY_TYPE_REALTIME_PLAY) { - if (!isLiveRTMP) { - return Constants.PLAY_STATUS_INVALID_RTMP_URL; - } - if (!playURL.contains(Constants.URL_TX_SECRET)) { - return Constants.PLAY_STATUS_INVALID_SECRET_RTMP_URL; - } - mCurrentPlayURLType = TXLivePlayer.PLAY_TYPE_LIVE_RTMP_ACC; - return Constants.PLAY_STATUS_SUCCESS; - } - return Constants.PLAY_STATUS_INVALID_URL; - } - - private void runMainThread(Runnable runnable) { - if (Looper.myLooper() != Looper.getMainLooper()) { - ((Activity) mContext).runOnUiThread(runnable); - } else { - runnable.run(); - } - } -} diff --git a/Android/Demo/livepusherdemo/src/main/java/com/tencent/liteav/demo/livepusher/camerapush/model/CameraPush.java b/Android/Demo/livepusherdemo/src/main/java/com/tencent/liteav/demo/livepusher/camerapush/model/CameraPush.java deleted file mode 100644 index 5ec35f1f..00000000 --- a/Android/Demo/livepusherdemo/src/main/java/com/tencent/liteav/demo/livepusher/camerapush/model/CameraPush.java +++ /dev/null @@ -1,195 +0,0 @@ -package com.tencent.liteav.demo.livepusher.camerapush.model; - -import android.os.Bundle; - -import com.tencent.rtmp.TXLivePusher; - -import java.io.File; - -/** - * 推流常用接口 - */ -public interface CameraPush { - - void setURL(String pushURL); - - /** - * 开始推流 - */ - void startPush(); - - /** - * 停止推流 - */ - void stopPush(); - - void togglePush(); - - /** - * 恢复推流 - */ - void resume(); - - void resumePush(); - - /** - * 暂停推流 - */ - void pause(); - - void pausePush(); - - /** - * 销毁推流 - */ - void destroy(); - - /** - * 切换摄像头 - */ - void switchCamera(); - - void setRotationForActivity(); - - void setOnLivePusherCallback(OnLivePusherCallback callback); - - TXLivePusher getTXLivePusher(); - - boolean isPushing(); - - /** - * 设置横竖屏推流 - * - * @param isPortrait - */ - void setHomeOrientation(boolean isPortrait); - - /** - * 设置是否开隐私模式 - * - * @param enable - */ - void setPrivateMode(boolean enable); - - /** - * 设置是否开启静音推流 - * - * @param enable - */ - void setMute(boolean enable); - - /** - * 设置开启或关闭观众端镜像 - * - * @param enable - */ - void setMirror(boolean enable); - - /** - * 设置开启或关闭后置摄像头闪光灯 - * - * @param enable - */ - void turnOnFlashLight(boolean enable); - - /** - * 设置开启或关闭 Debug 面板 - * - * @param enable - */ - void showLog(boolean enable); - - /** - * 设置开启或关闭水印 - * - * @param enable - */ - void setWatermark(boolean enable); - - /** - * 设置开启或关闭手动对焦 - * - * @param enable - */ - void setTouchFocus(boolean enable); - - /** - * 设置开启或关闭双手缩放 - * - * @param enable - */ - void setEnableZoom(boolean enable); - - /** - * 设置截图 - */ - void snapshot(); - - /** - * 设置发送sei消息 - */ - void sendMessage(String string); - - /** - * 设置硬件加速 - * - * @param enable - */ - void setHardwareAcceleration(boolean enable); - - /** - * 设置码率自适应 - * - * @param enable - */ - void setAdjustBitrate(boolean enable, int qualityType); - - /** - * 设置视频编码质量 - * - * @param type - */ - void setQuality(boolean enable, int type); - - /** - * 设置耳返开关 - * - * @param enable - */ - void enableAudioEarMonitoring(boolean enable); - - void enablePureAudioPush(boolean enable); - - /** - * 设置音质选择(声道设置) - * 语音(speech):16000,单声道 - * 标准(default):48000,单声道 - * 音乐(music):48000,双声道 - * - * @param channel 单声道 1,双声道 2 - * @param sampleRate 音频采样率 - */ - void setAudioQuality(int channel, int sampleRate); - - interface OnLivePusherCallback { - /** - * result返回值: - * 0 success; -1 invalid url; -3 invalid playType; -4 invalid rtmp url; -5 invalid secret rtmp url - */ - void onPushStart(int code); - - void onPushResume(); - - void onPushPause(); - - void onPushStop(); - - void onSnapshot(File file); - - void onPushEvent(int event, Bundle param); - - void onNetStatus(Bundle status); - - void onActivityRotationObserverChange(boolean selfChange); - } -} diff --git a/Android/Demo/livepusherdemo/src/main/java/com/tencent/liteav/demo/livepusher/camerapush/model/CameraPushImpl.java b/Android/Demo/livepusherdemo/src/main/java/com/tencent/liteav/demo/livepusher/camerapush/model/CameraPushImpl.java deleted file mode 100644 index 179c5c63..00000000 --- a/Android/Demo/livepusherdemo/src/main/java/com/tencent/liteav/demo/livepusher/camerapush/model/CameraPushImpl.java +++ /dev/null @@ -1,812 +0,0 @@ -package com.tencent.liteav.demo.livepusher.camerapush.model; - -import android.app.Activity; -import android.app.AlarmManager; -import android.app.Service; -import android.content.BroadcastReceiver; -import android.content.ContentResolver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.content.res.Resources; -import android.database.ContentObserver; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.os.AsyncTask; -import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; -import android.provider.Settings; -import android.telephony.PhoneStateListener; -import android.telephony.TelephonyManager; -import android.text.TextUtils; -import android.util.Log; -import android.util.TypedValue; -import android.view.Surface; -import android.view.View; - -import com.tencent.liteav.demo.livepusher.R; -import com.tencent.liteav.demo.livepusher.camerapush.ui.view.PusherSettingFragment; -import com.tencent.rtmp.ITXLivePushListener; -import com.tencent.rtmp.TXLiveConstants; -import com.tencent.rtmp.TXLivePushConfig; -import com.tencent.rtmp.TXLivePusher; -import com.tencent.rtmp.ui.TXCloudVideoView; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.UUID; - -import static com.tencent.liteav.demo.beauty.utils.ResourceUtils.getString; - -/** - * 腾讯云 {@link TXLivePusher} 推流器使用参考 Demo - * - * 有以下功能参考 : - * - * - 基本功能参考: 启动推流 {@link #startPush()} 与 结束推流 {@link #stopPush()} ()} - * - * - 场景化配置参考:{@link PusherSettingFragment} 与 {@link #setPushScene(int, boolean)} 您可以根据您的 App 使用设定不同的推流场景,SDK 内部会自动选择相关配置,让您可以快速搭建 - * 注:一般客户建议直接使用场景化配置;若您是专业级客户,推荐您参考 {@link TXLivePushConfig} 进行个性化配置 - * - * - 性能数据查看参考: {@link #onNetStatus(Bundle)} - * - * - 处理 SDK 回调事件参考: {@link #onPushEvent(int, Bundle)} - * - * - 混响、变声、码率自适应、硬件加速等使用参考: {@link PusherSettingFragment} 与 {@link PusherSettingFragment.OnSettingChangeListener} - * - * - 横屏推流使用参考:该功能较为复杂,需要区分Activity是否可以旋转。 - * A. 不可旋转情况下开启横屏推流:直接参考 {@link #setHomeOrientation(boolean)} 即可 - * B. 可旋转情况下开启横屏推流,参考: {@link ActivityRotationObserver} 与 {@link #setRotationForActivity()} - * - */ -public class CameraPushImpl implements CameraPush, ITXLivePushListener { - - private static final String TAG = "CameraPushImpl"; - - private Context mContext; - private TXLivePusher mLivePusher; - private TXLivePushConfig mLivePushConfig; - private TXCloudVideoView mPusherView; - - private OnLivePusherCallback mOnLivePusherCallback; - - private Bitmap mWaterMarkBitmap; - - private String mPusherURL = ""; - - private boolean mIsPushing = false; - private boolean mIsResume = false; - private boolean mIsWaterMarkEnable = true; - private boolean mIsDebugInfo = false; - private boolean mIsMuteAudio = false; - private boolean mIsPrivateMode = false; - private boolean mIsLandscape = true; - private boolean mIsMirrorEnable = false; - private boolean mIsFocusEnable = false; - private boolean mIsZoomEnable = false; - private boolean mIsPureAudio = false; - private boolean mIsEarMonitoringEnable = false; - private boolean mIsHWAcc = false; - private boolean mFrontCamera = true; - private boolean mIsEnableAdjustBitrate = false; - private boolean mIsFlashLight = false; - - private int mVideoResolution = TXLiveConstants.VIDEO_RESOLUTION_TYPE_540_960; - private int mAudioChannels; - private int mAudioSample; - private int mQualityType; - - /** - * 默认美颜参数 - */ - private int mBeautyLevel = 5; // 美颜等级 - private int mBeautyStyle = TXLiveConstants.BEAUTY_STYLE_SMOOTH; // 美颜样式 - private int mWhiteningLevel = 3; // 美白等级 - private int mRuddyLevel = 2; // 红润等级 - - public CameraPushImpl(Context context, TXCloudVideoView pusherView) { - initialize(context, pusherView); - } - - @Override - public void setURL(String pushURL) { - mPusherURL = pushURL; - } - - @Override - public void startPush() { - int resultCode = Constants.PLAY_STATUS_SUCCESS; - String tRTMPURL = ""; - if (!TextUtils.isEmpty(mPusherURL)) { - String url[] = mPusherURL.split("###"); - if (url.length > 0) { - tRTMPURL = url[0]; - } - } - - if (TextUtils.isEmpty(tRTMPURL) || (!tRTMPURL.trim().toLowerCase().startsWith("rtmp://"))) { - resultCode = Constants.PLAY_STATUS_INVALID_URL; - } else { - // 显示本地预览的View - mPusherView.setVisibility(View.VISIBLE); - // 添加播放回调 - mLivePusher.setPushListener(this); - // 添加后台垫片推流参数 - Bitmap bitmap = decodeResource(mContext.getResources(), R.drawable.livepusher_pause_publish); - mLivePushConfig.setPauseImg(bitmap); - mLivePushConfig.setPauseImg(300, 5); - mLivePushConfig.setPauseFlag(TXLiveConstants.PAUSE_FLAG_PAUSE_VIDEO);// 设置暂停时,只停止画面采集,不停止声音采集。 - - // 设置推流分辨率 - mLivePushConfig.setVideoResolution(mVideoResolution); - - // 如果当前Activity可以自动旋转的话,那么需要进行设置 - if (isActivityCanRotation(mContext)) { - setRotationForActivity(); - } - // 开启麦克风推流相关 - mLivePusher.setMute(mIsMuteAudio); - - // 横竖屏推流相关 - int renderRotation = 0; - if (mIsLandscape) { - mLivePushConfig.setHomeOrientation(TXLiveConstants.VIDEO_ANGLE_HOME_DOWN); - renderRotation = 0; - } else { - mLivePushConfig.setHomeOrientation(TXLiveConstants.VIDEO_ANGLE_HOME_RIGHT); - renderRotation = 90; // 因为采集旋转了,那么保证本地渲染是正的,则设置渲染角度为90度。 - } - mLivePusher.setRenderRotation(renderRotation); - - //根据activity方向调整横竖屏 - setRotationForActivity(); - - // 是否开启观众端镜像观看 - mLivePusher.setMirror(mIsMirrorEnable); - // 是否打开调试信息 - mPusherView.showLog(mIsDebugInfo); - - // 是否添加水印 - if (mIsWaterMarkEnable) { - mLivePushConfig.setWatermark(mWaterMarkBitmap, 0.02f, 0.05f, 0.2f); - } else { - mLivePushConfig.setWatermark(null, 0, 0, 0); - } - - // 是否打开曝光对焦 - mLivePushConfig.setTouchFocus(mIsFocusEnable); - // 是否打开手势放大预览画面 - mLivePushConfig.setEnableZoom(mIsZoomEnable); - mLivePushConfig.enablePureAudioPush(mIsPureAudio); - mLivePushConfig.enableAudioEarMonitoring(mIsEarMonitoringEnable); - // 设置推流配置 - mLivePusher.setConfig(mLivePushConfig); - // 设置场景 - setPushScene(mQualityType, mIsEnableAdjustBitrate); - - // 设置声道,必须在 TXLivePusher.setVideoQuality 之后,TXLivePusher.startPusher之前设置才能生效 - mLivePushConfig.setAudioChannels(mAudioChannels); - // 设置音频采样率,必须在 TXLivePusher.setVideoQuality 之后,TXLivePusher.startPusher之前设置才能生效 - mLivePushConfig.setAudioSampleRate(mAudioSample); - mLivePusher.setConfig(mLivePushConfig); - - // 设置本地预览View - mLivePusher.startCameraPreview(mPusherView); - if (!mFrontCamera) mLivePusher.switchCamera(); - // 发起推流 - resultCode = mLivePusher.startPusher(tRTMPURL.trim()); - - mIsPushing = true; - } - Log.i(TAG, "start: mIsResume -> " + mIsResume); - if (mOnLivePusherCallback != null) { - mOnLivePusherCallback.onPushStart(resultCode); - } - } - - @Override - public void stopPush() { - if (!mIsPushing) { - return; - } - // 停止本地预览 - mLivePusher.stopCameraPreview(true); - // 移除监听 - mLivePusher.setPushListener(null); - // 停止推流 - mLivePusher.stopPusher(); - // 隐藏本地预览的View - mPusherView.setVisibility(View.GONE); - // 移除垫片图像 - mLivePushConfig.setPauseImg(null); - mIsPrivateMode = false; - mIsPushing = false; - if (mOnLivePusherCallback != null) { - mOnLivePusherCallback.onPushStop(); - } - } - - @Override - public void togglePush() { - if (mIsPushing) { - stopPush(); - } else { - startPush(); - } - } - - @Override - public void resume() { - Log.i(TAG, "resume: mIsResume -> " + mIsResume); - if (mIsResume) { - return; - } - if (mPusherView != null) { - mPusherView.onResume(); - } - if (mIsPushing && mLivePusher != null) { - // 如果当前是隐私模式,那么不resume - if (!mIsPrivateMode) { - mLivePusher.resumePusher(); - } - } - mIsResume = true; - if (mOnLivePusherCallback != null) { - mOnLivePusherCallback.onPushResume(); - } - } - - @Override - public void resumePush() { - mLivePusher.resumePusher(); - } - - @Override - public void pause() { - Log.i(TAG, "pause: mIsResume -> " + mIsResume); - if (mPusherView != null) { - mPusherView.onPause(); - } - if (mIsPushing && mLivePusher != null) { - // 如果当前已经是隐私模式,那么则不pause - if (!mIsPrivateMode) { - mLivePusher.pausePusher(); - } - } - mIsResume = false; - if (mOnLivePusherCallback != null) { - mOnLivePusherCallback.onPushPause(); - } - } - - @Override - public void pausePush() { - mLivePusher.pausePusher(); - } - - @Override - public void destroy() { - stopPush(); - mPusherView.onDestroy(); - unInitPhoneListener(); - } - - @Override - public void switchCamera() { - mFrontCamera = !mFrontCamera; - mLivePusher.switchCamera(); - } - - @Override - public TXLivePusher getTXLivePusher() { - return mLivePusher; - } - - @Override - public boolean isPushing() { - return mIsPushing; - } - - @Override - public void setHomeOrientation(boolean isLandscape) { - mIsLandscape = isLandscape; - int renderRotation; - if (isLandscape) { // 横屏 - mLivePushConfig.setHomeOrientation(TXLiveConstants.VIDEO_ANGLE_HOME_RIGHT); - renderRotation = 90; // 因为采集旋转了,那么保证本地渲染是正的,则设置渲染角度为90度。 - } else { // 竖屏 - mLivePushConfig.setHomeOrientation(TXLiveConstants.VIDEO_ANGLE_HOME_DOWN); - renderRotation = 0; - } - if (mLivePusher.isPushing()) { - mLivePusher.setConfig(mLivePushConfig); - mLivePusher.setRenderRotation(renderRotation); - } - } - - @Override - public void setPrivateMode(boolean enable) { - mIsPrivateMode = enable; - // 隐私模式下,会进入垫片推流 - if (mIsPushing) { - if (enable) { - mLivePusher.pausePusher(); - } else { - mLivePusher.resumePusher(); - } - } - } - - @Override - public void setMute(boolean enable) { - mIsMuteAudio = enable; - mLivePusher.setMute(enable); - } - - @Override - public void setMirror(boolean enable) { - mIsMirrorEnable = enable; - mLivePusher.setMirror(enable); - } - - @Override - public void turnOnFlashLight(boolean enable) { - mIsFlashLight = enable; - mLivePusher.turnOnFlashLight(enable); - } - - @Override - public void showLog(boolean enable) { - mIsDebugInfo = enable; - mPusherView.showLog(enable); - } - - @Override - public void setWatermark(boolean enable) { - mIsWaterMarkEnable = enable; - if (enable) { - mLivePushConfig.setWatermark(mWaterMarkBitmap, 0.02f, 0.05f, 0.2f); - } else { - mLivePushConfig.setWatermark(null, 0, 0, 0); - } - if (mLivePusher.isPushing()) { - // 水印变更不需要重启推流,直接应用配置项即可 - mLivePusher.setConfig(mLivePushConfig); - } - } - - @Override - public void setTouchFocus(boolean enable) { - mIsFocusEnable = enable; - mLivePushConfig.setTouchFocus(enable); - if (mLivePusher.isPushing()) { - stopPush(); - startPush(); - } - } - - @Override - public void setEnableZoom(boolean enable) { - mIsZoomEnable = enable; - mLivePushConfig.setEnableZoom(enable); - if (mLivePusher.isPushing()) { - stopPush(); - startPush(); - } - } - - @Override - public void snapshot() { - mLivePusher.snapshot(new TXLivePusher.ITXSnapshotListener() { - @Override - public void onSnapshot(Bitmap bitmap) { - saveSnapshotBitmap(bitmap); - } - }); - } - - @Override - public void sendMessage(String msg) { - mLivePusher.sendMessage(msg.getBytes()); - } - - @Override - public void setHardwareAcceleration(boolean enable) { - mIsHWAcc = enable; - if (enable) { - mLivePushConfig.setHardwareAcceleration(TXLiveConstants.ENCODE_VIDEO_HARDWARE); // 启动硬编 - } else { - mLivePushConfig.setHardwareAcceleration(TXLiveConstants.ENCODE_VIDEO_SOFTWARE); // 启动软编 - } - if (mLivePusher.isPushing()) { - // 硬件加速变更不需要重启推流,直接应用配置项即可 - mLivePusher.setConfig(mLivePushConfig); - } - } - - @Override - public void setAdjustBitrate(boolean enable, int qualityType) { - mIsEnableAdjustBitrate = enable; - setPushScene(qualityType, enable); - } - - @Override - public void setQuality(boolean enable, int type) { - setPushScene(type, enable); - } - - @Override - public void enableAudioEarMonitoring(boolean enable) { - mIsEarMonitoringEnable = enable; - if (mLivePusher != null) { - TXLivePushConfig config = mLivePusher.getConfig(); - config.enableAudioEarMonitoring(enable); - mLivePusher.setConfig(config); - } - } - - @Override - public void enablePureAudioPush(boolean enable) { - mIsPureAudio = enable; - } - - @Override - public void setAudioQuality(int channel, int sampleRate) { - mAudioChannels = channel; - mAudioSample = sampleRate; - if (mLivePusher != null) { - TXLivePushConfig config = mLivePusher.getConfig(); - config.setAudioChannels(channel); - config.setAudioSampleRate(sampleRate); - mLivePusher.setConfig(config); - } - } - - private void initialize(Context context, TXCloudVideoView pusherView) { - mContext = context; - mLivePusher = new TXLivePusher(context); - mPusherView = pusherView; - mLivePushConfig = new TXLivePushConfig(); - mLivePushConfig.setVideoEncodeGop(5); - mLivePusher.setConfig(mLivePushConfig); - // 设置美颜 - mLivePusher.setBeautyFilter(mBeautyStyle, mBeautyLevel, mWhiteningLevel, mRuddyLevel); - mWaterMarkBitmap = decodeResource(mContext.getResources(), R.drawable.livepusher_watermark); - initListener(); - } - - @Override - public void onPushEvent(int event, Bundle param) { - String msg = param.getString(TXLiveConstants.EVT_DESCRIPTION); - String pushEventLog = getString(R.string.livepusher_receive_event) + event + ", " + msg; - Log.d(TAG, pushEventLog); - - // 如果开始推流,设置了隐私模式。 需要在回调里面设置,不能直接start之后直接pause - if (event == TXLiveConstants.PUSH_EVT_PUSH_BEGIN) { - if (mIsPrivateMode) { - pausePush(); - } - } - if (event == TXLiveConstants.PUSH_ERR_NET_DISCONNECT - || event == TXLiveConstants.PUSH_ERR_INVALID_ADDRESS - || event == TXLiveConstants.PUSH_ERR_OPEN_CAMERA_FAIL - || event == TXLiveConstants.PUSH_ERR_OPEN_MIC_FAIL) { - // 遇到以上错误,则停止推流 - stopPush(); - } else if (event == TXLiveConstants.PUSH_WARNING_HW_ACCELERATION_FAIL) { - // 开启硬件加速失败 - mLivePushConfig.setHardwareAcceleration(TXLiveConstants.ENCODE_VIDEO_SOFTWARE); - mLivePusher.setConfig(mLivePushConfig); - } else if (event == TXLiveConstants.PUSH_EVT_CHANGE_RESOLUTION) { - Log.d(TAG, "change resolution to " + param.getInt(TXLiveConstants.EVT_PARAM2) + ", bitrate to" + param.getInt(TXLiveConstants.EVT_PARAM1)); - } else if (event == TXLiveConstants.PUSH_EVT_CHANGE_BITRATE) { - Log.d(TAG, "change bitrate to" + param.getInt(TXLiveConstants.EVT_PARAM1)); - } else if (event == TXLiveConstants.PUSH_EVT_OPEN_CAMERA_SUCC) { - // 只有后置摄像头可以打开闪光灯,若默认需要开启闪光灯。 那么在打开摄像头成功后,才可以进行配置。 若果当前是前置,设定无效;若是后置,打开闪光灯。 - turnOnFlashLight(mIsFlashLight); - } - - if (mOnLivePusherCallback != null) { - mOnLivePusherCallback.onPushEvent(event, param); - } - } - - @Override - public void onNetStatus(Bundle bundle) { - if (mOnLivePusherCallback != null) { - mOnLivePusherCallback.onNetStatus(bundle); - } - } - - /** - * 根据当前 Activity 的旋转方向,配置推流器 - */ - @Override - public void setRotationForActivity() { - // 自动旋转打开,Activity随手机方向旋转之后,需要改变推流方向 - int mobileRotation = ((Activity) mContext).getWindowManager().getDefaultDisplay().getRotation(); - int pushRotation = TXLiveConstants.VIDEO_ANGLE_HOME_DOWN; - switch (mobileRotation) { - case Surface.ROTATION_0: - pushRotation = TXLiveConstants.VIDEO_ANGLE_HOME_DOWN; - break; - case Surface.ROTATION_180: - pushRotation = TXLiveConstants.VIDEO_ANGLE_HOME_UP; - break; - case Surface.ROTATION_90: - pushRotation = TXLiveConstants.VIDEO_ANGLE_HOME_RIGHT; - break; - case Surface.ROTATION_270: - pushRotation = TXLiveConstants.VIDEO_ANGLE_HOME_LEFT; - break; - default: - break; - } - - mLivePusher.setRenderRotation(0); // 因为activity也旋转了,本地渲染相对正方向的角度为0。 - mLivePushConfig.setHomeOrientation(pushRotation); // 根据Activity方向,设置采集角度 - - if (mLivePusher.isPushing()) { // 当前正在推流, - mLivePusher.setConfig(mLivePushConfig); - if (!mIsPrivateMode) { // 不是隐私模式,则开启摄像头推流。 - mLivePusher.stopCameraPreview(true); - mLivePusher.startCameraPreview(mPusherView); - } - } - } - - @Override - public void setOnLivePusherCallback(OnLivePusherCallback callback) { - mOnLivePusherCallback = callback; - } - - /** - * 设置推流场景 - *

- * SDK 内部将根据具体场景,进行推流 分辨率、码率、FPS、是否启动硬件加速、是否启动回声消除 等进行配置 - *

- * 适用于一般客户,方便快速进行配置 - *

- * 专业客户,推荐通过 {@link TXLivePushConfig} 进行逐一配置 - */ - private void setPushScene(int type, boolean enableAdjustBitrate) { - Log.i(TAG, "setPushScene: type = " + type + " enableAdjustBitrate = " + enableAdjustBitrate); - mQualityType = type; - mIsEnableAdjustBitrate = enableAdjustBitrate; - // 码率、分辨率自适应都关闭 - boolean autoResolution = false; - switch (type) { - case TXLiveConstants.VIDEO_QUALITY_STANDARD_DEFINITION: /*360p*/ - if (mLivePusher != null) { - mLivePusher.setVideoQuality(TXLiveConstants.VIDEO_QUALITY_STANDARD_DEFINITION, enableAdjustBitrate, autoResolution); - mVideoResolution = TXLiveConstants.VIDEO_RESOLUTION_TYPE_360_640; - } - break; - case TXLiveConstants.VIDEO_QUALITY_HIGH_DEFINITION: /*540p*/ - if (mLivePusher != null) { - mLivePusher.setVideoQuality(TXLiveConstants.VIDEO_QUALITY_HIGH_DEFINITION, enableAdjustBitrate, autoResolution); - mVideoResolution = TXLiveConstants.VIDEO_RESOLUTION_TYPE_540_960; - } - break; - case TXLiveConstants.VIDEO_QUALITY_SUPER_DEFINITION: /*720p*/ - if (mLivePusher != null) { - mLivePusher.setVideoQuality(TXLiveConstants.VIDEO_QUALITY_SUPER_DEFINITION, enableAdjustBitrate, autoResolution); - mVideoResolution = TXLiveConstants.VIDEO_RESOLUTION_TYPE_720_1280; - } - break; - case TXLiveConstants.VIDEO_QUALITY_ULTRA_DEFINITION: /*1080p*/ - if (mLivePusher != null) { - mLivePusher.setVideoQuality(TXLiveConstants.VIDEO_QUALITY_ULTRA_DEFINITION, enableAdjustBitrate, autoResolution); - mVideoResolution = TXLiveConstants.VIDEO_RESOLUTION_TYPE_1080_1920; - } - break; - case TXLiveConstants.VIDEO_QUALITY_LINKMIC_MAIN_PUBLISHER: /*连麦大主播*/ - if (mLivePusher != null) { - mLivePusher.setVideoQuality(TXLiveConstants.VIDEO_QUALITY_LINKMIC_MAIN_PUBLISHER, enableAdjustBitrate, autoResolution); - mVideoResolution = TXLiveConstants.VIDEO_RESOLUTION_TYPE_540_960; - } - break; - case TXLiveConstants.VIDEO_QUALITY_LINKMIC_SUB_PUBLISHER: /*连麦小主播*/ - if (mLivePusher != null) { - mLivePusher.setVideoQuality(TXLiveConstants.VIDEO_QUALITY_LINKMIC_SUB_PUBLISHER, enableAdjustBitrate, autoResolution); - mVideoResolution = TXLiveConstants.VIDEO_RESOLUTION_TYPE_320_480; - } - break; - case TXLiveConstants.VIDEO_QUALITY_REALTIEM_VIDEOCHAT: /*实时*/ - if (mLivePusher != null) { - mLivePusher.setVideoQuality(TXLiveConstants.VIDEO_QUALITY_REALTIEM_VIDEOCHAT, enableAdjustBitrate, autoResolution); - mVideoResolution = TXLiveConstants.VIDEO_RESOLUTION_TYPE_360_640; - } - break; - default: - break; - } - // 设置场景化配置后,SDK 内部会根据场景自动选择相关的配置参数,所以我们这里把内部的config获取出来,赋值到外部。 - mLivePushConfig = mLivePusher.getConfig(); - - // 是否开启硬件加速 - if (mIsHWAcc) { - mLivePushConfig.setHardwareAcceleration(TXLiveConstants.ENCODE_VIDEO_HARDWARE); - mLivePusher.setConfig(mLivePushConfig); - } - } - - private void runOnUiThread(Runnable runnable) { - ((Activity) mContext).runOnUiThread(runnable); - } - - private TXPhoneStateListener mPhoneListener; - private ActivityRotationObserver mActivityRotationObserver; - - /** - * 初始化电话监听、系统是否打开旋转监听 - */ - private void initListener() { - mPhoneListener = new TXPhoneStateListener(); - TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Service.TELEPHONY_SERVICE); - tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); - mActivityRotationObserver = new ActivityRotationObserver(new Handler(Looper.getMainLooper())); - mActivityRotationObserver.startObserver(); - } - - /** - * 销毁 - */ - private void unInitPhoneListener() { - TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Service.TELEPHONY_SERVICE); - tm.listen(mPhoneListener, PhoneStateListener.LISTEN_NONE); - mActivityRotationObserver.stopObserver(); - } - - /** - * 判断系统 "自动旋转" 设置功能是否打开 - * - * @return false---Activity可根据重力感应自动旋转 - */ - private boolean isActivityCanRotation(Context context) { - int flag = Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0); - return flag != 0; - } - - /** - * 获取资源图片 - * - * @param resources - * @param id - * @return - */ - private Bitmap decodeResource(Resources resources, int id) { - TypedValue value = new TypedValue(); - resources.openRawResource(id, value); - BitmapFactory.Options opts = new BitmapFactory.Options(); - opts.inTargetDensity = value.density; - return BitmapFactory.decodeResource(resources, id, opts); - } - - /** - * 保存并分享图片 - * - * @param bmp - */ - private void saveSnapshotBitmap(final Bitmap bmp) { - if (bmp == null) { - if (mOnLivePusherCallback != null) { - mOnLivePusherCallback.onSnapshot(null); - } - return; - } - AsyncTask.execute(new Runnable() { - @Override - public void run() { - String bitmapFileName = UUID.randomUUID().toString();//通过UUID生成字符串文件名 - FileOutputStream out = null; - File sdcardDir = mContext.getExternalFilesDir(null); - if (sdcardDir == null) { - Log.e(TAG, "sdcardDir is null"); - return; - } - final String path = sdcardDir + File.separator + bitmapFileName + ".png"; - final File file = new File(path); - try { - file.getParentFile().mkdirs(); - if (!file.exists()) { - file.createNewFile(); - } - out = new FileOutputStream(file); - bmp.compress(Bitmap.CompressFormat.PNG, 100, out); - } catch (IOException e) { - e.printStackTrace(); - } finally { - try { - if (out != null) { - out.flush(); - out.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - runOnUiThread(new Runnable() { - @Override - public void run() { - if (mOnLivePusherCallback != null) { - mOnLivePusherCallback.onSnapshot(file); - } - } - }); - } - }); - } - - /** - * 电话监听 - */ - private class TXPhoneStateListener extends PhoneStateListener { - - @Override - public void onCallStateChanged(int state, String incomingNumber) { - super.onCallStateChanged(state, incomingNumber); - Log.i(TAG, "onCallStateChanged: state -> " + state); - switch (state) { - case TelephonyManager.CALL_STATE_RINGING: //电话等待接听 - case TelephonyManager.CALL_STATE_OFFHOOK: //电话接听 - pause(); - break; - case TelephonyManager.CALL_STATE_IDLE: //电话挂机 - resume(); - break; - } - } - } - - /** - * 观察屏幕旋转设置变化 - */ - private class ActivityRotationObserver extends ContentObserver { - ContentResolver mResolver; - - public ActivityRotationObserver(Handler handler) { - super(handler); - mResolver = mContext.getContentResolver(); - } - - //屏幕旋转设置改变时调用 - @Override - public void onChange(boolean selfChange) { - super.onChange(selfChange); - if (isActivityCanRotation(mContext)) { - setRotationForActivity(); - } else { - // 恢复到正方向 - mLivePushConfig.setHomeOrientation(TXLiveConstants.VIDEO_ANGLE_HOME_DOWN); - // 恢复渲染角度 - mLivePusher.setRenderRotation(0); - if (mLivePusher.isPushing()) { - mLivePusher.setConfig(mLivePushConfig); - } - } - if (mOnLivePusherCallback != null) { - mOnLivePusherCallback.onActivityRotationObserverChange(selfChange); - } - } - - public void startObserver() { - mResolver.registerContentObserver(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), false, this); - } - - public void stopObserver() { - mResolver.unregisterContentObserver(this); - } - } - - public class PhoneStateReceiver extends BroadcastReceiver { - - @Override - public void onReceive(Context context, Intent intent) { - String action = intent.getAction(); - Log.d(TAG, "PhoneStateReceiver action: " + action); - } - } -} diff --git a/Android/Demo/livepusherdemo/src/main/java/com/tencent/liteav/demo/livepusher/camerapush/model/Constants.java b/Android/Demo/livepusherdemo/src/main/java/com/tencent/liteav/demo/livepusher/camerapush/model/Constants.java deleted file mode 100644 index 99b0bc1f..00000000 --- a/Android/Demo/livepusherdemo/src/main/java/com/tencent/liteav/demo/livepusher/camerapush/model/Constants.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.tencent.liteav.demo.livepusher.camerapush.model; - -public class Constants { - - public static final String URL_PRODUCT_DOCUMENT = "https://cloud.tencent.com/document/product/454/7885"; // 产品介绍链接 - - /** - * QRCodeScanActivity完成扫描后,传递过来的结果的KEY - */ - public static final String INTENT_SCAN_RESULT = "SCAN_RESULT"; - - /** - * LivePlayerURLActivity设置页面传递给LivePlayerActivity的直播地址 - */ - public static final String INTENT_URL_PUSH = "intent_url_push"; - public static final String INTENT_URL_PLAY_RTMP = "intent_url_play_rtmp"; - public static final String INTENT_URL_PLAY_FLV = "intent_url_play_flv"; - public static final String INTENT_URL_PLAY_HLS = "intent_url_play_hls"; - public static final String INTENT_URL_PLAY_ACC = "intent_url_play_acc"; - - public static final String URL_PUSH = "url_push"; // RTMP 推流地址 - public static final String URL_PLAY_RTMP = "url_play_rtmp"; // RTMP 播放地址 - public static final String URL_PLAY_FLV = "url_play_flv"; // FLA 播放地址 - public static final String URL_PLAY_HLS = "url_play_hls"; // HLS 播放地址 - public static final String URL_PLAY_ACC = "url_play_acc"; // RTMP 加速流地址 - - public static final int PLAY_STATUS_SUCCESS = 0; - public static final int PLAY_STATUS_INVALID_URL = -1; - public static final int PLAY_STATUS_INVALID_PLAY_TYPE = -3; - public static final int PLAY_STATUS_INVALID_RTMP_URL = -4; - public static final int PLAY_STATUS_LICENSE_ERROR = -5; -}