Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Darby Johnston <darbyjohnston@yahoo.com>
  • Loading branch information
darbyjohnston committed Dec 10, 2024
1 parent 9daa04a commit ebd39a8
Show file tree
Hide file tree
Showing 12 changed files with 483 additions and 233 deletions.
52 changes: 52 additions & 0 deletions data/MissingMedia.otio
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"OTIO_SCHEMA": "Timeline.1",
"metadata": {},
"name": "Gap",
"tracks": {
"OTIO_SCHEMA": "Stack.1",
"children": [
{
"OTIO_SCHEMA": "Track.1",
"children": [
{
"OTIO_SCHEMA": "Clip.1",
"media_reference": {
"OTIO_SCHEMA": "ExternalReference.1",
"available_range": {
"OTIO_SCHEMA": "TimeRange.1",
"duration": {
"OTIO_SCHEMA": "RationalTime.1",
"rate": 24,
"value": 3
},
"start_time": {
"OTIO_SCHEMA": "RationalTime.1",
"rate": 24,
"value": 0
}
},
"target_url": "Letter_None.png"
},
"source_range": {
"OTIO_SCHEMA": "TimeRange.1",
"duration": {
"OTIO_SCHEMA": "RationalTime.1",
"rate": 24,
"value": 3
},
"start_time": {
"OTIO_SCHEMA": "RationalTime.1",
"rate": 24,
"value": 0
}
},
"name": "Letter_None"
}
],
"kind": "Video",
"name": "Video"
}
],
"name": "Stack"
}
}
86 changes: 86 additions & 0 deletions data/MissingMedia2.otio
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"OTIO_SCHEMA": "Timeline.1",
"metadata": {},
"name": "Gap",
"tracks": {
"OTIO_SCHEMA": "Stack.1",
"children": [
{
"OTIO_SCHEMA": "Track.1",
"children": [
{
"OTIO_SCHEMA": "Clip.1",
"media_reference": {
"OTIO_SCHEMA": "ExternalReference.1",
"available_range": {
"OTIO_SCHEMA": "TimeRange.1",
"duration": {
"OTIO_SCHEMA": "RationalTime.1",
"rate": 24,
"value": 3
},
"start_time": {
"OTIO_SCHEMA": "RationalTime.1",
"rate": 24,
"value": 0
}
},
"target_url": "Letter_A.png"
},
"source_range": {
"OTIO_SCHEMA": "TimeRange.1",
"duration": {
"OTIO_SCHEMA": "RationalTime.1",
"rate": 24,
"value": 3
},
"start_time": {
"OTIO_SCHEMA": "RationalTime.1",
"rate": 24,
"value": 0
}
},
"name": "Letter_A"
},
{
"OTIO_SCHEMA": "Clip.1",
"media_reference": {
"OTIO_SCHEMA": "ExternalReference.1",
"available_range": {
"OTIO_SCHEMA": "TimeRange.1",
"duration": {
"OTIO_SCHEMA": "RationalTime.1",
"rate": 24,
"value": 3
},
"start_time": {
"OTIO_SCHEMA": "RationalTime.1",
"rate": 24,
"value": 0
}
},
"target_url": "Letter_None.png"
},
"source_range": {
"OTIO_SCHEMA": "TimeRange.1",
"duration": {
"OTIO_SCHEMA": "RationalTime.1",
"rate": 24,
"value": 3
},
"start_time": {
"OTIO_SCHEMA": "RationalTime.1",
"rate": 24,
"value": 0
}
},
"name": "Letter_None"
}
],
"kind": "Video",
"name": "Video"
}
],
"name": "Stack"
}
}
151 changes: 104 additions & 47 deletions lib/toucan/ImageGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,38 +53,64 @@ namespace toucan
{
if (auto externalRef = dynamic_cast<OTIO_NS::ExternalReference*>(clip->media_reference()))
{
auto read = std::make_shared<ReadNode>(
_timelineWrapper->getMediaPath(externalRef->target_url()),
_timelineWrapper->getMemoryReference(externalRef->target_url()));
const auto& spec = read->getSpec();
if (spec.width > 0)
try
{
_imageSize.x = spec.width;
_imageSize.y = spec.height;
_imageChannels = spec.nchannels;
_imageDataType = toImageDataType(spec.format);
break;
auto read = std::make_shared<ReadNode>(
_timelineWrapper->getMediaPath(externalRef->target_url()),
_timelineWrapper->getMemoryReference(externalRef->target_url()));
const auto& spec = read->getSpec();
if (spec.width > 0)
{
_imageSize.x = spec.width;
_imageSize.y = spec.height;
_imageChannels = spec.nchannels;
_imageDataType = toImageDataType(spec.format);
break;
}
}
catch (const std::exception& e)
{
if (_options.log)
{
_options.log->log(
"ImageGraph",
e.what(),
MessageLogType::Error);
}
}
}
else if (auto sequenceRef = dynamic_cast<OTIO_NS::ImageSequenceReference*>(clip->media_reference()))
{
auto read = std::make_shared<SequenceReadNode>(
_timelineWrapper->getMediaPath(sequenceRef->target_url_base()),
sequenceRef->name_prefix(),
sequenceRef->name_suffix(),
sequenceRef->start_frame(),
sequenceRef->frame_step(),
sequenceRef->rate(),
sequenceRef->frame_zero_padding(),
_timelineWrapper->getMemoryReferences());
const auto& spec = read->getSpec();
if (spec.width > 0)
try
{
_imageSize.x = spec.width;
_imageSize.y = spec.height;
_imageChannels = spec.nchannels;
_imageDataType = toImageDataType(spec.format);
break;
auto read = std::make_shared<SequenceReadNode>(
_timelineWrapper->getMediaPath(sequenceRef->target_url_base()),
sequenceRef->name_prefix(),
sequenceRef->name_suffix(),
sequenceRef->start_frame(),
sequenceRef->frame_step(),
sequenceRef->rate(),
sequenceRef->frame_zero_padding(),
_timelineWrapper->getMemoryReferences());
const auto& spec = read->getSpec();
if (spec.width > 0)
{
_imageSize.x = spec.width;
_imageSize.y = spec.height;
_imageChannels = spec.nchannels;
_imageDataType = toImageDataType(spec.format);
break;
}
}
catch (const std::exception& e)
{
if (_options.log)
{
_options.log->log(
"ImageGraph",
e.what(),
MessageLogType::Error);
}
}
}
else if (auto generatorRef = dynamic_cast<OTIO_NS::GeneratorReference*>(clip->media_reference()))
Expand Down Expand Up @@ -143,7 +169,7 @@ namespace toucan

// Get the track effects.
const auto& effects = track->effects();
if (!effects.empty())
if (trackNode && !effects.empty())
{
trackNode = _effects(host, effects, trackNode);
}
Expand Down Expand Up @@ -226,7 +252,7 @@ namespace toucan
}

// Handle transitions.
if (item)
if (item && out)
{
if (auto prevTransition = OTIO_NS::dynamic_retainer_cast<OTIO_NS::Transition>(prev))
{
Expand Down Expand Up @@ -323,34 +349,65 @@ namespace toucan
std::shared_ptr<ReadNode> read;
if (!_loadCache.get(externalRef, read))
{
read = std::make_shared<ReadNode>(
_timelineWrapper->getMediaPath(externalRef->target_url()),
_timelineWrapper->getMemoryReference(externalRef->target_url()));
try
{
read = std::make_shared<ReadNode>(
_timelineWrapper->getMediaPath(externalRef->target_url()),
_timelineWrapper->getMemoryReference(externalRef->target_url()));
}
catch (const std::exception& e)
{
if (_options.log)
{
_options.log->log(
"ImageGraph",
e.what(),
MessageLogType::Error);
}
}
_loadCache.add(externalRef, read);
}
out = read;

//! \bug Workaround for when the available range does not match
//! the range in the media.
const OTIO_NS::TimeRange& timeRange = read->getTimeRange();
const auto availableOpt = externalRef->available_range();
if (availableOpt.has_value() &&
!availableOpt.value().start_time().strictly_equal(timeRange.start_time()))
if (read)
{
timeOffset += availableOpt.value().start_time() - timeRange.start_time();
const OTIO_NS::TimeRange& timeRange = read->getTimeRange();
const auto availableOpt = externalRef->available_range();
if (availableOpt.has_value() &&
!availableOpt.value().start_time().strictly_equal(timeRange.start_time()))
{
timeOffset += availableOpt.value().start_time() - timeRange.start_time();
}
}

out = read;
}
else if (auto sequenceRef = dynamic_cast<OTIO_NS::ImageSequenceReference*>(clip->media_reference()))
{
auto read = std::make_shared<SequenceReadNode>(
_timelineWrapper->getMediaPath(sequenceRef->target_url_base()),
sequenceRef->name_prefix(),
sequenceRef->name_suffix(),
sequenceRef->start_frame(),
sequenceRef->frame_step(),
sequenceRef->rate(),
sequenceRef->frame_zero_padding(),
_timelineWrapper->getMemoryReferences());
std::shared_ptr<SequenceReadNode> read;
try
{
read = std::make_shared<SequenceReadNode>(
_timelineWrapper->getMediaPath(sequenceRef->target_url_base()),
sequenceRef->name_prefix(),
sequenceRef->name_suffix(),
sequenceRef->start_frame(),
sequenceRef->frame_step(),
sequenceRef->rate(),
sequenceRef->frame_zero_padding(),
_timelineWrapper->getMemoryReferences());
}
catch (const std::exception& e)
{
if (_options.log)
{
_options.log->log(
"ImageGraph",
e.what(),
MessageLogType::Error);
}
}
out = read;
}
else if (auto generatorRef = dynamic_cast<OTIO_NS::GeneratorReference*>(clip->media_reference()))
Expand All @@ -367,7 +424,7 @@ namespace toucan

// Get the effects.
const auto& effects = item->effects();
if (!effects.empty())
if (out && !effects.empty())
{
out = _effects(host, effects, out);
}
Expand Down
15 changes: 8 additions & 7 deletions lib/toucan/Read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,23 @@ namespace toucan
_path(path),
_memoryReader(getMemoryReader(memoryReference))
{
// Open the file ane get information.
try
// Open the file and get information.
if (ffmpeg::hasVideoExtension(path.extension().string()))
{
_ffRead = std::make_unique<ffmpeg::Read>(path, memoryReference);
_spec = _ffRead->getSpec();
_timeRange = _ffRead->getTimeRange();
}
catch (const std::exception&)
{}
if (!_ffRead)
else
{
_input = OIIO::ImageInput::open(_path.string(), nullptr, _memoryReader.get());
if (_input)
if (!_input)
{
_spec = _input->spec();
std::stringstream ss;
ss << "Cannot open file: " << _path.string();
throw std::runtime_error(ss.str());
}
_spec = _input->spec();
}
}

Expand Down
Loading

0 comments on commit ebd39a8

Please sign in to comment.