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_track.h"
20 class SingleThreadTaskRunner
;
26 class TextTrackConfig
;
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
{
33 // |task_runner| is the thread on which TextRenderer will execute.
35 // |add_text_track_cb] is called when the demuxer requests (via its host)
36 // that a new text track be created.
38 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
39 const AddTextTrackCB
& add_text_track_cb
);
42 // |ended_cb| is executed when all of the text tracks have reached
43 // end of stream, following a play request.
44 void Initialize(const base::Closure
& ended_cb
);
46 // Start text track cue decoding and rendering, executing |callback| when
47 // playback is underway.
48 void Play(const base::Closure
& callback
);
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|
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;
73 struct TextTrackState
{
74 // To determine read progress.
80 explicit TextTrackState(scoped_ptr
<TextTrack
> text_track
);
84 scoped_ptr
<TextTrack
> text_track
;
87 // Callback delivered by the demuxer |text_stream| when
88 // a read from the stream completes.
89 void BufferReady(DemuxerStream
* text_stream
,
90 DemuxerStream::Status status
,
91 const scoped_refptr
<DecoderBuffer
>& input
);
93 // Dispatches the decoded cue delivered on the demuxer's |text_stream|.
94 void CueReady(DemuxerStream
* text_stream
,
95 const scoped_refptr
<TextCue
>& text_cue
);
97 // Dispatched when the AddTextTrackCB completes, after having created
98 // the TextTrack object associated with |text_stream|.
99 void OnAddTextTrackDone(DemuxerStream
* text_stream
,
100 scoped_ptr
<TextTrack
> text_track
);
102 // Utility function to post a read request on |text_stream|.
103 void Read(TextTrackState
* state
, DemuxerStream
* text_stream
);
105 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
106 base::WeakPtrFactory
<TextRenderer
> weak_factory_
;
107 base::WeakPtr
<TextRenderer
> weak_this_
;
108 const AddTextTrackCB add_text_track_cb_
;
110 // Callbacks provided during Initialize().
111 base::Closure ended_cb_
;
113 // Callback provided to Pause().
114 base::Closure pause_cb_
;
116 // Callback provided to Stop().
117 base::Closure stop_cb_
;
119 // Simple state tracking variable.
131 typedef std::map
<DemuxerStream
*, TextTrackState
*> TextTrackStateMap
;
132 TextTrackStateMap text_track_state_map_
;
134 // Indicates how many read requests are in flight.
135 int pending_read_count_
;
137 // Indicates which text streams have not delivered end-of-stream yet.
138 typedef std::set
<DemuxerStream
*> PendingEosSet
;
139 PendingEosSet pending_eos_set_
;
141 DISALLOW_IMPLICIT_CONSTRUCTORS(TextRenderer
);
146 #endif // MEDIA_BASE_TEXT_RENDERER_H_