7#ifndef PACKAGER_MEDIA_BASE_MEDIA_HANDLER_TEST_BASE_H_
8#define PACKAGER_MEDIA_BASE_MEDIA_HANDLER_TEST_BASE_H_
16#include <absl/strings/escaping.h>
17#include <gmock/gmock.h>
18#include <gtest/gtest.h>
20#include <packager/media/base/media_handler.h>
21#include <packager/media/base/media_sample.h>
22#include <packager/media/base/stream_info.h>
23#include <packager/media/base/text_sample.h>
24#include <packager/media/base/video_stream_info.h>
25#include <packager/status.h>
30std::string BoolToString(
bool value);
31std::string ToPrettyString(
const std::string& str);
33bool TryMatchStreamDataType(
const StreamDataType& actual,
34 const StreamDataType& expected,
35 ::testing::MatchResultListener* listener);
37bool TryMatchStreamType(
const StreamType& actual,
38 const StreamType& expected,
39 ::testing::MatchResultListener* listener);
41template <
typename T,
typename M>
42bool TryMatch(
const T& value,
44 ::testing::MatchResultListener* listener,
45 const char* value_name) {
46 if (!ExplainMatchResult(matcher, value, listener)) {
49 *listener <<
" Mismatch on " << value_name;
56MATCHER_P(IsPsshInfoWithSystemId,
58 std::string(negation ?
"doesn't " :
"") +
" have system ID " +
59 testing::PrintToString(system_id)) {
60 *result_listener <<
"which is (" << testing::PrintToString(arg.system_id)
62 return arg.system_id == system_id;
65MATCHER_P4(IsStreamInfo, stream_index, time_scale, encrypted, language,
"") {
66 if (!TryMatchStreamDataType(arg->stream_data_type,
67 StreamDataType::kStreamInfo, result_listener)) {
71 const std::string is_encrypted_string =
72 BoolToString(arg->stream_info->is_encrypted());
74 *result_listener <<
"which is (" << arg->stream_index <<
", "
75 << arg->stream_info->time_scale() <<
", "
76 << is_encrypted_string <<
", "
77 << arg->stream_info->language() <<
")";
79 return TryMatch(arg->stream_index, stream_index, result_listener,
81 TryMatch(arg->stream_info->time_scale(), time_scale, result_listener,
83 TryMatch(arg->stream_info->is_encrypted(), encrypted, result_listener,
85 TryMatch(arg->stream_info->language(), language, result_listener,
89MATCHER_P3(IsVideoStream, stream_index, trick_play_factor, playback_rate,
"") {
90 if (!TryMatchStreamDataType(arg->stream_data_type,
91 StreamDataType::kStreamInfo, result_listener)) {
95 if (!TryMatchStreamType(arg->stream_info->stream_type(), kStreamVideo,
100 const VideoStreamInfo* info =
101 static_cast<const VideoStreamInfo*
>(arg->stream_info.get());
103 *result_listener <<
"which is (" << arg->stream_index <<
", "
104 << info->trick_play_factor() <<
", " << info->playback_rate()
107 return TryMatch(arg->stream_index, stream_index, result_listener,
109 TryMatch(info->trick_play_factor(), trick_play_factor, result_listener,
110 "trick_play_factor") &&
111 TryMatch(info->playback_rate(), playback_rate, result_listener,
115MATCHER_P5(IsSegmentInfo,
122 if (!TryMatchStreamDataType(arg->stream_data_type,
123 StreamDataType::kSegmentInfo, result_listener)) {
127 const std::string is_subsegment_string =
128 BoolToString(arg->segment_info->is_subsegment);
129 const std::string is_encrypted_string =
130 BoolToString(arg->segment_info->is_encrypted);
132 *result_listener <<
"which is (" << arg->stream_index <<
", "
133 << arg->segment_info->start_timestamp <<
", "
134 << arg->segment_info->duration <<
", "
135 << is_subsegment_string <<
", " << is_encrypted_string
138 return TryMatch(arg->stream_index, stream_index, result_listener,
140 TryMatch(arg->segment_info->start_timestamp, start_timestamp,
141 result_listener,
"start_timestamp") &&
142 TryMatch(arg->segment_info->duration, duration, result_listener,
144 TryMatch(arg->segment_info->is_subsegment, subsegment, result_listener,
146 TryMatch(arg->segment_info->is_encrypted, encrypted, result_listener,
150MATCHER_P6(MatchEncryptionConfig,
158 const std::string constant_iv_hex = absl::BytesToHexString(
159 std::string(std::begin(arg.constant_iv), std::end(arg.constant_iv)));
160 const std::string key_id_hex = absl::BytesToHexString(
161 std::string(std::begin(arg.key_id), std::end(arg.key_id)));
162 const std::string protection_scheme_as_string =
163 FourCCToString(arg.protection_scheme);
166 const int crypt_byte_as_int =
static_cast<int>(arg.crypt_byte_block);
167 const int skip_byte_as_int =
static_cast<int>(arg.skip_byte_block);
169 *result_listener <<
"which is (" << protection_scheme_as_string <<
", "
170 << crypt_byte_as_int <<
", " << skip_byte_as_int <<
", "
171 << arg.per_sample_iv_size <<
", " << constant_iv_hex <<
", "
172 << key_id_hex <<
")";
174 return TryMatch(arg.protection_scheme, protection_scheme, result_listener,
175 "protection_scheme") &&
176 TryMatch(arg.crypt_byte_block, crypt_byte_block, result_listener,
177 "crypt_byte_block") &&
178 TryMatch(arg.skip_byte_block, skip_byte_block, result_listener,
179 "skip_byte_block") &&
180 TryMatch(arg.per_sample_iv_size, per_sample_iv_size, result_listener,
181 "per_sample_iv_size") &&
182 TryMatch(arg.constant_iv, constant_iv, result_listener,
184 TryMatch(arg.key_id, key_id, result_listener,
"key_id");
187MATCHER_P5(IsMediaSample,
194 if (!TryMatchStreamDataType(arg->stream_data_type,
195 StreamDataType::kMediaSample, result_listener)) {
199 const std::string is_encrypted_string =
200 BoolToString(arg->media_sample->is_encrypted());
201 const std::string is_key_frame_string =
202 BoolToString(arg->media_sample->is_key_frame());
204 *result_listener <<
"which is (" << arg->stream_index <<
", "
205 << arg->media_sample->dts() <<
", "
206 << arg->media_sample->duration() <<
", "
207 << is_encrypted_string <<
", " << is_key_frame_string <<
")";
209 return TryMatch(arg->stream_index, stream_index, result_listener,
211 TryMatch(arg->media_sample->dts(), timestamp, result_listener,
213 TryMatch(arg->media_sample->duration(), duration, result_listener,
215 TryMatch(arg->media_sample->is_encrypted(), encrypted, result_listener,
217 TryMatch(arg->media_sample->is_key_frame(), keyframe, result_listener,
221MATCHER_P4(IsTextSample, stream_index,
id, start_time, end_time,
"") {
222 if (!TryMatchStreamDataType(arg->stream_data_type,
223 StreamDataType::kTextSample, result_listener)) {
227 *result_listener <<
"which is (" << arg->stream_index <<
", "
228 << ToPrettyString(arg->text_sample->id()) <<
", "
229 << arg->text_sample->start_time() <<
", "
230 << arg->text_sample->EndTime() <<
")";
232 return TryMatch(arg->stream_index, stream_index, result_listener,
234 TryMatch(arg->text_sample->id(),
id, result_listener,
"id") &&
235 TryMatch(arg->text_sample->start_time(), start_time, result_listener,
237 TryMatch(arg->text_sample->EndTime(), end_time, result_listener,
241MATCHER_P2(IsCueEvent, stream_index, time_in_seconds,
"") {
242 if (!TryMatchStreamDataType(arg->stream_data_type, StreamDataType::kCueEvent,
247 *result_listener <<
"which is (" << arg->stream_index <<
", "
248 << arg->cue_event->time_in_seconds <<
")";
250 return TryMatch(arg->stream_index, stream_index, result_listener,
252 TryMatch(arg->cue_event->time_in_seconds, time_in_seconds,
253 result_listener,
"time_in_seconds");
263 bool ValidateOutputStreamIndex(
size_t index)
const override;
264 Status InitializeInternal()
override;
265 Status Process(std::unique_ptr<StreamData> stream_data)
override;
270 MOCK_METHOD1(OnProcess,
void(
const StreamData*));
271 MOCK_METHOD1(OnFlush,
void(
size_t index));
274 Status InitializeInternal()
override;
275 Status Process(std::unique_ptr<StreamData> stream_data)
override;
276 Status OnFlushRequest(
size_t index)
override;
281 const std::vector<std::unique_ptr<StreamData>>& Cache()
const {
282 return stream_data_vector_;
287 void Clear() { stream_data_vector_.clear(); }
290 Status InitializeInternal()
override;
291 Status Process(std::unique_ptr<StreamData> stream_data)
override;
292 Status OnFlushRequest(
size_t input_stream_index)
override;
293 bool ValidateOutputStreamIndex(
size_t stream_index)
const override;
295 std::vector<std::unique_ptr<StreamData>> stream_data_vector_;
303 bool IsVideoCodec(Codec codec)
const;
305 std::unique_ptr<StreamInfo> GetVideoStreamInfo(int32_t time_scale)
const;
307 std::unique_ptr<StreamInfo> GetVideoStreamInfo(int32_t time_scale,
309 uint32_t height)
const;
311 std::unique_ptr<StreamInfo> GetVideoStreamInfo(int32_t time_scale,
314 std::unique_ptr<StreamInfo> GetVideoStreamInfo(int32_t time_scale,
317 uint32_t height)
const;
319 std::unique_ptr<StreamInfo> GetAudioStreamInfo(int32_t time_scale)
const;
321 std::unique_ptr<StreamInfo> GetAudioStreamInfo(int32_t time_scale,
324 std::shared_ptr<MediaSample> GetMediaSample(int64_t timestamp,
326 bool is_keyframe)
const;
328 std::shared_ptr<MediaSample> GetMediaSample(int64_t timestamp,
332 size_t data_length)
const;
334 std::unique_ptr<SegmentInfo> GetSegmentInfo(int64_t start_timestamp,
337 int64_t segment_number)
const;
339 std::unique_ptr<StreamInfo> GetTextStreamInfo(int32_t timescale)
const;
341 std::unique_ptr<TextSample> GetTextSample(
const std::string&
id,
344 const std::string& payload)
const;
346 std::unique_ptr<CueEvent> GetCueEvent(
double time_in_seconds)
const;
349 Status SetUpAndInitializeGraph(std::shared_ptr<MediaHandler> handler,
351 size_t output_count);
365 std::shared_ptr<MediaHandler> handler_;
367 std::vector<std::shared_ptr<FakeInputMediaHandler>> inputs_;
368 std::vector<std::shared_ptr<MockOutputMediaHandler>> outputs_;
379 std::shared_ptr<MediaHandler> handler);
392 std::shared_ptr<CachingMediaHandler>
next_handler() {
return next_handler_; }
400 std::shared_ptr<CachingMediaHandler> next_handler_;
402 std::shared_ptr<MediaHandler> some_handler_;
All the methods that are virtual are virtual for mocking.