Skip to content

Commit

Permalink
将【下载状态】和【保存多码率文件、单码率文件】从JDM3U8BaseDownloader以接口形式抽离出来,改为在开始下载时传入
Browse files Browse the repository at this point in the history
  • Loading branch information
chenguijian committed Sep 26, 2022
1 parent 18cfe02 commit 8294ba9
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 205 deletions.
4 changes: 2 additions & 2 deletions jdm3u8downloader/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
defaultConfig {
minSdkVersion 19
targetSdkVersion 29
versionCode 4
versionName "2.0.1"
versionCode 5
versionName "2.0.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.onedream.jdm3u8downloader.base.JDM3U8BaseDownloader;
import com.onedream.jdm3u8downloader.bean.JDDownloadQueue;
Expand All @@ -16,6 +17,8 @@
import com.onedream.jdm3u8downloader.file_downloader.JDM3U8FileAbstractDownloader;
import com.onedream.jdm3u8downloader.file_downloader.JDM3U8FileAbstractDownloaderFactory;
import com.onedream.jdm3u8downloader.file_downloader.imp.JDM3U8FileOriginalDownloaderFactory;
import com.onedream.jdm3u8downloader.listener.DownloadStateCallback;
import com.onedream.jdm3u8downloader.listener.FileSaveCallback;
import com.onedream.jdm3u8downloader.listener.JDM3U8DownloaderContract;
import com.onedream.jdm3u8downloader.utils.JDM3U8FileCacheUtils;
import com.onedream.jdm3u8downloader.utils.JDM3U8LogHelper;
Expand Down Expand Up @@ -145,122 +148,135 @@ public void getM3U8SingleRateFileContent(JDM3U8SingleRateUrlBean m3u8FileUrlBean
}

@Override
public JDM3U8TsBean getJDM3U8TsBean(JDM3U8SingleRateUrlBean m3U8SingleRateFileDownloadUrlBean, String tsFileUrl){
public List<String> getTsFileDownloadShortUrlList(String M3U8SingleRateFileContent) {
return jdm3U8SingleRateUrlBeanConvert.convertTsFileDownloadShortUrlList(M3U8SingleRateFileContent);
}

@Override
public JDM3U8TsBean getJDM3U8TsBean(JDM3U8SingleRateUrlBean m3U8SingleRateFileDownloadUrlBean, String tsFileUrl) {
return jdm3U8SingleRateUrlBeanConvert.convertM3U8TsBean(m3U8SingleRateFileDownloadUrlBean, tsFileUrl);
}

@Override
public void downloadM3U8Ts(List<String> tsUrlPathList, JDM3U8SingleRateUrlBean m3U8SingleRateFileDownloadUrlBean) {
public void downloadM3U8Ts(List<JDM3U8TsBean> tsUrlPathList, @Nullable DownloadStateCallback downloadStateCallback, JDM3U8DownloaderContract.JDM3U8DownloadFullSuccessListener jdm3U8DownloadFullSuccessListener) {
//
downloadQueue.setState(JDDownloadQueueState.STATE_DOWNLOAD_ING);
//
JDM3U8TsFileDownloadTask downloadTask = new JDM3U8TsFileDownloadTask();
downloadTask.tsUrlPathList = tsUrlPathList;
downloadTask.m3U8SingleRateFileDownloadUrlBean = m3U8SingleRateFileDownloadUrlBean;
downloadTask.m3U8BaseDownloader = this;
downloadTask.m3U8TsBeanList = tsUrlPathList;
downloadTask.tsFileDownloadCallback = tsFileDownloadCallback;
downloadTask.downloadStateCallback = downloadStateCallback;
downloadTask.jdm3U8DownloadFullSuccessListener = jdm3U8DownloadFullSuccessListener;
downloadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

@Override
public void saveLocalM3U8SingleRate(String oldString) {
//保存一份供本地播放的单码率m3u8文件
if (!TextUtils.isEmpty(oldString)) {
/**
* 创建一份供本地播放的m3u8
*/
File m3u8File = JDM3U8FileCacheUtils.getM3u8File(targetDir, downloadQueue.getMovie_id(), downloadQueue.getMovie_num_index());
File m3u8LocalFile = JDM3U8FileCacheUtils.getM3u8LocalFile(targetDir, downloadQueue.getMovie_id(), downloadQueue.getMovie_num_index());
alterStringToCreateNewFile(m3u8File, m3u8LocalFile, oldString, "");
JDM3U8FileCacheUtils.alterStringToCreateNewFile(m3u8File, m3u8LocalFile, oldString, "");
} else {
JDM3U8FileCacheUtils.saveM3U8LocalFile(targetDir, downloadQueue.getMovie_id(), downloadQueue.getMovie_num_index(), JDM3U8FileCacheUtils.getM3U8FileContent(targetDir, downloadQueue.getMovie_id(), downloadQueue.getMovie_num_index()));
}
}


@Override
public void saveM3U8MultiRateFile(String content) {
JDM3U8FileCacheUtils.saveM3U8TopFile(targetDir, downloadQueue.getMovie_id(), downloadQueue.getMovie_num_index(), content);
}
private final JDM3U8TsFileDownloadTask.TsFileDownloadCallback tsFileDownloadCallback = new JDM3U8TsFileDownloadTask.TsFileDownloadCallback() {
@Override
public boolean canDownloadTsFile() {
return downloadQueue.getState() != JDDownloadQueueState.STATE_DOWNLOAD_NO;
}

@Override
public void saveM3U8SingleRateFile(String content) {
JDM3U8FileCacheUtils.saveM3U8File(targetDir, downloadQueue.getMovie_id(), downloadQueue.getMovie_num_index(), content);
}
@Override
public int downLoadTsFile(JDM3U8TsBean m3U8TsBean) {
File tsSaveFile = JDM3U8FileCacheUtils.getTsFile(targetDir, downloadQueue.getMovie_id(), downloadQueue.getMovie_num_index(), m3U8TsBean.getTsFileName());
if (null != tsSaveFile) {
JDM3U8LogHelper.printLog("该电影" + downloadQueue.getMovie_id() + "的" + m3U8TsBean.getTsFileName() + "文件已经存在");
return JDM3U8TsDownloadState.DOWNLOAD_TS_FILE_SUCCESS;
} else {
tsSaveFile = JDM3U8FileCacheUtils.getTsFileAndIsEmptyNeedCreate(targetDir, downloadQueue.getMovie_id(), downloadQueue.getMovie_num_index(), m3U8TsBean.getTsFileName());
}
if (null == tsSaveFile) {
JDM3U8LogHelper.printLog("创建ts文件失败");
return JDM3U8TsDownloadState.DOWNLOAD_TS_FILE_FAILURE;
}
return abstractDownloader.downLoadTsFile(m3U8TsBean, tsSaveFile);
}

@Override
public long getTsFileSize(JDM3U8TsBean m3U8TsBean) {
File file = JDM3U8FileCacheUtils.getTsFileAndIsEmptyNeedCreate(targetDir, downloadQueue.getMovie_id(), downloadQueue.getMovie_num_index(), m3U8TsBean.getTsFileName());
if (null != file) {
return file.length();
}
return 0;
}
};

@Override
public boolean canDownloadTsFile() {
return downloadQueue.getState() != JDDownloadQueueState.STATE_DOWNLOAD_NO;
}

@Override
public int downLoadTsFile(JDM3U8TsBean m3U8TsBean) {
File tsSaveFile = JDM3U8FileCacheUtils.getTsFile(targetDir, downloadQueue.getMovie_id(), downloadQueue.getMovie_num_index(), m3U8TsBean.getTsFileName());
if (null != tsSaveFile) {
JDM3U8LogHelper.printLog("该电影" + downloadQueue.getMovie_id() + "的" + m3U8TsBean.getTsFileName() + "文件已经存在");
return JDM3U8TsDownloadState.DOWNLOAD_TS_FILE_SUCCESS;

public void startDownload() {
if (downloadQueue.isSingleRate()) {
super.startDownloadSingleRateM3U8(downloadQueue.getMovie_download_url(), downloadStateCallback, fileSaveCallback);
} else {
tsSaveFile = JDM3U8FileCacheUtils.getTsFileAndIsEmptyNeedCreate(targetDir, downloadQueue.getMovie_id(), downloadQueue.getMovie_num_index(), m3U8TsBean.getTsFileName());
}
if (null == tsSaveFile) {
JDM3U8LogHelper.printLog("创建ts文件失败");
return JDM3U8TsDownloadState.DOWNLOAD_TS_FILE_FAILURE;
super.startDownloadMultiRateM3U8(downloadQueue.getMovie_download_url(), downloadStateCallback, fileSaveCallback);
}
return abstractDownloader.downLoadTsFile(m3U8TsBean, tsSaveFile);
}

@Override
public void postDownloadProgress(int successCount, int tsFileCount, long itemLength) {
if (downloadQueue.getState() == JDDownloadQueueState.STATE_DOWNLOAD_ING || downloadQueue.getState() == JDDownloadQueueState.STATE_DOWNLOAD_FINISH) {
getM3U8FileListener.downloadProgress(downloadQueue, successCount * itemLength, tsFileCount * itemLength);


private final DownloadStateCallback downloadStateCallback = new DownloadStateCallback() {
@Override
public void postDownloadProgressEvent(int successCount, int tsFileCount, long itemLength) {
if (downloadQueue.getState() == JDDownloadQueueState.STATE_DOWNLOAD_ING || downloadQueue.getState() == JDDownloadQueueState.STATE_DOWNLOAD_FINISH) {
getM3U8FileListener.downloadProgress(downloadQueue, successCount * itemLength, tsFileCount * itemLength);
}
}
}

@Override
public void postDownloadErrorEvent(String errMsg) {
if (getM3U8FileListener instanceof JDM3U8DownloaderContract.JDM3U8DownloadListener) {
((JDM3U8DownloaderContract.JDM3U8DownloadListener) getM3U8FileListener).downloadError(downloadQueue, errMsg);
@Override
public void postDownloadErrorEvent(String errMsg) {
if (getM3U8FileListener instanceof JDM3U8DownloaderContract.JDM3U8DownloadListener) {
((JDM3U8DownloaderContract.JDM3U8DownloadListener) getM3U8FileListener).downloadError(downloadQueue, errMsg);
}
getM3U8FileListener.downloadState(downloadQueue, JDDownloadQueueState.STATE_DOWNLOAD_ERROR, errMsg);
}
getM3U8FileListener.downloadState(downloadQueue, JDDownloadQueueState.STATE_DOWNLOAD_ERROR, errMsg);
}

@Override
public void downloadSuccessEvent() {
if (getM3U8FileListener instanceof JDM3U8DownloaderContract.JDM3U8DownloadListener) {
((JDM3U8DownloaderContract.JDM3U8DownloadListener) getM3U8FileListener).downloadSuccess(downloadQueue);
@Override
public void postDownloadSuccessEvent() {
if (getM3U8FileListener instanceof JDM3U8DownloaderContract.JDM3U8DownloadListener) {
((JDM3U8DownloaderContract.JDM3U8DownloadListener) getM3U8FileListener).downloadSuccess(downloadQueue);
}
getM3U8FileListener.downloadState(downloadQueue, JDDownloadQueueState.STATE_DOWNLOAD_SUCCESS, JDDownloadQueueState.getSateStr(JDDownloadQueueState.STATE_DOWNLOAD_SUCCESS));
}
getM3U8FileListener.downloadState(downloadQueue, JDDownloadQueueState.STATE_DOWNLOAD_SUCCESS, JDDownloadQueueState.getSateStr(JDDownloadQueueState.STATE_DOWNLOAD_SUCCESS));
}

@Override
public void downloadFinish() {
getM3U8FileListener.downloadState(downloadQueue, JDDownloadQueueState.STATE_DOWNLOAD_FINISH, JDDownloadQueueState.getSateStr(JDDownloadQueueState.STATE_DOWNLOAD_FINISH));
}
@Override
public void postDownloadCloseEvent() {
getM3U8FileListener.downloadState(downloadQueue, JDDownloadQueueState.STATE_DOWNLOAD_FINISH, JDDownloadQueueState.getSateStr(JDDownloadQueueState.STATE_DOWNLOAD_FINISH));
}


@Override
public long getTsFileSize(JDM3U8TsBean m3U8TsBean) {
File file = JDM3U8FileCacheUtils.getTsFileAndIsEmptyNeedCreate(targetDir, downloadQueue.getMovie_id(), downloadQueue.getMovie_num_index(), m3U8TsBean.getTsFileName());
if (null != file) {
return file.length();
@Override
public void postDownloadPauseEvent() {
if (getM3U8FileListener instanceof JDM3U8DownloaderContract.JDM3U8DownloadListener) {
((JDM3U8DownloaderContract.JDM3U8DownloadListener) getM3U8FileListener).downloadPause(downloadQueue);
}
getM3U8FileListener.downloadState(downloadQueue, JDDownloadQueueState.STATE_DOWNLOAD_PAUSE, JDDownloadQueueState.getSateStr(JDDownloadQueueState.STATE_DOWNLOAD_PAUSE));
}
return 0;
}
};

@Override
public void pauseDownload() {
if (getM3U8FileListener instanceof JDM3U8DownloaderContract.JDM3U8DownloadListener) {
((JDM3U8DownloaderContract.JDM3U8DownloadListener) getM3U8FileListener).downloadPause(downloadQueue);

private final FileSaveCallback fileSaveCallback = new FileSaveCallback() {
@Override
public void saveM3U8MultiRateFile(String content) {
JDM3U8FileCacheUtils.saveM3U8TopFile(targetDir, downloadQueue.getMovie_id(), downloadQueue.getMovie_num_index(), content);
}
getM3U8FileListener.downloadState(downloadQueue, JDDownloadQueueState.STATE_DOWNLOAD_PAUSE, JDDownloadQueueState.getSateStr(JDDownloadQueueState.STATE_DOWNLOAD_PAUSE));
}

public void startDownload() {
if (downloadQueue.isSingleRate()) {
super.startDownloadSingleRateM3U8(downloadQueue.getMovie_download_url());
} else {
super.startDownloadMultiRateM3U8(downloadQueue.getMovie_download_url());
@Override
public void saveM3U8SingleRateFile(String content) {
JDM3U8FileCacheUtils.saveM3U8File(targetDir, downloadQueue.getMovie_id(), downloadQueue.getMovie_num_index(), content);
}
};

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import android.os.AsyncTask;

import com.onedream.jdm3u8downloader.base.JDM3U8BaseDownloader;
import com.onedream.jdm3u8downloader.bean.JDM3U8SingleRateUrlBean;
import androidx.annotation.Nullable;

import com.onedream.jdm3u8downloader.listener.DownloadStateCallback;
import com.onedream.jdm3u8downloader.bean.JDM3U8TsBean;
import com.onedream.jdm3u8downloader.common.JDM3U8TsDownloadState;
import com.onedream.jdm3u8downloader.listener.JDM3U8DownloaderContract;
import com.onedream.jdm3u8downloader.utils.JDM3U8LogHelper;

import java.util.List;
Expand All @@ -15,9 +17,11 @@
* @since 2020/4/3
*/
public class JDM3U8TsFileDownloadTask extends AsyncTask<Integer, Integer, Integer> {
public JDM3U8BaseDownloader m3U8BaseDownloader;
public JDM3U8SingleRateUrlBean m3U8SingleRateFileDownloadUrlBean;
public List<String> tsUrlPathList;
public List<JDM3U8TsBean> m3U8TsBeanList;
public TsFileDownloadCallback tsFileDownloadCallback;
public @Nullable DownloadStateCallback downloadStateCallback;
public JDM3U8DownloaderContract.JDM3U8DownloadFullSuccessListener jdm3U8DownloadFullSuccessListener;

//
private String oldString = "";

Expand All @@ -27,24 +31,25 @@ protected Integer doInBackground(Integer... params) {
int successCount = 0;
int currentTsNum = 1;//当前是第几个ts
long itemLength = 0;//每个item的大小
for (String tsFileUrl : tsUrlPathList) {
if (!m3U8BaseDownloader.canDownloadTsFile()) {//取消下载
for (JDM3U8TsBean m3U8TsBean : m3U8TsBeanList) {
if (!tsFileDownloadCallback.canDownloadTsFile()) {//取消下载
failureCount = -1;
break;
}
JDM3U8TsBean m3U8TsBean = m3U8BaseDownloader.getJDM3U8TsBean(m3U8SingleRateFileDownloadUrlBean, tsFileUrl);
oldString = m3U8TsBean.getOldString();
//3、下载ts文件
int ret = m3U8BaseDownloader.downLoadTsFile(m3U8TsBean);
int ret = tsFileDownloadCallback.downLoadTsFile(m3U8TsBean);
//
if (ret == JDM3U8TsDownloadState.DOWNLOAD_TS_FILE_SUCCESS) {
successCount++;
//由于每个ts文件的大小基本是固定的(头尾有点差距),可以通过单个文件的大小来算整个文件的大小
if (itemLength == 0 && currentTsNum > 1 && currentTsNum < tsUrlPathList.size()) {
itemLength = m3U8BaseDownloader.getTsFileSize(m3U8TsBean);
if (itemLength == 0 && currentTsNum > 1 && currentTsNum < m3U8TsBeanList.size()) {
itemLength = tsFileDownloadCallback.getTsFileSize(m3U8TsBean);
JDM3U8LogHelper.printLog("以第" + currentTsNum + "个的大小:" + itemLength + "计算总大小");
}
m3U8BaseDownloader.postDownloadProgress(successCount, tsUrlPathList.size(), itemLength);
if(null != downloadStateCallback){
downloadStateCallback.postDownloadProgressEvent(successCount, m3U8TsBeanList.size(), itemLength);
}
} else {
failureCount++;
}
Expand All @@ -60,15 +65,35 @@ protected Integer doInBackground(Integer... params) {
protected void onPostExecute(Integer failureCount) {
super.onPostExecute(failureCount);
if (failureCount > 0) {
m3U8BaseDownloader.postDownloadErrorEvent("失败下载次数" + failureCount);
if(null != downloadStateCallback) {
downloadStateCallback.postDownloadErrorEvent("失败下载次数" + failureCount);
}
} else if (failureCount == 0) {
m3U8BaseDownloader.downloadSuccessEvent();
if(null != downloadStateCallback) {
downloadStateCallback.postDownloadSuccessEvent();
}
//保存一份供本地播放的单码率m3u8文件
m3U8BaseDownloader.saveLocalM3U8SingleRate(oldString);
jdm3U8DownloadFullSuccessListener.downloadFullSuccessSaveLocalM3U8SingleRate(oldString);
} else if (failureCount == -1) {//手动暂停不处理
JDM3U8LogHelper.printLog("手动暂停");
m3U8BaseDownloader.pauseDownload();
if(null != downloadStateCallback) {
downloadStateCallback.postDownloadPauseEvent();
}
}
m3U8BaseDownloader.downloadFinish();
if(null != downloadStateCallback) {
downloadStateCallback.postDownloadCloseEvent();
}
}


public interface TsFileDownloadCallback {
//是否继续下载ts文件
boolean canDownloadTsFile();

//下载单个ts文件
int downLoadTsFile(JDM3U8TsBean m3U8TsBean);

//获取指定的ts文件大小
long getTsFileSize(JDM3U8TsBean m3U8TsBean);
}
}
Loading

0 comments on commit 8294ba9

Please sign in to comment.