]> git.sesse.net Git - nageru/blob - ffmpeg_util.cpp
Fix an issue where the mixer lagging too much behind CEF would cause us to display...
[nageru] / ffmpeg_util.cpp
1 #include "ffmpeg_util.h"
2
3 #include <ctype.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6
7 #include <string>
8 #include <vector>
9
10 #include "flags.h"
11
12 using namespace std;
13
14 string search_for_file(const string &filename)
15 {
16         if (!filename.empty() && filename[0] == '/') {
17                 // Absolute path.
18                 return filename;
19         }
20
21         // See if we match ^[a-z]:/, which is probably a URL of some sort
22         // (FFmpeg understands various forms of these).
23         for (size_t i = 0; i < filename.size() - 1; ++i) {
24                 if (filename[i] == ':' && filename[i + 1] == '/') {
25                         return filename;
26                 }
27                 if (!isalpha(filename[i])) {
28                         break;
29                 }
30         }
31
32         // Look for the file in all theme_dirs until we find one;
33         // that will be the permanent resolution of this file, whether
34         // it is actually valid or not.
35         // We store errors from all the attempts, and show them
36         // once we know we can't find any of them.
37         vector<string> errors;
38         for (const string &dir : global_flags.theme_dirs) {
39                 string pathname = dir + "/" + filename;
40                 if (access(pathname.c_str(), O_RDONLY) == 0) {
41                         return pathname;
42                 } else {
43                         char buf[512];
44                         snprintf(buf, sizeof(buf), "%s: %s", pathname.c_str(), strerror(errno));
45                         errors.push_back(buf);
46                 }
47         }
48
49         for (const string &error : errors) {
50                 fprintf(stderr, "%s\n", error.c_str());
51         }
52         return "";
53 }
54
55 string search_for_file_or_die(const string &filename)
56 {
57         string pathname = search_for_file(filename);
58         if (pathname.empty()) {
59                 fprintf(stderr, "Couldn't find %s in any directory in --theme-dirs, exiting.\n",
60                         filename.c_str());
61                 exit(1);
62         }
63         return pathname;
64 }
65
66 int find_stream_index(AVFormatContext *ctx, AVMediaType media_type)
67 {
68         for (unsigned i = 0; i < ctx->nb_streams; ++i) {
69                 if (ctx->streams[i]->codecpar->codec_type == media_type) {
70                         return i;
71                 }
72         }
73         return -1;
74 }
75