2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
2013-11-18 23:48:14 +00:00
|
|
|
// This file contains the MpdBuilder, AdaptationSet, and Representation class
|
|
|
|
// declarations.
|
|
|
|
// http://goo.gl/UrsSlF
|
|
|
|
#ifndef MPD_BASE_MPD_BUILDER_H_
|
|
|
|
#define MPD_BASE_MPD_BUILDER_H_
|
|
|
|
|
2014-09-30 23:52:58 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2013-11-18 23:48:14 +00:00
|
|
|
#include <list>
|
|
|
|
#include <string>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/atomic_sequence_num.h"
|
|
|
|
#include "packager/base/gtest_prod_util.h"
|
|
|
|
#include "packager/base/stl_util.h"
|
|
|
|
#include "packager/base/synchronization/lock.h"
|
|
|
|
#include "packager/mpd/base/bandwidth_estimator.h"
|
|
|
|
#include "packager/mpd/base/content_protection_element.h"
|
|
|
|
#include "packager/mpd/base/media_info.pb.h"
|
2014-12-13 00:13:48 +00:00
|
|
|
#include "packager/mpd/base/mpd_options.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/mpd/base/mpd_utils.h"
|
|
|
|
#include "packager/mpd/base/segment_info.h"
|
|
|
|
#include "packager/mpd/base/xml/scoped_xml_ptr.h"
|
2013-11-18 23:48:14 +00:00
|
|
|
|
2014-01-31 19:02:30 +00:00
|
|
|
// TODO(rkuroiwa): For classes with |id_|, consider removing the field and let
|
|
|
|
// the MPD (XML) generation functions take care of assigning an ID to each
|
|
|
|
// element.
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
|
|
|
|
|
|
|
namespace media {
|
|
|
|
class File;
|
|
|
|
} // namespace media
|
2013-11-18 23:48:14 +00:00
|
|
|
|
|
|
|
class AdaptationSet;
|
|
|
|
class Representation;
|
|
|
|
|
|
|
|
namespace xml {
|
|
|
|
|
|
|
|
class XmlNode;
|
2014-05-22 02:16:17 +00:00
|
|
|
class RepresentationXmlNode;
|
2013-11-18 23:48:14 +00:00
|
|
|
|
|
|
|
} // namespace xml
|
|
|
|
|
2014-02-06 21:20:36 +00:00
|
|
|
/// This class generates DASH MPDs (Media Presentation Descriptions).
|
2013-11-18 23:48:14 +00:00
|
|
|
class MpdBuilder {
|
|
|
|
public:
|
|
|
|
enum MpdType {
|
|
|
|
kStatic = 0,
|
|
|
|
kDynamic
|
|
|
|
};
|
|
|
|
|
2014-02-06 21:20:36 +00:00
|
|
|
/// Constructs MpdBuilder.
|
|
|
|
/// @param type indicates whether the MPD should be for VOD or live content
|
|
|
|
/// (kStatic for VOD profile, or kDynamic for live profile).
|
2014-05-22 02:16:17 +00:00
|
|
|
MpdBuilder(MpdType type, const MpdOptions& mpd_options);
|
2013-11-18 23:48:14 +00:00
|
|
|
~MpdBuilder();
|
|
|
|
|
2014-02-06 21:20:36 +00:00
|
|
|
/// Add <BaseURL> entry to the MPD.
|
|
|
|
/// @param base_url URL for <BaseURL> entry.
|
2013-11-18 23:48:14 +00:00
|
|
|
void AddBaseUrl(const std::string& base_url);
|
|
|
|
|
2014-02-06 21:20:36 +00:00
|
|
|
/// Adds <AdaptationSet> to the MPD.
|
|
|
|
/// @return The new adaptation set, which is owned by this instance.
|
2013-11-18 23:48:14 +00:00
|
|
|
AdaptationSet* AddAdaptationSet();
|
|
|
|
|
2014-01-31 19:02:30 +00:00
|
|
|
/// Write the MPD to specified file.
|
|
|
|
/// @param[out] output_file is MPD destination. output_file will be
|
|
|
|
/// flushed but not closed.
|
|
|
|
/// @return true on success, false otherwise.
|
|
|
|
bool WriteMpdToFile(media::File* output_file);
|
|
|
|
|
2014-02-06 21:20:36 +00:00
|
|
|
/// Writes the MPD to the given string.
|
|
|
|
/// @param[out] output is an output string where the MPD gets written.
|
|
|
|
/// @return true on success, false otherwise.
|
2013-11-18 23:48:14 +00:00
|
|
|
bool ToString(std::string* output);
|
|
|
|
|
2014-05-19 21:30:58 +00:00
|
|
|
/// @return The mpd type.
|
|
|
|
MpdType type() { return type_; }
|
|
|
|
|
2013-11-18 23:48:14 +00:00
|
|
|
private:
|
2014-06-26 01:33:09 +00:00
|
|
|
// DynamicMpdBuilderTest needs to set availabilityStartTime so that the test
|
|
|
|
// doesn't need to depend on current time.
|
2014-05-22 02:16:17 +00:00
|
|
|
friend class DynamicMpdBuilderTest;
|
|
|
|
|
2013-11-18 23:48:14 +00:00
|
|
|
bool ToStringImpl(std::string* output);
|
|
|
|
|
2014-01-31 19:02:30 +00:00
|
|
|
// This is a helper method for writing out MPDs, called from WriteMpdToFile()
|
|
|
|
// and ToString().
|
|
|
|
template <typename OutputType>
|
|
|
|
bool WriteMpdToOutput(OutputType* output);
|
|
|
|
|
2013-11-18 23:48:14 +00:00
|
|
|
// Returns the document pointer to the MPD. This must be freed by the caller
|
|
|
|
// using appropriate xmlDocPtr freeing function.
|
|
|
|
// On failure, this returns NULL.
|
|
|
|
xmlDocPtr GenerateMpd();
|
|
|
|
|
2014-06-26 01:33:09 +00:00
|
|
|
// Set MPD attributes common to all profiles. Uses non-zero |mpd_options_| to
|
|
|
|
// set attributes for the MPD.
|
|
|
|
void AddCommonMpdInfo(xml::XmlNode* mpd_node);
|
|
|
|
|
2013-11-18 23:48:14 +00:00
|
|
|
// Adds 'static' MPD attributes and elements to |mpd_node|. This assumes that
|
|
|
|
// the first child element is a Period element.
|
|
|
|
void AddStaticMpdInfo(xml::XmlNode* mpd_node);
|
2014-05-22 02:16:17 +00:00
|
|
|
|
|
|
|
// Same as AddStaticMpdInfo() but for 'dynamic' MPDs.
|
|
|
|
void AddDynamicMpdInfo(xml::XmlNode* mpd_node);
|
|
|
|
|
2014-01-03 00:59:16 +00:00
|
|
|
float GetStaticMpdDuration(xml::XmlNode* mpd_node);
|
2013-11-18 23:48:14 +00:00
|
|
|
|
2014-06-26 01:33:09 +00:00
|
|
|
// Set MPD attributes for dynamic profile MPD. Uses non-zero |mpd_options_| as
|
|
|
|
// well as various calculations to set attributes for the MPD.
|
|
|
|
void SetDynamicMpdAttributes(xml::XmlNode* mpd_node);
|
|
|
|
|
|
|
|
// Gets the earliest, normalized segment timestamp. Returns true if
|
|
|
|
// successful, false otherwise.
|
|
|
|
bool GetEarliestTimestamp(double* timestamp_seconds);
|
2014-05-22 02:16:17 +00:00
|
|
|
|
2013-11-18 23:48:14 +00:00
|
|
|
MpdType type_;
|
2014-05-27 22:21:42 +00:00
|
|
|
MpdOptions mpd_options_;
|
2013-11-18 23:48:14 +00:00
|
|
|
std::list<AdaptationSet*> adaptation_sets_;
|
|
|
|
::STLElementDeleter<std::list<AdaptationSet*> > adaptation_sets_deleter_;
|
|
|
|
|
|
|
|
std::list<std::string> base_urls_;
|
2014-06-26 01:33:09 +00:00
|
|
|
std::string availability_start_time_;
|
2013-11-18 23:48:14 +00:00
|
|
|
|
|
|
|
base::Lock lock_;
|
|
|
|
base::AtomicSequenceNumber adaptation_set_counter_;
|
|
|
|
base::AtomicSequenceNumber representation_counter_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(MpdBuilder);
|
|
|
|
};
|
|
|
|
|
2014-02-06 21:20:36 +00:00
|
|
|
/// AdaptationSet class provides methods to add Representations and
|
|
|
|
/// <ContentProtection> elements to the AdaptationSet element.
|
2013-11-18 23:48:14 +00:00
|
|
|
class AdaptationSet {
|
|
|
|
public:
|
|
|
|
~AdaptationSet();
|
|
|
|
|
2014-02-06 21:20:36 +00:00
|
|
|
/// Create a Representation instance using @a media_info.
|
|
|
|
/// @param media_info is a MediaInfo object used to initialize the returned
|
|
|
|
/// Representation instance.
|
|
|
|
/// @return On success, returns a pointer to Representation. Otherwise returns
|
|
|
|
/// NULL. The returned pointer is owned by the AdaptationSet instance.
|
2013-11-18 23:48:14 +00:00
|
|
|
Representation* AddRepresentation(const MediaInfo& media_info);
|
|
|
|
|
2014-02-06 21:20:36 +00:00
|
|
|
/// Add a ContenProtection element to the adaptation set.
|
|
|
|
/// @param element contains the ContentProtection element contents.
|
|
|
|
/// If @a element has {value, schemeIdUri} set and has
|
|
|
|
/// {“value”, “schemeIdUri”} as key for @a additional_attributes,
|
|
|
|
/// then the former is used.
|
2013-11-18 23:48:14 +00:00
|
|
|
void AddContentProtectionElement(const ContentProtectionElement& element);
|
|
|
|
|
2014-02-06 21:20:36 +00:00
|
|
|
/// Makes a copy of AdaptationSet xml element with its child Representation
|
|
|
|
/// and ContentProtection elements.
|
|
|
|
/// @return On success returns a non-NULL ScopedXmlPtr. Otherwise returns a
|
|
|
|
/// NULL ScopedXmlPtr.
|
2013-11-18 23:48:14 +00:00
|
|
|
xml::ScopedXmlPtr<xmlNode>::type GetXml();
|
|
|
|
|
|
|
|
// Must be unique in the Period.
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t id() const { return id_; }
|
2013-11-18 23:48:14 +00:00
|
|
|
|
|
|
|
private:
|
2014-05-27 22:21:42 +00:00
|
|
|
friend class MpdBuilder;
|
|
|
|
FRIEND_TEST_ALL_PREFIXES(StaticMpdBuilderTest, CheckAdaptationSetId);
|
|
|
|
|
|
|
|
/// @param adaptation_set_id is an ID number for this AdaptationSet.
|
|
|
|
/// @param representation_counter is a Counter for assigning ID numbers to
|
|
|
|
/// Representation. It can not be NULL.
|
2014-09-30 21:52:21 +00:00
|
|
|
AdaptationSet(uint32_t adaptation_set_id,
|
2014-05-27 22:21:42 +00:00
|
|
|
const MpdOptions& mpd_options_,
|
|
|
|
base::AtomicSequenceNumber* representation_counter);
|
|
|
|
|
2014-06-26 01:33:09 +00:00
|
|
|
// Gets the earliest, normalized segment timestamp. Returns true if
|
|
|
|
// successful, false otherwise.
|
|
|
|
bool GetEarliestTimestamp(double* timestamp_seconds);
|
|
|
|
|
2013-11-18 23:48:14 +00:00
|
|
|
std::list<ContentProtectionElement> content_protection_elements_;
|
|
|
|
std::list<Representation*> representations_;
|
|
|
|
::STLElementDeleter<std::list<Representation*> > representations_deleter_;
|
|
|
|
|
|
|
|
base::Lock lock_;
|
|
|
|
|
|
|
|
base::AtomicSequenceNumber* const representation_counter_;
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint32_t id_;
|
2014-05-27 22:21:42 +00:00
|
|
|
const MpdOptions& mpd_options_;
|
2013-11-18 23:48:14 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(AdaptationSet);
|
|
|
|
};
|
|
|
|
|
2014-02-06 21:20:36 +00:00
|
|
|
/// Representation class contains references to a single media stream, as
|
|
|
|
/// well as optional ContentProtection elements for that stream.
|
2013-11-18 23:48:14 +00:00
|
|
|
class Representation {
|
|
|
|
public:
|
|
|
|
~Representation();
|
|
|
|
|
2014-02-06 21:20:36 +00:00
|
|
|
/// Tries to initialize the instance. If this does not succeed, the instance
|
|
|
|
/// should not be used.
|
|
|
|
/// @return true on success, false otherwise.
|
2014-01-03 00:59:16 +00:00
|
|
|
bool Init();
|
|
|
|
|
2014-02-06 21:20:36 +00:00
|
|
|
/// Add a ContenProtection element to the representation.
|
|
|
|
/// @param element contains the ContentProtection element contents.
|
|
|
|
/// If @a element has {value, schemeIdUri} set and has
|
|
|
|
/// {“value”, “schemeIdUri”} as key for @a additional_attributes,
|
|
|
|
/// then the former is used.
|
2013-11-18 23:48:14 +00:00
|
|
|
void AddContentProtectionElement(const ContentProtectionElement& element);
|
|
|
|
|
2014-02-06 21:20:36 +00:00
|
|
|
/// Add a media segment to the representation.
|
|
|
|
/// @param start_time is the start time for the segment, in units of the
|
|
|
|
/// stream's time scale.
|
|
|
|
/// @param duration is the duration of the segment, in units of the stream's
|
|
|
|
/// time scale.
|
2014-05-22 02:16:17 +00:00
|
|
|
/// @param size of the segment in bytes.
|
2014-09-30 21:52:21 +00:00
|
|
|
void AddNewSegment(uint64_t start_time, uint64_t duration, uint64_t size);
|
2013-11-18 23:48:14 +00:00
|
|
|
|
2014-02-06 21:20:36 +00:00
|
|
|
/// @return Copy of <Representation>.
|
2013-11-18 23:48:14 +00:00
|
|
|
xml::ScopedXmlPtr<xmlNode>::type GetXml();
|
|
|
|
|
2014-02-06 21:20:36 +00:00
|
|
|
/// @return ID number for <Representation>.
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t id() const { return id_; }
|
2013-11-18 23:48:14 +00:00
|
|
|
|
|
|
|
private:
|
2014-05-27 22:21:42 +00:00
|
|
|
friend class AdaptationSet;
|
|
|
|
FRIEND_TEST_ALL_PREFIXES(StaticMpdBuilderTest, CheckRepresentationId);
|
|
|
|
|
|
|
|
/// @param media_info is a MediaInfo containing information on the media.
|
|
|
|
/// @a media_info.bandwidth is required for 'static' profile. If @a
|
|
|
|
/// media_info.bandwidth is not present in 'dynamic' profile, this
|
|
|
|
/// tries to estimate it using the info passed to AddNewSegment().
|
|
|
|
/// @param representation_id is the numeric ID for the <Representation>.
|
|
|
|
Representation(const MediaInfo& media_info,
|
|
|
|
const MpdOptions& mpd_options,
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t representation_id);
|
2014-05-27 22:21:42 +00:00
|
|
|
|
2014-05-22 02:16:17 +00:00
|
|
|
bool AddLiveInfo(xml::RepresentationXmlNode* representation);
|
|
|
|
|
2014-02-06 21:20:36 +00:00
|
|
|
// Returns true if |media_info_| has required fields to generate a valid
|
|
|
|
// Representation. Otherwise returns false.
|
2014-01-03 00:59:16 +00:00
|
|
|
bool HasRequiredMediaInfoFields();
|
|
|
|
|
2014-05-22 02:16:17 +00:00
|
|
|
// Return false if the segment should be considered a new segment. True if the
|
|
|
|
// segment is contiguous.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool IsContiguous(uint64_t start_time,
|
|
|
|
uint64_t duration,
|
|
|
|
uint64_t size) const;
|
2014-05-22 02:16:17 +00:00
|
|
|
|
2014-05-27 22:21:42 +00:00
|
|
|
// Remove elements from |segment_infos_| if
|
|
|
|
// mpd_options_.time_shift_buffer_depth is specified. Increments
|
|
|
|
// |start_number_| by the number of segments removed.
|
|
|
|
void SlideWindow();
|
|
|
|
|
2014-01-03 00:59:16 +00:00
|
|
|
// Note: Because 'mimeType' is a required field for a valid MPD, these return
|
|
|
|
// strings.
|
|
|
|
std::string GetVideoMimeType() const;
|
|
|
|
std::string GetAudioMimeType() const;
|
|
|
|
|
2014-06-26 01:33:09 +00:00
|
|
|
// Gets the earliest, normalized segment timestamp. Returns true if
|
|
|
|
// successful, false otherwise.
|
|
|
|
bool GetEarliestTimestamp(double* timestamp_seconds);
|
|
|
|
|
2013-11-18 23:48:14 +00:00
|
|
|
MediaInfo media_info_;
|
|
|
|
std::list<ContentProtectionElement> content_protection_elements_;
|
2014-05-22 02:16:17 +00:00
|
|
|
std::list<SegmentInfo> segment_infos_;
|
2013-11-18 23:48:14 +00:00
|
|
|
|
|
|
|
base::Lock lock_;
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint32_t id_;
|
2014-01-03 00:59:16 +00:00
|
|
|
std::string mime_type_;
|
|
|
|
std::string codecs_;
|
2014-05-22 02:16:17 +00:00
|
|
|
BandwidthEstimator bandwidth_estimator_;
|
2014-06-26 01:33:09 +00:00
|
|
|
const MpdOptions& mpd_options_;
|
2014-05-27 22:21:42 +00:00
|
|
|
|
|
|
|
// startNumber attribute for SegmentTemplate.
|
|
|
|
// Starts from 1.
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t start_number_;
|
2013-11-18 23:48:14 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(Representation);
|
|
|
|
};
|
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|
2013-11-18 23:48:14 +00:00
|
|
|
|
|
|
|
#endif // MPD_BASE_MPD_BUILDER_H_
|