/src/serenity/Userland/Libraries/LibMedia/DecoderError.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/ByteString.h> |
10 | | #include <AK/Error.h> |
11 | | #include <AK/Format.h> |
12 | | #include <AK/SourceLocation.h> |
13 | | #include <LibMedia/Forward.h> |
14 | | #include <errno.h> |
15 | | |
16 | | namespace Media { |
17 | | |
18 | | template<typename T> |
19 | | using DecoderErrorOr = ErrorOr<T, DecoderError>; |
20 | | |
21 | | enum class DecoderErrorCategory : u32 { |
22 | | Unknown, |
23 | | IO, |
24 | | NeedsMoreInput, |
25 | | EndOfStream, |
26 | | Memory, |
27 | | // The input is corrupted. |
28 | | Corrupted, |
29 | | // Invalid call. |
30 | | Invalid, |
31 | | // The input uses features that are not yet implemented. |
32 | | NotImplemented, |
33 | | }; |
34 | | |
35 | | class DecoderError { |
36 | | public: |
37 | | static DecoderError with_description(DecoderErrorCategory category, StringView description) |
38 | 17.6k | { |
39 | 17.6k | return DecoderError(category, description); |
40 | 17.6k | } |
41 | | |
42 | | template<typename... Parameters> |
43 | | static DecoderError format(DecoderErrorCategory category, CheckedFormatString<Parameters...>&& format_string, Parameters const&... parameters) |
44 | 17.3k | { |
45 | 17.3k | AK::VariadicFormatParams<AK::AllowDebugOnlyFormatters::No, Parameters...> variadic_format_params { parameters... }; |
46 | 17.3k | return DecoderError::with_description(category, ByteString::vformatted(format_string.view(), variadic_format_params)); |
47 | 17.3k | } Unexecuted instantiation: Media::DecoderError Media::DecoderError::format<AK::StringView>(Media::DecoderErrorCategory, AK::Format::Detail::CheckedFormatString<AK::Detail::__IdentityType<AK::StringView>::Type>&&, AK::StringView const&) Media::DecoderError Media::DecoderError::format<AK::StringView, AK::StringView, unsigned int, AK::StringView>(Media::DecoderErrorCategory, AK::Format::Detail::CheckedFormatString<AK::Detail::__IdentityType<AK::StringView>::Type, AK::Detail::__IdentityType<AK::StringView>::Type, AK::Detail::__IdentityType<unsigned int>::Type, AK::Detail::__IdentityType<AK::StringView>::Type>&&, AK::StringView const&, AK::StringView const&, unsigned int const&, AK::StringView const&) Line | Count | Source | 44 | 8.52k | { | 45 | 8.52k | AK::VariadicFormatParams<AK::AllowDebugOnlyFormatters::No, Parameters...> variadic_format_params { parameters... }; | 46 | 8.52k | return DecoderError::with_description(category, ByteString::vformatted(format_string.view(), variadic_format_params)); | 47 | 8.52k | } |
Media::DecoderError Media::DecoderError::format<AK::StringView, AK::StringView>(Media::DecoderErrorCategory, AK::Format::Detail::CheckedFormatString<AK::Detail::__IdentityType<AK::StringView>::Type, AK::Detail::__IdentityType<AK::StringView>::Type>&&, AK::StringView const&, AK::StringView const&) Line | Count | Source | 44 | 8.82k | { | 45 | 8.82k | AK::VariadicFormatParams<AK::AllowDebugOnlyFormatters::No, Parameters...> variadic_format_params { parameters... }; | 46 | 8.82k | return DecoderError::with_description(category, ByteString::vformatted(format_string.view(), variadic_format_params)); | 47 | 8.82k | } |
Unexecuted instantiation: Media::DecoderError Media::DecoderError::format<unsigned long>(Media::DecoderErrorCategory, AK::Format::Detail::CheckedFormatString<AK::Detail::__IdentityType<unsigned long>::Type>&&, unsigned long const&) Unexecuted instantiation: Media::DecoderError Media::DecoderError::format<Media::CodecID>(Media::DecoderErrorCategory, AK::Format::Detail::CheckedFormatString<AK::Detail::__IdentityType<Media::CodecID>::Type>&&, Media::CodecID const&) Unexecuted instantiation: Media::DecoderError Media::DecoderError::format<>(Media::DecoderErrorCategory, AK::Format::Detail::CheckedFormatString<>&&) Media::DecoderError Media::DecoderError::format<unsigned char>(Media::DecoderErrorCategory, AK::Format::Detail::CheckedFormatString<AK::Detail::__IdentityType<unsigned char>::Type>&&, unsigned char const&) Line | Count | Source | 44 | 9 | { | 45 | 9 | AK::VariadicFormatParams<AK::AllowDebugOnlyFormatters::No, Parameters...> variadic_format_params { parameters... }; | 46 | 9 | return DecoderError::with_description(category, ByteString::vformatted(format_string.view(), variadic_format_params)); | 47 | 9 | } |
|
48 | | |
49 | | static DecoderError from_source_location(DecoderErrorCategory category, StringView description, SourceLocation location = SourceLocation::current()) |
50 | 8.52k | { |
51 | 8.52k | return DecoderError::format(category, "[{} @ {}:{}]: {}", location.function_name(), location.filename(), location.line_number(), description); |
52 | 8.52k | } |
53 | | |
54 | | static DecoderError corrupted(StringView description, SourceLocation location = SourceLocation::current()) |
55 | 2.44k | { |
56 | 2.44k | return DecoderError::from_source_location(DecoderErrorCategory::Corrupted, description, location); |
57 | 2.44k | } |
58 | | |
59 | | static DecoderError not_implemented(SourceLocation location = SourceLocation::current()) |
60 | 0 | { |
61 | 0 | return DecoderError::format(DecoderErrorCategory::NotImplemented, "{} is not implemented", location.function_name()); |
62 | 0 | } |
63 | | |
64 | 6.66k | DecoderErrorCategory category() const { return m_category; } |
65 | 6.66k | StringView description() const { return m_description; } |
66 | 0 | StringView string_literal() const { return m_description; } |
67 | | |
68 | | private: |
69 | | DecoderError(DecoderErrorCategory category, ByteString description) |
70 | 17.6k | : m_category(category) |
71 | 17.6k | , m_description(move(description)) |
72 | 17.6k | { |
73 | 17.6k | } |
74 | | |
75 | | DecoderErrorCategory m_category { DecoderErrorCategory::Unknown }; |
76 | | ByteString m_description; |
77 | | }; |
78 | | |
79 | | #define DECODER_TRY(category, expression) \ |
80 | 1.75M | ({ \ |
81 | 7.26k | auto&& _result = ((expression)); \ Unexecuted instantiation: Reader.cpp:Media::Matroska::parse_cue_track_position(Media::Matroska::Streamer&)::$_0::operator()(unsigned long) const Unexecuted instantiation: PlaybackManager.cpp:Media::PlaybackManager::create(AK::NonnullOwnPtr<Media::Demuxer>)::$_1::operator()() const Unexecuted instantiation: Decoder.cpp:Media::Video::VP8::Decoder::receive_sample(AK::Duration, AK::Span<unsigned char const>)::$_0::operator()() const |
82 | 1.75M | if (_result.is_error()) [[unlikely]] { \ |
83 | 3.25k | auto _error_string = _result.release_error().string_literal(); \ |
84 | 3.25k | return DecoderError::from_source_location( \ |
85 | 3.25k | ((category)), _error_string, SourceLocation::current()); \ |
86 | 3.25k | } \ |
87 | 1.74M | static_assert(!::AK::Detail::IsLvalueReference<decltype(_result.release_value())>, \ |
88 | 1.74M | "Do not return a reference from a fallible expression"); \ |
89 | 1.74M | _result.release_value(); \ |
90 | 1.74M | }) |
91 | | |
92 | 89.3k | #define DECODER_TRY_ALLOC(expression) DECODER_TRY(DecoderErrorCategory::Memory, expression) |
93 | | |
94 | | } |