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