7 #include <packager/media/base/media_handler_test_base.h>
9 #include <absl/log/check.h>
11 #include <packager/macros/compiler.h>
12 #include <packager/media/base/audio_stream_info.h>
13 #include <packager/media/base/text_stream_info.h>
14 #include <packager/media/base/video_stream_info.h>
15 #include <packager/status/status_test_util.h>
19 const int kTrackId = 1;
20 const int64_t kDuration = 10000;
21 const char kCodecString[] =
"codec string";
22 const uint8_t kSampleBits = 1;
23 const uint8_t kNumChannels = 2;
24 const uint32_t kSamplingFrequency = 48000;
25 const uint64_t kSeekPrerollNs = 12345;
26 const uint64_t kCodecDelayNs = 56789;
27 const uint32_t kMaxBitrate = 13579;
28 const uint32_t kAvgBitrate = 13000;
29 const char kLanguage[] =
"eng";
30 const uint32_t kWidth = 10u;
31 const uint32_t kHeight = 20u;
32 const uint32_t kPixelWidth = 2u;
33 const uint32_t kPixelHeight = 3u;
34 const uint8_t kColorPrimaries = 0;
35 const uint8_t kMatrixCoefficients = 0;
36 const uint8_t kTransferCharacteristics = 0;
38 const int16_t kTrickPlayFactor = 0;
39 const uint8_t kNaluLengthSize = 1u;
40 const bool kEncrypted =
true;
43 const uint8_t kCodecConfig[]{
46 0x01, 0x64, 0x00, 0x1e, 0xff,
51 0x67, 0x64, 0x00, 0x1e, 0xac, 0xd9, 0x40, 0xa0, 0x2f, 0xf9, 0x70, 0x11,
52 0x00, 0x00, 0x03, 0x03, 0xe9, 0x00, 0x00, 0xea, 0x60, 0x0f, 0x16, 0x2d,
58 0x68, 0xeb, 0xe3, 0xcb, 0x22, 0xc0,
63 const uint8_t kData[]{
64 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
72 std::string BoolToString(
bool value) {
73 return value ?
"true" :
"false";
76 bool TryMatchStreamDataType(
const StreamDataType& actual,
77 const StreamDataType& expected,
78 ::testing::MatchResultListener* listener) {
79 if (actual != expected) {
80 std::string expected_as_string = StreamDataTypeToString(expected);
81 std::string actual_as_string = StreamDataTypeToString(actual);
83 *listener <<
"which is " << actual_as_string <<
" (expected "
84 << expected_as_string <<
")";
91 bool TryMatchStreamType(
const StreamType& actual,
92 const StreamType& expected,
93 ::testing::MatchResultListener* listener) {
94 if (actual != expected) {
95 std::string expected_as_string = StreamTypeToString(expected);
96 std::string actual_as_string = StreamTypeToString(actual);
98 *listener <<
"which is " << actual_as_string <<
" (expected "
99 << expected_as_string <<
")";
106 std::string ToPrettyString(
const std::string& str) {
117 }
else if (isalnum(c)) {
134 bool FakeInputMediaHandler::ValidateOutputStreamIndex(
size_t index)
const {
139 Status FakeInputMediaHandler::InitializeInternal() {
143 Status FakeInputMediaHandler::Process(std::unique_ptr<StreamData> stream_data) {
145 return Status(error::INTERNAL_ERROR,
146 "FakeInputMediaHandler should never be a downstream handler.");
149 Status MockOutputMediaHandler::InitializeInternal() {
153 Status MockOutputMediaHandler::Process(
154 std::unique_ptr<StreamData> stream_data) {
155 OnProcess(stream_data.get());
159 Status MockOutputMediaHandler::OnFlushRequest(
size_t index) {
164 Status CachingMediaHandler::InitializeInternal() {
168 Status CachingMediaHandler::Process(std::unique_ptr<StreamData> stream_data) {
169 stream_data_vector_.push_back(std::move(stream_data));
173 Status CachingMediaHandler::OnFlushRequest(
size_t input_stream_index) {
174 UNUSED(input_stream_index);
178 bool CachingMediaHandler::ValidateOutputStreamIndex(
size_t stream_index)
const {
179 UNUSED(stream_index);
183 bool MediaHandlerTestBase::IsVideoCodec(Codec codec)
const {
184 return codec >= kCodecVideo && codec < kCodecVideoMaxPlusOne;
187 std::unique_ptr<StreamInfo> MediaHandlerTestBase::GetVideoStreamInfo(
188 int32_t time_scale)
const {
189 return GetVideoStreamInfo(time_scale, kCodecVP9, kWidth, kHeight);
192 std::unique_ptr<StreamInfo> MediaHandlerTestBase::GetVideoStreamInfo(
195 uint32_t height)
const {
196 return GetVideoStreamInfo(time_scale, kCodecVP9, width, height);
199 std::unique_ptr<StreamInfo> MediaHandlerTestBase::GetVideoStreamInfo(
202 return GetVideoStreamInfo(time_scale, codec, kWidth, kHeight);
205 std::unique_ptr<StreamInfo> MediaHandlerTestBase::GetVideoStreamInfo(
209 uint32_t height)
const {
210 return std::unique_ptr<VideoStreamInfo>(
new VideoStreamInfo(
211 kTrackId, time_scale, kDuration, codec, H26xStreamFormat::kUnSpecified,
212 kCodecString, kCodecConfig,
sizeof(kCodecConfig), width, height,
213 kPixelWidth, kPixelHeight, kColorPrimaries, kMatrixCoefficients,
214 kTransferCharacteristics, kTrickPlayFactor, kNaluLengthSize, kLanguage,
218 std::unique_ptr<StreamInfo> MediaHandlerTestBase::GetAudioStreamInfo(
219 int32_t time_scale)
const {
220 return GetAudioStreamInfo(time_scale, kCodecAAC);
223 std::unique_ptr<StreamInfo> MediaHandlerTestBase::GetAudioStreamInfo(
226 return std::unique_ptr<AudioStreamInfo>(
new AudioStreamInfo(
227 kTrackId, time_scale, kDuration, codec, kCodecString, kCodecConfig,
228 sizeof(kCodecConfig), kSampleBits, kNumChannels, kSamplingFrequency,
229 kSeekPrerollNs, kCodecDelayNs, kMaxBitrate, kAvgBitrate, kLanguage,
233 std::shared_ptr<MediaSample> MediaHandlerTestBase::GetMediaSample(
236 bool is_keyframe)
const {
237 return GetMediaSample(timestamp, duration, is_keyframe, kData,
sizeof(kData));
240 std::shared_ptr<MediaSample> MediaHandlerTestBase::GetMediaSample(
245 size_t data_length)
const {
246 std::shared_ptr<MediaSample> sample =
248 sample->set_dts(timestamp);
249 sample->set_pts(timestamp);
250 sample->set_duration(duration);
255 std::unique_ptr<SegmentInfo> MediaHandlerTestBase::GetSegmentInfo(
256 int64_t start_timestamp,
259 int64_t segment_number)
const {
260 std::unique_ptr<SegmentInfo> info(
new SegmentInfo);
261 info->start_timestamp = start_timestamp;
262 info->duration = duration;
263 info->is_subsegment = is_subsegment;
264 info->segment_number = segment_number;
269 std::unique_ptr<StreamInfo> MediaHandlerTestBase::GetTextStreamInfo(
270 int32_t timescale)
const {
273 return std::unique_ptr<StreamInfo>(
274 new TextStreamInfo(0, timescale, 0, kUnknownCodec,
"",
"", 0, 0,
""));
277 std::unique_ptr<TextSample> MediaHandlerTestBase::GetTextSample(
278 const std::string&
id,
281 const std::string& payload)
const {
282 return std::unique_ptr<TextSample>{
283 new TextSample(
id, start, end, {}, TextFragment{{}, payload})};
286 std::unique_ptr<CueEvent> MediaHandlerTestBase::GetCueEvent(
287 double time_in_seconds)
const {
288 std::unique_ptr<CueEvent> event(
new CueEvent);
289 event->time_in_seconds = time_in_seconds;
294 Status MediaHandlerTestBase::SetUpAndInitializeGraph(
295 std::shared_ptr<MediaHandler> handler,
297 size_t output_count) {
299 DCHECK_EQ(
nullptr, handler_);
300 DCHECK(inputs_.empty());
301 DCHECK(outputs_.empty());
303 handler_ = std::move(handler);
308 for (
size_t i = 0; i < input_count; i++) {
309 inputs_.emplace_back(
new FakeInputMediaHandler);
312 for (
auto& input : inputs_) {
313 status.Update(input->AddHandler(handler_));
321 for (
size_t i = 0; i < output_count; i++) {
322 outputs_.emplace_back(
new testing::NiceMock<MockOutputMediaHandler>);
325 for (
auto& output : outputs_) {
326 status.Update(handler_->AddHandler(output));
334 for (
auto& input : inputs_) {
335 status.Update(input->Initialize());
341 if (inputs_.empty()) {
342 status.Update(handler_->Initialize());
348 FakeInputMediaHandler* MediaHandlerTestBase::Input(
size_t index) {
349 DCHECK_LT(index, inputs_.size());
350 return inputs_[index].get();
353 MockOutputMediaHandler* MediaHandlerTestBase::Output(
size_t index) {
354 DCHECK_LT(index, outputs_.size());
355 return outputs_[index].get();
358 MediaHandlerGraphTestBase::MediaHandlerGraphTestBase()
359 : next_handler_(new CachingMediaHandler),
360 some_handler_(new CachingMediaHandler) {}
362 void MediaHandlerGraphTestBase::SetUpGraph(
365 std::shared_ptr<MediaHandler> handler) {
368 auto input_handler = std::make_shared<CachingMediaHandler>();
369 for (
size_t i = 0; i < num_inputs; ++i)
370 ASSERT_OK(input_handler->SetHandler(i, handler));
372 for (
size_t i = 0; i < num_outputs; ++i)
373 ASSERT_OK(handler->SetHandler(i, next_handler_));
376 const std::vector<std::unique_ptr<StreamData>>&
377 MediaHandlerGraphTestBase::GetOutputStreamDataVector()
const {
378 return next_handler_->Cache();
381 void MediaHandlerGraphTestBase::ClearOutputStreamDataVector() {
382 next_handler_->Clear();
All the methods that are virtual are virtual for mocking.