This example demonstrates how to write a sequence of images to an AVI file.
The program can be found in the %TOPLEVEL%\Samples\VC10\MediaStreamSinkSample directory. In order to run the program, open the solution file MediaStreamSinkSample.sln in this directory and select Build -> Build SinkType in the menu. Then, the program can be executed by selecting Debug -> Start.
The program generates a list of all available video compressors and asks the end user for the name of the compressor that he/she would like to use and a filename. As soon as the end user presses any key, the program starts capturing. It stops capturing when enter is pressed again, at which point, it writes the captured data to an AVI file.
The example program uses the setupDeviceFromFile function from %TOPLEVEL%\Samples\VC10\Common\CmdHelper.h. This function displays the device selection dialog and saves the selected device in a configuration file, that is loaded when the function is called again.
Grabber grabber; if( !setupDeviceFromFile( grabber ) ) { return -1; }
The static method Codec::getAvailableCodecs returns a list with the available video compressors. With the help of presentUserChoice and toStringArrayPtrListPtr from %TOPLEVEL%\Samples\VC10\Common\CmdHelper.h, this list is displayed on the console and the end user can select one of the available codecs.
tCodecList codecList = Codec::getAvailableCodecList(); // Display all codec names on the screen. int choice = presentUserChoice( toStringArrayPtrList( codecList ) ); if( choice == -1 ) { return -1; }
In order to save image data to a video file, a MediaStreamSink is created and initialized to record an AVI files. The selected codec and filename are set. The sink is initially paused by calling GrabberSinkType::setSinkMode with the parameter ePAUSE.
// Create an MediaStreamSink to record an AVI file with the selected CoDec tMediaStreamSinkPtr pSink = MediaStreamSink::create( MediaStreamContainer::create( MSC_AviContainer ), codecList.at( choice ) ); // Set the filename. pSink->setFilename( filename ); // The sink is initially paused, so that no video data is written to the file. pSink->setSinkMode( GrabberSinkType::ePAUSE ); // Set pSink as the current sink. grabber.setSinkType( pSink ); // Start the live mode. The live video will be displayed but no images will be written // to the AVI file because pSink is in pause mode. if( !grabber.startLive( true ) ) { std::cerr << grabber.getLastError().toString() << std::endl; return -1; } std::cout << "Press [enter] to start capturing!"; std::cin.get(); // Start the sink. The image stream is written to the AVI file. pSink->setSinkMode(GrabberSinkType::eRUN ); std::cout << "Video recording started." << std::endl; std::cout << "Press [enter] to stop capturing!"; std::cin.get(); // Pause the sink. This stops writing the image stream to the AVI file. // A subsequent call to setSinkMode with GrabberSinkType::eRUN as the // parameter would restart AVI recording. pSink->setSinkMode(GrabberSinkType::ePAUSE ); std::cout << "Video recording stopped." << std::endl; // Stop the live mode. This stops writing images to the AVI file if the mode is not // GrabberSinkType::ePAUSE. The AVI file is closed. grabber.stopLive();
After live mode has been started using Grabber::startLive, the program waits for user input. To actually start AVI recording, the sink is unpaused.