1 // Copyright (c) 2012 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_FILTERS_CHUNK_DEMUXER_H_
6 #define MEDIA_FILTERS_CHUNK_DEMUXER_H_
13 #include "base/synchronization/lock.h"
14 #include "media/base/byte_queue.h"
15 #include "media/base/demuxer.h"
16 #include "media/base/ranges.h"
17 #include "media/base/stream_parser.h"
18 #include "media/filters/source_buffer_stream.h"
22 class ChunkDemuxerStream
;
23 class FFmpegURLProtocol
;
25 // Demuxer implementation that allows chunks of media data to be passed
26 // from JavaScript to the media stack.
27 class MEDIA_EXPORT ChunkDemuxer
: public Demuxer
{
30 kOk
, // ID added w/o error.
31 kNotSupported
, // Type specified is not supported.
32 kReachedIdLimit
, // Reached ID limit. We can't handle any more IDs.
35 typedef base::Callback
<void(const std::string
& type
,
36 scoped_array
<uint8
> init_data
,
37 int init_data_size
)> NeedKeyCB
;
39 // |open_cb| Run when Initialize() is called to signal that the demuxer
40 // is ready to receive media data via AppenData().
41 // |need_key_cb| Run when the demuxer determines that an encryption key is
42 // needed to decrypt the content.
43 // |log_cb| Run when parsing error messages need to be logged to the error
45 ChunkDemuxer(const base::Closure
& open_cb
, const NeedKeyCB
& need_key_cb
,
48 // Demuxer implementation.
49 virtual void Initialize(DemuxerHost
* host
,
50 const PipelineStatusCB
& cb
) OVERRIDE
;
51 virtual void Stop(const base::Closure
& callback
) OVERRIDE
;
52 virtual void Seek(base::TimeDelta time
, const PipelineStatusCB
& cb
) OVERRIDE
;
53 virtual void OnAudioRendererDisabled() OVERRIDE
;
54 virtual scoped_refptr
<DemuxerStream
> GetStream(
55 DemuxerStream::Type type
) OVERRIDE
;
56 virtual base::TimeDelta
GetStartTime() const OVERRIDE
;
58 // Methods used by an external object to control this demuxer.
59 void StartWaitingForSeek();
60 void CancelPendingSeek();
62 // Registers a new |id| to use for AppendData() calls. |type| indicates
63 // the MIME type for the data that we intend to append for this ID.
64 // kOk is returned if the demuxer has enough resources to support another ID
65 // and supports the format indicated by |type|.
66 // kNotSupported is returned if |type| is not a supported format.
67 // kReachedIdLimit is returned if the demuxer cannot handle another ID right
69 Status
AddId(const std::string
& id
, const std::string
& type
,
70 std::vector
<std::string
>& codecs
);
72 // Removed an ID & associated resources that were previously added with
74 void RemoveId(const std::string
& id
);
76 // Gets the currently buffered ranges for the specified ID.
77 Ranges
<base::TimeDelta
> GetBufferedRanges(const std::string
& id
) const;
79 // Appends media data to the source buffer associated with |id|. Returns
80 // false if this method is called in an invalid state.
81 bool AppendData(const std::string
& id
, const uint8
* data
, size_t length
);
83 // Aborts parsing the current segment and reset the parser to a state where
84 // it can accept a new segment.
85 void Abort(const std::string
& id
);
87 // Notifies the demuxer that the duration of the media has changed to
89 void SetDuration(base::TimeDelta duration
);
91 // Sets a time |offset| to be applied to subsequent buffers appended to the
92 // source buffer assicated with |id|. Returns true if the offset is set
93 // properly, false if the offset cannot be applied because we're in the
94 // middle of parsing a media segment.
95 bool SetTimestampOffset(const std::string
& id
, base::TimeDelta offset
);
97 // Signals an EndOfStream request.
98 // Returns false if called in an unexpected state or if there is a gap between
99 // the current position and the end of the buffered data.
100 bool EndOfStream(PipelineStatus status
);
104 virtual ~ChunkDemuxer();
116 void ChangeState_Locked(State new_state
);
118 // Reports an error and puts the demuxer in a state where it won't accept more
120 void ReportError_Locked(PipelineStatus error
);
122 // Returns true if any stream has seeked to a time without buffered data.
123 bool IsSeekPending_Locked() const;
125 // Returns true if all streams can successfully call EndOfStream,
126 // false if any can not.
127 bool CanEndOfStream_Locked() const;
129 // StreamParser callbacks.
130 void OnStreamParserInitDone(bool success
, base::TimeDelta duration
);
131 bool OnNewConfigs(bool has_audio
, bool has_video
,
132 const AudioDecoderConfig
& audio_config
,
133 const VideoDecoderConfig
& video_config
);
134 bool OnAudioBuffers(const StreamParser::BufferQueue
& buffers
);
135 bool OnVideoBuffers(const StreamParser::BufferQueue
& buffers
);
136 bool OnNeedKey(const std::string
& type
,
137 scoped_array
<uint8
> init_data
,
139 void OnNewMediaSegment(const std::string
& source_id
,
140 base::TimeDelta start_timestamp
);
141 void OnEndOfMediaSegment(const std::string
& source_id
);
143 // Computes the intersection between the video & audio
145 Ranges
<base::TimeDelta
> ComputeIntersection() const;
147 // Applies |time_offset| to the timestamps of |buffers|.
148 void AdjustBufferTimestamps(const StreamParser::BufferQueue
& buffers
,
149 base::TimeDelta timestamp_offset
);
151 // Returns true if |source_id| is valid, false otherwise.
152 bool IsValidId(const std::string
& source_id
) const;
154 // Increases |duration_| if the newly appended |buffers| exceed the current
155 // |duration_|. The |duration_| is set to the end buffered timestamp of
157 void IncreaseDurationIfNecessary(
158 const StreamParser::BufferQueue
& buffers
,
159 const scoped_refptr
<ChunkDemuxerStream
>& stream
);
161 // Decreases |duration_| if the buffered region is less than |duration_| when
162 // EndOfStream() is called.
163 void DecreaseDurationIfNecessary();
165 // Sets |duration_| to |new_duration| and notifies |host_|.
166 void UpdateDuration(base::TimeDelta new_duration
);
168 // Returns the ranges representing the buffered data in the demuxer.
169 Ranges
<base::TimeDelta
> GetBufferedRanges() const;
171 mutable base::Lock lock_
;
175 base::Closure open_cb_
;
176 NeedKeyCB need_key_cb_
;
177 // Callback used to report error strings that can help the web developer
178 // figure out what is wrong with the content.
181 PipelineStatusCB init_cb_
;
182 PipelineStatusCB seek_cb_
;
184 scoped_refptr
<ChunkDemuxerStream
> audio_
;
185 scoped_refptr
<ChunkDemuxerStream
> video_
;
187 base::TimeDelta duration_
;
189 typedef std::map
<std::string
, StreamParser
*> StreamParserMap
;
190 StreamParserMap stream_parser_map_
;
192 // Contains state belonging to a source id.
194 base::TimeDelta timestamp_offset
;
195 bool can_update_offset
;
197 typedef std::map
<std::string
, SourceInfo
> SourceInfoMap
;
198 SourceInfoMap source_info_map_
;
200 // Used to ensure that (1) config data matches the type and codec provided in
201 // AddId(), (2) only 1 audio and 1 video sources are added, and (3) ids may be
202 // removed with RemoveID() but can not be re-added (yet).
203 std::string source_id_audio_
;
204 std::string source_id_video_
;
206 DISALLOW_COPY_AND_ASSIGN(ChunkDemuxer
);
211 #endif // MEDIA_FILTERS_CHUNK_DEMUXER_H_