forked from TeamNewPipe/NewPipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostprocessing.java
More file actions
261 lines (211 loc) · 8.9 KB
/
Postprocessing.java
File metadata and controls
261 lines (211 loc) · 8.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package us.shandian.giga.postprocessing;
import android.util.Log;
import androidx.annotation.NonNull;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.streams.io.SharpStream;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import us.shandian.giga.get.DownloadMission;
import us.shandian.giga.io.ChunkFileInputStream;
import us.shandian.giga.io.CircularFileWriter;
import us.shandian.giga.io.CircularFileWriter.OffsetChecker;
import us.shandian.giga.io.ProgressReport;
import static us.shandian.giga.get.DownloadMission.ERROR_NOTHING;
import static us.shandian.giga.get.DownloadMission.ERROR_POSTPROCESSING;
import static us.shandian.giga.get.DownloadMission.ERROR_POSTPROCESSING_HOLD;
public abstract class Postprocessing implements Serializable {
static transient final byte OK_RESULT = ERROR_NOTHING;
public transient static final String ALGORITHM_TTML_CONVERTER = "ttml";
public transient static final String ALGORITHM_WEBM_MUXER = "webm";
public transient static final String ALGORITHM_MP4_FROM_DASH_MUXER = "mp4D-mp4";
public transient static final String ALGORITHM_M4A_NO_DASH = "mp4D-m4a";
public transient static final String ALGORITHM_OGG_FROM_WEBM_DEMUXER = "webm-ogg-d";
public static Postprocessing getAlgorithm(@NonNull String algorithmName, String[] args,
StreamInfo streamInfo) {
Postprocessing instance;
switch (algorithmName) {
case ALGORITHM_TTML_CONVERTER:
instance = new TtmlConverter();
break;
case ALGORITHM_WEBM_MUXER:
instance = new WebMMuxer();
break;
case ALGORITHM_MP4_FROM_DASH_MUXER:
instance = new Mp4FromDashMuxer();
break;
case ALGORITHM_M4A_NO_DASH:
instance = new M4aNoDash();
break;
case ALGORITHM_OGG_FROM_WEBM_DEMUXER:
instance = new OggFromWebmDemuxer();
break;
/*case "example-algorithm":
instance = new ExampleAlgorithm();*/
default:
throw new UnsupportedOperationException("Unimplemented post-processing algorithm: " + algorithmName);
}
instance.args = args;
instance.streamInfo = streamInfo;
return instance;
}
/**
* Get a boolean value that indicate if the given algorithm work on the same
* file
*/
public boolean worksOnSameFile;
/**
* Indicates whether the selected algorithm needs space reserved at the beginning of the file
*/
public boolean reserveSpace;
/**
* Gets the given algorithm short name
*/
private final String name;
private String[] args;
protected StreamInfo streamInfo;
private transient DownloadMission mission;
private transient File tempFile;
Postprocessing(boolean reserveSpace, boolean worksOnSameFile, String algorithmName) {
this.reserveSpace = reserveSpace;
this.worksOnSameFile = worksOnSameFile;
this.name = algorithmName;// for debugging only
}
public void setTemporalDir(@NonNull File directory) {
long rnd = (int) (Math.random() * 100000.0f);
tempFile = new File(directory, rnd + "_" + System.nanoTime() + ".tmp");
}
public void cleanupTemporalDir() {
if (tempFile != null && tempFile.exists()) {
try {
//noinspection ResultOfMethodCallIgnored
tempFile.delete();
} catch (Exception e) {
// nothing to do
}
}
}
public void run(DownloadMission target) throws IOException {
this.mission = target;
int result;
long finalLength = -1;
mission.done = 0;
long length = mission.storage.length() - mission.offsets[0];
mission.length = Math.max(length, mission.nearLength);
final ProgressReport readProgress = (long position) -> {
position -= mission.offsets[0];
if (position > mission.done) mission.done = position;
};
if (worksOnSameFile) {
ChunkFileInputStream[] sources = new ChunkFileInputStream[mission.urls.length];
try {
for (int i = 0, j = 1; i < sources.length; i++, j++) {
SharpStream source = mission.storage.getStream();
long end = j < sources.length ? mission.offsets[j] : source.length();
sources[i] = new ChunkFileInputStream(source, mission.offsets[i], end, readProgress);
}
if (test(sources)) {
for (SharpStream source : sources) source.rewind();
OffsetChecker checker = () -> {
for (ChunkFileInputStream source : sources) {
/*
* WARNING: never use rewind() in any chunk after any writing (especially on first chunks)
* or the CircularFileWriter can lead to unexpected results
*/
if (source.isClosed() || source.available() < 1) {
continue;// the selected source is not used anymore
}
return source.getFilePointer() - 1;
}
return -1;
};
try (CircularFileWriter out = new CircularFileWriter(
mission.storage.getStream(), tempFile, checker)) {
out.onProgress = (long position) -> mission.done = position;
out.onWriteError = err -> {
mission.psState = 3;
mission.notifyError(ERROR_POSTPROCESSING_HOLD, err);
try {
synchronized (this) {
while (mission.psState == 3)
wait();
}
} catch (InterruptedException e) {
// nothing to do
Log.e(getClass().getSimpleName(), "got InterruptedException");
}
return mission.errCode == ERROR_NOTHING;
};
result = process(out, sources);
if (result == OK_RESULT)
finalLength = out.finalizeFile();
}
} else {
result = OK_RESULT;
}
} finally {
for (SharpStream source : sources) {
if (source != null && !source.isClosed()) {
source.close();
}
}
if (tempFile != null) {
//noinspection ResultOfMethodCallIgnored
tempFile.delete();
tempFile = null;
}
}
} else {
result = test() ? process(null) : OK_RESULT;
}
if (result == OK_RESULT) {
if (finalLength != -1) {
mission.length = finalLength;
}
} else {
mission.errCode = ERROR_POSTPROCESSING;
mission.errObject = new RuntimeException("post-processing algorithm returned " + result);
}
if (result != OK_RESULT && worksOnSameFile) mission.storage.delete();
this.mission = null;
}
/**
* Test if the post-processing algorithm can be skipped
*
* @param sources files to be processed
* @return {@code true} if the post-processing is required, otherwise, {@code false}
* @throws IOException if an I/O error occurs.
*/
boolean test(SharpStream... sources) throws IOException {
return true;
}
/**
* Abstract method to execute the post-processing algorithm
*
* @param out output stream
* @param sources files to be processed
* @return an error code, {@code OK_RESULT} means the operation was successful
* @throws IOException if an I/O error occurs.
*/
abstract int process(SharpStream out, SharpStream... sources) throws IOException;
String getArgumentAt(int index, String defaultValue) {
if (args == null || index >= args.length) {
return defaultValue;
}
return args[index];
}
@NonNull
@Override
public String toString() {
StringBuilder str = new StringBuilder();
str.append("{ name=").append(name).append('[');
if (args != null) {
for (String arg : args) {
str.append(", ");
str.append(arg);
}
str.delete(0, 1);
}
return str.append("] }").toString();
}
}