OLD | NEW |
(Empty) | |
| 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 #include "media/test/mock_media_source.h" |
| 6 |
| 7 #include "base/threading/thread_task_runner_handle.h" |
| 8 #include "media/base/test_data_util.h" |
| 9 #include "media/base/timestamp_constants.h" |
| 10 |
| 11 namespace media { |
| 12 |
| 13 constexpr char kSourceId[] = "SourceId"; |
| 14 const size_t kAppendWholeFile = std::numeric_limits<size_t>::max(); |
| 15 |
| 16 MockMediaSource::MockMediaSource(const std::string& filename, |
| 17 const std::string& mimetype, |
| 18 size_t initial_append_size) |
| 19 : current_position_(0), |
| 20 initial_append_size_(initial_append_size), |
| 21 mimetype_(mimetype), |
| 22 chunk_demuxer_(new ChunkDemuxer( |
| 23 base::Bind(&MockMediaSource::DemuxerOpened, base::Unretained(this)), |
| 24 base::Bind(&MockMediaSource::OnEncryptedMediaInitData, |
| 25 base::Unretained(this)), |
| 26 &media_log_)), |
| 27 owned_chunk_demuxer_(chunk_demuxer_) { |
| 28 file_data_ = ReadTestDataFile(filename); |
| 29 |
| 30 if (initial_append_size_ == kAppendWholeFile) |
| 31 initial_append_size_ = file_data_->data_size(); |
| 32 |
| 33 DCHECK_GT(initial_append_size_, 0u); |
| 34 DCHECK_LE(initial_append_size_, file_data_->data_size()); |
| 35 } |
| 36 |
| 37 MockMediaSource::~MockMediaSource() {} |
| 38 |
| 39 std::unique_ptr<Demuxer> MockMediaSource::GetDemuxer() { |
| 40 return std::move(owned_chunk_demuxer_); |
| 41 } |
| 42 |
| 43 void MockMediaSource::Seek(base::TimeDelta seek_time, |
| 44 size_t new_position, |
| 45 size_t seek_append_size) { |
| 46 chunk_demuxer_->StartWaitingForSeek(seek_time); |
| 47 |
| 48 chunk_demuxer_->ResetParserState(kSourceId, base::TimeDelta(), |
| 49 kInfiniteDuration, &last_timestamp_offset_); |
| 50 |
| 51 DCHECK_LT(new_position, file_data_->data_size()); |
| 52 current_position_ = new_position; |
| 53 |
| 54 AppendData(seek_append_size); |
| 55 } |
| 56 |
| 57 void MockMediaSource::Seek(base::TimeDelta seek_time) { |
| 58 chunk_demuxer_->StartWaitingForSeek(seek_time); |
| 59 } |
| 60 |
| 61 void MockMediaSource::AppendData(size_t size) { |
| 62 DCHECK(chunk_demuxer_); |
| 63 DCHECK_LT(current_position_, file_data_->data_size()); |
| 64 DCHECK_LE(current_position_ + size, file_data_->data_size()); |
| 65 |
| 66 ASSERT_TRUE(chunk_demuxer_->AppendData( |
| 67 kSourceId, file_data_->data() + current_position_, size, |
| 68 base::TimeDelta(), kInfiniteDuration, &last_timestamp_offset_)); |
| 69 current_position_ += size; |
| 70 } |
| 71 |
| 72 bool MockMediaSource::AppendAtTime(base::TimeDelta timestamp_offset, |
| 73 const uint8_t* pData, |
| 74 int size) { |
| 75 CHECK(!chunk_demuxer_->IsParsingMediaSegment(kSourceId)); |
| 76 bool success = |
| 77 chunk_demuxer_->AppendData(kSourceId, pData, size, base::TimeDelta(), |
| 78 kInfiniteDuration, ×tamp_offset); |
| 79 last_timestamp_offset_ = timestamp_offset; |
| 80 return success; |
| 81 } |
| 82 |
| 83 void MockMediaSource::AppendAtTimeWithWindow( |
| 84 base::TimeDelta timestamp_offset, |
| 85 base::TimeDelta append_window_start, |
| 86 base::TimeDelta append_window_end, |
| 87 const uint8_t* pData, |
| 88 int size) { |
| 89 CHECK(!chunk_demuxer_->IsParsingMediaSegment(kSourceId)); |
| 90 ASSERT_TRUE(chunk_demuxer_->AppendData(kSourceId, pData, size, |
| 91 append_window_start, append_window_end, |
| 92 ×tamp_offset)); |
| 93 last_timestamp_offset_ = timestamp_offset; |
| 94 } |
| 95 |
| 96 void MockMediaSource::SetMemoryLimits(size_t limit_bytes) { |
| 97 chunk_demuxer_->SetMemoryLimitsForTest(DemuxerStream::AUDIO, limit_bytes); |
| 98 chunk_demuxer_->SetMemoryLimitsForTest(DemuxerStream::VIDEO, limit_bytes); |
| 99 } |
| 100 |
| 101 bool MockMediaSource::EvictCodedFrames(base::TimeDelta currentMediaTime, |
| 102 size_t newDataSize) { |
| 103 return chunk_demuxer_->EvictCodedFrames(kSourceId, currentMediaTime, |
| 104 newDataSize); |
| 105 } |
| 106 |
| 107 void MockMediaSource::RemoveRange(base::TimeDelta start, base::TimeDelta end) { |
| 108 chunk_demuxer_->Remove(kSourceId, start, end); |
| 109 } |
| 110 |
| 111 void MockMediaSource::EndOfStream() { |
| 112 chunk_demuxer_->MarkEndOfStream(PIPELINE_OK); |
| 113 } |
| 114 |
| 115 void MockMediaSource::Shutdown() { |
| 116 if (!chunk_demuxer_) |
| 117 return; |
| 118 chunk_demuxer_->ResetParserState(kSourceId, base::TimeDelta(), |
| 119 kInfiniteDuration, &last_timestamp_offset_); |
| 120 chunk_demuxer_->Shutdown(); |
| 121 chunk_demuxer_ = NULL; |
| 122 } |
| 123 |
| 124 void MockMediaSource::DemuxerOpened() { |
| 125 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 126 FROM_HERE, |
| 127 base::Bind(&MockMediaSource::DemuxerOpenedTask, base::Unretained(this))); |
| 128 } |
| 129 |
| 130 void MockMediaSource::DemuxerOpenedTask() { |
| 131 ChunkDemuxer::Status status = AddId(); |
| 132 if (status != ChunkDemuxer::kOk) { |
| 133 CHECK(!demuxer_failure_cb_.is_null()); |
| 134 demuxer_failure_cb_.Run(DEMUXER_ERROR_COULD_NOT_OPEN); |
| 135 return; |
| 136 } |
| 137 chunk_demuxer_->SetTracksWatcher( |
| 138 kSourceId, base::Bind(&MockMediaSource::InitSegmentReceived, |
| 139 base::Unretained(this))); |
| 140 |
| 141 AppendData(initial_append_size_); |
| 142 } |
| 143 |
| 144 ChunkDemuxer::Status MockMediaSource::AddId() { |
| 145 // This code assumes that |mimetype_| is one of the following forms. |
| 146 // 1. audio/mpeg |
| 147 // 2. video/webm;codec="vorbis,vp8". |
| 148 size_t semicolon = mimetype_.find(";"); |
| 149 std::string type = mimetype_; |
| 150 std::string codecs_param = ""; |
| 151 if (semicolon != std::string::npos) { |
| 152 type = mimetype_.substr(0, semicolon); |
| 153 size_t codecs_param_start = mimetype_.find("codecs=\"", semicolon); |
| 154 |
| 155 CHECK_NE(codecs_param_start, std::string::npos); |
| 156 |
| 157 codecs_param_start += 8; // Skip over the codecs=". |
| 158 |
| 159 size_t codecs_param_end = mimetype_.find("\"", codecs_param_start); |
| 160 |
| 161 CHECK_NE(codecs_param_end, std::string::npos); |
| 162 |
| 163 codecs_param = mimetype_.substr(codecs_param_start, |
| 164 codecs_param_end - codecs_param_start); |
| 165 } |
| 166 |
| 167 return chunk_demuxer_->AddId(kSourceId, type, codecs_param); |
| 168 } |
| 169 |
| 170 void MockMediaSource::OnEncryptedMediaInitData( |
| 171 EmeInitDataType init_data_type, |
| 172 const std::vector<uint8_t>& init_data) { |
| 173 DCHECK(!init_data.empty()); |
| 174 CHECK(!encrypted_media_init_data_cb_.is_null()); |
| 175 encrypted_media_init_data_cb_.Run(init_data_type, init_data); |
| 176 } |
| 177 |
| 178 void MockMediaSource::InitSegmentReceived(std::unique_ptr<MediaTracks> tracks) { |
| 179 CHECK(tracks.get()); |
| 180 EXPECT_GT(tracks->tracks().size(), 0u); |
| 181 CHECK(chunk_demuxer_); |
| 182 // Verify that track ids are unique. |
| 183 std::set<MediaTrack::Id> track_ids; |
| 184 for (const auto& track : tracks->tracks()) { |
| 185 EXPECT_EQ(track_ids.end(), track_ids.find(track->id())); |
| 186 track_ids.insert(track->id()); |
| 187 } |
| 188 InitSegmentReceivedMock(tracks); |
| 189 } |
| 190 |
| 191 } // namespace media |
OLD | NEW |