summaryrefslogtreecommitdiffstats
path: root/media/base/decoder_buffer_queue.cc
blob: ab24c2dcc252b658d725d86513673a6d581ef705 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/decoder_buffer_queue.h"

#include "base/logging.h"
#include "media/base/buffers.h"
#include "media/base/decoder_buffer.h"

namespace media {

DecoderBufferQueue::DecoderBufferQueue()
    : earliest_valid_timestamp_(kNoTimestamp()) {
}

DecoderBufferQueue::~DecoderBufferQueue() {}

void DecoderBufferQueue::Push(const scoped_refptr<DecoderBuffer>& buffer) {
  CHECK(!buffer->IsEndOfStream());

  queue_.push_back(buffer);

  // TODO(scherkus): FFmpeg returns some packets with no timestamp after
  // seeking. Fix and turn this into CHECK(). See http://crbug.com/162192
  if (buffer->GetTimestamp() == kNoTimestamp()) {
    DVLOG(1) << "Buffer has no timestamp";
    return;
  }

  if (earliest_valid_timestamp_ == kNoTimestamp()) {
    earliest_valid_timestamp_ = buffer->GetTimestamp();
  }

  if (buffer->GetTimestamp() < earliest_valid_timestamp_) {
    DVLOG(1)
        << "Out of order timestamps: "
        << buffer->GetTimestamp().InMicroseconds()
        << " vs. "
        << earliest_valid_timestamp_.InMicroseconds();
    return;
  }

  earliest_valid_timestamp_ = buffer->GetTimestamp();
  in_order_queue_.push_back(buffer);
}

scoped_refptr<DecoderBuffer> DecoderBufferQueue::Pop() {
  scoped_refptr<DecoderBuffer> buffer = queue_.front();
  queue_.pop_front();

  if (!in_order_queue_.empty() &&
      in_order_queue_.front() == buffer) {
    in_order_queue_.pop_front();
  }

  return buffer;
}

void DecoderBufferQueue::Clear() {
  queue_.clear();
  in_order_queue_.clear();
  earliest_valid_timestamp_ = kNoTimestamp();
}

bool DecoderBufferQueue::IsEmpty() {
  return queue_.empty();
}

base::TimeDelta DecoderBufferQueue::Duration() {
  if (in_order_queue_.size() < 2)
    return base::TimeDelta();

  base::TimeDelta start = in_order_queue_.front()->GetTimestamp();
  base::TimeDelta end = in_order_queue_.back()->GetTimestamp();
  return end - start;
}

}  // namespace media