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 "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) {

View File

@ -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_

View File

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

View File

@ -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 <typename OutputType>
bool MpdBuilder::WriteMpdToOutput(OutputType* output) {
xmlInitParser();
static LibXmlInitializer lib_xml_initializer;
xml::ScopedXmlPtr<xmlDoc>::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;
}

View File

@ -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_