001package com.baidu.cloud.media.download;
002
003import java.util.Observable;
004
005/**
006 * downloader interface
007 * 
008 * @author shunwei
009 * 
010 */
011public abstract class DownloadableVideoItem extends Observable {
012
013    public static final String[] ERROR_CODE_DESC = {"ERROR_CODE_NO_ERROR", "ERROR_CODE_INVALID_URL",
014            "ERROR_CODE_NETWORK_FAILED", "ERROR_CODE_SDCARD_UNMOUNTED", "ERROR_CODE_M3U8_INVALID_FORMAT",
015            "ERROR_CODE_M3U8_SAVE_FAILED", "ERROR_CODE_M3U8_DRM_INVALID", "ERROR_CODE_TS_SAVE_FAILED"};
016    public static final int ERROR_CODE_NO_ERROR = 0;
017    public static final int ERROR_CODE_INVALID_URL = 1;
018    public static final int ERROR_CODE_NETWORK_FAILED = 2;
019    public static final int ERROR_CODE_SDCARD_UNMOUNTED = 3;
020    public static final int ERROR_CODE_M3U8_INVALID_FORMAT = 4;
021    public static final int ERROR_CODE_M3U8_SAVE_FAILED = 5;
022    public static final int ERROR_CODE_M3U8_DRM_INVALID = 6;
023    public static final int ERROR_CODE_TS_SAVE_FAILED = 7;
024    
025    protected String url;
026
027    protected String folderPath;
028    protected String fileName;
029
030    protected volatile int progress; // 2341 means 23.41%
031    protected volatile int speed;
032
033    protected volatile DownloadStatus downloadStatus = DownloadStatus.NONE;
034    protected String failReason;
035    
036    protected volatile int errorCode;
037
038    /**
039     * 获取错误码
040     * 当DownloadStatus的状态为ERROR时,通过该接口获取错误码
041     * @return
042     */
043    public int getErrorCode() {
044        return errorCode;
045    }
046
047    /**
048     * 下载状态 解析hls文件失败时,返回错误 ERROR;下载ts遇到网络问题时或被强关时,返回 暂停状态;
049     */
050    public static enum DownloadStatus {
051        NONE(0, "first add"), DOWNLOADING(1, "downloading videos"), PAUSED(2, "paused"), COMPLETED(3, "completed"),
052        ERROR(4, "failed to download"), DELETED(5, "delete manually"),
053        PENDING(6, "pending, will start automatically(blocked by Parallel Strategy)");
054
055        private int code;
056        private String msg;
057
058        private DownloadStatus(int code, String msg) {
059            this.code = code;
060            this.msg = msg;
061        }
062
063        public int getCode() {
064            return code;
065        }
066
067        public String getMessage() {
068            return msg;
069        }
070    }
071
072    /**
073     * Get the URL (in String)
074     */
075    public String getUrl() {
076        return url;
077    }
078
079    /**
080     * 获取本地文件的全路径名
081     * @return
082     */
083    public String getLocalAbsolutePath() {
084        if (fileName == null || fileName.equals("")) {
085            return folderPath;
086        } else if (folderPath == null || folderPath.equals("")) {
087            return null;
088        }
089        return folderPath + "/" + fileName;
090    }
091
092    /**
093     * Get the downloaded file's size 注:总大小需要换算 = ts1 * totalTime /
094     * ts1_time;暂不对外提供
095     */
096    // public int getFileSize();
097
098    /**
099     * Get the current progress of the download
100     * 
101     * @return progress the progress of downloading. 0<=progress<=100
102     */
103    public float getProgress() {
104        return ((float) progress) / 100;
105    }
106
107    public String getSpeed() {
108        if (downloadStatus == DownloadStatus.DOWNLOADING) {
109            if (speed < 1024) {
110                return speed + "KB/s";
111            } else {
112                return speed / 1024 + "MB/s";
113            }
114        } else {
115            return "0KB/s";
116        }
117    }
118
119    /**
120     * Get current state of the downloader
121     */
122    public DownloadStatus getStatus() {
123        return downloadStatus;
124    }
125
126    /**
127     * 获取错误描述
128     * 推荐使用getErrorCode接口
129     * @return
130     */
131    public String getFailReason() {
132        return failReason;
133    }
134
135}