Browse Source

Remove ofxIO dependency from MediaServer

master
Krisjanis Rijnieks 8 years ago
parent
commit
e781dfb587
  1. 49
      src/MediaServer/DirectoryWatcher.cpp
  2. 133
      src/MediaServer/DirectoryWatcher.h
  3. 45
      src/MediaServer/MediaServer.cpp
  4. 46
      src/MediaServer/MediaServer.h

49
src/MediaServer/DirectoryWatcher.cpp

@ -1,46 +1,41 @@
//
// DirectoryWatcher.cpp
// example
//
// Created by felix on 23.09.14.
//
//
#include "DirectoryWatcher.h"
namespace ofx {
namespace piMapper {
DirectoryWatcher::DirectoryWatcher(string path, int watcherMediaType){
mediaType = watcherMediaType;
// Decide what filter we need depending on media type
if(mediaType == SourceType::SOURCE_TYPE_VIDEO){
filter = new VideoPathFilter();
}else if(mediaType == SourceType::SOURCE_TYPE_IMAGE){
filter = new ImagePathFilter();
_mediaType = watcherMediaType;
if(_mediaType == SourceType::SOURCE_TYPE_VIDEO){
_directory.allowExt("mp4");
_directory.allowExt("h264");
_directory.allowExt("mov");
_directory.allowExt("avi");
_directory.allowExt("ogv");
_directory.allowExt("mpeg");
}else if(_mediaType == SourceType::SOURCE_TYPE_IMAGE){
_directory.allowExt("png");
_directory.allowExt("jpg");
_directory.allowExt("jpeg");
}else{
ofLogFatalError("DirectoryWatcher::DirectoryWatcher", "Unkonwn media type");
exit(EXIT_FAILURE);
}
dirWatcher.registerAllEvents(this);
// For some reason the filters are not working,
// we leave just the path here and do the filter logic in the listeners
dirWatcher.addPath(path);
// Initial directory listing. Fill the file paths vector.
IO::DirectoryUtils::list(path, filePaths, true, filter);
}
DirectoryWatcher::~DirectoryWatcher(){
delete filter;
filter = 0;
_directory.listDir(path);
_directory.sort();
for(int i = 0; i < _directory.size(); ++i){
_filePaths.push_back(path + _directory.getName(i));
}
}
vector <string> & DirectoryWatcher::getFilePaths(){
return filePaths;
return _filePaths;
}
int DirectoryWatcher::getMediaType(){
return mediaType;
return _mediaType;
}
} // namespace piMapper

133
src/MediaServer/DirectoryWatcher.h

@ -1,146 +1,21 @@
//
// DirectoryWatcher.h
// example
//
// Created by felix on 23.09.14.
//
//
#pragma once
#include "ofMain.h"
#include "ofxIO.h"
#include "SourceType.h"
namespace ofx {
namespace piMapper {
class BasePathFilter : public ofx::IO::AbstractPathFilter {
public:
BasePathFilter(){}
virtual ~BasePathFilter(){}
virtual bool accept(const Poco::Path & path) const { return false; }
};
class VideoPathFilter : public BasePathFilter {
public:
VideoPathFilter(){}
virtual ~VideoPathFilter(){}
// TODO: Make Raspi load only mp4s (and mov and h264?)
// How to detect codec? Read file headers?
bool accept(const Poco::Path & path) const {
return !Poco::File(path).isHidden() &&
(ofIsStringInString(path.toString(), ".mp4") ||
ofIsStringInString(path.toString(), ".h264") ||
ofIsStringInString(path.toString(), ".mov") ||
ofIsStringInString(path.toString(), ".avi") ||
ofIsStringInString(path.toString(), ".ogv") ||
ofIsStringInString(path.toString(), ".mpeg"));
}
};
class ImagePathFilter : public BasePathFilter {
public:
ImagePathFilter(){}
virtual ~ImagePathFilter(){}
bool accept(const Poco::Path & path) const {
return !Poco::File(path).isHidden() &&
(ofIsStringInString(path.toString(), ".png") ||
ofIsStringInString(path.toString(), ".jpg") ||
ofIsStringInString(path.toString(), ".jpeg"));
}
};
class DirectoryWatcher {
public:
DirectoryWatcher(string path, int watcherMediaType);
~DirectoryWatcher();
// TODO make useful stuff with onDirectoryWatcher*
void onDirectoryWatcherItemAdded(const ofx::IO::DirectoryWatcherManager::DirectoryEvent & evt){
string path = evt.item.path();
Poco::Path pocoPath = Poco::Path(path);
if(!filter->accept(pocoPath)){
return;
}
ofLogNotice("DirectoryWatcher::onDirectoryWatcherItemAdded")
<< "Added item: "
<< path;
filePaths.push_back(path);
ofNotifyEvent(onItemAdded, path, this);
}
void onDirectoryWatcherItemRemoved(const ofx::IO::DirectoryWatcherManager::DirectoryEvent & evt){
string path = evt.item.path();
Poco::Path pocoPath = Poco::Path(path);
if(!filter->accept(pocoPath)){
return;
}
// Remove path from vector
for(int i = 0; i < filePaths.size(); i++){
if(path == filePaths[i]){
filePaths.erase(filePaths.begin() + i);
break;
}
}
ofNotifyEvent(onItemRemoved, path, this);
}
void onDirectoryWatcherItemModified(const ofx::IO::DirectoryWatcherManager::DirectoryEvent & evt){
string path = evt.item.path();
Poco::Path pocoPath = Poco::Path(path);
if(!filter->accept(pocoPath)){
return;
}
ofNotifyEvent(onItemModified, path, this);
}
void onDirectoryWatcherItemMovedFrom(const ofx::IO::DirectoryWatcherManager::DirectoryEvent & evt){
string path = evt.item.path();
Poco::Path pocoPath = Poco::Path(path);
if(!filter->accept(pocoPath)){
return;
}
ofLogNotice("ofApp::onDirectoryWatcherItemMovedFrom")
<< "Moved From: " << path;
ofNotifyEvent(onItemMovedFrom, path, this);
}
void onDirectoryWatcherItemMovedTo(const ofx::IO::DirectoryWatcherManager::DirectoryEvent & evt){
string path = evt.item.path();
Poco::Path pocoPath = Poco::Path(path);
if(!filter->accept(pocoPath)){
return;
}
ofLogNotice("ofApp::onDirectoryWatcherItemMovedTo")
<< "Moved To: " << path;
ofNotifyEvent(onItemMovedTo, path, this);
}
void onDirectoryWatcherError(const Poco::Exception & exc){
ofLogError("ofApp::onDirectoryWatcherError")
<< "Error: " << exc.displayText();
}
// Getters
vector <string> & getFilePaths();
vector<string> & getFilePaths();
int getMediaType();
// Custom events
ofEvent <string> onItemAdded;
ofEvent <string> onItemRemoved;
ofEvent <string> onItemModified;
ofEvent <string> onItemMovedFrom;
ofEvent <string> onItemMovedTo;
private:
ofx::IO::DirectoryWatcherManager dirWatcher;
BasePathFilter * filter;
vector <string> filePaths;
int mediaType;
ofDirectory _directory;
vector<string> _filePaths;
int _mediaType;
};
} // namespace piMapper

45
src/MediaServer/MediaServer.cpp

@ -1,11 +1,3 @@
//
// MediaServer.cpp
// example
//
// Created by felix on 13.09.14.
//
//
#include "MediaServer.h"
namespace ofx {
@ -25,11 +17,7 @@ MediaServer::MediaServer():
usb2ImageWatcher(USB2_IMAGES_DIR, SourceType::SOURCE_TYPE_IMAGE),
usb3ImageWatcher(USB3_IMAGES_DIR, SourceType::SOURCE_TYPE_IMAGE)
{
addWatcherListeners();
}
MediaServer::~MediaServer(){
removeWatcherListeners();
// What to do here?
}
void MediaServer::setup(){
@ -464,36 +452,5 @@ void MediaServer::unloadFboSource(string & fboSourceName){
}
} // unloadFboSource
void MediaServer::handleImageAdded(string & path){
ofLogNotice("MediaServer::handleImageAdded")
<< "Image path: "
<< path;
ofNotifyEvent(onImageAdded, path, this);
}
void MediaServer::handleImageRemoved(string & path){
ofNotifyEvent(onImageRemoved, path, this);
}
void MediaServer::handleVideoAdded(string & path){
ofNotifyEvent(onVideoAdded, path, this);
}
void MediaServer::handleVideoRemoved(string & path){
ofNotifyEvent(onVideoRemoved, path, this);
}
void MediaServer::addWatcherListeners(){
ofAddListener(imageWatcher.onItemAdded, this, &MediaServer::handleImageAdded);
ofAddListener(imageWatcher.onItemRemoved, this, &MediaServer::handleImageRemoved);
ofAddListener(videoWatcher.onItemAdded, this, &MediaServer::handleVideoAdded);
ofAddListener(videoWatcher.onItemRemoved, this, &MediaServer::handleVideoRemoved);
}
void MediaServer::removeWatcherListeners(){
ofRemoveListener(imageWatcher.onItemAdded, this, &MediaServer::handleImageAdded);
ofRemoveListener(imageWatcher.onItemRemoved, this, &MediaServer::handleImageRemoved);
ofRemoveListener(videoWatcher.onItemAdded, this, &MediaServer::handleVideoAdded);
ofRemoveListener(videoWatcher.onItemRemoved, this, &MediaServer::handleVideoRemoved);
}
} // namespace piMapper
} // namespace ofx

46
src/MediaServer/MediaServer.h

@ -1,25 +1,8 @@
//
// MediaServer.h
// example
//
// Created by felix on 13.09.14.
//
//
// TODO (by Krisjanis Rijnieks 2016-06-02): Transform MediaServer into a singleton.
// TODO: move reference counting, enabling and disabling of sources
// to source classes themselves
#pragma once
#include "ofMain.h"
#include "DirectoryWatcher.h"
/* Discussion: This could be the right place for a Factory Method or
* Abstract Factory design pattern - replace all these includes with a
* SourceFactory that can create sources with the interfaces below.
*/
#include "BaseSource.h"
#include "ImageSource.h"
#include "VideoSource.h"
@ -64,7 +47,6 @@ namespace piMapper {
class MediaServer {
public:
MediaServer();
virtual ~MediaServer();
void setup();
void update();
@ -133,33 +115,7 @@ class MediaServer {
vector <string> _tempVideoPaths;
map <string, BaseSource *> loadedSources;
// imageWatcher event listeners
void handleImageAdded(string & path);
void handleImageRemoved(string & path);
// TODO rest of listeners
/*
void onImageModified();
void onImageMovedFrom();
void onImageMovedTo();
*/
// videoWatcher event listeners
void handleVideoAdded(string & path);
void handleVideoRemoved(string & path);
// TODO rest of listeners
/*
void onVideoModified();
void onVideoMovedFrom();
void onVideoMovedTo();
*/
// Add/remove event listeners.
// Add event listeners to image and video watcher events.
void addWatcherListeners();
// Remove event listeners to image and video watcher events
void removeWatcherListeners();
// FBO source storage before they go to loadedSources
vector <FboSource *> fboSources; // FBO source storage
};

Loading…
Cancel
Save