1 // Copyright (c) 2012 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 "media/base/decoder_buffer_queue.h"
6 
7 #include "base/logging.h"
8 #include "media/base/decoder_buffer.h"
9 #include "media/base/timestamp_constants.h"
10 
11 namespace media {
12 
DecoderBufferQueue()13 DecoderBufferQueue::DecoderBufferQueue()
14     : earliest_valid_timestamp_(kNoTimestamp), data_size_(0) {}
15 
16 DecoderBufferQueue::~DecoderBufferQueue() = default;
17 
18 // TODO(dalecurtis): This whole class can be simplified significantly; there's
19 // no reason to track an in order queue, instead track min + max timestamps.
Push(scoped_refptr<DecoderBuffer> buffer)20 void DecoderBufferQueue::Push(scoped_refptr<DecoderBuffer> buffer) {
21   DCHECK(!buffer->end_of_stream());
22 
23   queue_.push_back(buffer);
24   data_size_ += buffer->data_size();
25 
26   // TODO(scherkus): FFmpeg returns some packets with no timestamp after
27   // seeking. Fix and turn this into CHECK(). See http://crbug.com/162192
28   if (buffer->timestamp() == kNoTimestamp) {
29     DVLOG(1) << "Buffer has no timestamp";
30     return;
31   }
32 
33   if (earliest_valid_timestamp_ == kNoTimestamp) {
34     earliest_valid_timestamp_ = buffer->timestamp();
35   }
36 
37   if (buffer->timestamp() < earliest_valid_timestamp_) {
38     DVLOG(2) << "Out of order timestamps: "
39              << buffer->timestamp().InMicroseconds() << " vs. "
40              << earliest_valid_timestamp_.InMicroseconds();
41     return;
42   }
43 
44   earliest_valid_timestamp_ = buffer->timestamp();
45   in_order_queue_.emplace_back(std::move(buffer));
46 }
47 
Pop()48 scoped_refptr<DecoderBuffer> DecoderBufferQueue::Pop() {
49   scoped_refptr<DecoderBuffer> buffer = std::move(queue_.front());
50   queue_.pop_front();
51 
52   size_t buffer_data_size = buffer->data_size();
53   DCHECK_LE(buffer_data_size, data_size_);
54   data_size_ -= buffer_data_size;
55 
56   if (!in_order_queue_.empty() && in_order_queue_.front() == buffer)
57     in_order_queue_.pop_front();
58 
59   return buffer;
60 }
61 
Clear()62 void DecoderBufferQueue::Clear() {
63   queue_.clear();
64   data_size_ = 0;
65   in_order_queue_.clear();
66   earliest_valid_timestamp_ = kNoTimestamp;
67 }
68 
IsEmpty()69 bool DecoderBufferQueue::IsEmpty() {
70   return queue_.empty();
71 }
72 
Duration()73 base::TimeDelta DecoderBufferQueue::Duration() {
74   if (in_order_queue_.size() < 2)
75     return base::TimeDelta();
76 
77   base::TimeDelta start = in_order_queue_.front()->timestamp();
78   base::TimeDelta end = in_order_queue_.back()->timestamp();
79   return end - start;
80 }
81 
82 }  // namespace media
83