MBRSetResponseGenerator.java
9.11 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.scene7.is.util.SizeInt
* com.scene7.is.util.TextEncodingTypeEnum
* com.scene7.is.util.XMLUtil
* com.scene7.is.util.callbacks.Option
* com.scene7.is.util.text.coders.NetPathCoder
* org.jetbrains.annotations.NotNull
*/
package com.scene7.is.ps.provider.fvctx;
import com.scene7.is.ps.provider.IZoomException;
import com.scene7.is.ps.provider.Request;
import com.scene7.is.ps.provider.fvctx.MediaSetRequest;
import com.scene7.is.ps.provider.fvctx.MediaSetVideoEntry;
import com.scene7.is.util.SizeInt;
import com.scene7.is.util.TextEncodingTypeEnum;
import com.scene7.is.util.XMLUtil;
import com.scene7.is.util.callbacks.Option;
import com.scene7.is.util.text.coders.NetPathCoder;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class MBRSetResponseGenerator {
private static final String NEW_LINE = "\n";
private static final String M3U8_EXTENSION = ".m3u8";
private static final String M3U8_HEADER = "#EXTM3U";
private static final String M3U8_PREFIX = "#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=";
private static final String[] M3U8_SUFFIX_PRECEDENCE = new String[]{"mp4", "f4v"};
private static final String F4M_EXTENSION = ".f4m";
private static final String[] F4M_SUFFIX_PRECEDENCE = new String[]{"f4v", "mp4"};
private static final String F4M_ELEMENT_MANIFEST = "manifest";
private static final String F4M_ELEMENT_MEDIA = "media";
private static final String F4M_ATTR_HREF = "href";
private static final String F4M_ATTR_BITRATE = "bitrate";
private static final String F4M_ATTR_XMLNS = "xmlns";
private static final String F4M_ATTR_VALUE_XMLNS = "http://ns.adobe.com/f4m/2.0";
private static final String F4M_ATTR_WIDTH = "width";
private static final String F4M_ATTR_HEIGHT = "height";
private static final String STREAMING_CONTEXT_ERROR = "STREAMING_CONTEXT_NOT_DEFINED";
@NotNull
static byte[] generateM3U8Response(@NotNull MediaSetRequest mediaSetRequest) throws IZoomException {
StringBuilder builder = new StringBuilder();
builder.append("#EXTM3U");
List<MediaSetVideoEntry> videoEntries = MBRSetResponseGenerator.filterVideos(mediaSetRequest.getVideoEntries(), M3U8_SUFFIX_PRECEDENCE);
for (MediaSetVideoEntry videoEntry : videoEntries) {
if (!MBRSetResponseGenerator.isValidVideoEntry(videoEntry)) continue;
MBRSetResponseGenerator.appendNewLines(builder, 2);
builder.append("#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=");
builder.append(((Long)videoEntry.getBitRate().get()).toString());
MBRSetResponseGenerator.appendNewLines(builder, 2);
builder.append(MBRSetResponseGenerator.addTrailing((String)videoEntry.getHttpAppleStreamingContext().getOrElse((Object)"STREAMING_CONTEXT_NOT_DEFINED"), '/'));
builder.append(MBRSetResponseGenerator.processFilePath(videoEntry.getFilePath()));
builder.append(".m3u8");
}
MBRSetResponseGenerator.appendNewLines(builder, 1);
try {
return builder.toString().getBytes(mediaSetRequest.getRequest().getResponseEncoding().toString());
}
catch (UnsupportedEncodingException e) {
throw new IZoomException(IZoomException.XML_ENCODING_ERROR, mediaSetRequest.getRequest().getResponseEncoding().toString(), e);
}
}
@NotNull
static byte[] generateF4MResponse(@NotNull MediaSetRequest mediaSetRequest) throws IZoomException {
Document document = XMLUtil.createDocument();
Element root = document.createElement("manifest");
root.setAttribute("xmlns", "http://ns.adobe.com/f4m/2.0");
document.appendChild(root);
List<MediaSetVideoEntry> videoEntries = MBRSetResponseGenerator.filterVideos(mediaSetRequest.getVideoEntries(), F4M_SUFFIX_PRECEDENCE);
for (MediaSetVideoEntry videoEntry : videoEntries) {
if (!MBRSetResponseGenerator.isValidVideoEntry(videoEntry)) continue;
Element media = document.createElement("media");
StringBuilder pathBuilder = new StringBuilder();
pathBuilder.append(MBRSetResponseGenerator.addTrailing((String)videoEntry.getHttpFlashStreamingContext().getOrElse((Object)"STREAMING_CONTEXT_NOT_DEFINED"), '/'));
pathBuilder.append(MBRSetResponseGenerator.processFilePath(videoEntry.getFilePath()));
pathBuilder.append(".f4m");
media.setAttribute("href", pathBuilder.toString());
long bitRate = (Long)videoEntry.getBitRate().get() / 1000;
media.setAttribute("bitrate", Long.toString(bitRate));
if (videoEntry.getVideoSize().isDefined()) {
SizeInt videoSize = (SizeInt)videoEntry.getVideoSize().get();
media.setAttribute("width", Integer.toString(videoSize.getWidth()));
media.setAttribute("height", Integer.toString(videoSize.getHeight()));
}
root.appendChild(media);
}
try {
return XMLUtil.toXMLBytes((Document)document, (TextEncodingTypeEnum)mediaSetRequest.getRequest().getResponseEncoding(), (boolean)false);
}
catch (UnsupportedEncodingException e) {
throw new IZoomException(IZoomException.XML_ENCODING_ERROR, mediaSetRequest.getRequest().getResponseEncoding().toString(), e);
}
}
private static void appendNewLines(@NotNull StringBuilder builder, int numNewLines) {
for (int i = 0; i < numNewLines; ++i) {
builder.append("\n");
}
}
@NotNull
private static List<MediaSetVideoEntry> filterVideos(@NotNull List<MediaSetVideoEntry> videoEntries, @NotNull String[] suffices) {
if (!videoEntries.isEmpty()) {
for (String suffix : suffices) {
List<MediaSetVideoEntry> filteredList = MBRSetResponseGenerator.filterVideos(videoEntries, suffix);
if (filteredList.isEmpty()) continue;
return filteredList;
}
return new ArrayList<MediaSetVideoEntry>();
}
return new ArrayList<MediaSetVideoEntry>();
}
@NotNull
private static List<MediaSetVideoEntry> filterVideos(@NotNull List<MediaSetVideoEntry> videoEntries, @NotNull String suffix) {
ArrayList<MediaSetVideoEntry> filteredList = new ArrayList<MediaSetVideoEntry>();
for (MediaSetVideoEntry videoEntry : videoEntries) {
if (!videoEntry.getFileSuffix().equalsIgnoreCase(suffix)) continue;
filteredList.add(videoEntry);
}
return filteredList;
}
private static boolean isValidVideoEntry(@NotNull MediaSetVideoEntry videoEntry) {
if (videoEntry.getFileSuffix().isEmpty()) {
return false;
}
if (!videoEntry.getBitRate().isDefined()) {
return false;
}
if (videoEntry.getFilePath().isEmpty()) {
return false;
}
return true;
}
@NotNull
private static String processFilePath(@NotNull String filePath) {
String dotStripped = MBRSetResponseGenerator.stripLeading(filePath, '.');
dotStripped = MBRSetResponseGenerator.stripLeading(dotStripped, '/');
return NetPathCoder.netPathCoder().encode(dotStripped);
}
@NotNull
private static String addLeading(@NotNull String s, char c) {
if (s.isEmpty()) {
return s;
}
if (s.charAt(0) != c) {
return s + c;
}
return s;
}
@NotNull
private static String addTrailing(@NotNull String s, char c) {
if (s.isEmpty()) {
return s;
}
if (s.charAt(s.length() - 1) != c) {
return s + c;
}
return s;
}
@NotNull
private static String stripLeading(@NotNull String s, char c) {
if (s.length() < 2) {
return s;
}
if (s.charAt(0) == c) {
return s.substring(1);
}
return s;
}
@NotNull
private static String stripTrailing(@NotNull String s, char c) {
if (s.isEmpty()) {
return s;
}
if (s.charAt(s.length() - 1) == c) {
return s.substring(0, s.length() - 1);
}
return s;
}
@NotNull
static String stripLeadingAndTrailing(@NotNull String s, char c) {
String leadingStripped = MBRSetResponseGenerator.stripLeading(s, c);
return MBRSetResponseGenerator.stripTrailing(leadingStripped, c);
}
public static boolean isExtensionSupported(@NotNull String ext) {
if (ext.equalsIgnoreCase(".m3u8")) {
return true;
}
if (ext.equalsIgnoreCase(".f4m")) {
return true;
}
return false;
}
@NotNull
public static String deriveQueryPathFromExt(@NotNull String ext) {
if (ext.equalsIgnoreCase(".m3u8")) {
return "req=mbrset&fmt=m3u8";
}
if (ext.equalsIgnoreCase(".f4m")) {
return "req=mbrset&fmt=f4m";
}
return "";
}
}