Implementing a Smart Online Video Application
by Robin Rowe


Listing One
 // test_mpeg.cpp: MPEGator test program
 #include "windows.h"
 #include "../mpegator/mpeg.h"
 #include "test_mpeg.h"
 int PASCAL WinMain(HINSTANCE iCur, HINSTANCE iPrev, LPSTR lpCmdLine, int
 nCmdShow )
 {       const char* filename="test.mpg";
         if(!MsgBox("Start Capture",filename,MB_OKCANCEL))
         {       return 0;
         }
         MPEG mpeg;
         if(!mpeg)
         {       MsgErrorBox(mpeg.ErrorMsg());
                 return 1;
         }
         if(!mpeg.Open(filename))
         {       MsgErrorBox(mpeg.ErrorMsg());
                 return 1;
         }
         MsgBox("Recording",
                 "Press button to stop",
                 MB_OK|MB_ICONEXCLAMATION);
         mpeg.Stop();
         MsgBox("Finished","Done",MB_OK);
         return 0;
 }

Listing Two
 // test_mpeg.h: message boxes

 #ifndef TEST_MPEG_H
 #define TEST_MPEG_H

 inline
 int MsgBox(const char* title,const char* string,UINT
 style=MB_OK|MB_ICONQUESTION)
 {       return MessageBox(NULL,string,title,style)!=IDCANCEL;
 }
 inline
 int MsgErrorBox(const char* string)
 {       return MessageBox(NULL,
         string,
         "Error",
         MB_OK|MB_ICONERROR)!=IDCANCEL;
 }
 #endif


Listing Three
 // mpeg.h: encapsulated MPEGator control

 #ifndef MPEG_H
 #define MPEG_H
 #include <objbase.h>
 #include <initguid.h>
 #include "inc/mtrif.h"
 #include "inc/mtruid.h"

 class Ole
 {public:
         void Load()
         {       CoInitialize(NULL);
         }
         void Unload()
         {       CoUninitialize();
 }       };
 class MPEG
 {       IMtrCapture* mpeg;
         const char* errorMsg;
         enum {len=80};
         char buffer[80];
         Ole ole;
         void GetErrorMsg()
         {       if(mpeg)
                 {       mpeg->GetLastError(buffer,len-1);
         }       }
         int GetOLE()
     {   HRESULT status = CoCreateInstance(
                 CLSID_MtrMe,
                     NULL,
                 CLSCTX_SERVER,
                 IID_IMtrCapture,
                     (void**)&mpeg);
         return !FAILED(status);
     }
 public:
         operator!() const
         {       return !mpeg;
         }
         MPEG()
         {       mpeg=0;
                 errorMsg="CoCreateInstance failed";
                 ole.Load();
                 if(!GetOLE())
                 {       mpeg=0;
                         return;
                 }
                 if(mpeg->Open()!=S_OK)
                 {       GetErrorMsg();
                         mpeg->Release();
                         mpeg=0;
         }       }
         ~MPEG()
         {       Unload();
         }
         void Unload()
         {       if(!mpeg)
                 {       return;
                 }
                 Stop();
                 mpeg->Close();
                 mpeg->Release();
                 mpeg=0;
                 ole.Unload();
         }
         const char* ErrorMsg() const
         {       return errorMsg;
         }
         int Open(const char* filename)
         {       if(!mpeg)
                 {       return 0;
                 }
                 if(mpeg->SetFileName((char*)filename)!=S_OK)
                 {       errorMsg="SetFileName failed";
                         return 0;
                 }
                 if(mpeg->OpenStream()!=S_OK)
                 {       errorMsg="OpenStream failed";
                         return 0;
                 }
                 return 1;
         }
         void Start()
         {       if(!mpeg)
                 {       return;
                 }
                 mpeg->Start();
         }
         void Stop()
         {       if(!mpeg)
                 {       return;
                 }
                 mpeg->Stop();
                 mpeg->CloseStream();
         }
 };
 #endif

Listing Four
 // hello_cgi.cpp
 #include <iostream.h>

 int main()
 {   cout<<"Content-Type: text/html\n\n"
           "<HTML><HEAD><TITLE>Hello cgi</TITLE></HEAD>\n"
           "<BODY><H2>Hello World!</H2>"
           "</BODY>\n"
           "</HTML>"<<endl;
     return 0;
 }



Listing Five
 // form_cgi.cpp
 #include <iostream.h>
 #include <stdlib.h>
 int main()
 {   const char* data=getenv("QUERY_STRING");
     if(!data)
     {   data="NULL";
     }
     cout<<"Content-Type: text/html\n\n"
           "<HTML><HEAD><TITLE>CGI TEST</TITLE></HEAD>\n"
           "<BODY><H2>CGI TEST</H2>"
           "QUERY_STRING=\""<<data<<"\""
           "</BODY>\n"
           "</HTML>"<<endl;
     return 0;
 }

Listing Six
 <HTML>
 <HEAD>
 <TITLE>Test Form</TITLE>
 </HEAD><BODY>
 <H3>Input text and press enter</H3>
 <FORM ACTION="http://tower.jumpsite.com/cgi-bin/form_cgi" METHOD=GET>
 Query string: <INPUT NAME=ISINDEX>
 </FORM>
 <P>
 </BODY>
 </HTML>


Listing Seven
 <HTML>
 <HEAD>
 <TITLE>Video Search</TITLE>
 </HEAD>
 <BODY>
 <EMBED
         type=application/x-SVCRearch-plugin
         name=plugin
         width=400
         height=100>
 <P>
 <APPLET CODE="Applet1.class"
         MAYSCRIPT
         WIDTH=430
         HEIGHT=270>
 <PARAM name="dataDir" value="C:/video">
 <PARAM name="hostURL" value="file://C:/windows/desktop/javacode.html">
 <PARAM name="filename" value="C:/windows/desktop/javacode.html">
 </APPLET>
 </BODY>
 </HTML>

Listing Eight
       ...
       System.out.println("Fetching plug-in...");
       JSObject win=JSObject.getWindow(this);
       JSObject doc = (JSObject) win.getMember("document");
       Plugin_tv_cgi plug=(Plugin_tv_cgi)doc.getMember("plugin");
       System.out.println("Searching...");
       int errorCode=plug.Search(dataDir,hostURL,query,filename);
       ...

4


