| 1 |
#include "aviplay_impl.h" |
| 2 |
#include "avm_cpuinfo.h" |
| 3 |
#include "avm_output.h" |
| 4 |
#include "avm_creators.h" |
| 5 |
#include "utils.h" |
| 6 |
|
| 7 |
#include <sys/time.h> //needed by sys/resource.h in FreeBSD |
| 8 |
#include <sys/resource.h> //set/getpriority |
| 9 |
#include <sys/ioctl.h> |
| 10 |
#include <errno.h> |
| 11 |
#include <fcntl.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> //exit |
| 14 |
#include <unistd.h> //close |
| 15 |
|
| 16 |
#ifdef __NetBSD__ |
| 17 |
#include <sys/sched.h> |
| 18 |
#else |
| 19 |
#include <sched.h> |
| 20 |
#endif |
| 21 |
|
| 22 |
// has to go with mga_vid |
| 23 |
#if HAVE_SYS_IOCCOM_H |
| 24 |
#include <sys/ioccom.h> |
| 25 |
#endif |
| 26 |
#include "mga_vid.h" |
| 27 |
|
| 28 |
|
| 29 |
AVM_BEGIN_NAMESPACE; |
| 30 |
|
| 31 |
// preffer video playing thread |
| 32 |
static const int PRIORITY_ADD_VIDEO = 0; |
| 33 |
static const int PRIORITY_ADD_AUDIO = 0; |
| 34 |
static const float m_fDropLimit = -0.315f; |
| 35 |
|
| 36 |
float AviPlayer::getVideoAsync() |
| 37 |
{ |
| 38 |
if (m_llTimeStart == 0) |
| 39 |
{ |
| 40 |
m_dFrameStart = (m_pVideostream) ? m_pVideostream->GetTime() : 0.; |
| 41 |
m_dLastFrameStart = m_dFrameStart; |
| 42 |
m_llTimeStart = avm_get_time_us(); |
| 43 |
AVM_WRITE("aviplay", 1, "AviPlayer::getVideoAsync(): resetting\n"); |
| 44 |
} |
| 45 |
|
| 46 |
if (!m_pVideostream) |
| 47 |
return 0.f; |
| 48 |
|
| 49 |
double actual_time; |
| 50 |
if (m_pAudioRenderer && !m_pAudioRenderer->Eof()) |
| 51 |
{ |
| 52 |
actual_time = m_pAudioRenderer->GetTime(); |
| 53 |
} |
| 54 |
else |
| 55 |
{ |
| 56 |
// for stream without audio track or when audio track has finished... |
| 57 |
// since we want to show real-time video we have to |
| 58 |
// compute async deviation from real time |
| 59 |
actual_time = m_dFrameStart + avm_get_time_diff(avm_get_time_us(), m_llTimeStart); |
| 60 |
} |
| 61 |
|
| 62 |
//printf("ATIM %f %f\n", m_pVideostream->GetTime(), actual_time); |
| 63 |
return (float) (m_pVideostream->GetTime() - actual_time); |
| 64 |
} |
| 65 |
|
| 66 |
/* |
| 67 |
* check for synchronization event |
| 68 |
* |
| 69 |
* might block this task - it will await for broadcast from the main thread |
| 70 |
* returns: 0 - quit |
| 71 |
* 1 - ok could continue |
| 72 |
*/ |
| 73 |
int AviPlayer::checkSync(ThreadId i) |
| 74 |
{ |
| 75 |
//printf("Thread: %d m_bHangup: %d\n", i, m_bHangup); |
| 76 |
if (!m_bQuit && m_bHangup) |
| 77 |
{ |
| 78 |
m_ThreadCond[i].Wait(m_ThreadMut[i]); |
| 79 |
AVM_WRITE("aviplay", 1, "Thread unlocked %d\n", i); |
| 80 |
} |
| 81 |
return (!m_bQuit); |
| 82 |
} |
| 83 |
|
| 84 |
/* |
| 85 |
* block all running threads - implemented as recursive lock |
| 86 |
*/ |
| 87 |
int AviPlayer::lockThreads(const char *name) |
| 88 |
{ |
| 89 |
Locker locker(m_LockMutex); |
| 90 |
|
| 91 |
if (m_bQuit) |
| 92 |
return -1; |
| 93 |
|
| 94 |
if (++m_iLockCount > 1) |
| 95 |
return 0; |
| 96 |
|
| 97 |
m_bHangup = true; |
| 98 |
AVM_WRITE("aviplay", 1, "Waiting for main_thread to hang up (%s)...", ((name) ? name : "???")); |
| 99 |
|
| 100 |
if (m_pVideostream) |
| 101 |
{ |
| 102 |
m_ThreadMut[THREAD_VIDEO].Lock(); |
| 103 |
AVM_WRITE("aviplay", 1, " video"); |
| 104 |
|
| 105 |
m_ThreadMut[THREAD_DECODER].Lock(); |
| 106 |
AVM_WRITE("aviplay", 1, " decoder"); |
| 107 |
} |
| 108 |
|
| 109 |
m_ThreadMut[THREAD_AUDIO].Lock(); |
| 110 |
m_QueueMutex.Lock(); |
| 111 |
|
| 112 |
AVM_WRITE("aviplay", 1, " audio"); |
| 113 |
|
| 114 |
syncFrame(); |
| 115 |
|
| 116 |
// now we have all threads locked |
| 117 |
m_bInitialized = false; |
| 118 |
|
| 119 |
AVM_WRITE("aviplay", 1, " OK!\n"); |
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
/* |
| 124 |
* unlock all waiting threads in cond_wait state |
| 125 |
*/ |
| 126 |
void AviPlayer::unlockThreads() |
| 127 |
{ |
| 128 |
Locker locker(m_LockMutex); |
| 129 |
|
| 130 |
if (--m_iLockCount > 0) |
| 131 |
return; |
| 132 |
|
| 133 |
// pausetest |
| 134 |
m_iLockCount = 0; |
| 135 |
m_llTimeStart = 0; |
| 136 |
m_bHangup = false; |
| 137 |
m_bInitialized = true; |
| 138 |
|
| 139 |
AVM_WRITE("aviplay", 1, "Unlock threads\n"); |
| 140 |
|
| 141 |
m_QueueCond.Broadcast(); |
| 142 |
m_QueueMutex.Unlock(); |
| 143 |
|
| 144 |
m_ThreadCond[THREAD_AUDIO].Broadcast(); |
| 145 |
m_ThreadMut[THREAD_AUDIO].Unlock(); |
| 146 |
|
| 147 |
if (m_pVideostream) |
| 148 |
{ |
| 149 |
m_ThreadCond[THREAD_DECODER].Broadcast(); |
| 150 |
m_ThreadMut[THREAD_DECODER].Unlock(); |
| 151 |
|
| 152 |
m_ThreadCond[THREAD_VIDEO].Broadcast(); |
| 153 |
m_ThreadMut[THREAD_VIDEO].Unlock(); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
// returns true when some frames have been dropped |
| 158 |
// called with m_QueueMutex LOCKED! |
| 159 |
bool AviPlayer::dropFrame() |
| 160 |
{ |
| 161 |
if (m_bVideoAsync || !m_bVideoDropping |
| 162 |
|| avm_get_time_diff(avm_get_time_us(), m_llTimeStart) < 0.3) |
| 163 |
return false; |
| 164 |
|
| 165 |
bool ret = false; |
| 166 |
// here we try to eliminate all the frames |
| 167 |
// until next key-frame with reasonable close time |
| 168 |
//printf("enter drop: async=%f\n", getVideoAsync()); |
| 169 |
framepos_t orig = m_pVideostream->GetPos(); |
| 170 |
framepos_t kpos = orig; |
| 171 |
bool locked = false; |
| 172 |
// do not skip frames if we have full buffer |
| 173 |
// in this case the problem is with slow rendering |
| 174 |
while (m_pVideostream->GetBuffering() <= 1) |
| 175 |
{ |
| 176 |
framepos_t npos = m_pVideostream->GetNextKeyFrame(kpos + 1); |
| 177 |
if (npos == m_pVideostream->ERR || kpos >= npos) |
| 178 |
break; |
| 179 |
double curtime = (m_pAudioRenderer) ? m_pAudioRenderer->GetTime() : |
| 180 |
m_dFrameStart + avm_get_time_diff(avm_get_time_us(), m_llTimeStart); |
| 181 |
double nt = m_pVideostream->GetTime(npos) - curtime; |
| 182 |
if (nt < 0.1) |
| 183 |
{ |
| 184 |
kpos = npos; |
| 185 |
continue; // next kf needed |
| 186 |
} |
| 187 |
if (nt > 0.3) |
| 188 |
npos = kpos; |
| 189 |
|
| 190 |
if (npos == orig) |
| 191 |
break; |
| 192 |
|
| 193 |
AVM_WRITE("aviplay", 1, "AviPlayer::dropFrame()\n" |
| 194 |
" async %f new_vtime: %f cur_atime: %f diff %f %d - %d\n", |
| 195 |
getVideoAsync(), m_pVideostream->GetTime(npos), curtime, nt, npos, kpos); |
| 196 |
|
| 197 |
//printf("o: %d k: %d n: %d\n", orig, kpos, npos); |
| 198 |
|
| 199 |
if (!locked) |
| 200 |
{ |
| 201 |
// stop decoder for proper frame skip |
| 202 |
m_bDropping = true; |
| 203 |
m_QueueMutex.Unlock(); |
| 204 |
m_ThreadMut[THREAD_DECODER].Lock(); |
| 205 |
m_QueueMutex.Lock(); |
| 206 |
m_bDropping = false; |
| 207 |
locked = true; |
| 208 |
} |
| 209 |
framepos_t posbef = m_pVideostream->GetPos(); |
| 210 |
|
| 211 |
kpos = m_pVideostream->SeekToKeyFrame(npos); |
| 212 |
if (kpos != npos) |
| 213 |
{ |
| 214 |
AVM_WRITE("aviplay", 1, "AviPlayer::dropFrame() logical fault compare %d %d %d %d " |
| 215 |
"before %d\n", npos, kpos, orig, m_pVideostream->GetPos(), posbef); |
| 216 |
//abort(); |
| 217 |
kpos = orig; |
| 218 |
} |
| 219 |
|
| 220 |
//if (nt > 0.1 || kpos >= npos || npos == m_pVideostream->ERR) |
| 221 |
break; |
| 222 |
} |
| 223 |
|
| 224 |
if (kpos > orig && kpos != m_pVideostream->ERR) |
| 225 |
{ |
| 226 |
// and finaly we we have keyframe which is |
| 227 |
// is not too ahead of audio |
| 228 |
AVM_WRITE("aviplay", 1, "AviPlayer::dropFrame() skipped %d frames ( %d, %d )\n", |
| 229 |
(kpos - orig), orig, kpos); |
| 230 |
while (orig++ < kpos) |
| 231 |
{ |
| 232 |
m_Drop.insert(100.0); |
| 233 |
m_iFrameDrop++; |
| 234 |
} |
| 235 |
ret = true; |
| 236 |
} |
| 237 |
//printf("DDD %d %f %d\n", ret, getVideoAsync(), m_pVideostream->GetBuffering()); |
| 238 |
if (!ret) |
| 239 |
{ |
| 240 |
uint_t bm = 2 - (m_bVideoBuffered == 0); |
| 241 |
while (m_pVideostream->GetBuffering() > bm |
| 242 |
&& getVideoAsync() < m_fDropLimit) |
| 243 |
{ |
| 244 |
CImage *im = m_pVideostream->GetFrame(!m_bVideoBuffered); |
| 245 |
if (!im) |
| 246 |
break; |
| 247 |
im->Release(); |
| 248 |
m_Drop.insert(100.0); |
| 249 |
m_iFrameDrop++; |
| 250 |
AVM_WRITE("aviplay", 1, "Dropped video frames: %d atime: %f vtime: %f bufs: %" PRIsz "\n", |
| 251 |
m_iFrameDrop, |
| 252 |
((m_pAudioRenderer) ? m_pAudioRenderer->GetTime() : 0.0), |
| 253 |
m_pVideostream->GetTime(), |
| 254 |
m_pVideostream->GetBuffering()); |
| 255 |
ret = true; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
if (locked) |
| 260 |
{ |
| 261 |
m_ThreadCond[THREAD_DECODER].Broadcast(); |
| 262 |
m_ThreadMut[THREAD_DECODER].Unlock(); |
| 263 |
} |
| 264 |
|
| 265 |
return ret; |
| 266 |
} |
| 267 |
|
| 268 |
void AviPlayer::setQuality() |
| 269 |
{ |
| 270 |
if (!m_bQualityAuto || m_iMaxAuto < 0) |
| 271 |
return; |
| 272 |
|
| 273 |
IRtConfig* rt = GetRtConfig(VIDEO_CODECS); |
| 274 |
if (rt) |
| 275 |
{ |
| 276 |
const char ppstr[] = "postprocessing"; |
| 277 |
if (CodecGetAttr(GetCodecInfo(), "maxauto", &m_iMaxAuto) < 0) |
| 278 |
return; |
| 279 |
|
| 280 |
int val; |
| 281 |
rt->GetValue(ppstr, &val); |
| 282 |
float as = getVideoAsync(); |
| 283 |
size_t bs; |
| 284 |
size_t bf = m_pVideostream->GetBuffering(&bs); |
| 285 |
|
| 286 |
//if (!m_dVframetime) |
| 287 |
//printf("As: %f Max %d PP: %d b: %d %f %f\n", as, m_iMaxAuto, val, bf, m_fDecodingTime, m_dVframetime); |
| 288 |
|
| 289 |
// some tricky checks here - |
| 290 |
// it must work with buffering or even just with one buffer |
| 291 |
// and should prevent large drops with high postprocessing levels |
| 292 |
if (val < m_iMaxAuto && as > m_fDropLimit |
| 293 |
&& (m_fDecodingTime < m_dVframetime * (1. - double(3 - bf) / 10.))) |
| 294 |
{ |
| 295 |
rt->SetValue(ppstr, val + 1); |
| 296 |
//printf("+++++++++++\n"); |
| 297 |
} |
| 298 |
else if (val > 0 |
| 299 |
&& ((m_fDecodingTime > m_dVframetime * ((bf >= 2) ? 1.1 : 0.7)) |
| 300 |
|| val > m_iMaxAuto |
| 301 |
|| as < m_fDropLimit) |
| 302 |
) |
| 303 |
{ |
| 304 |
//printf("As: %f Max %d PP: %d b: %d %f %f\n", as, m_iMaxAuto, val, bf, m_fDecodingTime, m_dVframetime); |
| 305 |
if (as < m_fDropLimit || m_fDecodingTime > m_dVframetime * ((bf < 2) ? 1.0 : 1.4)) |
| 306 |
val = 1; |
| 307 |
rt->SetValue(ppstr, val - 1); |
| 308 |
//printf("-----------\n"); |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
void* AviPlayer::videoThread() |
| 314 |
{ |
| 315 |
const ThreadId th = THREAD_VIDEO; |
| 316 |
m_ThreadMut[th].Lock(); |
| 317 |
AVM_WRITE("aviplay", 1, "Thread video started\n"); |
| 318 |
|
| 319 |
#ifdef SCHED_FIFO |
| 320 |
changePriority("Video player", 0, SCHED_FIFO); |
| 321 |
#endif |
| 322 |
int mga_vid = open("/dev/mga_vid", O_RDONLY); |
| 323 |
if (mga_vid == -1) |
| 324 |
mga_vid = open("/dev/misc/mga_vid", O_RDONLY); |
| 325 |
|
| 326 |
float minWait = 0.01f; |
| 327 |
float lastCopyTime = 0.f; |
| 328 |
int framecnt = 0; |
| 329 |
double dLastEofTime = 0.f; |
| 330 |
int lastirq = 0; |
| 331 |
m_llLastVideoSync = avm_get_time_us(); |
| 332 |
m_dLastFrameStart = m_pVideostream->GetTime(); |
| 333 |
|
| 334 |
if (mga_vid != -1) |
| 335 |
{ |
| 336 |
if (ioctl(mga_vid, MGA_VID_IRQ_ON, 0) < 0) |
| 337 |
//if (ioctl(mga_vid, MGA_VID_IRQ2_ON, 0) < 0) |
| 338 |
{ |
| 339 |
AVM_WRITE("aviplay", "Can't use MGA_VID device for VBI (vsync) synchronization (incompatible)\n"); |
| 340 |
// not supported |
| 341 |
close(mga_vid); |
| 342 |
mga_vid = -1; |
| 343 |
} |
| 344 |
else |
| 345 |
{ |
| 346 |
minWait = 0.02f; // better precision with interrupt handler |
| 347 |
AVM_WRITE("aviplay", "Using MGA_VID device for VBI (vsync) synchronization\n"); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
// video synchronization |
| 352 |
while (checkSync(th)) |
| 353 |
{ |
| 354 |
if (m_pVideostream->Eof()) |
| 355 |
{ |
| 356 |
if (m_dLastFrameStart != dLastEofTime) |
| 357 |
AVM_WRITE("aviplay", "Video stream eof\n"); |
| 358 |
m_Drop.clear(); |
| 359 |
dLastEofTime = m_dLastFrameStart; |
| 360 |
m_ThreadCond[th].Wait(m_ThreadMut[th], 1.0); |
| 361 |
continue; |
| 362 |
} |
| 363 |
|
| 364 |
float async = getVideoAsync(); |
| 365 |
//printf("V %d %f %f as:%f %d\n", m_pVideostream->Eof(), m_dLastFrameStart, dLastEofTime, async, (m_pAudioRenderer) ? m_pAudioRenderer->Eof() : 0); |
| 366 |
if (async > minWait) |
| 367 |
{ |
| 368 |
if (mga_vid >= 0) |
| 369 |
{ |
| 370 |
int buf[3]; |
| 371 |
read(mga_vid, &buf, sizeof(buf)); |
| 372 |
// always wait for at least one VBI event |
| 373 |
// if (getVideoAsync() <= minWait) { //AVM_WRITE("aviplay", "VID: Diff: %d %d async:%f\n", buf[0] - lastirq, buf[1], getVideoAsync()); lastirq = buf[0]; } |
| 374 |
} |
| 375 |
else |
| 376 |
{ |
| 377 |
if (async > 0.5f) |
| 378 |
async = 0.4f; // max sleep time |
| 379 |
|
| 380 |
int64_t t1 = avm_get_time_us(); |
| 381 |
m_ThreadCond[th].Wait(m_ThreadMut[th], async); |
| 382 |
float oversleep = avm_get_time_diff(avm_get_time_us(), t1); |
| 383 |
//printf("Oversleep %f a: %f\n", oversleep - async, async); |
| 384 |
m_fLastSleepTime += oversleep; |
| 385 |
if ((oversleep - async) > 0.016) |
| 386 |
{ |
| 387 |
AVM_WRITE("aviplay", 1, " Sleep too long!!! real: %f expected: %f async: %f over: %f\n", m_fLastSleepTime, async, getVideoAsync(), oversleep); |
| 388 |
} |
| 389 |
} |
| 390 |
continue; // let's check the status after sleep |
| 391 |
} |
| 392 |
m_QueueMutex.Lock(); |
| 393 |
if (m_bVideoBuffered) |
| 394 |
{ |
| 395 |
size_t bufsize; |
| 396 |
size_t buffull = m_pVideostream->GetBuffering(&bufsize); |
| 397 |
//printf("BUFFfull %f %d : %d\n", getVideoAsync(), buffull, bufsize); |
| 398 |
if (buffull < 1) |
| 399 |
{ |
| 400 |
m_ThreadMut[th].Unlock(); |
| 401 |
///AVM_WRITE("aviplay", "wait...\n"); |
| 402 |
m_QueueCond.Broadcast(); |
| 403 |
m_QueueCond.Wait(m_QueueMutex); |
| 404 |
//buffull = m_pVideostream->GetBuffering(&bufsize); |
| 405 |
//printf("AfterwaitBUFFfull %d : %d\n", buffull, bufsize); |
| 406 |
m_QueueMutex.Unlock(); |
| 407 |
m_ThreadMut[th].Lock(); |
| 408 |
continue; |
| 409 |
} |
| 410 |
} |
| 411 |
else // !m_bVideoBuffered |
| 412 |
setQuality(); |
| 413 |
|
| 414 |
m_llLastTimeStart = avm_get_time_us(); |
| 415 |
//printf("BUFFfull %d\n", m_pVideostream->GetBuffering()); |
| 416 |
CImage* im = m_pVideostream->GetFrame(!m_bVideoBuffered); |
| 417 |
//printf("NEWFT t:%f p:%d %d\n", m_pVideostream->GetTime(), m_pVideostream->GetPos(), m_pVideostream->GetBuffering()); |
| 418 |
|
| 419 |
// read time for the next frame |
| 420 |
double nf = m_pVideostream->GetTime() - m_dLastFrameStart; |
| 421 |
m_dVframetime = nf; |
| 422 |
|
| 423 |
if (!m_bVideoBuffered) |
| 424 |
{ |
| 425 |
//avm_usleep(40000); // debug drop |
| 426 |
m_fDecodingTime = avm_get_time_diff(avm_get_time_us(), m_llLastTimeStart); |
| 427 |
} |
| 428 |
|
| 429 |
if (im) |
| 430 |
{ |
| 431 |
//printf("DRATM %f\n", (lastCopyTime + m_fLastSyncTime)); |
| 432 |
// FIXME - this bool is stupid |
| 433 |
// condition: - no drop if there is just small async |
| 434 |
// or if its odd frame or rendering time is quite small |
| 435 |
if (!m_bVideoMute |
| 436 |
&& (async > -0.1 || !m_bDropping |
| 437 |
|| (framecnt++ & 1) |
| 438 |
))//|| (lastCopyTime + m_fLastSyncTime) < 0.007)) |
| 439 |
{ |
| 440 |
//printf("D %f\n", lastCopyTime + m_fLastSyncTime); |
| 441 |
m_Quality.insert(im->GetQuality() * 100.0f); |
| 442 |
m_Drop.insert(0.0); |
| 443 |
m_iFramesVideo++; |
| 444 |
const subtitle_line_t* sl = GetCurrentSubtitles(); |
| 445 |
// unlock here - as it could stop decoding process |
| 446 |
// because of block in xfree, but we do not need to |
| 447 |
// send signal as decoder will be waken by drop in that case |
| 448 |
// invoked in audio thread |
| 449 |
m_QueueMutex.Unlock(); |
| 450 |
int64_t copys = avm_get_time_us(); |
| 451 |
for (size_t i = 0; i < m_VideoRenderers.size(); ++i) |
| 452 |
{ |
| 453 |
m_VideoRenderers[i]->Draw(im); |
| 454 |
if (HasSubtitles()) |
| 455 |
m_VideoRenderers[i]->DrawSubtitles(sl); |
| 456 |
//m_VideoRenderers[i]->Sync(); // syncFrame() executed elsewhere |
| 457 |
} |
| 458 |
lastCopyTime = avm_get_time_diff(avm_get_time_us(), copys); // copy time |
| 459 |
m_bCallSync = true; |
| 460 |
// copy image to graphics card |
| 461 |
syncFrame(); |
| 462 |
m_QueueMutex.Lock(); |
| 463 |
} |
| 464 |
else |
| 465 |
{ |
| 466 |
m_Drop.insert(100.0); |
| 467 |
m_iFrameDrop++; |
| 468 |
printf("Ignore %f as: %f - %f\n", m_Drop.average(), async, getVideoAsync()); |
| 469 |
} |
| 470 |
|
| 471 |
im->Release(); |
| 472 |
} |
| 473 |
m_dLastFrameStart += m_dVframetime; // this will not accumulate error |
| 474 |
if (async < m_fDropLimit) |
| 475 |
dropFrame(); |
| 476 |
|
| 477 |
m_QueueCond.Broadcast(); |
| 478 |
m_QueueMutex.Unlock(); |
| 479 |
m_fLastSleepTime = 0.0; |
| 480 |
} |
| 481 |
|
| 482 |
if (mga_vid != -1) |
| 483 |
{ |
| 484 |
//ioctl(mga_vid, MGA_VID_IRQ_OFF, 0); |
| 485 |
close(mga_vid); |
| 486 |
} |
| 487 |
|
| 488 |
AVM_WRITE("aviplay", 1, "Thread video finished\n"); |
| 489 |
return 0; |
| 490 |
} |
| 491 |
|
| 492 |
void* AviPlayer::audioThread() |
| 493 |
{ |
| 494 |
const ThreadId th = THREAD_AUDIO; |
| 495 |
m_ThreadMut[th].Lock(); |
| 496 |
AVM_WRITE("aviplay", 1, "Thread audio started\n"); |
| 497 |
|
| 498 |
changePriority("Audio decoder ", PRIORITY_ADD_AUDIO); |
| 499 |
|
| 500 |
bool paused = false; |
| 501 |
while (checkSync(th)) |
| 502 |
{ |
| 503 |
if (!m_pAudioRenderer) |
| 504 |
{ |
| 505 |
m_ThreadCond[th].Wait(m_ThreadMut[th], 0.1f); |
| 506 |
continue; |
| 507 |
} |
| 508 |
|
| 509 |
const float async = getVideoAsync(); |
| 510 |
const double buftime = m_pAudioRenderer->GetBufferTime(); |
| 511 |
|
| 512 |
if (!m_bVideoAsync && async < -0.5 |
| 513 |
&& avm_get_time_diff(avm_get_time_us(), m_llTimeStart) > 1.0 |
| 514 |
&& (!m_pVideostream || !m_pVideostream->Eof())) |
| 515 |
{ |
| 516 |
if (!paused && async < -5.0) |
| 517 |
{ |
| 518 |
// ok even with dropping we could not stay in sync |
| 519 |
// so for a while turn of audio track and wait |
| 520 |
// until video will match audio time |
| 521 |
AVM_WRITE("aviplay", 1, "Thread audio large async time: %f buftime: %f - pause\n", async, buftime); |
| 522 |
// if the video stream has finished sooner than audio |
| 523 |
m_pAudioRenderer->Pause(true); |
| 524 |
paused = true; |
| 525 |
} |
| 526 |
if (m_pVideostream && m_QueueMutex.TryLock() == 0) |
| 527 |
{ |
| 528 |
dropFrame(); |
| 529 |
m_QueueCond.Broadcast(); |
| 530 |
m_QueueMutex.Unlock(); |
| 531 |
} |
| 532 |
} |
| 533 |
else if (paused && !m_pAudioRenderer->Eof() |
| 534 |
&& (async > 0.0 |
| 535 |
|| (!m_pVideostream || m_pVideostream->Eof()))) |
| 536 |
paused = (m_pAudioRenderer->Pause(false) != 0); |
| 537 |
|
| 538 |
//AVM_WRITE("aviplay", "atime: %f btime: %f\n", async, buftime); |
| 539 |
int r = m_pAudioRenderer->Extract(); |
| 540 |
if (r < 0) |
| 541 |
{ |
| 542 |
if (m_pAudioRenderer->Eof()) |
| 543 |
{ |
| 544 |
// FIXME FIXME - this is a big mess - fix |
| 545 |
if (!paused) |
| 546 |
{ |
| 547 |
m_pAudioRenderer->Pause(true); |
| 548 |
paused = true; |
| 549 |
AVM_WRITE("aviplay", "Audio stream eof\n"); |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
// either we have enough prebuffered sound or audio renderer |
| 554 |
// is unable to extract new samples - wait for a while |
| 555 |
m_ThreadCond[th].Wait(m_ThreadMut[th], 0.1f); |
| 556 |
} |
| 557 |
else if (m_pAudioRenderer->GetBufferTime() - buftime > 0.02) |
| 558 |
{ |
| 559 |
// just a small trick - so the buffers will not be filled |
| 560 |
// so fast when seeking - it's better for ffrwd |
| 561 |
//AVM_WRITE("aviplay", "JO %f %f\n", buftime, avm_get_time_diff(avm_get_time_us(), m_llTimeStart)); |
| 562 |
m_ThreadCond[th].Wait(m_ThreadMut[th], 0.01f); |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
AVM_WRITE("aviplay", 1, "Thread audio finished\n"); |
| 567 |
return 0; |
| 568 |
} |
| 569 |
|
| 570 |
void* AviPlayer::decoderThread() |
| 571 |
{ |
| 572 |
const ThreadId th = THREAD_DECODER; |
| 573 |
|
| 574 |
m_ThreadMut[th].Lock(); |
| 575 |
// thread started only with (stream != NULL) |
| 576 |
AVM_WRITE("aviplay", 1, "Thread decoder started\n"); |
| 577 |
|
| 578 |
changePriority("Video decoder ", PRIORITY_ADD_VIDEO); |
| 579 |
|
| 580 |
if (geteuid() == 0) |
| 581 |
AVM_WRITE("aviplay", "!!!Running video decoder as ROOT - Dangerous!!!\n"); |
| 582 |
|
| 583 |
while (checkSync(th)) |
| 584 |
{ |
| 585 |
// if we are after seek - leave some time for the user |
| 586 |
// if he will want to seek more there is no need to |
| 587 |
// precache frames at this point |
| 588 |
if (m_bVideoBuffered && !m_bDropping && m_llTimeStart && avm_get_time_diff(avm_get_time_us(), m_llTimeStart) > 0.05) |
| 589 |
{ |
| 590 |
m_QueueMutex.Lock(); |
| 591 |
size_t bs; |
| 592 |
size_t b = m_pVideostream->GetBuffering(&bs); |
| 593 |
//AVM_WRITE("aviplay", "Bdecoder %d %d %f\n", b, bs, avm_get_time_diff(avm_get_time_us(), m_llTimeStart)); |
| 594 |
// smart condition - do not stop with one-buffer |
| 595 |
if (b >= (bs - 1) && b > 0) |
| 596 |
{ |
| 597 |
//int64_t tin = avm_get_time_us(); |
| 598 |
m_ThreadMut[th].Unlock(); |
| 599 |
//AVM_WRITE("aviplay", "MUTEXWAIT %d\n", b); |
| 600 |
m_QueueCond.Broadcast(); |
| 601 |
m_QueueCond.Wait(m_QueueMutex); |
| 602 |
m_QueueMutex.Unlock(); |
| 603 |
m_ThreadMut[th].Lock(); |
| 604 |
//float td = avm_get_time_diff(avm_get_time_us(), tin); |
| 605 |
|
| 606 |
//b = m_pVideostream->GetBuffering(&bs); |
| 607 |
//AVM_WRITE("aviplay", "Bdecoder %d %d\n", b, bs); |
| 608 |
//AVM_WRITE("aviplay", "FREETM %f\n", td); |
| 609 |
continue; // recheck m_bVideoBuffered |
| 610 |
} |
| 611 |
setQuality(); |
| 612 |
m_QueueMutex.Unlock(); |
| 613 |
|
| 614 |
int64_t tin = avm_get_time_us(); |
| 615 |
//avm_usleep(40000); // debug drop |
| 616 |
//AVM_WRITE("aviplay", "readin %f\n", m_pVideostream->GetTime()); |
| 617 |
int hr = m_pVideostream->ReadFrame(true); |
| 618 |
m_fDecodingTime = avm_get_time_diff(avm_get_time_us(), tin); |
| 619 |
//AVM_WRITE("aviplay", "readout %d %f %f\n", hr, m_pVideostream->GetTime(), m_fDecodingTime); |
| 620 |
|
| 621 |
m_QueueMutex.Lock(); |
| 622 |
m_QueueCond.Broadcast(); |
| 623 |
m_QueueMutex.Unlock(); |
| 624 |
|
| 625 |
if (hr >= 0) |
| 626 |
continue; // frame is OK -> next frame |
| 627 |
} |
| 628 |
|
| 629 |
m_ThreadCond[th].Wait(m_ThreadMut[th], 0.1f); |
| 630 |
} |
| 631 |
|
| 632 |
AVM_WRITE("aviplay", 1, "Thread decoder finished\n"); |
| 633 |
return 0; |
| 634 |
} |
| 635 |
|
| 636 |
void AviPlayer::changePriority(const char* taskName, int addPriority, int schedt) |
| 637 |
{ return ; |
| 638 |
#if !defined(__NetBSD__) && !defined(__FreeBSD__) |
| 639 |
if (addPriority != 0) |
| 640 |
{ |
| 641 |
if (!taskName) |
| 642 |
taskName = ""; |
| 643 |
#if 1 |
| 644 |
int p = getpriority(PRIO_PROCESS, 0) + addPriority; |
| 645 |
if (p > 20) |
| 646 |
p = 20; |
| 647 |
setpriority(PRIO_PROCESS, 0, p); |
| 648 |
AVM_WRITE("aviplay", "%s thread lowered priority to %d\n", taskName, getpriority(PRIO_PROCESS, 0)); |
| 649 |
#else |
| 650 |
// this code doesn't work on Linux! |
| 651 |
// it looks like pthread priority is completely ignored and the only |
| 652 |
// way is to call 'setpriority' as you see above |
| 653 |
|
| 654 |
int policy; |
| 655 |
struct sched_param sched; |
| 656 |
pthread_getschedparam(pthread_self(), &policy, &sched); |
| 657 |
sched.sched_priority += addPriority; |
| 658 |
pthread_setschedparam(pthread_self(), policy, &sched); |
| 659 |
AVM_WRITE("aviplay", "%s using priority: %d\n", taskName, sched.sched_priority); |
| 660 |
#endif |
| 661 |
} |
| 662 |
|
| 663 |
if (schedt != 0 && m_iEffectiveUid == 0) |
| 664 |
{ |
| 665 |
// we could use FIFO scheduler with root access privilegies |
| 666 |
seteuid(m_iEffectiveUid); |
| 667 |
setegid(m_iEffectiveGid); |
| 668 |
|
| 669 |
const char sched_txt[][12] = { "SCHED_OTHER", "SCHED_FIFO", "SCHED_RR" }; |
| 670 |
struct sched_param schedRec; |
| 671 |
|
| 672 |
if (sched_getparam(0, &schedRec) != 0) { |
| 673 |
perror("sched_getparam"); |
| 674 |
return; |
| 675 |
} |
| 676 |
|
| 677 |
schedRec.sched_priority = sched_get_priority_min(schedt); |
| 678 |
|
| 679 |
if (sched_setscheduler(0, schedt, &schedRec) != 0) { |
| 680 |
perror("sched_setscheduler"); |
| 681 |
return; |
| 682 |
} |
| 683 |
|
| 684 |
AVM_WRITE("aviplay", "Video thread - using scheduler: %s\n", sched_txt[sched_getscheduler(0)]); |
| 685 |
setuid(getuid()); |
| 686 |
setgid(getgid()); |
| 687 |
} |
| 688 |
#endif // __NetBSD__ & __FreeBSD__ |
| 689 |
} |
| 690 |
|
| 691 |
void AviPlayer::syncFrame() |
| 692 |
{ |
| 693 |
if (m_pVideostream && !m_bVideoMute && m_bCallSync) |
| 694 |
{ |
| 695 |
m_bCallSync = false; |
| 696 |
int64_t newSync = avm_get_time_us(); |
| 697 |
// blit image on screen |
| 698 |
for (size_t i = 0; i < m_VideoRenderers.size(); ++i) |
| 699 |
m_VideoRenderers[i]->Sync(); |
| 700 |
// splited into two parts so we do not create jumping images |
| 701 |
// even in the debug mode |
| 702 |
int64_t newVideo = avm_get_time_us(); |
| 703 |
float newDiff = avm_get_time_diff(newVideo, m_llLastVideoSync); |
| 704 |
m_fLastSyncTime = avm_get_time_diff(newVideo, newSync); |
| 705 |
// after check of ahead might be more precise time for this |
| 706 |
// however we would have to play some with pausing |
| 707 |
// so we currently place the image right here |
| 708 |
double newAudio = (m_pAudioRenderer) ? m_pAudioRenderer->GetTime() : 0.0; |
| 709 |
//if (d < 0.027 || d > 0.043) |
| 710 |
AVM_WRITE("aviplay", 1, "AVS:%+8.4f s:%5.1f Vd:%5.1f Ad:%5.1f VSync:%2.1f %.3f\n", |
| 711 |
m_dLastFrameStart - newAudio, // sync |
| 712 |
m_fLastSleepTime * 1000.0, // sleep |
| 713 |
newDiff * 1000.0, // vdiff |
| 714 |
(newAudio - m_dLastAudioSync) * 1000.0, // adiff |
| 715 |
m_fLastSyncTime * 1000.0, |
| 716 |
m_dLastFrameStart |
| 717 |
); |
| 718 |
m_dLastAudioSync = newAudio; |
| 719 |
m_llLastVideoSync = newVideo; |
| 720 |
m_fLastDiff = newDiff; |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
AVM_END_NAMESPACE; |