Home | History | Annotate | Download | only in media
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "content/renderer/media/websourcebuffer_impl.h"
      6 
      7 #include "base/float_util.h"
      8 #include "media/filters/chunk_demuxer.h"
      9 
     10 namespace content {
     11 
     12 static base::TimeDelta DoubleToTimeDelta(double time) {
     13   DCHECK(!base::IsNaN(time));
     14   DCHECK_GE(time, 0);
     15   if (time == std::numeric_limits<double>::infinity())
     16     return media::kInfiniteDuration();
     17 
     18   base::TimeDelta max_time = base::TimeDelta::FromInternalValue(kint64max - 1);
     19   double max_time_in_seconds = max_time.InSecondsF();
     20 
     21   if (time >= max_time_in_seconds)
     22     return max_time;
     23 
     24   return base::TimeDelta::FromMicroseconds(
     25       time * base::Time::kMicrosecondsPerSecond);
     26 }
     27 
     28 WebSourceBufferImpl::WebSourceBufferImpl(
     29     const std::string& id, media::ChunkDemuxer* demuxer)
     30     : id_(id),
     31       demuxer_(demuxer) {
     32   DCHECK(demuxer_);
     33 }
     34 
     35 WebSourceBufferImpl::~WebSourceBufferImpl() {
     36   DCHECK(!demuxer_) << "Object destroyed w/o removedFromMediaSource() call";
     37 }
     38 
     39 blink::WebTimeRanges WebSourceBufferImpl::buffered() {
     40   media::Ranges<base::TimeDelta> ranges = demuxer_->GetBufferedRanges(id_);
     41   blink::WebTimeRanges result(ranges.size());
     42   for (size_t i = 0; i < ranges.size(); i++) {
     43     result[i].start = ranges.start(i).InSecondsF();
     44     result[i].end = ranges.end(i).InSecondsF();
     45   }
     46   return result;
     47 }
     48 
     49 void WebSourceBufferImpl::append(const unsigned char* data, unsigned length) {
     50   demuxer_->AppendData(id_, data, length);
     51 }
     52 
     53 void WebSourceBufferImpl::abort() {
     54   demuxer_->Abort(id_);
     55 }
     56 
     57 void WebSourceBufferImpl::remove(double start, double end) {
     58   demuxer_->Remove(id_, DoubleToTimeDelta(start), DoubleToTimeDelta(end));
     59 }
     60 
     61 bool WebSourceBufferImpl::setTimestampOffset(double offset) {
     62   base::TimeDelta time_offset = base::TimeDelta::FromMicroseconds(
     63       offset * base::Time::kMicrosecondsPerSecond);
     64   return demuxer_->SetTimestampOffset(id_, time_offset);
     65 }
     66 
     67 void WebSourceBufferImpl::setAppendWindowStart(double start) {
     68   demuxer_->SetAppendWindowStart(id_, DoubleToTimeDelta(start));
     69 }
     70 
     71 void WebSourceBufferImpl::setAppendWindowEnd(double end) {
     72   demuxer_->SetAppendWindowEnd(id_, DoubleToTimeDelta(end));
     73 }
     74 
     75 void WebSourceBufferImpl::removedFromMediaSource() {
     76   demuxer_->RemoveId(id_);
     77   demuxer_ = NULL;
     78 }
     79 
     80 }  // namespace content
     81