Monday, July 03, 2006

Requested: Video texture in MDX

I got a request for some code using the TextureReadyToRender with video surfaces in MDX. The following works, but there's a problem with disposing the video texture after the vido ends. I've seen a solution or two, but since the approach with video surfaces won't work for my project, I decided to not implement that part for now. Maybe I'll do it later, just to satisfy my curiosity :)


#region Groundplane with video texture
protected VertexBuffer CreateVertexBuffer(Device dev)
{
try
{
video = Video.FromFile("test.avi");//videoTexPath
//video.Ending += new EventHandler(MovieOver); // TODO: DISPOSING DOESN'T WORK!!
video.TextureReadyToRender += new TextureRenderEventHandler(onTextureReadyToRender);
video.RenderToTexture(dev);
video.Play();

// vidFrame.Text = Convert.ToString(video.CurrentPosition);
vidLength.Text = Convert.ToString(video.Duration);
//rotX.Text = Convert.ToString(video.CurrentPosition);
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}

CustomVertex.PositionTextured[] quad = new CustomVertex.PositionTextured[4];
quad[0] = new CustomVertex.PositionTextured(-300.0f, -300.0f, 0.0f, 0.0f, 0.0f);
quad[1] = new CustomVertex.PositionTextured(-300.0f, 300.0f, 0.0f, 0.0f, 1.0f);
quad[2] = new CustomVertex.PositionTextured(300.0f, -300.0f, 0.0f, 1.0f, 0.0f);
quad[3] = new CustomVertex.PositionTextured(300.0f, 300.0f, 0.0f, 1.0f, 1.0f);

VertexBuffer buf = new VertexBuffer(
typeof(CustomVertex.PositionTextured), // What type of vertices
4, // How many
dev, // The device
0, // Default usage
CustomVertex.PositionTextured.Format, // Vertex format
Pool.Default); // Default pooling

GraphicsStream stm = buf.Lock(0, 0, 0);
stm.Write(quad);

buf.Unlock();
return buf;
}

///
/// The onTextureReadyToRender method (called from TextureRenderEventHandler to handle videotexture)
///

protected void onTextureReadyToRender(object sender, TextureRenderEventArgs e)
{
if (e.Texture == null)
return;

SurfaceDescription ds = e.Texture.GetLevelDescription(0);

if (ds.Pool == Pool.Default)
{
sysSurf = _device.CreateOffscreenPlainSurface(ds.Width, ds.Height,
ds.Format, Pool.SystemMemory);
}

using (Surface vidSurf = e.Texture.GetSurfaceLevel(0))
{
if (_tex == null)
{
_tex = new Texture(_device, ds.Width, ds.Height,
1, Usage.Dynamic, ds.Format, ds.Pool);
}
using (Surface texSurf = _tex.GetSurfaceLevel(0))
{
//_device.GetRenderTargetData(vidSurf, sysSurf);
//_device.UpdateSurface(sysSurf, texSurf);
SurfaceLoader.FromSurface(texSurf, vidSurf, Filter.Linear, unchecked((int)0xffffffff));
}
}
Invalidate();
}

///
/// Movie playback has ended
///

/*
void MovieOver(object sender, EventArgs e)
{
Dispose();
}
*/
#endregion


I've tried the following for disposing (cluttered with disposing some other textures):
protected void DisposeTextures()
{
if (_textures == null)
{
return;
}
foreach (Texture t in _textures)
{
if (t != null)
{
t.Dispose();
}
}

if (_tex == null)
{
return;
}
else
{
_tex.Dispose();
_tex = null;
}
if (video != null)
{

if (!video.Audio.Disposed)
{
video.Audio.Dispose();
}
if (!video.Disposed)
{
video.Stop();
//video.Dispose();
video = null;
}
}

}


I'll update here if I find a link where disposal of the video is explained, I should have one somewhere... From what I remember it doesn't work with only managed directx though, so you'll have to do some other tweak ;)

90 comments:

E. H. said...

There's some talk on disposing video at http://www.gamedev.net/community/forums/topic.asp?topic_id=386498&PageSize=25&WhichPage=2

If you figure the video.dispose() using the code I posted, please reply here with a solution. Thanks

OpenSebJ said...

Thanks!

The issue I was having was implementing this inside a render loop I already had prepared; basically I just needed to copy it to the render method for the vertex I already had prepared ;-)
I haven't got much further though, now I need to get it to stop ripping and tearing during playback.

Also any ideas on the video formats it supports?

Cheers,
Sebastian

E. H. said...

No, sorry - as I said, I quit the 'video in directx' trail a while back. By the way, I've heard that it's supposed to be fewer problems if you work with DirectShow than AudioVideoPlayback - even though DirectShow isn't officially supported in the latest SDK (Managed Code).

Currently, I'm working on a modeller, mainly to be a able to create buildings through a simple GUI. I just finished planning for it though, so it will probably be a couple of weeks before I have it working ;)

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

Great article! Thanks.

Anonymous said...

Thanks for interesting article.

Anonymous said...

Nice Blog!

Anonymous said...

Thank You! Very interesting article. Do you can write anything else about it?

Anonymous said...

Very interesting site. Blog is very good. I am happy that I think the same!

Anonymous said...

Nice! Nice site! Good resources here. I will bookmark!

Anonymous said...

Excellent website. Good work. Very useful. I will bookmark!

Anonymous said...

I see first time your site guys. I like you :)

Anonymous said...

djA0Dh You have a talant! Write more!

Anonymous said...

AMqCrQ Thanks to author.

Anonymous said...

rhUJFC The best blog you have!

Anonymous said...

TVwZSu Nice Article.

Anonymous said...

Please write anything else!

Anonymous said...

Good job!

Anonymous said...

Good job!

Anonymous said...

actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

Anonymous said...

Thanks to author.

Anonymous said...

actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

Anonymous said...

actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

Anonymous said...

qa5IUS write more, thanks.

Anonymous said...

Good job!

Anonymous said...

Hello all!

Anonymous said...

Thanks to author.

Anonymous said...

Wonderful blog.

Anonymous said...

Hello all!

Anonymous said...

Nice Article.

Anonymous said...

Nice Article.

Anonymous said...

Nice Article.

Anonymous said...

Good job!

Anonymous said...

actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

Anonymous said...

Good job!

Anonymous said...

Good job!

Anonymous said...

Thanks to author.

Anonymous said...

Thanks to author.

Anonymous said...

If ignorance is bliss, you must be orgasmic.

Anonymous said...

The gene pool could use a little chlorine.

Anonymous said...

Friends help you move. Real friends help you move bodies

Anonymous said...

Build a watch in 179 easy steps - by C. Forsberg.

Anonymous said...

Build a watch in 179 easy steps - by C. Forsberg.

Anonymous said...

What is a free gift ? Aren't all gifts free?

Anonymous said...

Oops. My brain just hit a bad sector.

Anonymous said...

actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

Anonymous said...

Please write anything else!

Anonymous said...

Friends help you move. Real friends help you move bodies

Anonymous said...

Friends help you move. Real friends help you move bodies

Anonymous said...

Oops. My brain just hit a bad sector.

Anonymous said...

Energizer Bunny Arrested! Charged with battery.

Anonymous said...

640K ought to be enough for anybody. - Bill Gates 81

Anonymous said...

The gene pool could use a little chlorine.

Anonymous said...

Beam me aboard, Scotty..... Sure. Will a 2x10 do?

Anonymous said...

When there's a will, I want to be in it.

Anonymous said...

Save the whales, collect the whole set

Anonymous said...

Give me ambiguity or give me something else.

Anonymous said...

Friends help you move. Real friends help you move bodies

Anonymous said...

640K ought to be enough for anybody. - Bill Gates 81

Anonymous said...

Give me ambiguity or give me something else.

Anonymous said...

640K ought to be enough for anybody. - Bill Gates 81

Anonymous said...

What is a free gift ? Aren't all gifts free?

Anonymous said...

The gene pool could use a little chlorine.

Anonymous said...

Please write anything else!

Anonymous said...

I'm not a complete idiot, some parts are missing!

Anonymous said...

When there's a will, I want to be in it.

Anonymous said...

Save the whales, collect the whole set

Anonymous said...

What is a free gift ? Aren't all gifts free?

Anonymous said...

Build a watch in 179 easy steps - by C. Forsberg.

Anonymous said...

I don't suffer from insanity. I enjoy every minute of it.

Anonymous said...

A flashlight is a case for holding dead batteries.

Anonymous said...

Calvin, we will not have an anatomically correct snowman!

Anonymous said...

Suicidal twin kills sister by mistake!

Anonymous said...

Clap on! , Clap off! clap@#&$NO CARRIER

Anonymous said...

Thanks to author.

Anonymous said...

The gene pool could use a little chlorine.

Anonymous said...

Please write anything else!

Anonymous said...

Friends help you move. Real friends help you move bodies.

Anonymous said...

640K ought to be enough for anybody. - Bill Gates 81

Anonymous said...

Clap on! , Clap off! clap@#&$NO CARRIER

Anonymous said...

actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

Anonymous said...

C++ should have been called B

Anonymous said...

What is a free gift ? Aren't all gifts free?

Anonymous said...

C++ should have been called B

Anonymous said...

Lottery: A tax on people who are bad at math.

Anonymous said...

Build a watch in 179 easy steps - by C. Forsberg.

Anonymous said...

Change is inevitable, except from a vending machine.

Anonymous said...

Energizer Bunny Arrested! Charged with battery.

Anonymous said...

Oops. My brain just hit a bad sector.

Anonymous said...

Hello E.H.

Do you have a complete example?

I integrate your code in my code, but the video is bucking. It is very slow and flickers.