Debian Codegraph @

plugins/libwin32/videocodec/Module.cpp

raw
1 #include "Module.h"
2
3 extern "C" {
4 #include "wine/winreg.h"
5 #include "driver.h"
6 }
7
8 #include "avm_output.h"
9 #include <stdio.h>
10 #include <string.h>
11
12 AVM_BEGIN_NAMESPACE;
13
14 VideoCodecControl control;
15
16 VideoCodecControl::~VideoCodecControl()
17 {
18 printf("DELETE VIDEO %d\n", _modules.size());
19 while (_modules.size())
20 {
21 Module* m = _modules.back();
22 _modules.pop_back();
23 m->ForgetParent();
24 // at this time player using this codec has to be stopped
25 // currently this is partial responsibility of user task
26 // FIXME !!! - destroy codec thread if necessary
27 }
28 }
29
30 Module* VideoCodecControl::Create(const CodecInfo& info)
31 {
32 //printf("VideoCodecControl::Create %s\n", info.dll.c_str());
33 for (unsigned i = 0; i < _modules.size(); i++)
34 {
35 if (info.dll == _modules[i]->GetName())
36 {
37 //printf("VideoCodecControl::Create cache %d\n", i);
38 return _modules[i];
39 }
40 }
41 Module* module = new Module(info.dll.c_str(), *this);
42 if (module && module->init() < 0)
43 {
44 delete module;
45 return 0;
46 }
47
48 _modules.push_back(module);
49 return module;
50 }
51
52 void VideoCodecControl::Erase(Module* mod)
53 {
54 //printf("VideoCodecControl::Erase module %p\n", mod);
55 for (unsigned i = 0; i < _modules.size(); i++)
56 if (_modules[i] == mod)
57 {
58 // remove this member (swap with last one)
59 Module* m = _modules.back();
60 _modules.pop_back();
61 if (_modules.size() > i)
62 _modules[i] = m;
63
64 //printf("VideoCodecControl::Erase success - new _modules size %d %p\n", _modules.size(), m);
65 //if (_modules.size() > 0) printf("VideoCodecControl::Erase last %s\n", _modules[_modules.size() - 1]->GetName());
66 break;
67 }
68 }
69
70 Module::Module(const char* name, VideoCodecControl& parent)
71 :_parent(parent), m_pName(name), forgotten(0), m_iRefCount(0)
72 {
73 }
74
75 int Module::init()
76 {
77 m_pHandle = LoadLibraryA(m_pName.c_str());
78 if (!m_pHandle)
79 {
80 AVM_WRITE("Win32 plugin", "Could not load Win32 dll library: %s\n", m_pName.c_str());
81 return -1;
82 }
83
84 CodecAlloc();
85
86 driver.uDriverSignature = 0;
87 driver.hDriverModule = m_pHandle;
88 driver.dwDriverID = 0;
89 driver.DriverProc = (DRIVERPROC) GetProcAddress(m_pHandle, "DriverProc");
90
91 if (!driver.DriverProc)
92 {
93 AVM_WRITE("Win32 plugin", "Not a valid Win32 dll library: %s\n", m_pName.c_str());
94 return -1;
95 }
96
97 ICSendMessage((int)&driver, DRV_LOAD, 0, 0);
98 ICSendMessage((int)&driver, DRV_ENABLE, 0, 0);
99
100 AVM_WRITE("Win32 plugin", "Using Win32 dll library: %s\n", m_pName.c_str());
101 return 0;
102 }
103
104 Module::~Module()
105 {
106 if (m_pHandle)
107 {
108 if (driver.DriverProc)
109 ICSendMessage((int)&driver, DRV_FREE, 0, 0); // 0 drivreid
110 FreeLibrary(m_pHandle);
111 CodecRelease();
112 }
113
114 if (!forgotten)
115 _parent.Erase(this);
116 }
117
118 HIC Module::CreateHandle(unsigned int compressor, Mode mode)
119 {
120 ICOPEN icopen;
121 icopen.fccType = mmioFOURCC('v', 'i', 'd', 'c');
122 icopen.fccHandler = compressor;
123 icopen.dwSize = sizeof(ICOPEN);
124 icopen.dwFlags = (mode == Compress) ? ICMODE_COMPRESS : ICMODE_DECOMPRESS;
125
126 driver.dwDriverID = m_iRefCount;
127 DRVR* hDriver = new DRVR(driver);
128 if (!hDriver)
129 return 0;
130
131 //printf("Creating new handle for m_iRefCount %d, driver %d\n", m_iRefCount,(int)hDriver);
132 //printf("fcc: 0x%lx, handler: 0x%lx (%.4s)\n", icopen.fccType, icopen.fccHandler, (char*) &icopen.fccHandler);
133
134 hDriver->dwDriverID = ICSendMessage((int)hDriver, DRV_OPEN, 0, (int) &icopen);
135 if (!hDriver->dwDriverID)
136 {
137 AVM_WRITE("Win32 plugin", "WARNING DRV_OPEN failed (0x%lx/%d)\n", icopen.fccHandler, m_iRefCount);
138 return 0;
139 }
140 //else printf("New ID: %ld\n", hDriver->dwDriverID);
141 m_iRefCount++;
142 //printf("CREATE HANDLE %p %d\n", hDriver, m_iRefCount);
143 return (HIC) hDriver;
144 }
145
146 int Module::CloseHandle(HIC handle)
147 {
148 DRVR* hDriver = (DRVR*) handle;
149 //printf("DELETE HANDLE %p %d\n", hDriver, m_iRefCount);
150
151 if (hDriver)
152 ICSendMessage((int)hDriver, DRV_CLOSE, 0, 0);
153
154 if (--m_iRefCount < 1)
155 delete this;
156 delete hDriver;
157
158 return 0;
159 }
160
161 AVM_END_NAMESPACE;