From 096224ffc2cb67bcaefad6315cee83ac37b7cbbf Mon Sep 17 00:00:00 2001 From: Deb Mukherjee Date: Mon, 14 Jul 2014 13:31:29 -0700 Subject: [PATCH] Use custom mkstemp() to fix Win issue in y4m_test 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 | 67 +++++++++++++++++++++++++++++++++++++++++++-- test/y4m_test.cc | 23 ++++++++-------- y4menc.c | 1 + 3 files changed, 77 insertions(+), 14 deletions(-) diff --git a/test/video_source.h b/test/video_source.h index 94500b43d..78e7d466b 100644 --- a/test/video_source.h +++ b/test/video_source.h @@ -10,6 +10,9 @@ #ifndef TEST_VIDEO_SOURCE_H_ #define TEST_VIDEO_SOURCE_H_ +#if defined(_WIN32) +#include +#endif #include #include #include @@ -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 { diff --git a/test/y4m_test.cc b/test/y4m_test.cc index 73ff68308..3927b7da4 100644 --- a/test/y4m_test.cc +++ b/test/y4m_test.cc @@ -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) { diff --git a/y4menc.c b/y4menc.c index 9211452a4..b647e8dcc 100644 --- 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,