-
Notifications
You must be signed in to change notification settings - Fork 114
/
gui_play.cpp
470 lines (419 loc) · 12.1 KB
/
gui_play.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
/***************************************************************************
\file gui_play.cpp
\brief Playback loop
copyright : (C) 2001/2009 by mean
email : fixounet@free.fr
***************************************************************************/
/***************************************************************************
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "ADM_cpp.h"
#include "ADM_default.h"
#include <math.h>
#include "prefs.h"
#include "avi_vars.h"
#include "ADM_Video.h"
#include "ADM_edit.hxx"
#include "audio_out.h"
#include "DIA_coreToolkit.h"
#include "gtkgui.h"
#include "ADM_render/GUI_render.h"
#include "ADM_preview.h"
#include "audiofilter.h"
#include "ADM_filterChain.h"
#include "GUI_ui.h"
#include "ADM_coreUtils.h"
#include "ADM_vidMisc.h"
//___________________________________
// In 10 ms chunks
#define AUDIO_PRELOAD 100
#define EVEN(x) (x&0xffffffe)
//___________________________________
uint8_t stop_req;
extern ADM_Composer *video_body;
static uint32_t originalPriority;
/**
\class GUIPlayback
\brief Wrapper for the playback stuff
*/
class GUIPlayback
{
private:
AUDMAudioFilter *playbackAudio;
Clock ticktock;
uint32_t nbSamplesSent ;
float *wavbuf ;
uint64_t firstPts,lastPts;
uint64_t vuMeterPts;
uint64_t audioLatency;
ADM_coreVideoFilter *videoFilter;
ADM_videoFilterChain *videoChain;
private :
bool initialized;
bool initializeAudio();
bool cleanup(void);
bool audioPump(bool wait);
bool cleanupAudio(void);
bool updateVu(void);
public:
bool run(void);
bool initialize(void);
GUIPlayback();
~GUIPlayback();
};
/**
\fn GUIPlayback
*/
GUIPlayback::GUIPlayback(void)
{
playbackAudio=NULL;
wavbuf=NULL;
videoFilter=NULL;
videoChain=NULL;
initialized=false;
vuMeterPts=0;
audioLatency=0;
}
/**
\fn ~GUIPlayback
*/
GUIPlayback::~GUIPlayback()
{
cleanup();
}
/**
\fn GUI_PlayAvi
\brief MainLoop for internal movie playback
*/
void GUI_PlayAvi(void)
{
uint32_t framelen,flags;
uint32_t max,err;
uint64_t oldTimeFrame;
aviInfo info;
renderZoom oldZoom=admPreview::getCurrentZoom();
video_body->getVideoInfo(&info);
// check we got everything...
if (!video_body->getNbSegment()) return;
if (playing)
{
stop_req = 1;
return;
}
oldTimeFrame=admPreview::getCurrentPts();
uint32_t priorityLevel;
#ifndef __HAIKU__
originalPriority = getpriority(PRIO_PROCESS, 0);
prefs->get(PRIORITY_PLAYBACK,&priorityLevel);
setpriority(PRIO_PROCESS, 0, ADM_getNiceValue(priorityLevel));
#endif
stop_req = 0;
playing = 1;
admPreview::deferDisplay(true);
//admPreview::samePicture();
GUIPlayback *playLoop=new GUIPlayback;
playLoop->initialize();
playLoop->run();
delete playLoop;
playing = 0;
admPreview::deferDisplay(false);
// Resize the output window to original size...
ADM_info("Restoring display.\n");
admPreview::setMainDimension(info.width,info.height,oldZoom);
// If we are processing the video, the current time
// might not be matching a source video time => PROBLEM
// Go back to the beginning to be on safe ground
// In copy mode, we can keep the current position
if(getPreviewMode()!=ADM_PREVIEW_NONE)
admPreview::seekToTime(oldTimeFrame);
UI_purge();
admPreview::samePicture();
GUI_setCurrentFrameAndTime();
UI_purge();
}
/**
\fn cleanupAudio
*/
bool GUIPlayback::cleanupAudio(void)
{
if (wavbuf)
ADM_dealloc(wavbuf);
wavbuf=NULL;
AVDM_AudioClose();
if (playbackAudio)
{
destroyPlaybackFilter();
playbackAudio=NULL;
}
return true;
}
/**
\fn cleanupAudio
*/
bool GUIPlayback::cleanup(void)
{
ADM_info("Cleaning up..\n");
cleanupAudio();
if(videoChain)
{
ADM_info("Destroying video playback chain\n");
destroyVideoFilterChain(videoChain);
videoChain=NULL;
}
videoFilter=NULL; // it has been destroyed by the chain
// done.
#ifndef __HAIKU__
setpriority(PRIO_PROCESS, 0, originalPriority);
#endif
return true;
}
/**
\fn initialize
*/
bool GUIPlayback::initialize(void)
{
firstPts=admPreview::getCurrentPts();
if(getPreviewMode()==ADM_PREVIEW_NONE) // copy
{
videoChain=createEmptyVideoFilterChain(firstPts,1000*1000*1000*1000LL);
}else
{
videoChain=createVideoFilterChain(firstPts,1000*1000*1000*1000LL);
}
if(!videoChain)
{
ADM_warning("Cannot create video chain\n");
return false;
}
int nb=videoChain->size();
videoFilter=(*videoChain)[nb-1];
FilterInfo *info=videoFilter->getInfo();
renderZoom currentZoom=admPreview::getCurrentZoom();
renderZoom zoom=ZOOM_AUTO;
// if the video output has same width/height as input, we keep the same zoom
aviInfo originalVideo;
video_body->getVideoInfo(&originalVideo);
if(info->width==originalVideo.width && info->height==originalVideo.height)
zoom=currentZoom;
//
admPreview::setMainDimension(info->width,info->height,zoom);
initializeAudio();
return true;
}
/**
\fn run
*/
bool GUIPlayback::run(void)
{
uint32_t movieTime;
uint32_t systemTime;
uint32_t fn;
int refreshCounter=0;
ticktock.reset();
vuMeterPts=0;
ADMImage *previewBuffer=admPreview::getBuffer();
ADM_HW_IMAGE hwImageFormat=admPreview::getPreferedHwImageFormat();
do
{
admPreview::displayNow();;
GUI_setCurrentFrameAndTime(firstPts);
if(false==videoFilter->getNextFrameAs(hwImageFormat,&fn,previewBuffer))
{
printf("[Play] Cancelling playback, nextPicture failed\n");
break;
}
audioPump(false);
lastPts=admPreview::getCurrentPts();
systemTime = ticktock.getElapsedMS();
movieTime=(uint32_t)((lastPts-firstPts*0)/1000);
movieTime+=audioLatency;
//printf("[Playback] systemTime: %lu movieTime : %lu audioLatency=%lu \r",systemTime,movieTime,(uint32_t)audioLatency);
//printf("[LastPTS]=%s \n",ADM_us2plain(lastPts));
//printf("[firstPts]=%s \n",ADM_us2plain(firstPts));
if(systemTime>movieTime) // We are late, the current PTS is after current closk
{
if(systemTime >movieTime+20)
{
refreshCounter++;
}
if(refreshCounter>15)
{
UI_purge();
UI_purge();
refreshCounter=0;
}
}
else
{
int32_t delta;
delta=(int32_t)movieTime-(int32_t)systemTime;
// a call to whatever sleep function will last at leat 10 ms
// give some time to GTK
while(delta > 10)
{
if(stop_req)
break;
if(delta>10)
{
if(playbackAudio)
audioPump(true);
else
ADM_usleep(10*1000);
}else
audioPump(false);
UI_purge();
refreshCounter=0;
systemTime = ticktock.getElapsedMS();
delta=(int32_t)movieTime-(int32_t)systemTime;
}
}
}
while (!stop_req);
abort_play:
return true;
};
/**
\fn audioPump
\brief send ~ worth of one video frame of audio
*/
bool GUIPlayback::audioPump(bool wait)
{
uint32_t oaf = 0;
uint32_t load = 0;
uint8_t channels;
uint32_t fq;
static bool errorMet=false;
if (!playbackAudio) return false;
channels= playbackAudio->getInfo()->channels;
fq=playbackAudio->getInfo()->frequency;
uint32_t slice=(fq * channels)/50; // 20 ms
if(AVDM_getMsFullness() >= AUDIO_PRELOAD)
{
if(wait==true) GUI_Sleep(10);
updateVu();
return true;
}
while(AVDM_getMsFullness() < AUDIO_PRELOAD)
{
AUD_Status status;
if (! (oaf = playbackAudio->fill(slice, wavbuf,&status)))
{
if(errorMet==false)
ADM_warning("[Playback] Error reading audio stream...\n");
errorMet=true;
break;
}
errorMet=false;
AVDM_AudioPlay(wavbuf, oaf);
nbSamplesSent += oaf/channels;
load+=oaf;
}
updateVu();
return true;
}
/**
\fn initializeAudio
\brief Initialize audio
*/
bool GUIPlayback::initializeAudio(void)
{
uint32_t state,latency, preload;
uint32_t small_;
uint32_t channels,frequency;
wavbuf = 0;
uint64_t startPts=firstPts;
int32_t shift=0; // unit is ms, + => delay audio, -=> advance audio
// if audio shift is activated, take it into account
//
EditableAudioTrack *ed=video_body->getDefaultEditableAudioTrack();
if(!ed)
return false;
if(ed->audioEncodingConfig.shiftEnabled)
shift=ed->audioEncodingConfig.shiftInMs;
playbackAudio = createPlaybackFilter(startPts,shift);
if(!playbackAudio)
{
ADM_info("No audio\n");
return false;
}
channels= playbackAudio->getInfo()->channels;
frequency=playbackAudio->getInfo()->frequency;
preload= (frequency * channels)/5; // 200 ms preload
// 4 sec buffer..
wavbuf = (float *) ADM_alloc((20*sizeof(float)*preload)); // 4 secs buffers
ADM_assert(wavbuf);
// Read a at least one block to have the proper channel mapping
uint32_t fill=0;
AUD_Status status;
small_ = playbackAudio->fill(channels, wavbuf,&status);
fill+=small_;
// Call it twice to be sure it is properly setup
state = AVDM_AudioSetup(frequency, channels ,playbackAudio->getChannelMapping());
AVDM_AudioClose();
state = AVDM_AudioSetup(frequency, channels ,playbackAudio->getChannelMapping());
latency=AVDM_GetLayencyMs();
printf("[Playback] Latency : %d ms\n",latency);
audioLatency=latency; // ms -> us
if (!state)
{
GUI_Error_HIG(QT_TRANSLATE_NOOP("adm","Trouble initializing audio device"), NULL);
cleanupAudio();
return false;
}
while(fill<preload)
{
if (!(small_ = playbackAudio->fill(preload-fill, wavbuf+fill,&status)))
{
break;
}
fill+=small_;
}
nbSamplesSent = fill/channels; // In sample
AVDM_AudioPlay(wavbuf, fill);
// Let audio latency sets in...
ticktock.reset();
uint32_t slice=(frequency * channels)/100; // 10 ms
// pump data until latency is over
updateVu();
#if 0
while(ticktock.getElapsedMS()<latency)
{
if(AVDM_getMsFullness()<AUDIO_PRELOAD)
{
if (!(small_ = playbackAudio->fill(slice, wavbuf,&status)))
{
printf("[Playback] Compensating for latency failed\n");
break;
}
AVDM_AudioPlay(wavbuf, slice);
}
ADM_usleep(10*1000);
updateVu();
}
#endif
printf("[Playback] Latency is now %u\n",ticktock.getElapsedMS());
return true;
}
/**
\fn updateVu
*/
bool GUIPlayback::updateVu(void)
{
uint64_t time= ticktock.getElapsedMS();
// Refresh vumeter every 50 ms
if(!playbackAudio) return true;
if(time>(vuMeterPts+50))
{
uint32_t stat[6];
AVDM_getStats(stat);
UI_setVUMeter(stat);
vuMeterPts=time;
}
return true;
}
// EOF