fix bug https://github.com/AriaLyy/Aria/issues/508 fix bug https://github.com/AriaLyy/Aria/issues/503 修复m3u8创建索引不成功的问题
This commit is contained in:
@@ -68,8 +68,9 @@ public abstract class BaseM3U8Loader extends AbsLoader {
|
||||
/**
|
||||
* 创建索引文件
|
||||
*/
|
||||
protected boolean generateIndexFile() {
|
||||
File tempFile = new File(M3U8InfoThread.M3U8_INDEX_FORMAT, getEntity().getFilePath());
|
||||
public boolean generateIndexFile(boolean isLive) {
|
||||
File tempFile =
|
||||
new File(String.format(M3U8InfoThread.M3U8_INDEX_FORMAT, getEntity().getFilePath()));
|
||||
if (!tempFile.exists()) {
|
||||
ALog.e(TAG, "源索引文件不存在");
|
||||
return false;
|
||||
@@ -85,21 +86,29 @@ public abstract class BaseM3U8Loader extends AbsLoader {
|
||||
int i = 0;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
byte[] bytes;
|
||||
if (line.startsWith("EXTINF")) {
|
||||
if (line.startsWith("#EXTINF")) {
|
||||
fos.write(line.concat("\r\n").getBytes(Charset.forName("UTF-8")));
|
||||
String tsPath = getTsFilePath(cacheDir, mRecord.threadRecords.get(i).threadId);
|
||||
bytes = tsPath.concat("\r\n").getBytes(Charset.forName("UTF-8"));
|
||||
reader.readLine(); // 继续读一行,避免写入源索引文件的切片地址
|
||||
i++;
|
||||
} else if (line.startsWith("EXT-X-KEY")) {
|
||||
} else if (line.startsWith("#EXT-X-KEY")) {
|
||||
M3U8Entity m3U8Entity = getEntity().getM3U8Entity();
|
||||
String keyInfo = String.format("#EXT-X-KEY:METHOD=%s,URI=%s,IV=%s\r\n", m3U8Entity.method,
|
||||
m3U8Entity.keyPath, m3U8Entity.iv);
|
||||
bytes = keyInfo.getBytes(Charset.forName("UTF-8"));
|
||||
} else {
|
||||
bytes = line.getBytes(Charset.forName("UTF-8"));
|
||||
bytes = line.concat("\r\n").getBytes(Charset.forName("UTF-8"));
|
||||
}
|
||||
fos.write(bytes, 0, bytes.length);
|
||||
}
|
||||
// 直播的索引文件需要在结束的时候才写入结束标志
|
||||
if (isLive) {
|
||||
fos.write("#EXT-X-ENDLIST".concat("\r\n").getBytes(Charset.forName("UTF-8")));
|
||||
}
|
||||
|
||||
fos.flush();
|
||||
return true;
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -24,8 +24,8 @@ import com.arialyy.aria.core.common.CompleteInfo;
|
||||
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.processor.IBandWidthUrlConverter;
|
||||
import com.arialyy.aria.core.inf.OnFileInfoCallback;
|
||||
import com.arialyy.aria.core.processor.IBandWidthUrlConverter;
|
||||
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
|
||||
import com.arialyy.aria.core.wrapper.ITaskWrapper;
|
||||
import com.arialyy.aria.exception.M3U8Exception;
|
||||
@@ -39,7 +39,6 @@ import com.arialyy.aria.util.FileUtil;
|
||||
import com.arialyy.aria.util.Regular;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -55,6 +54,7 @@ import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 解析url中获取到到m3u8文件信息
|
||||
* 协议地址:https://tools.ietf.org/html/rfc8216
|
||||
* https://www.cnblogs.com/renhui/p/10351870.html
|
||||
* https://blog.csdn.net/Guofengpu/article/details/54922865
|
||||
*/
|
||||
@@ -72,13 +72,9 @@ final public class M3U8InfoThread implements Runnable {
|
||||
* 是否停止获取切片信息,{@code true}停止获取切片信息
|
||||
*/
|
||||
private boolean isStop = false;
|
||||
/**
|
||||
* m3u8文件信息
|
||||
*/
|
||||
private List<String> mInfos = new ArrayList<>();
|
||||
|
||||
public interface OnGetLivePeerCallback {
|
||||
void onGetPeer(String url);
|
||||
void onGetPeer(String url, String extInf);
|
||||
}
|
||||
|
||||
public M3U8InfoThread(DTaskWrapper taskWrapper, OnFileInfoCallback callback) {
|
||||
@@ -122,38 +118,51 @@ final public class M3U8InfoThread implements Runnable {
|
||||
}
|
||||
List<String> extInf = new ArrayList<>();
|
||||
boolean isLive = mTaskWrapper.getRequestType() == ITaskWrapper.M3U8_LIVE;
|
||||
boolean isGenerateIndexFile = mTaskWrapper.getEntity().getM3U8Entity().isGenerateIndexFile();
|
||||
boolean isGenerateIndexFile =
|
||||
((M3U8TaskOption) mTaskWrapper.getM3u8Option()).isGenerateIndexFile();
|
||||
// 写入索引信息的流
|
||||
FileOutputStream fos = null;
|
||||
if (isGenerateIndexFile) {
|
||||
mInfos.add(line);
|
||||
String indexPath = String.format(M3U8_INDEX_FORMAT, mEntity.getFilePath());
|
||||
File indexFile = new File(indexPath);
|
||||
if (!indexFile.exists()) {
|
||||
FileUtil.createFile(indexPath);
|
||||
}else {
|
||||
//FileUtil.deleteFile(indexPath);
|
||||
}
|
||||
fos = new FileOutputStream(indexFile);
|
||||
ALog.d(TAG, line);
|
||||
addIndexInfo(isGenerateIndexFile, fos, line);
|
||||
}
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (isStop) {
|
||||
break;
|
||||
}
|
||||
if (isGenerateIndexFile) {
|
||||
mInfos.add(line);
|
||||
}
|
||||
if (line.startsWith("#EXT-X-ENDLIST")) {
|
||||
break;
|
||||
}
|
||||
ALog.d(TAG, line);
|
||||
if (line.startsWith("#EXTINF")) {
|
||||
String info = reader.readLine();
|
||||
mInfos.add(info);
|
||||
if (line.startsWith("#EXT-X-ENDLIST")) {
|
||||
// 点播文件的下载写入结束标志,直播文件的下载在停止时才写入结束标志
|
||||
addIndexInfo(isGenerateIndexFile && !isLive, fos, line);
|
||||
break;
|
||||
} else if (line.startsWith("#EXTINF")) {
|
||||
String url = reader.readLine();
|
||||
if (isLive) {
|
||||
if (onGetPeerCallback != null) {
|
||||
onGetPeerCallback.onGetPeer(info);
|
||||
onGetPeerCallback.onGetPeer(url, line);
|
||||
}
|
||||
} else {
|
||||
extInf.add(info);
|
||||
extInf.add(url);
|
||||
}
|
||||
ALog.d(TAG, url);
|
||||
addIndexInfo(isGenerateIndexFile && !isLive, fos, line);
|
||||
addIndexInfo(isGenerateIndexFile && !isLive, fos, url);
|
||||
} else if (line.startsWith("#EXT-X-STREAM-INF")) {
|
||||
addIndexInfo(isGenerateIndexFile, fos, line);
|
||||
int setBand = mM3U8Option.getBandWidth();
|
||||
int bandWidth = getBandWidth(line);
|
||||
// 多码率的m3u8配置文件,清空信息
|
||||
if (isGenerateIndexFile && mInfos != null) {
|
||||
mInfos.clear();
|
||||
}
|
||||
//if (isGenerateIndexFile && mInfos != null) {
|
||||
// mInfos.clear();
|
||||
//}
|
||||
if (setBand == 0) {
|
||||
handleBandWidth(conn, reader.readLine());
|
||||
} else if (bandWidth == setBand) {
|
||||
@@ -162,8 +171,11 @@ final public class M3U8InfoThread implements Runnable {
|
||||
failDownload(String.format("【%s】码率不存在", bandWidth), false);
|
||||
}
|
||||
return;
|
||||
} else if (line.startsWith("EXT-X-KEY")) {
|
||||
} else if (line.startsWith("#EXT-X-KEY")) {
|
||||
addIndexInfo(isGenerateIndexFile, fos, line);
|
||||
getKeyInfo(line);
|
||||
} else {
|
||||
addIndexInfo(isGenerateIndexFile, fos, line);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,8 +189,11 @@ final public class M3U8InfoThread implements Runnable {
|
||||
}
|
||||
CompleteInfo info = new CompleteInfo();
|
||||
info.obj = extInf;
|
||||
generateIndexFile();
|
||||
|
||||
onFileInfoCallback.onComplete(mEntity.getKey(), info);
|
||||
if (fos != null) {
|
||||
fos.close();
|
||||
}
|
||||
} else if (code == HttpURLConnection.HTTP_MOVED_TEMP
|
||||
|| code == HttpURLConnection.HTTP_MOVED_PERM
|
||||
|| code == HttpURLConnection.HTTP_SEE_OTHER
|
||||
@@ -193,40 +208,19 @@ final public class M3U8InfoThread implements Runnable {
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建索引文件
|
||||
* 添加切片信息到索引文件中
|
||||
* 直播下载的索引只记录头部信息,不记录EXTINF中的信息,该信息在onGetPeer的方法中添加。
|
||||
* 点播下载记录所有信息
|
||||
*
|
||||
* @param write true 将信息写入文件
|
||||
* @param info 切片信息
|
||||
*/
|
||||
private void generateIndexFile() {
|
||||
if (mTaskWrapper.getEntity().getM3U8Entity().isGenerateIndexFile()) {
|
||||
|
||||
String indexPath = String.format(M3U8_INDEX_FORMAT, mEntity.getFilePath());
|
||||
File indexFile = new File(indexPath);
|
||||
if (indexFile.exists()) {
|
||||
FileUtil.deleteFile(indexPath);
|
||||
}
|
||||
FileUtil.createFile(indexPath);
|
||||
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
fos = new FileOutputStream(indexFile);
|
||||
for (String str : mInfos) {
|
||||
byte[] by = str.concat("\r\n").getBytes(Charset.forName("UTF-8"));
|
||||
fos.write(by, 0, by.length);
|
||||
}
|
||||
fos.flush();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (fos != null) {
|
||||
try {
|
||||
fos.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
private void addIndexInfo(boolean write, FileOutputStream fos, String info)
|
||||
throws IOException {
|
||||
if (!write) {
|
||||
return;
|
||||
}
|
||||
fos.write(info.concat("\r\n").getBytes(Charset.forName("UTF-8")));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -24,6 +24,7 @@ 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.FileUtil;
|
||||
import com.arialyy.aria.util.RecordUtil;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
@@ -42,7 +43,7 @@ public class M3U8RecordAdapter extends AbsRecordHandlerAdapter {
|
||||
|
||||
@Override public void onPre() {
|
||||
super.onPre();
|
||||
if (getWrapper().getRequestType() == ITaskWrapper.M3U8_LIVE){
|
||||
if (getWrapper().getRequestType() == ITaskWrapper.M3U8_LIVE) {
|
||||
RecordUtil.delTaskRecord(getEntity().getFilePath(), IRecordHandler.TYPE_DOWNLOAD);
|
||||
}
|
||||
}
|
||||
@@ -54,18 +55,22 @@ public class M3U8RecordAdapter extends AbsRecordHandlerAdapter {
|
||||
String cacheDir = mOption.getCacheDir();
|
||||
long currentProgress = 0;
|
||||
int completeNum = 0;
|
||||
File targetFile = new File(mTaskRecord.filePath);
|
||||
if (!targetFile.exists()){
|
||||
FileUtil.createFile(targetFile);
|
||||
}
|
||||
|
||||
M3U8Entity m3U8Entity = ((DownloadEntity) getEntity()).getM3U8Entity();
|
||||
// 重新下载所有切片
|
||||
boolean reDownload =
|
||||
(m3U8Entity.getPeerNum() <= 0 || (m3U8Entity.isGenerateIndexFile() && !new File(
|
||||
(m3U8Entity.getPeerNum() <= 0 || (mOption.isGenerateIndexFile() && !new File(
|
||||
String.format(M3U8InfoThread.M3U8_INDEX_FORMAT, getEntity().getFilePath())).exists()));
|
||||
|
||||
for (ThreadRecord record : mTaskRecord.threadRecords) {
|
||||
File temp = new File(BaseM3U8Loader.getTsFilePath(cacheDir, record.threadId));
|
||||
if (!record.isComplete || reDownload) {
|
||||
if (temp.exists()) {
|
||||
temp.delete();
|
||||
FileUtil.deleteFile(temp);
|
||||
}
|
||||
record.startLocation = 0;
|
||||
//ALog.d(TAG, String.format("分片【%s】未完成,将重新下载该分片", record.threadId));
|
||||
|
||||
@@ -101,14 +101,14 @@ public class M3U8TaskOption implements ITaskOption {
|
||||
/**
|
||||
* 生成索引占位字段
|
||||
*/
|
||||
private boolean generateIndexFileTemp = false;
|
||||
private boolean generateIndexFile = false;
|
||||
|
||||
public boolean isGenerateIndexFileTemp() {
|
||||
return generateIndexFileTemp;
|
||||
public boolean isGenerateIndexFile() {
|
||||
return generateIndexFile;
|
||||
}
|
||||
|
||||
public void setGenerateIndexFileTemp(boolean generateIndexFileTemp) {
|
||||
this.generateIndexFileTemp = generateIndexFileTemp;
|
||||
public void setGenerateIndexFile(boolean generateIndexFile) {
|
||||
this.generateIndexFile = generateIndexFile;
|
||||
}
|
||||
|
||||
public int getJumpIndex() {
|
||||
|
||||
@@ -89,6 +89,14 @@ public class M3U8ThreadTaskAdapter extends AbsThreadTaskAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
int code = conn.getResponseCode();
|
||||
if (code != HttpURLConnection.HTTP_OK) {
|
||||
fail(new TaskException(TAG,
|
||||
String.format("连接错误,http错误码:%s,url:%s", code, getThreadConfig().url)),
|
||||
false);
|
||||
return;
|
||||
}
|
||||
|
||||
is = new BufferedInputStream(ConnectionHelp.convertInputStream(conn));
|
||||
if (mHttpTaskOption.isChunked()) {
|
||||
readChunked(is);
|
||||
|
||||
@@ -24,17 +24,22 @@ import com.arialyy.aria.core.common.SubThreadConfig;
|
||||
import com.arialyy.aria.core.download.DTaskWrapper;
|
||||
import com.arialyy.aria.core.inf.IThreadState;
|
||||
import com.arialyy.aria.core.listener.IEventListener;
|
||||
import com.arialyy.aria.core.listener.ISchedulers;
|
||||
import com.arialyy.aria.core.manager.ThreadTaskManager;
|
||||
import com.arialyy.aria.core.processor.ITsMergeHandler;
|
||||
import com.arialyy.aria.core.task.ThreadTask;
|
||||
import com.arialyy.aria.m3u8.BaseM3U8Loader;
|
||||
import com.arialyy.aria.core.processor.ITsMergeHandler;
|
||||
import com.arialyy.aria.m3u8.IdGenerator;
|
||||
import com.arialyy.aria.m3u8.M3U8Listener;
|
||||
import com.arialyy.aria.m3u8.M3U8TaskOption;
|
||||
import com.arialyy.aria.m3u8.M3U8ThreadTaskAdapter;
|
||||
import com.arialyy.aria.util.ALog;
|
||||
import com.arialyy.aria.util.FileUtil;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
@@ -42,6 +47,8 @@ import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import static com.arialyy.aria.m3u8.M3U8InfoThread.M3U8_INDEX_FORMAT;
|
||||
|
||||
/**
|
||||
* M3U8点播文件下载器
|
||||
*/
|
||||
@@ -49,15 +56,21 @@ public class M3U8LiveLoader extends BaseM3U8Loader {
|
||||
/**
|
||||
* 最大执行数
|
||||
*/
|
||||
private static final int EXEC_MAX_NUM = 4;
|
||||
private static int EXEC_MAX_NUM = 4;
|
||||
private Handler mStateHandler;
|
||||
private ArrayBlockingQueue<Long> mFlagQueue = new ArrayBlockingQueue<>(EXEC_MAX_NUM);
|
||||
private ReentrantLock LOCK = new ReentrantLock();
|
||||
private Condition mCondition = LOCK.newCondition();
|
||||
private LinkedBlockingQueue<String> mPeerQueue = new LinkedBlockingQueue<>();
|
||||
private LinkedBlockingQueue<ExtInfo> mPeerQueue = new LinkedBlockingQueue<>();
|
||||
private ExtInfo mCurExtInfo;
|
||||
private FileOutputStream mIndexFos;
|
||||
|
||||
M3U8LiveLoader(M3U8Listener listener, DTaskWrapper wrapper) {
|
||||
super(listener, wrapper);
|
||||
if (((M3U8TaskOption) wrapper.getM3u8Option()).isGenerateIndexFile()) {
|
||||
ALog.i(TAG, "直播文件下载,创建索引文件的操作将导致只能同时下载一个切片");
|
||||
EXEC_MAX_NUM = 1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override protected IThreadState createStateManager(Looper looper) {
|
||||
@@ -66,8 +79,8 @@ public class M3U8LiveLoader extends BaseM3U8Loader {
|
||||
return manager;
|
||||
}
|
||||
|
||||
void offerPeer(String peerUrl) {
|
||||
mPeerQueue.offer(peerUrl);
|
||||
void offerPeer(ExtInfo extInfo) {
|
||||
mPeerQueue.offer(extInfo);
|
||||
}
|
||||
|
||||
@Override protected void handleTask() {
|
||||
@@ -80,13 +93,14 @@ public class M3U8LiveLoader extends BaseM3U8Loader {
|
||||
try {
|
||||
LOCK.lock();
|
||||
while (mFlagQueue.size() < EXEC_MAX_NUM) {
|
||||
String url = mPeerQueue.poll();
|
||||
if (url == null) {
|
||||
ExtInfo extInfo = mPeerQueue.poll();
|
||||
if (extInfo == null) {
|
||||
break;
|
||||
}
|
||||
ThreadTask task = createThreadTask(cacheDir, index, url);
|
||||
mCurExtInfo = extInfo;
|
||||
ThreadTask task = createThreadTask(cacheDir, index, extInfo.url);
|
||||
getTaskList().put(index, task);
|
||||
mFlagQueue.offer(startThreadTask(task));
|
||||
mFlagQueue.offer(startThreadTask(task, task.getConfig().peerIndex));
|
||||
index++;
|
||||
}
|
||||
if (mFlagQueue.size() > 0) {
|
||||
@@ -106,11 +120,15 @@ public class M3U8LiveLoader extends BaseM3U8Loader {
|
||||
return mTempFile.length();
|
||||
}
|
||||
|
||||
private void notifyLock() {
|
||||
private void notifyLock(boolean success, int peerId) {
|
||||
try {
|
||||
LOCK.lock();
|
||||
long id = mFlagQueue.take();
|
||||
ALog.d(TAG, String.format("线程【%s】完成", id));
|
||||
if (success) {
|
||||
ALog.d(TAG, String.format("切片【%s】下载成功", peerId));
|
||||
} else {
|
||||
ALog.e(TAG, String.format("切片【%s】下载失败", peerId));
|
||||
}
|
||||
mCondition.signalAll();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
@@ -119,13 +137,27 @@ public class M3U8LiveLoader extends BaseM3U8Loader {
|
||||
}
|
||||
}
|
||||
|
||||
@Override protected void onPostStop() {
|
||||
super.onPostStop();
|
||||
if (mIndexFos != null) {
|
||||
try {
|
||||
mIndexFos.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动线程任务
|
||||
*
|
||||
* @return 线程唯一id标志
|
||||
*/
|
||||
private long startThreadTask(ThreadTask task) {
|
||||
private long startThreadTask(ThreadTask task, int indexId) {
|
||||
ThreadTaskManager.getInstance().startThread(mTaskWrapper.getKey(), task);
|
||||
((M3U8Listener) mListener).onPeerStart(mTaskWrapper.getKey(),
|
||||
task.getConfig().tempFile.getPath(),
|
||||
indexId);
|
||||
return IdGenerator.getInstance().nextId();
|
||||
}
|
||||
|
||||
@@ -139,6 +171,7 @@ public class M3U8LiveLoader extends BaseM3U8Loader {
|
||||
record.tsUrl = tsUrl;
|
||||
record.threadType = TaskRecord.TYPE_M3U8_LIVE;
|
||||
record.threadId = indexId;
|
||||
mRecord.threadRecords.add(record);
|
||||
|
||||
SubThreadConfig config = new SubThreadConfig();
|
||||
config.url = tsUrl;
|
||||
@@ -148,6 +181,7 @@ public class M3U8LiveLoader extends BaseM3U8Loader {
|
||||
config.taskWrapper = mTaskWrapper;
|
||||
config.record = record;
|
||||
config.stateHandler = mStateHandler;
|
||||
config.peerIndex = indexId;
|
||||
|
||||
if (!config.tempFile.exists()) {
|
||||
FileUtil.createFile(config.tempFile);
|
||||
@@ -163,10 +197,7 @@ public class M3U8LiveLoader extends BaseM3U8Loader {
|
||||
*
|
||||
* @return {@code true} 合并成功,{@code false}合并失败
|
||||
*/
|
||||
public boolean mergeFile() {
|
||||
if (getEntity().getM3U8Entity().isGenerateIndexFile()) {
|
||||
return generateIndexFile();
|
||||
}
|
||||
boolean mergeFile() {
|
||||
ITsMergeHandler mergeHandler = mM3U8Option.getMergeHandler();
|
||||
String cacheDir = getCacheDir();
|
||||
List<String> partPath = new ArrayList<>();
|
||||
@@ -213,7 +244,7 @@ public class M3U8LiveLoader extends BaseM3U8Loader {
|
||||
/**
|
||||
* 任务状态回调
|
||||
*/
|
||||
private IEventListener mListener;
|
||||
private M3U8Listener mListener;
|
||||
private long mProgress; //当前总进度
|
||||
private Looper mLooper;
|
||||
|
||||
@@ -222,7 +253,7 @@ public class M3U8LiveLoader extends BaseM3U8Loader {
|
||||
*/
|
||||
LiveStateManager(Looper looper, IEventListener listener) {
|
||||
mLooper = looper;
|
||||
mListener = listener;
|
||||
mListener = (M3U8Listener) listener;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -234,6 +265,7 @@ public class M3U8LiveLoader extends BaseM3U8Loader {
|
||||
}
|
||||
|
||||
@Override public boolean handleMessage(Message msg) {
|
||||
int peerIndex = msg.getData().getInt(ISchedulers.DATA_M3U8_PEER_INDEX);
|
||||
switch (msg.what) {
|
||||
case STATE_STOP:
|
||||
if (isBreak()) {
|
||||
@@ -248,15 +280,46 @@ public class M3U8LiveLoader extends BaseM3U8Loader {
|
||||
}
|
||||
break;
|
||||
case STATE_COMPLETE:
|
||||
notifyLock();
|
||||
notifyLock(true, peerIndex);
|
||||
if (mM3U8Option.isGenerateIndexFile() && !isBreak()) {
|
||||
addExtInf(mCurExtInfo.url, mCurExtInfo.extInf);
|
||||
}
|
||||
mListener.onPeerComplete(mTaskWrapper.getKey(),
|
||||
msg.getData().getString(ISchedulers.DATA_M3U8_PEER_PATH), peerIndex);
|
||||
break;
|
||||
case STATE_RUNNING:
|
||||
mProgress += (long) msg.obj;
|
||||
break;
|
||||
case STATE_FAIL:
|
||||
notifyLock(false, peerIndex);
|
||||
mListener.onPeerFail(mTaskWrapper.getKey(),
|
||||
msg.getData().getString(ISchedulers.DATA_M3U8_PEER_PATH), peerIndex);
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给索引文件添加extInfo信息
|
||||
*/
|
||||
private void addExtInf(String url, String extInf) {
|
||||
File indexFile =
|
||||
new File(String.format(M3U8_INDEX_FORMAT, getEntity().getFilePath()));
|
||||
if (!indexFile.exists()) {
|
||||
ALog.e(TAG, String.format("索引文件【%s】不存在,添加peer的extInf失败", indexFile.getPath()));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (mIndexFos == null) {
|
||||
mIndexFos = new FileOutputStream(indexFile, true);
|
||||
}
|
||||
mIndexFos.write(extInf.concat("\r\n").getBytes(Charset.forName("UTF-8")));
|
||||
mIndexFos.write(url.concat("\r\n").getBytes(Charset.forName("UTF-8")));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override public boolean isFail() {
|
||||
return false;
|
||||
}
|
||||
@@ -269,4 +332,14 @@ public class M3U8LiveLoader extends BaseM3U8Loader {
|
||||
return mProgress;
|
||||
}
|
||||
}
|
||||
|
||||
static class ExtInfo {
|
||||
String url;
|
||||
String extInf;
|
||||
|
||||
ExtInfo(String url, String extInf) {
|
||||
this.url = url;
|
||||
this.extInf = extInf;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import com.arialyy.aria.core.processor.ILiveTsUrlConverter;
|
||||
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
|
||||
import com.arialyy.aria.exception.BaseException;
|
||||
import com.arialyy.aria.exception.M3U8Exception;
|
||||
import com.arialyy.aria.exception.TaskException;
|
||||
import com.arialyy.aria.http.HttpTaskOption;
|
||||
import com.arialyy.aria.m3u8.M3U8InfoThread;
|
||||
import com.arialyy.aria.m3u8.M3U8Listener;
|
||||
@@ -87,7 +88,7 @@ public class M3U8LiveUtil extends AbsNormalLoaderUtil {
|
||||
}
|
||||
});
|
||||
infoThread.setOnGetPeerCallback(new M3U8InfoThread.OnGetLivePeerCallback() {
|
||||
@Override public void onGetPeer(String url) {
|
||||
@Override public void onGetPeer(String url, String extInf) {
|
||||
if (mPeerUrls.contains(url)) {
|
||||
return;
|
||||
}
|
||||
@@ -104,7 +105,7 @@ public class M3U8LiveUtil extends AbsNormalLoaderUtil {
|
||||
fail(new M3U8Exception(TAG, String.format("ts地址错误,url:%s", url)), false);
|
||||
return;
|
||||
}
|
||||
getLoader().offerPeer(url);
|
||||
getLoader().offerPeer(new M3U8LiveLoader.ExtInfo(url, extInf));
|
||||
}
|
||||
});
|
||||
return infoThread;
|
||||
@@ -129,7 +130,13 @@ public class M3U8LiveUtil extends AbsNormalLoaderUtil {
|
||||
if (mInfoThread != null) {
|
||||
mInfoThread.setStop(true);
|
||||
closeTimer();
|
||||
if (mM3U8Option.isMergeFile()) {
|
||||
if (((M3U8TaskOption) getTaskWrapper().getM3u8Option()).isGenerateIndexFile()) {
|
||||
if (getLoader().generateIndexFile(true)) {
|
||||
getListener().onComplete();
|
||||
} else {
|
||||
getListener().onFail(false, new TaskException(TAG, "创建索引文件失败"));
|
||||
}
|
||||
} else if (mM3U8Option.isMergeFile()) {
|
||||
if (getLoader().mergeFile()) {
|
||||
getListener().onComplete();
|
||||
} else {
|
||||
|
||||
@@ -31,14 +31,16 @@ import com.arialyy.aria.core.inf.IThreadState;
|
||||
import com.arialyy.aria.core.listener.IEventListener;
|
||||
import com.arialyy.aria.core.listener.ISchedulers;
|
||||
import com.arialyy.aria.core.manager.ThreadTaskManager;
|
||||
import com.arialyy.aria.core.processor.ITsMergeHandler;
|
||||
import com.arialyy.aria.core.task.ThreadTask;
|
||||
import com.arialyy.aria.exception.BaseException;
|
||||
import com.arialyy.aria.exception.TaskException;
|
||||
import com.arialyy.aria.m3u8.BaseM3U8Loader;
|
||||
import com.arialyy.aria.core.processor.ITsMergeHandler;
|
||||
import com.arialyy.aria.m3u8.M3U8Listener;
|
||||
import com.arialyy.aria.m3u8.M3U8TaskOption;
|
||||
import com.arialyy.aria.m3u8.M3U8ThreadTaskAdapter;
|
||||
import com.arialyy.aria.util.ALog;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import com.arialyy.aria.util.FileUtil;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
@@ -219,6 +221,12 @@ public class M3U8VodLoader extends BaseM3U8Loader {
|
||||
}
|
||||
}
|
||||
mManager.updateStateCount();
|
||||
if (mCompleteNum <= 0){
|
||||
mListener.onStart(0);
|
||||
}else {
|
||||
int percent = mCompleteNum * 100 / mRecord.threadRecords.size();
|
||||
mListener.onResume(percent);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -446,7 +454,7 @@ public class M3U8VodLoader extends BaseM3U8Loader {
|
||||
* M3U8线程状态管理
|
||||
*/
|
||||
private class VodStateManager implements IThreadState {
|
||||
private final String TAG = "M3U8ThreadStateManager";
|
||||
private final String TAG = CommonUtil.getClassName(VodStateManager.class);
|
||||
|
||||
/**
|
||||
* 任务状态回调
|
||||
@@ -456,7 +464,7 @@ public class M3U8VodLoader extends BaseM3U8Loader {
|
||||
private int cancelNum = 0; // 已经取消的线程的数
|
||||
private int stopNum = 0; // 已经停止的线程数
|
||||
private int failNum = 0; // 失败的线程数
|
||||
private long progress; //当前总进度
|
||||
private long progress; //当前总进度,百分比进度
|
||||
private TaskRecord taskRecord; // 任务记录
|
||||
private Looper looper;
|
||||
|
||||
@@ -559,7 +567,14 @@ public class M3U8VodLoader extends BaseM3U8Loader {
|
||||
"startThreadNum = %s, stopNum = %s, cancelNum = %s, failNum = %s, completeNum = %s, flagQueueSize = %s",
|
||||
startThreadNum, stopNum, cancelNum, failNum, mCompleteNum, mFlagQueue.size()));
|
||||
ALog.d(TAG, String.format("vod任务【%s】完成", mTempFile.getName()));
|
||||
if (mM3U8Option.isMergeFile()) {
|
||||
|
||||
if (mM3U8Option.isGenerateIndexFile()) {
|
||||
if (generateIndexFile(false)){
|
||||
listener.onComplete();
|
||||
}else {
|
||||
listener.onFail(false, new TaskException(TAG, "创建索引文件失败"));
|
||||
}
|
||||
} else if (mM3U8Option.isMergeFile()) {
|
||||
if (mergeFile()) {
|
||||
listener.onComplete();
|
||||
} else {
|
||||
@@ -572,7 +587,7 @@ public class M3U8VodLoader extends BaseM3U8Loader {
|
||||
}
|
||||
break;
|
||||
case STATE_RUNNING:
|
||||
progress += (long) msg.obj;
|
||||
//progress += (long) msg.obj;
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
@@ -596,6 +611,7 @@ public class M3U8VodLoader extends BaseM3U8Loader {
|
||||
int percent = completeNum * 100 / taskRecord.threadRecords.size();
|
||||
getEntity().setPercent(percent);
|
||||
getEntity().update();
|
||||
progress = percent;
|
||||
}
|
||||
|
||||
@Override public boolean isFail() {
|
||||
@@ -625,9 +641,6 @@ public class M3U8VodLoader extends BaseM3U8Loader {
|
||||
* @return {@code true} 合并成功,{@code false}合并失败
|
||||
*/
|
||||
private boolean mergeFile() {
|
||||
if (getEntity().getM3U8Entity().isGenerateIndexFile()) {
|
||||
return generateIndexFile();
|
||||
}
|
||||
ITsMergeHandler mergeHandler = mM3U8Option.getMergeHandler();
|
||||
String cacheDir = getCacheDir();
|
||||
List<String> partPath = new ArrayList<>();
|
||||
|
||||
Reference in New Issue
Block a user