[email protected] | a4dae8e | 2012-03-15 23:35:31 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | a2b5c47 | 2011-09-13 20:24:10 | [diff] [blame] | 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_DEMUXER_H_ |
| 6 | #define MEDIA_BASE_DEMUXER_H_ |
| 7 | |
avi | 1323b9c2 | 2015-12-23 06:22:36 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
danakj | 6aaed6a | 2016-04-26 01:25:44 | [diff] [blame] | 10 | #include <memory> |
[email protected] | 322d22c | 2013-08-26 18:50:41 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
avi | a82b9b5 | 2015-12-19 04:27:08 | [diff] [blame] | 13 | #include "base/macros.h" |
servolk | 9bed660 | 2017-02-24 01:20:11 | [diff] [blame] | 14 | #include "base/optional.h" |
[email protected] | 7e72e7b | 2013-06-28 05:40:10 | [diff] [blame] | 15 | #include "base/time/time.h" |
[email protected] | 236119c | 2011-12-16 17:14:25 | [diff] [blame] | 16 | #include "media/base/data_source.h" |
[email protected] | a2b5c47 | 2011-09-13 20:24:10 | [diff] [blame] | 17 | #include "media/base/demuxer_stream.h" |
jrummell | cf78967c | 2015-04-03 05:03:58 | [diff] [blame] | 18 | #include "media/base/eme_constants.h" |
[email protected] | a2b5c47 | 2011-09-13 20:24:10 | [diff] [blame] | 19 | #include "media/base/media_export.h" |
servolk | cfc91f15 | 2017-02-02 05:11:23 | [diff] [blame] | 20 | #include "media/base/media_resource.h" |
servolk | f25ceed | 2016-07-01 03:44:38 | [diff] [blame] | 21 | #include "media/base/media_track.h" |
[email protected] | a2b5c47 | 2011-09-13 20:24:10 | [diff] [blame] | 22 | #include "media/base/pipeline_status.h" |
servolk | 21e682e | 2016-01-14 22:38:23 | [diff] [blame] | 23 | #include "media/base/ranges.h" |
[email protected] | a2b5c47 | 2011-09-13 20:24:10 | [diff] [blame] | 24 | |
| 25 | namespace media { |
| 26 | |
servolk | 81e01e0 | 2016-03-05 03:29:15 | [diff] [blame] | 27 | class MediaTracks; |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 28 | |
[email protected] | a61ce5a9 | 2014-04-03 18:54:39 | [diff] [blame] | 29 | class MEDIA_EXPORT DemuxerHost { |
[email protected] | 236119c | 2011-12-16 17:14:25 | [diff] [blame] | 30 | public: |
servolk | 21e682e | 2016-01-14 22:38:23 | [diff] [blame] | 31 | // Notify the host that buffered time ranges have changed. Note that buffered |
| 32 | // time ranges can grow (when new media data is appended), but they can also |
| 33 | // shrink (when buffering reaches limit capacity and some buffered data |
| 34 | // becomes evicted, e.g. due to MSE GC algorithm, or by explicit removal of |
| 35 | // ranges directed by MSE web app). |
| 36 | virtual void OnBufferedTimeRangesChanged( |
| 37 | const Ranges<base::TimeDelta>& ranges) = 0; |
[email protected] | a61ce5a9 | 2014-04-03 18:54:39 | [diff] [blame] | 38 | |
[email protected] | 60bf922 | 2012-05-26 00:31:34 | [diff] [blame] | 39 | // Sets the duration of the media in microseconds. |
dalecurtis | 39a7f93 | 2016-07-19 18:34:59 | [diff] [blame] | 40 | // Duration may be kInfiniteDuration if the duration is not known. |
[email protected] | 236119c | 2011-12-16 17:14:25 | [diff] [blame] | 41 | virtual void SetDuration(base::TimeDelta duration) = 0; |
| 42 | |
dalecurtis | 2ff781da | 2016-03-03 01:52:13 | [diff] [blame] | 43 | // Stops execution of the pipeline due to a fatal error. Do not call this |
| 44 | // method with PIPELINE_OK. Stopping is not immediate so demuxers must be |
| 45 | // prepared to soft fail on subsequent calls. E.g., if Demuxer::Seek() is |
| 46 | // called after an unrecoverable error the provided PipelineStatusCB must be |
| 47 | // called with an error. |
[email protected] | 236119c | 2011-12-16 17:14:25 | [diff] [blame] | 48 | virtual void OnDemuxerError(PipelineStatus error) = 0; |
[email protected] | 512d03f | 2012-06-26 01:06:06 | [diff] [blame] | 49 | |
| 50 | protected: |
| 51 | virtual ~DemuxerHost(); |
[email protected] | 236119c | 2011-12-16 17:14:25 | [diff] [blame] | 52 | }; |
[email protected] | a2b5c47 | 2011-09-13 20:24:10 | [diff] [blame] | 53 | |
servolk | cfc91f15 | 2017-02-02 05:11:23 | [diff] [blame] | 54 | class MEDIA_EXPORT Demuxer : public MediaResource { |
[email protected] | a2b5c47 | 2011-09-13 20:24:10 | [diff] [blame] | 55 | public: |
[email protected] | 322d22c | 2013-08-26 18:50:41 | [diff] [blame] | 56 | // A new potentially encrypted stream has been parsed. |
| 57 | // First parameter - The type of initialization data. |
| 58 | // Second parameter - The initialization data associated with the stream. |
danakj | 6aaed6a | 2016-04-26 01:25:44 | [diff] [blame] | 59 | using EncryptedMediaInitDataCB = |
| 60 | base::Callback<void(EmeInitDataType type, |
| 61 | const std::vector<uint8_t>& init_data)>; |
[email protected] | 322d22c | 2013-08-26 18:50:41 | [diff] [blame] | 62 | |
servolk | 81e01e0 | 2016-03-05 03:29:15 | [diff] [blame] | 63 | // Notifies demuxer clients that media track configuration has been updated |
servolk | f25ceed | 2016-07-01 03:44:38 | [diff] [blame] | 64 | // (e.g. the initial stream metadata has been parsed successfully, or a new |
servolk | 81e01e0 | 2016-03-05 03:29:15 | [diff] [blame] | 65 | // init segment has been parsed successfully in MSE case). |
danakj | 6aaed6a | 2016-04-26 01:25:44 | [diff] [blame] | 66 | using MediaTracksUpdatedCB = |
| 67 | base::Callback<void(std::unique_ptr<MediaTracks>)>; |
servolk | 81e01e0 | 2016-03-05 03:29:15 | [diff] [blame] | 68 | |
Ted Meyer | da82b60 | 2018-04-19 00:36:56 | [diff] [blame] | 69 | // Called once the demuxer has finished enabling or disabling tracks. The type |
| 70 | // argument is required because the vector may be empty. |
| 71 | using TrackChangeCB = |
| 72 | base::OnceCallback<void(DemuxerStream::Type type, |
| 73 | const std::vector<DemuxerStream*>&)>; |
| 74 | |
[email protected] | 236119c | 2011-12-16 17:14:25 | [diff] [blame] | 75 | Demuxer(); |
dcheng | c24565478f | 2014-10-21 12:23:27 | [diff] [blame] | 76 | ~Demuxer() override; |
[email protected] | 236119c | 2011-12-16 17:14:25 | [diff] [blame] | 77 | |
xhwang | a9ca9db | 2015-06-11 23:52:39 | [diff] [blame] | 78 | // Returns the name of the demuxer for logging purpose. |
| 79 | virtual std::string GetDisplayName() const = 0; |
| 80 | |
[email protected] | 9bfe9b8 | 2012-04-02 17:56:27 | [diff] [blame] | 81 | // Completes initialization of the demuxer. |
| 82 | // |
[email protected] | d09ef25 | 2012-04-05 04:31:30 | [diff] [blame] | 83 | // The demuxer does not own |host| as it is guaranteed to outlive the |
dalecurtis | 21b12829 | 2015-01-21 11:09:24 | [diff] [blame] | 84 | // lifetime of the demuxer. Don't delete it! |status_cb| must only be run |
| 85 | // after this method has returned. |
[email protected] | d09ef25 | 2012-04-05 04:31:30 | [diff] [blame] | 86 | virtual void Initialize(DemuxerHost* host, |
Ted Meyer | 4ca53de | 2018-05-26 00:21:31 | [diff] [blame] | 87 | const PipelineStatusCB& status_cb) = 0; |
[email protected] | 9bfe9b8 | 2012-04-02 17:56:27 | [diff] [blame] | 88 | |
dalecurtis | f299e19 | 2016-08-30 19:54:19 | [diff] [blame] | 89 | // Aborts any pending read operations that the demuxer is involved with; any |
sandersd | 216002c | 2016-12-05 22:28:58 | [diff] [blame] | 90 | // read aborted will be aborted with a status of kAborted. Future reads will |
| 91 | // also be aborted until Seek() is called. |
dalecurtis | f299e19 | 2016-08-30 19:54:19 | [diff] [blame] | 92 | virtual void AbortPendingReads() = 0; |
| 93 | |
sandersd | b5e2146 | 2016-03-09 01:49:07 | [diff] [blame] | 94 | // Indicates that a new Seek() call is on its way. Implementations may abort |
| 95 | // pending reads and future Read() calls may return kAborted until Seek() is |
| 96 | // executed. |seek_time| is the presentation timestamp of the new Seek() call. |
| 97 | // |
| 98 | // In actual use, this call occurs on the main thread while Seek() is called |
| 99 | // on the media thread. StartWaitingForSeek() can be used to synchronize the |
| 100 | // two. |
| 101 | // |
| 102 | // StartWaitingForSeek() MUST be called before Seek(). |
| 103 | virtual void StartWaitingForSeek(base::TimeDelta seek_time) = 0; |
| 104 | |
| 105 | // Indicates that the current Seek() operation is obsoleted by a new one. |
| 106 | // Implementations can expect that StartWaitingForSeek() will be called |
| 107 | // when the current seek operation completes. |
| 108 | // |
| 109 | // Like StartWaitingForSeek(), CancelPendingSeek() is called on the main |
| 110 | // thread. Ordering with respect to the to-be-canceled Seek() is not |
| 111 | // guaranteed. Regardless of ordering, implementations may abort pending reads |
| 112 | // and may return kAborted from future Read() calls, until after |
| 113 | // StartWaitingForSeek() and the following Seek() call occurs. |
| 114 | // |
| 115 | // |seek_time| should match that passed to the next StartWaitingForSeek(), but |
| 116 | // may not if the seek target changes again before the current seek operation |
| 117 | // completes or is aborted. |
| 118 | virtual void CancelPendingSeek(base::TimeDelta seek_time) = 0; |
| 119 | |
[email protected] | a2b5c47 | 2011-09-13 20:24:10 | [diff] [blame] | 120 | // Carry out any actions required to seek to the given time, executing the |
| 121 | // callback upon completion. |
[email protected] | 7c63c2e | 2013-10-10 20:00:45 | [diff] [blame] | 122 | virtual void Seek(base::TimeDelta time, |
| 123 | const PipelineStatusCB& status_cb) = 0; |
[email protected] | a2b5c47 | 2011-09-13 20:24:10 | [diff] [blame] | 124 | |
xhwang | fcdacaf | 2014-09-02 18:08:35 | [diff] [blame] | 125 | // Stops this demuxer. |
[email protected] | 8b75455 | 2013-08-22 00:31:05 | [diff] [blame] | 126 | // |
xhwang | fcdacaf | 2014-09-02 18:08:35 | [diff] [blame] | 127 | // After this call the demuxer may be destroyed. It is illegal to call any |
| 128 | // method (including Stop()) after a demuxer has stopped. |
| 129 | virtual void Stop() = 0; |
[email protected] | a2b5c47 | 2011-09-13 20:24:10 | [diff] [blame] | 130 | |
dalecurtis | b9ec078cf | 2014-09-16 22:23:21 | [diff] [blame] | 131 | // Returns the starting time for the media file; it's always positive. |
| 132 | virtual base::TimeDelta GetStartTime() const = 0; |
| 133 | |
[email protected] | db66d009 | 2014-04-16 07:15:12 | [diff] [blame] | 134 | // Returns Time represented by presentation timestamp 0. |
| 135 | // If the timstamps are not associated with a Time, then |
| 136 | // a null Time is returned. |
| 137 | virtual base::Time GetTimelineOffset() const = 0; |
| 138 | |
wdzierzanowski | 9836863 | 2015-12-04 09:22:01 | [diff] [blame] | 139 | // Returns the memory usage in bytes for the demuxer. |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 140 | virtual int64_t GetMemoryUsage() const = 0; |
| 141 | |
Ted Meyer | da82b60 | 2018-04-19 00:36:56 | [diff] [blame] | 142 | // The |track_ids| vector has either 1 track, or is empty, indicating that |
| 143 | // all tracks should be disabled. |change_completed_cb| is fired after the |
| 144 | // demuxer streams are disabled, however this callback should then notify |
| 145 | // the appropriate renderer in order for tracks to be switched fully. |
servolk | f25ceed | 2016-07-01 03:44:38 | [diff] [blame] | 146 | virtual void OnEnabledAudioTracksChanged( |
| 147 | const std::vector<MediaTrack::Id>& track_ids, |
Ted Meyer | da82b60 | 2018-04-19 00:36:56 | [diff] [blame] | 148 | base::TimeDelta curr_time, |
| 149 | TrackChangeCB change_completed_cb) = 0; |
servolk | f25ceed | 2016-07-01 03:44:38 | [diff] [blame] | 150 | |
servolk | f25ceed | 2016-07-01 03:44:38 | [diff] [blame] | 151 | virtual void OnSelectedVideoTrackChanged( |
Ted Meyer | da82b60 | 2018-04-19 00:36:56 | [diff] [blame] | 152 | const std::vector<MediaTrack::Id>& track_ids, |
| 153 | base::TimeDelta curr_time, |
| 154 | TrackChangeCB change_completed_cb) = 0; |
servolk | f25ceed | 2016-07-01 03:44:38 | [diff] [blame] | 155 | |
[email protected] | a2b5c47 | 2011-09-13 20:24:10 | [diff] [blame] | 156 | private: |
[email protected] | a2b5c47 | 2011-09-13 20:24:10 | [diff] [blame] | 157 | DISALLOW_COPY_AND_ASSIGN(Demuxer); |
| 158 | }; |
| 159 | |
| 160 | } // namespace media |
| 161 | |
| 162 | #endif // MEDIA_BASE_DEMUXER_H_ |