From e3d18b684ce75c79e61e2a5b245ceb26f598ca3e Mon Sep 17 00:00:00 2001 From: Thomas Inskip Date: Mon, 23 Mar 2015 12:55:58 -0700 Subject: [PATCH] Added static initialization and cleanup of libxml and libcurl. Change-Id: I31cfc9a76c3c90c66059998ffc77371619a43acc --- packager/media/base/http_key_fetcher.cc | 41 +++++++++++++++++++------ packager/media/base/http_key_fetcher.h | 5 ++- packager/media/file/threaded_io_file.cc | 4 --- packager/mpd/base/mpd_builder.cc | 33 ++++++++++++++++++-- packager/mpd/base/mpd_builder.h | 4 +++ 5 files changed, 70 insertions(+), 17 deletions(-) diff --git a/packager/media/base/http_key_fetcher.cc b/packager/media/base/http_key_fetcher.cc index 4f8e6701ee..7f774f4b32 100644 --- a/packager/media/base/http_key_fetcher.cc +++ b/packager/media/base/http_key_fetcher.cc @@ -8,6 +8,7 @@ #include #include "packager/base/strings/stringprintf.h" +#include "packager/base/synchronization/lock.h" namespace { const char kUserAgentString[] = "edash-packager-http_fetcher/1.0"; @@ -35,23 +36,43 @@ size_t AppendToString(char* ptr, size_t size, size_t nmemb, std::string* respons response->append(ptr, total_size); return total_size; } + +class LibCurlInitializer { + public: + LibCurlInitializer() : initialized_(false) { + base::AutoLock lock(lock_); + if (!initialized_) { + curl_global_init(CURL_GLOBAL_DEFAULT); + initialized_ = true; + } + } + + ~LibCurlInitializer() { + base::AutoLock lock(lock_); + if (initialized_) { + curl_global_cleanup(); + initialized_ = false; + } + } + + private: + base::Lock lock_; + bool initialized_; + + DISALLOW_COPY_AND_ASSIGN(LibCurlInitializer); +}; + } // namespace namespace edash_packager { namespace media { -HttpKeyFetcher::HttpKeyFetcher() : timeout_in_seconds_(0) { - curl_global_init(CURL_GLOBAL_DEFAULT); -} +HttpKeyFetcher::HttpKeyFetcher() : timeout_in_seconds_(0) {} HttpKeyFetcher::HttpKeyFetcher(uint32_t timeout_in_seconds) - : timeout_in_seconds_(timeout_in_seconds) { - curl_global_init(CURL_GLOBAL_DEFAULT); -} + : timeout_in_seconds_(timeout_in_seconds) {} -HttpKeyFetcher::~HttpKeyFetcher() { - curl_global_cleanup(); -} +HttpKeyFetcher::~HttpKeyFetcher() {} Status HttpKeyFetcher::FetchKeys(const std::string& url, const std::string& request, @@ -75,6 +96,8 @@ Status HttpKeyFetcher::FetchInternal(HttpMethod method, std::string* response) { DCHECK(method == GET || method == POST); + static LibCurlInitializer lib_curl_initializer; + ScopedCurl scoped_curl; CURL* curl = scoped_curl.get(); if (!curl) { diff --git a/packager/media/base/http_key_fetcher.h b/packager/media/base/http_key_fetcher.h index 6d05e21f41..1e8ba852d1 100644 --- a/packager/media/base/http_key_fetcher.h +++ b/packager/media/base/http_key_fetcher.h @@ -3,6 +3,10 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd +// +/// NOTE: Inclusion of this module will cause curl_global_init and +/// curl_global_cleanup to be called at static initialization / +/// deinitialization time. #ifndef MEDIA_BASE_HTTP_KEY_FETCHER_H_ #define MEDIA_BASE_HTTP_KEY_FETCHER_H_ @@ -69,4 +73,3 @@ class HttpKeyFetcher : public KeyFetcher { } // namespace edash_packager #endif // MEDIA_BASE_HTTP_KEY_FETCHER_H_ - diff --git a/packager/media/file/threaded_io_file.cc b/packager/media/file/threaded_io_file.cc index 2c95de599b..e2dca86ec5 100644 --- a/packager/media/file/threaded_io_file.cc +++ b/packager/media/file/threaded_io_file.cc @@ -14,10 +14,6 @@ namespace edash_packager { namespace media { -namespace { -const int kWaitDelayMs = 10; -} - ThreadedIoFile::ThreadedIoFile(scoped_ptr internal_file, Mode mode, uint64_t io_cache_size, diff --git a/packager/mpd/base/mpd_builder.cc b/packager/mpd/base/mpd_builder.cc index 3e133e8b05..ad3ec99264 100644 --- a/packager/mpd/base/mpd_builder.cc +++ b/packager/mpd/base/mpd_builder.cc @@ -18,6 +18,7 @@ #include "packager/base/memory/scoped_ptr.h" #include "packager/base/strings/string_number_conversions.h" #include "packager/base/strings/stringprintf.h" +#include "packager/base/synchronization/lock.h" #include "packager/base/time/time.h" #include "packager/media/file/file.h" #include "packager/mpd/base/content_protection_element.h" @@ -188,6 +189,32 @@ std::string MakePathRelative(const std::string& path, const std::string& mpd_dir return (path.find(mpd_dir) == 0) ? path.substr(mpd_dir.size()) : path; } +// Spooky static initialization/cleanup of libxml. +class LibXmlInitializer { + public: + LibXmlInitializer() : initialized_(false) { + base::AutoLock lock(lock_); + if (!initialized_) { + xmlInitParser(); + initialized_ = true; + } + } + + ~LibXmlInitializer() { + base::AutoLock lock(lock_); + if (initialized_) { + xmlCleanupParser(); + initialized_ = false; + } + } + + private: + base::Lock lock_; + bool initialized_; + + DISALLOW_COPY_AND_ASSIGN(LibXmlInitializer); +}; + } // namespace MpdBuilder::MpdBuilder(MpdType type, const MpdOptions& mpd_options) @@ -227,7 +254,8 @@ bool MpdBuilder::ToString(std::string* output) { template bool MpdBuilder::WriteMpdToOutput(OutputType* output) { - xmlInitParser(); + static LibXmlInitializer lib_xml_initializer; + xml::ScopedXmlPtr::type doc(GenerateMpd()); if (!doc.get()) return false; @@ -241,9 +269,8 @@ bool MpdBuilder::WriteMpdToOutput(OutputType* output) { bool result = WriteXmlCharArrayToOutput(doc_str, doc_str_size, output); xmlFree(doc_str); - // Cleanup, free the doc then cleanup parser. + // Cleanup, free the doc. doc.reset(); - xmlCleanupParser(); return result; } diff --git a/packager/mpd/base/mpd_builder.h b/packager/mpd/base/mpd_builder.h index 18b21bd708..1ed169bb49 100644 --- a/packager/mpd/base/mpd_builder.h +++ b/packager/mpd/base/mpd_builder.h @@ -7,6 +7,10 @@ // This file contains the MpdBuilder, AdaptationSet, and Representation class // declarations. // http://goo.gl/UrsSlF +// +/// NOTE: Inclusion of this module will cause xmlInitParser and xmlCleanupParser +/// to be called at static initialization / deinitialization time. + #ifndef MPD_BASE_MPD_BUILDER_H_ #define MPD_BASE_MPD_BUILDER_H_