Wednesday, April 10, 2013

C++: DirectX and 3-D editing in Visual Studio



Weird.  This has been a hidden story since VS 2012 came out.  Not sure why.
Did you know that you can do 3-D editing in Visual Studio?  Check it out, and it isn’t documented very well on the MSDN sites, sure there are a few videos and so forth, but when I show this to C++ TEs many are surprised at the simplicity of working with 3D in Visual Studio.  Ok, doesn’t make up for XNA, but it certainly is a reason to check out C++ and Direct3 (I tricked you with DirectX, sorry).  This tool gives you a chance to do some 3D editing.

Is it blender?  No.  Is it whatever Maya is being called these days? No.
Do I like it? Yep.
Here is what it looks like, I am using Ultimate, and it is only available in Pro and Ultimate.

Cool, 3D editing in Visual Studio

But how?
image
How do I use it?
Easy.

Open a New Project C++, Windows Store, Blank App

image

From the solution explorer at the project level add a 3-D Graphic, the process is shown below:

image

Once the 3-D Scene opens you will see the following, you can also use this to touch up an existing .FBX:

image

Now what about lighting?

That get’s more interesting and I will cover the way that you edit lighting, which in any tool has it’s complexity. 

Conclusion:

Wow, why isn’t this discussed in more depth on the MSDN and Channel 9 site?  I have no idea, but now that I know about, I am going to use the heck out of it and I hope you do too.
Go forth and build apps!

Tuesday, March 19, 2013

New tool for converting test lists to Visual Studio 2012 Playlists


One of the requests we have heard upon the introduction of test Playlists in Visual Studio Update 2 was a tool for converting Visual Studio 2010 test lists to the new Playlists format…Well it turns a developer, Rolf Huisman, at one of our “go to” ALM Partners InfoSupport wrote and and released one Codeplex! 

To use it:
1. Install the VSIX from http://testlistmigration.codeplex.com/
2. Load one of your projects and right click on the VSMDI with test lists and select Migrate
image
3. Save the Playlists to your hard drive
image 
4. Load the newly created Playlists and run them!
image
image

Monday, January 7, 2013

Asynchronously streaming video with ASP.NET Web API


A lot of people think that ASP.NET Web API is basically a fancy framework for building APIs – which couldn’t be further from the truth. Web API is mainly about the new .NET HTTP programming modelit brings to the table – embracing HTTP to perform a whole magnitude of web related tasks; and APIs are just a small portion of that. Hopefully if you follow this blog, you have seen examples of that already, as we often wander in the unkown areas of ASP.NET Web API, beyond building “traditional” APIs.

Today, let’s go back to the HTTP programming model and use Web API to asynchronously stream videos.
More after the jump.

The concept of asynchronous streaming

To perform the async streaming task, we will revert to the class we already previously used on this blog – and that is PushStreamContent. It allows the developer to progressively push packets of data down to the receiving client. Previously, we used PushStreamContent and JavaScript’s Server Sent Events to create an HTML5 chat.
In our new scenario, we will read the video stream from the file on the server’s hard drive, and flush it down to the client (using PushStreamContent) in the packets of 65 536 bytes. The streaming video playback could then start immediately (client doesn’t have to wait for the entire video to be flushed down), without causing unnecessary load on our server, especially as the process of writing to the client happens asynchronously. Once the client disconnects, the writing stops.

Implementation

PushStreamContent takes an Action in its constructor:
C#
1
public PushStreamContent(Action<Stream, HttpContent, TransportContext> onStreamAvailable);
This Action is called as soon as the output stream (HTTP content to be flushed to the client) becomes available.
Therefore, we could create a small helper class, which would read the from the disk, and expose this activity for PushStreamContent to call repeatedly.
C#
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
public class VideoStream
{
private readonly string _filename;
public VideoStream(string filename, string ext)
{
_filename = @"C:\Users\Filip\Downloads\" + filename + "."+ext;
}
public async void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
{
try
{
var buffer = new byte[65536];
using (var video = File.Open(_filename, FileMode.Open, FileAccess.Read))
{
var length = (int)video.Length;
var bytesRead = 1;
while (length > 0 && bytesRead > 0)
{
bytesRead = video.Read(buffer, 0, Math.Min(length, buffer.Length));
await outputStream.WriteAsync(buffer, 0, bytesRead);
length -= bytesRead;
}
}
}
catch (HttpException ex)
{
return;
}
finally
{
outputStream.Close();
}
}
}
This class is obviously little simplified, for the sake of demo purposes, but hopefully you get the idea. We allow the consumer of the class to give as information about the file (for which we arbitrarily look in a specific location, the Downloads folder in this case). In the WriteToStream method, we proceed to read the file progressively and flush these bits to the output stream.
Notice that the signature of this method matches the Action expected by PushStreamContent and as a result can be used by it.
Now let’s move to the controller action, which, by now, will be very simple (all the heavy lifting happens in the above VideoStream class).
C#
1
2
3
4
5
6
7
8
9
10
11
12
public class VideosController : ApiController
{
public HttpResponseMessage Get(string filename, string ext)
{
var video = new VideoStream(filename, ext);
var response = Request.CreateResponse();
response.Content = new PushStreamContent(video.WriteToStream, new MediaTypeHeaderValue("video/"+ext));
return response;
}
}
We allow the client to pass video info (name, extension), construct the instance of VideoStream and then create and instance of PushStreamContent which gets returned to the client.
All we need now is just a route registration:
C#
1
2
3
4
config.Routes.MapHttpRoute(
name: "DefaultVideo",
routeTemplate: "api/{controller}/{ext}/{filename}"
);

Consuming the asynchronous video stream

We can now make an HTML5 web page, with the video tag.
XHTML
1
2
3
4
5
6
7
<html>
<body>
<video width="480" height="320" controls="controls" autoplay="autoplay">
<source src="/api/videos/webm/CkY96QuiteBitterBeings" type="video/webm">
</video>
</body>
</html>
In this case, I will be requesting this video:C:\Users\Filip\Downloads\CkY96QuiteBitterBeings.webm.
If we run this in the browser that supports WebM, we could see that the video (especially if you open the network inspection console) is streamed rather than loaded at once. To better illustrate this, I have recorded a short video which shows the application in action. I’ve put a breakpoint insideWriteToStream method, to show that subsequent packets of video get sent down *after* the playback has already started; the client can commence the playback already after the first 64kB packet. Also, as soon as the breakpoint hits, nothing more gets sent to the client, yet the browser still continues the playback of what it has already received.
Video below (choose 480p when playing for better quality):


Summary

While there are arguably many better solution for video streaming (media protocols, media servers and so on), in my opinion, this type of functionality is a pretty nifty example of how flexible Web API can be in terms of working with HTTP programming – and how many different things it can do.
On a side, PushStreamContent itself is a very interesting class, and if used correctly, can be a very powerful weapon in the arsenal of Web API developer. A very interesting article about a similar topic (async with PushStreamContent) can be found here, by AndrĂ©s Vettor. Really worth a read!