Added static initialization and cleanup of libxml and libcurl.

Change-Id: I31cfc9a76c3c90c66059998ffc77371619a43acc
This commit is contained in:
Thomas Inskip 2015-03-23 12:55:58 -07:00
parent 0ff596e75b
commit e3d18b684c
5 changed files with 70 additions and 17 deletions

View File

@ -8,6 +8,7 @@
#include <curl/curl.h> #include <curl/curl.h>
#include "packager/base/strings/stringprintf.h" #include "packager/base/strings/stringprintf.h"
#include "packager/base/synchronization/lock.h"
namespace { namespace {
const char kUserAgentString[] = "edash-packager-http_fetcher/1.0"; 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); response->append(ptr, total_size);
return 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
namespace edash_packager { namespace edash_packager {
namespace media { namespace media {
HttpKeyFetcher::HttpKeyFetcher() : timeout_in_seconds_(0) { HttpKeyFetcher::HttpKeyFetcher() : timeout_in_seconds_(0) {}
curl_global_init(CURL_GLOBAL_DEFAULT);
}
HttpKeyFetcher::HttpKeyFetcher(uint32_t timeout_in_seconds) HttpKeyFetcher::HttpKeyFetcher(uint32_t timeout_in_seconds)
: timeout_in_seconds_(timeout_in_seconds) { : timeout_in_seconds_(timeout_in_seconds) {}
curl_global_init(CURL_GLOBAL_DEFAULT);
}
HttpKeyFetcher::~HttpKeyFetcher() { HttpKeyFetcher::~HttpKeyFetcher() {}
curl_global_cleanup();
}
Status HttpKeyFetcher::FetchKeys(const std::string& url, Status HttpKeyFetcher::FetchKeys(const std::string& url,
const std::string& request, const std::string& request,
@ -75,6 +96,8 @@ Status HttpKeyFetcher::FetchInternal(HttpMethod method,
std::string* response) { std::string* response) {
DCHECK(method == GET || method == POST); DCHECK(method == GET || method == POST);
static LibCurlInitializer lib_curl_initializer;
ScopedCurl scoped_curl; ScopedCurl scoped_curl;
CURL* curl = scoped_curl.get(); CURL* curl = scoped_curl.get();
if (!curl) { if (!curl) {

View File

@ -3,6 +3,10 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at // license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd // 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_ #ifndef MEDIA_BASE_HTTP_KEY_FETCHER_H_
#define MEDIA_BASE_HTTP_KEY_FETCHER_H_ #define MEDIA_BASE_HTTP_KEY_FETCHER_H_
@ -69,4 +73,3 @@ class HttpKeyFetcher : public KeyFetcher {
} // namespace edash_packager } // namespace edash_packager
#endif // MEDIA_BASE_HTTP_KEY_FETCHER_H_ #endif // MEDIA_BASE_HTTP_KEY_FETCHER_H_

View File

@ -14,10 +14,6 @@
namespace edash_packager { namespace edash_packager {
namespace media { namespace media {
namespace {
const int kWaitDelayMs = 10;
}
ThreadedIoFile::ThreadedIoFile(scoped_ptr<File, FileCloser> internal_file, ThreadedIoFile::ThreadedIoFile(scoped_ptr<File, FileCloser> internal_file,
Mode mode, Mode mode,
uint64_t io_cache_size, uint64_t io_cache_size,

View File

@ -18,6 +18,7 @@
#include "packager/base/memory/scoped_ptr.h" #include "packager/base/memory/scoped_ptr.h"
#include "packager/base/strings/string_number_conversions.h" #include "packager/base/strings/string_number_conversions.h"
#include "packager/base/strings/stringprintf.h" #include "packager/base/strings/stringprintf.h"
#include "packager/base/synchronization/lock.h"
#include "packager/base/time/time.h" #include "packager/base/time/time.h"
#include "packager/media/file/file.h" #include "packager/media/file/file.h"
#include "packager/mpd/base/content_protection_element.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; 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 } // namespace
MpdBuilder::MpdBuilder(MpdType type, const MpdOptions& mpd_options) MpdBuilder::MpdBuilder(MpdType type, const MpdOptions& mpd_options)
@ -227,7 +254,8 @@ bool MpdBuilder::ToString(std::string* output) {
template <typename OutputType> template <typename OutputType>
bool MpdBuilder::WriteMpdToOutput(OutputType* output) { bool MpdBuilder::WriteMpdToOutput(OutputType* output) {
xmlInitParser(); static LibXmlInitializer lib_xml_initializer;
xml::ScopedXmlPtr<xmlDoc>::type doc(GenerateMpd()); xml::ScopedXmlPtr<xmlDoc>::type doc(GenerateMpd());
if (!doc.get()) if (!doc.get())
return false; return false;
@ -241,9 +269,8 @@ bool MpdBuilder::WriteMpdToOutput(OutputType* output) {
bool result = WriteXmlCharArrayToOutput(doc_str, doc_str_size, output); bool result = WriteXmlCharArrayToOutput(doc_str, doc_str_size, output);
xmlFree(doc_str); xmlFree(doc_str);
// Cleanup, free the doc then cleanup parser. // Cleanup, free the doc.
doc.reset(); doc.reset();
xmlCleanupParser();
return result; return result;
} }

View File

@ -7,6 +7,10 @@
// This file contains the MpdBuilder, AdaptationSet, and Representation class // This file contains the MpdBuilder, AdaptationSet, and Representation class
// declarations. // declarations.
// http://goo.gl/UrsSlF // 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_ #ifndef MPD_BASE_MPD_BUILDER_H_
#define MPD_BASE_MPD_BUILDER_H_ #define MPD_BASE_MPD_BUILDER_H_