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_
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"
21 class SingleThreadTaskRunner;
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 {
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.
39 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
40 const AddTextTrackCB& add_text_track_cb);
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, executing |callback| when
48 // playback is underway.
49 void Play(const base::Closure& callback);
51 // Temporarily suspend decoding and rendering, executing |callback| when
52 // playback has been suspended.
53 void Pause(const base::Closure& callback);
55 // Discard any text data, executing |callback| when completed.
56 void Flush(const base::Closure& callback);
58 // Stop all operations in preparation for being deleted, executing |callback|
60 void Stop(const base::Closure& callback);
62 // Add new |text_stream|, having the indicated |config|, to the text stream
63 // collection managed by this text renderer.
64 void AddTextStream(DemuxerStream* text_stream,
65 const TextTrackConfig& config);
67 // Remove |text_stream| from the text stream collection.
68 void RemoveTextStream(DemuxerStream* text_stream);
70 // Returns true if there are extant text tracks.
71 bool HasTracks() const;
74 struct TextTrackState {
75 // To determine read progress.
81 explicit TextTrackState(scoped_ptr<TextTrack> text_track);
85 scoped_ptr<TextTrack> text_track;
86 TextRanges text_ranges_;
89 // Callback delivered by the demuxer |text_stream| when
90 // a read from the stream completes.
91 void BufferReady(DemuxerStream* text_stream,
92 DemuxerStream::Status status,
93 const scoped_refptr<DecoderBuffer>& input);
95 // Dispatches the decoded cue delivered on the demuxer's |text_stream|.
96 void CueReady(DemuxerStream* text_stream,
97 const scoped_refptr<TextCue>& text_cue);
99 // Dispatched when the AddTextTrackCB completes, after having created
100 // the TextTrack object associated with |text_stream|.
101 void OnAddTextTrackDone(DemuxerStream* text_stream,
102 scoped_ptr<TextTrack> text_track);
104 // Utility function to post a read request on |text_stream|.
105 void Read(TextTrackState* state, DemuxerStream* text_stream);
107 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
108 base::WeakPtrFactory<TextRenderer> weak_factory_;
109 base::WeakPtr<TextRenderer> weak_this_;
110 const AddTextTrackCB add_text_track_cb_;
112 // Callbacks provided during Initialize().
113 base::Closure ended_cb_;
115 // Callback provided to Pause().
116 base::Closure pause_cb_;
118 // Callback provided to Stop().
119 base::Closure stop_cb_;
121 // Simple state tracking variable.
133 typedef std::map<DemuxerStream*, TextTrackState*> TextTrackStateMap;
134 TextTrackStateMap text_track_state_map_;
136 // Indicates how many read requests are in flight.
137 int pending_read_count_;
139 // Indicates which text streams have not delivered end-of-stream yet.
140 typedef std::set<DemuxerStream*> PendingEosSet;
141 PendingEosSet pending_eos_set_;
143 DISALLOW_IMPLICIT_CONSTRUCTORS(TextRenderer);
148 #endif // MEDIA_BASE_TEXT_RENDERER_H_