]> git.vomp.tv Git - vompclient.git/blob - videowin.cc
A little convergence to NEW_DEMUXER
[vompclient.git] / videowin.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "videowin.h"
22 #include "log.h"
23 #include "dssourcefilter.h"
24 #include "dsallocator.h"
25 #include "vdr.h"
26
27 void AdjustWindow();
28
29
30
31 VideoWin::VideoWin()
32 {
33   dsgraphbuilder=NULL;
34   dsmediacontrol=NULL;
35   dsvmrrenderer=NULL;
36   dsrefclock=NULL;
37   dsmediafilter=NULL;
38   sourcefilter=NULL;
39   allocatorvmr=NULL;
40   dsvmrsurfnotify=NULL;
41   filtermutex=CreateMutex(NULL,FALSE,NULL);
42   offsetnotset=true;
43   offsetvideonotset=true;
44   offsetaudionotset=true;
45   startoffset=0;
46   lastrefaudiotime=0;
47   lastrefvideotime=0;
48   lastreftimeBYTE=0;
49   lastreftimeRT=0;
50   firstsynched=false;
51   cur_audio_media_sample=NULL;
52   cur_video_media_sample=NULL;
53   videoon=true;
54   audioon=true;
55   pseudotvsize=0;
56   videoposx=0;
57   videoposy=0;
58
59
60
61 }
62
63 VideoWin::~VideoWin()
64 {
65   CleanupDS();
66   CloseHandle(filtermutex);
67
68
69
70   instance = NULL;
71 }
72
73 int VideoWin::init(UCHAR tformat)
74 {
75   if (initted) return 0;
76
77   initted = 1;
78   tvsize=Video::ASPECT16X9; //Internally Vomp should think we are a 16:9 TV
79   videoposx=0;
80   videoposy=0;
81
82   if (!setFormat(tformat)){ shutdown(); return 0; }
83   return 1;
84 }
85
86 int VideoWin::setTVsize(UCHAR ttvsize)
87 {
88   pseudotvsize=ttvsize;
89   return 1;
90 }
91
92 int VideoWin::setDefaultAspect()
93 {
94   return setAspectRatio(Video::ASPECT4X3);
95 }
96
97 int VideoWin::shutdown()
98 {
99   if (!initted) return 0;
100   initted = 0;
101   return 1;
102 }
103
104 int VideoWin::setFormat(UCHAR tformat)
105 {
106   if (!initted) return 0;
107   if ((tformat != PAL) && (tformat != NTSC)) return 0;
108   format = tformat;
109   if (format == NTSC)
110   {
111     screenWidth = 720;
112     screenHeight = 480;
113   }
114   if (format == PAL)
115   {
116     screenWidth = 720;
117     screenHeight = 576;
118   }
119
120   return 1;
121 }
122
123 int VideoWin::setConnection(UCHAR tconnection)
124 {
125   if (!initted) return 0;
126   if ((tconnection != COMPOSITERGB) && (tconnection != SVIDEO)) return 0;
127   connection = tconnection;
128
129   return 1;
130 }
131
132 int VideoWin::setAspectRatio(UCHAR taspectRatio)
133 {
134   if (!initted) return 0;
135   if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
136   aspectRatio = taspectRatio;
137   AdjustWindow();
138   return 1;
139 }
140
141 int VideoWin::setMode(UCHAR tmode)
142 {
143   if (!initted) return 0;
144
145   //if ((tmode == LETTERBOX) && (tvsize == ASPECT16X9)) return 0; // invalid mode
146
147   if ((tmode != NORMAL) && (tmode != LETTERBOX) && (tmode != UNKNOWN2) && (tmode != QUARTER) && (tmode != EIGHTH)
148       && (tmode != ZOOM) && (tmode != UNKNOWN6)) return 0;
149   mode = tmode;
150   videoposx=0;
151   videoposy=0;
152   AdjustWindow();
153
154   return 1;
155 }
156
157 int VideoWin::signalOff()
158 {
159   return 1;
160 }
161
162 int VideoWin::signalOn()
163 {
164   return 1;
165 }
166
167 int VideoWin::setSource()
168 {
169   if (!initted) return 0;
170
171   return 1;
172 }
173
174 int VideoWin::setPosition(int x, int y)
175 {
176   if (!initted) return 0;
177   if (mode==QUARTER || mode==EIGHTH) {
178   videoposx=x;
179   videoposy=y;
180   }
181   return 1;
182 }
183
184 int VideoWin::sync()
185 {
186   if (!initted) return 0;
187
188   return 1;
189 }
190
191 #ifdef DS_DEBUG // This stuff would not included in vomp due to lincemse restrcitions
192 #include "dshelper.h"
193 #endif
194
195 #define DO_VIDEO
196
197 int VideoWin::play()
198 {
199   if (!initted) return 0;
200
201   //Build filter graph
202   HRESULT hres;
203
204   if (hres=CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC_SERVER,
205     IID_IGraphBuilder,(void**)&dsgraphbuilder)!=S_OK) {
206       return 0;
207    }
208    #ifdef DS_DEBUG
209    AddToRot(dsgraphbuilder,&graphidentifier);
210    #endif
211    //This is just a try to see if building the graph works
212 //   dsgraphbuilder->RenderFile(L"D:\\Projekte\\VTP Client\\test.mpa" ,NULL);
213    //So this is the real code, this prevents the feeder from calling noexisting objects!
214    WaitForSingleObject(filtermutex,INFINITE);
215    firstsynched=false;
216    sourcefilter=new DsSourceFilter(); //Creating our Source filter for pushing Data
217    // to DirectShow
218    if (hres=dsgraphbuilder->AddFilter(sourcefilter,L"Vomp Win Source Filter")!=S_OK) {
219    Log::getInstance()->log("VideoWin", Log::WARN , "Failed adding Vomp Source Filter!");
220      CleanupDS();
221      ReleaseMutex(filtermutex);
222      return 0;
223    }
224    //if (audioon) {
225      if (hres=dsgraphbuilder->Render(sourcefilter->GetPin(0)/*audio*/)!=S_OK) {
226      Log::getInstance()->log("VideoWin", Log::WARN , "Failed rendering audio!");
227        CleanupDS();
228        ReleaseMutex(filtermutex);
229        return 0;
230      }
231    //}
232 #ifdef DO_VIDEO
233     if (videoon) {
234     //We alloc the vmr9 as next step
235     if (hres=CoCreateInstance(CLSID_VideoMixingRenderer9,0,
236       CLSCTX_INPROC_SERVER,IID_IBaseFilter,(void**) &dsvmrrenderer)!=S_OK) {
237       Log::getInstance()->log("VideoWin", Log::WARN ,"Failed creating VMR9 renderer!");
238       CleanupDS();
239       ReleaseMutex(filtermutex);
240     }
241       /*VMR 9 stuff**/
242     if (hres=dsgraphbuilder->AddFilter(dsvmrrenderer,L"VMR9")!=S_OK) {
243       CleanupDS();
244       Log::getInstance()->log("VideoWin", Log::WARN ,"Failed adding VMR9 renderer!");
245       ReleaseMutex(filtermutex);
246       return 0;
247     }
248     IVMRFilterConfig9* vmrfilconfig;
249     if (dsvmrrenderer->QueryInterface(IID_IVMRFilterConfig9,(void**)&vmrfilconfig)!=S_OK) {
250       CleanupDS();
251       Log::getInstance()->log("VideoWin", Log::WARN ,"Failed getting VMR9 Filterconfig interface!");
252       ReleaseMutex(filtermutex);
253       return 0;
254     }
255     vmrfilconfig->SetRenderingMode(VMR9Mode_Renderless);
256     vmrfilconfig->Release();
257
258     if (dsvmrrenderer->QueryInterface(IID_IVMRSurfaceAllocatorNotify9,(void**)& dsvmrsurfnotify)!=S_OK) {
259       CleanupDS();
260       Log::getInstance()->log("VideoWin", Log::WARN ,"Failed getting VMR9 Surface Allocator interface!");
261       ReleaseMutex(filtermutex);
262       return 0;
263     }
264     allocatorvmr=new DsAllocator();
265     dsvmrsurfnotify->AdviseSurfaceAllocator(NULL,allocatorvmr);
266     allocatorvmr->AdviseNotify(dsvmrsurfnotify);
267
268
269
270     /*VMR 9 stuff end */
271     IFilterGraph2*fg2=NULL;
272     if (dsgraphbuilder->QueryInterface(IID_IFilterGraph2,(void**)&fg2)!=S_OK) {
273       Log::getInstance()->log("VideoWin", Log::WARN , "Failed querying for FilterGraph2 Interface!");
274       CleanupDS();
275       ReleaseMutex(filtermutex);
276       return 0;
277     }
278     if (hres=fg2->RenderEx(sourcefilter->GetPin(1)/*video*/,
279         AM_RENDEREX_RENDERTOEXISTINGRENDERERS,NULL)!=S_OK) {
280       Log::getInstance()->log("VideoWin", Log::WARN , "Failed rendering Video!");
281       CleanupDS();
282       ReleaseMutex(filtermutex);
283       return 0;
284     }
285    }
286 #endif
287    if (hres=CoCreateInstance(CLSID_SystemClock,NULL,CLSCTX_INPROC_SERVER,
288     IID_IReferenceClock,(void**)&dsrefclock)!=S_OK) {
289       return 0;
290    }
291
292    dsgraphbuilder->QueryInterface(IID_IMediaFilter,(void **) &dsmediafilter);
293    dsmediafilter->SetSyncSource(dsrefclock);
294
295    dsgraphbuilder->QueryInterface(IID_IMediaControl,(void **) &dsmediacontrol);
296
297    dsmediacontrol->Run();
298    ReleaseMutex(filtermutex);
299   return 1;
300 }
301
302 int VideoWin::stop()
303 {
304   if (!initted) return 0;
305
306   CleanupDS();
307
308
309   return 1;
310 }
311
312 int VideoWin::reset()
313 {
314   if (!initted) return 0;
315   videoposx=0;
316   videoposy=0;
317
318   return 1;
319 }
320
321 int VideoWin::pause()
322 {
323   if (!initted) return 0;
324   if (dsmediacontrol) dsmediacontrol->Pause();
325   return 1;
326 }
327
328 int VideoWin::unPause() // FIXME get rid - same as play!!
329 {//No on windows this is not the same, I don't get rid of!
330   if (!initted) return 0;
331   if (dsmediacontrol) dsmediacontrol->Run();
332   return 1;
333 }
334
335 int VideoWin::fastForward()
336 {
337   if (!initted) return 0;
338   return 1;
339 }
340
341 int VideoWin::unFastForward()
342 {
343   if (!initted) return 0;
344   return 1;
345 }
346
347 int VideoWin::attachFrameBuffer()
348 {
349   if (!initted) return 0;
350   return 1;
351 }
352
353 int VideoWin::blank(void)
354 {
355   return 1;
356 }
357
358 ULLONG VideoWin::getCurrentTimestamp()
359 {
360   REFERENCE_TIME cr_time,startoffset;
361
362   if (!dsrefclock || !sourcefilter) return 0;
363
364   dsrefclock->GetTime(&cr_time);
365   startoffset=sourcefilter->getStartOffset();
366   cr_time-=startoffset;
367   cr_time-=lastreftimeRT;
368   ULLONG result=frameNumberToTimecode(
369     VDR::getInstance()->frameNumberFromPosition(lastreftimeBYTE));
370   result+=(ULLONG)(cr_time/10000LL*90LL);
371   return result;
372
373 }
374
375 ULONG VideoWin::timecodeToFrameNumber(ULLONG timecode)
376 {
377   if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
378   else               return (ULONG)(((double)timecode / (double)90000) * (double)30);
379 }
380
381 ULLONG VideoWin::frameNumberToTimecode(ULONG framenumber)
382 {
383   if (format == PAL) return (ULLONG)(((double)framenumber * (double)90000) / (double)25);
384   else               return (ULLONG)(((double)framenumber * (double)90000) / (double)30);
385 }
386
387 void VideoWin::CleanupDS()
388 {
389   WaitForSingleObject(filtermutex,INFINITE);
390   if (cur_audio_media_sample) {
391     cur_audio_media_sample->Release();
392     cur_audio_media_sample=NULL;
393   }
394   if (cur_video_media_sample) {
395     cur_video_media_sample->Release();
396     cur_video_media_sample=NULL;
397   }
398   if (dsvmrsurfnotify) {
399     dsvmrsurfnotify->Release();
400     dsvmrsurfnotify=NULL;
401   }
402   if (dsvmrrenderer) {
403     dsvmrrenderer->Release();
404     dsvmrrenderer=NULL;
405   }
406
407   if (allocatorvmr) {
408     allocatorvmr->Release();
409     allocatorvmr=NULL;
410   }
411
412   if (dsrefclock) {
413     dsrefclock->Release();
414     dsrefclock=NULL;
415   }
416   if (dsmediafilter) {
417     dsmediafilter->Release();
418     dsmediafilter=NULL;
419   }
420
421   if (dsmediacontrol) {
422     dsmediacontrol->Stop();
423     dsmediacontrol->Release();
424     dsmediacontrol=NULL;
425   }
426   if (dsgraphbuilder){
427 #ifdef DS_DEBUG
428     RemoveFromRot(graphidentifier);
429 #endif
430     dsgraphbuilder->Release();
431     dsgraphbuilder=NULL;
432     sourcefilter=NULL; //The Graph Builder destroys our SourceFilter
433   }
434   ReleaseMutex(filtermutex);
435
436 }
437
438
439 UINT VideoWin::DeliverMediaSample(MediaPacket packet,
440      UCHAR* buffer,
441      UINT *samplepos)
442 {
443   /*First Check, if we have an audio sample*/
444 #ifdef DO_VIDEO
445   /*First Check, if we have an audio sample*/
446
447   IMediaSample* ms=NULL;
448   REFERENCE_TIME reftime1=0;
449   REFERENCE_TIME reftime2=0;
450
451   UINT headerstrip=0;
452   if (packet.disconti) {
453     firstsynched=false;
454     DeliverVideoMediaSample();
455
456   }
457
458
459   /*Inspect PES-Header */
460
461   if (*samplepos==0) {//stripheader
462     headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
463     *samplepos+=headerstrip;
464     if ( packet.synched ) {
465       DeliverVideoMediaSample();//write out old data
466    /*   if (packet.presentation_time<0) { //Preroll?
467         *samplepos=packet.length;//if we have not processed at least one
468         return packet.length;//synched packet ignore it!
469       }*/
470
471       reftime1=packet.presentation_time;
472       reftime2=reftime1+1;
473       firstsynched=true;
474     } else {
475       if (!firstsynched) {//
476         *samplepos=packet.length;//if we have not processed at least one
477         return packet.length;//synched packet ignore it!
478       }
479     }
480   }
481   BYTE *ms_buf;
482   UINT ms_length;
483   UINT ms_pos;
484   UINT haveToCopy;
485   if (!getCurrentVideoMediaSample(&ms) || ms==NULL) {// get the current sample
486     samplepos=0;
487     MILLISLEEP(10);
488     return 0;
489   }
490   ms_pos=ms->GetActualDataLength();
491   ms_length=ms->GetSize();
492   haveToCopy=min(ms_length-ms_pos,packet.length-*samplepos);
493   if ((ms_length-ms_pos)<1) {
494     DeliverVideoMediaSample(); //we are full!
495     if (!getCurrentVideoMediaSample(&ms) || ms==NULL) {// get the current sample
496       samplepos=0;
497       MILLISLEEP(10);
498       return 0;
499     }
500     ms_pos=ms->GetActualDataLength();
501     ms_length=ms->GetSize();
502     haveToCopy=min(ms_length-ms_pos,packet.length-*samplepos);
503   }
504   ms->GetPointer(&ms_buf);
505
506
507   if (ms_pos==0) {//will only be changed on first packet
508     if (packet.disconti) {
509       ms->SetDiscontinuity(TRUE);
510     } else {
511       ms->SetDiscontinuity(FALSE);
512     }
513     if (packet.synched) {
514       ms->SetSyncPoint(TRUE);
515       ms->SetTime(&reftime1,&reftime2);
516       //ms->SetTime(NULL,NULL);
517       ms->SetMediaTime(NULL, NULL);
518     if (reftime1<0) ms->SetPreroll(TRUE);
519     else ms->SetPreroll(FALSE);
520     /*Timecode handling*/
521     lastreftimeRT=reftime1;
522     lastreftimeBYTE=packet.recording_byte_pos;
523
524     }else {
525       ms->SetSyncPoint(FALSE);
526       ms->SetTime(NULL,NULL);
527       ms->SetMediaTime(NULL, NULL);
528     ms->SetPreroll(FALSE);
529
530     //  ms->SetSyncPoint(TRUE);
531     }
532   }
533
534
535   memcpy(ms_buf+ms_pos,buffer+packet.pos_buffer+*samplepos,haveToCopy);
536     ms->SetActualDataLength(haveToCopy+ms_pos);
537
538   *samplepos+=haveToCopy;
539
540   return haveToCopy+headerstrip;
541
542 #else
543
544        *samplepos+=packet.length;
545       MILLISLEEP(0); //yet not implemented//bad idea
546        return packet.length;
547 #endif
548 }
549
550 int VideoWin::getCurrentAudioMediaSample(IMediaSample** ms)
551 {
552   //WaitForSingleObject(filtermutex,INFINITE);
553   if (!sourcefilter){
554   //  ReleaseMutex(filtermutex);
555     return 0;
556   }
557   if (cur_audio_media_sample) {
558     *ms=cur_audio_media_sample;//already open
559     return 1;
560   }
561   if (!sourcefilter->getCurrentAudioMediaSample(ms)) {
562   //  ReleaseMutex(filtermutex);
563   }
564   if (*ms) (*ms)->SetActualDataLength(0);
565   cur_audio_media_sample=*ms;
566   //Don't release the mutex before deliver
567   return 1;
568 }
569
570 int VideoWin::getCurrentVideoMediaSample(IMediaSample** ms)
571 {
572   //WaitForSingleObject(filtermutex,INFINITE);
573   if (!sourcefilter){
574   //  ReleaseMutex(filtermutex);
575     return 0;
576   }
577   if (cur_video_media_sample) {
578     *ms=cur_video_media_sample;//already open
579     return 1;
580   }
581   if (!sourcefilter->getCurrentVideoMediaSample(ms)) {
582   //  ReleaseMutex(filtermutex);
583   }
584   if (*ms) (*ms)->SetActualDataLength(0);
585
586   cur_video_media_sample=*ms;
587   //Don't release the mutex before deliver
588   return 1;
589 }
590
591 int VideoWin::DeliverAudioMediaSample(){
592   if (cur_audio_media_sample) {
593     sourcefilter->DeliverAudioMediaSample(cur_audio_media_sample);
594     cur_audio_media_sample=NULL;
595   }
596   //ReleaseMutex(filtermutex);
597   return 1;
598 }
599
600 int VideoWin::DeliverVideoMediaSample(){
601   if (cur_video_media_sample) {
602     sourcefilter->DeliverVideoMediaSample(cur_video_media_sample);
603     cur_video_media_sample=NULL;
604   }
605   //ReleaseMutex(filtermutex);
606   return 1;
607 }
608
609 long long VideoWin::SetStartOffset(long long curreftime, bool *rsync)
610 {
611   *rsync=false;
612   if (offsetnotset) {
613     startoffset=curreftime;//offset is set for audio
614     offsetnotset=false;
615     offsetvideonotset=false;
616
617
618   } else {
619     if (offsetvideonotset) {
620       offsetvideonotset=false;
621       *rsync=true;
622     } else {
623       if ( (curreftime-lastrefvideotime)>10000000LL
624         || (curreftime-lastrefvideotime)<-10000000LL) {//if pts jumps to big resync
625         startoffset+=curreftime-lastrefvideotime;
626         lastrefaudiotime+=curreftime-lastrefvideotime;
627         //*rsync=true;
628         offsetaudionotset=true;
629
630       }
631     }
632
633   }
634   lastrefvideotime=curreftime;
635   return startoffset;
636
637 }
638
639 long long VideoWin::SetStartAudioOffset(long long curreftime, bool *rsync)
640 {
641   *rsync=false;
642   if (offsetnotset) {
643     startoffset=curreftime;
644     offsetnotset=false;
645     offsetaudionotset=false;
646   }else {
647     if (offsetaudionotset) {
648       offsetaudionotset=false;
649       *rsync=true;
650     } else {
651       if ( (curreftime-lastrefaudiotime)>10000000LL
652         || (curreftime-lastrefaudiotime)<-10000000LL) {//if pts jumps to big resync
653         startoffset+=curreftime-lastrefaudiotime;
654         lastrefvideotime+=curreftime-lastrefaudiotime;
655         //*rsync=true;
656         offsetvideonotset=true;
657
658       }
659     }
660
661   }
662   lastrefaudiotime=curreftime;
663   return startoffset;
664
665 }
666 void VideoWin::ResetTimeOffsets() {
667   offsetnotset=true; //called from demuxer
668   offsetvideonotset=true;
669   offsetaudionotset=true;
670   startoffset=0;
671   lastrefaudiotime=0;
672   lastrefvideotime=0;
673   lastreftimeBYTE=0;
674   lastreftimeRT=0;
675
676
677 }
678
679
680 #ifdef DEV
681 int VideoWin::test()
682 {
683   return 0;
684 }
685
686 int VideoWin::test2()
687 {
688   return 0;
689 }
690 #endif
691 \r