1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
#ifndef LIVE_VIDEO_TEXTURE
#define LIVE_VIDEO_TEXTURE
#if ENABLE_WEBCAM
#include "BaseVideoTexture.h"
#include "Runtime/Graphics/Image.h"
#include "Runtime/Math/Color.h"
#include "Runtime/Scripting/ScriptingUtility.h"
#if UNITY_WINRT
#include <windows.foundation.h>
#endif
struct PlatformDependentWebCamTextureData;
enum WebCamFlags
{
kWebCamFrontFacing = 1,
};
struct MonoWebCamDevice
{
ScriptingStringPtr name;
int flags;
bool operator== (std::string const &other) const
{
std::string cppStr = scripting_cpp_string_for (name);
return cppStr == other;
}
};
typedef UNITY_VECTOR(kMemWebCam, MonoWebCamDevice) MonoWebCamDevices;
typedef MonoWebCamDevices::iterator MonoWebCamDevicesIter;
class WebCamTexture: public BaseVideoTexture
{
public:
REGISTER_DERIVED_CLASS (WebCamTexture, Texture)
WebCamTexture (MemLabelId label, ObjectCreationMode mode = kCreateObjectDefault)
: BaseVideoTexture(label, mode)
{
m_RequestedFPS = 0.0f;
m_RequestedWidth = 0;
m_RequestedHeight = 0;
m_IsCreated = false;
m_VT = NULL;
#if UNITY_WINRT
RunExactlyOnce();
#endif
}
static void InitializeClass ();
static void CleanupClass ();
virtual void Play();
virtual void Pause();
virtual void Stop ();
virtual void Update ();
#if UNITY_WP8
virtual void Suspend();
virtual void Resume();
#endif
void SetRequestedWidth (int width) { m_RequestedWidth = width; SetDirty(); }
int GetRequestedWidth () const { return m_RequestedWidth; }
void SetRequestedHeight (int width) { m_RequestedHeight = width; SetDirty(); }
int GetRequestedHeight () const { return m_RequestedHeight; }
void SetRequestedFPS (float fps) { m_RequestedFPS = fps; SetDirty(); }
float GetRequestedFPS () const { return m_RequestedFPS; }
void SetDevice (const std::string &name) { m_DeviceName = name; SetDirty(); }
std::string GetDevice() const;
static void GetDeviceNames (MonoWebCamDevices &devices, bool forceUpdate);
#if ENABLE_PROFILER || UNITY_EDITOR
virtual int GetStorageMemorySize() const { return 0; }
#endif
ColorRGBAf GetPixel (int x, int y) const;
bool GetPixels (int x, int y, int width, int height, ColorRGBAf* data) const;
bool GetPixels (int dstFormat, void *dstData, size_t dstSize) const;
#if UNITY_IPHONE || UNITY_ANDROID || UNITY_BLACKBERRY || UNITY_TIZEN
virtual int GetVideoRotationAngle() const;
#endif
#if UNITY_IPHONE || UNITY_BLACKBERRY
virtual bool IsVideoVerticallyMirrored() const;
#endif
private:
#if UNITY_WINRT
// C-tor helper. It is here to ensure that some invariants are satisfied from the moment
// the object is created, so we don't have to check and recheck the state all the time
void RunExactlyOnce();
#endif
void Create();
void Cleanup ();
public:
static void EnsureUniqueName (MonoWebCamDevice &device,
const MonoWebCamDevices &devs);
private:
int m_RequestedWidth;
int m_RequestedHeight;
float m_RequestedFPS;
std::string m_DeviceName;
bool m_IsCreated;
static void InitDeviceList();
int GetDeviceIdFromDeviceList(const std::string& name) const;
PlatformDependentWebCamTextureData *m_VT;
protected:
#if UNITY_IPHONE
virtual TextureFormat GetBufferTextureFormat() const { return kTexFormatBGRA32; }
virtual bool CanSetReadable(bool readable) const;
#endif
#if UNITY_BLACKBERRY || UNITY_TIZEN || UNITY_WP8
virtual TextureFormat GetBufferTextureFormat() const { return kTexFormatBGRA32; }
#endif
};
inline ColorRGBAf WebCamTexture::GetPixel (int x, int y) const
{
if (!m_IsCreated)
{
ErrorString ("Cannot get pixels when webcam is not running");
return ColorRGBAf(0,0,0,0);
}
if (!IsReadable())
{
ErrorString ("Cannot get pixels when webcam is non-readable");
return ColorRGBAf(0,0,0,0);
}
return GetImagePixel ((UInt8*)GetImageBuffer(), GetPaddedWidth(), GetPaddedHeight(), GetBufferTextureFormat(), static_cast<TextureWrapMode>(GetSettings().m_WrapMode), x, y);
}
inline bool WebCamTexture::GetPixels( int x, int y, int width, int height, ColorRGBAf* colors ) const
{
if (width == 0 || height == 0)
return true; // nothing to do
if (!m_IsCreated)
{
ErrorString ("Cannot get pixels when webcam is not running");
return false;
}
if (!IsReadable())
{
ErrorString ("Cannot get pixels when webcam is non-readable");
return false;
}
return GetImagePixelBlock ((UInt8*)GetImageBuffer(), GetPaddedWidth(), GetPaddedHeight(), GetBufferTextureFormat(), x, y, width, height, colors);
}
inline bool WebCamTexture::GetPixels (int dstFormat, void *dstData, size_t dstSize) const
{
size_t srcRowBytes = GetRowBytesFromWidthAndFormat(GetPaddedWidth(), GetBufferTextureFormat());
size_t dstRowBytes = GetRowBytesFromWidthAndFormat(GetDataWidth(), dstFormat);
if (dstSize < dstRowBytes * GetDataHeight())
{
ErrorString ("Buffer is too small to get image data");
return false;
}
ImageReference src (GetDataWidth(), GetDataHeight(), srcRowBytes, GetBufferTextureFormat(), (UInt8*)GetImageBuffer());
ImageReference dst (GetDataWidth(), GetDataHeight(), dstRowBytes, dstFormat, dstData);
dst.BlitImage( src );
return true;
}
inline int WebCamTexture::GetDeviceIdFromDeviceList(const std::string& name) const
{
MonoWebCamDevices names;
GetDeviceNames(names, false);
if(!name.empty())
{
for(int i = 0 ; i < names.size() ; i++)
{
if(names[i] == name)
return i;
}
ErrorString ("Cannot find webcam device "+name+".");
return -1;
}
else
{
// Return camera 0 as default
if(!names.empty())
return 0;
else
{
ErrorString ("No available webcams are found. Either there is no webcam connected, or they are all in use by other applications (like Skype).");
return -1;
}
}
}
inline std::string WebCamTexture::GetDevice() const
{
if(m_DeviceName.size() > 0)
{
return m_DeviceName;
}
else
{
MonoWebCamDevices names;
GetDeviceNames(names, false);
if(names.size() > 0)
return scripting_cpp_string_for(names[0].name);
else
return "no camera available.";
}
}
inline void WebCamTexture::EnsureUniqueName (MonoWebCamDevice &device,
MonoWebCamDevices const &devs)
{
int num = 0;
std::string testname = scripting_cpp_string_for (device.name);
while (true)
{
if (num > 0)
testname += Format (" %d", num);
if (std::find (devs.begin (), devs.end (), testname) == devs.end ())
{
device.name = scripting_string_new(testname.c_str ());
break;
}
num++;
}
}
#endif
#endif
|