[MediaRouter] Update MR-2-Extension's PostMessage to return boolean.
[chromium-blink-merge.git] / chromecast / media / cma / base / buffering_frame_provider.h
blob0cb99e2c59192de8e4c35e8155197e5ab3af24f3
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 #ifndef CHROMECAST_MEDIA_CMA_BASE_BUFFERING_FRAME_PROVIDER_H_
6 #define CHROMECAST_MEDIA_CMA_BASE_BUFFERING_FRAME_PROVIDER_H_
8 #include <list>
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/threading/thread_checker.h"
14 #include "chromecast/media/cma/base/coded_frame_provider.h"
15 #include "media/base/audio_decoder_config.h"
16 #include "media/base/video_decoder_config.h"
18 namespace chromecast {
19 namespace media {
20 class DecoderBufferBase;
22 // BufferingFrameProvider -
23 // Fetch some data from another CodedFrameProvider up to a certain size limit.
24 class BufferingFrameProvider : public CodedFrameProvider {
25 public:
26 typedef base::Callback<void(const scoped_refptr<DecoderBufferBase>&, bool)>
27 FrameBufferedCB;
29 // Creates a frame provider that buffers coded frames up to the
30 // |max_buffer_size| limit (given as a number of bytes).
31 // |max_frame_size| corresponds to an upper bound of the expected frame size.
32 // Each time a frame is buffered, |frame_buffered_cb| is invoked with the
33 // last frame buffered. The second parameter of the callback indicates
34 // whether the maximum capacity has been reached, i.e. whether the next frame
35 // size might overflow the buffer: |total_buffer_size_| + next_frame_size
36 // might be greater than |max_buffer_size|.
37 // Note: takes ownership of |coded_frame_provider|.
38 BufferingFrameProvider(
39 scoped_ptr<CodedFrameProvider> coded_frame_provider,
40 size_t max_buffer_size,
41 size_t max_frame_size,
42 const FrameBufferedCB& frame_buffered_cb);
43 ~BufferingFrameProvider() override;
45 // CodedFrameProvider implementation.
46 void Read(const ReadCB& read_cb) override;
47 void Flush(const base::Closure& flush_cb) override;
49 private:
50 class BufferWithConfig {
51 public:
52 BufferWithConfig(
53 const scoped_refptr<DecoderBufferBase>& buffer,
54 const ::media::AudioDecoderConfig& audio_config,
55 const ::media::VideoDecoderConfig& video_config);
56 ~BufferWithConfig();
58 const scoped_refptr<DecoderBufferBase>& buffer() const { return buffer_; }
59 const ::media::AudioDecoderConfig& audio_config() const {
60 return audio_config_;
62 const ::media::VideoDecoderConfig& video_config() const {
63 return video_config_;
66 private:
67 scoped_refptr<DecoderBufferBase> buffer_;
68 ::media::AudioDecoderConfig audio_config_;
69 ::media::VideoDecoderConfig video_config_;
72 void OnNewBuffer(const scoped_refptr<DecoderBufferBase>& buffer,
73 const ::media::AudioDecoderConfig& audio_config,
74 const ::media::VideoDecoderConfig& video_config);
75 void RequestBufferIfNeeded();
76 void CompleteReadIfNeeded();
78 base::ThreadChecker thread_checker_;
80 // Frame provider the buffering frame provider fetches data from.
81 scoped_ptr<CodedFrameProvider> coded_frame_provider_;
83 // Indicates whether there is a pending read request on
84 // |coded_frame_provider_|.
85 bool is_pending_request_;
87 // Indicates whether the end of stream has been reached.
88 bool is_eos_;
90 std::list<BufferWithConfig> buffer_list_;
92 // Size in bytes of audio/video buffers in |buffer_list_|.
93 size_t total_buffer_size_;
95 // Max amount of data to buffer.
96 // i.e. this is the maximum size of buffers in |buffer_list_|.
97 const size_t max_buffer_size_;
99 // Maximum expected frame size.
100 const size_t max_frame_size_;
102 // Callback invoked each time there is a new frame buffered.
103 FrameBufferedCB frame_buffered_cb_;
105 // Pending read callback.
106 ReadCB read_cb_;
108 base::WeakPtr<BufferingFrameProvider> weak_this_;
109 base::WeakPtrFactory<BufferingFrameProvider> weak_factory_;
111 DISALLOW_COPY_AND_ASSIGN(BufferingFrameProvider);
114 } // namespace media
115 } // namespace chromecast
117 #endif // CHROMECAST_MEDIA_CMA_BASE_BUFFERING_FRAME_PROVIDER_H_