test_media_source.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2017 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_TEST_TEST_MEDIA_SOURCE_H_
  5. #define MEDIA_TEST_TEST_MEDIA_SOURCE_H_
  6. #include <limits>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/time/time.h"
  9. #include "media/base/demuxer.h"
  10. #include "media/base/media_util.h"
  11. #include "media/base/pipeline_status.h"
  12. #include "media/filters/chunk_demuxer.h"
  13. #include "testing/gmock/include/gmock/gmock.h"
  14. namespace media {
  15. // Indicates that the whole file should be appended.
  16. constexpr size_t kAppendWholeFile = std::numeric_limits<size_t>::max();
  17. // Helper class that emulates calls made on the ChunkDemuxer by the
  18. // Media Source API.
  19. class TestMediaSource {
  20. public:
  21. enum class ExpectedAppendResult {
  22. kSuccess,
  23. kFailure,
  24. kSuccessOrFailure, // e.g., for fuzzing when parse may pass or fail
  25. };
  26. TestMediaSource(const std::string& filename,
  27. const std::string& mimetype,
  28. size_t initial_append_size,
  29. bool initial_sequence_mode = false);
  30. // Same as the constructor above, but use GetMimeTypeForFile() to get the mime
  31. // type.
  32. TestMediaSource(const std::string& filename,
  33. size_t initial_append_size,
  34. bool initial_sequence_mode = false);
  35. TestMediaSource(scoped_refptr<DecoderBuffer> data,
  36. const std::string& mimetype,
  37. size_t initial_append_size,
  38. bool initial_sequence_mode = false);
  39. TestMediaSource(const TestMediaSource&) = delete;
  40. TestMediaSource& operator=(const TestMediaSource&) = delete;
  41. ~TestMediaSource();
  42. std::unique_ptr<Demuxer> GetDemuxer();
  43. void set_encrypted_media_init_data_cb(
  44. const Demuxer::EncryptedMediaInitDataCB& encrypted_media_init_data_cb) {
  45. encrypted_media_init_data_cb_ = encrypted_media_init_data_cb;
  46. }
  47. void set_demuxer_failure_cb(const PipelineStatusCB& demuxer_failure_cb) {
  48. demuxer_failure_cb_ = demuxer_failure_cb;
  49. }
  50. void set_do_eos_after_next_append(bool flag) {
  51. do_eos_after_next_append_ = flag;
  52. }
  53. void SetAppendWindow(base::TimeDelta timestamp_offset,
  54. base::TimeDelta append_window_start,
  55. base::TimeDelta append_window_end);
  56. void Seek(base::TimeDelta seek_time,
  57. size_t new_position,
  58. size_t seek_append_size);
  59. void Seek(base::TimeDelta seek_time);
  60. void SetSequenceMode(bool sequence_mode);
  61. void AppendData(size_t size);
  62. bool AppendAtTime(base::TimeDelta timestamp_offset,
  63. const uint8_t* pData,
  64. int size);
  65. void AppendAtTimeWithWindow(base::TimeDelta timestamp_offset,
  66. base::TimeDelta append_window_start,
  67. base::TimeDelta append_window_end,
  68. const uint8_t* pData,
  69. int size);
  70. void SetMemoryLimits(size_t limit_bytes);
  71. bool EvictCodedFrames(base::TimeDelta currentMediaTime, size_t newDataSize);
  72. void RemoveRange(base::TimeDelta start, base::TimeDelta end);
  73. void EndOfStream();
  74. void UnmarkEndOfStream();
  75. void Shutdown();
  76. void DemuxerOpened();
  77. void DemuxerOpenedTask();
  78. ChunkDemuxer::Status AddId();
  79. void ChangeType(const std::string& type);
  80. void OnEncryptedMediaInitData(EmeInitDataType init_data_type,
  81. const std::vector<uint8_t>& init_data);
  82. base::TimeDelta last_timestamp_offset() const {
  83. return last_timestamp_offset_;
  84. }
  85. void set_expected_append_result(ExpectedAppendResult expectation) {
  86. expected_append_result_ = expectation;
  87. }
  88. void InitSegmentReceived(std::unique_ptr<MediaTracks> tracks);
  89. MOCK_METHOD1(InitSegmentReceivedMock, void(std::unique_ptr<MediaTracks>&));
  90. MOCK_METHOD1(OnParseWarningMock, void(const SourceBufferParseWarning));
  91. private:
  92. void VerifyExpectedAppendResult(bool append_result);
  93. NullMediaLog media_log_;
  94. scoped_refptr<DecoderBuffer> file_data_;
  95. size_t current_position_;
  96. size_t initial_append_size_;
  97. bool initial_sequence_mode_;
  98. std::string mimetype_;
  99. raw_ptr<ChunkDemuxer> chunk_demuxer_;
  100. std::unique_ptr<Demuxer> owned_chunk_demuxer_;
  101. PipelineStatusCB demuxer_failure_cb_;
  102. Demuxer::EncryptedMediaInitDataCB encrypted_media_init_data_cb_;
  103. base::TimeDelta last_timestamp_offset_;
  104. base::TimeDelta append_window_start_;
  105. base::TimeDelta append_window_end_ = kInfiniteDuration;
  106. bool do_eos_after_next_append_ = false;
  107. ExpectedAppendResult expected_append_result_ = ExpectedAppendResult::kSuccess;
  108. };
  109. } // namespace media
  110. #endif // MEDIA_TEST_TEST_MEDIA_SOURCE_H_