]> git.sesse.net Git - nageru/blobdiff - image_input.cpp
Support audio-only FFmpeg inputs. Somewhat wonky, though.
[nageru] / image_input.cpp
index a224af9d47e1d8fc5a0f16ac702f5ce66cbdd7d5..2bf4a237bb86758f92cbdcfba808c514c7c19b82 100644 (file)
@@ -1,6 +1,7 @@
 #include "image_input.h"
 
 #include <errno.h>
+#include <movit/flat_input.h>
 #include <movit/image_format.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -30,49 +31,18 @@ extern "C" {
 #include <vector>
 
 #include "ffmpeg_raii.h"
+#include "ffmpeg_util.h"
 #include "flags.h"
-#include "flat_input.h"
 
 struct SwsContext;
 
 using namespace std;
 
-namespace {
-
-string search_for_file(const string &filename)
-{
-       // Look for the file in all theme_dirs until we find one;
-       // that will be the permanent resolution of this file, whether
-       // it is actually valid or not.
-       // We store errors from all the attempts, and show them
-       // once we know we can't find any of them.
-       vector<string> errors;
-       for (const string &dir : global_flags.theme_dirs) {
-               string pathname = dir + "/" + filename;
-               if (access(pathname.c_str(), O_RDONLY) == 0) {
-                       return pathname;
-               } else {
-                       char buf[512];
-                       snprintf(buf, sizeof(buf), "%s: %s", pathname.c_str(), strerror(errno));
-                       errors.push_back(buf);
-               }
-       }
-
-       for (const string &error : errors) {
-               fprintf(stderr, "%s\n", error.c_str());
-       }
-       fprintf(stderr, "Couldn't find %s in any directory in --theme-dirs, exiting.\n",
-               filename.c_str());
-       exit(1);
-}
-
-}  // namespace
-
 ImageInput::ImageInput(const string &filename)
        : movit::FlatInput({movit::COLORSPACE_sRGB, movit::GAMMA_sRGB}, movit::FORMAT_RGBA_POSTMULTIPLIED_ALPHA,
                           GL_UNSIGNED_BYTE, 1280, 720),  // Resolution will be overwritten.
          filename(filename),
-         pathname(search_for_file(filename)),
+         pathname(search_for_file_or_die(filename)),
          current_image(load_image(filename, pathname))
 {
        if (current_image == nullptr) {  // Could happen even though search_for_file() returned.
@@ -140,13 +110,7 @@ shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &pat
                return nullptr;
        }
 
-       int stream_index = -1;
-       for (unsigned i = 0; i < format_ctx->nb_streams; ++i) {
-               if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
-                       stream_index = i;
-                       break;
-               }
-       }
+       int stream_index = find_stream_index(format_ctx.get(), AVMEDIA_TYPE_VIDEO);
        if (stream_index == -1) {
                fprintf(stderr, "%s: No video stream found\n", pathname.c_str());
                return nullptr;
@@ -246,7 +210,10 @@ void ImageInput::update_thread_func(const std::string &filename, const std::stri
        timespec last_modified = first_modified;
        struct stat buf;
        for ( ;; ) {
-               sleep(1);
+               {
+                       unique_lock<mutex> lock(threads_should_quit_mu);
+                       threads_should_quit_modified.wait_for(lock, chrono::seconds(1), []() { return threads_should_quit; });
+               }
 
                if (threads_should_quit) {
                        return;
@@ -275,9 +242,11 @@ void ImageInput::update_thread_func(const std::string &filename, const std::stri
 
 void ImageInput::shutdown_updaters()
 {
-       // TODO: Kick these out of the sleep before one second?
-       threads_should_quit = true;
-
+       {
+               unique_lock<mutex> lock(threads_should_quit_mu);
+               threads_should_quit = true;
+               threads_should_quit_modified.notify_all();
+       }
        for (auto &it : update_threads) {
                it.second.join();
        }
@@ -286,4 +255,6 @@ void ImageInput::shutdown_updaters()
 mutex ImageInput::all_images_lock;
 map<string, shared_ptr<const ImageInput::Image>> ImageInput::all_images;
 map<string, thread> ImageInput::update_threads;
-volatile bool ImageInput::threads_should_quit = false;
+mutex ImageInput::threads_should_quit_mu;
+bool ImageInput::threads_should_quit = false;
+condition_variable ImageInput::threads_should_quit_modified;