Skip to content

Commit

Permalink
修复转换器可能出现的内存泄漏
Browse files Browse the repository at this point in the history
  • Loading branch information
AriaLyy committed Jun 20, 2019
1 parent ae3d24f commit 060b1f1
Show file tree
Hide file tree
Showing 22 changed files with 275 additions and 75 deletions.
Expand Up @@ -41,13 +41,15 @@
*
* </pre>
*/
public interface IFtpUploadInterceptor {
public class IFtpUploadInterceptor {

/**
* 处理拦截事件
*
* @param entity 上传信息实体
* @param fileList ftp服务器端remotePath下的文件列表
*/
FtpInterceptHandler onIntercept(UploadEntity entity, List<String> fileList);
public FtpInterceptHandler onIntercept(UploadEntity entity, List<String> fileList) {
return null;
}
}
Expand Up @@ -118,7 +118,8 @@ private void checkM3U8() {
wrapper.asM3U8().getBandWidth()));
if (wrapper.getRequestType() == ITaskWrapper.M3U8_VOD) {
if (mEntity.getFileSize() == 0) {
ALog.w(TAG, "由于m3u8协议的特殊性质,无法获取到正确到文件长度,因此你需要自行设置文件大小:asM3U8().setFileSize(xxx)");
ALog.w(TAG,
"由于m3u8协议的特殊性质,无法有效快速获取到正确到文件长度,如果你需要显示文件中长度,你需要自行设置文件长度:.asM3U8().asVod().setFileSize(xxx)");
}
} else if (wrapper.getRequestType() == ITaskWrapper.M3U8_LIVE) {
if (file.exists()) {
Expand Down
Expand Up @@ -19,9 +19,13 @@
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import com.arialyy.aria.core.download.m3u8.M3U8Entity;
import com.arialyy.aria.core.inf.AbsNormalEntity;
import com.arialyy.aria.core.inf.AbsTaskWrapper;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.orm.annotation.Ignore;
import com.arialyy.aria.orm.annotation.Unique;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;

/**
Expand Down Expand Up @@ -52,6 +56,27 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
*/
private String serverFileName;

@Ignore
private M3U8Entity m3U8Entity;

public M3U8Entity getM3U8Entity() {
if (TextUtils.isEmpty(downloadPath)) {
ALog.e("DownloadEntity", "文件保存路径为空,获取m3u8实体之前需要设置文件保存路径");
return null;
}
if (m3U8Entity == null) {
m3U8Entity = DbEntity.findFirst(M3U8Entity.class, "filePath=?", downloadPath);
}
if (m3U8Entity == null) {
m3U8Entity = new M3U8Entity();
m3U8Entity.setFilePath(downloadPath);
m3U8Entity.setPeerIndex(0);
m3U8Entity.insert();
}

return m3U8Entity;
}

@Override public String getKey() {
return getUrl();
}
Expand Down Expand Up @@ -124,19 +149,6 @@ public DownloadEntity setFilePath(String filePath) {
return (DownloadEntity) super.clone();
}

@Override public int describeContents() {
return 0;
}

@Override public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeString(this.downloadPath);
dest.writeString(this.groupHash);
dest.writeString(this.md5Code);
dest.writeString(this.disposition);
dest.writeString(this.serverFileName);
}

@Override public String toString() {
return "DownloadEntity{"
+ "downloadPath='"
Expand All @@ -160,13 +172,28 @@ public DownloadEntity setFilePath(String filePath) {
+ '}';
}

@Override public int describeContents() {
return 0;
}

@Override public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeString(this.downloadPath);
dest.writeString(this.groupHash);
dest.writeString(this.md5Code);
dest.writeString(this.disposition);
dest.writeString(this.serverFileName);
dest.writeParcelable(this.m3U8Entity, flags);
}

protected DownloadEntity(Parcel in) {
super(in);
this.downloadPath = in.readString();
this.groupHash = in.readString();
this.md5Code = in.readString();
this.disposition = in.readString();
this.serverFileName = in.readString();
this.m3U8Entity = in.readParcelable(M3U8Entity.class.getClassLoader());
}

public static final Creator<DownloadEntity> CREATOR = new Creator<DownloadEntity>() {
Expand Down
Expand Up @@ -90,11 +90,6 @@ public HttpFileInfoThread(DTaskWrapper taskWrapper, OnFileInfoCallback callback)
if (conn != null) {
conn.disconnect();
}
// 销毁文件长度适配器
IHttpFileLenAdapter lenAdapter = mTaskWrapper.asHttp().getFileLenAdapter();
if (lenAdapter != null && lenAdapter.getClass().isAnonymousClass()) {
mTaskWrapper.asHttp().setFileLenAdapter(null);
}
}
}

Expand Down Expand Up @@ -336,7 +331,7 @@ private void failDownload(BaseException e, boolean needRetry) {
}
}

private static class FileLenAdapter implements IHttpFileLenAdapter {
private static class FileLenAdapter extends IHttpFileLenAdapter {

@Override public long handleFileLen(Map<String, List<String>> headers) {
List<String> sLength = headers.get("Content-Length");
Expand Down
Expand Up @@ -19,7 +19,7 @@
* M3U8 bandWidth 码率url转换器,对于某些服务器,返回的ts地址可以是相对地址,也可能是处理过的,
* 对于这种情况,你需要使用url转换器将地址转换为可正常访问的http地址
*/
public interface IBandWidthUrlConverter {
public class IBandWidthUrlConverter {

/**
* 转换码率地址为可用的http地址,对于某些服务器,返回的切片信息有可能是相对地址,也可能是处理过的,
Expand All @@ -28,5 +28,7 @@ public interface IBandWidthUrlConverter {
* @param bandWidthUrl 原始码率地址
* @return 可正常访问的http地址
*/
String convert(String bandWidthUrl);
public String convert(String bandWidthUrl) {
return null;
}
}
Expand Up @@ -19,7 +19,7 @@
* M3U8 直播下载,ts url转换器,对于某些服务器,返回的ts地址可以是相对地址,也可能是处理过的
* 对于这种情况,你需要使用url转换器将地址转换为可正常访问的http地址
*/
public interface ILiveTsUrlConverter {
public class ILiveTsUrlConverter {

/**
* 处理#EXTINF信息,对于某些服务器,返回的切片信息有可能是相对地址,因此,你需要自行转换为可下载http连接
Expand All @@ -28,5 +28,7 @@ public interface ILiveTsUrlConverter {
* @param tsUrl ts文件下载地址
* @return 转换后的http地址
*/
String convert(String m3u8Url, String tsUrl);
public String convert(String m3u8Url, String tsUrl) {
return null;
}
}
Expand Up @@ -21,7 +21,7 @@
/**
* Ts文件合并处理,如果你希望使用自行处理ts文件的合并,你可以实现该接口
*/
public interface ITsMergeHandler {
public class ITsMergeHandler {

/**
* 合并ts文件
Expand All @@ -30,5 +30,7 @@ public interface ITsMergeHandler {
* @param tsPath ts文件列表
* @return {@code true} 合并成功
*/
boolean merge(@Nullable M3U8KeyInfo keyInfo, List<String> tsPath);
public boolean merge(@Nullable M3U8KeyInfo keyInfo, List<String> tsPath) {
return false;
}
}
Expand Up @@ -21,7 +21,7 @@
* M3U8 点播文件下载,ts url转换器,对于某些服务器,返回的ts地址可以是相对地址,也可能是处理过的
* 对于这种情况,你需要使用url转换器将地址转换为可正常访问的http地址
*/
public interface IVodTsUrlConverter {
public class IVodTsUrlConverter {

/**
* 处理#EXTINF信息,对于某些服务器,返回的切片信息有可能是相对地址,因此,你需要自行转换为可下载http连接
Expand All @@ -30,5 +30,7 @@ public interface IVodTsUrlConverter {
* @param tsUrls ts文件下载地址列表
* @return 根据切片信息转换后的http连接列表,如果你的切片信息是可以直接下载的http连接,直接返回extInf便可
*/
List<String> convert(String m3u8Url, List<String> tsUrls);
public List<String> convert(String m3u8Url, List<String> tsUrls) {
return null;
}
}
Expand Up @@ -52,20 +52,6 @@ public M3U8Delegate setMergeHandler(ITsMergeHandler handler) {
return this;
}

/**
* 由于m3u8协议的特殊性质,无法获取到正确到文件长度,因此你需要自行设置文件大小
*
* @param fileSize 文件长度
*/
public M3U8Delegate setFileSize(long fileSize) {
if (fileSize <= 0) {
ALog.e(TAG, "文件长度错误");
return this;
}
mTaskWrapper.getEntity().setFileSize(fileSize);
return this;
}

/**
* M3U8 ts 文件url转换器,对于某些服务器,返回的ts地址可以是相对地址,也可能是处理过的
* 对于这种情况,你需要使用url转换器将地址转换为可正常访问的http地址
Expand Down Expand Up @@ -98,6 +84,13 @@ public M3U8Delegate setBandWidthUrlConverter(IBandWidthUrlConverter converter) {
return this;
}

/**
* 处理点播文件的下载参数
*/
public M3U8VodDelegate<TARGET> asVod() {
return new M3U8VodDelegate<>(mTarget);
}

/**
* 处理直播类的下载
*/
Expand Down
109 changes: 109 additions & 0 deletions Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8Entity.java
@@ -0,0 +1,109 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.download.m3u8;

import android.os.Parcel;
import android.os.Parcelable;
import com.arialyy.aria.orm.DbEntity;

/**
* M3U8实体信息
*/
public class M3U8Entity extends DbEntity implements Parcelable {

/**
* 文件保存路径
*/
private String filePath;

/**
* 当前peer的位置
*/
private int peerIndex;

/**
* peer总数
*/
private int peerNum;

/**
* 是否是直播,true 直播
*/
private boolean isLive;

public boolean isLive() {
return isLive;
}

public void setLive(boolean live) {
isLive = live;
}

public String getFilePath() {
return filePath;
}

public void setFilePath(String filePath) {
this.filePath = filePath;
}

public int getPeerIndex() {
return peerIndex;
}

public void setPeerIndex(int peerIndex) {
this.peerIndex = peerIndex;
}

public int getPeerNum() {
return peerNum;
}

public void setPeerNum(int peerNum) {
this.peerNum = peerNum;
}

public M3U8Entity() {
}

@Override public int describeContents() {
return 0;
}

@Override public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.filePath);
dest.writeInt(this.peerIndex);
dest.writeInt(this.peerNum);
dest.writeByte(this.isLive ? (byte) 1 : (byte) 0);
}

protected M3U8Entity(Parcel in) {
this.filePath = in.readString();
this.peerIndex = in.readInt();
this.peerNum = in.readInt();
this.isLive = in.readByte() != 0;
}

public static final Creator<M3U8Entity> CREATOR = new Creator<M3U8Entity>() {
@Override public M3U8Entity createFromParcel(Parcel source) {
return new M3U8Entity(source);
}

@Override public M3U8Entity[] newArray(int size) {
return new M3U8Entity[size];
}
};
}

0 comments on commit 060b1f1

Please sign in to comment.