2 Copyright 2004-2005 Chris Tallon
\r
4 This file is part of VOMP.
\r
6 VOMP is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 VOMP is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with VOMP; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
21 #include "videowin.h"
\r
23 #include "dssourcefilter.h"
\r
24 #include "dsallocator.h"
\r
28 void AdjustWindow();
\r
32 VideoWin::VideoWin()
\r
34 dsgraphbuilder=NULL;
\r
35 dsmediacontrol=NULL;
\r
43 dsvmrsurfnotify=NULL;
\r
44 filtermutex=CreateMutex(NULL,FALSE,NULL);
\r
46 offsetvideonotset=true;
\r
47 offsetaudionotset=true;
\r
54 cur_audio_media_sample=NULL;
\r
55 cur_video_media_sample=NULL;
\r
66 VideoWin::~VideoWin()
\r
69 CloseHandle(filtermutex);
\r
76 int VideoWin::init(UCHAR tformat)
\r
78 if (initted) return 0;
\r
81 tvsize=Video::ASPECT16X9; //Internally Vomp should think we are a 16:9 TV
\r
85 if (!setFormat(tformat)){ shutdown(); return 0; }
\r
89 int VideoWin::setTVsize(UCHAR ttvsize)
\r
91 pseudotvsize=ttvsize;
\r
95 int VideoWin::setDefaultAspect()
\r
97 return setAspectRatio(Video::ASPECT4X3);
\r
100 int VideoWin::shutdown()
\r
102 if (!initted) return 0;
\r
107 int VideoWin::setFormat(UCHAR tformat)
\r
109 if (!initted) return 0;
\r
110 if ((tformat != PAL) && (tformat != NTSC)) return 0;
\r
112 if (format == NTSC)
\r
115 screenHeight = 480;
\r
120 screenHeight = 576;
\r
126 int VideoWin::setConnection(UCHAR tconnection)
\r
128 if (!initted) return 0;
\r
129 if ((tconnection != COMPOSITERGB) && (tconnection != SVIDEO)) return 0;
\r
130 connection = tconnection;
\r
135 int VideoWin::setAspectRatio(UCHAR taspectRatio)
\r
137 if (!initted) return 0;
\r
138 if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
\r
139 aspectRatio = taspectRatio;
\r
144 int VideoWin::setMode(UCHAR tmode)
\r
146 if (!initted) return 0;
\r
148 //if ((tmode == LETTERBOX) && (tvsize == ASPECT16X9)) return 0; // invalid mode
\r
150 if ((tmode != NORMAL) && (tmode != LETTERBOX) && (tmode != UNKNOWN2) && (tmode != QUARTER) && (tmode != EIGHTH)
\r
151 && (tmode != ZOOM) && (tmode != UNKNOWN6)) return 0;
\r
160 int VideoWin::signalOff()
\r
165 int VideoWin::signalOn()
\r
170 int VideoWin::setSource()
\r
172 if (!initted) return 0;
\r
177 int VideoWin::setPosition(int x, int y)
\r
179 if (!initted) return 0;
\r
180 if (mode==QUARTER || mode==EIGHTH) {
\r
187 int VideoWin::sync()
\r
189 if (!initted) return 0;
\r
194 #ifdef DS_DEBUG // This stuff would not included in vomp due to lincemse restrcitions
\r
195 #include "dshelper.h"
\r
200 int VideoWin::play()
\r
202 if (!initted) return 0;
\r
204 //Build filter graph
\r
207 if (hres=CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC_SERVER,
\r
208 IID_IGraphBuilder,(void**)&dsgraphbuilder)!=S_OK) {
\r
212 AddToRot(dsgraphbuilder,&graphidentifier);
\r
214 //This is just a try to see if building the graph works
\r
215 // dsgraphbuilder->RenderFile(L"D:\\Projekte\\VTP Client\\test.mpa" ,NULL);
\r
216 //So this is the real code, this prevents the feeder from calling noexisting objects!
\r
217 WaitForSingleObject(filtermutex,INFINITE);
\r
218 firstsynched=false;
\r
219 sourcefilter=new DsSourceFilter(); //Creating our Source filter for pushing Data
\r
221 if (hres=dsgraphbuilder->AddFilter(sourcefilter,L"Vomp Win Source Filter")!=S_OK) {
\r
222 Log::getInstance()->log("VideoWin", Log::WARN , "Failed adding Vomp Source Filter!");
\r
224 ReleaseMutex(filtermutex);
\r
228 if (hres=dsgraphbuilder->Render(sourcefilter->GetPin(0)/*audio*/)!=S_OK) {
\r
229 Log::getInstance()->log("VideoWin", Log::WARN , "Failed rendering audio!");
\r
231 ReleaseMutex(filtermutex);
\r
237 //We alloc the vmr9 as next step
\r
238 if (hres=CoCreateInstance(CLSID_VideoMixingRenderer9,0,
\r
239 CLSCTX_INPROC_SERVER,IID_IBaseFilter,(void**) &dsvmrrenderer)!=S_OK) {
\r
240 Log::getInstance()->log("VideoWin", Log::WARN ,"Failed creating VMR9 renderer!");
\r
242 ReleaseMutex(filtermutex);
\r
245 if (hres=dsgraphbuilder->AddFilter(dsvmrrenderer,L"VMR9")!=S_OK) {
\r
247 Log::getInstance()->log("VideoWin", Log::WARN ,"Failed adding VMR9 renderer!");
\r
248 ReleaseMutex(filtermutex);
\r
251 IVMRFilterConfig9* vmrfilconfig;
\r
252 if (dsvmrrenderer->QueryInterface(IID_IVMRFilterConfig9,(void**)&vmrfilconfig)!=S_OK) {
\r
254 Log::getInstance()->log("VideoWin", Log::WARN ,"Failed getting VMR9 Filterconfig interface!");
\r
255 ReleaseMutex(filtermutex);
\r
258 vmrfilconfig->SetRenderingMode(VMR9Mode_Renderless);
\r
259 vmrfilconfig->Release();
\r
261 if (dsvmrrenderer->QueryInterface(IID_IVMRSurfaceAllocatorNotify9,(void**)& dsvmrsurfnotify)!=S_OK) {
\r
263 Log::getInstance()->log("VideoWin", Log::WARN ,"Failed getting VMR9 Surface Allocator interface!");
\r
264 ReleaseMutex(filtermutex);
\r
267 allocatorvmr=new DsAllocator();
\r
268 dsvmrsurfnotify->AdviseSurfaceAllocator(NULL,allocatorvmr);
\r
269 allocatorvmr->AdviseNotify(dsvmrsurfnotify);
\r
273 /*VMR 9 stuff end */
\r
274 IFilterGraph2*fg2=NULL;
\r
275 if (dsgraphbuilder->QueryInterface(IID_IFilterGraph2,(void**)&fg2)!=S_OK) {
\r
276 Log::getInstance()->log("VideoWin", Log::WARN , "Failed querying for FilterGraph2 Interface!");
\r
278 ReleaseMutex(filtermutex);
\r
281 if (hres=fg2->RenderEx(sourcefilter->GetPin(1)/*video*/,
\r
282 AM_RENDEREX_RENDERTOEXISTINGRENDERERS,NULL)!=S_OK) {
\r
283 Log::getInstance()->log("VideoWin", Log::WARN , "Failed rendering Video!");
\r
285 ReleaseMutex(filtermutex);
\r
290 if (hres=CoCreateInstance(CLSID_SystemClock,NULL,CLSCTX_INPROC_SERVER,
\r
291 IID_IReferenceClock,(void**)&dsrefclock)!=S_OK) {
\r
295 dsgraphbuilder->QueryInterface(IID_IMediaFilter,(void **) &dsmediafilter);
\r
296 dsmediafilter->SetSyncSource(dsrefclock);
\r
298 dsgraphbuilder->QueryInterface(IID_IMediaControl,(void **) &dsmediacontrol);
\r
299 dsgraphbuilder->QueryInterface(IID_IBasicAudio,(void **) &dsbasicaudio);
\r
301 dsmediacontrol->Run();
\r
302 ReleaseMutex(filtermutex);
\r
306 int VideoWin::stop()
\r
308 if (!initted) return 0;
\r
316 int VideoWin::reset()
\r
318 if (!initted) return 0;
\r
326 int VideoWin::pause()
\r
328 if (!initted) return 0;
\r
329 if (dsmediacontrol) dsmediacontrol->Pause();
\r
333 int VideoWin::unPause() // FIXME get rid - same as play!!
\r
334 {//No on windows this is not the same, I don't get rid of!
\r
335 if (!initted) return 0;
\r
336 if (dsmediacontrol) dsmediacontrol->Run();
\r
340 int VideoWin::fastForward()
\r
342 if (!initted) return 0;
\r
347 int VideoWin::unFastForward()
\r
349 if (!initted) return 0;
\r
354 int VideoWin::attachFrameBuffer()
\r
356 if (!initted) return 0;
\r
360 int VideoWin::blank(void)
\r
362 ((OsdWin*)Osd::getInstance())->Blank();
\r
366 ULLONG VideoWin::getCurrentTimestamp()
\r
368 REFERENCE_TIME startoffset;
\r
369 REFERENCE_TIME ncr_time;
\r
371 if (!dsrefclock || !sourcefilter) return 0;
\r
372 FILTER_STATE state;
\r
373 sourcefilter->GetState(10,&state);
\r
375 if (state==State_Running) dsrefclock->GetTime(&cr_time);
\r
377 startoffset=sourcefilter->getStartOffset();
\r
378 ncr_time-=startoffset;
\r
379 ncr_time-=lastreftimeRT;
\r
380 /* ULLONG result=frameNumberToTimecode(
\r
381 VDR::getInstance()->frameNumberFromPosition(lastreftimeBYTE));*/
\r
382 ULLONG result=lastreftimePTS;
\r
383 result+=(ULLONG)(ncr_time/10000LL*90LL);
\r
388 ULONG VideoWin::timecodeToFrameNumber(ULLONG timecode)
\r
390 if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
\r
391 else return (ULONG)(((double)timecode / (double)90000) * (double)30);
\r
394 ULLONG VideoWin::frameNumberToTimecode(ULONG framenumber)
\r
396 if (format == PAL) return (ULLONG)(((double)framenumber * (double)90000) / (double)25);
\r
397 else return (ULLONG)(((double)framenumber * (double)90000) / (double)30);
\r
400 void VideoWin::CleanupDS()
\r
402 WaitForSingleObject(filtermutex,INFINITE);
\r
403 if (cur_audio_media_sample) {
\r
404 cur_audio_media_sample->Release();
\r
405 cur_audio_media_sample=NULL;
\r
407 if (cur_video_media_sample) {
\r
408 cur_video_media_sample->Release();
\r
409 cur_video_media_sample=NULL;
\r
411 if (dsbasicaudio) {
\r
412 dsbasicaudio->Release();
\r
415 if (dsvmrsurfnotify) {
\r
416 dsvmrsurfnotify->Release();
\r
417 dsvmrsurfnotify=NULL;
\r
419 if (dsvmrrenderer) {
\r
420 dsvmrrenderer->Release();
\r
421 dsvmrrenderer=NULL;
\r
424 if (allocatorvmr) {
\r
425 allocatorvmr->Release();
\r
430 dsrefclock->Release();
\r
433 if (dsmediafilter) {
\r
434 dsmediafilter->Release();
\r
435 dsmediafilter=NULL;
\r
439 if (dsmediacontrol) {
\r
440 dsmediacontrol->Stop();
\r
441 dsmediacontrol->Release();
\r
442 dsmediacontrol=NULL;
\r
444 if (dsgraphbuilder){
\r
446 RemoveFromRot(graphidentifier);
\r
448 dsgraphbuilder->Release();
\r
449 dsgraphbuilder=NULL;
\r
450 sourcefilter=NULL; //The Graph Builder destroys our SourceFilter
\r
452 ReleaseMutex(filtermutex);
\r
456 void VideoWin::PrepareMediaSample(const MediaPacketList& mplist,
\r
459 mediapacket = mplist.front();
\r
462 UINT VideoWin::DeliverMediaSample(const UCHAR* buffer, UINT *samplepos)
\r
464 DeliverMediaPacket(mediapacket, buffer, samplepos);
\r
465 if (*samplepos == mediapacket.length) {
\r
472 UINT VideoWin::DeliverMediaPacket(MediaPacket packet,
\r
473 const UCHAR* buffer,
\r
476 /*First Check, if we have an audio sample*/
\r
478 /*First Check, if we have an audio sample*/
\r
480 IMediaSample* ms=NULL;
\r
481 REFERENCE_TIME reftime1=0;
\r
482 REFERENCE_TIME reftime2=0;
\r
484 UINT headerstrip=0;
\r
485 if (packet.disconti) {
\r
486 firstsynched=false;
\r
487 DeliverVideoMediaSample();
\r
492 /*Inspect PES-Header */
\r
494 if (*samplepos==0) {//stripheader
\r
495 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
\r
496 *samplepos+=headerstrip;
\r
497 if ( packet.synched ) {
\r
498 DeliverVideoMediaSample();//write out old data
\r
499 /* if (packet.presentation_time<0) { //Preroll?
\r
500 *samplepos=packet.length;//if we have not processed at least one
\r
501 return packet.length;//synched packet ignore it!
\r
504 reftime1=packet.presentation_time;
\r
505 reftime2=reftime1+1;
\r
508 if (!firstsynched) {//
\r
509 *samplepos=packet.length;//if we have not processed at least one
\r
510 return packet.length;//synched packet ignore it!
\r
518 if (!getCurrentVideoMediaSample(&ms) || ms==NULL) {// get the current sample
\r
523 ms_pos=ms->GetActualDataLength();
\r
524 ms_length=ms->GetSize();
\r
525 haveToCopy=min(ms_length-ms_pos,packet.length-*samplepos);
\r
526 if ((ms_length-ms_pos)<1) {
\r
527 DeliverVideoMediaSample(); //we are full!
\r
528 if (!getCurrentVideoMediaSample(&ms) || ms==NULL) {// get the current sample
\r
533 ms_pos=ms->GetActualDataLength();
\r
534 ms_length=ms->GetSize();
\r
535 haveToCopy=min(ms_length-ms_pos,packet.length-*samplepos);
\r
537 ms->GetPointer(&ms_buf);
\r
540 if (ms_pos==0) {//will only be changed on first packet
\r
541 if (packet.disconti) {
\r
542 ms->SetDiscontinuity(TRUE);
\r
544 ms->SetDiscontinuity(FALSE);
\r
546 if (packet.synched) {
\r
547 ms->SetSyncPoint(TRUE);
\r
548 ms->SetTime(&reftime1,&reftime2);
\r
549 //ms->SetTime(NULL,NULL);
\r
550 ms->SetMediaTime(NULL, NULL);
\r
551 if (reftime1<0) ms->SetPreroll(TRUE);
\r
552 else ms->SetPreroll(FALSE);
\r
553 /*Timecode handling*/
\r
554 lastreftimeRT=reftime1;
\r
555 lastreftimePTS=packet.pts;
\r
558 ms->SetSyncPoint(FALSE);
\r
559 ms->SetTime(NULL,NULL);
\r
560 ms->SetMediaTime(NULL, NULL);
\r
561 ms->SetPreroll(FALSE);
\r
563 // ms->SetSyncPoint(TRUE);
\r
568 memcpy(ms_buf+ms_pos,buffer+packet.pos_buffer+*samplepos,haveToCopy);
\r
569 ms->SetActualDataLength(haveToCopy+ms_pos);
\r
571 *samplepos+=haveToCopy;
\r
573 return haveToCopy+headerstrip;
\r
577 *samplepos+=packet.length;
\r
578 MILLISLEEP(0); //yet not implemented//bad idea
\r
579 return packet.length;
\r
583 int VideoWin::getCurrentAudioMediaSample(IMediaSample** ms)
\r
585 //WaitForSingleObject(filtermutex,INFINITE);
\r
586 if (!sourcefilter){
\r
587 // ReleaseMutex(filtermutex);
\r
590 if (cur_audio_media_sample) {
\r
591 *ms=cur_audio_media_sample;//already open
\r
594 if (!sourcefilter->getCurrentAudioMediaSample(ms)) {
\r
595 // ReleaseMutex(filtermutex);
\r
597 if (*ms) (*ms)->SetActualDataLength(0);
\r
598 cur_audio_media_sample=*ms;
\r
599 //Don't release the mutex before deliver
\r
603 int VideoWin::getCurrentVideoMediaSample(IMediaSample** ms)
\r
605 //WaitForSingleObject(filtermutex,INFINITE);
\r
606 if (!sourcefilter){
\r
607 // ReleaseMutex(filtermutex);
\r
610 if (cur_video_media_sample) {
\r
611 *ms=cur_video_media_sample;//already open
\r
614 if (!sourcefilter->getCurrentVideoMediaSample(ms)) {
\r
615 // ReleaseMutex(filtermutex);
\r
617 if (*ms) (*ms)->SetActualDataLength(0);
\r
619 cur_video_media_sample=*ms;
\r
620 //Don't release the mutex before deliver
\r
624 int VideoWin::DeliverAudioMediaSample(){
\r
625 if (cur_audio_media_sample) {
\r
626 sourcefilter->DeliverAudioMediaSample(cur_audio_media_sample);
\r
627 cur_audio_media_sample=NULL;
\r
629 //ReleaseMutex(filtermutex);
\r
633 int VideoWin::DeliverVideoMediaSample(){
\r
634 if (cur_video_media_sample) {
\r
635 sourcefilter->DeliverVideoMediaSample(cur_video_media_sample);
\r
636 cur_video_media_sample=NULL;
\r
638 //ReleaseMutex(filtermutex);
\r
642 long long VideoWin::SetStartOffset(long long curreftime, bool *rsync)
\r
645 if (offsetnotset) {
\r
646 startoffset=curreftime;//offset is set for audio
\r
647 offsetnotset=false;
\r
648 offsetvideonotset=false;
\r
652 if (offsetvideonotset) {
\r
653 offsetvideonotset=false;
\r
656 if ( (curreftime-lastrefvideotime)>10000000LL
\r
657 || (curreftime-lastrefvideotime)<-10000000LL) {//if pts jumps to big resync
\r
658 startoffset+=curreftime-lastrefvideotime;
\r
659 lastrefaudiotime+=curreftime-lastrefvideotime;
\r
661 offsetaudionotset=true;
\r
668 lastrefvideotime=curreftime;
\r
670 return startoffset;
\r
674 long long VideoWin::SetStartAudioOffset(long long curreftime, bool *rsync)
\r
677 if (offsetnotset) {
\r
678 startoffset=curreftime;
\r
679 offsetnotset=false;
\r
680 offsetaudionotset=false;
\r
682 if (offsetaudionotset) {
\r
683 offsetaudionotset=false;
\r
686 if ( (curreftime-lastrefaudiotime)>10000000LL
\r
687 || (curreftime-lastrefaudiotime)<-10000000LL) {//if pts jumps to big resync
\r
688 startoffset+=curreftime-lastrefaudiotime;
\r
689 lastrefvideotime+=curreftime-lastrefaudiotime;
\r
691 offsetvideonotset=true;
\r
697 lastrefaudiotime=curreftime;
\r
698 return startoffset;
\r
701 void VideoWin::ResetTimeOffsets() {
\r
702 offsetnotset=true; //called from demuxer
\r
703 offsetvideonotset=true;
\r
704 offsetaudionotset=true;
\r
706 lastrefaudiotime=0;
\r
707 lastrefvideotime=0;
\r
714 void VideoWin::SetAudioVolume(long volume)
\r
716 if (dsbasicaudio) dsbasicaudio->put_Volume(volume);
\r
720 int VideoWin::test()
\r
725 int VideoWin::test2()
\r