Browse Source

Merge branch 'DirectoryWatcher'

master
c-mendoza 8 years ago
parent
commit
ae75eb62b5
  1. 6
      .gitignore
  2. 11
      example/bin/data/ofxpimapper.xml
  3. 37
      src/MediaServer/DirectoryWatcher.cpp
  4. 18
      src/MediaServer/DirectoryWatcher.h
  5. 15
      src/MediaServer/MediaServer.cpp

6
.gitignore

@ -113,3 +113,9 @@ example/\.idea/
example/cmake-build-release/ example/cmake-build-release/
example/example\.xcodeproj/xcshareddata/xcschemes/ example/example\.xcodeproj/xcshareddata/xcschemes/
.idea/
cmake-build-debug/
CMakeLists\.txt

11
example/bin/data/ofxpimapper.xml

@ -73,6 +73,17 @@
<source> <source>
<source-type>fbo</source-type> <source-type>fbo</source-type>
<source-name>Slide Show Source</source-name> <source-name>Slide Show Source</source-name>
<magSlideShow>
<Width>1920</Width>
<Height>1080</Height>
<SlideDuration>1</SlideDuration> <!-- Optional default duration for each slide-->
<Loop>
<Type>PING-PONG</Type> <!-- NONE | NORMAL | PING-PONG -->
</Loop>
<Transition></Transition>
<!-- NoResize | Native | Fit | FitProportionally | FillProportionally -->
<ResizeOption>FitProportionally</ResizeOption>
</magSlideShow>
</source> </source>
<properties> <properties>
<perspectiveWarping>1</perspectiveWarping> <perspectiveWarping>1</perspectiveWarping>

37
src/MediaServer/DirectoryWatcher.cpp

@ -4,6 +4,7 @@ namespace ofx {
namespace piMapper { namespace piMapper {
DirectoryWatcher::DirectoryWatcher(string path, int watcherMediaType){ DirectoryWatcher::DirectoryWatcher(string path, int watcherMediaType){
directoryPath = path;
_mediaType = watcherMediaType; _mediaType = watcherMediaType;
if(_mediaType == SourceType::SOURCE_TYPE_VIDEO){ if(_mediaType == SourceType::SOURCE_TYPE_VIDEO){
@ -22,19 +23,26 @@ DirectoryWatcher::DirectoryWatcher(string path, int watcherMediaType){
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
_directory.listDir(path); _directory.listDir(directoryPath);
_directory.sort(); _directory.sort();
for(int i = 0; i < _directory.size(); ++i){ for(int i = 0; i < _directory.size(); ++i){
_filePaths.push_back(path + _directory.getName(i)); _filePaths.push_back(directoryPath + _directory.getName(i));
ofFile file(path + _directory.getName(i)); ofFile file(directoryPath + _directory.getName(i));
if(_mediaType == SourceType::SOURCE_TYPE_VIDEO){ if(_mediaType == SourceType::SOURCE_TYPE_VIDEO){
file.copyTo("sources/videos/" + _directory.getName(i)); file.copyTo("sources/videos/" + _directory.getName(i));
}else if(_mediaType == SourceType::SOURCE_TYPE_IMAGE){ }else if(_mediaType == SourceType::SOURCE_TYPE_IMAGE){
file.copyTo("sources/images/" + _directory.getName(i)); file.copyTo("sources/images/" + _directory.getName(i));
} }
} }
dirSize = _directory.size();
}
DirectoryWatcher::~DirectoryWatcher() {
endWatch();
// waitForThread(false);
} }
vector <string> & DirectoryWatcher::getFilePaths(){ vector <string> & DirectoryWatcher::getFilePaths(){
@ -45,5 +53,28 @@ int DirectoryWatcher::getMediaType(){
return _mediaType; return _mediaType;
} }
void DirectoryWatcher::beginWatch(int intervalInMillis) {
watchInterval = intervalInMillis;
startThread();
}
void DirectoryWatcher::endWatch() {
stopThread();
}
void DirectoryWatcher::threadedFunction() {
while (isThreadRunning()) {
int newSize = _directory.listDir();
if (newSize != dirSize) {
ofLogVerbose("DirectoryWatcher") << "Directory changed";
ofNotifyEvent(directoryFileCountChangedEvent, this);
}
sleep(watchInterval);
}
}
} // namespace piMapper } // namespace piMapper
} // namespace ofx } // namespace ofx

18
src/MediaServer/DirectoryWatcher.h

@ -6,16 +6,32 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class DirectoryWatcher { class DirectoryWatcher : public ofThread {
public: public:
DirectoryWatcher(string path, int watcherMediaType); DirectoryWatcher(string path, int watcherMediaType);
virtual ~DirectoryWatcher();
vector<string> & getFilePaths(); vector<string> & getFilePaths();
int getMediaType(); int getMediaType();
void beginWatch(int intervalInMillis = 5000);
void endWatch();
void threadedFunction();
/**
* Triggered when the file count of a directory increases
* or decreases.
*
* Sender is a pointer to this DirectoryWatcher
*/
ofEvent<void> directoryFileCountChangedEvent;
private: private:
ofDirectory _directory; ofDirectory _directory;
vector<string> _filePaths; vector<string> _filePaths;
std::string directoryPath;
int _mediaType; int _mediaType;
int dirSize;
long watchInterval; // in millis.
}; };
} // namespace piMapper } // namespace piMapper

15
src/MediaServer/MediaServer.cpp

@ -19,14 +19,17 @@ MediaServer::MediaServer():
{ {
// By initialising all above we also copy files from external media // By initialising all above we also copy files from external media
// to the ofxPiMapper storage. // to the ofxPiMapper storage.
imageWatcher.beginWatch();
imageWatcher = DirectoryWatcher(
ofToDataPath(DEFAULT_IMAGES_DIR, true),
SourceType::SOURCE_TYPE_IMAGE);
videoWatcher = DirectoryWatcher( // imageWatcher = DirectoryWatcher(
ofToDataPath(DEFAULT_VIDEOS_DIR, true), // ofToDataPath(DEFAULT_IMAGES_DIR, true),
SourceType::SOURCE_TYPE_VIDEO); // SourceType::SOURCE_TYPE_IMAGE);
// imageWatcher.beginWatch();
//
// videoWatcher = DirectoryWatcher(
// ofToDataPath(DEFAULT_VIDEOS_DIR, true),
// SourceType::SOURCE_TYPE_VIDEO);
} }
void MediaServer::setup(){ void MediaServer::setup(){

Loading…
Cancel
Save