📄 vmwithdirectshow.cpp
字号:
#include <windows.h>
#include <GL/glut.h>
#include <iostream>
#include "VideoManControl.h"
#include "VideoManInputFormat.h"
using namespace std;
/*
This is an example using VideoMan with DirectShow and OpenGL.
Two inputs are initilized using DirectShow: one video file and one camera
To use this example, VideoMan must be built with the directive VM_OGLRenderer,
also you need to build the input VMDirectShow
*/
VideoManControl videoMan;
int screenLeft, screenUp, screenWidth, screenHeight;
bool fullScreened;
double videoLength;
bool activated; //To know if the first input is activated or not
std::vector< int > videoInputIDs; //List of indexes of the initialized inputs
string dirPath;
void glutResize(int width, int height)
{
screenLeft = 0;
screenUp = 0;
screenWidth = width;
screenHeight = height;
//Notify to VideoMan the change of the screen size
videoMan.changeScreenSize( screenLeft, screenUp, screenWidth, screenHeight );
}
void glutKeyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
{
exit(0);
}
}
}
void glutSpecialKeyboard(int value, int x, int y)
{
switch (value)
{
case GLUT_KEY_F1:
{
if ( !fullScreened )
glutFullScreen();
else
{
glutPositionWindow( 0, 20 );
glutReshapeWindow( 640, 480 );
}
fullScreened = !fullScreened;
break;
}
case GLUT_KEY_F2:
{
if ( activated )
videoMan.deactivateVideoInput( 0 );
else
videoMan.activateVideoInput( 0 );
activated = videoMan.isActivated(0);
break;
}
}
}
void InitializeOpenGL()
{
}
bool InitializeVideoMan()
{
VideoManInputFormat format;
inputIdentification device;
//Initialize one input from a video file
device.fileName = dirPath;
device.identifier = "DSHOW_VIDEO_FILE"; //using directshow
format.timeFormat = SECONDS; //We want the time format in seconds
//play in real-time
format.clock = true;
format.dropFrames = true;
format.renderAudio = true;
int inputID;
if ( !dirPath.empty() && ( inputID = videoMan.addVideoInput( device, &format ) ) != -1 )
{
videoInputIDs.push_back( inputID );
printf("Loaded video file: %s\n", device.fileName.c_str() );
printf("resolution: %d %d\n", format.width, format.height );
//get the length of the video
videoLength = videoMan.getLength( inputID );
printf("duration: %f seconds\n\n", videoLength );
videoMan.playVideo( inputID );
}
//Initialize one input from a camera
std::vector<inputIdentification> list;
videoMan.getAvailableDevices( "DSHOW_CAPTURE_DEVICE", list ); //list all the available devices
if ( list.size()>0 )
device = list[0]; //take the first
else
printf("There is no available camera\n");
//Show dialog to select the format
format.showDlg = true;
format.SetFormat( 640, 320,30, RGB24, RGB24 );
if ( list.size()>0 && ( inputID = videoMan.addVideoInput( device, &format ) ) != -1 )
{
videoInputIDs.push_back( inputID );
videoMan.showPropertyPage( inputID );
videoMan.getFormat( inputID, format );
printf("Initilized camera: %s\n", device.friendlyName.c_str() );
printf("resolution: %d %d\n", format.width, format.height );
printf("FPS: %f\n\n", format.fps );
}
//We want to display all the intialized video inputs
videoMan.activateAllVideoInputs();
activated = true;
return ( videoInputIDs.size() > 0);
}
void glutDisplay(void)
{
//Clear the opengl window
glClear( GL_COLOR_BUFFER_BIT );
//For each initialized inputs
for ( size_t n=0; n < videoInputIDs.size(); n++ )
{
//Get a new frame from input n
char *image = videoMan.getFrame( n );
if ( image != NULL )
{
//Update the texture of the renderer
videoMan.updateTexture( n );
/*
Process the image...
*/
//Release the frame
videoMan.releaseFrame( n );
}
//render the image of input n in the screen
videoMan.renderFrame( n );
}
//Check if the video file (input number 0) has reached the end
if ( videoMan.getPosition(0) == videoLength )
videoMan.goToFrame( 0, 0 ); //restart from the begining
glFlush();
glutSwapBuffers();
}
void showHelp()
{
printf("========\n");
printf("keys:\n");
printf("Esc->Exit\n");
printf("F1->Fullscreen\n");
printf("F2->Show Video File y/n\n");
printf("========\n");
}
int main(int argc, char** argv)
{
cout << "This example initilizes two inputs: one video file and one camera" << endl;
cout << "Usage: VMwithDirectShow.exe filePath(string)" << endl;
cout << "Example: VMwithDirectShow.exe c:\\video.avi" << endl;
cout << "The camera is automatically detected" << endl;
cout << "=====================================================" << endl;
if ( argc > 1 )
dirPath = argv[1];
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA | GLUT_MULTISAMPLE );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 640, 480 );
glutInit( &argc, argv );
glutCreateWindow("VideoMan with DirectShow");
glutReshapeFunc(glutResize);
glutDisplayFunc(glutDisplay);
glutIdleFunc(glutDisplay);
glutKeyboardFunc(glutKeyboard);
glutSpecialFunc(glutSpecialKeyboard);
InitializeOpenGL();
if ( !InitializeVideoMan() )
{
return 0;
}
fullScreened = false;
showHelp();
glutMainLoop();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -