2010-08-05 07:40:35 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-08-05 07:40:35 +00:00
|
|
|
|
2013-03-02 19:14:44 +00:00
|
|
|
#include "mozilla/dom/TimeRanges.h"
|
2013-03-02 19:16:43 +00:00
|
|
|
#include "mozilla/dom/TimeRangesBinding.h"
|
2013-03-19 12:23:54 +00:00
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2010-08-05 07:40:35 +00:00
|
|
|
|
2013-03-02 19:14:44 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2010-08-05 07:40:35 +00:00
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(TimeRanges, nsIDOMTimeRanges)
|
2010-08-05 07:40:35 +00:00
|
|
|
|
2013-03-02 19:14:44 +00:00
|
|
|
TimeRanges::TimeRanges()
|
2012-05-01 00:29:24 +00:00
|
|
|
{
|
2013-03-02 19:14:44 +00:00
|
|
|
MOZ_COUNT_CTOR(TimeRanges);
|
2011-03-23 22:28:57 +00:00
|
|
|
}
|
|
|
|
|
2013-03-02 19:14:44 +00:00
|
|
|
TimeRanges::~TimeRanges()
|
2012-05-01 00:29:24 +00:00
|
|
|
{
|
2013-03-02 19:14:44 +00:00
|
|
|
MOZ_COUNT_DTOR(TimeRanges);
|
2011-03-23 22:28:57 +00:00
|
|
|
}
|
|
|
|
|
2010-08-05 07:40:35 +00:00
|
|
|
NS_IMETHODIMP
|
2013-03-02 19:14:44 +00:00
|
|
|
TimeRanges::GetLength(uint32_t* aLength)
|
2012-05-01 00:29:24 +00:00
|
|
|
{
|
2013-03-02 19:16:43 +00:00
|
|
|
*aLength = Length();
|
2010-08-05 07:40:35 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-02 19:16:43 +00:00
|
|
|
double
|
|
|
|
TimeRanges::Start(uint32_t aIndex, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (aIndex >= mRanges.Length()) {
|
|
|
|
aRv = NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mRanges[aIndex].mStart;
|
|
|
|
}
|
|
|
|
|
2010-08-05 07:40:35 +00:00
|
|
|
NS_IMETHODIMP
|
2013-03-02 19:14:44 +00:00
|
|
|
TimeRanges::Start(uint32_t aIndex, double* aTime)
|
2012-05-01 00:29:24 +00:00
|
|
|
{
|
2013-03-02 19:16:43 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
*aTime = Start(aIndex, rv);
|
|
|
|
return rv.ErrorCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
TimeRanges::End(uint32_t aIndex, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (aIndex >= mRanges.Length()) {
|
|
|
|
aRv = NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mRanges[aIndex].mEnd;
|
2010-08-05 07:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-03-02 19:14:44 +00:00
|
|
|
TimeRanges::End(uint32_t aIndex, double* aTime)
|
2012-05-01 00:29:24 +00:00
|
|
|
{
|
2013-03-02 19:16:43 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
*aTime = End(aIndex, rv);
|
|
|
|
return rv.ErrorCode();
|
2010-08-05 07:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-03-02 19:14:44 +00:00
|
|
|
TimeRanges::Add(double aStart, double aEnd)
|
2012-05-01 00:29:24 +00:00
|
|
|
{
|
|
|
|
if (aStart > aEnd) {
|
|
|
|
NS_WARNING("Can't add a range if the end is older that the start.");
|
|
|
|
return;
|
|
|
|
}
|
2010-08-05 07:40:35 +00:00
|
|
|
mRanges.AppendElement(TimeRange(aStart,aEnd));
|
|
|
|
}
|
2012-05-01 00:29:24 +00:00
|
|
|
|
2013-01-29 02:34:27 +00:00
|
|
|
double
|
2014-04-14 11:23:00 +00:00
|
|
|
TimeRanges::GetStartTime()
|
2013-01-29 02:34:27 +00:00
|
|
|
{
|
|
|
|
if (mRanges.IsEmpty()) {
|
|
|
|
return -1.0;
|
|
|
|
}
|
2014-04-14 11:23:00 +00:00
|
|
|
return mRanges[0].mStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
TimeRanges::GetEndTime()
|
|
|
|
{
|
|
|
|
if (mRanges.IsEmpty()) {
|
|
|
|
return -1.0;
|
|
|
|
}
|
|
|
|
return mRanges[mRanges.Length() - 1].mEnd;
|
2013-01-29 02:34:27 +00:00
|
|
|
}
|
|
|
|
|
2012-05-01 00:29:24 +00:00
|
|
|
void
|
2014-12-10 21:52:57 +00:00
|
|
|
TimeRanges::Normalize(double aError)
|
2012-05-01 00:29:24 +00:00
|
|
|
{
|
|
|
|
if (mRanges.Length() >= 2) {
|
|
|
|
nsAutoTArray<TimeRange,4> normalized;
|
|
|
|
|
|
|
|
mRanges.Sort(CompareTimeRanges());
|
|
|
|
|
|
|
|
// This merges the intervals.
|
|
|
|
TimeRange current(mRanges[0]);
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 1; i < mRanges.Length(); i++) {
|
2012-06-06 23:43:13 +00:00
|
|
|
if (current.mStart <= mRanges[i].mStart &&
|
|
|
|
current.mEnd >= mRanges[i].mEnd) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-12-10 21:52:57 +00:00
|
|
|
if (current.mEnd + aError >= mRanges[i].mStart) {
|
2012-05-01 00:29:24 +00:00
|
|
|
current.mEnd = mRanges[i].mEnd;
|
|
|
|
} else {
|
|
|
|
normalized.AppendElement(current);
|
|
|
|
current = mRanges[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
normalized.AppendElement(current);
|
|
|
|
|
|
|
|
mRanges = normalized;
|
|
|
|
}
|
|
|
|
}
|
2013-03-02 19:14:44 +00:00
|
|
|
|
2014-08-11 04:32:21 +00:00
|
|
|
void
|
2014-12-10 21:52:57 +00:00
|
|
|
TimeRanges::Union(const TimeRanges* aOtherRanges, double aError)
|
2014-08-11 04:32:21 +00:00
|
|
|
{
|
|
|
|
mRanges.AppendElements(aOtherRanges->mRanges);
|
2014-12-10 21:52:57 +00:00
|
|
|
Normalize(aError);
|
2014-08-11 04:32:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TimeRanges::Intersection(const TimeRanges* aOtherRanges)
|
|
|
|
{
|
|
|
|
nsAutoTArray<TimeRange,4> intersection;
|
|
|
|
|
|
|
|
const nsTArray<TimeRange>& otherRanges = aOtherRanges->mRanges;
|
|
|
|
for (index_type i = 0, j = 0; i < mRanges.Length() && j < otherRanges.Length();) {
|
|
|
|
double start = std::max(mRanges[i].mStart, otherRanges[j].mStart);
|
|
|
|
double end = std::min(mRanges[i].mEnd, otherRanges[j].mEnd);
|
|
|
|
if (start < end) {
|
|
|
|
intersection.AppendElement(TimeRange(start, end));
|
|
|
|
}
|
|
|
|
if (mRanges[i].mEnd < otherRanges[j].mEnd) {
|
|
|
|
i += 1;
|
|
|
|
} else {
|
|
|
|
j += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mRanges = intersection;
|
|
|
|
}
|
|
|
|
|
2014-08-11 02:05:09 +00:00
|
|
|
TimeRanges::index_type
|
2015-01-11 20:47:56 +00:00
|
|
|
TimeRanges::Find(double aTime, double aError /* = 0 */)
|
2014-08-11 02:05:09 +00:00
|
|
|
{
|
|
|
|
for (index_type i = 0; i < mRanges.Length(); ++i) {
|
2015-01-11 20:47:56 +00:00
|
|
|
if (aTime < mRanges[i].mEnd && (aTime + aError) >= mRanges[i].mStart) {
|
2014-08-11 02:05:09 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NoIndex;
|
|
|
|
}
|
|
|
|
|
2015-01-08 21:56:42 +00:00
|
|
|
bool
|
|
|
|
TimeRanges::WrapObject(JSContext* aCx, JS::MutableHandle<JSObject*> aReflector)
|
2013-03-02 19:16:43 +00:00
|
|
|
{
|
2015-01-08 21:56:42 +00:00
|
|
|
return TimeRangesBinding::Wrap(aCx, this, aReflector);
|
2013-03-02 19:16:43 +00:00
|
|
|
}
|
|
|
|
|
2013-03-02 19:14:44 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|