home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / Riza / source / capreplay.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  3.7 KB  |  150 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    A/V interface library
  3. //    Copyright (C) 1998-2005 Avery Lee
  4. //
  5. //    This program is free software; you can redistribute it and/or modify
  6. //    it under the terms of the GNU General Public License as published by
  7. //    the Free Software Foundation; either version 2 of the License, or
  8. //    (at your option) any later version.
  9. //
  10. //    This program is distributed in the hope that it will be useful,
  11. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. //    GNU General Public License for more details.
  14. //
  15. //    You should have received a copy of the GNU General Public License
  16. //    along with this program; if not, write to the Free Software
  17. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. #include <vd2/system/file.h>
  20. #include <vd2/system/math.h>
  21. #include <vd2/system/vdstl.h>
  22. #include <vd2/system/error.h>
  23. #include <vd2/Riza/capdriver.h>
  24. #include <vd2/Riza/capreplay.h>
  25.  
  26. ///////////////////////////////////////////////////////////////////////////
  27.  
  28. class VDCaptureReplayDriver : public IVDCaptureReplayDriver {
  29. public:
  30.     VDCaptureReplayDriver();
  31.     ~VDCaptureReplayDriver();
  32.  
  33.     void SetChildCallback(IVDCaptureDriverCallback *pChild);
  34.  
  35.     void Init(const wchar_t *filename);
  36.     bool ReplayNext();
  37.  
  38. protected:
  39.     IVDCaptureDriverCallback *mpCB;
  40.  
  41.     struct Event {
  42.         bool    audio;
  43.         bool    key;
  44.         sint64    captime;
  45.         sint64    globaltime;
  46.         uint32    bytes;
  47.     };
  48.  
  49.     struct ReverseEventSorter {
  50.         bool operator()(const Event& ev1, const Event& ev2) const {
  51.             return ev1.globaltime > ev2.globaltime;
  52.         }
  53.     };
  54.  
  55.     std::vector<Event> mEvents;
  56.     vdblock<char> mDummyData;
  57. };
  58.  
  59. VDCaptureReplayDriver::VDCaptureReplayDriver() {
  60. }
  61.  
  62. VDCaptureReplayDriver::~VDCaptureReplayDriver() {
  63. }
  64.  
  65. IVDCaptureReplayDriver *VDCreateCaptureReplayDriver() {
  66.     return new VDCaptureReplayDriver;
  67. }
  68.  
  69. void VDCaptureReplayDriver::SetChildCallback(IVDCaptureDriverCallback *pChild) {
  70.     mpCB = pChild;
  71. }
  72.  
  73. void VDCaptureReplayDriver::Init(const wchar_t *filename) {
  74.     VDTextInputFile ifile(filename);
  75.  
  76.     // skip first line
  77.     ifile.GetNextLine();
  78.  
  79.     uint32 largestDataBlock = 0;
  80.  
  81.     while(const char *txt = ifile.GetNextLine()) {
  82.         const char *ranges[9][2];
  83.  
  84.         for(int i=0; i<9; ++i) {
  85.             ranges[i][0] = txt;
  86.             ranges[i][1] = strchr(txt, ',');
  87.             if (!ranges[i][1])
  88.                 ranges[i][1] = txt + strlen(txt);
  89.  
  90.             txt = ranges[i][1];
  91.             if (*txt)
  92.                 ++txt;
  93.         }
  94.  
  95.         if (ranges[1][0] != ranges[1][1]) {
  96.             Event vev;
  97.  
  98.             vev.audio = false;
  99.             vev.key = atoi(ranges[4][0]) > 0;
  100.             vev.captime = VDRoundToInt64(strtod(ranges[1][0], NULL) * 1000.0);
  101.             vev.globaltime = VDRoundToInt64(strtod(ranges[2][0], NULL) * 1000.0);
  102.             vev.bytes = atoi(ranges[3][0]);
  103.  
  104.             if (largestDataBlock < vev.bytes)
  105.                 largestDataBlock = vev.bytes;
  106.  
  107.             mEvents.push_back(vev);
  108.         }
  109.  
  110.         if (ranges[5][0] != ranges[5][1]) {
  111.             Event aev;
  112.  
  113.             aev.audio = true;
  114.             aev.key = false;
  115.             aev.globaltime = VDRoundToInt64(strtod(ranges[7][0], NULL) * 1000.0);
  116.             aev.captime = aev.globaltime;
  117.             aev.bytes = atoi(ranges[8][0]);
  118.  
  119.             if (largestDataBlock < aev.bytes)
  120.                 largestDataBlock = aev.bytes;
  121.  
  122.             mEvents.push_back(aev);
  123.         }
  124.     }
  125.  
  126.     mDummyData.resize(largestDataBlock);
  127.  
  128.     std::sort(mEvents.begin(), mEvents.end(), ReverseEventSorter());
  129.  
  130.     mpCB->CapBegin(0);
  131. }
  132.  
  133. bool VDCaptureReplayDriver::ReplayNext() {
  134.     if (mEvents.empty())
  135.         return false;
  136.  
  137.     Event& ev = mEvents.back();
  138.  
  139.     try {
  140.         mpCB->CapProcessData(ev.audio ? 1 : 0, mDummyData.data(), ev.bytes, ev.captime, ev.key, ev.globaltime);
  141.     } catch(const MyError& e) {
  142.         mpCB->CapEnd(&e);
  143.         return false;
  144.     }
  145.  
  146.     mEvents.pop_back();
  147.  
  148.     return true;
  149. }
  150.