summaryrefslogtreecommitdiffstats
path: root/media/base/buffer_queue.cc
blob: 26317c2308940425a4cb78b3eea4fae06712e3b0 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) 2009 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/buffer_queue.h"

#include "media/base/buffers.h"

namespace media {

BufferQueue::BufferQueue()
    : data_offset_(0),
      size_in_bytes_(0),
      most_recent_time_() {
}

BufferQueue::~BufferQueue() {
}

void BufferQueue::Consume(size_t bytes_to_be_consumed) {
  // Make sure user isn't trying to consume more than we have.
  DCHECK(size_in_bytes_ >= bytes_to_be_consumed);

  // As we have enough data to consume, adjust |size_in_bytes_|.
  size_in_bytes_ -= bytes_to_be_consumed;

  // Now consume them.
  while (bytes_to_be_consumed > 0) {
    // Calculate number of usable bytes in the front of the |queue_|.
    size_t front_remaining = queue_.front()->GetDataSize() - data_offset_;

    // If there is enough data in our first buffer to advance into it, do so.
    // Otherwise, advance into the queue.
    if (front_remaining > bytes_to_be_consumed) {
      data_offset_ += bytes_to_be_consumed;
      bytes_to_be_consumed = 0;
      // Garbage values are unavoidable, so this check will remain.
      if (queue_.front()->GetTimestamp().InMicroseconds() > 0) {
        int64 offset = (queue_.front()->GetDuration().InMicroseconds() *
            data_offset_) / queue_.front()->GetDataSize();

        most_recent_time_ = queue_.front()->GetTimestamp() +
            base::TimeDelta::FromMicroseconds(offset);
      }
    } else {
      data_offset_ = 0;
      // Garbage values are unavoidable, so this check will remain.
      if (queue_.front()->GetTimestamp().InMicroseconds() > 0) {
        most_recent_time_ = queue_.front()->GetTimestamp() +
            queue_.front()->GetDuration();
      }
      queue_.pop_front();
      bytes_to_be_consumed -= front_remaining;
    }
  }
}

size_t BufferQueue::Copy(uint8* dest, size_t bytes) {
  if (bytes == 0)
    return 0;

  DCHECK(!queue_.empty());

  size_t current_remaining = 0;
  const uint8* current = NULL;
  size_t copied = 0;

  for (size_t i = 0; i < queue_.size() && bytes > 0; ++i) {
    // Calculate number of usable bytes in the front of the |queue_|. Special
    // case for front due to |data_offset_|.
    if (i == 0) {
      current_remaining = queue_.front()->GetDataSize() - data_offset_;
      current = queue_.front()->GetData() + data_offset_;
    } else {
      current_remaining = queue_[i]->GetDataSize();
      current = queue_[i]->GetData();
    }

    // Prevent writing over the end of the buffer.
    if (current_remaining > bytes)
      current_remaining = bytes;

    memcpy(dest + copied, current, current_remaining);

    // Modify counts and pointers.
    copied += current_remaining;
    bytes -= current_remaining;
  }
  return copied;
}

void BufferQueue::Enqueue(Buffer* buffer_in) {
  if (queue_.empty() && buffer_in->GetTimestamp().InMicroseconds() > 0) {
    most_recent_time_ = buffer_in->GetTimestamp();
  }
  queue_.push_back(buffer_in);
  size_in_bytes_ += buffer_in->GetDataSize();
}

base::TimeDelta BufferQueue::GetTime() {
  return most_recent_time_;
}

void BufferQueue::Clear() {
  queue_.clear();
  size_in_bytes_ = 0;
  data_offset_ = 0;
  most_recent_time_ = base::TimeDelta();
}

bool BufferQueue::IsEmpty() {
  // Since we keep track of the number of bytes, this is easier than calling
  // into |queue_|.
  return size_in_bytes_ == 0;
}

size_t BufferQueue::SizeInBytes() {
  return size_in_bytes_;
}

}  // namespace media