Bug 1425219 - Stop using GetNativePath in ffvpx. r?jya draft
authorMasatoshi Kimura <VYV03354@nifty.ne.jp>
Fri, 15 Dec 2017 23:16:39 +0900
changeset 756972 f1527bf328333771743918d2198987f65241b094
parent 756802 1ee83391c94900eee1630951d23469f93ddcef0a
push id99611
push userVYV03354@nifty.ne.jp
push dateMon, 19 Feb 2018 14:35:17 +0000
reviewersjya
bugs1425219
milestone60.0a1
Bug 1425219 - Stop using GetNativePath in ffvpx. r?jya FFVPXRuntimeLinker could not handle file paths that contain characters outside the current system code page on Windows. This patch will fix it by using wide char APIs. MozReview-Commit-ID: 9ES1xFELjDs
dom/media/platforms/ffmpeg/ffvpx/FFVPXRuntimeLinker.cpp
--- a/dom/media/platforms/ffmpeg/ffvpx/FFVPXRuntimeLinker.cpp
+++ b/dom/media/platforms/ffmpeg/ffvpx/FFVPXRuntimeLinker.cpp
@@ -2,19 +2,23 @@
 /* vim:set ts=2 sw=2 sts=2 et cindent: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "FFVPXRuntimeLinker.h"
 #include "FFmpegLibWrapper.h"
 #include "FFmpegLog.h"
-#include "nsIFile.h"
+#include "mozilla/FileUtils.h"
+#include "nsLocalFile.h"
 #include "prmem.h"
 #include "prlink.h"
+#ifdef XP_WIN
+#include <windows.h>
+#endif
 
 // We use a known symbol located in lgpllibs to determine its location.
 // soundtouch happens to be always included in lgpllibs
 #include "soundtouch/SoundTouch.h"
 
 namespace mozilla {
 
 template <int V> class FFmpegDecoderModule
@@ -24,83 +28,86 @@ public:
 };
 
 static FFmpegLibWrapper sFFVPXLib;
 
 FFVPXRuntimeLinker::LinkStatus FFVPXRuntimeLinker::sLinkStatus =
   LinkStatus_INIT;
 
 static PRLibrary*
-MozAVLink(const char* aName)
+MozAVLink(nsIFile* aFile)
 {
   PRLibSpec lspec;
+  PathString path = aFile->NativePath();
+#ifdef XP_WIN
+  lspec.type = PR_LibSpec_PathnameU;
+  lspec.value.pathname_u = path.get();
+#else
   lspec.type = PR_LibSpec_Pathname;
-  lspec.value.pathname = aName;
+  lspec.value.pathname = path.get();
+#endif
 #ifdef MOZ_WIDGET_ANDROID
   PRLibrary* lib = PR_LoadLibraryWithFlags(lspec, PR_LD_NOW | PR_LD_GLOBAL);
 #else
   PRLibrary* lib = PR_LoadLibraryWithFlags(lspec, PR_LD_NOW | PR_LD_LOCAL);
 #endif
   if (!lib) {
-    FFMPEG_LOG("unable to load library %s", aName);
+    FFMPEG_LOG("unable to load library %s", aFile->HumanReadablePath().get());
   }
   return lib;
 }
 
 /* static */ bool
 FFVPXRuntimeLinker::Init()
 {
   if (sLinkStatus) {
     return sLinkStatus == LinkStatus_SUCCEEDED;
   }
 
   sLinkStatus = LinkStatus_FAILED;
 
   // We retrieve the path of the lgpllibs library as this is where mozavcodec
   // and mozavutil libs are located.
-  char* lgpllibsname = PR_GetLibraryName(nullptr, "lgpllibs");
-  if (!lgpllibsname) {
+  PathString lgpllibsname = GetLibraryName(nullptr, "lgpllibs");
+  if (lgpllibsname.IsEmpty()) {
     return false;
   }
-  char* path =
-    PR_GetLibraryFilePathname(lgpllibsname,
-                              (PRFuncPtr)&soundtouch::SoundTouch::getVersionId);
-  PR_FreeLibraryName(lgpllibsname);
-  if (!path) {
+  PathString path =
+    GetLibraryFilePathname(lgpllibsname.get(),
+                           (PRFuncPtr)&soundtouch::SoundTouch::getVersionId);
+  if (path.IsEmpty()) {
     return false;
   }
-  nsCOMPtr<nsIFile> xulFile = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID);
-  if (!xulFile ||
-      NS_FAILED(xulFile->InitWithNativePath(nsDependentCString(path)))) {
-    PR_Free(path); // PR_GetLibraryFilePathname() uses PR_Malloc().
+  RefPtr<nsLocalFile> xulFile = new nsLocalFile(path);
+  if (xulFile->NativePath().IsEmpty()) {
     return false;
   }
-  PR_Free(path); // PR_GetLibraryFilePathname() uses PR_Malloc().
 
   nsCOMPtr<nsIFile> rootDir;
   if (NS_FAILED(xulFile->GetParent(getter_AddRefs(rootDir))) || !rootDir) {
     return false;
   }
-  nsAutoCString rootPath;
-  if (NS_FAILED(rootDir->GetNativePath(rootPath))) {
+  PathString rootPath = rootDir->NativePath();
+
+  /* Get the platform-dependent library name of the module */
+  PathString libname = GetLibraryName(rootPath.get(), "mozavutil");
+  if (libname.IsEmpty()) {
     return false;
   }
-
-  char* libname = NULL;
-  /* Get the platform-dependent library name of the module */
-  libname = PR_GetLibraryName(rootPath.get(), "mozavutil");
-  if (!libname) {
+  RefPtr<nsLocalFile> libFile = new nsLocalFile(libname);
+  if (libFile->NativePath().IsEmpty()) {
     return false;
   }
-  sFFVPXLib.mAVUtilLib = MozAVLink(libname);
-  PR_FreeLibraryName(libname);
-  libname = PR_GetLibraryName(rootPath.get(), "mozavcodec");
-  if (libname) {
-    sFFVPXLib.mAVCodecLib = MozAVLink(libname);
-    PR_FreeLibraryName(libname);
+  sFFVPXLib.mAVUtilLib = MozAVLink(libFile);
+  libname = GetLibraryName(rootPath.get(), "mozavcodec");
+  if (!libname.IsEmpty()) {
+    libFile = new nsLocalFile(libname);
+    if (!libFile->NativePath().IsEmpty()) {
+      sFFVPXLib.mAVCodecLib = MozAVLink(libFile);
+    }
   }
   if (sFFVPXLib.Link() == FFmpegLibWrapper::LinkResult::Success) {
     sLinkStatus = LinkStatus_SUCCEEDED;
     return true;
   }
   return false;
 }