1 #include "ADM_inttype.h"
2 #include "DIA_coreToolkit.h"
3 
4 #include "Editor.h"
5 #include "FrameProperties.h"
6 #include "SegmentCollection.h"
7 #include "VideoDecoder.h"
8 #include "VideoFileProperties.h"
9 #include "AudioOutputCollectionPrototype.h"
10 #include "VideoFilterCollection.h"
11 #include "VideoFilterCollectionPrototype.h"
12 
13 #define VALIDATE_OPEN_VIDEO() \
14     if (!this->_editor->getVideoCount()) \
15     { \
16         return this->throwError(QString(QT_TR_NOOP("A video must be open to perform this operation."))); \
17     }
18 
19 namespace ADM_qtScript
20 {
Editor(QScriptEngine * engine,IEditor * editor,std::map<ADM_dynMuxer *,Muxer * > * muxers,std::map<ADM_videoEncoder6 *,VideoEncoder * > * videoEncoders)21     Editor::Editor(
22         QScriptEngine *engine, IEditor * editor, std::map<ADM_dynMuxer*, Muxer*>* muxers,
23         std::map<ADM_videoEncoder6*, VideoEncoder*>* videoEncoders) : QtScriptObject(editor)
24     {
25         this->_engine = engine;
26         this->_muxers = muxers;
27         this->_videoEncoders = videoEncoders;
28     }
29 
appendVideo(const QString & path)30     QScriptValue Editor::appendVideo(const QString& path)
31     {
32         if (!this->_editor->appendFile(path.toUtf8().constData()))
33         {
34             return this->throwError(QString(QT_TR_NOOP("Unable to append %1")).arg(path));
35         }
36 
37         return getVideoFileProperties(this->_editor->getVideoCount() - 1);
38     }
39 
clearMarkers()40     void Editor::clearMarkers()
41     {
42         this->_editor->setMarkerAPts(0);
43         this->_editor->setMarkerBPts(this->_editor->getVideoDuration());
44     }
45 
closeVideo()46     void Editor::closeVideo()
47     {
48         this->_editor->closeFile();
49     }
50 
getVideoFileProperties(int videoIndex)51     QScriptValue Editor::getVideoFileProperties(int videoIndex)
52     {
53         _VIDEOS *video = this->_editor->getRefVideo(videoIndex);
54 
55         return this->engine()->newQObject(
56                    new VideoFileProperties(this->_editor, video), QScriptEngine::ScriptOwnership);
57     }
58 
getVideoFileProperties()59     QScriptValue Editor::getVideoFileProperties()
60     {
61         int count = this->_editor->getVideoCount();
62 
63         if (count == 0)
64         {
65             return QScriptValue(QScriptValue::NullValue);
66         }
67         else
68         {
69             QScriptValue videos = this->engine()->newArray(count);
70 
71             for (int videoIndex = 0; videoIndex < count; videoIndex++)
72             {
73                 videos.setProperty(
74                     videoIndex, this->getVideoFileProperties(videoIndex));
75             }
76 
77             return videos;
78         }
79     }
80 
isVideoOpen(void)81     QScriptValue Editor::isVideoOpen(void)
82     {
83         return this->engine()->toScriptValue((bool)this->_editor->isFileOpen());
84     }
85 
openVideo(const QString & path)86     QScriptValue Editor::openVideo(const QString& path)
87     {
88         if (!this->_editor->openFile(path.toUtf8().constData()))
89         {
90             return this->throwError(QString(QT_TR_NOOP("Unable to open %1")).arg(path));
91         }
92 
93         return getVideoFileProperties(0);
94     }
95 
saveAudio(const QString & path,int audioIndex)96     QScriptValue Editor::saveAudio(const QString& path, int audioIndex)
97     {
98         VALIDATE_OPEN_VIDEO()
99 
100         if (!this->_editor->getRefVideo(0)->audioTracks.size())
101         {
102             return this->throwError(QString(QT_TR_NOOP("Video must contain an audio track to perform this operation.")));
103         }
104 
105         this->_editor->saveAudio(audioIndex, path.toUtf8().constData());
106 
107         return QScriptValue(QScriptValue::UndefinedValue);
108     }
109 
saveImage(const QString & path,ImageType imageType)110     QScriptValue Editor::saveImage(const QString& path, ImageType imageType)
111     {
112         VALIDATE_OPEN_VIDEO()
113 
114         switch (imageType)
115         {
116             case Editor::Bitmap:
117                 _editor->saveImageBmp(path.toUtf8().constData());
118                 break;
119 
120             case Editor::Jpeg:
121                 _editor->saveImageJpg(path.toUtf8().constData());
122                 break;
123         }
124 
125         return QScriptValue(QScriptValue::UndefinedValue);
126     }
127 
saveVideo(const QString & path)128     QScriptValue Editor::saveVideo(const QString& path)
129     {
130         VALIDATE_OPEN_VIDEO()
131 
132         this->_editor->saveFile(path.toUtf8().constData());
133 
134         return QScriptValue(QScriptValue::UndefinedValue);
135     }
136 
seekFrame(int frameCount,SeekFrameType seekFrameType)137     void Editor::seekFrame(int frameCount, SeekFrameType seekFrameType)
138     {
139         switch (seekFrameType)
140         {
141             case Editor::Intra:
142                 this->_editor->seekKeyFrame(frameCount);
143                 break;
144 
145             case Editor::Black:
146                 this->_editor->seekBlackFrame(frameCount);
147                 break;
148 
149             default:
150                 this->_editor->seekFrame(frameCount);
151                 break;
152         }
153     }
154 
seekNextFrame(QScriptValue frameCount,SeekFrameType seekFrameType)155     QScriptValue Editor::seekNextFrame(QScriptValue frameCount, SeekFrameType seekFrameType)
156     {
157         QScriptValue result = this->validateNumber("frameCount", frameCount, 1, INT_MAX);
158 
159         if (result.isUndefined())
160         {
161             this->seekFrame(frameCount.toNumber(), seekFrameType);
162         }
163 
164         return result;
165     }
166 
seekPreviousFrame(QScriptValue frameCount,SeekFrameType seekFrameType)167     QScriptValue Editor::seekPreviousFrame(QScriptValue frameCount, SeekFrameType seekFrameType)
168     {
169         QScriptValue result = this->validateNumber("frameCount", frameCount, 1, INT_MAX);
170 
171         if (result.isUndefined())
172         {
173             this->seekFrame(-frameCount.toNumber(), seekFrameType);
174         }
175 
176         return result;
177     }
178 
getAppliedVideoFilters(void)179     QScriptValue Editor::getAppliedVideoFilters(void)
180     {
181         if (this->_editor->isFileOpen())
182         {
183             return this->engine()->newObject(
184                        new VideoFilterCollection(
185                            this->engine(), this->_editor), QScriptEngine::ScriptOwnership);
186         }
187         else
188         {
189             return QScriptValue(QScriptValue::NullValue);
190         }
191     }
192 
getAudioOutputs(void)193     QScriptValue Editor::getAudioOutputs(void)
194     {
195         if (this->_editor->isFileOpen())
196         {
197             return this->engine()->newObject(
198                        new AudioOutputCollection(
199                            this->engine(), this->_editor), QScriptEngine::ScriptOwnership);
200         }
201         else
202         {
203             return QScriptValue(QScriptValue::NullValue);
204         }
205     }
206 
getCurrentFrameProperties(void)207     QScriptValue Editor::getCurrentFrameProperties(void)
208     {
209         if (this->_editor->isFileOpen())
210         {
211             return this->engine()->newQObject(
212                        new FrameProperties(
213                            this->_editor, this->_editor->getCurrentFramePts()), QScriptEngine::ScriptOwnership);
214         }
215         else
216         {
217             return QScriptValue(QScriptValue::NullValue);
218         }
219     }
220 
getCurrentTime(void)221     QScriptValue Editor::getCurrentTime(void)
222     {
223         return (double)this->_editor->getCurrentFramePts();
224     }
225 
getMarkerA(void)226     QScriptValue Editor::getMarkerA(void)
227     {
228         return (double)this->_editor->getMarkerAPts();
229     }
230 
getMarkerB(void)231     QScriptValue Editor::getMarkerB(void)
232     {
233         return (double)this->_editor->getMarkerBPts();
234     }
235 
getPosition(void)236     QScriptValue Editor::getPosition(void)
237     {
238         return (double)this->_editor->getCurrentFramePts();
239     }
240 
getSegments(void)241     QScriptValue Editor::getSegments(void)
242     {
243         if (this->_editor->isFileOpen())
244         {
245             return this->engine()->newObject(
246                        new SegmentCollection(this->engine(), this->_editor), QScriptEngine::ScriptOwnership);
247         }
248         else
249         {
250             return QScriptValue(QScriptValue::NullValue);
251         }
252     }
253 
getVideoCount(void)254     QScriptValue Editor::getVideoCount(void)
255     {
256         return this->_editor->getVideoCount();
257     }
258 
getVideoDecoders(void)259     QScriptValue Editor::getVideoDecoders(void)
260     {
261         int count = this->_editor->getVideoCount();
262 
263         if (count == 0)
264         {
265             return QScriptValue(QScriptValue::NullValue);
266         }
267         else
268         {
269             QScriptValue videos = this->engine()->newArray(count);
270 
271             for (int videoIndex = 0; videoIndex < count; videoIndex++)
272             {
273                 videos.setProperty(
274                     videoIndex, this->engine()->newQObject(
275                         new VideoDecoder(this->_engine, this->_editor, this->_editor->getRefVideo(videoIndex)),
276                         QScriptEngine::ScriptOwnership));
277             }
278 
279             return videos;
280         }
281     }
282 
setCurrentTime(QScriptValue time)283     QScriptValue Editor::setCurrentTime(QScriptValue time)
284     {
285         QScriptValue result = this->validateMarker("time", time);
286 
287         if (result.isUndefined())
288         {
289             this->_editor->setCurrentFramePts(time.toNumber());
290             result = time;
291         }
292 
293         return result;
294     }
295 
setMarkerA(QScriptValue time)296     QScriptValue Editor::setMarkerA(QScriptValue time)
297     {
298         QScriptValue result = this->validateMarker("time", time);
299 
300         if (result.isUndefined())
301         {
302             this->_editor->setMarkerAPts(time.toNumber());
303             result = time;
304         }
305 
306         return result;
307     }
308 
setMarkerB(QScriptValue time)309     QScriptValue Editor::setMarkerB(QScriptValue time)
310     {
311         QScriptValue result = this->validateMarker("time", time);
312 
313         if (result.isUndefined())
314         {
315             this->_editor->setMarkerBPts(time.toNumber());
316             result = time;
317         }
318 
319         return result;
320     }
321 
setMarkers(QScriptValue markerA,QScriptValue markerB)322     QScriptValue Editor::setMarkers(QScriptValue markerA, QScriptValue markerB)
323     {
324         QScriptValue result;
325 
326         result = this->validateMarker("markerA", markerA);
327 
328         if (result.isUndefined())
329         {
330             result = this->validateMarker("markerB", markerB);
331 
332             if (result.isUndefined())
333             {
334                 this->_editor->setMarkerAPts(markerA.toNumber());
335                 this->_editor->setMarkerBPts(markerB.toNumber());
336             }
337         }
338 
339         return result;
340     }
341 
setPosition(QScriptValue position)342     QScriptValue Editor::setPosition(QScriptValue position)
343     {
344         QScriptValue result = this->validateMarker("position", position);
345 
346         if (result.isUndefined())
347         {
348             this->_editor->setCurrentFramePts(position.toNumber());
349             result = QScriptValue((double)this->_editor->getCurrentFramePts());
350         }
351 
352         return result;
353     }
354 
validateMarker(const QString & parameterName,QScriptValue time)355     QScriptValue Editor::validateMarker(const QString& parameterName, QScriptValue time)
356     {
357         return this->validateNumber(parameterName, time, 0, this->_editor->getVideoDuration());
358     }
359 
getMuxer(void)360     QScriptValue Editor::getMuxer(void)
361     {
362         ADM_dynMuxer *muxerPlugin = this->_editor->getCurrentMuxer();
363 
364         return this->engine()->newQObject(
365                    this->_muxers->find(muxerPlugin)->second, QScriptEngine::ScriptOwnership);
366     }
367 
setMuxer(QScriptValue muxer)368     QScriptValue Editor::setMuxer(QScriptValue muxer)
369     {
370         Muxer *muxerObject = qobject_cast<Muxer*>(muxer.toQObject());
371 
372         if (muxerObject != NULL)
373         {
374             this->_editor->setContainer(muxerObject->muxerPlugin->name, NULL);
375 
376             return muxer;
377         }
378 
379         return this->engine()->undefinedValue();
380     }
381 
getVideoEncoder(void)382     QScriptValue Editor::getVideoEncoder(void)
383     {
384         ADM_videoEncoder6 *encoderPlugin = this->_editor->getCurrentVideoEncoder();
385 
386         return this->engine()->newQObject(
387                    this->_videoEncoders->find(encoderPlugin)->second, QScriptEngine::ScriptOwnership);
388     }
389 
setVideoEncoder(QScriptValue encoder)390     QScriptValue Editor::setVideoEncoder(QScriptValue encoder)
391     {
392         VideoEncoder *encoderObject = qobject_cast<VideoEncoder*>(encoder.toQObject());
393 
394         if (encoderObject != NULL)
395         {
396             this->_editor->setVideoCodec(encoderObject->encoderPlugin->desc->encoderName, NULL);
397             return encoder;
398         }
399 
400         return this->engine()->undefinedValue();
401     }
402 }
403