forked from carlonluca/pot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
omx_videoprocessor.cpp
523 lines (455 loc) · 19.4 KB
/
omx_videoprocessor.cpp
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
* Project: PiOmxTextures
* Author: Luca Carlon
* Date: 12.16.2012
*
* Copyright (c) 2012 Luca Carlon. All rights reserved.
*
* This file is part of PiOmxTextures.
*
* PiOmxTextures is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PiOmxTextures is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PiOmxTextures. If not, see <http://www.gnu.org/licenses/>.
*/
/*------------------------------------------------------------------------------
| includes
+-----------------------------------------------------------------------------*/
#include <QFile>
#include <QGLWidget>
#include <IL/OMX_Broadcom.h>
#include "omx_videoprocessor.h"
#include "OMXComponent.h"
#include "OMX_Core.h"
#include "omxtunnel.h"
#include "lgl_logging.h"
/*------------------------------------------------------------------------------
| definitions
+-----------------------------------------------------------------------------*/
using namespace std;
#define TIMEOUT_MS 10000
/*------------------------------------------------------------------------------
| MyEGLRenderer class
+-----------------------------------------------------------------------------*/
class MyEGLRenderer : public OMXComponent
{
protected:
OMX_ERRORTYPE fillBufferDone(OMX_BUFFERHEADERTYPE* pBuffer) {
assert(pBuffer);
if (pBuffer->nFlags & OMX_BUFFERFLAG_EOS)
return OMX_ErrorNone;
LOG_VERBOSE(LOG_TAG, "FillBufferDoneCallback: %ld!", pBuffer->nFilledLen);
FillThisBuffer(pBuffer);
return OMX_ErrorNone;
}
};
typedef std::tr1::shared_ptr<MyEGLRenderer> MyEGLRendererShared;
/*------------------------------------------------------------------------------
| OMX_VideoProcessor::OMX_VideoProcessor
+-----------------------------------------------------------------------------*/
OMX_VideoProcessor::OMX_VideoProcessor(
EGLDisplay eglDisplay,
EGLContext eglContext,
OMX_TextureProvider* provider,
QObject* parent
) :
QObject(parent),
m_state(OMX_VideoProcessor::STATE_INACTIVE),
m_pendingCmd(0),
m_texture(eglDisplay),
m_provider(provider),
m_eglDisplay(eglDisplay),
m_eglContext(eglContext),
m_eglBuffer(NULL),
m_playLock(0)
{
// I want the object to live in a separate thread. This makes it
// possible to decode without blocking. The other methods are supposed
// to be called in any thread different than this (the event loop
// will remain busy).
moveToThread(&m_thread);
m_thread.start();
}
/*------------------------------------------------------------------------------
| OMX_VideoProcessor::~OMX_VideoProcessor
+-----------------------------------------------------------------------------*/
OMX_VideoProcessor::~OMX_VideoProcessor()
{
m_thread.quit();
m_thread.wait();
}
/*------------------------------------------------------------------------------
| OMX_VideoProcessor::setVideoPath
+-----------------------------------------------------------------------------*/
bool OMX_VideoProcessor::setVideoPath(const QString& videoAbsolutePath)
{
// Stop video playback and reset video about to be played. The stop
// method is blocking, so no need to use m_pendingCmd here (also I
// can't because that is used by stop()).
QMutexLocker locker(&m_sendCmd);
if (!checkCurrentThread())
return false;
// Validate state.
switch (m_state) {
case STATE_INACTIVE:
case STATE_STOPPED:
break;
case STATE_PAUSED:
case STATE_PLAYING:
return false;
}
m_videoAbsolutePath = videoAbsolutePath;
m_state = STATE_STOPPED;
return true;
}
/*------------------------------------------------------------------------------
| OMX_VideoProcessor::play
+-----------------------------------------------------------------------------*/
bool OMX_VideoProcessor::play()
{
// I need to invoke this in another thread (this object is owned by another
// thread).
QMutexLocker locker(&m_sendCmd);
if (!checkCurrentThread())
return false;
switch (m_state) {
case STATE_INACTIVE:
return false;
case STATE_PAUSED:
m_playLock.release();
m_state = STATE_PLAYING;
return true;
case STATE_PLAYING:
return true;
case STATE_STOPPED:
m_state = STATE_PLAYING;
return QMetaObject::invokeMethod(this, "videoDecoding");
default:
return false;
}
}
/*------------------------------------------------------------------------------
| OMX_VideoProcessor::stop
+-----------------------------------------------------------------------------*/
bool OMX_VideoProcessor::stop()
{
QMutexLocker locker(&m_sendCmd);
if (!checkCurrentThread())
return false;
switch (m_state) {
case STATE_INACTIVE:
return true;
case STATE_PAUSED:
// TODO: What here?
return false;
case STATE_PLAYING:
m_state = STATE_STOPPED;
m_pendingCmd.acquire();
return true;
case STATE_STOPPED:
return true;
}
return true;
}
/*------------------------------------------------------------------------------
| OMX_VideoProcessor::pause
+-----------------------------------------------------------------------------*/
bool OMX_VideoProcessor::pause()
{
QMutexLocker locker(&m_sendCmd);
if (!checkCurrentThread())
return false;
switch (m_state) {
case STATE_INACTIVE:
return false;
case STATE_PAUSED:
return true;
case STATE_PLAYING:
m_state = STATE_PAUSED;
m_pendingCmd.acquire();
return true;
case STATE_STOPPED:
return false;
}
return true;
}
/*------------------------------------------------------------------------------
| OMX_VideoProcessor::checkCurrentThread
+-----------------------------------------------------------------------------*/
inline
bool OMX_VideoProcessor::checkCurrentThread()
{
if (QThread::currentThreadId() == m_thread.getThreadId()) {
LOG_ERROR(LOG_TAG, "Do not invoke in the object's thread!");
return false;
}
return true;
}
/*------------------------------------------------------------------------------
| OMX_VideoProcessor::checkCurrentThread
+-----------------------------------------------------------------------------*/
void OMX_VideoProcessor::videoDecoding()
{
try {
doVideoDecoding();
}
catch (const runtime_error& e) {
LOG_ERROR(LOG_TAG, "Decoding error: %s.", e.what());
emit playbackFailed(OMX_VideoProcessor::ERROR_FAILED_DECODING);
}
}
/*------------------------------------------------------------------------------
| OMX_VideoProcessor::doVideoDecoding
+-----------------------------------------------------------------------------*/
void OMX_VideoProcessor::doVideoDecoding()
{
OMX_VIDEO_PARAM_PORTFORMATTYPE format;
OMX_TIME_CONFIG_CLOCKSTATETYPE cstate;
// Try to open file. Destroying the object results in closing the file.
QFile file(m_videoAbsolutePath);
if (!file.open(QIODevice::ReadOnly)) {
emit playbackFailed(OMX_VideoProcessor::ERROR_RESOURCE_MISSING);
return;
}
// Get core.
// TODO: Init of core must be changed.
OMX_Core* pCore = OMX_Core::instance();
if (!pCore) {
emit playbackFailed(OMX_VideoProcessor::ERROR_FAILED_DECODING);
return;
}
unsigned int data_len = 0;
int packet_size = 16 << 10;
// Create the components.
MyEGLRendererShared compEGLRender = OMXComponentFactory<MyEGLRenderer>::getInstance(
"OMX.broadcom.egl_render"
);
OMXComponentShared compDecoder = OMXComponentFactory<OMXComponent>::getInstance(
"OMX.broadcom.video_decode"
);
OMXComponentShared compClock = OMXComponentFactory<OMXComponent>::getInstance(
"OMX.broadcom.clock"
);
OMXComponentShared compScheduler = OMXComponentFactory<OMXComponent>::getInstance(
"OMX.broadcom.video_scheduler"
);
if (!(compEGLRender && compDecoder && compClock && compScheduler)) {
emit playbackFailed(OMX_VideoProcessor::ERROR_FAILED_DECODING);
return;
}
// Disable all ports. It is necessary to do it here because otherwise
// I'll not be able to make the components switch to IDLE state.
LOG_VERBOSE(LOG_TAG, "Disabling all the ports...");
compEGLRender->sendCommand(OMX_CommandPortDisable, 220, NULL);
compEGLRender->waitForEvent(OMX_EventCmdComplete, OMX_CommandPortDisable, 220, TIMEOUT_MS);
compEGLRender->sendCommand(OMX_CommandPortDisable, 221, NULL);
compEGLRender->waitForEvent(OMX_EventCmdComplete, OMX_CommandPortDisable, 221, TIMEOUT_MS);
compDecoder->sendCommand(OMX_CommandPortDisable, 130, NULL);
compDecoder->waitForEvent(OMX_EventCmdComplete, OMX_CommandPortDisable, 130, TIMEOUT_MS);
compDecoder->sendCommand(OMX_CommandPortDisable, 131, NULL);
compDecoder->waitForEvent(OMX_EventCmdComplete, OMX_CommandPortDisable, 131, TIMEOUT_MS);
for (int i = 80; i <= 85; i++) {
compClock->sendCommand(OMX_CommandPortDisable, i, NULL);
compClock->waitForEvent(OMX_EventCmdComplete, OMX_CommandPortDisable, i, TIMEOUT_MS);
}
for (int i = 10; i <= 12; i++) {
compScheduler->sendCommand(OMX_CommandPortDisable, i, NULL);
compScheduler->waitForEvent(OMX_EventCmdComplete, OMX_CommandPortDisable, i, TIMEOUT_MS);
}
LOG_VERBOSE(LOG_TAG, "All the ports were disabled.");
// Setup clock.
LOG_VERBOSE(LOG_TAG, "Setting params.");
memset(&cstate, 0, sizeof(cstate));
cstate.nSize = sizeof(cstate);
cstate.nVersion.nVersion = OMX_VERSION;
cstate.eState = OMX_TIME_ClockStateWaitingForStartTime;
cstate.nWaitMask = 1;
compClock->SetParameter(OMX_IndexConfigTimeClockState, &cstate);
// Setup the tunnels.
OMXTunnel tunnelDecSched(
compDecoder.get(),
OMXCompVideoDecoder::outputPort,
compScheduler.get(),
OMXCompVideoScheduler::inputVideoPort
);
OMXTunnel tunnelSchedRender(
compScheduler.get(),
OMXCompVideoScheduler::outputPort,
compEGLRender.get(),
220
);
OMXTunnel tunnelClockSched(
compClock.get(),
80,
compScheduler.get(),
OMXCompVideoScheduler::inputClockPort
);
LOG_VERBOSE(LOG_TAG, "Setting up tunnel clock/scheduler.");
tunnelClockSched.setupTunnel(0, 0);
compClock->sendCommand(OMX_CommandStateSet, OMX_StateExecuting, NULL);
compDecoder->sendCommand(OMX_CommandStateSet, OMX_StateIdle, NULL);
LOG_VERBOSE(LOG_TAG, "Setting input parameters.");
memset(&format, 0, sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE));
format.nSize = sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE);
format.nVersion.nVersion = OMX_VERSION;
format.nPortIndex = 130;
format.eCompressionFormat = OMX_VIDEO_CodingAVC;
compDecoder->SetParameter(OMX_IndexParamVideoPortFormat, &format);
OMX_BUFFERHEADERTYPE *buf;
int port_settings_changed = 0;
int first_packet = 1;
bool fillCalled = false;
// Provide buffers and enabled the port.
compDecoder->enablePortBuffers(130);
LOG_VERBOSE(LOG_TAG, "Changing decoder state to executing.");
compDecoder->sendCommand(OMX_CommandStateSet, OMX_StateExecuting, NULL);
qint64 bytesRead = 0;
bool releaseOnExit = false; /* Set to true in case I need to release a pending command. */
while (compDecoder->waitForInputBufferReady(130, buf)) {
// Check current state.
if (m_state == OMX_VideoProcessor::STATE_INACTIVE)
assert(false);
else if (m_state == OMX_VideoProcessor::STATE_PAUSED) {
// Release the pending command semaphore and wait on semaphore for
// a play or a stop.
m_pendingCmd.release();
m_playLock.acquire();
}
else if (m_state == OMX_VideoProcessor::STATE_PLAYING) {
// Do nothing.
}
else if (m_state == OMX_VideoProcessor::STATE_STOPPED) {
releaseOnExit = true;
break;
}
LOG_VERBOSE(LOG_TAG, "Filling buffer with data.");
// feed data and wait until we get port settings changed
unsigned char* dest = buf->pBuffer;
data_len += file.read((char*)dest, packet_size - data_len);
bytesRead += data_len;
LOG_VERBOSE(LOG_TAG, "Read: %u.", data_len);
if (port_settings_changed == 0 && compDecoder->waitForEvent(OMX_EventPortSettingsChanged, 131, 0, -1)) {
LOG_VERBOSE(LOG_TAG, "Port settings changed event thrown!");
port_settings_changed = 1;
tunnelDecSched.setupTunnel(0, TIMEOUT_MS);
LOG_VERBOSE(LOG_TAG, "Setting scheduler to EXECUTING...");
compScheduler->sendCommand(OMX_CommandStateSet, OMX_StateExecuting, NULL);
compScheduler->waitForEvent(OMX_EventCmdComplete, OMX_CommandStateSet, OMX_StateExecuting, TIMEOUT_MS);
// video_render is in LOADED here.
// now setup tunnel to video_render
LOG_VERBOSE(LOG_TAG, "Setting up scheduler -> renderer tunnel...");
tunnelSchedRender.setupTunnel(0, TIMEOUT_MS);
// video_render is in IDLE here.
// Query output buffer requirements for renderer and provide the native display
// the renderer will use.
OMX_PARAM_PORTDEFINITIONTYPE portdef;
portdef.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
portdef.nVersion.nVersion = OMX_VERSION;
portdef.nPortIndex = 221;
compEGLRender->GetParameter(OMX_IndexParamPortDefinition, &portdef);
portdef.format.video.pNativeWindow = m_eglDisplay;
compEGLRender->SetParameter(OMX_IndexParamPortDefinition, &portdef);
LOG_VERBOSE(LOG_TAG, "Waiting for EXECUTING state...");
compEGLRender->sendCommand(OMX_CommandStateSet, OMX_StateExecuting, NULL);
compEGLRender->waitForEvent(OMX_EventCmdComplete, OMX_CommandStateSet, OMX_StateExecuting, TIMEOUT_MS);
// Wait for port settings changed.
LOG_VERBOSE(LOG_TAG, "Waiting for port settings changed!");
compEGLRender->waitForEvent(OMX_EventPortSettingsChanged, 221, 0, TIMEOUT_MS);
// Enable output port of video_render.
LOG_VERBOSE(LOG_TAG, "Enabling output port of EGL renderer...");
compEGLRender->sendCommand(OMX_CommandPortEnable, 221, NULL);
// Instead of providing a buffer I provide the EGL image to use.
GLuint texture;
QMetaObject::invokeMethod(
m_provider,
"instantiateTexture",
Qt::BlockingQueuedConnection,
Q_RETURN_ARG(GLuint, texture),
Q_ARG(QSize, QSize(1920, 1080)));
m_texture.setTexture(eglImageVideo, texture, QSize(1920, 1080));
emit textureReady(texture);
LOG_VERBOSE(LOG_TAG, "Providing EGLImage: %x.", (unsigned int)eglImageVideo);
OMX_ERRORTYPE omxErr = OMX_UseEGLImage(compEGLRender->GetHandle(), &m_eglBuffer, 221, NULL, eglImageVideo);
if (omxErr != OMX_ErrorNone) {
LOG_ERROR(LOG_TAG, "OpenMAXILTextureLoader::decode - OMX_UseEGLImage - failed with omxErr(0x%x)\n", omxErr);
return;
}
compEGLRender->waitForEvent(OMX_EventCmdComplete, OMX_CommandPortEnable, 221, TIMEOUT_MS);
LOG_VERBOSE(LOG_TAG, "Port enabled!!!");
// video_render is in EXECUTING here.
}
// File is finished.
if (!data_len) {
LOG_VERBOSE(LOG_TAG, "Read finished.");
break;
}
buf->nFilledLen = data_len;
data_len = 0;
buf->nOffset = 0;
if (first_packet) {
buf->nFlags = OMX_BUFFERFLAG_STARTTIME;
first_packet = 0;
}
else
buf->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
LOG_VERBOSE(LOG_TAG, "Calling empty this buffer (%x)...", (unsigned int)buf);
compDecoder->EmptyThisBuffer(buf);
if (m_eglBuffer && !fillCalled) {
m_eglBuffer->nFilledLen = 0;
LOG_VERBOSE(LOG_TAG, "Calling FillThisBuffer for the first time.");
compEGLRender->FillThisBuffer(m_eglBuffer);
fillCalled = true;
}
}
// Send the EOS to the decoder and wait for it to come out of the renderer.
LOG_VERBOSE(LOG_TAG, "Rading file finished! Waiting for EOS...");
buf->nFilledLen = 0;
buf->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN | OMX_BUFFERFLAG_EOS;
assert(OMX_EmptyThisBuffer(compDecoder->GetHandle(), buf) == OMX_ErrorNone);
compEGLRender->waitForEvent(OMX_EventBufferFlag, 221, OMX_BUFFERFLAG_EOS, TIMEOUT_MS);
LOG_VERBOSE(LOG_TAG, "Flushing tunnels...");
tunnelDecSched.flush(TIMEOUT_MS);
tunnelSchedRender.flush(TIMEOUT_MS);
tunnelClockSched.flush(TIMEOUT_MS);
LOG_VERBOSE(LOG_TAG, "Disabling buffers...");
compDecoder->disablePortBuffers(130);
file.close();
LOG_VERBOSE(LOG_TAG, "Disabling tunnels...");
tunnelDecSched.disable(TIMEOUT_MS);
tunnelSchedRender.disable(TIMEOUT_MS);
tunnelClockSched.disable(TIMEOUT_MS);
LOG_VERBOSE(LOG_TAG, "Go to idle...");
compDecoder->sendCommand(OMX_CommandStateSet, OMX_StateIdle, NULL);
compClock->sendCommand(OMX_CommandStateSet, OMX_StateIdle, NULL);
compScheduler->sendCommand(OMX_CommandStateSet, OMX_StateIdle, NULL);
compEGLRender->sendCommand(OMX_CommandStateSet, OMX_StateIdle, NULL);
compDecoder->waitForEvent(OMX_EventCmdComplete, OMX_CommandStateSet, OMX_StateIdle, TIMEOUT_MS);
compClock->waitForEvent(OMX_EventCmdComplete, OMX_CommandStateSet, OMX_StateIdle, TIMEOUT_MS);
compScheduler->waitForEvent(OMX_EventCmdComplete, OMX_CommandStateSet, OMX_StateIdle, TIMEOUT_MS);
compEGLRender->waitForEvent(OMX_EventCmdComplete, OMX_CommandStateSet, OMX_StateIdle, TIMEOUT_MS);
// All the resources must be released.
LOG_VERBOSE(LOG_TAG, "Go to loaded...");
assert(OMX_FreeBuffer(compEGLRender->GetHandle(), 221, m_eglBuffer) == OMX_ErrorNone);
m_eglBuffer = 0;
compDecoder->sendCommand(OMX_CommandStateSet, OMX_StateLoaded, NULL);
compClock->sendCommand(OMX_CommandStateSet, OMX_StateLoaded, NULL);
compScheduler->sendCommand(OMX_CommandStateSet, OMX_StateLoaded, NULL);
compEGLRender->sendCommand(OMX_CommandStateSet, OMX_StateLoaded, NULL);
compDecoder->waitForEvent(OMX_EventCmdComplete, OMX_CommandStateSet, OMX_StateLoaded, TIMEOUT_MS);
compClock->waitForEvent(OMX_EventCmdComplete, OMX_CommandStateSet, OMX_StateLoaded, TIMEOUT_MS);
compScheduler->waitForEvent(OMX_EventCmdComplete, OMX_CommandStateSet, OMX_StateLoaded, TIMEOUT_MS);
compEGLRender->waitForEvent(OMX_EventCmdComplete, OMX_CommandStateSet, OMX_StateLoaded, TIMEOUT_MS);
LOG_INFORMATION(LOG_TAG, "Decoding done! Bye bye!");
if (releaseOnExit)
m_pendingCmd.release();
else
emit playbackFinished();
}