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