OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 // Implements a Demuxer that can switch among different data sources mid-stream. |
| 6 // Uses FFmpegDemuxer under the covers, so see the caveats at the top of |
| 7 // ffmpeg_demuxer.h. |
| 8 |
| 9 #ifndef MEDIA_FILTERS_CHUNK_DEMUXER_H_ |
| 10 #define MEDIA_FILTERS_CHUNK_DEMUXER_H_ |
| 11 |
| 12 #include <list> |
| 13 |
| 14 #include "base/synchronization/lock.h" |
| 15 #include "media/base/filter_factories.h" |
| 16 #include "media/base/filters.h" |
| 17 #include "media/webm/webm_parser.h" |
| 18 |
| 19 struct AVFormatContext; |
| 20 |
| 21 namespace media { |
| 22 |
| 23 class ChunkDemuxer; |
| 24 class ChunkDemuxerStream; |
| 25 |
| 26 // Class used by an external object to send media data to the |
| 27 // Demuxer. This object is created by the DemuxerFactory and |
| 28 // contains the Demuxer that will be returned in the next Build() |
| 29 // call on the factory. The external object tells the factory |
| 30 // to create one of these objects before it starts the Pipeline. |
| 31 // It does this because the external object may need to make AddData() |
| 32 // calls before the pipeline has completely initialized. This class |
| 33 // allows data from these calls to be queued until initialization |
| 34 // completes. It represents the minimal operations needed by |
| 35 // the external object to talk to the Demuxer. It also allows |
| 36 // the external object to have a longer lifetime than the pipeline. |
| 37 class MediaDataSink { |
| 38 public: |
| 39 MediaDataSink(const scoped_refptr<ChunkDemuxer>& demuxer); |
| 40 ~MediaDataSink(); |
| 41 |
| 42 // Flush all data passed via AddData(). |
| 43 void Flush(); |
| 44 |
| 45 // Sends media data to the demuxer. Returns true if the data is valid. |
| 46 bool AddData(const uint8* data, unsigned length); |
| 47 |
| 48 // Signals that playback is shutting down and further AddData() calls |
| 49 // should fail. This also cancels pending Read()s on DemuxerStreams. |
| 50 void Shutdown(); |
| 51 |
| 52 private: |
| 53 scoped_refptr<ChunkDemuxer> demuxer_; |
| 54 DISALLOW_IMPLICIT_CONSTRUCTORS(MediaDataSink); |
| 55 }; |
| 56 |
| 57 class ChunkDemuxer : public Demuxer { |
| 58 public: |
| 59 ChunkDemuxer(); |
| 60 virtual ~ChunkDemuxer(); |
| 61 |
| 62 bool Init(const uint8* data, int size); |
| 63 |
| 64 // Filter implementation. |
| 65 virtual void set_host(FilterHost* filter_host); |
| 66 virtual void Stop(FilterCallback* callback); |
| 67 virtual void Seek(base::TimeDelta time, const FilterStatusCB& cb); |
| 68 virtual void OnAudioRendererDisabled(); |
| 69 virtual void SetPreload(Preload preload); |
| 70 |
| 71 // Demuxer implementation. |
| 72 virtual scoped_refptr<DemuxerStream> GetStream(DemuxerStream::Type type); |
| 73 virtual base::TimeDelta GetStartTime() const; |
| 74 |
| 75 // Methods used by MediaDataSink |
| 76 void FlushData(); |
| 77 bool AddData(const uint8* data, unsigned length); |
| 78 void Shutdown(); |
| 79 |
| 80 private: |
| 81 // Helper class that collects information from the INFO & TRACKS elements |
| 82 // passed to Init(). |
| 83 class InfoTrackWebMParserClient : public WebMParserClient { |
| 84 public: |
| 85 InfoTrackWebMParserClient(); |
| 86 |
| 87 int64 timecode_scale() const; |
| 88 double duration() const; |
| 89 int64 audio_track_num() const; |
| 90 base::TimeDelta audio_default_duration() const; |
| 91 int64 video_track_num() const; |
| 92 base::TimeDelta video_default_duration() const; |
| 93 |
| 94 // WebMParserClient methods |
| 95 virtual bool OnListStart(int id); |
| 96 virtual bool OnListEnd(int id); |
| 97 virtual bool OnUInt(int id, int64 val); |
| 98 virtual bool OnFloat(int id, double val); |
| 99 virtual bool OnBinary(int id, const uint8* data, int size); |
| 100 virtual bool OnString(int id, const std::string& str); |
| 101 virtual bool OnSimpleBlock(int track_num, int timecode, |
| 102 int flags, |
| 103 const uint8* data, int size); |
| 104 private: |
| 105 int64 timecode_scale_; |
| 106 double duration_; |
| 107 int64 track_type_; |
| 108 int64 track_num_; |
| 109 int64 track_default_duration_; |
| 110 int64 audio_track_num_; |
| 111 base::TimeDelta audio_default_duration_; |
| 112 int64 video_track_num_; |
| 113 base::TimeDelta video_default_duration_; |
| 114 }; |
| 115 |
| 116 enum State { |
| 117 WAITING_FOR_INIT, |
| 118 INITIALIZED, |
| 119 INIT_ERROR, |
| 120 SHUTDOWN, |
| 121 }; |
| 122 |
| 123 void ChangeState(State new_state); |
| 124 |
| 125 // Generates an AVFormatContext for the INFO & TRACKS elements contained |
| 126 // in |data|. Returns NULL if parsing |data| fails. |
| 127 AVFormatContext* CreateFormatContext(const uint8* data, int size) const; |
| 128 |
| 129 // Sets up |audio_| & |video_| DemuxerStreams based on the data in |
| 130 // |format_context_|. Returns false if no valid audio or video stream were |
| 131 // found. |
| 132 bool SetupStreams(); |
| 133 |
| 134 // Parse all the buffers in |pending_buffers_|. Returns false if parsing one |
| 135 // of the buffers fails. |
| 136 bool ParsePendingBuffers(); |
| 137 |
| 138 // Parse a buffer that was passed to AddData(). |data| is expected to contain |
| 139 // one or more WebM Clusters. Returns false if parsing the data fails. |
| 140 bool ParseAndAddData_Locked(const uint8* data, int length); |
| 141 |
| 142 base::Lock lock_; |
| 143 State state_; |
| 144 |
| 145 base::TimeDelta seek_time_; |
| 146 FilterStatusCB seek_cb_; |
| 147 |
| 148 scoped_refptr<ChunkDemuxerStream> audio_; |
| 149 scoped_refptr<ChunkDemuxerStream> video_; |
| 150 |
| 151 AVFormatContext* format_context_; |
| 152 |
| 153 int64 buffered_bytes_; |
| 154 |
| 155 bool first_seek_; |
| 156 base::TimeDelta duration_; |
| 157 |
| 158 InfoTrackWebMParserClient info_; |
| 159 |
| 160 typedef std::list<scoped_refptr<media::Buffer> > BufferList; |
| 161 BufferList pending_buffers_; |
| 162 |
| 163 DISALLOW_COPY_AND_ASSIGN(ChunkDemuxer); |
| 164 }; |
| 165 |
| 166 class ChunkDemuxerFactory : public DemuxerFactory { |
| 167 public: |
| 168 // Takes a reference to |demuxer_factory|. |
| 169 ChunkDemuxerFactory(DataSourceFactory* data_source_factory); |
| 170 virtual ~ChunkDemuxerFactory(); |
| 171 |
| 172 bool IsUrlSupported(const std::string& url) const; |
| 173 MediaDataSink* CreateMediaDataSink(); |
| 174 |
| 175 // DemuxerFactory methods. |
| 176 virtual void Build(const std::string& url, BuildCallback* cb); |
| 177 virtual DemuxerFactory* Clone() const; |
| 178 |
| 179 private: |
| 180 static const char kURLPrefix[]; |
| 181 class BuildState; |
| 182 |
| 183 scoped_ptr<DataSourceFactory> data_source_factory_; |
| 184 scoped_refptr<ChunkDemuxer> demuxer_; |
| 185 |
| 186 DISALLOW_IMPLICIT_CONSTRUCTORS(ChunkDemuxerFactory); |
| 187 }; |
| 188 |
| 189 } // namespace media |
| 190 |
| 191 #endif // MEDIA_FILTERS_CHUNK_DEMUXER_H_ |
OLD | NEW |