Blender V5.3
media_presence.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_fileops.hh"
10#include "BLI_listbase.hh"
11#include "BLI_map.hh"
12#include "BLI_mutex.hh"
13#include "BLI_path_utils.hh"
14#include "BLI_string.hh"
15
16#include "DNA_scene_types.h"
17#include "DNA_sequence_types.h"
18#include "DNA_sound_types.h"
19
20#include "BKE_library.hh"
21#include "BKE_main.hh"
22
23#include "SEQ_sequencer.hh"
24#include "SEQ_utils.hh"
25
26namespace blender::seq {
27
29
30static bool check_sound_media_missing(const bSound *sound)
31{
32 if (sound == nullptr) {
33 return false;
34 }
35
36 char filepath[FILE_MAX];
37 STRNCPY(filepath, sound->filepath);
38 BLI_path_abs(filepath, ID_BLEND_PATH_FROM_GLOBAL(&sound->id));
39 return !BLI_exists(filepath);
40}
41
42static bool check_media_missing(const Scene *scene, const Strip *strip)
43{
44 if (strip == nullptr || strip->data == nullptr) {
45 return false;
46 }
47
48 /* Images or movies. */
49 if (ELEM((strip)->type, STRIP_TYPE_MOVIE, STRIP_TYPE_IMAGE)) {
50 const StripElem *elem = strip->data->stripdata;
51 if (elem != nullptr) {
52 int paths_count = 1;
53 if (strip->type == STRIP_TYPE_IMAGE) {
54 /* Image strip has array of file names. */
55 paths_count = strip->data->stripdata_num;
56 }
57 char filepath[FILE_MAX];
58 const char *basepath = ID_BLEND_PATH_FROM_GLOBAL(&scene->id);
59 for (int i = 0; i < paths_count; i++, elem++) {
60 BLI_path_join(filepath, sizeof(filepath), strip->data->dirpath, elem->filename);
61 BLI_path_abs(filepath, basepath);
62 if (!BLI_exists(filepath)) {
63 return true;
64 }
65 }
66 }
67 }
68
69 /* Recurse into meta strips. */
70 if (strip->type == STRIP_TYPE_META) {
71 for (Strip &strip_n : strip->seqbase) {
72 if (check_media_missing(scene, &strip_n)) {
73 return true;
74 }
75 }
76 }
77
78 /* Nothing is missing. */
79 return false;
80}
81
86
88{
89 MediaPresence **presence = &scene->ed->runtime->media_presence;
90 if (*presence == nullptr) {
91 *presence = MEM_new<MediaPresence>(__func__);
92 }
93 return *presence;
94}
95
96bool media_presence_is_missing(Scene *scene, const Strip *strip)
97{
98 if (strip == nullptr || scene == nullptr || scene->ed == nullptr) {
99 return false;
100 }
101
102 std::scoped_lock lock(presence_lock);
103
104 MediaPresence *presence = get_media_presence_cache(scene);
105
106 bool missing = false;
107
108 /* Strips that reference another data block that has path to media
109 * (e.g. sound strips) need to key the presence cache on that data
110 * block. Since it can be used by multiple strips. */
111 if (strip->type == STRIP_TYPE_SOUND) {
112 const bSound *sound = strip->sound;
113 if (sound && sound->packedfile != nullptr) {
114 /* The sound file has been packed, don't look up the path. */
115 return false;
116 }
117 const bool *val = presence->map_sound.lookup_ptr(sound);
118 if (val != nullptr) {
119 missing = *val;
120 }
121 else {
122 missing = check_sound_media_missing(sound);
123 presence->map_sound.add_new(sound, missing);
124 }
125 }
126 else {
127 /* Regular strips that point to media directly. */
128 const bool *val = presence->map_seq.lookup_ptr(strip);
129 if (val != nullptr) {
130 missing = *val;
131 }
132 else {
133 missing = check_media_missing(scene, strip);
134 presence->map_seq.add_new(strip, missing);
135 }
136 }
137
138 return missing;
139}
140
141void media_presence_set_missing(Scene *scene, const Strip *strip, bool missing)
142{
143 if (strip == nullptr || scene == nullptr || scene->ed == nullptr) {
144 return;
145 }
146
147 std::scoped_lock lock(presence_lock);
148
149 MediaPresence *presence = get_media_presence_cache(scene);
150
151 if (strip->type == STRIP_TYPE_SOUND) {
152 const bSound *sound = strip->sound;
153 presence->map_sound.add_overwrite(sound, missing);
154 }
155 else {
156 presence->map_seq.add_overwrite(strip, missing);
157 }
158}
159
161{
162 std::scoped_lock lock(presence_lock);
163 if (scene != nullptr && scene->ed != nullptr && scene->ed->runtime->media_presence != nullptr) {
164 scene->ed->runtime->media_presence->map_seq.remove(strip);
165 }
166}
167
169{
170 std::scoped_lock lock(presence_lock);
171 if (scene != nullptr && scene->ed != nullptr && scene->ed->runtime->media_presence != nullptr) {
172 scene->ed->runtime->media_presence->map_sound.remove(sound);
173 }
174}
175
177{
178 std::scoped_lock lock(presence_lock);
179 if (scene != nullptr && scene->ed != nullptr && scene->ed->runtime->media_presence != nullptr) {
180 MEM_delete(scene->ed->runtime->media_presence);
181 scene->ed->runtime->media_presence = nullptr;
182 }
183}
184
185} // namespace blender::seq
File and directory operations.
#define FILE_MAX
#define BLI_path_join(...)
#define ID_BLEND_PATH_FROM_GLOBAL(_id)
Definition DNA_ID.h:691
volatile int lock
#define ELEM(...)
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
bool add_overwrite(const Key &key, const Value &value)
Definition BLI_map.hh:325
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
bool remove(const Key &key)
Definition BLI_map.hh:368
const T & data(const Action &owning_action) const
static Mutex presence_lock
bool media_presence_is_missing(Scene *scene, const Strip *strip)
void media_presence_free(Scene *scene)
static bool check_sound_media_missing(const bSound *sound)
static bool check_media_missing(const Scene *scene, const Strip *strip)
void media_presence_invalidate_sound(Scene *scene, const bSound *sound)
void media_presence_invalidate_strip(Scene *scene, const Strip *strip)
void media_presence_set_missing(Scene *scene, const Strip *strip, bool missing)
static MediaPresence * get_media_presence_cache(Scene *scene)
char * STRNCPY(char(&dst)[N], const char *src)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
std::mutex Mutex
Definition BLI_mutex.hh:51
seq::EditingRuntime * runtime
struct Editing * ed
char filepath[1024]
struct PackedFile * packedfile
Map< const void *, bool > map_seq
Map< const bSound *, bool > map_sound