VideoTestServlet.java
10.8 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.PathNotFoundException
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.ValueFormatException
* javax.servlet.ServletException
* org.apache.commons.io.FileUtils
* org.apache.commons.io.IOUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.commons.JcrUtils
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.video.servlet;
import com.day.cq.dam.handler.ffmpeg.ExecutableLocator;
import com.day.cq.dam.handler.ffmpeg.FFMpegWrapper;
import com.day.cq.dam.handler.ffmpeg.FfmpegNotFoundException;
import com.day.cq.dam.video.VideoProfile;
import com.day.cq.dam.video.servlet.VideoTestResult;
import java.awt.Dimension;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Calendar;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFormatException;
import javax.servlet.ServletException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"dam/components/video/profile"}), @Property(name="sling.servlet.extensions", value={"json"}), @Property(name="sling.servlet.selectors", value={"transcode.test"}), @Property(name="sling.servlet.methods", value={"GET"}), @Property(name="service.description", value={"Test a video profile."})})
public class VideoTestServlet
extends SlingAllMethodsServlet {
private static final long serialVersionUID = -780464494271946961L;
private static final String NN_TEST = "test";
private static final String PROPERTY_INPUT_FILE_NAME = "inputName";
private static final String PROPERTY_OUTPUT_FILE_NAME = "outputName";
private static final String PROPERTY_INPUT_DURATION = "inputDuration";
private static final String PROPERTY_INPUT_SIZE = "inputSize";
private static final String PROPERTY_TEST_LOG = "log";
private static final String PROPERTY_TEST_SUCCESS = "success";
private static final String PROPERTY_TEST_LAST_RUN = "lastRun";
private static final String NODE_INPUT_FILE = "input";
private static final String NODE_OUTPUT_FILE = "output";
private static final String TMP_DIR = "videotest";
private final Logger log = LoggerFactory.getLogger(VideoTestServlet.class);
@Reference(policy=ReferencePolicy.STATIC)
protected ExecutableLocator locator;
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
File tmpDir = null;
try {
tmpDir = File.createTempFile("videotest", null);
tmpDir.delete();
tmpDir.mkdir();
VideoProfile videoProfile = new VideoProfile(request.getResource());
Node testNode = this.getTestNode(videoProfile);
VideoTestResult testResult = this.runTest(videoProfile, testNode, tmpDir);
this.storeTestResult(testNode, testResult);
this.writeTestResult(response, testResult);
}
catch (JSONException e) {
throw new ServletException(e.getMessage(), (Throwable)e);
}
catch (FileNotFoundException e) {
throw new ServletException(e.getMessage(), (Throwable)e);
}
catch (RepositoryException e) {
throw new ServletException(e.getMessage(), (Throwable)e);
}
finally {
if (tmpDir != null) {
try {
FileUtils.deleteDirectory((File)tmpDir);
}
catch (IOException e) {
throw new ServletException(e.getMessage(), (Throwable)e);
}
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void storeTestResult(Node testNode, VideoTestResult testResult) throws RepositoryException, FileNotFoundException {
if (testResult.getOutput() != null) {
FileInputStream resultIs = null;
try {
resultIs = new FileInputStream(testResult.getOutput());
JcrUtils.putFile((Node)testNode, (String)"output", (String)testResult.getOutputMimeType(), (InputStream)resultIs);
}
finally {
IOUtils.closeQuietly((InputStream)resultIs);
}
}
if (testResult.getOutputName() != null) {
testNode.setProperty("outputName", testResult.getOutputName());
}
if (testResult.getInputDuration() > 0.0) {
testNode.setProperty("inputDuration", testResult.getInputDuration());
}
if (testResult.getInputSize() != null) {
testNode.setProperty("inputSize", this.formatInputSize(testResult.getInputSize()));
}
if (testResult.getFfmpegLog() != null) {
testNode.setProperty("log", testResult.getFfmpegLog());
}
testNode.setProperty("success", testResult.isSuccess());
testNode.setProperty("lastRun", Calendar.getInstance());
testNode.getSession().save();
}
private String formatInputSize(Dimension inputSize) {
if (inputSize != null) {
return "" + inputSize.width + ":" + inputSize.height;
}
return "";
}
private void writeTestResult(SlingHttpServletResponse response, VideoTestResult testResult) throws IOException, JSONException {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
JSONWriter writer = new JSONWriter((Writer)response.getWriter());
writer.object();
writer.key("inputSize").value((Object)this.formatInputSize(testResult.getInputSize()));
writer.key("inputDuration").value(testResult.getInputDuration());
writer.key("outputName").value((Object)testResult.getOutputName());
writer.key("success").value(testResult.isSuccess());
writer.key("log").value((Object)testResult.getFfmpegLog());
writer.endObject();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private VideoTestResult runTest(VideoProfile videoProfile, Node testNode, File tmpDir) {
VideoTestResult result;
result = new VideoTestResult();
try {
String testVideoName = testNode.getProperty("inputName").getString();
javax.jcr.Property testVideoData = testNode.getProperty("input/jcr:content/jcr:data");
Binary binary = null;
InputStream is = null;
FFMpegWrapper wrapper = null;
FileOutputStream fos = null;
try {
binary = testVideoData.getBinary();
is = binary.getStream();
File tmpFile = new File(tmpDir, testVideoName);
fos = new FileOutputStream(tmpFile);
IOUtils.copy((InputStream)is, (OutputStream)fos);
wrapper = FFMpegWrapper.fromProfile(tmpFile, videoProfile, tmpDir);
wrapper.setExecutableLocator(this.locator);
File video = wrapper.transcode();
result.setOutput(video);
result.setOutputName(video.getName());
result.setOutputMimeType(wrapper.getOutputMimetype());
result.setInputDuration(wrapper.getInputDuration());
result.setInputSize(wrapper.getInputSize());
result.setFfmpegLog(wrapper.getFFMpegOutput().toString());
result.setSuccess(true);
}
catch (IOException e) {
this.log.error(e.getMessage(), (Throwable)e);
if (wrapper != null) {
result.setFfmpegLog(wrapper.getFFMpegOutput().toString());
}
}
finally {
IOUtils.closeQuietly((InputStream)is);
IOUtils.closeQuietly((OutputStream)fos);
if (binary != null) {
binary.dispose();
}
}
}
catch (FfmpegNotFoundException e) {
this.log.error(e.getMessage(), (Throwable)e);
result.setFfmpegLog("Error transcoding: " + e.getMessage());
}
catch (ValueFormatException e) {
this.log.error(e.getMessage(), (Throwable)e);
result.setFfmpegLog("Error transcoding: " + e.getMessage());
}
catch (PathNotFoundException e) {
this.log.error(e.getMessage(), (Throwable)e);
result.setFfmpegLog("Error transcoding: " + e.getMessage());
}
catch (RepositoryException e) {
this.log.error(e.getMessage(), (Throwable)e);
result.setFfmpegLog("Error transcoding: " + e.getMessage());
}
return result;
}
private Node getTestNode(VideoProfile videoProfile) throws RepositoryException {
Node profileNode = videoProfile.getContentNode();
if (profileNode.hasNode("test")) {
return profileNode.getNode("test");
}
return profileNode.addNode("test");
}
protected void bindLocator(ExecutableLocator executableLocator) {
this.locator = executableLocator;
}
protected void unbindLocator(ExecutableLocator executableLocator) {
if (this.locator == executableLocator) {
this.locator = null;
}
}
}