Articles Archive
Articles Search
Director Wiki
 

Using Webcams in Director

March 12, 2002
by Will Turnage

Dear Handyman,

Would you please tell me how to use a webcam to show a real life image in Director 8?

Thanks & regards,

YK

 

Dear YK,

Well, there are many different kinds of webcams, and each webcam delivers its images in different formats. For this article, you'll learn how to use Director with the most basic kind of webcam. Ths kind of webcam just constantly refreshes a single image on a webserver. For instance, your webcam might always capture an image and store it in a file named webcam.jpg. That way, you could always view the most recent image on the web by visiting http://www.myserver.com/images/webcam.jpg.

To use a webcam like that in Director, all you have to do is just take this URL and set it to the filename of the cast member that you want to use to display the webcam image. For example:

member ("webcam image").filename = "http://www.myserver.com/images/webcam.jpg"

If you just want to show a single image of the webcam, then that's all you have to do and you're finished! However, if you want to have this image constantly update while your Director movie is running, then you've got to write a behavior to do that work for you.

The reason you have to write a behavior is to get around Director's internal caching. In order to increase performance when using web-based assets, Director will download your image initially and store it in its own internal cache. It will never go out on the web again to find the image unless the URL of your image changes. So what your behavior will do is trick Director into thinking that the URL of the image is constantly changing, when in fact, it really isn't. But enough explanation, lets see some code.

First of all you need two pieces of information: the URL of your webcam image, and the frequency which you want your pictures to be updated:

property pURL
property pFrequency
property pMemRef
property pNetID

on beginSprite me

  pNetID = 0
  pMemRef = sprite (me.spriteNum).member
  pURL = "http://nyctmc.org/images/cams/govt_property/cctv44.jpg"
  pFrequency = 5
  timeout ("webcam refresh").new (pFrequency * 1000, #preloadNewImage, me)
  me.preloadNewImage ()

end

On the beginning of this behavior, you start by initializing a few variables. The first variable is pNetID, which will be used to track each of your web downloads. Next, you initialize pMemRef, which will contain a reference to the bitmap that this behavior is attached to. Next, you store the URL of your webcam image into the pURL property. After that, you set the pFrequency variable equal to 5, meaning that you will try and download a new image once every five seconds. Next, you create a timeout object that will call the preloadNewImage handler in your behavior once every five seconds. Finally, since the timeout object won't call the preloadNewImage handler for another five seconds, you go ahead and call it yourself manually to get things started.

The next step in your behavior is to create the preloadNewImage handler that will be called every five seconds.

property pRandomNum

on preloadNewImage me
    if pNetID <> 0 then
      netAbort (pNetID)
    end if
    clearCache ()
    pRandomNum = random (the maxInteger)
    pNetID = preloadNetThing (pURL & "?id=" & pRandomNum)
    timeout ("check webcam preload").new (100, #checkPreload, me)
end

This handler starts by checking to see if a net operation is currently in progress. If it is, then it calls the netAbort function which stops the process. This is used to allow for server timeouts and other net traffic which might slow the update of your webcam image. Next, you call the clearCache function in Director. This function is designed to clear out Director's internal web cache. It's not always 100% reliable, but calling it never hurts and it helps keep some of your memory freed up. The next step is to pick a random number between 1 and 2,147,483,647. Once you have this random number, you add it to the end of your URL and begin preloading the image using Director's preloadNetThing command. By appending this random number to the end of your URL, you are essentially tricking Director into thinking that it's downloading a new image. However, since you're downloading a JPEG file, the web server will pretty much ignore the random number and just return the latest JPEG image from the webcam. The last step is to create another timeout object that will execute 10 times a second. This timeout object will call the checkPreload handler which will check on the progress of your preloading image.

on checkPreload me
  if netDone (pNetID) then
    pMemRef.filename = pURL & "?id=" & pRandomNum
    timeout ("check webcam preload").forget ()
    pNetID = 0
  end if
end

In this checkPreload handler, you start by checking to see if the your image is finished preloading. If it is done, then you set the filename of your member equal to the full URL you originally created. By specifying the full URL, you ensure that Director displays the correct image from its internal cache and immediately display it on screen. Next, you forget the timeout object that will constantly check on the status of the preload operation. Finally, you set pNetID equal to zero so that the next time you start to preload an image, you won't abort the net operation.

Now, all you have to do is attach this behavior to the bitmap in your movie that you want to display the webcam image, and the end result might look something like this.



[Editor's note: If no image is visible, the webcam may be
down for servicing, check back again later]

A sample Director 8 movie is available for download in ZIP or SIT archive format.

Of course, if you need to show a streaming video from a web cam, you can use a Quicktime or RealMedia (in Director 8.5) digital video cast member. If you need to show video from a camera attached to the local computer, you might want to look at the VideoSprite Xtra and others from Penworks (Windows only), the TrackThemColors Xtra from Smoothware (Mac only).

All colorized Lingo code samples have been processed by Dave Mennenoh's brilliant HTMLingo Xtra, available from his site at http://www.crackconspiracy.com/~davem/

Will Turnage is a multimedia programmer based in New York City. In addition to his weekly role as Director Online's Multimedia Handyman, he is also the Technology Director for Sony Music's Client Side Technologies Group. You can read more about his work at http://will.turnage.com/.

Copyright 1997-2017, Director Online. Article content copyright by respective authors.