Advertisement
Guest User

Untitled

a guest
Jul 1st, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.92 KB | None | 0 0
  1. #include "ofApp.h"
  2.  
  3. //--------------------------------------------------------------
  4. void ofApp::setup(){
  5.  
  6.     // whether to append to the log (true) or overwrite (false)
  7.     bool appendLog = false;
  8.  
  9.     // path to videos directory, may be absolute or relative to bin/data
  10.     videoDirectory = "";
  11.  
  12.     // setup logging
  13.     ofSetLogLevel(OF_LOG_VERBOSE); // OF_LOG_VERBOSE| OF_LOG_NOTICE | OF_LOG_WARNING | OF_LOG_ERROR | OF_LOG_FATAL_ERROR | OF_LOG_SILENT
  14.     ofLogToFile("tvSimulator_log.txt", appendLog); // comment this out to log to console, true is append, false is overwrite
  15.  
  16.     // log the time the app started
  17.     ofLogNotice("System time: " + ofGetTimestampString("%w, %e %B %Y, %H:%M:%S"));
  18.  
  19.     // setup basic settings
  20.     ofBackground(ofColor::white);
  21.     ofSetVerticalSync(true);
  22.     ofHideCursor();
  23.     videoStarted = false;
  24.     videosLoaded = false;
  25.     requestVideoLoad = false;
  26.     requestVideoRefresh = false;
  27.  
  28.     // load the fonts
  29.     channelFont.loadFont("Golden-Sun.ttf", 64);
  30.     noticeFont.loadFont(OF_TTF_SANS, 32);
  31.     normalFont.loadFont(OF_TTF_MONO, 9);
  32.    
  33. }
  34.  
  35. //--------------------------------------------------------------
  36. void ofApp::update(){
  37.    
  38.     if(videoStarted) {
  39.         videoPlayers[currentVideo].update();
  40.     }
  41.  
  42.     if(!videosLoaded && !requestVideoLoad && ofGetElapsedTimef() > 2) {
  43.         requestVideoLoad = true;
  44.         // load the videos from the xml file
  45.         loadFromXml();
  46.     }
  47.  
  48.     if(requestVideoRefresh && (ofGetElapsedTimef() - timeRequestVideoRefresh > 2)) {
  49.         requestVideoRefresh = false;
  50.         // refresh the videos from the directory
  51.         reloadFiles();
  52.     }
  53.  
  54. }
  55.  
  56. //--------------------------------------------------------------
  57. void ofApp::draw(){
  58.    
  59.     if(!videoStarted) {
  60.        
  61.         if(!videosLoaded) {
  62.  
  63.             // display the please wait screen
  64.             ofSetColor(ofColor::black);
  65.             noticeFont.drawString("Please wait...", ofGetWindowWidth()/2 - 175,ofGetWindowHeight()/2);
  66.  
  67.         } else {
  68.  
  69.             // display the title and instructions
  70.             ofSetColor(ofColor::black);
  71.             noticeFont.drawString("TV Simulator", 25, 50);
  72.             normalFont.drawString("Developed by C Nolan <chris@cenolan.com>", ofGetWindowWidth()-300, 50);
  73.             normalFont.drawString("Keyboard: Press 'f' to toggle fullscreen, 'p' to play, or 'r' to rescan videos directory", 25, 80);
  74.             normalFont.drawString("Remote: Press 'double up' to toggle fullscreen, 'up' to play, or 'down' to rescan videos directory", 25, 95);
  75.             normalFont.drawString("Once playback has started, reload is disabled and only keyboard can toggle fullscreen. Press cmd + Q to quit.", 25, 125);
  76.             normalFont.drawString("If any videos below are shown in red this means the files are missing and you should rescan the videos directory.", 25, 140);
  77.            
  78.             // show the list of videos loaded from the xml file
  79.             for(int i = 0; i < totalVideos; i++){
  80.                 string nextVideo = "VIDEO["+ofToString(i)+"]";
  81.                 XML.reset();
  82.                 XML.setTo(nextVideo);
  83.                 ofFile file;
  84.                 if(!file.doesFileExist(XML.getValue("FILE"))) {
  85.                     // show the file in red if it doesn't exist
  86.                     ofSetColor(ofColor::red);
  87.                 } else {
  88.                     ofSetColor(ofColor::black);
  89.                 }
  90.                 string fileInfo = "Channel " + ofToString(i + 1) + ": " + XML.getValue("FILE");
  91.                 normalFont.drawString(fileInfo, 25,i * 15 + 170);
  92.             }
  93.  
  94.         }
  95.  
  96.     }
  97.    
  98.     if(videoStarted) {
  99.  
  100.         // draw the video
  101.         ofSetColor(ofColor::white);
  102.         videoPlayers[currentVideo].draw(0,0, ofGetWindowWidth(), ofGetWindowHeight()); // output the video to the screen
  103.  
  104.         // show the channel number for 5 seconds after the channel is changed
  105.         if((ofGetElapsedTimef() - timeChannelChanged) < 5) {
  106.             char channelNumber[2];
  107.             sprintf(channelNumber, "%02d", currentVideo + 1);
  108.             ofSetColor(ofColor::green);
  109.             channelFont.drawString(ofToString(channelNumber),100,100); // show the channel number on screen
  110.         }
  111.  
  112.     }
  113.    
  114. }
  115.  
  116. //--------------------------------------------------------------
  117. void ofApp::keyPressed(int key){
  118.  
  119.     /*
  120.      * Remote control key presses:
  121.      *
  122.      *  356 : Left        : go down a channel
  123.      *  357 : Up          : start playback
  124.      *  358 : Right       : go up a channel
  125.      *  359 : Down        : reload videos
  126.      *  360 : Scroll Up   : toggle fullscreen
  127.      *  361 : Scroll Down :
  128.      *  -1  : Menu        :
  129.      *
  130.      */
  131.     //ofLogNotice("Key pressed: " + ofToString(key));
  132.  
  133.     switch(key){
  134.  
  135.         // move to previous video
  136.         case OF_KEY_LEFT:
  137.             if(videoStarted) {
  138.                 if((ofGetElapsedTimef() - timeChannelChanged) > 2) {
  139.                     videoPlayers[currentVideo].stop();
  140.                     if (currentVideo == 0) {
  141.                         currentVideo = totalVideos - 1;
  142.                     } else {
  143.                         currentVideo--;
  144.                     }
  145.                     goStartFrame();
  146.                 }
  147.             }
  148.             break;
  149.  
  150.         // move to next video
  151.         case OF_KEY_RIGHT:
  152.             if(videoStarted) {
  153.                 if((ofGetElapsedTimef() - timeChannelChanged) > 2) {
  154.                     videoPlayers[currentVideo].stop();
  155.                     if (currentVideo == totalVideos - 1) {
  156.                         currentVideo = 0;
  157.                     } else {
  158.                         currentVideo++;
  159.                     }
  160.                     goStartFrame();
  161.                 }
  162.             }
  163.             break;
  164.  
  165.         // toggle fullscreen mode
  166.         case 'f':
  167.             ofToggleFullscreen();
  168.             break;
  169.  
  170.         // toggle fullscreen mode (remote control)
  171.         case 360:
  172.             if(!videoStarted) {
  173.                 ofToggleFullscreen();
  174.             }
  175.             break;
  176.  
  177.         // reload files
  178.         case 'r':
  179.         case 359:
  180.             if(!videoStarted) {
  181.                 videosLoaded = false;
  182.                 timeRequestVideoRefresh = ofGetElapsedTimef();
  183.                 requestVideoRefresh = true;
  184.             }
  185.             break;
  186.  
  187.         // start the playback
  188.         case 'p':
  189.         case 357:
  190.             if(!videoStarted) {
  191.                 timePlaybackStarted = ofGetElapsedTimef();
  192.                 videoStarted = true;
  193.                 goStartFrame();
  194.             }
  195.             break;
  196.     }
  197. }
  198.  
  199. //--------------------------------------------------------------
  200. void ofApp::keyReleased(int key){
  201.    
  202. }
  203.  
  204. //--------------------------------------------------------------
  205. void ofApp::mouseMoved(int x, int y ){
  206.    
  207. }
  208.  
  209. //--------------------------------------------------------------
  210. void ofApp::mouseDragged(int x, int y, int button){
  211.    
  212. }
  213.  
  214. //--------------------------------------------------------------
  215. void ofApp::mousePressed(int x, int y, int button){
  216.    
  217. }
  218.  
  219. //--------------------------------------------------------------
  220. void ofApp::mouseReleased(int x, int y, int button){
  221.    
  222. }
  223.  
  224. //--------------------------------------------------------------
  225. void ofApp::windowResized(int w, int h){
  226.    
  227. }
  228.  
  229. //--------------------------------------------------------------
  230. void ofApp::gotMessage(ofMessage msg){
  231.    
  232. }
  233.  
  234. //--------------------------------------------------------------
  235. void ofApp::dragEvent(ofDragInfo dragInfo){
  236.    
  237. }
  238.  
  239. //--------------------------------------------------------------
  240. void ofApp::goStartFrame() {
  241.    
  242.     // log the request
  243.     ofLogNotice("-- Channel Change Requested --");
  244.    
  245.     // navigate to the correct XML node
  246.     string nextVideo = "VIDEO["+ofToString(currentVideo)+"]";
  247.     XML.reset(); // go back to the root node
  248.     XML.setTo(nextVideo); // go to the requested video node
  249.  
  250.     // work out which frame to jump to based on elapsed time
  251.     float timeElapsed = ofGetElapsedTimef() - timePlaybackStarted; // time in seconds since playback was started
  252.     timeChannelChanged = ofGetElapsedTimef(); // remember the time that the channel changed
  253.  
  254.     float videoDuration = XML.getFloatValue("DURATION");
  255.     float videoFrames = XML.getIntValue("FRAMES");
  256.     float videoFPS = XML.getIntValue("FPS");
  257.    
  258.     float startTime = fmod(timeElapsed, videoDuration); // calculated start point in seconds
  259.     int startFrame = int(startTime * videoFPS); // start point frame
  260.     if(startFrame > videoFrames || startFrame == 0) {
  261.         startFrame = 1;
  262.     }
  263.    
  264.     // go to the correct frame and start playback
  265.     videoPlayers[currentVideo].setFrame(startFrame);
  266.     videoPlayers[currentVideo].play();
  267.  
  268.     // some logging stuff
  269.     ofLogNotice("-- Channel Changed --");
  270.     ofLogNotice("New channel: " + ofToString(currentVideo + 1));
  271.     ofLogNotice("File: " + ofToString(XML.getValue("FILE")));
  272.     ofLogNotice("ElapsedTime: " + ofToString(timeElapsed));
  273.     ofLogNotice("Duration: " + ofToString(videoDuration));
  274.     ofLogNotice("Frames: " + ofToString(videoFrames));
  275.     ofLogNotice("FPS: " + ofToString(videoFPS));
  276.     ofLogNotice("startTime: " + ofToString(startTime));
  277.     ofLogNotice("startFrame: " + ofToString(startFrame));
  278.    
  279. }
  280.  
  281. //--------------------------------------------------------------
  282. void ofApp::loadFromXml() {
  283.  
  284.     // log the request
  285.     ofLogNotice("-- Load Videos from XML Requested --");
  286.  
  287.     XML.load("videos.xml");
  288.     XML.reset();
  289.     currentVideo = 0;
  290.     totalVideos = XML.getNumChildren();
  291.     ofLogNotice("Total Videos in XML: " + ofToString(totalVideos));
  292.    
  293.     // allocate the vector to have as many ofVideoPlayers as files
  294.     if( totalVideos > 0 ){
  295.         videoPlayers.assign(totalVideos, ofVideoPlayer());
  296.     }
  297.  
  298.  
  299.     // iterate through the files and load them into the ofVideoPlayer vector
  300.     for(int i = 0; i < (int)totalVideos; i++){
  301.         string nextVideo = "VIDEO["+ofToString(i)+"]";
  302.         XML.reset(); // go back to the root node
  303.         XML.setTo(nextVideo); // go to the requested video node
  304.         ofLogNotice("Loading file: " + ofToString(i + 1) + " " + ofToString(XML.getValue("FILE")));
  305.         ofFile file;
  306.         if(file.doesFileExist(XML.getValue("FILE"))) {
  307.             videoPlayers[i].loadMovie(XML.getValue("FILE"));
  308.         }
  309.     }
  310.  
  311.     videosLoaded = true;
  312.  
  313. }
  314.  
  315. //--------------------------------------------------------------
  316. void ofApp::reloadFiles() {
  317.    
  318.     // log the request
  319.     ofLogNotice("-- Rescan Files Requested --");
  320.  
  321.     // set up the directory to scan
  322.     ofDirectory* newDir;
  323.     newDir = new ofDirectory(videoDirectory);
  324.     newDir->listDir();
  325.     newDir->sort();
  326.  
  327.     // clear the videoFiles vector
  328.     videoFiles.clear();
  329.  
  330.     // scan the directory for video files (re-populates videoFiles vector)
  331.     scanDirectory(*newDir);
  332.  
  333.     ofLogNotice("Total Files Found: " + ofToString(videoFiles.size()));
  334.  
  335.     // prepare our XML object for storing the video details
  336.     XML.clear(); // start from fresh
  337.     XML.addChild("VIDEOS"); // make root node
  338.     XML.setTo("VIDEOS"); // move into root node
  339.    
  340.     ofXml videoNode; // a new XML object which will be used for each single video node
  341.  
  342.     // allocate the vector to have as many ofVideoPlayers as files
  343.     if( videoFiles.size() ){
  344.         videoPlayers.assign(videoFiles.size(), ofVideoPlayer());
  345.     }
  346.  
  347.     // iterate through the video files
  348.     for(int i = 0; i < (int)videoFiles.size(); i++){
  349.  
  350.         // load this file into the ofVideoPlayers vector
  351.         videoPlayers[i].loadMovie(videoFiles[i].getAbsolutePath());
  352.  
  353.         // get the required meta data
  354.         float videoDuration = videoPlayers[i].getDuration(); // duration in seconds
  355.         int videoFrames = videoPlayers[i].getTotalNumFrames(); // number of frames
  356.         float videoFPS = videoFrames / videoDuration; // frames per second
  357.        
  358.         // populate the video node to be added to the XML
  359.         videoNode.addChild("VIDEO"); // add child node
  360.         videoNode.setTo("VIDEO"); // move into child node
  361.        
  362.         videoNode.addValue("FILE", videoFiles[i].getAbsolutePath());
  363.         videoNode.addValue("DURATION", videoDuration);
  364.         videoNode.addValue("FRAMES", videoFrames);
  365.         videoNode.addValue("FPS", videoFPS);
  366.        
  367.         XML.addXml(videoNode); // add the video node to the XML dom
  368.         videoNode.clear(); // clear the video node
  369.     }
  370.        
  371.     XML.save("videos.xml"); // save the XML file
  372.    
  373.     XML.reset(); // go back to the root node
  374.     totalVideos = XML.getNumChildren(); // count number of videos
  375.     ofLogNotice("Total Videos: " + ofToString(totalVideos));
  376.    
  377.     videosLoaded = true;
  378.  
  379. }
  380.  
  381. //--------------------------------------------------------------
  382. void ofApp::scanDirectory(ofDirectory dir) {
  383.    
  384.     int i, size;
  385.     ofDirectory* newDir;
  386.  
  387.     // get size of directory passed
  388.     size = dir.size();
  389.    
  390.     // loop through the directory
  391.     for (i = 0; i < size; i++) {
  392.         // if the current file is a directory, recurse
  393.         if (dir.getFile(i).isDirectory()==1){
  394.             newDir = new ofDirectory(dir.getFile(i).getAbsolutePath());
  395.             newDir->listDir();
  396.             newDir->sort();
  397.             scanDirectory(*newDir);
  398.         // else if the current file has extension mp4 then add it to the videoFiles vector
  399.         } else if (dir.getFile(i).getExtension() == "mp4") {
  400.             videoFiles.push_back(dir.getFile(i));
  401.             ofLogNotice("File Added: " + ofToString(i + 1) + " " + ofToString(dir.getFile(i).getAbsolutePath()));
  402.         }
  403.     }
  404.    
  405. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement