Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / base / text_renderer.h
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_ranges.h"
18 #include "media/base/text_track.h"
19
20 namespace base {
21 class SingleThreadTaskRunner;
22 }
23
24 namespace media {
25
26 class TextCue;
27 class TextTrackConfig;
28
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.
35   //
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();
42
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);
46
47   // Start text track cue decoding and rendering, executing |callback| when
48   // playback is underway.
49   void Play(const base::Closure& callback);
50
51   // Temporarily suspend decoding and rendering, executing |callback| when
52   // playback has been suspended.
53   void Pause(const base::Closure& callback);
54
55   // Discard any text data, executing |callback| when completed.
56   void Flush(const base::Closure& callback);
57
58   // Stop all operations in preparation for being deleted, executing |callback|
59   // when complete.
60   void Stop(const base::Closure& callback);
61
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);
66
67   // Remove |text_stream| from the text stream collection.
68   void RemoveTextStream(DemuxerStream* text_stream);
69
70   // Returns true if there are extant text tracks.
71   bool HasTracks() const;
72
73  private:
74   struct TextTrackState {
75     // To determine read progress.
76     enum ReadState {
77       kReadIdle,
78       kReadPending
79     };
80
81     explicit TextTrackState(scoped_ptr<TextTrack> text_track);
82     ~TextTrackState();
83
84     ReadState read_state;
85     scoped_ptr<TextTrack> text_track;
86     TextRanges text_ranges_;
87   };
88
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);
94
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);
98
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);
103
104   // Utility function to post a read request on |text_stream|.
105   void Read(TextTrackState* state, DemuxerStream* text_stream);
106
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_;
111
112   // Callbacks provided during Initialize().
113   base::Closure ended_cb_;
114
115   // Callback provided to Pause().
116   base::Closure pause_cb_;
117
118   // Callback provided to Stop().
119   base::Closure stop_cb_;
120
121   // Simple state tracking variable.
122   enum State {
123     kUninitialized,
124     kPausePending,
125     kPaused,
126     kPlaying,
127     kEnded,
128     kStopPending,
129     kStopped
130   };
131   State state_;
132
133   typedef std::map<DemuxerStream*, TextTrackState*> TextTrackStateMap;
134   TextTrackStateMap text_track_state_map_;
135
136   // Indicates how many read requests are in flight.
137   int pending_read_count_;
138
139   // Indicates which text streams have not delivered end-of-stream yet.
140   typedef std::set<DemuxerStream*> PendingEosSet;
141   PendingEosSet pending_eos_set_;
142
143   DISALLOW_IMPLICIT_CONSTRUCTORS(TextRenderer);
144 };
145
146 }  // namespace media
147
148 #endif  // MEDIA_BASE_TEXT_RENDERER_H_