Webcam Texture plugin

I am building a plugin which supports all webcams (unlike the default Unity WebcamTexture) and also enables fast CV processing of the video frame. Unfortunately I can’t get the webcam image from the C++ DLL to Unity. I am using this example project as a base:
http://docs.unity3d.com/Documentation/Images/manual/RenderingPluginExample42.zip

These are the code snippets I have tried. All of them cause the Editor to crash upon run.
Note that pixelbuffer is a BYTE* and it holds the current webcam video frame.

IDirect3DTexture9* d3dtex = (IDirect3DTexture9*)g_TexturePointer;
D3DSURFACE_DESC desc;
d3dtex->GetLevelDesc (0, &desc);
D3DLOCKED_RECT lr;
d3dtex->LockRect (0, &lr, NULL, 0);

for (int y = 0; y < desc.Height; ++y)
{
	BYTE* ptrdst = (BYTE*)lr.pBits;
	BYTE* ptrsrc = pixelBuffer;

	for (int x = 0; x < desc.Width; ++x)
	{
		// Write the texture pixel
		ptrdst[0] = ptrsrc[0];
		ptrdst[1] = ptrsrc[1];
		ptrdst[2] = ptrsrc[2];
		ptrdst[3] = ptrsrc[3];

		//pixels are 4 bpp
		ptrdst += 4;
		ptrsrc += 4;
	}

	// To next image row
	ptrdst += lr.Pitch;
	ptrsrc += lr.Pitch;
}

d3dtex->UnlockRect (0);
IDirect3DTexture9* d3dtex = (IDirect3DTexture9*)g_TexturePointer;
D3DSURFACE_DESC desc;
d3dtex->GetLevelDesc (0, &desc);
D3DLOCKED_RECT lr;
d3dtex->LockRect (0, &lr, NULL, 0);

BYTE* ptrdst = (BYTE*)lr.pBits;
BYTE* ptrsrc = pixelBuffer;

for(int y=0; y < desc.Height; ++y)
{
	memcpy(ptrdst, ptrsrc, desc.Width * 4);
	ptrdst += lr.Pitch;
	ptrsrc += lr.Pitch;
}

d3dtex->UnlockRect (0);
IDirect3DTexture9* d3dtex = (IDirect3DTexture9*)g_TexturePointer;
unsigned int m_nData[16*16]; //ARGB, 8 bits per element
D3DLOCKED_RECT lr;
d3dtex->LockRect(0, &lr, NULL, D3DLOCK_DISCARD);

BYTE* ptrdst = (BYTE*)lr.pBits;

for(int y=0; y<16; ++y){

	memcpy(ptrdst + y*lr.Pitch, pixelBuffer + y*16, 16*4);
}

d3dtex->UnlockRect(0);

Any ideas how to get this to work?

Ok, I fixed it. It was the second one. My texture in unity was not the same size as in C++ so that caused a crash.

Here is the full code:

#if SUPPORT_D3D9
// D3D9 case
if((g_DeviceType == kGfxRendererD3D9)  (pixelBuffer))
{
	// Update native texture from code
	if (g_TexturePointer)
	{
		IDirect3DTexture9* d3dtex = (IDirect3DTexture9*)g_TexturePointer;
		D3DSURFACE_DESC desc;
		d3dtex->GetLevelDesc (0, &desc);
		D3DLOCKED_RECT lr;
		d3dtex->LockRect (0, &lr, NULL, 0);

		BYTE* ptrdst = (BYTE*)lr.pBits;
		BYTE* ptrsrc = pixelBuffer;

		for(int y=0; y < desc.Height; ++y)
		{
			memcpy(ptrdst, ptrsrc, desc.Width * 4);
			ptrdst += lr.Pitch;
			ptrsrc += lr.Pitch;
		}

		d3dtex->UnlockRect (0);
	}
}
#endif

It is pretty fast but there is a slight amount of latency though. I wonder if there is a faster way to set the video texture rather then to copy the pixels line by line using memcpy…

By the way, I am using DVSL to get the camera image: