7 #ifndef PACKAGER_MEDIA_BASE_MEDIA_HANDLER_TEST_BASE_H_
8 #define PACKAGER_MEDIA_BASE_MEDIA_HANDLER_TEST_BASE_H_
10 #include <absl/strings/escaping.h>
11 #include <absl/strings/numbers.h>
12 #include <gmock/gmock.h>
13 #include <gtest/gtest.h>
15 #include <packager/media/base/media_handler.h>
16 #include <packager/media/base/video_stream_info.h>
17 #include <packager/utils/bytes_to_string_view.h>
22 std::string BoolToString(
bool value);
23 std::string ToPrettyString(
const std::string& str);
25 bool TryMatchStreamDataType(
const StreamDataType& actual,
26 const StreamDataType& expected,
27 ::testing::MatchResultListener* listener);
29 bool TryMatchStreamType(
const StreamType& actual,
30 const StreamType& expected,
31 ::testing::MatchResultListener* listener);
33 template <
typename T,
typename M>
34 bool TryMatch(
const T& value,
36 ::testing::MatchResultListener* listener,
37 const char* value_name) {
38 if (!ExplainMatchResult(matcher, value, listener)) {
41 *listener <<
" Mismatch on " << value_name;
48 MATCHER_P(IsPsshInfoWithSystemId,
50 std::string(negation ?
"doesn't " :
"") +
" have system ID " +
51 testing::PrintToString(system_id)) {
52 *result_listener <<
"which is (" << testing::PrintToString(arg.system_id)
54 return arg.system_id == system_id;
57 MATCHER_P4(IsStreamInfo, stream_index, time_scale, encrypted, language,
"") {
58 if (!TryMatchStreamDataType(arg->stream_data_type,
59 StreamDataType::kStreamInfo, result_listener)) {
63 const std::string is_encrypted_string =
64 BoolToString(arg->stream_info->is_encrypted());
66 *result_listener <<
"which is (" << arg->stream_index <<
", "
67 << arg->stream_info->time_scale() <<
", "
68 << is_encrypted_string <<
", "
69 << arg->stream_info->language() <<
")";
71 return TryMatch(arg->stream_index, stream_index, result_listener,
73 TryMatch(arg->stream_info->time_scale(), time_scale, result_listener,
75 TryMatch(arg->stream_info->is_encrypted(), encrypted, result_listener,
77 TryMatch(arg->stream_info->language(), language, result_listener,
81 MATCHER_P3(IsVideoStream, stream_index, trick_play_factor, playback_rate,
"") {
82 if (!TryMatchStreamDataType(arg->stream_data_type,
83 StreamDataType::kStreamInfo, result_listener)) {
87 if (!TryMatchStreamType(arg->stream_info->stream_type(), kStreamVideo,
92 const VideoStreamInfo* info =
93 static_cast<const VideoStreamInfo*
>(arg->stream_info.get());
95 *result_listener <<
"which is (" << arg->stream_index <<
", "
96 << info->trick_play_factor() <<
", " << info->playback_rate()
99 return TryMatch(arg->stream_index, stream_index, result_listener,
101 TryMatch(info->trick_play_factor(), trick_play_factor, result_listener,
102 "trick_play_factor") &&
103 TryMatch(info->playback_rate(), playback_rate, result_listener,
107 MATCHER_P5(IsSegmentInfo,
114 if (!TryMatchStreamDataType(arg->stream_data_type,
115 StreamDataType::kSegmentInfo, result_listener)) {
119 const std::string is_subsegment_string =
120 BoolToString(arg->segment_info->is_subsegment);
121 const std::string is_encrypted_string =
122 BoolToString(arg->segment_info->is_encrypted);
124 *result_listener <<
"which is (" << arg->stream_index <<
", "
125 << arg->segment_info->start_timestamp <<
", "
126 << arg->segment_info->duration <<
", "
127 << is_subsegment_string <<
", " << is_encrypted_string
130 return TryMatch(arg->stream_index, stream_index, result_listener,
132 TryMatch(arg->segment_info->start_timestamp, start_timestamp,
133 result_listener,
"start_timestamp") &&
134 TryMatch(arg->segment_info->duration, duration, result_listener,
136 TryMatch(arg->segment_info->is_subsegment, subsegment, result_listener,
138 TryMatch(arg->segment_info->is_encrypted, encrypted, result_listener,
142 MATCHER_P6(MatchEncryptionConfig,
150 const std::string constant_iv_hex = absl::BytesToHexString(
151 std::string(std::begin(arg.constant_iv), std::end(arg.constant_iv)));
152 const std::string key_id_hex = absl::BytesToHexString(
153 std::string(std::begin(arg.key_id), std::end(arg.key_id)));
154 const std::string protection_scheme_as_string =
155 FourCCToString(arg.protection_scheme);
158 const int crypt_byte_as_int =
static_cast<int>(arg.crypt_byte_block);
159 const int skip_byte_as_int =
static_cast<int>(arg.skip_byte_block);
161 *result_listener <<
"which is (" << protection_scheme_as_string <<
", "
162 << crypt_byte_as_int <<
", " << skip_byte_as_int <<
", "
163 << arg.per_sample_iv_size <<
", " << constant_iv_hex <<
", "
164 << key_id_hex <<
")";
166 return TryMatch(arg.protection_scheme, protection_scheme, result_listener,
167 "protection_scheme") &&
168 TryMatch(arg.crypt_byte_block, crypt_byte_block, result_listener,
169 "crypt_byte_block") &&
170 TryMatch(arg.skip_byte_block, skip_byte_block, result_listener,
171 "skip_byte_block") &&
172 TryMatch(arg.per_sample_iv_size, per_sample_iv_size, result_listener,
173 "per_sample_iv_size") &&
174 TryMatch(arg.constant_iv, constant_iv, result_listener,
176 TryMatch(arg.key_id, key_id, result_listener,
"key_id");
179 MATCHER_P5(IsMediaSample,
186 if (!TryMatchStreamDataType(arg->stream_data_type,
187 StreamDataType::kMediaSample, result_listener)) {
191 const std::string is_encrypted_string =
192 BoolToString(arg->media_sample->is_encrypted());
193 const std::string is_key_frame_string =
194 BoolToString(arg->media_sample->is_key_frame());
196 *result_listener <<
"which is (" << arg->stream_index <<
", "
197 << arg->media_sample->dts() <<
", "
198 << arg->media_sample->duration() <<
", "
199 << is_encrypted_string <<
", " << is_key_frame_string <<
")";
201 return TryMatch(arg->stream_index, stream_index, result_listener,
203 TryMatch(arg->media_sample->dts(), timestamp, result_listener,
205 TryMatch(arg->media_sample->duration(), duration, result_listener,
207 TryMatch(arg->media_sample->is_encrypted(), encrypted, result_listener,
209 TryMatch(arg->media_sample->is_key_frame(), keyframe, result_listener,
213 MATCHER_P4(IsTextSample, stream_index,
id, start_time, end_time,
"") {
214 if (!TryMatchStreamDataType(arg->stream_data_type,
215 StreamDataType::kTextSample, result_listener)) {
219 *result_listener <<
"which is (" << arg->stream_index <<
", "
220 << ToPrettyString(arg->text_sample->id()) <<
", "
221 << arg->text_sample->start_time() <<
", "
222 << arg->text_sample->EndTime() <<
")";
224 return TryMatch(arg->stream_index, stream_index, result_listener,
226 TryMatch(arg->text_sample->id(),
id, result_listener,
"id") &&
227 TryMatch(arg->text_sample->start_time(), start_time, result_listener,
229 TryMatch(arg->text_sample->EndTime(), end_time, result_listener,
233 MATCHER_P2(IsCueEvent, stream_index, time_in_seconds,
"") {
234 if (!TryMatchStreamDataType(arg->stream_data_type, StreamDataType::kCueEvent,
239 *result_listener <<
"which is (" << arg->stream_index <<
", "
240 << arg->cue_event->time_in_seconds <<
")";
242 return TryMatch(arg->stream_index, stream_index, result_listener,
244 TryMatch(arg->cue_event->time_in_seconds, time_in_seconds,
245 result_listener,
"time_in_seconds");
255 bool ValidateOutputStreamIndex(
size_t index)
const override;
256 Status InitializeInternal()
override;
257 Status Process(std::unique_ptr<StreamData> stream_data)
override;
262 MOCK_METHOD1(OnProcess,
void(
const StreamData*));
263 MOCK_METHOD1(OnFlush,
void(
size_t index));
266 Status InitializeInternal()
override;
267 Status Process(std::unique_ptr<StreamData> stream_data)
override;
268 Status OnFlushRequest(
size_t index)
override;
273 const std::vector<std::unique_ptr<StreamData>>& Cache()
const {
274 return stream_data_vector_;
279 void Clear() { stream_data_vector_.clear(); }
282 Status InitializeInternal()
override;
283 Status Process(std::unique_ptr<StreamData> stream_data)
override;
284 Status OnFlushRequest(
size_t input_stream_index)
override;
285 bool ValidateOutputStreamIndex(
size_t stream_index)
const override;
287 std::vector<std::unique_ptr<StreamData>> stream_data_vector_;
295 bool IsVideoCodec(Codec codec)
const;
297 std::unique_ptr<StreamInfo> GetVideoStreamInfo(int32_t time_scale)
const;
299 std::unique_ptr<StreamInfo> GetVideoStreamInfo(int32_t time_scale,
301 uint32_t height)
const;
303 std::unique_ptr<StreamInfo> GetVideoStreamInfo(int32_t time_scale,
306 std::unique_ptr<StreamInfo> GetVideoStreamInfo(int32_t time_scale,
309 uint32_t height)
const;
311 std::unique_ptr<StreamInfo> GetAudioStreamInfo(int32_t time_scale)
const;
313 std::unique_ptr<StreamInfo> GetAudioStreamInfo(int32_t time_scale,
316 std::shared_ptr<MediaSample> GetMediaSample(int64_t timestamp,
318 bool is_keyframe)
const;
320 std::shared_ptr<MediaSample> GetMediaSample(int64_t timestamp,
324 size_t data_length)
const;
326 std::unique_ptr<SegmentInfo> GetSegmentInfo(int64_t start_timestamp,
329 int64_t segment_number)
const;
331 std::unique_ptr<StreamInfo> GetTextStreamInfo(int32_t timescale)
const;
333 std::unique_ptr<TextSample> GetTextSample(
const std::string&
id,
336 const std::string& payload)
const;
338 std::unique_ptr<CueEvent> GetCueEvent(
double time_in_seconds)
const;
341 Status SetUpAndInitializeGraph(std::shared_ptr<MediaHandler> handler,
343 size_t output_count);
357 std::shared_ptr<MediaHandler> handler_;
359 std::vector<std::shared_ptr<FakeInputMediaHandler>> inputs_;
360 std::vector<std::shared_ptr<MockOutputMediaHandler>> outputs_;
371 std::shared_ptr<MediaHandler> handler);
384 std::shared_ptr<CachingMediaHandler>
next_handler() {
return next_handler_; }
392 std::shared_ptr<CachingMediaHandler> next_handler_;
394 std::shared_ptr<MediaHandler> some_handler_;
All the methods that are virtual are virtual for mocking.