summaryrefslogtreecommitdiff
path: root/chromium/media/base/android/access_unit_queue.cc
blob: 151e4aa885fedf6141c4eff5d5f7362dae972872 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2015 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/android/access_unit_queue.h"

#include "base/logging.h"
#include "base/stl_util.h"
#include "media/base/demuxer_stream.h"

namespace media {

namespace {
// Amount of history chunks we keep by default. The zero size means we do not
// keep chunks before the current one and the history is limited by the size
// of one chunk.
const int kDefaultHistoryChunksAmount = 0;
}

AccessUnitQueue::AccessUnitQueue()
    : index_in_chunk_(0),
      history_chunks_amount_(kDefaultHistoryChunksAmount),
      has_eos_(false) {
  current_chunk_ = chunks_.end();
}

AccessUnitQueue::~AccessUnitQueue() {
  STLDeleteContainerPointers(chunks_.begin(), chunks_.end());
}

void AccessUnitQueue::PushBack(const DemuxerData& data) {
  // Media thread
  DCHECK(!data.access_units.empty());

#if DCHECK_IS_ON()
  // If there is an AU with |kConfigChanged| status, it must be the last
  // AU in the chunk and the data should have exactly one corresponding
  // DemuxerConfigs.
  for (size_t i = 0; i < data.access_units.size(); ++i) {
    const AccessUnit& unit = data.access_units[i];

    // EOS must be the last unit in the chunk.
    if (unit.is_end_of_stream) {
      DCHECK(i == data.access_units.size() - 1);
    }

    // kConfigChanged must be the last unit in the chunk.
    if (unit.status == DemuxerStream::kConfigChanged) {
      DCHECK(i == data.access_units.size() - 1);
      DCHECK(data.demuxer_configs.size() == 1);
    }

    if (unit.status == DemuxerStream::kAborted) {
      DVLOG(1) << "AccessUnitQueue::" << __FUNCTION__ << " kAborted";
    }
  }
#endif

  // Create the next chunk and copy data to it.
  DemuxerData* chunk = new DemuxerData(data);

  // EOS flag can only be in the last access unit.
  bool has_eos = chunk->access_units.back().is_end_of_stream;

  // Append this chunk to the queue.
  base::AutoLock lock(lock_);

  // Ignore the input after we have received EOS.
  if (has_eos_) {
    delete chunk;
    return;
  }

  bool was_empty = (current_chunk_ == chunks_.end());

  // The container |chunks_| will own the chunk.
  chunks_.push_back(chunk);

  // Position the current chunk.
  if (was_empty) {
    current_chunk_ = --chunks_.end();
    index_in_chunk_ = 0;
  }

  // We expect that the chunk containing EOS is the last chunk.
  DCHECK(!has_eos_);
  has_eos_ = has_eos;
}

void AccessUnitQueue::Advance() {
  // Decoder thread
  base::AutoLock lock(lock_);

  if (current_chunk_ == chunks_.end())
    return;

  ++index_in_chunk_;
  if (index_in_chunk_ < (*current_chunk_)->access_units.size())
    return;

  index_in_chunk_ = 0;
  ++current_chunk_;

  // Keep only |history_chunks_amount_| before the current one.
  // std::distance() and std::advance() do not work efficiently with std::list,
  // but the history_size should be small (default is 0).
  size_t num_consumed_chunks = std::distance(chunks_.begin(), current_chunk_);
  if (num_consumed_chunks > history_chunks_amount_) {
    DataChunkQueue::iterator first_to_keep = chunks_.begin();
    std::advance(first_to_keep, num_consumed_chunks - history_chunks_amount_);
    STLDeleteContainerPointers(chunks_.begin(), first_to_keep);
    chunks_.erase(chunks_.begin(), first_to_keep);
  }
}

void AccessUnitQueue::Flush() {
  // Media thread
  base::AutoLock lock(lock_);

  STLDeleteContainerPointers(chunks_.begin(), chunks_.end());
  chunks_.clear();

  current_chunk_ = chunks_.end();
  index_in_chunk_ = 0;
  has_eos_ = false;
}

bool AccessUnitQueue::RewindToLastKeyFrame() {
  // Media thread
  base::AutoLock lock(lock_);

  // Search for the key frame backwards. Start with the current AU.

  // Start with current chunk.
  if (current_chunk_ != chunks_.end()) {
    for (int i = (int)index_in_chunk_; i >= 0; --i) {
      if ((*current_chunk_)->access_units[i].is_key_frame) {
        index_in_chunk_ = i;
        return true;
      }
    }
  }

  // Position reverse iterator before the current chunk.
  DataChunkQueue::reverse_iterator rchunk(current_chunk_);

  for (; rchunk != chunks_.rend(); ++rchunk) {
    int i = (int)(*rchunk)->access_units.size() - 1;
    for (; i >= 0; --i) {
      if ((*rchunk)->access_units[i].is_key_frame) {
        index_in_chunk_ = i;
        current_chunk_ = --rchunk.base();
        return true;
      }
    }
  }

  return false;
}

AccessUnitQueue::Info AccessUnitQueue::GetInfo() const {
  // Media thread, Decoder thread

  Info info;
  base::AutoLock lock(lock_);

  GetUnconsumedAccessUnitLength(&info.length, &info.data_length);

  info.has_eos = has_eos_;
  info.front_unit = nullptr;
  info.configs = nullptr;

  if (info.length > 0) {
    DCHECK(current_chunk_ != chunks_.end());
    DCHECK(index_in_chunk_ < (*current_chunk_)->access_units.size());
    info.front_unit = &(*current_chunk_)->access_units[index_in_chunk_];

    if (info.front_unit->status == DemuxerStream::kConfigChanged) {
      DCHECK((*current_chunk_)->demuxer_configs.size() == 1);
      info.configs = &(*current_chunk_)->demuxer_configs[0];
    }
  }
  return info;
}

void AccessUnitQueue::SetHistorySizeForTesting(size_t history_chunks_amount) {
  history_chunks_amount_ = history_chunks_amount;
}

void AccessUnitQueue::GetUnconsumedAccessUnitLength(int* total_length,
                                                    int* data_length) const {
  *total_length = *data_length = 0;

  DataChunkQueue::const_iterator chunk;
  for (chunk = current_chunk_; chunk != chunks_.end(); ++chunk) {
    size_t chunk_size = (*chunk)->access_units.size();
    *total_length += chunk_size;
    *data_length += chunk_size;

    // Do not count configuration changes for |data_length|.
    if (!(*chunk)->demuxer_configs.empty()) {
      DCHECK((*chunk)->demuxer_configs.size() == 1);
      --(*data_length);
    }
  }

  *total_length -= index_in_chunk_;
  *data_length -= index_in_chunk_;
}

}  // namespace media