This is jamesr@ code I am landing.
[chromium-blink-merge.git] / media / base / text_renderer.h
blob7e719dbac96f4e80b04421c3199e751f45dcbab3
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.
5 #ifndef MEDIA_BASE_TEXT_RENDERER_H_
6 #define MEDIA_BASE_TEXT_RENDERER_H_
8 #include <map>
9 #include <set>
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_ranges.h"
18 #include "media/base/text_track.h"
20 namespace base {
21 class SingleThreadTaskRunner;
24 namespace media {
26 class TextCue;
27 class TextTrackConfig;
29 // Receives decoder buffers from the upstream demuxer, decodes them to text
30 // cues, and then passes them onto the TextTrack object associated with each
31 // demuxer text stream.
32 class MEDIA_EXPORT TextRenderer {
33 public:
34 // |task_runner| is the thread on which TextRenderer will execute.
36 // |add_text_track_cb] is called when the demuxer requests (via its host)
37 // that a new text track be created.
38 TextRenderer(
39 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
40 const AddTextTrackCB& add_text_track_cb);
41 ~TextRenderer();
43 // |ended_cb| is executed when all of the text tracks have reached
44 // end of stream, following a play request.
45 void Initialize(const base::Closure& ended_cb);
47 // Start text track cue decoding and rendering.
48 void StartPlaying();
50 // Temporarily suspend decoding and rendering, executing |callback| when
51 // playback has been suspended.
52 void Pause(const base::Closure& callback);
54 // Discard any text data, executing |callback| when completed.
55 void Flush(const base::Closure& callback);
57 // Stop all operations in preparation for being deleted, executing |callback|
58 // when complete.
59 void Stop(const base::Closure& callback);
61 // Add new |text_stream|, having the indicated |config|, to the text stream
62 // collection managed by this text renderer.
63 void AddTextStream(DemuxerStream* text_stream,
64 const TextTrackConfig& config);
66 // Remove |text_stream| from the text stream collection.
67 void RemoveTextStream(DemuxerStream* text_stream);
69 // Returns true if there are extant text tracks.
70 bool HasTracks() const;
72 private:
73 struct TextTrackState {
74 // To determine read progress.
75 enum ReadState {
76 kReadIdle,
77 kReadPending
80 explicit TextTrackState(scoped_ptr<TextTrack> text_track);
81 ~TextTrackState();
83 ReadState read_state;
84 scoped_ptr<TextTrack> text_track;
85 TextRanges text_ranges_;
88 // Callback delivered by the demuxer |text_stream| when
89 // a read from the stream completes.
90 void BufferReady(DemuxerStream* text_stream,
91 DemuxerStream::Status status,
92 const scoped_refptr<DecoderBuffer>& input);
94 // Dispatches the decoded cue delivered on the demuxer's |text_stream|.
95 void CueReady(DemuxerStream* text_stream,
96 const scoped_refptr<TextCue>& text_cue);
98 // Dispatched when the AddTextTrackCB completes, after having created
99 // the TextTrack object associated with |text_stream|.
100 void OnAddTextTrackDone(DemuxerStream* text_stream,
101 scoped_ptr<TextTrack> text_track);
103 // Utility function to post a read request on |text_stream|.
104 void Read(TextTrackState* state, DemuxerStream* text_stream);
106 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
107 const AddTextTrackCB add_text_track_cb_;
109 // Callbacks provided during Initialize().
110 base::Closure ended_cb_;
112 // Callback provided to Pause().
113 base::Closure pause_cb_;
115 // Callback provided to Stop().
116 base::Closure stop_cb_;
118 // Simple state tracking variable.
119 enum State {
120 kUninitialized,
121 kPausePending,
122 kPaused,
123 kPlaying,
124 kEnded,
125 kStopPending,
126 kStopped
128 State state_;
130 typedef std::map<DemuxerStream*, TextTrackState*> TextTrackStateMap;
131 TextTrackStateMap text_track_state_map_;
133 // Indicates how many read requests are in flight.
134 int pending_read_count_;
136 // Indicates which text streams have not delivered end-of-stream yet.
137 typedef std::set<DemuxerStream*> PendingEosSet;
138 PendingEosSet pending_eos_set_;
140 // NOTE: Weak pointers must be invalidated before all other member variables.
141 base::WeakPtrFactory<TextRenderer> weak_factory_;
143 DISALLOW_IMPLICIT_CONSTRUCTORS(TextRenderer);
146 } // namespace media
148 #endif // MEDIA_BASE_TEXT_RENDERER_H_