Specific MediaPlayer error message for opening non-existing file

This commit is contained in:
Bruno Herbelin
2023-08-17 17:50:59 +02:00
parent 3a7da2bc98
commit aaefa356ae

View File

@@ -116,7 +116,7 @@ guint MediaPlayer::texture() const
MediaInfo MediaPlayer::UriDiscoverer(const std::string &uri)
{
#ifdef MEDIA_PLAYER_DEBUG
Log::Info("Checking file '%s'", uri.c_str());
Log::Info("Checking uri '%s'", uri.c_str());
#endif
#ifdef LIMIT_DISCOVERER
@@ -132,119 +132,130 @@ MediaInfo MediaPlayer::UriDiscoverer(const std::string &uri)
mtx_secondary.lock(); // blocking
}
#endif
MediaInfo video_stream_info;
GError *err = NULL;
GstDiscoverer *discoverer = gst_discoverer_new (DISCOVER_TIMOUT * GST_SECOND, &err);
/* Instantiate the Discoverer */
if (!discoverer) {
Log::Warning("MediaPlayer Error creating discoverer instance: %s\n", err->message);
// returned value
MediaInfo video_stream_info;
// if uri is valid
if ( SystemToolkit::file_exists( gst_uri_get_location(uri.c_str()) ) ) {
GError *err = NULL;
GstDiscoverer *discoverer = gst_discoverer_new (DISCOVER_TIMOUT * GST_SECOND, &err);
/* Instantiate the Discoverer */
if (!discoverer) {
Log::Warning("MediaPlayer Error creating discoverer instance: %s\n", err->message);
}
else {
GstDiscovererInfo *info = NULL;
info = gst_discoverer_discover_uri (discoverer, uri.c_str(), &err);
GstDiscovererResult result = gst_discoverer_info_get_result (info);
switch (result) {
case GST_DISCOVERER_URI_INVALID:
video_stream_info.log = "Invalid URI";
break;
case GST_DISCOVERER_ERROR:
video_stream_info.log = std::string( "Warning: " ) + err->message;
result = GST_DISCOVERER_OK; // try to read the file anyways, discoverer can report errors but still read the file
break;
case GST_DISCOVERER_TIMEOUT:
video_stream_info.log = "Timeout loading";
break;
case GST_DISCOVERER_BUSY:
video_stream_info.log = "Busy";
break;
case GST_DISCOVERER_MISSING_PLUGINS:
{
const GstStructure *s = gst_discoverer_info_get_misc (info);
gchar *str = gst_structure_to_string (s);
video_stream_info.log = std::string( "Warning: " ) + std::string(str);
g_free (str);
result = GST_DISCOVERER_OK; // try to read the file anyways, discoverer can report errors but still read the file
}
break;
default:
break;
}
if ( result == GST_DISCOVERER_OK ) {
// get videos in information found
GList *streams = gst_discoverer_info_get_video_streams(info);
if ( g_list_length(streams) > 0) {
GList *tmp;
for (tmp = streams; tmp && !video_stream_info.valid; tmp = tmp->next ) {
GstDiscovererStreamInfo *tmpinf = (GstDiscovererStreamInfo *) tmp->data;
if ( GST_IS_DISCOVERER_VIDEO_INFO(tmpinf) )
{
// found a video / image stream : fill-in information
GstDiscovererVideoInfo* vinfo = GST_DISCOVERER_VIDEO_INFO(tmpinf);
video_stream_info.width = gst_discoverer_video_info_get_width(vinfo);
video_stream_info.height = gst_discoverer_video_info_get_height(vinfo);
guint parn = gst_discoverer_video_info_get_par_num(vinfo);
guint pard = gst_discoverer_video_info_get_par_denom(vinfo);
video_stream_info.par_width = (video_stream_info.width * parn) / pard;
video_stream_info.interlaced = gst_discoverer_video_info_is_interlaced(vinfo);
video_stream_info.bitrate = gst_discoverer_video_info_get_bitrate(vinfo);
video_stream_info.isimage = gst_discoverer_video_info_is_image(vinfo);
// if its a video, set duration, framerate, etc.
if ( !video_stream_info.isimage ) {
video_stream_info.end = gst_discoverer_info_get_duration (info) ;
video_stream_info.seekable = gst_discoverer_info_get_seekable (info);
video_stream_info.framerate_n = gst_discoverer_video_info_get_framerate_num(vinfo);
video_stream_info.framerate_d = gst_discoverer_video_info_get_framerate_denom(vinfo);
if (video_stream_info.framerate_n == 0 || video_stream_info.framerate_d == 0) {
Log::Info("'%s': No framerate indicated in the file; using default 30fps", uri.c_str());
video_stream_info.framerate_n = 30;
video_stream_info.framerate_d = 1;
}
video_stream_info.dt = ( (GST_SECOND * static_cast<guint64>(video_stream_info.framerate_d)) / (static_cast<guint64>(video_stream_info.framerate_n)) );
// confirm (or infirm) that its not a single frame
if ( video_stream_info.end < video_stream_info.dt * 2)
video_stream_info.isimage = true;
}
// try to fill-in the codec information
GstCaps *caps = gst_discoverer_stream_info_get_caps (tmpinf);
if (caps) {
gchar *codecstring = gst_pb_utils_get_codec_description(caps);
video_stream_info.codec_name = std::string( codecstring );
g_free(codecstring);
gst_caps_unref (caps);
}
const GstTagList *tags = gst_discoverer_stream_info_get_tags(tmpinf);
if ( tags ) {
gchar *container = NULL;
if ( gst_tag_list_get_string (tags, GST_TAG_CONTAINER_FORMAT, &container) )
video_stream_info.codec_name += ", " + std::string(container);
if (container)
g_free(container);
}
// exit loop
// inform that it succeeded
video_stream_info.valid = true;
}
}
if (!video_stream_info.valid)
video_stream_info.log = "Invalid video stream";
}
else
video_stream_info.log = "No video stream";
gst_discoverer_stream_info_list_free(streams);
}
if (info)
gst_discoverer_info_unref (info);
g_object_unref( discoverer );
}
g_clear_error (&err);
}
else {
GstDiscovererInfo *info = NULL;
info = gst_discoverer_discover_uri (discoverer, uri.c_str(), &err);
GstDiscovererResult result = gst_discoverer_info_get_result (info);
switch (result) {
case GST_DISCOVERER_URI_INVALID:
video_stream_info.log = "Invalid URI";
break;
case GST_DISCOVERER_ERROR:
video_stream_info.log = std::string( "Warning: " ) + err->message;
result = GST_DISCOVERER_OK; // try to read the file anyways, discoverer can report errors but still read the file
break;
case GST_DISCOVERER_TIMEOUT:
video_stream_info.log = "Timeout loading";
break;
case GST_DISCOVERER_BUSY:
video_stream_info.log = "Busy";
break;
case GST_DISCOVERER_MISSING_PLUGINS:
{
const GstStructure *s = gst_discoverer_info_get_misc (info);
gchar *str = gst_structure_to_string (s);
video_stream_info.log = std::string( "Warning: " ) + std::string(str);
g_free (str);
result = GST_DISCOVERER_OK; // try to read the file anyways, discoverer can report errors but still read the file
}
break;
default:
break;
}
if ( result == GST_DISCOVERER_OK ) {
// get videos in information found
GList *streams = gst_discoverer_info_get_video_streams(info);
if ( g_list_length(streams) > 0) {
GList *tmp;
for (tmp = streams; tmp && !video_stream_info.valid; tmp = tmp->next ) {
GstDiscovererStreamInfo *tmpinf = (GstDiscovererStreamInfo *) tmp->data;
if ( GST_IS_DISCOVERER_VIDEO_INFO(tmpinf) )
{
// found a video / image stream : fill-in information
GstDiscovererVideoInfo* vinfo = GST_DISCOVERER_VIDEO_INFO(tmpinf);
video_stream_info.width = gst_discoverer_video_info_get_width(vinfo);
video_stream_info.height = gst_discoverer_video_info_get_height(vinfo);
guint parn = gst_discoverer_video_info_get_par_num(vinfo);
guint pard = gst_discoverer_video_info_get_par_denom(vinfo);
video_stream_info.par_width = (video_stream_info.width * parn) / pard;
video_stream_info.interlaced = gst_discoverer_video_info_is_interlaced(vinfo);
video_stream_info.bitrate = gst_discoverer_video_info_get_bitrate(vinfo);
video_stream_info.isimage = gst_discoverer_video_info_is_image(vinfo);
// if its a video, set duration, framerate, etc.
if ( !video_stream_info.isimage ) {
video_stream_info.end = gst_discoverer_info_get_duration (info) ;
video_stream_info.seekable = gst_discoverer_info_get_seekable (info);
video_stream_info.framerate_n = gst_discoverer_video_info_get_framerate_num(vinfo);
video_stream_info.framerate_d = gst_discoverer_video_info_get_framerate_denom(vinfo);
if (video_stream_info.framerate_n == 0 || video_stream_info.framerate_d == 0) {
Log::Info("'%s': No framerate indicated in the file; using default 30fps", uri.c_str());
video_stream_info.framerate_n = 30;
video_stream_info.framerate_d = 1;
}
video_stream_info.dt = ( (GST_SECOND * static_cast<guint64>(video_stream_info.framerate_d)) / (static_cast<guint64>(video_stream_info.framerate_n)) );
// confirm (or infirm) that its not a single frame
if ( video_stream_info.end < video_stream_info.dt * 2)
video_stream_info.isimage = true;
}
// try to fill-in the codec information
GstCaps *caps = gst_discoverer_stream_info_get_caps (tmpinf);
if (caps) {
gchar *codecstring = gst_pb_utils_get_codec_description(caps);
video_stream_info.codec_name = std::string( codecstring );
g_free(codecstring);
gst_caps_unref (caps);
}
const GstTagList *tags = gst_discoverer_stream_info_get_tags(tmpinf);
if ( tags ) {
gchar *container = NULL;
if ( gst_tag_list_get_string (tags, GST_TAG_CONTAINER_FORMAT, &container) )
video_stream_info.codec_name += ", " + std::string(container);
if (container)
g_free(container);
}
// exit loop
// inform that it succeeded
video_stream_info.valid = true;
}
}
if (!video_stream_info.valid)
video_stream_info.log = "Invalid video stream";
}
else
video_stream_info.log = "No video stream";
gst_discoverer_stream_info_list_free(streams);
}
if (info)
gst_discoverer_info_unref (info);
g_object_unref( discoverer );
video_stream_info.log = "No such file";
}
g_clear_error (&err);
#ifdef LIMIT_DISCOVERER
if (use_primary)
mtx_primary.unlock();