Use custom mkstemp() to fix Win issue in y4m_test
authorDeb Mukherjee <debargha@google.com>
Mon, 14 Jul 2014 20:31:29 +0000 (13:31 -0700)
committerDeb Mukherjee <debargha@google.com>
Fri, 18 Jul 2014 22:35:17 +0000 (15:35 -0700)
Uses mkstmp() with directory being the same as the test data
directory to create temporary output file. For Windows
GetTempFileNameA() function is used.

Change-Id: Ie4681b2b4f44f8c22d3b3faf134c44087b484f94

test/video_source.h
test/y4m_test.cc
y4menc.c

index 94500b4..78e7d46 100644 (file)
@@ -10,6 +10,9 @@
 #ifndef TEST_VIDEO_SOURCE_H_
 #define TEST_VIDEO_SOURCE_H_
 
+#if defined(_WIN32)
+#include <windows.h>
+#endif
 #include <cstdio>
 #include <cstdlib>
 #include <string>
@@ -55,10 +58,70 @@ static FILE *OpenTestOutFile(const std::string& file_name) {
   return fopen(path_to_source.c_str(), "wb");
 }
 
-static FILE *OpenTempOutFile() {
-  return tmpfile();
+static std::string GetTempOutFilename() {
+  std::string basename;
+#if defined(_WIN32)
+  char fname[MAX_PATH];
+  // Assume for now that the filename generated is unique per process
+  const UINT ret = GetTempFileNameA(
+      GetDataPath().c_str(), "lvx", 0, fname);
+  if (ret != 0) {
+    const char *slash = strrchr(fname, '\\');
+    if (slash == NULL) slash = strrchr(fname, '/');
+    if (slash == NULL)
+      basename.assign(fname);
+    else
+      basename.assign(slash + 1);
+  } else {
+    basename.clear();
+  }
+#else
+  char fname[256];
+  const std::string templ = GetDataPath() + "/libvpx_test_XXXXXX";
+  strncpy(fname, templ.c_str(), templ.size());
+  fname[templ.size()] = '\0';
+  const int fd = mkstemp(fname);
+  if (fd != -1) {
+    close(fd);
+    basename.assign(strrchr(fname, '/') + 1);
+  } else {
+    basename.clear();
+  }
+#endif
+  return basename;
 }
 
+class TempOutFile {
+ public:
+  TempOutFile() {
+    file_name_ = GetTempOutFilename();
+    file_ = OpenTestOutFile(file_name_);
+  }
+  ~TempOutFile() {
+    CloseFile();
+    if (!file_name_.empty()) {
+      const std::string path_to_source = GetDataPath() + "/" + file_name_;
+      EXPECT_EQ(0, remove(path_to_source.c_str()));
+    }
+  }
+  FILE *file() {
+    return file_;
+  }
+  const std::string& file_name() {
+    return file_name_;
+  }
+  void CloseFile() {
+    if (file_) {
+      fclose(file_);
+      file_ = NULL;
+    }
+  }
+
+ protected:
+  FILE *file_;
+  std::string file_name_;
+};
+
 // Abstract base class for test video sources, which provide a stream of
 // vpx_image_t images with associated timestamps and duration.
 class VideoSource {
index 73ff683..3927b7d 100644 (file)
@@ -138,14 +138,11 @@ INSTANTIATE_TEST_CASE_P(C, Y4mVideoSourceTest,
 class Y4mVideoWriteTest
     : public Y4mVideoSourceTest {
  protected:
-  Y4mVideoWriteTest() : Y4mVideoSourceTest() {}
+  Y4mVideoWriteTest() {}
 
-  virtual void ReplaceInputFp(FILE *input_file) {
+  virtual ~Y4mVideoWriteTest() {
     CloseSource();
-    frame_ = 0;
-    input_file_ = input_file;
-    rewind(input_file_);
-    ReadSourceToStart();
+    delete tmpfile_;
   }
 
   // Writes out a y4m file and then reads it back
@@ -153,26 +150,28 @@ class Y4mVideoWriteTest
     ASSERT_TRUE(input_file_ != NULL);
     char buf[Y4M_BUFFER_SIZE] = {0};
     const struct VpxRational framerate = {y4m_.fps_n, y4m_.fps_d};
-    FILE *out_file = libvpx_test::OpenTempOutFile();
-    ASSERT_TRUE(out_file != NULL);
+    tmpfile_ = new libvpx_test::TempOutFile;
+    ASSERT_TRUE(tmpfile_->file() != NULL);
     y4m_write_file_header(buf, sizeof(buf),
                           kWidth, kHeight,
                           &framerate, y4m_.vpx_fmt,
                           y4m_.bit_depth);
-    fputs(buf, out_file);
+    fputs(buf, tmpfile_->file());
     for (unsigned int i = start_; i < limit_; i++) {
       y4m_write_frame_header(buf, sizeof(buf));
-      fputs(buf, out_file);
-      write_image_file(img(), out_file);
+      fputs(buf, tmpfile_->file());
+      write_image_file(img(), tmpfile_->file());
       Next();
     }
-    ReplaceInputFp(out_file);
+    tmpfile_->CloseFile();
+    Y4mVideoSourceTest::Init(tmpfile_->file_name(), limit_);
   }
 
   virtual void Init(const std::string &file_name, int limit) {
     Y4mVideoSourceTest::Init(file_name, limit);
     WriteY4mAndReadBack();
   }
+  libvpx_test::TempOutFile *tmpfile_;
 };
 
 TEST_P(Y4mVideoWriteTest, WriteTest) {
index 9211452..b647e8d 100644 (file)
--- a/y4menc.c
+++ b/y4menc.c
@@ -48,6 +48,7 @@ int y4m_write_file_header(char *buf, size_t len, int width, int height,
               "C420p16 XYSCSS=420P16\n";
       break;
     default:
+      color = NULL;
       assert(0);
   }
   return snprintf(buf, len, "YUV4MPEG2 W%u H%u F%u:%u I%c %s", width, height,