X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Futil.cc;h=cfeacdb92cece74e3c0049081bba2964ace11fdb;hb=182b9d2e2feb6545592868606aaf0f0146095481;hp=6e2a1ad642c9d2b7a945abf32314879438b233c2;hpb=a2f8e8839f276bc83d5926983ab2d38e0a36f927;p=dcpomatic.git diff --git a/src/lib/util.cc b/src/lib/util.cc index 6e2a1ad64..cfeacdb92 100644 --- a/src/lib/util.cc +++ b/src/lib/util.cc @@ -34,6 +34,7 @@ #include "cross.h" #include "crypto.h" #include "dcp_content_type.h" +#include "dcpomatic_log.h" #include "digester.h" #include "exceptions.h" #include "ffmpeg_image_proxy.h" @@ -250,9 +251,11 @@ LIBDCP_DISABLE_WARNINGS LONG WINAPI exception_handler(struct _EXCEPTION_POINTERS * info) { - auto f = fopen_boost (backtrace_file, "w"); - fprintf (f, "C-style exception %d\n", info->ExceptionRecord->ExceptionCode); - fclose(f); + dcp::File f(backtrace_file, "w"); + if (f) { + fprintf(f.get(), "C-style exception %d\n", info->ExceptionRecord->ExceptionCode); + f.close(); + } if (info->ExceptionRecord->ExceptionCode != EXCEPTION_STACK_OVERFLOW) { CONTEXT* context = info->ContextRecord; @@ -408,8 +411,8 @@ LIBDCP_ENABLE_WARNINGS optional(), false, false, false, dcp::Colour(), 42, 1, dcp::Time(), dcp::Time(), 0, dcp::HAlign::CENTER, 0, dcp::VAlign::CENTER, dcp::Direction::LTR, "Hello dolly", dcp::Effect::NONE, dcp::Colour(), dcp::Time(), dcp::Time(), 0 ); - subs.push_back (StringText(ss, 0)); - render_text (subs, list>(), dcp::Size(640, 480), DCPTime(), 24); + subs.push_back (StringText(ss, 0, {}, dcp::Standard::SMPTE)); + render_text (subs, dcp::Size(640, 480), DCPTime(), 24); #endif Ratio::setup_ratios (); @@ -422,6 +425,8 @@ LIBDCP_ENABLE_WARNINGS curl_global_init (CURL_GLOBAL_ALL); ui_thread = boost::this_thread::get_id (); + + capture_asdcp_logs (); } #ifdef DCPOMATIC_WINDOWS @@ -490,16 +495,15 @@ digest_head_tail (vector files, boost::uintmax_t size) char* p = buffer.get (); int i = 0; while (i < int64_t (files.size()) && to_do > 0) { - auto f = fopen_boost (files[i], "rb"); + dcp::File f(files[i], "rb"); if (!f) { throw OpenFileError (files[i].string(), errno, OpenFileError::READ); } boost::uintmax_t this_time = min (to_do, boost::filesystem::file_size (files[i])); - checked_fread (p, this_time, f, files[i]); + f.checked_read(p, this_time); p += this_time; to_do -= this_time; - fclose (f); ++i; } @@ -510,17 +514,16 @@ digest_head_tail (vector files, boost::uintmax_t size) p = buffer.get (); i = files.size() - 1; while (i >= 0 && to_do > 0) { - auto f = fopen_boost (files[i], "rb"); + dcp::File f(files[i], "rb"); if (!f) { throw OpenFileError (files[i].string(), errno, OpenFileError::READ); } boost::uintmax_t this_time = min (to_do, boost::filesystem::file_size (files[i])); - dcpomatic_fseek (f, -this_time, SEEK_END); - checked_fread (p, this_time, f, files[i]); + f.seek(-this_time, SEEK_END); + f.checked_read(p, this_time); p += this_time; to_do -= this_time; - fclose (f); --i; } @@ -591,14 +594,14 @@ short_audio_channel_name (int c) _("Rs"), _("HI"), _("VI"), - _("Lc"), - _("Rc"), + _("9"), + _("10"), _("BsL"), _("BsR"), _("DBP"), _("DBS"), _("Sign"), - "" + _("16") }; return channels[c]; @@ -705,44 +708,47 @@ split_get_request (string url) return r; } + +static string -video_asset_filename (shared_ptr asset, int reel_index, int reel_count, optional summary) +asset_filename (shared_ptr asset, string type, int reel_index, int reel_count, optional summary, string extension) { dcp::NameFormat::Map values; - values['t'] = "j2c"; - values['r'] = raw_convert (reel_index + 1); - values['n'] = raw_convert (reel_count); + values['t'] = type; + values['r'] = raw_convert(reel_index + 1); + values['n'] = raw_convert(reel_count); if (summary) { - values['c'] = careful_string_filter (summary.get()); + values['c'] = careful_string_filter(summary.get()); } - return Config::instance()->dcp_asset_filename_format().get(values, "_" + asset->id() + ".mxf"); + return Config::instance()->dcp_asset_filename_format().get(values, "_" + asset->id() + extension); } + +string +video_asset_filename (shared_ptr asset, int reel_index, int reel_count, optional summary) +{ + return asset_filename(asset, "j2c", reel_index, reel_count, summary, ".mxf"); +} + + string audio_asset_filename (shared_ptr asset, int reel_index, int reel_count, optional summary) { - dcp::NameFormat::Map values; - values['t'] = "pcm"; - values['r'] = raw_convert (reel_index + 1); - values['n'] = raw_convert (reel_count); - if (summary) { - values['c'] = careful_string_filter (summary.get()); - } - return Config::instance()->dcp_asset_filename_format().get(values, "_" + asset->id() + ".mxf"); + return asset_filename(asset, "pcm", reel_index, reel_count, summary, ".mxf"); +} + + +string +subtitle_asset_filename (shared_ptr asset, int reel_index, int reel_count, optional summary, string extension) +{ + return asset_filename(asset, "sub", reel_index, reel_count, summary, extension); } string atmos_asset_filename (shared_ptr asset, int reel_index, int reel_count, optional summary) { - dcp::NameFormat::Map values; - values['t'] = "atmos"; - values['r'] = raw_convert (reel_index + 1); - values['n'] = raw_convert (reel_count); - if (summary) { - values['c'] = careful_string_filter (summary.get()); - } - return Config::instance()->dcp_asset_filename_format().get(values, "_" + asset->id() + ".mxf"); + return asset_filename(asset, "atmos", reel_index, reel_count, summary, ".mxf"); } @@ -767,19 +773,32 @@ careful_string_filter (string s) */ /* First transliterate using libicu to try to remove accents in a "nice" way */ - auto icu_utf16 = icu::UnicodeString::fromUTF8(icu::StringPiece(s)); + auto transliterated = icu::UnicodeString::fromUTF8(icu::StringPiece(s)); auto status = U_ZERO_ERROR; auto transliterator = icu::Transliterator::createInstance("NFD; [:M:] Remove; NFC", UTRANS_FORWARD, status); - transliterator->transliterate(icu_utf16); - s.clear (); - icu_utf16.toUTF8String(s); + transliterator->transliterate(transliterated); + + /* Some things are missed by ICU's transliterator */ + std::map replacements = { + { L'ł', L'l' }, + { L'Ł', L'L' } + }; + + icu::UnicodeString transliterated_more; + for (int i = 0; i < transliterated.length(); ++i) { + auto replacement = replacements.find(transliterated[i]); + if (replacement != replacements.end()) { + transliterated_more += replacement->second; + } else { + transliterated_more += transliterated[i]; + } + } /* Then remove anything that's not in a very limited character set */ - wstring ws = boost::locale::conv::utf_to_utf(s); - string out; - string const allowed = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_%.+"; - for (size_t i = 0; i < ws.size(); ++i) { - wchar_t c = ws[i]; + wstring out; + wstring const allowed = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_%.+"; + for (int i = 0; i < transliterated_more.length(); ++i) { + wchar_t c = transliterated_more[i]; if (allowed.find(c) != string::npos) { out += c; } @@ -864,35 +883,6 @@ increment_eyes (Eyes e) return Eyes::LEFT; } -void -checked_fwrite (void const * ptr, size_t size, FILE* stream, boost::filesystem::path path) -{ - size_t N = fwrite (ptr, 1, size, stream); - if (N != size) { - if (ferror(stream)) { - fclose (stream); - throw FileError (String::compose("fwrite error %1", errno), path); - } else { - fclose (stream); - throw FileError ("Unexpected short write", path); - } - } -} - -void -checked_fread (void* ptr, size_t size, FILE* stream, boost::filesystem::path path) -{ - size_t N = fread (ptr, 1, size, stream); - if (N != size) { - if (ferror(stream)) { - fclose (stream); - throw FileError (String::compose("fread error %1", errno), path); - } else { - fclose (stream); - throw FileError ("Unexpected short read", path); - } - } -} size_t utf8_strlen (string s) @@ -1034,52 +1024,38 @@ show_jobs_on_console (bool progress) void copy_in_bits (boost::filesystem::path from, boost::filesystem::path to, std::function progress) { - auto f = fopen_boost (from, "rb"); + dcp::File f(from, "rb"); if (!f) { throw OpenFileError (from, errno, OpenFileError::READ); } - auto t = fopen_boost (to, "wb"); + dcp::File t(to, "wb"); if (!t) { - fclose (f); throw OpenFileError (to, errno, OpenFileError::WRITE); } /* on the order of a second's worth of copying */ boost::uintmax_t const chunk = 20 * 1024 * 1024; - auto buffer = static_cast (malloc(chunk)); - if (!buffer) { - throw std::bad_alloc (); - } + std::vector buffer(chunk); boost::uintmax_t const total = boost::filesystem::file_size (from); boost::uintmax_t remaining = total; while (remaining) { boost::uintmax_t this_time = min (chunk, remaining); - size_t N = fread (buffer, 1, chunk, f); + size_t N = f.read(buffer.data(), 1, chunk); if (N < this_time) { - fclose (f); - fclose (t); - free (buffer); throw ReadFileError (from, errno); } - N = fwrite (buffer, 1, this_time, t); + N = t.write(buffer.data(), 1, this_time); if (N < this_time) { - fclose (f); - fclose (t); - free (buffer); throw WriteFileError (to, errno); } progress (1 - float(remaining) / total); remaining -= this_time; } - - fclose (f); - fclose (t); - free (buffer); } @@ -1145,6 +1121,9 @@ default_font_file () if (!boost::filesystem::exists(liberation_normal)) { liberation_normal = "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf"; } + if (!boost::filesystem::exists(liberation_normal)) { + liberation_normal = "/usr/share/fonts/liberation-sans/LiberationSans-Regular.ttf"; + } return liberation_normal; } @@ -1175,3 +1154,36 @@ start_of_thread (string) } #endif + +class LogSink : public Kumu::ILogSink +{ +public: + LogSink () {} + LogSink (LogSink const&) = delete; + LogSink& operator= (LogSink const&) = delete; + + void WriteEntry(const Kumu::LogEntry& entry) override { + Kumu::AutoMutex L(m_lock); + WriteEntryToListeners(entry); + if (entry.TestFilter(m_filter)) { + string buffer; + entry.CreateStringWithOptions(buffer, m_options); + LOG_GENERAL("asdcplib: %1", buffer); + } + } +}; + + +void +capture_asdcp_logs () +{ + static LogSink log_sink; + Kumu::SetDefaultLogSink(&log_sink); +} + + +string +error_details(boost::system::error_code ec) +{ + return String::compose("%1:%2:%3", ec.category().name(), ec.value(), ec.message()); +}