3. helloworld11

helloworld11: simple mp3 player using GraphDescription with error handling and access to 'serverregistry'

#include <iostream> 

// include NMM 
#include "nmm/base/connect.hpp"
#include "nmm/base/ProxyObject.hpp"
#include "nmm/base/ProxyApplication.hpp"

// include used interfaces
#include "nmm/interfaces/file/IFileHandler.hpp"

// import namespace
using namespace NMM;

int main(int argc, char ** argv)
{
  if(argc != 2) {
    cerr << "Usage:" << endl
	 << "  " << argv[0] << " <mp3file> " << endl
	 << endl;
  }
  cerr << argv[0] << " running ... " << endl << endl;

  // disable all debug output (error, warning, message, debug)
  // see http://graphics.cs.uni-sb.de/NMM/current/Docs/messages/index.html
  NamedObject::getGlobalInstance().setErrorStream(0, NamedObject::ALL_LEVELS);
  NamedObject::getGlobalInstance().setWarningStream(0, NamedObject::ALL_LEVELS);
  NamedObject::getGlobalInstance().setMessageStream(0, NamedObject::ALL_LEVELS);
  NamedObject::getGlobalInstance().setDebugStream(0, NamedObject::ALL_LEVELS);

  // the graph description for the example
  GraphDescription graph;

  // the NMM application for the example
  NMMApplication* app = 0;

  // NMM-operations may throw exception, in this simple example, all exceptions
  // are just caught at the end, and the application is terminated
  try {
    // create application object for example
    app = ProxyApplication::getApplication(argc, argv);

    // create the descriptions for the GenericReadNode, MPEGAudioDecodeNode, and PlaybackNode
    NodeDescription readfile_descr("GenericReadNode");
    NodeDescription decoder_descr("MPEGAudioDecodeNode");
    NodeDescription audioplay_descr("PlaybackNode");

    // create a flow graph: readfile ->  decoder ->  audioplay
    graph.addEdges(&readfile_descr, &decoder_descr, &audioplay_descr);

    // request complete graph
    cerr << "request GraphDescription from registry" << endl;
    ClientRegistry& registry = app-> getRegistry();
    registry.requestGraph(graph);

    // set the filename by requesting appropriate interface
    cerr << "set filename to " << argv[1] << endl;
    INode* readfile = graph.getINode(readfile_descr);
    IFileHandler_var filehandler (readfile-> getParentObject()-> getCheckedInterface<IFileHandler> ());
    filehandler-> setFilename(argv[1]);

    // realize and start graph
    cerr << "realizeGraph()" << endl;
    graph.realizeGraph();
    cerr << "startGraph()" << endl;
    graph.startGraph();
    
    // wait ...
    cerr << "Playing audio ... enter 'q <enter> ' to exit." << endl;
    string s;
    cin >> s;
    
    // stop and release graph
    cerr << "stopGraph()" << endl;
    graph.stopGraph();

    cerr << "releaseGraph()" << endl;
    registry.releaseGraph(graph);
    return 0;
  }
  catch(const Exception& e) { // catch NMM-exception and print it
    cerr << e << endl;

    // release graph
    cerr << "releaseGraph()" << endl;
    ClientRegistry& registry = app-> getRegistry();
    registry.releaseGraph(graph);

    return 1;
  }
  catch(...) { // any other exception
    cerr << "something went wrong!" << endl;

    // release graph
    cerr << "releaseGraph()" << endl;
    ClientRegistry& registry = app-> getRegistry();
    registry.releaseGraph(graph);

    return 1;
  }
}