summaryrefslogtreecommitdiff
path: root/comphelper
diff options
context:
space:
mode:
authorMichael Stahl <michael.stahl@allotropia.de>2023-04-05 11:50:44 +0200
committerMichael Stahl <michael.stahl@allotropia.de>2023-04-05 14:34:51 +0200
commita5225ba82e94a549f44420f56a7cb9d7906561cc (patch)
tree084be80b91028e3bfbe8cd5e3b50ba97fa8b516d /comphelper
parent8b4af1eb0549b6832361da85ddaa1e13be34ec76 (diff)
avmedia,*: guess the mime type of media files based on file name
.. at least for the most popular types, and do it automatically in MediaItem::setURL(). This should work in practice in most cases and is much simpler than adding some type detection or calling into platform dependent avmedia backends. Remove the parameter that was only ever set to "application/vnd.sun.star.media" anyway, the same value that would be used if it's missing. Stop using that silly type for everything, only use it when guessing fails. In case an ODF document is loaded, it will use the mime type loaded from the file (see setting of MediaMimeType in SdXMLPluginShapeContext) and not guess it because that would require updating the entry in manifest.xml as well. Change-Id: I8ce29cf7425678ae11dda1d8c875be818f8623af Reviewed-on: https://gerrit.libreoffice.org/c/core/+/150049 Tested-by: Jenkins Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
Diffstat (limited to 'comphelper')
-rw-r--r--comphelper/source/misc/graphicmimetype.cxx68
1 files changed, 68 insertions, 0 deletions
diff --git a/comphelper/source/misc/graphicmimetype.cxx b/comphelper/source/misc/graphicmimetype.cxx
index f9c6034ac8f9..8ae3dad5619c 100644
--- a/comphelper/source/misc/graphicmimetype.cxx
+++ b/comphelper/source/misc/graphicmimetype.cxx
@@ -18,6 +18,10 @@
*/
#include <comphelper/graphicmimetype.hxx>
+#include <comphelper/mediamimetype.hxx>
+
+#include <map>
+#include <set>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
@@ -166,6 +170,70 @@ char const* GraphicMimeTypeHelper::GetExtensionForConvertDataFormat(ConvertDataF
}
return pExt;
}
+
+static auto GetMediaMimes() -> std::map<OString, OString> const&
+{
+ static std::map<OString, OString> const mimes = {
+ { "mp4", "video/mp4" },
+ { "ts", "video/MP2T" },
+ { "mpeg", "video/mpeg" },
+ { "mpg", "video/mpeg" },
+ { "mkv", "video/x-matroska" },
+ { "webm", "video/webm" },
+ { "ogv", "video/ogg" },
+ { "mov", "video/quicktime" },
+ { "wmv", "video/x-ms-wmv" },
+ { "avi", "video/x-msvideo" },
+ { "m4a", "audio/mp4" },
+ { "aac", "audio/aac" },
+ { "mp3", "audio/mpeg" }, // https://bugs.chromium.org/p/chromium/issues/detail?id=227004
+ { "ogg", "audio/ogg" },
+ { "oga", "audio/ogg" },
+ { "opus", "audio/ogg" },
+ { "flac", "audio/flac" }, // missing at IANA?
+ // note there is RFC 2631 but i got the impression that vnd.wave
+ // requires specifying the codec in the container; also this page
+ // says "Historic" whatever that means:
+ // https://www.iana.org/assignments/wave-avi-codec-registry/wave-avi-codec-registry.xhtml
+ { "wav", "audio/x-wav" },
+ };
+ return mimes;
+}
+
+auto IsMediaMimeType(::std::string_view const rMimeType) -> bool
+{
+ return IsMediaMimeType(OStringToOUString(rMimeType, RTL_TEXTENCODING_UTF8));
+}
+
+auto IsMediaMimeType(OUString const& rMimeType) -> bool
+{
+ static std::set<OUString> mimes;
+ if (mimes.empty())
+ {
+ auto const& rMap(GetMediaMimes());
+ for (auto const& it : rMap)
+ {
+ mimes.insert(OStringToOUString(it.second, RTL_TEXTENCODING_UTF8));
+ }
+ }
+ return rMimeType == AVMEDIA_MIMETYPE_COMMON || mimes.find(rMimeType) != mimes.end();
+}
+
+auto GuessMediaMimeType(::std::u16string_view rFileName) -> OUString
+{
+ if (auto const i = rFileName.rfind('.'); i != ::std::string_view::npos)
+ {
+ OString const ext(OUStringToOString(rFileName.substr(i + 1), RTL_TEXTENCODING_UTF8));
+ auto const& rMap(GetMediaMimes());
+ auto const it(rMap.find(ext));
+ if (it != rMap.end())
+ {
+ return OStringToOUString(it->second, RTL_TEXTENCODING_ASCII_US);
+ }
+ }
+ return OUString();
}
+} // namespace comphelper
+
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */