Fixed terminology for path (in filesystem) versus uri (with protocol)

for gstreamer in MediaPlayer and MediaSource.
This commit is contained in:
brunoherbelin
2020-05-10 00:40:36 +02:00
parent db0acc9ae2
commit 77cffa160e
16 changed files with 104 additions and 40 deletions

View File

@@ -102,7 +102,7 @@ void ImGuiVisitor::visit(FrameBufferSurface &n)
void ImGuiVisitor::visit(MediaSurface &n)
{
ImGui::Text("%s", n.uri().c_str());
ImGui::Text("%s", n.path().c_str());
if (n.mediaPlayer())
n.mediaPlayer()->accept(*this);

View File

@@ -76,10 +76,10 @@ guint MediaPlayer::texture() const
return textureindex_;
}
void MediaPlayer::open(string uri)
void MediaPlayer::open(string path)
{
// set uri to open
this->uri_ = uri;
uri_ = string( gst_uri_construct("file", path.c_str()) );
// reset
ready_ = false;
@@ -101,8 +101,8 @@ void MediaPlayer::open(string uri)
// start discoverer
gst_discoverer_start(discoverer_);
// Add the request to process asynchronously the URI
if (!gst_discoverer_discover_uri_async (discoverer_, uri.c_str())) {
Log::Warning("MediaPlayer %s Failed to start discovering URI '%s'\n", id_.c_str(), uri.c_str());
if (!gst_discoverer_discover_uri_async (discoverer_, uri_.c_str())) {
Log::Warning("MediaPlayer %s Failed to start discovering URI '%s'\n", id_.c_str(), uri_.c_str());
g_object_unref (discoverer_);
discoverer_ = nullptr;
}

View File

@@ -101,7 +101,7 @@ public:
/**
* Open a media using gstreamer URI
* */
void open( std::string uri_);
void open( std::string path);
/**
* True if a media was oppenned
* */

View File

@@ -160,14 +160,14 @@ void Mixer::draw()
}
// manangement of sources
void Mixer::createSourceMedia(std::string uri)
void Mixer::createSourceMedia(std::string path)
{
// create source
MediaSource *m = new MediaSource();
m->setURI(uri);
m->setPath(path);
// propose a new name based on uri
renameSource(m, SystemToolkit::base_filename(uri));
renameSource(m, SystemToolkit::base_filename(path));
// add to mixer
insertSource(m);

View File

@@ -34,7 +34,7 @@ public:
void draw();
// manangement of sources
void createSourceMedia(std::string uri);
void createSourceMedia(std::string path);
// TODO: deleteSource();
void deleteSource(Source *s);

View File

@@ -113,9 +113,9 @@ void ImageSurface::accept(Visitor& v)
v.visit(*this);
}
MediaSurface::MediaSurface(const std::string& uri, Shader *s) : Surface(s)
MediaSurface::MediaSurface(const std::string& p, Shader *s) : Surface(s)
{
uri_ = uri;
path_ = p;
mediaplayer_ = new MediaPlayer;
}
@@ -128,7 +128,7 @@ void MediaSurface::init()
{
Surface::init();
mediaplayer_->open(uri_);
mediaplayer_->open(path_);
mediaplayer_->play(true);
}

View File

@@ -67,7 +67,7 @@ protected:
class MediaSurface : public Surface {
public:
MediaSurface(const std::string& uri, Shader *s = new ImageShader);
MediaSurface(const std::string& p, Shader *s = new ImageShader);
~MediaSurface();
void init () override;
@@ -75,11 +75,11 @@ public:
void accept (Visitor& v) override;
void update (float dt) override;
inline std::string uri() const { return uri_; }
inline std::string path() const { return path_; }
inline MediaPlayer *mediaPlayer() const { return mediaplayer_; }
protected:
std::string uri_;
std::string path_;
MediaPlayer *mediaplayer_;
};

View File

@@ -205,7 +205,7 @@ void SessionCreator::visit (MediaSource& s)
XMLElement* uriNode = xmlCurrent_->FirstChildElement("uri");
if (uriNode) {
std::string uri = std::string ( uriNode->GetText() );
s.setURI(uri);
s.setPath(uri);
}
// set config media player

View File

@@ -328,7 +328,7 @@ void SessionVisitor::visit (MediaSource& s)
XMLElement *uri = xmlDoc_->NewElement("uri");
xmlCurrent_->InsertEndChild(uri);
XMLText *text = xmlDoc_->NewText( s.uri().c_str() );
XMLText *text = xmlDoc_->NewText( s.path().c_str() );
uri->InsertEndChild( text );
s.mediaplayer()->accept(*this);

View File

@@ -107,6 +107,7 @@ void Settings::Save()
recent->InsertEndChild(recentsession);
XMLElement *recentmedia = xmlDoc.NewElement( "Media" );
recentmedia->SetAttribute("path", application.recentMedia.path.c_str());
for(auto it = application.recentMedia.filenames.begin();
it != application.recentMedia.filenames.end(); it++) {
@@ -240,6 +241,11 @@ void Settings::Load()
XMLElement * pMedia = pElement->FirstChildElement("Media");
if (pMedia)
{
const char *path_ = pMedia->Attribute("path");
if (path_)
application.recentMedia.path = std::string(path_);
else
application.recentMedia.path = SystemToolkit::home_path();
application.recentMedia.filenames.clear();
XMLElement* path = pMedia->FirstChildElement("path");
for( ; path ; path = path->NextSiblingElement())

View File

@@ -95,7 +95,7 @@ bool hasNode::operator()(const Source* elem) const
return false;
}
MediaSource::MediaSource(const std::string &name) : Source(name), uri_("")
MediaSource::MediaSource(const std::string &name) : Source(name), path_("")
{
// create media player
mediaplayer_ = new MediaPlayer;
@@ -125,16 +125,16 @@ MediaSource::~MediaSource()
// TODO verify that all surfaces and node is deleted in Source destructor
}
void MediaSource::setURI(const std::string &uri)
void MediaSource::setPath(const std::string &p)
{
uri_ = uri;
mediaplayer_->open(uri_);
path_ = p;
mediaplayer_->open(path_);
mediaplayer_->play(true);
}
std::string MediaSource::uri() const
std::string MediaSource::path() const
{
return uri_;
return path_;
}
MediaPlayer *MediaSource::mediaplayer() const

View File

@@ -117,8 +117,8 @@ public:
void accept (Visitor& v);
// Media specific interface
void setURI(const std::string &uri);
std::string uri() const;
void setPath(const std::string &p);
std::string path() const;
MediaPlayer *mediaplayer() const;
protected:
@@ -126,7 +126,7 @@ protected:
void init();
Surface *mediasurface_;
std::string uri_;
std::string path_;
MediaPlayer *mediaplayer_;
};

View File

@@ -56,9 +56,9 @@ string SystemToolkit::date_time_string()
return oss.str();
}
std::string SystemToolkit::base_filename(const std::string& uri)
std::string SystemToolkit::base_filename(const std::string& filename)
{
std::string basefilename = uri.substr(uri.find_last_of(PATH_SEP) + 1);
std::string basefilename = filename.substr(filename.find_last_of(PATH_SEP) + 1);
const size_t period_idx = basefilename.rfind('.');
if (std::string::npos != period_idx)
{
@@ -67,13 +67,26 @@ std::string SystemToolkit::base_filename(const std::string& uri)
return basefilename;
}
std::string SystemToolkit::path_filename(const std::string& uri)
std::string SystemToolkit::path_filename(const std::string& filename)
{
std::string path = uri.substr(0, uri.find_last_of(PATH_SEP) + 1);
std::string path = filename.substr(0, filename.find_last_of(PATH_SEP) + 1);
return path;
}
// TODO : for windows path C:\gstreamer\1.0\x86\bin\pima.mp3 <=> file:///C:/gstreamer/1.0/x86/bin/pima.mp3
std::string SystemToolkit::path_to_uri(const std::string& path)
{
std::string uri = "file://" + path;
return uri;
}
std::string SystemToolkit::uri_to_path(const std::string& uri)
{
// prefix "file://" of lenght 7
std::string path = uri.substr(7);
return path;
}
std::string SystemToolkit::home_path()
{

View File

@@ -25,10 +25,14 @@ namespace SystemToolkit
std::string settings_prepend_path(const std::string& basefilename);
// extract the base file name from a full URI (e.g. file:://home/me/toto.mpg -> toto)
std::string base_filename(const std::string& uri);
std::string base_filename(const std::string& filename);
// extract the path of a file from a full URI (e.g. file:://home/me/toto.mpg -> file:://home/me/)
std::string path_filename(const std::string& uri);
std::string path_filename(const std::string& filename);
std::string path_to_uri(const std::string& path);
std::string uri_to_path(const std::string& uri);
// true of file exists
bool file_exists(const std::string& path);

View File

@@ -104,7 +104,25 @@ static void FileDialogSave(std::string path)
FileDialogSaveFinished_ = true;
}
static std::atomic<bool> MediaDialogFinished_ = false;
static std::string MediaDialogUri_ = "";
static void MediaDialogOpen(std::string path)
{
FileDialogPending_ = true;
MediaDialogFinished_ = false;
char const * open_file_name;
char const * open_pattern[1] = { "*.mp4" };
open_file_name = tinyfd_openFileDialog( "Open a Media file", path.c_str(), 1, open_pattern, nullptr, 0);
if (!open_file_name)
MediaDialogUri_ = "";
else
MediaDialogUri_ = std::string( open_file_name );
MediaDialogFinished_ = true;
}
UserInterface::UserInterface()
{
@@ -352,6 +370,14 @@ void UserInterface::NewFrame()
Settings::application.recentSessions.path = SystemToolkit::path_filename(FileDialogFilename_);
}
}
if (MediaDialogFinished_) {
MediaDialogFinished_ = false;
FileDialogPending_ = false;
if (!MediaDialogUri_.empty()) {
navigator.setMediaUri(MediaDialogUri_);
Settings::application.recentMedia.path = SystemToolkit::path_filename(MediaDialogUri_);
}
}
// overlay when disabled
if (FileDialogPending_){
@@ -439,7 +465,7 @@ void UserInterface::showMenuFile()
navigator.hidePannel();
}
if (ImGui::MenuItem( ICON_FA_FOLDER_OPEN " Save as")) {
std::thread (FileDialogSave, "./").detach();
std::thread (FileDialogSave, Settings::application.recentSessions.path).detach();
navigator.hidePannel();
}
@@ -982,6 +1008,12 @@ void Navigator::RenderSourcePannel(Source *s)
ImGui::End();
}
void Navigator::setMediaUri(std::string path)
{
// std::string uri = SystemToolkit::path_to_uri(path);
sprintf(uri_, "%s", path.c_str());
}
void Navigator::RenderNewPannel()
{
// Next window is a side pannel
@@ -999,21 +1031,25 @@ void Navigator::RenderNewPannel()
static int new_source_type = 0;
ImGui::Combo("Type", &new_source_type, "Media\0Render\0Clone\0");
if (new_source_type == 0) {
static char filename[128] = "file:///home/bhbn/Videos/iss.mov";
// browse folder
if (ImGuiToolkit::ButtonIcon(2, 5)) {
// browse file
Log::Info("Settings::application.recentMedia.path %s", Settings::application.recentMedia.path.c_str());
std::thread (MediaDialogOpen, Settings::application.recentMedia.path).detach();
}
// uri text entry
ImGui::SameLine(0, 10);
ImGui::SetNextItemWidth(IMGUI_RIGHT_ALIGN);
ImGui::InputText("Filename", filename, 64, ImGuiInputTextFlags_CharsNoBlank);
if (ImGui::InputText("Uri", uri_, IM_ARRAYSIZE(uri_), ImGuiInputTextFlags_EnterReturnsTrue) ) {
Mixer::manager().createSourceMedia(uri_);
selected_button[NAV_NEW] = false;
}
// Description
ImGuiToolkit::HelpMarker("A Media source displays an image or a video file.");
// Validate button
ImGui::Text(" ");
if ( ImGui::Button("Create !", ImVec2(5.f * width - padding_width, 0)) ) {
Mixer::manager().createSourceMedia(filename);
Mixer::manager().createSourceMedia(uri_);
selected_button[NAV_NEW] = false;
}
}
@@ -1030,6 +1066,7 @@ void Navigator::RenderNewPannel()
ImGui::End();
}
void Navigator::RenderMainPannel()
{
// Next window is a side pannel

View File

@@ -32,12 +32,16 @@ class Navigator
void RenderNewPannel();
void RenderMainPannel();
char uri_[1024];
public:
Navigator();
void hidePannel();
void showPannelSource(int index);
void Render();
void setMediaUri(std::string path);
};
class ToolBox