7#ifndef PACKAGER_MEDIA_BASE_MEDIA_HANDLER_TEST_BASE_H_
8#define PACKAGER_MEDIA_BASE_MEDIA_HANDLER_TEST_BASE_H_
12#include <absl/strings/escaping.h>
13#include <absl/strings/numbers.h>
14#include <gmock/gmock.h>
15#include <gtest/gtest.h>
17#include <packager/media/base/media_handler.h>
18#include <packager/media/base/video_stream_info.h>
19#include <packager/utils/bytes_to_string_view.h>
24std::string BoolToString(
bool value);
25std::string ToPrettyString(
const std::string& str);
27bool TryMatchStreamDataType(
const StreamDataType& actual,
28 const StreamDataType& expected,
29 ::testing::MatchResultListener* listener);
31bool TryMatchStreamType(
const StreamType& actual,
32 const StreamType& expected,
33 ::testing::MatchResultListener* listener);
35template <
typename T,
typename M>
36bool TryMatch(
const T& value,
38 ::testing::MatchResultListener* listener,
39 const char* value_name) {
40 if (!ExplainMatchResult(matcher, value, listener)) {
43 *listener <<
" Mismatch on " << value_name;
50MATCHER_P(IsPsshInfoWithSystemId,
52 std::string(negation ?
"doesn't " :
"") +
" have system ID " +
53 testing::PrintToString(system_id)) {
54 *result_listener <<
"which is (" << testing::PrintToString(arg.system_id)
56 return arg.system_id == system_id;
59MATCHER_P4(IsStreamInfo, stream_index, time_scale, encrypted, language,
"") {
60 if (!TryMatchStreamDataType(arg->stream_data_type,
61 StreamDataType::kStreamInfo, result_listener)) {
65 const std::string is_encrypted_string =
66 BoolToString(arg->stream_info->is_encrypted());
68 *result_listener <<
"which is (" << arg->stream_index <<
", "
69 << arg->stream_info->time_scale() <<
", "
70 << is_encrypted_string <<
", "
71 << arg->stream_info->language() <<
")";
73 return TryMatch(arg->stream_index, stream_index, result_listener,
75 TryMatch(arg->stream_info->time_scale(), time_scale, result_listener,
77 TryMatch(arg->stream_info->is_encrypted(), encrypted, result_listener,
79 TryMatch(arg->stream_info->language(), language, result_listener,
83MATCHER_P3(IsVideoStream, stream_index, trick_play_factor, playback_rate,
"") {
84 if (!TryMatchStreamDataType(arg->stream_data_type,
85 StreamDataType::kStreamInfo, result_listener)) {
89 if (!TryMatchStreamType(arg->stream_info->stream_type(), kStreamVideo,
94 const VideoStreamInfo* info =
95 static_cast<const VideoStreamInfo*
>(arg->stream_info.get());
97 *result_listener <<
"which is (" << arg->stream_index <<
", "
98 << info->trick_play_factor() <<
", " << info->playback_rate()
101 return TryMatch(arg->stream_index, stream_index, result_listener,
103 TryMatch(info->trick_play_factor(), trick_play_factor, result_listener,
104 "trick_play_factor") &&
105 TryMatch(info->playback_rate(), playback_rate, result_listener,
109MATCHER_P5(IsSegmentInfo,
116 if (!TryMatchStreamDataType(arg->stream_data_type,
117 StreamDataType::kSegmentInfo, result_listener)) {
121 const std::string is_subsegment_string =
122 BoolToString(arg->segment_info->is_subsegment);
123 const std::string is_encrypted_string =
124 BoolToString(arg->segment_info->is_encrypted);
126 *result_listener <<
"which is (" << arg->stream_index <<
", "
127 << arg->segment_info->start_timestamp <<
", "
128 << arg->segment_info->duration <<
", "
129 << is_subsegment_string <<
", " << is_encrypted_string
132 return TryMatch(arg->stream_index, stream_index, result_listener,
134 TryMatch(arg->segment_info->start_timestamp, start_timestamp,
135 result_listener,
"start_timestamp") &&
136 TryMatch(arg->segment_info->duration, duration, result_listener,
138 TryMatch(arg->segment_info->is_subsegment, subsegment, result_listener,
140 TryMatch(arg->segment_info->is_encrypted, encrypted, result_listener,
144MATCHER_P6(MatchEncryptionConfig,
152 const std::string constant_iv_hex = absl::BytesToHexString(
153 std::string(std::begin(arg.constant_iv), std::end(arg.constant_iv)));
154 const std::string key_id_hex = absl::BytesToHexString(
155 std::string(std::begin(arg.key_id), std::end(arg.key_id)));
156 const std::string protection_scheme_as_string =
157 FourCCToString(arg.protection_scheme);
160 const int crypt_byte_as_int =
static_cast<int>(arg.crypt_byte_block);
161 const int skip_byte_as_int =
static_cast<int>(arg.skip_byte_block);
163 *result_listener <<
"which is (" << protection_scheme_as_string <<
", "
164 << crypt_byte_as_int <<
", " << skip_byte_as_int <<
", "
165 << arg.per_sample_iv_size <<
", " << constant_iv_hex <<
", "
166 << key_id_hex <<
")";
168 return TryMatch(arg.protection_scheme, protection_scheme, result_listener,
169 "protection_scheme") &&
170 TryMatch(arg.crypt_byte_block, crypt_byte_block, result_listener,
171 "crypt_byte_block") &&
172 TryMatch(arg.skip_byte_block, skip_byte_block, result_listener,
173 "skip_byte_block") &&
174 TryMatch(arg.per_sample_iv_size, per_sample_iv_size, result_listener,
175 "per_sample_iv_size") &&
176 TryMatch(arg.constant_iv, constant_iv, result_listener,
178 TryMatch(arg.key_id, key_id, result_listener,
"key_id");
181MATCHER_P5(IsMediaSample,
188 if (!TryMatchStreamDataType(arg->stream_data_type,
189 StreamDataType::kMediaSample, result_listener)) {
193 const std::string is_encrypted_string =
194 BoolToString(arg->media_sample->is_encrypted());
195 const std::string is_key_frame_string =
196 BoolToString(arg->media_sample->is_key_frame());
198 *result_listener <<
"which is (" << arg->stream_index <<
", "
199 << arg->media_sample->dts() <<
", "
200 << arg->media_sample->duration() <<
", "
201 << is_encrypted_string <<
", " << is_key_frame_string <<
")";
203 return TryMatch(arg->stream_index, stream_index, result_listener,
205 TryMatch(arg->media_sample->dts(), timestamp, result_listener,
207 TryMatch(arg->media_sample->duration(), duration, result_listener,
209 TryMatch(arg->media_sample->is_encrypted(), encrypted, result_listener,
211 TryMatch(arg->media_sample->is_key_frame(), keyframe, result_listener,
215MATCHER_P4(IsTextSample, stream_index,
id, start_time, end_time,
"") {
216 if (!TryMatchStreamDataType(arg->stream_data_type,
217 StreamDataType::kTextSample, result_listener)) {
221 *result_listener <<
"which is (" << arg->stream_index <<
", "
222 << ToPrettyString(arg->text_sample->id()) <<
", "
223 << arg->text_sample->start_time() <<
", "
224 << arg->text_sample->EndTime() <<
")";
226 return TryMatch(arg->stream_index, stream_index, result_listener,
228 TryMatch(arg->text_sample->id(),
id, result_listener,
"id") &&
229 TryMatch(arg->text_sample->start_time(), start_time, result_listener,
231 TryMatch(arg->text_sample->EndTime(), end_time, result_listener,
235MATCHER_P2(IsCueEvent, stream_index, time_in_seconds,
"") {
236 if (!TryMatchStreamDataType(arg->stream_data_type, StreamDataType::kCueEvent,
241 *result_listener <<
"which is (" << arg->stream_index <<
", "
242 << arg->cue_event->time_in_seconds <<
")";
244 return TryMatch(arg->stream_index, stream_index, result_listener,
246 TryMatch(arg->cue_event->time_in_seconds, time_in_seconds,
247 result_listener,
"time_in_seconds");
257 bool ValidateOutputStreamIndex(
size_t index)
const override;
258 Status InitializeInternal()
override;
259 Status Process(std::unique_ptr<StreamData> stream_data)
override;
264 MOCK_METHOD1(OnProcess,
void(
const StreamData*));
265 MOCK_METHOD1(OnFlush,
void(
size_t index));
268 Status InitializeInternal()
override;
269 Status Process(std::unique_ptr<StreamData> stream_data)
override;
270 Status OnFlushRequest(
size_t index)
override;
275 const std::vector<std::unique_ptr<StreamData>>& Cache()
const {
276 return stream_data_vector_;
281 void Clear() { stream_data_vector_.clear(); }
284 Status InitializeInternal()
override;
285 Status Process(std::unique_ptr<StreamData> stream_data)
override;
286 Status OnFlushRequest(
size_t input_stream_index)
override;
287 bool ValidateOutputStreamIndex(
size_t stream_index)
const override;
289 std::vector<std::unique_ptr<StreamData>> stream_data_vector_;
297 bool IsVideoCodec(Codec codec)
const;
299 std::unique_ptr<StreamInfo> GetVideoStreamInfo(int32_t time_scale)
const;
301 std::unique_ptr<StreamInfo> GetVideoStreamInfo(int32_t time_scale,
303 uint32_t height)
const;
305 std::unique_ptr<StreamInfo> GetVideoStreamInfo(int32_t time_scale,
308 std::unique_ptr<StreamInfo> GetVideoStreamInfo(int32_t time_scale,
311 uint32_t height)
const;
313 std::unique_ptr<StreamInfo> GetAudioStreamInfo(int32_t time_scale)
const;
315 std::unique_ptr<StreamInfo> GetAudioStreamInfo(int32_t time_scale,
318 std::shared_ptr<MediaSample> GetMediaSample(int64_t timestamp,
320 bool is_keyframe)
const;
322 std::shared_ptr<MediaSample> GetMediaSample(int64_t timestamp,
326 size_t data_length)
const;
328 std::unique_ptr<SegmentInfo> GetSegmentInfo(int64_t start_timestamp,
331 int64_t segment_number)
const;
333 std::unique_ptr<StreamInfo> GetTextStreamInfo(int32_t timescale)
const;
335 std::unique_ptr<TextSample> GetTextSample(
const std::string&
id,
338 const std::string& payload)
const;
340 std::unique_ptr<CueEvent> GetCueEvent(
double time_in_seconds)
const;
343 Status SetUpAndInitializeGraph(std::shared_ptr<MediaHandler> handler,
345 size_t output_count);
359 std::shared_ptr<MediaHandler> handler_;
361 std::vector<std::shared_ptr<FakeInputMediaHandler>> inputs_;
362 std::vector<std::shared_ptr<MockOutputMediaHandler>> outputs_;
373 std::shared_ptr<MediaHandler> handler);
386 std::shared_ptr<CachingMediaHandler>
next_handler() {
return next_handler_; }
394 std::shared_ptr<CachingMediaHandler> next_handler_;
396 std::shared_ptr<MediaHandler> some_handler_;
All the methods that are virtual are virtual for mocking.