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. 4 5 #ifndef CHROMECAST_MEDIA_CMA_BASE_BUFFERING_FRAME_PROVIDER_H_ 6 #define CHROMECAST_MEDIA_CMA_BASE_BUFFERING_FRAME_PROVIDER_H_ 7 8 #include <list> 9 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" 17 18 namespace chromecast { 19 namespace media { 20 class DecoderBufferBase; 21 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; 28 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 virtual ~BufferingFrameProvider(); 44 45 // CodedFrameProvider implementation. 46 virtual void Read(const ReadCB& read_cb) OVERRIDE; 47 virtual void Flush(const base::Closure& flush_cb) OVERRIDE; 48 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(); 57 58 const scoped_refptr<DecoderBufferBase>& buffer() const { return buffer_; } 59 const ::media::AudioDecoderConfig& audio_config() const { 60 return audio_config_; 61 } 62 const ::media::VideoDecoderConfig& video_config() const { 63 return video_config_; 64 } 65 66 private: 67 scoped_refptr<DecoderBufferBase> buffer_; 68 ::media::AudioDecoderConfig audio_config_; 69 ::media::VideoDecoderConfig video_config_; 70 }; 71 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(); 77 78 base::ThreadChecker thread_checker_; 79 80 // Frame provider the buffering frame provider fetches data from. 81 scoped_ptr<CodedFrameProvider> coded_frame_provider_; 82 83 // Indicates whether there is a pending read request on 84 // |coded_frame_provider_|. 85 bool is_pending_request_; 86 87 // Indicates whether the end of stream has been reached. 88 bool is_eos_; 89 90 std::list<BufferWithConfig> buffer_list_; 91 92 // Size in bytes of audio/video buffers in |buffer_list_|. 93 size_t total_buffer_size_; 94 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_; 98 99 // Maximum expected frame size. 100 const size_t max_frame_size_; 101 102 // Callback invoked each time there is a new frame buffered. 103 FrameBufferedCB frame_buffered_cb_; 104 105 // Pending read callback. 106 ReadCB read_cb_; 107 108 base::WeakPtrFactory<BufferingFrameProvider> weak_factory_; 109 base::WeakPtr<BufferingFrameProvider> weak_this_; 110 111 DISALLOW_COPY_AND_ASSIGN(BufferingFrameProvider); 112 }; 113 114 } // namespace media 115 } // namespace chromecast 116 117 #endif // CHROMECAST_MEDIA_CMA_BASE_BUFFERING_FRAME_PROVIDER_H_ 118