Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chromecast / media / cma / base / buffering_frame_provider.cc
blobab2af87da879d63fc60b4d98239dd629f7d5fda6
1 // Copyright 2014 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.
5 #include "chromecast/media/cma/base/buffering_frame_provider.h"
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "chromecast/media/cma/base/buffering_state.h"
10 #include "chromecast/media/cma/base/decoder_buffer_base.h"
11 #include "media/base/bind_to_current_loop.h"
13 namespace chromecast {
14 namespace media {
16 BufferingFrameProvider::BufferWithConfig::BufferWithConfig(
17 const scoped_refptr<DecoderBufferBase>& buffer,
18 const ::media::AudioDecoderConfig& audio_config,
19 const ::media::VideoDecoderConfig& video_config)
20 : buffer_(buffer),
21 audio_config_(audio_config),
22 video_config_(video_config) {
25 BufferingFrameProvider::BufferWithConfig::~BufferWithConfig() {
28 BufferingFrameProvider::BufferingFrameProvider(
29 scoped_ptr<CodedFrameProvider> coded_frame_provider,
30 size_t max_buffer_size,
31 size_t max_frame_size,
32 const FrameBufferedCB& frame_buffered_cb)
33 : coded_frame_provider_(coded_frame_provider.Pass()),
34 is_pending_request_(false),
35 is_eos_(false),
36 total_buffer_size_(0),
37 max_buffer_size_(max_buffer_size),
38 max_frame_size_(max_frame_size),
39 frame_buffered_cb_(frame_buffered_cb),
40 weak_factory_(this) {
41 DCHECK_LE(max_frame_size, max_buffer_size);
42 weak_this_ = weak_factory_.GetWeakPtr();
43 thread_checker_.DetachFromThread();
46 BufferingFrameProvider::~BufferingFrameProvider() {
47 // Required since some weak pointers might be released in the destructor.
48 DCHECK(thread_checker_.CalledOnValidThread());
51 void BufferingFrameProvider::Read(const ReadCB& read_cb) {
52 DCHECK(thread_checker_.CalledOnValidThread());
54 DCHECK(!read_cb.is_null());
55 read_cb_ = read_cb;
57 CompleteReadIfNeeded();
59 RequestBufferIfNeeded();
62 void BufferingFrameProvider::Flush(const base::Closure& flush_cb) {
63 DCHECK(thread_checker_.CalledOnValidThread());
65 // Invalidate all the buffers that belong to this media timeline.
66 // This is needed since, even though |coded_frame_provider_| is flushed later
67 // in this function, there might be a pending task holding onto a buffer.
68 weak_factory_.InvalidateWeakPtrs();
70 // Create a new valid weak pointer that is used for the next media timeline.
71 weak_this_ = weak_factory_.GetWeakPtr();
73 is_pending_request_ = false;
74 is_eos_ = false;
75 buffer_list_.clear();
76 total_buffer_size_ = 0;
77 read_cb_.Reset();
78 coded_frame_provider_->Flush(flush_cb);
81 void BufferingFrameProvider::OnNewBuffer(
82 const scoped_refptr<DecoderBufferBase>& buffer,
83 const ::media::AudioDecoderConfig& audio_config,
84 const ::media::VideoDecoderConfig& video_config) {
85 is_pending_request_ = false;
86 buffer_list_.push_back(
87 BufferWithConfig(buffer, audio_config, video_config));
89 if (buffer->end_of_stream()) {
90 is_eos_ = true;
91 } else {
92 total_buffer_size_ += buffer->data_size();
95 if (!frame_buffered_cb_.is_null()) {
96 // If the next upcoming frame is possibly filling the whole buffer,
97 // then the buffer is considered as having reached its max capacity.
98 bool max_capacity_flag =
99 (total_buffer_size_ + max_frame_size_ >= max_buffer_size_);
100 frame_buffered_cb_.Run(buffer, max_capacity_flag);
103 RequestBufferIfNeeded();
105 CompleteReadIfNeeded();
108 void BufferingFrameProvider::RequestBufferIfNeeded() {
109 if (is_pending_request_)
110 return;
112 if (is_eos_ || total_buffer_size_ >= max_buffer_size_)
113 return;
115 is_pending_request_ = true;
116 coded_frame_provider_->Read(::media::BindToCurrentLoop(
117 base::Bind(&BufferingFrameProvider::OnNewBuffer, weak_this_)));
120 void BufferingFrameProvider::CompleteReadIfNeeded() {
121 if (read_cb_.is_null())
122 return;
124 if (buffer_list_.empty())
125 return;
127 BufferWithConfig buffer_with_config(buffer_list_.front());
128 buffer_list_.pop_front();
129 if (!buffer_with_config.buffer()->end_of_stream())
130 total_buffer_size_ -= buffer_with_config.buffer()->data_size();
132 base::ResetAndReturn(&read_cb_).Run(
133 buffer_with_config.buffer(),
134 buffer_with_config.audio_config(),
135 buffer_with_config.video_config());
138 } // namespace media
139 } // namespace chromecast