Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ofApp.h"
- //--------------------------------------------------------------
- void ofApp::setup(){
- // whether to append to the log (true) or overwrite (false)
- bool appendLog = false;
- // path to videos directory, may be absolute or relative to bin/data
- videoDirectory = "";
- // setup logging
- ofSetLogLevel(OF_LOG_VERBOSE); // OF_LOG_VERBOSE| OF_LOG_NOTICE | OF_LOG_WARNING | OF_LOG_ERROR | OF_LOG_FATAL_ERROR | OF_LOG_SILENT
- ofLogToFile("tvSimulator_log.txt", appendLog); // comment this out to log to console, true is append, false is overwrite
- // log the time the app started
- ofLogNotice("System time: " + ofGetTimestampString("%w, %e %B %Y, %H:%M:%S"));
- // setup basic settings
- ofBackground(ofColor::white);
- ofSetVerticalSync(true);
- ofHideCursor();
- videoStarted = false;
- videosLoaded = false;
- requestVideoLoad = false;
- requestVideoRefresh = false;
- // load the fonts
- channelFont.loadFont("Golden-Sun.ttf", 64);
- noticeFont.loadFont(OF_TTF_SANS, 32);
- normalFont.loadFont(OF_TTF_MONO, 9);
- }
- //--------------------------------------------------------------
- void ofApp::update(){
- if(videoStarted) {
- videoPlayers[currentVideo].update();
- }
- if(!videosLoaded && !requestVideoLoad && ofGetElapsedTimef() > 2) {
- requestVideoLoad = true;
- // load the videos from the xml file
- loadFromXml();
- }
- if(requestVideoRefresh && (ofGetElapsedTimef() - timeRequestVideoRefresh > 2)) {
- requestVideoRefresh = false;
- // refresh the videos from the directory
- reloadFiles();
- }
- }
- //--------------------------------------------------------------
- void ofApp::draw(){
- if(!videoStarted) {
- if(!videosLoaded) {
- // display the please wait screen
- ofSetColor(ofColor::black);
- noticeFont.drawString("Please wait...", ofGetWindowWidth()/2 - 175,ofGetWindowHeight()/2);
- } else {
- // display the title and instructions
- ofSetColor(ofColor::black);
- noticeFont.drawString("TV Simulator", 25, 50);
- normalFont.drawString("Developed by C Nolan <chris@cenolan.com>", ofGetWindowWidth()-300, 50);
- normalFont.drawString("Keyboard: Press 'f' to toggle fullscreen, 'p' to play, or 'r' to rescan videos directory", 25, 80);
- normalFont.drawString("Remote: Press 'double up' to toggle fullscreen, 'up' to play, or 'down' to rescan videos directory", 25, 95);
- normalFont.drawString("Once playback has started, reload is disabled and only keyboard can toggle fullscreen. Press cmd + Q to quit.", 25, 125);
- 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);
- // show the list of videos loaded from the xml file
- for(int i = 0; i < totalVideos; i++){
- string nextVideo = "VIDEO["+ofToString(i)+"]";
- XML.reset();
- XML.setTo(nextVideo);
- ofFile file;
- if(!file.doesFileExist(XML.getValue("FILE"))) {
- // show the file in red if it doesn't exist
- ofSetColor(ofColor::red);
- } else {
- ofSetColor(ofColor::black);
- }
- string fileInfo = "Channel " + ofToString(i + 1) + ": " + XML.getValue("FILE");
- normalFont.drawString(fileInfo, 25,i * 15 + 170);
- }
- }
- }
- if(videoStarted) {
- // draw the video
- ofSetColor(ofColor::white);
- videoPlayers[currentVideo].draw(0,0, ofGetWindowWidth(), ofGetWindowHeight()); // output the video to the screen
- // show the channel number for 5 seconds after the channel is changed
- if((ofGetElapsedTimef() - timeChannelChanged) < 5) {
- char channelNumber[2];
- sprintf(channelNumber, "%02d", currentVideo + 1);
- ofSetColor(ofColor::green);
- channelFont.drawString(ofToString(channelNumber),100,100); // show the channel number on screen
- }
- }
- }
- //--------------------------------------------------------------
- void ofApp::keyPressed(int key){
- /*
- * Remote control key presses:
- *
- * 356 : Left : go down a channel
- * 357 : Up : start playback
- * 358 : Right : go up a channel
- * 359 : Down : reload videos
- * 360 : Scroll Up : toggle fullscreen
- * 361 : Scroll Down :
- * -1 : Menu :
- *
- */
- //ofLogNotice("Key pressed: " + ofToString(key));
- switch(key){
- // move to previous video
- case OF_KEY_LEFT:
- if(videoStarted) {
- if((ofGetElapsedTimef() - timeChannelChanged) > 2) {
- videoPlayers[currentVideo].stop();
- if (currentVideo == 0) {
- currentVideo = totalVideos - 1;
- } else {
- currentVideo--;
- }
- goStartFrame();
- }
- }
- break;
- // move to next video
- case OF_KEY_RIGHT:
- if(videoStarted) {
- if((ofGetElapsedTimef() - timeChannelChanged) > 2) {
- videoPlayers[currentVideo].stop();
- if (currentVideo == totalVideos - 1) {
- currentVideo = 0;
- } else {
- currentVideo++;
- }
- goStartFrame();
- }
- }
- break;
- // toggle fullscreen mode
- case 'f':
- ofToggleFullscreen();
- break;
- // toggle fullscreen mode (remote control)
- case 360:
- if(!videoStarted) {
- ofToggleFullscreen();
- }
- break;
- // reload files
- case 'r':
- case 359:
- if(!videoStarted) {
- videosLoaded = false;
- timeRequestVideoRefresh = ofGetElapsedTimef();
- requestVideoRefresh = true;
- }
- break;
- // start the playback
- case 'p':
- case 357:
- if(!videoStarted) {
- timePlaybackStarted = ofGetElapsedTimef();
- videoStarted = true;
- goStartFrame();
- }
- break;
- }
- }
- //--------------------------------------------------------------
- void ofApp::keyReleased(int key){
- }
- //--------------------------------------------------------------
- void ofApp::mouseMoved(int x, int y ){
- }
- //--------------------------------------------------------------
- void ofApp::mouseDragged(int x, int y, int button){
- }
- //--------------------------------------------------------------
- void ofApp::mousePressed(int x, int y, int button){
- }
- //--------------------------------------------------------------
- void ofApp::mouseReleased(int x, int y, int button){
- }
- //--------------------------------------------------------------
- void ofApp::windowResized(int w, int h){
- }
- //--------------------------------------------------------------
- void ofApp::gotMessage(ofMessage msg){
- }
- //--------------------------------------------------------------
- void ofApp::dragEvent(ofDragInfo dragInfo){
- }
- //--------------------------------------------------------------
- void ofApp::goStartFrame() {
- // log the request
- ofLogNotice("-- Channel Change Requested --");
- // navigate to the correct XML node
- string nextVideo = "VIDEO["+ofToString(currentVideo)+"]";
- XML.reset(); // go back to the root node
- XML.setTo(nextVideo); // go to the requested video node
- // work out which frame to jump to based on elapsed time
- float timeElapsed = ofGetElapsedTimef() - timePlaybackStarted; // time in seconds since playback was started
- timeChannelChanged = ofGetElapsedTimef(); // remember the time that the channel changed
- float videoDuration = XML.getFloatValue("DURATION");
- float videoFrames = XML.getIntValue("FRAMES");
- float videoFPS = XML.getIntValue("FPS");
- float startTime = fmod(timeElapsed, videoDuration); // calculated start point in seconds
- int startFrame = int(startTime * videoFPS); // start point frame
- if(startFrame > videoFrames || startFrame == 0) {
- startFrame = 1;
- }
- // go to the correct frame and start playback
- videoPlayers[currentVideo].setFrame(startFrame);
- videoPlayers[currentVideo].play();
- // some logging stuff
- ofLogNotice("-- Channel Changed --");
- ofLogNotice("New channel: " + ofToString(currentVideo + 1));
- ofLogNotice("File: " + ofToString(XML.getValue("FILE")));
- ofLogNotice("ElapsedTime: " + ofToString(timeElapsed));
- ofLogNotice("Duration: " + ofToString(videoDuration));
- ofLogNotice("Frames: " + ofToString(videoFrames));
- ofLogNotice("FPS: " + ofToString(videoFPS));
- ofLogNotice("startTime: " + ofToString(startTime));
- ofLogNotice("startFrame: " + ofToString(startFrame));
- }
- //--------------------------------------------------------------
- void ofApp::loadFromXml() {
- // log the request
- ofLogNotice("-- Load Videos from XML Requested --");
- XML.load("videos.xml");
- XML.reset();
- currentVideo = 0;
- totalVideos = XML.getNumChildren();
- ofLogNotice("Total Videos in XML: " + ofToString(totalVideos));
- // allocate the vector to have as many ofVideoPlayers as files
- if( totalVideos > 0 ){
- videoPlayers.assign(totalVideos, ofVideoPlayer());
- }
- // iterate through the files and load them into the ofVideoPlayer vector
- for(int i = 0; i < (int)totalVideos; i++){
- string nextVideo = "VIDEO["+ofToString(i)+"]";
- XML.reset(); // go back to the root node
- XML.setTo(nextVideo); // go to the requested video node
- ofLogNotice("Loading file: " + ofToString(i + 1) + " " + ofToString(XML.getValue("FILE")));
- ofFile file;
- if(file.doesFileExist(XML.getValue("FILE"))) {
- videoPlayers[i].loadMovie(XML.getValue("FILE"));
- }
- }
- videosLoaded = true;
- }
- //--------------------------------------------------------------
- void ofApp::reloadFiles() {
- // log the request
- ofLogNotice("-- Rescan Files Requested --");
- // set up the directory to scan
- ofDirectory* newDir;
- newDir = new ofDirectory(videoDirectory);
- newDir->listDir();
- newDir->sort();
- // clear the videoFiles vector
- videoFiles.clear();
- // scan the directory for video files (re-populates videoFiles vector)
- scanDirectory(*newDir);
- ofLogNotice("Total Files Found: " + ofToString(videoFiles.size()));
- // prepare our XML object for storing the video details
- XML.clear(); // start from fresh
- XML.addChild("VIDEOS"); // make root node
- XML.setTo("VIDEOS"); // move into root node
- ofXml videoNode; // a new XML object which will be used for each single video node
- // allocate the vector to have as many ofVideoPlayers as files
- if( videoFiles.size() ){
- videoPlayers.assign(videoFiles.size(), ofVideoPlayer());
- }
- // iterate through the video files
- for(int i = 0; i < (int)videoFiles.size(); i++){
- // load this file into the ofVideoPlayers vector
- videoPlayers[i].loadMovie(videoFiles[i].getAbsolutePath());
- // get the required meta data
- float videoDuration = videoPlayers[i].getDuration(); // duration in seconds
- int videoFrames = videoPlayers[i].getTotalNumFrames(); // number of frames
- float videoFPS = videoFrames / videoDuration; // frames per second
- // populate the video node to be added to the XML
- videoNode.addChild("VIDEO"); // add child node
- videoNode.setTo("VIDEO"); // move into child node
- videoNode.addValue("FILE", videoFiles[i].getAbsolutePath());
- videoNode.addValue("DURATION", videoDuration);
- videoNode.addValue("FRAMES", videoFrames);
- videoNode.addValue("FPS", videoFPS);
- XML.addXml(videoNode); // add the video node to the XML dom
- videoNode.clear(); // clear the video node
- }
- XML.save("videos.xml"); // save the XML file
- XML.reset(); // go back to the root node
- totalVideos = XML.getNumChildren(); // count number of videos
- ofLogNotice("Total Videos: " + ofToString(totalVideos));
- videosLoaded = true;
- }
- //--------------------------------------------------------------
- void ofApp::scanDirectory(ofDirectory dir) {
- int i, size;
- ofDirectory* newDir;
- // get size of directory passed
- size = dir.size();
- // loop through the directory
- for (i = 0; i < size; i++) {
- // if the current file is a directory, recurse
- if (dir.getFile(i).isDirectory()==1){
- newDir = new ofDirectory(dir.getFile(i).getAbsolutePath());
- newDir->listDir();
- newDir->sort();
- scanDirectory(*newDir);
- // else if the current file has extension mp4 then add it to the videoFiles vector
- } else if (dir.getFile(i).getExtension() == "mp4") {
- videoFiles.push_back(dir.getFile(i));
- ofLogNotice("File Added: " + ofToString(i + 1) + " " + ofToString(dir.getFile(i).getAbsolutePath()));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement