Skip to content

Commit

Permalink
优化直播
Browse files Browse the repository at this point in the history
  • Loading branch information
q215613905 committed Feb 15, 2025
1 parent a2bf235 commit 758e068
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {
defaultConfig {
applicationId 'com.github.tvbox.osc'
minSdkVersion 16
targetSdkVersion 26
targetSdkVersion 28
versionCode 1
versionName '1.0.0'
multiDexEnabled true
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/github/tvbox/osc/api/ApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,11 @@ private void parseJson(String apiUrl, String jsonStr) {
String epg =livesOBJ.get("epg").getAsString();
Hawk.put(HawkConfig.EPG_URL,epg);
}
//直播播放器类型
if(livesOBJ.has("playerType")){
String livePlayType =livesOBJ.get("playerType").getAsString();
Hawk.put(HawkConfig.LIVE_PLAY_TYPE,livePlayType);
}

LiveChannelGroup liveChannelGroup = new LiveChannelGroup();
liveChannelGroup.setGroupName(url);
Expand All @@ -422,6 +427,11 @@ private void parseJson(String apiUrl, String jsonStr) {
String epg =fengMiLives.get("epg").getAsString();
Hawk.put(HawkConfig.EPG_URL,epg);
}
//直播播放器类型
if(livesOBJ.has("playerType")){
String livePlayType =livesOBJ.get("playerType").getAsString();
Hawk.put(HawkConfig.LIVE_PLAY_TYPE,livePlayType);
}

if(url.startsWith("http")){
url = Base64.encodeToString(url.getBytes("UTF-8"), Base64.DEFAULT | Base64.URL_SAFE | Base64.NO_WRAP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class LivePlayerManager {

public void init(VideoView videoView) {
try {
defaultPlayerConfig.put("pl", Hawk.get(HawkConfig.PLAY_TYPE, 0));
defaultPlayerConfig.put("pl", Hawk.get(HawkConfig.LIVE_PLAY_TYPE, 1));
defaultPlayerConfig.put("ijk", Hawk.get(HawkConfig.IJK_CODEC, "硬解码"));
defaultPlayerConfig.put("pr", Hawk.get(HawkConfig.PLAY_RENDER, 0));
defaultPlayerConfig.put("sc", Hawk.get(HawkConfig.PLAY_SCALE, 0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class HawkConfig {
public static final String PARSE_WEBVIEW = "parse_webview"; // true 系统 false xwalk
public static final String IJK_CODEC = "ijk_codec";
public static final String PLAY_TYPE = "play_type";//0 系统 1 ijk 2 exo 10 MXPlayer
public static final String LIVE_PLAY_TYPE = "live_play_type";//0 系统 1 ijk 2 exo 10 MXPlayer
public static final String PLAY_RENDER = "play_render"; //0 texture 2
public static final String PLAY_SCALE = "play_scale"; //0 texture 2
public static final String PLAY_TIME_STEP = "play_time_step"; //0 texture 2
Expand Down
3 changes: 2 additions & 1 deletion player/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {

defaultConfig {
minSdkVersion 16
targetSdkVersion 26
targetSdkVersion 28
}

buildTypes {
Expand All @@ -34,6 +34,7 @@ android {
dependencies {
implementation "androidx.annotation:annotation:1.3.0"
api "com.google.android.exoplayer:exoplayer-core:2.14.2"
api "com.google.android.exoplayer:exoplayer-ui:2.14.2"
api "com.google.android.exoplayer:exoplayer-dash:2.14.2"
api "com.google.android.exoplayer:exoplayer-hls:2.14.2"
api "com.google.android.exoplayer:exoplayer-rtsp:2.14.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.view.Surface;
import android.view.SurfaceHolder;

import androidx.annotation.NonNull;

import com.google.android.exoplayer2.DefaultLoadControl;
import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.ExoPlaybackException;
Expand All @@ -24,8 +26,13 @@
import com.google.android.exoplayer2.util.EventLogger;
import com.google.android.exoplayer2.video.VideoSize;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;

import okhttp3.Dns;
import okhttp3.OkHttpClient;
import xyz.doikki.videoplayer.player.AbstractPlayer;
import xyz.doikki.videoplayer.player.VideoViewManager;
import xyz.doikki.videoplayer.util.PlayerUtils;
Expand All @@ -47,14 +54,32 @@ public class ExoMediaPlayer extends AbstractPlayer implements Player.Listener {

public ExoMediaPlayer(Context context) {
mAppContext = context.getApplicationContext();
// 初始化 ExoMediaSourceHelper 实例
mMediaSourceHelper = ExoMediaSourceHelper.getInstance(context);
// 构造自定义的 OkHttpClient,加入自定义 DNS 逻辑:
// 当请求的域名以 "cache.ott" 开头时,使用 "base-v4-free-mghy.e.cdn.chinamobile.com" 解析
OkHttpClient customOkHttpClient = new OkHttpClient.Builder()
.dns(new Dns() {
@Override
public List<InetAddress> lookup(@NonNull String hostname) throws UnknownHostException {
if (hostname.matches("^cache\\.ott\\..*\\.cmvideo\\.cn$")) {
// 这里将 hostname 强制解析为目标域名
return Dns.SYSTEM.lookup("base-v4-free-mghy.e.cdn.chinamobile.com");
}
return Dns.SYSTEM.lookup(hostname);
}
})
.build();
// 注入自定义 OkHttpClient 到 ExoMediaSourceHelper 中
mMediaSourceHelper.setOkClient(customOkHttpClient);
}

@Override
public void initPlayer() {
mInternalPlayer = new SimpleExoPlayer.Builder(
mAppContext,
mRenderersFactory == null ? mRenderersFactory = new DefaultRenderersFactory(mAppContext) : mRenderersFactory,
mRenderersFactory == null ? mRenderersFactory = new DefaultRenderersFactory(mAppContext).setEnableDecoderFallback(true) // 启用解码器回退,避免硬件加速问题
.setExtensionRendererMode(DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER) : mRenderersFactory,
mTrackSelector == null ? mTrackSelector = new DefaultTrackSelector(mAppContext) : mTrackSelector,
new DefaultMediaSourceFactory(mAppContext),
mLoadControl == null ? mLoadControl = new DefaultLoadControl() : mLoadControl,
Expand All @@ -63,7 +88,7 @@ public void initPlayer() {
.build();
setOptions();

//播放器日志
// 播放器日志(当开启日志且 mTrackSelector 为 MappingTrackSelector 时)
if (VideoViewManager.getConfig().mIsEnableLog && mTrackSelector instanceof MappingTrackSelector) {
mInternalPlayer.addAnalyticsListener(new EventLogger((MappingTrackSelector) mTrackSelector, "ExoPlayer"));
}
Expand All @@ -90,7 +115,7 @@ public void setDataSource(String path, Map<String, String> headers) {

@Override
public void setDataSource(AssetFileDescriptor fd) {
//no support
// 不支持AssetFileDescriptor方式
}

@Override
Expand Down Expand Up @@ -167,7 +192,6 @@ public void release() {
mInternalPlayer.release();
mInternalPlayer = null;
}

mIsPreparing = false;
mSpeedPlaybackParameters = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Iterator;
import java.util.Map;

import okhttp3.Call;
import okhttp3.OkHttpClient;

public final class ExoMediaSourceHelper {
Expand Down Expand Up @@ -144,7 +145,7 @@ private DataSource.Factory getDataSourceFactory() {
*/
private DataSource.Factory getHttpDataSourceFactory() {
if (mHttpDataSourceFactory == null) {
mHttpDataSourceFactory = new OkHttpDataSource.Factory(mOkClient)
mHttpDataSourceFactory = new OkHttpDataSource.Factory((Call.Factory) mOkClient)
.setUserAgent(mUserAgent)/*
.setAllowCrossProtocolRedirects(true)*/;
}
Expand Down
2 changes: 1 addition & 1 deletion quickjs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {

defaultConfig {
minSdkVersion 16
targetSdkVersion 26
targetSdkVersion 28
externalNativeBuild {
cmake {
abiFilters 'armeabi-v7a'
Expand Down

0 comments on commit 758e068

Please sign in to comment.