Debian Codegraph @

lib/aviplay/aviplay.cpp

raw
1 /********************************************************
2
3 AVI player object
4 Copyright 2000 Eugene Kuznetsov ([email protected])
5
6 *********************************************************/
7
8 #include "aviplay_impl.h"
9
10 #ifdef HAVE_LIBSDL
11 #include "SdlAudioRenderer.h"
12 #endif
13
14 #ifdef HAVE_OSS
15 #include "OssAudioRenderer.h"
16 #endif
17
18 #ifdef HAVE_SUNAUDIO
19 #include "SunAudioRenderer.h"
20 #endif
21
22 #include "CopyAudioRenderer.h"
23
24 #include "avm_cpuinfo.h"
25 #include "avm_creators.h"
26 #include "avm_fourcc.h"
27 #include "avm_output.h"
28 #include "utils.h"
29
30 #include <unistd.h> // geteuid
31 #include <string.h> // memcpy
32 #include <stdlib.h> // getenv, free()
33 #include <stdio.h>
34
35 AVM_BEGIN_NAMESPACE;
36
37 #define __MODULE__ "IAviPlayer"
38
39 //int AVIPLAY_DEBUG = 0;
40 //#undef Debug
41 //#define Debug if(AVIPLAY_DEBUG)
42
43 #undef min
44 #define min(X,Y) ((X)<(Y)?(X):(Y))
45
46
47 AviPlayer::AviPlayer(const char* filename, int depth, const char* subname,
48 int flags, const char* vcodec, const char* acodec)
49 :
50 m_pAudioRenderer(0),
51 m_pKillhandler(0),
52 m_pAudiofunc(0),
53 m_pClip(0),
54 m_pClipAudio(0),
55 m_pVideostream(0),
56 m_pAudiostream(0),
57 m_Drop("Drop", 50),
58 m_Quality("Quality", 25),
59
60 m_Filename(filename),
61 m_Subfilename(subname ? subname : ""),
62 m_pSubtitles(0),
63 m_pSubline(0),
64
65 m_fAsync(0.),
66 m_fSubAsync(0.),
67 m_iMaxAuto(0),
68 m_iFramesVideo(0),
69 m_iFrameDrop(0),
70 m_iLockCount(0),
71 m_llTimeStart(0),
72 m_dFrameStart(0),
73 m_dLastFrameStart(0),
74 m_dLastAudioSync(0.),
75 m_fLastDiff(0.f),
76 m_fLastSyncTime(0.f),
77 m_fLastSleepTime(0.f),
78
79 m_pVideoThread(0),
80 m_pAudioThread(0),
81 m_pDecoderThread(0),
82
83 m_uiPgPrevPos(~0U),
84 m_llPgPrevTime(0),
85
86 m_dVframetime(0.04),
87 m_iWidth(0),
88 m_iHeight(0),
89 m_iDepth(depth),
90 m_CSP(24),
91
92 m_bQualityAuto(false),
93 m_bVideoBuffered(false),
94 m_bVideoDirect(false),
95 m_bDropping(false),
96 m_bPaused(false),
97 m_bInitialized(false),
98 m_bQuit(false),
99 m_bHangup(false),
100 m_bBuffering(false),
101 m_bCallSync(false),
102 m_bConstructed(false)
103 {
104 if (vcodec && strlen(vcodec))
105 m_vcodec = vcodec;
106 if (acodec && strlen(acodec))
107 m_acodec = acodec;
108
109 memset(propertyRead, 0, sizeof(propertyRead));
110
111 // remember effective ids at the creation time of the object
112 // and use the real user id until we really need the euid
113 m_iEffectiveUid = geteuid();
114 m_iEffectiveGid = getegid();
115 if ((int) getuid() != (int) m_iEffectiveUid)
116 seteuid(getuid());
117 if ((int) getgid() != (int) m_iEffectiveGid)
118 setegid(getgid());
119
120 //m_bAudioMute = (getenv("AVIPLAY_MUTE_AUDIO") != 0);
121 m_bVideoMute = (getenv("AVIPLAY_MUTE_VIDEO") != 0) ? true : false;
122
123 m_bVideoAsync = (getenv("AVIPLAY_VIDEO_ASYNC") != 0) ? true : false;
124
125 const char* ad = getenv("AVIPLAY_DEBUG");
126 if (ad)
127 {
128 int AVIPLAY_DEBUG = atoi(ad);
129 if (AVIPLAY_DEBUG)
130 {
131 AvmOutput()->setDebugLevel("aviplayxx", 4);
132 AVM_WRITE("aviplay", "Debug is on %d\n", AVIPLAY_DEBUG);
133 }
134 }
135
136 if (m_bVideoAsync)
137 AVM_WRITE("aviplay", 1, "Video is running asynchronously\n");
138
139 m_pClip = CreateReadFile(filename, flags);
140 }
141
142 AviPlayer::~AviPlayer()
143 {
144 Stop();
145
146 assert(!m_bQuit);
147 m_bQuit = true;
148 if (!IsRedirector())
149 {
150 if (m_pVideostream)
151 m_pVideostream->StopStreaming();
152 if (m_pAudiostream)
153 m_pAudiostream->StopStreaming();
154
155 while (!m_bInitialized)
156 unlockThreads();
157
158 delete m_pVideoThread;
159 delete m_pDecoderThread;
160 delete m_pAudioThread;
161 }
162 if (m_pClipAudio)
163 {
164 AVM_WRITE("aviplay", "Closing audio clip\n");
165 delete m_pClipAudio;
166 }
167 if (m_pClip)
168 {
169 AVM_WRITE("aviplay", "Closing clip\n");
170 delete m_pClip;
171 }
172
173 while (m_VideoRenderers.size() > 0)
174 {
175 delete m_VideoRenderers.back();
176 m_VideoRenderers.pop_back();
177 }
178
179 if (m_iFramesVideo)
180 AVM_WRITE("aviplay", "Played %d video frames ( %f%% drop )\n",
181 m_iFramesVideo, (float)m_iFrameDrop * 100.f / (float)m_iFramesVideo);
182 if (m_pSubtitles)
183 subtitle_close(m_pSubtitles);
184 if (m_pSubline)
185 subtitle_line_free(m_pSubline);
186 }
187
188 void AviPlayer::construct()
189 {
190 if (IsRedirector())
191 {
192 AVM_WRITE("aviplay", "Redirector\n");
193 return;
194 }
195
196 int def_audio, def_video, asynctm, avol, subasync;
197 Get(AUDIO_STREAM, &def_audio,
198 VIDEO_STREAM, &def_video,
199 ASYNC_TIME_MS, &asynctm,
200 SUBTITLE_ASYNC_TIME_MS, &subasync,
201 AUDIO_VOLUME, &avol,
202 0);
203
204 m_iWidth = 0;
205 m_iHeight = 0;
206 m_fAsync = (float)asynctm / 1000.f;
207 m_fSubAsync = (float)subasync / 1000.f;
208 //fprintf(stderr, "MP0 audio %p\n", m_pVideostream);
209 while (!m_pAudiostream && def_audio >= 0)
210 m_pAudiostream = m_pClip->GetStream(def_audio--, IReadStream::Audio);
211 // Initialize audio - thread from SDL is initialized here
212 if (m_pAudiostream == 0)
213 AVM_WRITE("aviplay", "Audiostream not detected\n");
214
215 //fprintf(stderr, "MP0 construct %p\n", m_pVideostream);
216 if (!m_bVideoMute && m_iDepth)
217 {
218 // >128 will try to 'touch' all streams
219 int o = def_video;
220 while ((!m_pVideostream || o > 128) && def_video >= 0)
221 m_pVideostream = m_pClip->GetStream(def_video--, IReadStream::Video);
222 }
223 else
224 {
225 m_pVideostream = 0;
226 AVM_WRITE("aviplay", "Video disabled\n");
227 }
228
229 // check if it's still valid
230 // might not be in the case of network stream
231 m_pClip->IsValid();
232
233 Get(VIDEO_QUALITY_AUTO, &m_bQualityAuto,
234 VIDEO_DROPPING, &m_bVideoDropping,
235 VIDEO_BUFFERED, &m_bVideoBuffered,
236 VIDEO_DIRECT, &m_bVideoDirect,
237 0);
238
239 if (!m_pVideostream)
240 AVM_WRITE("aviplay", "Videostream not detected\n");
241 else
242 {
243 //printf("VSTREAMTIME %f\n", m_pVideostream->GetTime());
244 // reading subtitles
245 avm::string fn(m_Filename, m_Filename.rfind('.'));
246
247 InitSubtitles(m_Subfilename.size()
248 ? m_Subfilename.c_str() : fn.c_str());
249
250 if (restartVideoStreaming(m_vcodec.size() ? m_vcodec.c_str() : 0) == 0)
251 createVideoRenderer();
252 }
253
254 m_iFramesVideo = 0;
255 m_iFrameDrop = 0;
256 m_llTimeStart = 0;
257 m_bQuit = false;
258 m_bConstructed = true;
259 // short fs;
260 // __asm__ __volatile__ ("movw %%fs, %%ax":"=a"(fs));
261 // cout<<"Before pthread_create: fs is "<<fs<<endl;
262 // fs_seg=fs;
263
264 m_bHangup = true; // newly created thread should hangup and wait
265 if (m_pVideostream)
266 {
267 m_pDecoderThread = new PthreadTask(0, startDecoderThread, (void*)this);
268 m_pVideoThread = new PthreadTask(0, startVideoThread, (void*)this);
269 }
270 else if (!m_pAudiostream)
271 {
272 AVM_WRITE("aviplay", "Cannot play this\n");
273 delete m_pClip;
274 m_pClip = 0;
275 return;
276 }
277
278 if (m_pAudiostream)
279 m_pAudioThread = new PthreadTask(0, startAudioThread, (void*)this);
280
281 // now wait until all new threads hang in the cond_wait
282 lockThreads("Init");
283 }
284
285 int AviPlayer::SetColorSpace(fourcc_t csp, bool test_only)
286 {
287 if (!m_pVideostream || !m_pVideostream->GetVideoDecoder())
288 return -1;
289
290 //cout << "SET COLOR SPACE " << hex << csp << dec << " " << test_only << endl;
291 if (!test_only)
292 return m_pVideostream->GetVideoDecoder()->SetDestFmt(0, csp);
293
294 IVideoDecoder::CAPS cap;
295 switch (csp)
296 {
297 case fccYUY2:
298 cap = IVideoDecoder::CAP_YUY2;
299 break;
300 case fccI420:
301 cap = IVideoDecoder::CAP_I420;
302 break;
303 case fccYV12:
304 cap = IVideoDecoder::CAP_YV12;
305 break;
306 case fccIYUV:
307 cap = IVideoDecoder::CAP_IYUV;
308 break;
309 case fccUYVY:
310 cap = IVideoDecoder::CAP_UYVY;
311 break;
312 case fccYVYU:
313 cap = IVideoDecoder::CAP_YVYU;
314 break;
315 default:
316 cap = IVideoDecoder::CAP_NONE;
317 }
318
319 return !(m_pVideostream->GetVideoDecoder()->GetCapabilities() & cap);
320 }
321
322 int AviPlayer::InitSubtitles(const char* filename)
323 {
324 if (lockThreads("InitSubtitles") == 0)
325 {
326 if (m_pSubtitles)
327 subtitle_close(m_pSubtitles);
328 char* fn = NULL;
329 int fd = subtitle_filename(filename, &fn);
330 if (fd >= 0)
331 {
332 char* cp;
333 Get(SUBTITLE_CODEPAGE, &cp, 0);
334 m_pSubtitles = subtitle_open(fd, GetFps(), cp);
335 if (fn)
336 {
337 AVM_WRITE("aviplay", "Subtitles from: %s (codepage: %s)\n", fn, cp);
338 free(fn);
339 }
340 if (cp)
341 free(cp);
342 }
343 unlockThreads();
344 }
345 return 0;
346 }
347
348 const char* AviPlayer::GetAudioFormat() const
349 {
350 return (m_pAudioRenderer) ? m_pAudioRenderer->GetAudioFormat() : 0;
351 }
352
353 const char* AviPlayer::GetVideoFormat() const
354 {
355 return (m_pVideostream) ? GetCodecInfo().GetName() : 0;
356 }
357
358 StreamInfo* AviPlayer::GetAudioStreamInfo() const
359 {
360 return (m_pAudiostream) ? m_pAudiostream->GetStreamInfo() : 0;
361 }
362
363 StreamInfo* AviPlayer::GetVideoStreamInfo() const
364 {
365 return (m_pVideostream) ? m_pVideostream->GetStreamInfo() : 0;
366 }
367
368 const CodecInfo& AviPlayer::GetCodecInfo(int type) const
369 {
370 if (type == AUDIO_CODECS)
371 return m_pAudiostream->GetAudioDecoder()->GetCodecInfo();
372
373 return m_pVideostream->GetVideoDecoder()->GetCodecInfo();
374 }
375
376 const subtitle_line_t* AviPlayer::GetCurrentSubtitles()
377 {
378 if (!m_pSubtitles)
379 return 0;
380 if (m_pSubline == 0)
381 {
382 m_pSubline = subtitle_line_new();
383 if (m_pSubline == 0)
384 return 0;
385 }
386
387 if (subtitle_get(m_pSubline, m_pSubtitles, GetTime() + m_fSubAsync) != 0)
388 return 0;
389
390 return m_pSubline;
391 }
392
393 bool AviPlayer::HasSubtitles() const
394 {
395 return (m_pSubtitles && subtitle_get_lines(m_pSubtitles) > 0);
396 }
397
398 void AviPlayer::Start()
399 {
400 AVM_WRITE("aviplay", 1, "AviPlayer::Start()\n");
401 if (!IsValid() || IsRedirector())
402 return;
403
404 //printf("CONSTRUCT %d %d i: %d p: %d\n", IsValid(), IsRedirector(), m_bInitialized, m_bPaused);
405 if (!m_bConstructed)
406 construct();
407
408 if (IsPlaying())
409 {
410 AVM_WRITE("aviplay", "AviPlayer::Start(), already started\n");
411 return;
412 }
413
414 createAudioRenderer();
415
416 if (m_pAudioRenderer)
417 m_pAudioRenderer->Start();
418 else if (!m_pVideostream)
419 return; // nothing to do
420
421 m_llLastTimeStart = avm_get_time_us();
422 m_Drop.clear();
423 m_bQuit = false;
424 m_bPaused = false;
425 m_bBuffering = false;
426 unlockThreads();
427 }
428
429 void AviPlayer::Stop()
430 {
431 AVM_WRITE("aviplay", 1, "AviPlayer::Stop()\n");
432 if (!IsPlaying())
433 return;
434
435 if (m_bPaused || lockThreads("Stop") == 0)
436 {
437 delete m_pAudioRenderer;
438 m_pAudioRenderer = 0;
439
440 m_bPaused = false;
441
442 if (m_pKillhandler)
443 m_pKillhandler(0, m_pKillhandlerArg);
444
445 if (m_pVideostream)
446 m_pVideostream->SeekTime(0);//0x7fffffff);
447
448 if (m_pAudiostream)
449 m_pAudiostream->SeekTime(0);//x7fffffff);
450 }
451 }
452
453 void AviPlayer::Pause(bool state)
454 {
455 AVM_WRITE("aviplay", 1, "AviPlayer::pause() ( %d -> %d )\n", m_bPaused, state);
456 if (!IsPlaying())
457 return;
458
459 if (m_bPaused == state)
460 return;
461
462 if (state)
463 {
464 lockThreads("Pause");
465 if (m_pAudioRenderer)
466 m_pAudioRenderer->Pause(state);
467 m_bPaused = state;
468 m_bBuffering = true;
469 }
470 else
471 {
472 if (m_pAudioRenderer && (m_pAudioRenderer->Pause(state) != 0))
473 return;
474 m_bPaused = state;
475 m_bBuffering = false;
476 unlockThreads();
477 }
478 }
479
480 void AviPlayer::Play()
481 {
482 AVM_WRITE("aviplay", 1, "AviPlayer::Play() ( %d )\n", m_bPaused);
483 if (!IsPlaying())
484 return;
485
486 if (m_bPaused)
487 Pause(!m_bPaused);
488 }
489
490 double AviPlayer::GetTime() const
491 {
492 if (m_pVideostream && !m_pVideostream->Eof())
493 {
494 // this is bit more complicated so here goes light explanation:
495 // Asf file can display the same frame for 5 second - and
496 // it doesn't look good when we show jumping numbers
497 // so we pick the last remebered frame time and we are
498 // calculating time from this frame - we should be preserving
499 // this speed - for paused mode we display time for currently
500 // displayed frame
501 double len = m_pVideostream->GetLengthTime();
502 if (len > 0.)
503 {
504 double t = m_pVideostream->GetTime();
505 if (m_pAudioRenderer && !m_pAudioRenderer->Eof())
506 {
507 double at = m_pAudioRenderer->GetTime();
508 if ((t - at) > 5.)
509 t = at;
510 }
511 return t;
512 }
513 }
514 return (m_pAudioRenderer) ? m_pAudioRenderer->GetTime() : 0.;
515 }
516
517 double AviPlayer::GetLengthTime() const
518 {
519 double len = GetVideoLengthTime();
520 double alen = GetAudioLengthTime();
521 return (alen > len) ? alen : len;
522 }
523
524 framepos_t AviPlayer::GetFramePos() const
525 {
526 return (m_pVideostream) ? m_pVideostream->GetPos() : 0;
527 }
528
529 double AviPlayer::Reseek(double pos)
530 {
531 AVM_WRITE("aviplay", 1, "Seek pos: %f %" PRIsz "\n", pos, m_VideoRenderers.size());
532
533 if (!IsPlaying())
534 return -1;
535
536 if (lockThreads("Reseek") == 0)
537 {
538 if (m_pVideostream)
539 {
540 pos = m_pVideostream->SeekTimeToKeyFrame(pos);
541 AVM_WRITE("aviplay", 1, "Keyframe pos: %f\n", pos);
542 if (m_bPaused)
543 drawFrame(false);
544 }
545 if (m_pAudioRenderer)
546 m_pAudioRenderer->SeekTime(pos);
547
548 m_Drop.clear();
549 unlockThreads();
550 }
551
552 return pos;
553 }
554
555 int AviPlayer::ReseekExact(double pos)
556 {
557 if (!IsPlaying())
558 return -1;
559
560 // we might not need this in future
561 int r = 0;
562 if (lockThreads("ReseekExact") == 0)
563 {
564 AVM_WRITE("aviplay", 1, "Reseek pos: %f %p %" PRIsz "\n", pos, m_pVideoThread, m_VideoRenderers.size());
565
566 double pos2 = pos;
567
568 if (m_pVideostream)
569 {
570 double cft = m_pVideostream->GetTime();
571 double nkft = m_pVideostream->GetTime(m_pVideostream->GetNextKeyFrame());
572 //printf("TIMES %f %f\n", cft, nkft);
573 if (cft < pos && pos < nkft)
574 {
575 // do nothing
576 unlockThreads();
577 return 0;
578 }
579
580 pos2 = m_pVideostream->SeekTimeToKeyFrame(pos);
581 //printf("TIMESPOS2 %f \n", pos2);
582 // in case the video is delayed and doesn't start at 0.0s
583 if (pos < (m_pVideostream->GetTime(0) - 0.001) || pos > m_pVideostream->GetLengthTime()) {
584 pos2 = pos;
585 //printf("TIMESPOS3 %f %f %f %f\n", pos2, pos, m_pVideostream->GetTime(0), m_pVideostream->GetLengthTime());
586 }
587 }
588
589 AVM_WRITE("aviplay", 1, "Seek OK ( %fs -> %fs )\n", pos, pos2);
590 if (pos2 < 0.)
591 {
592 pos2 = pos = 0.;
593 AVM_WRITE("aviplay", "Warning: reseek_exact pos2<0!\n");
594 r = -1;
595 }
596
597 if (m_pVideostream)
598 {
599 if (pos2 > pos && pos2 > (m_pVideostream->GetTime(0) + 0.001))
600 {
601 AVM_WRITE("aviplay", "Warning: reseek_exact: pos2>pos! %f %f %f\n", pos2, pos, m_pVideostream->GetTime(0));
602 r = -1;
603 }
604 drawFrame(false);
605 }
606 if (m_pAudioRenderer) {
607 AVM_WRITE("aviplay", 1, "Seek Audio ( %fs )\n", pos2);
608 m_pAudioRenderer->SeekTime(pos2); // might be different from vpos
609 }
610
611 m_Drop.clear();
612 unlockThreads();
613 }
614 return r;
615 }
616
617 int AviPlayer::NextKeyFrame()
618 {
619 if (!IsPlaying())
620 return -1;
621
622 int r = 0;
623 if (lockThreads("NextKeyFrame") == 0)
624 {
625 if (m_pVideostream)
626 {
627 m_pVideostream->SeekToNextKeyFrame();
628 drawFrame();
629 }
630 else if (m_pAudioRenderer)
631 m_pAudioRenderer->SeekTime(m_pAudioRenderer->GetTime() + 1.);
632
633 unlockThreads();
634 }
635
636 return r;
637 }
638
639 int AviPlayer::PrevKeyFrame()
640 {
641 if (!IsPlaying())
642 return -1;
643
644 int r = 0;
645 if (lockThreads("PrevKeyFrame") == 0)
646 {
647 if (m_pVideostream && m_pVideostream->GetPos())
648 {
649 framepos_t cpos = m_pVideostream->GetPos();
650 framepos_t npos = m_pVideostream->SeekToPrevKeyFrame();
651 if (avm_get_time_diff(avm_get_time_us(), m_llPgPrevTime) < 0.3)
652 {
653 framepos_t p = npos;
654 while (npos > 0 && npos >= m_uiPgPrevPos)
655 {
656 npos = m_pVideostream->SeekToPrevKeyFrame();
657 if (p == npos)
658 break;
659 }
660 }
661
662 if ((cpos - npos < 5) && !IsPaused())
663 {
664 m_pVideostream->Seek(npos);
665 npos = m_pVideostream->SeekToPrevKeyFrame();
666 }
667 m_llPgPrevTime = avm_get_time_us();
668 m_uiPgPrevPos = npos;
669
670 if (npos != m_pVideostream->ERR)
671 {
672 //m_pVideostream->SeekToKeyFrame(npos);
673 double pos = m_pVideostream->GetTime();
674 if (m_pAudioRenderer)
675 m_pAudioRenderer->SeekTime(pos > 0. ? pos : 0.);
676 }
677 drawFrame();
678 }
679 else if (m_pAudioRenderer)
680 m_pAudioRenderer->SeekTime(m_pAudioRenderer->GetTime() - 1.);
681
682 unlockThreads();
683 }
684
685 return r;
686 }
687
688 int AviPlayer::NextFrame()
689 {
690 if (!IsPlaying())
691 return -1;
692 if (lockThreads("NextFrame") == 0)
693 {
694 drawFrame();
695 unlockThreads();
696 }
697
698 return 0;
699 }
700
701 int AviPlayer::PrevFrame()
702 {
703 if (!IsPlaying())
704 return -1;
705
706 if (lockThreads("PrevFrame") == 0)
707 {
708 if (m_pVideostream)
709 {
710 framepos_t cpos = m_pVideostream->GetPos();
711 framepos_t prev = cpos;
712 m_pVideostream->SeekToPrevKeyFrame();
713 m_fDecodingTime = 1.f;
714 setQuality();
715 if ((m_pVideostream->GetPos() + 2) < cpos)
716 {
717 //printf("GGP %d %d\n",m_pVideostream->GetPos(), cpos);
718 while ((m_pVideostream->ReadFrame(false) == 0))
719 {
720 //printf("npos %d\n", m_pVideostream->GetPos());
721 if ((m_pVideostream->GetPos() + 2) >= cpos)
722 break;
723 }
724 if (m_pVideostream->GetPos() > cpos)
725 {
726 // there were some skiped frame - so jump to keyframe
727 m_pVideostream->SeekToPrevKeyFrame();
728 }
729 }
730
731 //printf("RENDER %d %f %d\n", m_pVideostream->GetPos(), m_pVideostream->GetTime(), cpos);
732 if (m_pVideostream->GetPos() < cpos || cpos == 0)
733 drawFrame();
734 }
735 unlockThreads();
736 }
737
738 return 0;
739 }
740
741 IRtConfig* AviPlayer::GetRtConfig(int type) const
742 {
743 // maybe add lock ???
744 switch (type)
745 {
746 case AUDIO_CODECS:
747 {
748 IAudioDecoder* d = (m_pAudiostream) ? m_pAudiostream->GetAudioDecoder() : 0;
749 // here is INCREDIBLE BUG in g++ 3.0.4
750 //IRtConfig* c = dynamic_cast<IRtConfig*>(d)
751 // so whole avifile now doesn't use dynamic_cast operator
752 return (d) ? d->GetRtConfig() : 0;
753 }
754 case AUDIO_RENDERER:
755 return 0;
756 case VIDEO_CODECS:
757 {
758 IVideoDecoder* d = (m_pVideostream) ? m_pVideostream->GetVideoDecoder() : 0;
759 return (d) ? d->GetRtConfig() : 0;
760 }
761 case VIDEO_RENDERER:
762 return (m_VideoRenderers.size() > 0) ? m_VideoRenderers[0]->GetRtConfig() : 0;
763 }
764 return 0;
765 }
766
767 // used when we are in pause mode or the player is not playing
768 int AviPlayer::drawFrame(bool aseek)
769 {
770 if (!m_bQuit && m_pVideostream)
771 {
772 CImage* im = m_pVideostream->GetFrame(true); // ReadFrame
773 m_fLastDiff = 0.f;
774 setQuality();
775 if (im)
776 {
777 const subtitle_line_t* sl = GetCurrentSubtitles();
778 for (size_t i = 0; i < m_VideoRenderers.size(); ++i)
779 {
780 m_VideoRenderers[i]->Draw(im);
781 if (HasSubtitles())
782 m_VideoRenderers[i]->DrawSubtitles(sl);
783
784 m_VideoRenderers[i]->Sync();
785 }
786 m_Quality.insert(im->GetQuality() * 100.f);
787 im->Release();
788 m_iFramesVideo++;
789 }
790 if (aseek && m_pAudioRenderer)
791 m_pAudioRenderer->SeekTime(m_pVideostream->GetTime());
792 return 0;
793 }
794 return -1;
795 }
796
797 void AviPlayer::createAudioRenderer()
798 {
799 double origtime = (m_pAudioRenderer) ? m_pAudioRenderer->GetTime() : -1.;
800 delete m_pAudioRenderer;
801 m_pAudioRenderer = 0;
802 if (!m_pAudiostream)
803 return;
804
805 WAVEFORMATEX MyOwf;
806 memset(&MyOwf, 0, sizeof(MyOwf));
807 uint_t forcedFreq = 0;
808
809 bool resamp;
810 Get(AUDIO_RESAMPLING, &resamp, 0);
811 if (resamp)
812 {
813 Get(AUDIO_RESAMPLING_RATE, &MyOwf.nSamplesPerSec,
814 AUDIO_PLAYING_RATE, &forcedFreq,
815 0);
816 }
817
818 char* arend;
819 Get(AUDIO_RENDERER, &arend, 0);
820 if (!arend)
821 return;
822 m_pAudiostream->StopStreaming();
823 if (m_pAudiostream->StartStreaming(m_acodec.size() ? m_acodec.c_str() : 0))
824 return;
825
826 char* arends = arend;
827 while (arends && !m_pAudioRenderer)
828 {
829 WAVEFORMATEX Owf(MyOwf);
830 AVM_WRITE("aviplay", "Will try audio renderers in this order: %s\n", arends);
831
832 if (m_pAudiofunc)
833 {
834 // we could run this code sooner - but why...
835 m_pAudioRenderer = new CopyAudioRenderer(m_pAudiostream, Owf, m_pAudiofunc, m_pAudiofuncArg);
836 }
837 else if (!strncasecmp(arends, "SDL", 3))
838 {
839 #ifdef HAVE_LIBSDL
840 m_pAudioRenderer = new SdlAudioRenderer(m_pAudiostream, Owf, forcedFreq);
841 #else
842 AVM_WRITE("aviplay", "Warning: SDL audio renderer unavailable!\n");
843 #endif
844 }
845 else if (!strncasecmp(arends, "OSS", 3))
846 {
847 #ifdef HAVE_OSS
848 m_pAudioRenderer = new OssAudioRenderer(m_pAudiostream, Owf, forcedFreq);
849 #else
850 AVM_WRITE("aviplay", "Warning: OSS audio renderer unavailable!\n");
851 #endif
852 }
853 else if (!strncasecmp(arends, "SUN", 3))
854 {
855 #ifdef HAVE_SUNAUDIO
856 AVM_WRITE("aviplay", "Warning: Sun audio renderer out of date - update me!\n");
857 m_pAudioRenderer = new SunAudioRenderer(m_pAudiostream, Owf);
858 #else
859 AVM_WRITE("aviplay", "Warning: Sun audio renderer unavailable!\n");
860 #endif
861 }
862 else if (!strncasecmp(arends, "noaudio", 5))
863 {
864 if (strlen(arend) < 6)
865 {
866 AVM_WRITE("aviplay", "--- 'noaudio' audio renderer selected - if this is unintentional\n");
867 AVM_WRITE("aviplay", "--- please remove ~/.avm directory and default renderer set will be used\n");
868 }
869 break;
870 }
871 if (m_pAudioRenderer && m_pAudioRenderer->Init() < 0)
872 {
873 delete m_pAudioRenderer;
874 m_pAudioRenderer = 0;
875 }
876 arends = strchr(arends, ',');
877 if (arends)
878 arends++;
879 }
880 if (m_pAudioRenderer)
881 {
882 m_pAudioRenderer->SetAsync(m_fAsync);
883 // m_pAudioRenderer->SetVolume(m_fVolume * m_pAudioRenderer->VOL_MAX);
884 if (origtime > 0)
885 {
886 m_pAudioRenderer->SeekTime(origtime);
887 m_pAudioRenderer->Start();
888 }
889 }
890 free(arend);
891 }
892
893 AviPlayer::State AviPlayer::GetState(double* percent)
894 {
895 if (!m_pClip || !m_pClip->IsValid())
896 {
897 if (m_pClip && !m_pClip->IsOpened())
898 return Opening;
899 return Invalid;
900 }
901
902 double p;
903 if (m_pVideostream)
904 p = m_pVideostream->CacheSize();
905 else if (m_pAudioRenderer)
906 p = m_pAudioRenderer->GetCacheSize();
907 else
908 p = 0;
909 if (percent)
910 *percent = p;
911
912 if (m_bBuffering)
913 {
914 if (p >= 1.)
915 m_bBuffering = false;
916 return Buffering;
917 }
918 if (IsPaused())
919 return Paused;
920 if (IsPlaying())
921 return Playing;
922
923 return Stopped;
924 }
925
926 bool AviPlayer::IsPaused() const
927 {
928 return (m_pClip && m_bPaused);
929 }
930
931 bool AviPlayer::IsPlaying() const
932 {
933 // when we are in paused mode initialized is 0
934 return m_pClip && !IsRedirector() && (m_bInitialized || m_bPaused);
935 }
936
937 bool AviPlayer::IsStopped() const
938 {
939 return (m_pClip && !IsPlaying());
940 }
941
942 const avm::vector<IVideoRenderer*>& AviPlayer::GetVideoRenderers() const
943 {
944 return m_VideoRenderers;
945 }
946
947 int AviPlayer::SetVideoRenderers(avm::vector<IVideoRenderer*> rv)
948 {
949 if (lockThreads("SetVideoRenderes") == 0)
950 {
951 m_VideoRenderers = rv;
952 unlockThreads();
953 }
954 return 0;
955 }
956
957 void AviPlayer::SetAsync(float async)
958 {
959 Set(ASYNC_TIME_MS, int(async * 1000.f), 0);
960 }
961
962 void AviPlayer::SetAudioFunc(AUDIOFUNC func, void* arg)
963 {
964 if (lockThreads("SetAudioFunc") == 0)
965 {
966 m_pAudiofunc = func;
967 m_pAudiofuncArg = arg;
968 createAudioRenderer();
969 unlockThreads();
970 }
971 }
972
973 void AviPlayer::Restart()
974 {
975 if (!IsPlaying() || !m_pVideostream)
976 return;
977
978 IVideoDecoder* vs = m_pVideostream->GetVideoDecoder();
979 if (!vs)
980 return;
981
982 if (m_bPaused)
983 {
984 vs->Restart();
985 return;
986 }
987
988 lockThreads("Restart");
989 vs->Restart();
990 unlockThreads();
991 ReseekExact(GetTime());
992 }
993
994 int AviPlayer::restartVideoStreaming(const char* vc)
995 {
996 int r = -1;
997 if (m_pVideostream)
998 {
999 m_pVideostream->StopStreaming();
1000
1001 r = m_pVideostream->StartStreaming(vc);
1002 if (r == 0)
1003 {
1004 if (!GetRtConfig(VIDEO_CODECS)
1005 || !GetCodecInfo().FindAttribute("maxauto")
1006 || CodecGetAttr(GetCodecInfo(), "maxauto", &m_iMaxAuto) < 0)
1007 m_iMaxAuto = -1;
1008
1009 // prefer this format
1010 // remove 0 to get YUY2 rendering
1011 StreamInfo* si = m_pVideostream->GetStreamInfo();
1012 if (si)
1013 {
1014 m_iWidth = si->GetVideoWidth();
1015 m_iHeight = si->GetVideoHeight();
1016 delete si;
1017 }
1018 // FIXME - might be sometimes better to use different
1019 // colorspace
1020 const fourcc_t m[] = { m_CSP, IMG_FMT_YUY2, IMG_FMT_YV12, 0 };
1021 for (unsigned i = 0; m[i]; ++i)
1022 {
1023 if (m[i] > 32 && m_pVideostream->GetVideoDecoder()->SetDestFmt(m_CSP) == 0)
1024 {
1025 //printf("OK %x\n", m[i]);
1026 m_CSP = m[i];
1027 break;
1028 }
1029 }
1030 }
1031 else
1032 {
1033 AVM_WRITE("aviplay", "Failed to initialize decoder\n");
1034 m_pVideostream = 0;
1035 }
1036 }
1037 return r;
1038 }
1039
1040 void* AviPlayer::startVideoThread(void* arg)
1041 {
1042 return ((AviPlayer*) arg)->videoThread();
1043 }
1044
1045 void* AviPlayer::startAudioThread(void* arg)
1046 {
1047 return ((AviPlayer*) arg)->audioThread();
1048 }
1049
1050 void* AviPlayer::startDecoderThread(void* arg)
1051 {
1052 return ((AviPlayer*) arg)->decoderThread();
1053 }
1054
1055 IAviPlayer* CreateAviPlayer(const char* filename, int bitdepth, const char* subname,
1056 int flags, const char* vcodec, const char* acodec)
1057 {
1058 return new AviPlayer(filename, bitdepth, subname, flags, vcodec, acodec);
1059 }
1060
1061 #undef __MODULE__
1062
1063 AVM_END_NAMESPACE;