1 // Copyright 2013 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 MEDIA_BASE_TEXT_RENDERER_H_ 6 #define MEDIA_BASE_TEXT_RENDERER_H_ 7 8 #include <map> 9 #include <set> 10 11 #include "base/callback.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/weak_ptr.h" 14 #include "media/base/demuxer_stream.h" 15 #include "media/base/media_export.h" 16 #include "media/base/pipeline_status.h" 17 #include "media/base/text_track.h" 18 19 namespace base { 20 class MessageLoopProxy; 21 } 22 23 namespace media { 24 25 class TextCue; 26 class TextTrackConfig; 27 28 // Receives decoder buffers from the upstream demuxer, decodes them to text 29 // cues, and then passes them onto the TextTrack object associated with each 30 // demuxer text stream. 31 class MEDIA_EXPORT TextRenderer { 32 public: 33 // |message_loop| is the thread on which TextRenderer will execute. 34 // 35 // |add_text_track_cb] is called when the demuxer requests (via its host) 36 // that a new text track be created. 37 TextRenderer(const scoped_refptr<base::MessageLoopProxy>& message_loop, 38 const AddTextTrackCB& add_text_track_cb); 39 ~TextRenderer(); 40 41 // |ended_cb| is executed when all of the text tracks have reached 42 // end of stream, following a play request. 43 void Initialize(const base::Closure& ended_cb); 44 45 // Start text track cue decoding and rendering, executing |callback| when 46 // playback is underway. 47 void Play(const base::Closure& callback); 48 49 // Temporarily suspend decoding and rendering, executing |callback| when 50 // playback has been suspended. 51 void Pause(const base::Closure& callback); 52 53 // Discard any text data, executing |callback| when completed. 54 void Flush(const base::Closure& callback); 55 56 // Stop all operations in preparation for being deleted, executing |callback| 57 // when complete. 58 void Stop(const base::Closure& callback); 59 60 // Add new |text_stream|, having the indicated |config|, to the text stream 61 // collection managed by this text renderer. 62 void AddTextStream(DemuxerStream* text_stream, 63 const TextTrackConfig& config); 64 65 // Remove |text_stream| from the text stream collection. 66 void RemoveTextStream(DemuxerStream* text_stream); 67 68 // Returns true if there are extant text tracks. 69 bool HasTracks() const; 70 71 private: 72 struct TextTrackState { 73 // To determine read progress. 74 enum ReadState { 75 kReadIdle, 76 kReadPending 77 }; 78 79 explicit TextTrackState(scoped_ptr<TextTrack> text_track); 80 ~TextTrackState(); 81 82 ReadState read_state; 83 scoped_ptr<TextTrack> text_track; 84 }; 85 86 // Callback delivered by the demuxer |text_stream| when 87 // a read from the stream completes. 88 void BufferReady(DemuxerStream* text_stream, 89 DemuxerStream::Status status, 90 const scoped_refptr<DecoderBuffer>& input); 91 92 // Dispatches the decoded cue delivered on the demuxer's |text_stream|. 93 void CueReady(DemuxerStream* text_stream, 94 const scoped_refptr<TextCue>& text_cue); 95 96 // Dispatched when the AddTextTrackCB completes, after having created 97 // the TextTrack object associated with |text_stream|. 98 void OnAddTextTrackDone(DemuxerStream* text_stream, 99 scoped_ptr<TextTrack> text_track); 100 101 // Utility function to post a read request on |text_stream|. 102 void Read(TextTrackState* state, DemuxerStream* text_stream); 103 104 scoped_refptr<base::MessageLoopProxy> message_loop_; 105 base::WeakPtrFactory<TextRenderer> weak_factory_; 106 base::WeakPtr<TextRenderer> weak_this_; 107 const AddTextTrackCB add_text_track_cb_; 108 109 // Callbacks provided during Initialize(). 110 base::Closure ended_cb_; 111 112 // Callback provided to Pause(). 113 base::Closure pause_cb_; 114 115 // Callback provided to Stop(). 116 base::Closure stop_cb_; 117 118 // Simple state tracking variable. 119 enum State { 120 kUninitialized, 121 kPausePending, 122 kPaused, 123 kPlaying, 124 kEnded, 125 kStopPending, 126 kStopped 127 }; 128 State state_; 129 130 typedef std::map<DemuxerStream*, TextTrackState*> TextTrackStateMap; 131 TextTrackStateMap text_track_state_map_; 132 133 // Indicates how many read requests are in flight. 134 int pending_read_count_; 135 136 // Indicates which text streams have not delivered end-of-stream yet. 137 typedef std::set<DemuxerStream*> PendingEosSet; 138 PendingEosSet pending_eos_set_; 139 140 DISALLOW_IMPLICIT_CONSTRUCTORS(TextRenderer); 141 }; 142 143 } // namespace media 144 145 #endif // MEDIA_BASE_TEXT_RENDERER_H_ 146