From 9f673503c1ae0355d14fdb403d0541590ec80523 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Wed, 9 Oct 2019 20:16:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dm3u8=E3=80=81http=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E8=AE=B0=E5=BD=95=E9=87=8D=E5=A4=8D=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../arialyy/aria/http/HttpRecordAdapter.java | 7 +++++ .../com/arialyy/aria/m3u8/BaseM3U8Loader.java | 7 +++-- .../arialyy/aria/m3u8/M3U8RecordAdapter.java | 11 ++++++- .../aria/m3u8/live/M3U8LiveLoader.java | 9 +++--- .../arialyy/aria/m3u8/live/M3U8LiveUtil.java | 1 + .../core/common/AbsRecordHandlerAdapter.java | 3 ++ .../aria/core/common/RecordHandler.java | 1 + .../aria/core/inf/IRecordHandlerAdapter.java | 5 ++++ .../com/arialyy/aria/util/RecordUtil.java | 29 +++++++++++++------ .../download/m3u8/M3U8LiveDLoadActivity.java | 13 ++++++++- 10 files changed, 68 insertions(+), 18 deletions(-) diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/HttpRecordAdapter.java b/HttpComponent/src/main/java/com/arialyy/aria/http/HttpRecordAdapter.java index 1955c1be..311ec0ed 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/HttpRecordAdapter.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/HttpRecordAdapter.java @@ -36,6 +36,13 @@ public class HttpRecordAdapter extends AbsRecordHandlerAdapter { super(wrapper); } + @Override public void onPre() { + super.onPre(); + if (getWrapper().getRequestType() == ITaskWrapper.U_HTTP){ + RecordUtil.delTaskRecord(getEntity().getFilePath(), IRecordHandler.TYPE_UPLOAD); + } + } + @Override public void handlerTaskRecord(TaskRecord record) { RecordHelper helper = new RecordHelper(getWrapper(), record); if (record.isBlock) { diff --git a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/BaseM3U8Loader.java b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/BaseM3U8Loader.java index c6b17fbe..91cb85f6 100644 --- a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/BaseM3U8Loader.java +++ b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/BaseM3U8Loader.java @@ -15,6 +15,7 @@ */ package com.arialyy.aria.m3u8; +import com.arialyy.aria.core.common.RecordHandler; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.M3U8Entity; @@ -127,8 +128,10 @@ public abstract class BaseM3U8Loader extends AbsLoader { } @Override protected IRecordHandler getRecordHandler(AbsTaskWrapper wrapper) { - - return null; + RecordHandler handler = new RecordHandler(wrapper); + M3U8RecordAdapter adapter = new M3U8RecordAdapter((DTaskWrapper) wrapper); + handler.setAdapter(adapter); + return handler; } protected DownloadEntity getEntity() { diff --git a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8RecordAdapter.java b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8RecordAdapter.java index 06193d55..25de650f 100644 --- a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8RecordAdapter.java +++ b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8RecordAdapter.java @@ -21,8 +21,10 @@ import com.arialyy.aria.core.common.AbsRecordHandlerAdapter; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.M3U8Entity; +import com.arialyy.aria.core.inf.IRecordHandler; import com.arialyy.aria.core.wrapper.ITaskWrapper; import com.arialyy.aria.util.ALog; +import com.arialyy.aria.util.RecordUtil; import java.io.File; import java.util.ArrayList; @@ -33,11 +35,18 @@ import java.util.ArrayList; public class M3U8RecordAdapter extends AbsRecordHandlerAdapter { private M3U8TaskOption mOption; - public M3U8RecordAdapter(DTaskWrapper wrapper) { + M3U8RecordAdapter(DTaskWrapper wrapper) { super(wrapper); mOption = (M3U8TaskOption) wrapper.getM3u8Option(); } + @Override public void onPre() { + super.onPre(); + if (getWrapper().getRequestType() == ITaskWrapper.M3U8_LIVE){ + RecordUtil.delTaskRecord(getEntity().getFilePath(), IRecordHandler.TYPE_DOWNLOAD); + } + } + /** * 不处理live的记录 */ diff --git a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveLoader.java b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveLoader.java index b7433ca3..e7d7928d 100644 --- a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveLoader.java +++ b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveLoader.java @@ -52,19 +52,18 @@ public class M3U8LiveLoader extends BaseM3U8Loader { private static final int EXEC_MAX_NUM = 4; private Handler mStateHandler; private ArrayBlockingQueue mFlagQueue = new ArrayBlockingQueue<>(EXEC_MAX_NUM); - private LiveStateManager mManager; private ReentrantLock LOCK = new ReentrantLock(); private Condition mCondition = LOCK.newCondition(); private LinkedBlockingQueue mPeerQueue = new LinkedBlockingQueue<>(); - public M3U8LiveLoader(M3U8Listener listener, DTaskWrapper wrapper) { + M3U8LiveLoader(M3U8Listener listener, DTaskWrapper wrapper) { super(listener, wrapper); } @Override protected IThreadState createStateManager(Looper looper) { - mManager = new LiveStateManager(looper, mListener); - mStateHandler = new Handler(looper, mManager); - return mManager; + LiveStateManager manager = new LiveStateManager(looper, mListener); + mStateHandler = new Handler(looper, manager); + return manager; } void offerPeer(String peerUrl) { diff --git a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveUtil.java b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveUtil.java index 04346744..3620fc03 100644 --- a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveUtil.java +++ b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveUtil.java @@ -154,6 +154,7 @@ public class M3U8LiveUtil extends AbsNormalLoaderUtil { mInfoPool.execute(mInfoThread); } }, 0, mM3U8Option.getLiveUpdateInterval(), TimeUnit.MILLISECONDS); + getLoader().start(); } private void closeTimer() { diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsRecordHandlerAdapter.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsRecordHandlerAdapter.java index 139a5633..ee74b0e4 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsRecordHandlerAdapter.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsRecordHandlerAdapter.java @@ -27,6 +27,9 @@ public abstract class AbsRecordHandlerAdapter implements IRecordHandlerAdapter { private AbsTaskWrapper mWrapper; protected String TAG = CommonUtil.getClassName(getClass()); + @Override public void onPre() { + } + public AbsRecordHandlerAdapter(AbsTaskWrapper wrapper) { mWrapper = wrapper; } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHandler.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHandler.java index 367f1f71..ff15fc02 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHandler.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHandler.java @@ -70,6 +70,7 @@ public class RecordHandler implements IRecordHandler { if (mConfigFile.exists()) { convertDb(); } else { + mAdapter.onPre(); mTaskRecord = DbDataHelper.getTaskRecord(getFilePath()); if (mTaskRecord == null) { initRecord(true); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IRecordHandlerAdapter.java b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IRecordHandlerAdapter.java index 85e75f1b..e2a2992b 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IRecordHandlerAdapter.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IRecordHandlerAdapter.java @@ -26,6 +26,11 @@ import com.arialyy.aria.core.ThreadRecord; */ public interface IRecordHandlerAdapter { + /** + * 记录处理前的操作,可用来删除任务记录 + */ + void onPre(); + /** * 处理任务记录 */ diff --git a/PublicComponent/src/main/java/com/arialyy/aria/util/RecordUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/util/RecordUtil.java index ce8a9b58..a76cf20d 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/util/RecordUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/util/RecordUtil.java @@ -16,16 +16,16 @@ package com.arialyy.aria.util; import android.text.TextUtils; -import com.arialyy.aria.core.inf.IRecordHandler; -import com.arialyy.aria.core.wrapper.RecordWrapper; import com.arialyy.aria.core.TaskRecord; import com.arialyy.aria.core.ThreadRecord; +import com.arialyy.aria.core.common.AbsEntity; +import com.arialyy.aria.core.common.AbsNormalEntity; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.download.M3U8Entity; -import com.arialyy.aria.core.common.AbsEntity; -import com.arialyy.aria.core.common.AbsNormalEntity; +import com.arialyy.aria.core.inf.IRecordHandler; import com.arialyy.aria.core.upload.UploadEntity; +import com.arialyy.aria.core.wrapper.RecordWrapper; import com.arialyy.aria.orm.DbEntity; import java.io.File; import java.util.List; @@ -148,13 +148,13 @@ public class RecordUtil { * 处理任务未完成的情况 */ if (!entity.isComplete()) { - if (record.taskType == TaskRecord.TYPE_M3U8_VOD) { // 删除ts分片文件 + if (recordIsM3U8(record.taskType)) { // 删除ts分片文件 removeTsCache(targetFile, record.bandWidth); } else if (record.isBlock) { // 删除分块文件 removeBlockFile(record); } } else if (removeFile) { // 处理任务完成情况 - if (record.taskType == TaskRecord.TYPE_M3U8_VOD) { + if (recordIsM3U8(record.taskType)) { removeTsCache(targetFile, record.bandWidth); } removeTargetFile(targetFile); @@ -164,6 +164,16 @@ public class RecordUtil { removeRecord(filePath); } + /** + * 任务记录是否是m3u8记录 + * + * @param recordType 任务记录类型 + * @return true 为m3u8任务 + */ + private static boolean recordIsM3U8(int recordType) { + return recordType == TaskRecord.TYPE_M3U8_VOD || recordType == TaskRecord.TYPE_M3U8_LIVE; + } + /** * 删除任务记录,默认删除文件 * @@ -208,13 +218,13 @@ public class RecordUtil { * 处理任务未完成的情况 */ if (!entity.isComplete()) { - if (record.taskType == TaskRecord.TYPE_M3U8_VOD) { // 删除ts分片文件 + if (recordIsM3U8(record.taskType)) { // 删除ts分片文件 removeTsCache(targetFile, record.bandWidth); } else if (record.isBlock) { // 删除分块文件 removeBlockFile(record); } } else if (removeFile) { // 处理任务完成情况 - if (record.taskType == TaskRecord.TYPE_M3U8_VOD) { + if (recordIsM3U8(record.taskType)) { removeTsCache(targetFile, record.bandWidth); } removeTargetFile(targetFile); @@ -357,7 +367,8 @@ public class RecordUtil { tr.taskKey = newPath; File blockFile = new File(String.format(IRecordHandler.SUB_PATH, oldPath, tr.threadId)); if (blockFile.exists()) { - blockFile.renameTo(new File(String.format(IRecordHandler.SUB_PATH, newPath, tr.threadId))); + blockFile.renameTo( + new File(String.format(IRecordHandler.SUB_PATH, newPath, tr.threadId))); } } DbEntity.updateManyData(record.threadRecords); diff --git a/app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8LiveDLoadActivity.java b/app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8LiveDLoadActivity.java index 69fcb184..0488f5bd 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8LiveDLoadActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8LiveDLoadActivity.java @@ -219,7 +219,18 @@ public class M3U8LiveDLoadActivity extends BaseActivity if (Aria.download(this).load(mEntity.getId()).isRunning()) { Aria.download(this).load(mEntity.getId()).stop(); } else { - Aria.download(this).load(mEntity.getId()).resume(); + Aria.download(this).load(mEntity.getId()) + .asM3U8() + .asLive() + .setLiveTsUrlConvert(new ILiveTsUrlConverter() { + @Override public String convert(String m3u8Url, String tsUrl) { + int index = m3u8Url.lastIndexOf("/"); + String parentUrl = m3u8Url.substring(0, index + 1); + return parentUrl + tsUrl; + } + }) + .controller(ControllerType.TASK_CONTROLLER) + .resume(); } break; case R.id.cancel: