Chapter 16

About the Sample Code


CONTENTS


Introduction

This book and its CD-ROM contain many examples of working Navigator plug-ins. These plug-in samples can serve as a basis for your plug-in development because each one demonstrates a different technique.

All of the samples were written for Windows 95/NT using Microsoft Visual C++ 4.0. If you are using compilers from Borland or Watcom, you can still use the samples, but you should refer to the chapters covering those compilers in this book to properly port your code.

The plug-in samples demonstrate two basic design types. As you probably remember from earlier chapters, these are streaming plug-ins and file-based plug-ins. The streaming plug-in example in Chapter 17, "A Streaming Audio Sample," plays audio in real-time over the Internet (depending on the speed of your connection). This plug-in uses a buffer class that is examined in Chapter 18, "The Buffer Classes." The next few plug-in samples are file-based. They are a server CPU monitor, an MCIWnd sample, a MIDI sample, and a LiveConnect sample from Netscape using Java and JavaScript. Chapter 24 documents how to build your own Socket Spy for debugging.

Each sample contains the source file locations on the CD-ROM, plug-in design discussion with a block diagram, and documentation for each code section. There are many notes about the caveats of Windows plug-in development that can save you time in your development efforts.

Audio Streamer

If you are new to Windows 95/NT multithreaded programming, Chapter 17, "A Streaming Audio Sample," is for you. This plug-in creates a separate worker thread that feeds the audio driver buffers. The thread is fully synchronized using semaphores and critical sections. Discussion covers potential deadlock situations and how to avoid them.

The Audio Streamer is a good basis for any plug-in that streams data from Navigator to a device. For example, you can use the same model for streaming real-time video data. Additionally, you might want to add audio decompression using Window's Audio Compression Manager (ACM). After adding ACM, your audio plug-in should come close to commercial performance. Or, perhaps you want to add real-time audio/video capability to your corporate Intranet without paying for a commercially available plug-in.

Buffer Classes

Chapter 18, "The Buffer Classes," explains two different buffer classes for use with a real-time streaming plug-in. These classes allow your plug-in to easily preread a configurable number and size of data buffers from Netscape's Navigator. The audio streaming sample can use either of the classes, depending on your preference. The buffer classes demonstrate circular and FIFO buffering schemes.

Server CPU Monitor

Chapter 19, "The CPU Monitor Plug-In," demonstrates client/server communications with a plug-in using a Common Gateway Interface (CGI) program on the server. This plug-in uses the NPN_GetURL API to request server load status. The CGI program, written in C, calls out to the UNIX command vmstat for system statistics. These statistics are sent back to the plug-in for graphical display.

It would be cool to extend this idea with a better user interface, using more of the available vmstat data. Although the sample uses only CPU percentages, your plug-in can display information about kernel threads, virtual memory, interrupts, system calls, and context switches. Don't limit yourself to vmstat. The results of any command line server command can be displayed with a Navigator plug-in.

Using MCIWnd in Windows

MCIWnd is a Windows class for Microsoft Windows that makes it very easy to code multimedia controller plug-ins. The example in Chapter 20, "Writing a Plug-In with MCIWnd," shows how to make a plug-in that can handle AVI video, WAV audio, and MIDI playback with very little code. The sample is also a good example of a plug-in that handles multiple MIME types.

Using MCIWnd for a plug-in might be more suitable to an Intranet with relatively high bandwidth. For example, you can make a really great video plug-in for your company that plays AVI videos over the LAN. With MCIWnd, you can modify video controls easily, using the provided messages, macros, and functions. Impress your boss!

MIDI Background Music

Using the Musical Instrument Device Interface (MIDI) can add real spice to your Web page with extremely low bandwidth requirements. Many of today's hottest computer games use a combination of MIDI and PCM audio to add cool effects. Most audio cards such as Sound Blaster support real-time MIDI and PCM audio mixing, which means that you can play MIDI background music with bursts of PCM audio intermixed.

The MIDI background sample in Chapter 21, "Using MIDI with a Background Plug-In," is a good example of using the HIDDEN attribute of the EMBED tag.

Window Subclassing

Chapter 22, "Subclassing without Microsoft's Foundation Class Library," gives an example of subclassing your plug-in's window using standard Windows APIs. This technique avoids using a class library for window subclassing and takes advantage of Windows APIs such as SetWindowLong, SetProp, GetProp, CallWindowProc, and RemoveProp. Use this example for the basis of creating a very small plug-in of less than 25KB in size.

Using LiveConnect

Netscape's 3.0 Plug-in SDK includes support for LiveConnect. LiveConnect facilitates the communication between Java, JavaScript, and plug-ins. Using Netscape's javah tool along with a Java compiler and C or C++ compilers allows Java to call plug-in implemented classes and allows plug-ins to call Java implemented classes.

Chapter 23, "Netscape's LiveConnect Sample," examines Netscape's Windows AVI video player that is included with the 3.0 plug-in SDK. Because LiveConnect is still in beta at the time of this writing, you should supplement any information on LiveConnect found in this book with current documentation from Netscape's Web site.

Socket Spy

Debugging a plug-in can be a very frustrating experience. When you request an action from the Navigator (for example, NPN_PostURL), Navigator translates your request to the proper HTTP format and sends it via sockets and TCP/IP to the Web server. In return, data is sent back to Navigator and, if your lucky, to your plug-in.

The problem with this multilayered approach is that sometimes you don't know what is actually sent to the Web server (or any IP address). You can use expensive hardware, such as a protocol analyzer, to overcome this problem or do it in software with the Socket Spy (as shown in Chapter 24, "Debugging with the Socket Spy").

The Socket Spy is truly a software hack that will horrify many traditionalists, but it works well. Socket Spy is just a fancy name for a DLL that is inserted between a Windows socket application and WinSock. The ugliness or beauty of it is that you must use a binary editor to make your WinSock application load the Socket Spy DLL instead of WinSock (wsock32.dll for 32-bit WinSock). This enables you to source-level debug any socket calls made from a WinSock application. With Navigator and your plug-in, this technique allows you to "spy" on the Navigator to WinSock data while you are debugging your plug-in.

Conclusion

That was an overview of the sample plug-in chapters. Feel free to use any code or ideas presented in this section for your plug-in development. You certainly should consider running Navigator with Socket Spy to aid in debugging.

What's Next?

The next chapters in this section give detailed examples of the topics mentioned here. All source code for the examples is included on the CD-ROM.