demuxer.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.
  4. #ifndef MEDIA_BASE_DEMUXER_H_
  5. #define MEDIA_BASE_DEMUXER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/time/time.h"
  10. #include "media/base/container_names.h"
  11. #include "media/base/data_source.h"
  12. #include "media/base/demuxer_stream.h"
  13. #include "media/base/eme_constants.h"
  14. #include "media/base/media_export.h"
  15. #include "media/base/media_resource.h"
  16. #include "media/base/media_track.h"
  17. #include "media/base/pipeline_status.h"
  18. #include "media/base/ranges.h"
  19. #include "third_party/abseil-cpp/absl/types/optional.h"
  20. namespace media {
  21. class MediaTracks;
  22. class MEDIA_EXPORT DemuxerHost {
  23. public:
  24. // Notify the host that buffered time ranges have changed. Note that buffered
  25. // time ranges can grow (when new media data is appended), but they can also
  26. // shrink (when buffering reaches limit capacity and some buffered data
  27. // becomes evicted, e.g. due to MSE GC algorithm, or by explicit removal of
  28. // ranges directed by MSE web app).
  29. virtual void OnBufferedTimeRangesChanged(
  30. const Ranges<base::TimeDelta>& ranges) = 0;
  31. // Sets the duration of the media in microseconds.
  32. // Duration may be kInfiniteDuration if the duration is not known.
  33. virtual void SetDuration(base::TimeDelta duration) = 0;
  34. // Stops execution of the pipeline due to a fatal error. Do not call this
  35. // method with PIPELINE_OK. Stopping is not immediate so demuxers must be
  36. // prepared to soft fail on subsequent calls. E.g., if Demuxer::Seek() is
  37. // called after an unrecoverable error the provided PipelineStatusCallback
  38. // must be called with an error.
  39. virtual void OnDemuxerError(PipelineStatus error) = 0;
  40. protected:
  41. virtual ~DemuxerHost();
  42. };
  43. class MEDIA_EXPORT Demuxer : public MediaResource {
  44. public:
  45. // A new potentially encrypted stream has been parsed.
  46. // First parameter - The type of initialization data.
  47. // Second parameter - The initialization data associated with the stream.
  48. using EncryptedMediaInitDataCB =
  49. base::RepeatingCallback<void(EmeInitDataType type,
  50. const std::vector<uint8_t>& init_data)>;
  51. // Notifies demuxer clients that media track configuration has been updated
  52. // (e.g. the initial stream metadata has been parsed successfully, or a new
  53. // init segment has been parsed successfully in MSE case).
  54. using MediaTracksUpdatedCB =
  55. base::RepeatingCallback<void(std::unique_ptr<MediaTracks>)>;
  56. // Called once the demuxer has finished enabling or disabling tracks. The type
  57. // argument is required because the vector may be empty.
  58. using TrackChangeCB =
  59. base::OnceCallback<void(DemuxerStream::Type type,
  60. const std::vector<DemuxerStream*>&)>;
  61. enum DemuxerTypes {
  62. kChunkDemuxer,
  63. kFFmpegDemuxer,
  64. kMediaUrlDemuxer,
  65. };
  66. Demuxer();
  67. Demuxer(const Demuxer&) = delete;
  68. Demuxer& operator=(const Demuxer&) = delete;
  69. ~Demuxer() override;
  70. // Returns the name of the demuxer for logging purpose.
  71. virtual std::string GetDisplayName() const = 0;
  72. // Completes initialization of the demuxer.
  73. //
  74. // The demuxer does not own |host| as it is guaranteed to outlive the
  75. // lifetime of the demuxer. Don't delete it! |status_cb| must only be run
  76. // after this method has returned.
  77. virtual void Initialize(DemuxerHost* host,
  78. PipelineStatusCallback status_cb) = 0;
  79. // Aborts any pending read operations that the demuxer is involved with; any
  80. // read aborted will be aborted with a status of kAborted. Future reads will
  81. // also be aborted until Seek() is called.
  82. virtual void AbortPendingReads() = 0;
  83. // Indicates that a new Seek() call is on its way. Implementations may abort
  84. // pending reads and future Read() calls may return kAborted until Seek() is
  85. // executed. |seek_time| is the presentation timestamp of the new Seek() call.
  86. //
  87. // In actual use, this call occurs on the main thread while Seek() is called
  88. // on the media thread. StartWaitingForSeek() can be used to synchronize the
  89. // two.
  90. //
  91. // StartWaitingForSeek() MUST be called before Seek().
  92. virtual void StartWaitingForSeek(base::TimeDelta seek_time) = 0;
  93. // Indicates that the current Seek() operation is obsoleted by a new one.
  94. // Implementations can expect that StartWaitingForSeek() will be called
  95. // when the current seek operation completes.
  96. //
  97. // Like StartWaitingForSeek(), CancelPendingSeek() is called on the main
  98. // thread. Ordering with respect to the to-be-canceled Seek() is not
  99. // guaranteed. Regardless of ordering, implementations may abort pending reads
  100. // and may return kAborted from future Read() calls, until after
  101. // StartWaitingForSeek() and the following Seek() call occurs.
  102. //
  103. // |seek_time| should match that passed to the next StartWaitingForSeek(), but
  104. // may not if the seek target changes again before the current seek operation
  105. // completes or is aborted.
  106. virtual void CancelPendingSeek(base::TimeDelta seek_time) = 0;
  107. // Carry out any actions required to seek to the given time, executing the
  108. // callback upon completion.
  109. virtual void Seek(base::TimeDelta time, PipelineStatusCallback status_cb) = 0;
  110. // Stops this demuxer.
  111. //
  112. // After this call the demuxer may be destroyed. It is illegal to call any
  113. // method (including Stop()) after a demuxer has stopped.
  114. virtual void Stop() = 0;
  115. // Returns the starting time for the media file; it's always positive.
  116. virtual base::TimeDelta GetStartTime() const = 0;
  117. // Returns Time represented by presentation timestamp 0.
  118. // If the timstamps are not associated with a Time, then
  119. // a null Time is returned.
  120. virtual base::Time GetTimelineOffset() const = 0;
  121. // Returns the memory usage in bytes for the demuxer.
  122. virtual int64_t GetMemoryUsage() const = 0;
  123. // Returns the container name to use for metrics.
  124. // Implementations where this is not meaningful will return an empty value.
  125. // Implementations that do provide values should always provide a value,
  126. // returning CONTAINER_UNKNOWN in cases where the container is not known.
  127. virtual absl::optional<container_names::MediaContainerName>
  128. GetContainerForMetrics() const = 0;
  129. // The |track_ids| vector has either 1 track, or is empty, indicating that
  130. // all tracks should be disabled. |change_completed_cb| is fired after the
  131. // demuxer streams are disabled, however this callback should then notify
  132. // the appropriate renderer in order for tracks to be switched fully.
  133. virtual void OnEnabledAudioTracksChanged(
  134. const std::vector<MediaTrack::Id>& track_ids,
  135. base::TimeDelta curr_time,
  136. TrackChangeCB change_completed_cb) = 0;
  137. virtual void OnSelectedVideoTrackChanged(
  138. const std::vector<MediaTrack::Id>& track_ids,
  139. base::TimeDelta curr_time,
  140. TrackChangeCB change_completed_cb) = 0;
  141. };
  142. } // namespace media
  143. #endif // MEDIA_BASE_DEMUXER_H_