2016-03-21 19:16:58 +00:00
|
|
|
// Copyright 2016 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
|
|
|
#include <gmock/gmock.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include "packager/media/base/audio_stream_info.h"
|
|
|
|
#include "packager/media/base/video_stream_info.h"
|
2016-04-16 22:58:47 +00:00
|
|
|
#include "packager/media/event/mock_muxer_listener.h"
|
2017-10-23 22:59:30 +00:00
|
|
|
#include "packager/media/formats/mp2t/pes_packet.h"
|
|
|
|
#include "packager/media/formats/mp2t/program_map_table_writer.h"
|
2016-03-21 19:16:58 +00:00
|
|
|
#include "packager/media/formats/mp2t/ts_segmenter.h"
|
2017-06-29 22:23:53 +00:00
|
|
|
#include "packager/status_test_util.h"
|
2020-07-04 22:18:30 +00:00
|
|
|
#include "packager/media/base/macros.h"
|
2016-03-21 19:16:58 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2016-03-21 19:16:58 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp2t {
|
|
|
|
|
|
|
|
using ::testing::InSequence;
|
|
|
|
using ::testing::Return;
|
|
|
|
using ::testing::Sequence;
|
|
|
|
using ::testing::StrEq;
|
|
|
|
using ::testing::_;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// All data here is bogus. They are used to create VideoStreamInfo but the
|
|
|
|
// actual values don't matter at all.
|
|
|
|
const bool kIsKeyFrame = true;
|
2016-07-27 00:51:08 +00:00
|
|
|
const Codec kH264Codec = Codec::kCodecH264;
|
2016-03-21 19:16:58 +00:00
|
|
|
const uint8_t kExtraData[] = {
|
|
|
|
0x00,
|
|
|
|
};
|
|
|
|
const int kTrackId = 0;
|
2018-07-03 23:52:05 +00:00
|
|
|
const uint32_t kZeroTransportStreamTimestampOffset = 0;
|
2016-03-21 19:16:58 +00:00
|
|
|
const uint32_t kTimeScale = 90000;
|
|
|
|
const uint64_t kDuration = 180000;
|
|
|
|
const char kCodecString[] = "avc1";
|
|
|
|
const char kLanguage[] = "eng";
|
|
|
|
const uint32_t kWidth = 1280;
|
|
|
|
const uint32_t kHeight = 720;
|
|
|
|
const uint32_t kPixelWidth = 1;
|
|
|
|
const uint32_t kPixelHeight = 1;
|
2019-09-23 06:24:33 +00:00
|
|
|
const uint8_t kTransferCharacteristics = 0;
|
2017-05-15 16:22:06 +00:00
|
|
|
const uint16_t kTrickPlayFactor = 1;
|
2016-03-21 19:16:58 +00:00
|
|
|
const uint8_t kNaluLengthSize = 1;
|
|
|
|
const bool kIsEncrypted = false;
|
|
|
|
|
2016-04-27 22:04:50 +00:00
|
|
|
const uint8_t kAnyData[] = {
|
|
|
|
0x01, 0x0F, 0x3C,
|
|
|
|
};
|
|
|
|
|
2016-03-21 19:16:58 +00:00
|
|
|
class MockPesPacketGenerator : public PesPacketGenerator {
|
|
|
|
public:
|
2018-07-03 23:52:05 +00:00
|
|
|
MockPesPacketGenerator()
|
|
|
|
: PesPacketGenerator(kZeroTransportStreamTimestampOffset) {}
|
|
|
|
|
2016-03-21 19:16:58 +00:00
|
|
|
MOCK_METHOD1(Initialize, bool(const StreamInfo& info));
|
2017-09-12 17:24:24 +00:00
|
|
|
MOCK_METHOD1(PushSample, bool(const MediaSample& sample));
|
2016-04-27 22:04:50 +00:00
|
|
|
|
2016-03-21 19:16:58 +00:00
|
|
|
MOCK_METHOD0(NumberOfReadyPesPackets, size_t());
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
// Hack found at the URL below for mocking methods that return
|
|
|
|
// std::unique_ptr.
|
2016-03-21 19:16:58 +00:00
|
|
|
// https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/01sDxsJ1OYw
|
|
|
|
MOCK_METHOD0(GetNextPesPacketMock, PesPacket*());
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<PesPacket> GetNextPesPacket() override {
|
|
|
|
return std::unique_ptr<PesPacket>(GetNextPesPacketMock());
|
2016-03-21 19:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MOCK_METHOD0(Flush, bool());
|
|
|
|
};
|
|
|
|
|
|
|
|
class MockTsWriter : public TsWriter {
|
|
|
|
public:
|
2017-10-23 22:59:30 +00:00
|
|
|
MockTsWriter()
|
|
|
|
: TsWriter(std::unique_ptr<ProgramMapTableWriter>(
|
|
|
|
// Create a bogus pmt writer, which we don't really care.
|
|
|
|
new VideoProgramMapTableWriter(kUnknownCodec))) {}
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
MOCK_METHOD1(NewSegment, bool(BufferWriter* buffer_writer));
|
2016-07-01 21:49:46 +00:00
|
|
|
MOCK_METHOD0(SignalEncrypted, void());
|
2016-03-21 19:16:58 +00:00
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
// Similar to the hack above but takes a std::unique_ptr.
|
2020-07-04 22:18:30 +00:00
|
|
|
MOCK_METHOD2(AddPesPacketMock, bool(PesPacket* pes_packet,
|
|
|
|
BufferWriter* buffer_writer));
|
|
|
|
bool AddPesPacket(std::unique_ptr<PesPacket> pes_packet,
|
|
|
|
BufferWriter* buffer_writer) override {
|
|
|
|
buffer_writer->AppendArray(kAnyData, arraysize(kAnyData));
|
2016-03-21 19:16:58 +00:00
|
|
|
// No need to keep the pes packet around for the current tests.
|
2020-07-04 22:18:30 +00:00
|
|
|
return AddPesPacketMock(pes_packet.get(), buffer_writer);
|
2016-03-21 19:16:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
class TsSegmenterTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
void SetUp() override {
|
|
|
|
mock_ts_writer_.reset(new MockTsWriter());
|
|
|
|
mock_pes_packet_generator_.reset(new MockPesPacketGenerator());
|
|
|
|
}
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<MockTsWriter> mock_ts_writer_;
|
|
|
|
std::unique_ptr<MockPesPacketGenerator> mock_pes_packet_generator_;
|
2016-03-21 19:16:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(TsSegmenterTest, Initialize) {
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<VideoStreamInfo> stream_info(new VideoStreamInfo(
|
2017-03-23 18:34:20 +00:00
|
|
|
kTrackId, kTimeScale, kDuration, kH264Codec,
|
|
|
|
H26xStreamFormat::kAnnexbByteStream, kCodecString, kExtraData,
|
2016-07-27 00:51:08 +00:00
|
|
|
arraysize(kExtraData), kWidth, kHeight, kPixelWidth, kPixelHeight,
|
2019-09-23 06:24:33 +00:00
|
|
|
kTransferCharacteristics, kTrickPlayFactor, kNaluLengthSize, kLanguage,
|
|
|
|
kIsEncrypted));
|
2016-03-21 19:16:58 +00:00
|
|
|
MuxerOptions options;
|
|
|
|
options.segment_template = "file$Number$.ts";
|
2016-04-16 22:58:47 +00:00
|
|
|
TsSegmenter segmenter(options, nullptr);
|
2016-03-21 19:16:58 +00:00
|
|
|
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, Initialize(_))
|
|
|
|
.WillOnce(Return(true));
|
|
|
|
|
|
|
|
segmenter.InjectPesPacketGeneratorForTesting(
|
2016-08-17 17:41:40 +00:00
|
|
|
std::move(mock_pes_packet_generator_));
|
2016-03-21 19:16:58 +00:00
|
|
|
|
2017-03-11 02:49:55 +00:00
|
|
|
EXPECT_OK(segmenter.Initialize(*stream_info));
|
2016-03-21 19:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TsSegmenterTest, AddSample) {
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<VideoStreamInfo> stream_info(new VideoStreamInfo(
|
2017-03-23 18:34:20 +00:00
|
|
|
kTrackId, kTimeScale, kDuration, kH264Codec,
|
|
|
|
H26xStreamFormat::kAnnexbByteStream, kCodecString, kExtraData,
|
2016-07-27 00:51:08 +00:00
|
|
|
arraysize(kExtraData), kWidth, kHeight, kPixelWidth, kPixelHeight,
|
2019-09-23 06:24:33 +00:00
|
|
|
kTransferCharacteristics, kTrickPlayFactor, kNaluLengthSize, kLanguage,
|
|
|
|
kIsEncrypted));
|
2016-03-21 19:16:58 +00:00
|
|
|
MuxerOptions options;
|
|
|
|
options.segment_template = "file$Number$.ts";
|
2016-04-16 22:58:47 +00:00
|
|
|
TsSegmenter segmenter(options, nullptr);
|
2016-03-21 19:16:58 +00:00
|
|
|
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, Initialize(_))
|
|
|
|
.WillOnce(Return(true));
|
|
|
|
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<MediaSample> sample =
|
2016-03-21 19:16:58 +00:00
|
|
|
MediaSample::CopyFrom(kAnyData, arraysize(kAnyData), kIsKeyFrame);
|
|
|
|
|
|
|
|
Sequence writer_sequence;
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_CALL(*mock_ts_writer_, NewSegment(_))
|
2016-03-21 19:16:58 +00:00
|
|
|
.InSequence(writer_sequence)
|
|
|
|
.WillOnce(Return(true));
|
|
|
|
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, PushSample(_))
|
|
|
|
.WillOnce(Return(true));
|
|
|
|
|
|
|
|
Sequence ready_pes_sequence;
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, NumberOfReadyPesPackets())
|
|
|
|
.InSequence(ready_pes_sequence)
|
|
|
|
.WillOnce(Return(1u));
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, NumberOfReadyPesPackets())
|
|
|
|
.InSequence(ready_pes_sequence)
|
|
|
|
.WillOnce(Return(0u));
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_CALL(*mock_ts_writer_, AddPesPacketMock(_, _))
|
2016-03-21 19:16:58 +00:00
|
|
|
.WillOnce(Return(true));
|
|
|
|
|
|
|
|
// The pointer is released inside the segmenter.
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, GetNextPesPacketMock())
|
2016-04-06 05:00:05 +00:00
|
|
|
.WillOnce(Return(new PesPacket()));
|
2016-03-21 19:16:58 +00:00
|
|
|
|
|
|
|
segmenter.InjectPesPacketGeneratorForTesting(
|
2016-08-17 17:41:40 +00:00
|
|
|
std::move(mock_pes_packet_generator_));
|
2016-03-21 19:16:58 +00:00
|
|
|
|
2017-03-11 02:49:55 +00:00
|
|
|
EXPECT_OK(segmenter.Initialize(*stream_info));
|
2017-10-23 22:59:30 +00:00
|
|
|
segmenter.InjectTsWriterForTesting(std::move(mock_ts_writer_));
|
2017-09-12 17:24:24 +00:00
|
|
|
EXPECT_OK(segmenter.AddSample(*sample));
|
2016-03-21 19:16:58 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 01:17:47 +00:00
|
|
|
// This will add one sample then finalize segment then add another sample.
|
2016-04-06 05:00:05 +00:00
|
|
|
TEST_F(TsSegmenterTest, PassedSegmentDuration) {
|
2016-04-19 01:00:06 +00:00
|
|
|
// Use something significantly smaller than 90000 to check that the scaling is
|
|
|
|
// done correctly in the segmenter.
|
|
|
|
const uint32_t kInputTimescale = 1001;
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<VideoStreamInfo> stream_info(new VideoStreamInfo(
|
2017-03-23 18:34:20 +00:00
|
|
|
kTrackId, kInputTimescale, kDuration, kH264Codec,
|
|
|
|
H26xStreamFormat::kAnnexbByteStream, kCodecString, kExtraData,
|
|
|
|
arraysize(kExtraData), kWidth, kHeight, kPixelWidth, kPixelHeight,
|
2019-09-23 06:24:33 +00:00
|
|
|
kTransferCharacteristics, kTrickPlayFactor, kNaluLengthSize, kLanguage,
|
|
|
|
kIsEncrypted));
|
2016-03-21 19:16:58 +00:00
|
|
|
MuxerOptions options;
|
2020-07-04 22:18:30 +00:00
|
|
|
options.segment_template = "memory://file$Number$.ts";
|
2016-04-16 22:58:47 +00:00
|
|
|
|
|
|
|
MockMuxerListener mock_listener;
|
|
|
|
TsSegmenter segmenter(options, &mock_listener);
|
|
|
|
|
|
|
|
const uint32_t kFirstPts = 1000;
|
2016-03-21 19:16:58 +00:00
|
|
|
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, Initialize(_))
|
|
|
|
.WillOnce(Return(true));
|
|
|
|
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<MediaSample> sample1 =
|
2016-04-06 05:00:05 +00:00
|
|
|
MediaSample::CopyFrom(kAnyData, arraysize(kAnyData), kIsKeyFrame);
|
2017-02-24 01:17:47 +00:00
|
|
|
sample1->set_duration(kInputTimescale * 11);
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<MediaSample> sample2 =
|
2016-03-21 19:16:58 +00:00
|
|
|
MediaSample::CopyFrom(kAnyData, arraysize(kAnyData), kIsKeyFrame);
|
2017-02-24 01:17:47 +00:00
|
|
|
// Doesn't really matter how long this is.
|
|
|
|
sample2->set_duration(kInputTimescale * 7);
|
2016-04-06 05:00:05 +00:00
|
|
|
|
2016-04-16 22:58:47 +00:00
|
|
|
EXPECT_CALL(mock_listener,
|
2020-07-04 22:18:30 +00:00
|
|
|
OnNewSegment("memory://file1.ts",
|
|
|
|
kFirstPts * kTimeScale / kInputTimescale,
|
2017-02-24 01:17:47 +00:00
|
|
|
kTimeScale * 11, _));
|
2016-04-06 05:00:05 +00:00
|
|
|
|
2016-03-21 19:16:58 +00:00
|
|
|
Sequence writer_sequence;
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_CALL(*mock_ts_writer_, NewSegment(_))
|
2016-03-21 19:16:58 +00:00
|
|
|
.InSequence(writer_sequence)
|
|
|
|
.WillOnce(Return(true));
|
|
|
|
|
2016-04-06 05:00:05 +00:00
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, PushSample(_))
|
|
|
|
.Times(2)
|
|
|
|
.WillRepeatedly(Return(true));
|
2016-03-21 19:16:58 +00:00
|
|
|
|
|
|
|
Sequence ready_pes_sequence;
|
2016-04-06 05:00:05 +00:00
|
|
|
// First AddSample().
|
2016-03-21 19:16:58 +00:00
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, NumberOfReadyPesPackets())
|
|
|
|
.InSequence(ready_pes_sequence)
|
|
|
|
.WillOnce(Return(1u));
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, NumberOfReadyPesPackets())
|
|
|
|
.InSequence(ready_pes_sequence)
|
|
|
|
.WillOnce(Return(0u));
|
2016-04-06 05:00:05 +00:00
|
|
|
// When Flush() is called, inside second AddSample().
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, NumberOfReadyPesPackets())
|
|
|
|
.InSequence(ready_pes_sequence)
|
|
|
|
.WillOnce(Return(0u));
|
|
|
|
// Still inside AddSample() but after Flush().
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, NumberOfReadyPesPackets())
|
|
|
|
.InSequence(ready_pes_sequence)
|
|
|
|
.WillOnce(Return(1u));
|
2020-07-04 22:18:30 +00:00
|
|
|
|
2016-04-06 05:00:05 +00:00
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, NumberOfReadyPesPackets())
|
|
|
|
.InSequence(ready_pes_sequence)
|
|
|
|
.WillOnce(Return(0u));
|
|
|
|
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, Flush())
|
|
|
|
.WillOnce(Return(true));
|
2016-03-21 19:16:58 +00:00
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_CALL(*mock_ts_writer_, NewSegment(_))
|
2016-04-06 05:00:05 +00:00
|
|
|
.InSequence(writer_sequence)
|
|
|
|
.WillOnce(Return(true));
|
2016-03-21 19:16:58 +00:00
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_CALL(*mock_ts_writer_, AddPesPacketMock(_, _))
|
2016-04-06 05:00:05 +00:00
|
|
|
.Times(2)
|
|
|
|
.WillRepeatedly(Return(true));
|
2016-03-21 19:16:58 +00:00
|
|
|
|
2016-04-06 05:00:05 +00:00
|
|
|
// The pointers are released inside the segmenter.
|
|
|
|
Sequence pes_packet_sequence;
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, GetNextPesPacketMock())
|
|
|
|
.InSequence(pes_packet_sequence)
|
2017-02-24 01:17:47 +00:00
|
|
|
.WillOnce(Return(new PesPacket));
|
2016-03-21 19:16:58 +00:00
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, GetNextPesPacketMock())
|
2016-04-06 05:00:05 +00:00
|
|
|
.InSequence(pes_packet_sequence)
|
|
|
|
.WillOnce(Return(new PesPacket()));
|
2016-03-21 19:16:58 +00:00
|
|
|
|
|
|
|
segmenter.InjectPesPacketGeneratorForTesting(
|
2016-08-17 17:41:40 +00:00
|
|
|
std::move(mock_pes_packet_generator_));
|
2017-03-11 02:49:55 +00:00
|
|
|
EXPECT_OK(segmenter.Initialize(*stream_info));
|
2017-10-23 22:59:30 +00:00
|
|
|
segmenter.InjectTsWriterForTesting(std::move(mock_ts_writer_));
|
2017-09-12 17:24:24 +00:00
|
|
|
EXPECT_OK(segmenter.AddSample(*sample1));
|
2017-02-24 01:17:47 +00:00
|
|
|
EXPECT_OK(segmenter.FinalizeSegment(kFirstPts, sample1->duration()));
|
2017-09-12 17:24:24 +00:00
|
|
|
EXPECT_OK(segmenter.AddSample(*sample2));
|
2016-03-21 19:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finalize right after Initialize(). The writer will not be initialized.
|
|
|
|
TEST_F(TsSegmenterTest, InitializeThenFinalize) {
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<VideoStreamInfo> stream_info(new VideoStreamInfo(
|
2017-03-23 18:34:20 +00:00
|
|
|
kTrackId, kTimeScale, kDuration, kH264Codec,
|
|
|
|
H26xStreamFormat::kAnnexbByteStream, kCodecString, kExtraData,
|
2016-07-27 00:51:08 +00:00
|
|
|
arraysize(kExtraData), kWidth, kHeight, kPixelWidth, kPixelHeight,
|
2019-09-23 06:24:33 +00:00
|
|
|
kTransferCharacteristics, kTrickPlayFactor, kNaluLengthSize, kLanguage,
|
|
|
|
kIsEncrypted));
|
2016-03-21 19:16:58 +00:00
|
|
|
MuxerOptions options;
|
|
|
|
options.segment_template = "file$Number$.ts";
|
2016-04-16 22:58:47 +00:00
|
|
|
TsSegmenter segmenter(options, nullptr);
|
2016-03-21 19:16:58 +00:00
|
|
|
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, Initialize(_))
|
|
|
|
.WillOnce(Return(true));
|
|
|
|
|
2017-02-24 01:17:47 +00:00
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, Flush()).Times(0);
|
2016-04-06 05:00:05 +00:00
|
|
|
ON_CALL(*mock_pes_packet_generator_, NumberOfReadyPesPackets())
|
|
|
|
.WillByDefault(Return(0));
|
2016-03-21 19:16:58 +00:00
|
|
|
|
|
|
|
segmenter.InjectPesPacketGeneratorForTesting(
|
2016-08-17 17:41:40 +00:00
|
|
|
std::move(mock_pes_packet_generator_));
|
2017-03-11 02:49:55 +00:00
|
|
|
EXPECT_OK(segmenter.Initialize(*stream_info));
|
2016-03-21 19:16:58 +00:00
|
|
|
EXPECT_OK(segmenter.Finalize());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the "normal" case where samples have been added and the writer has
|
|
|
|
// been initialized.
|
|
|
|
// The test does not really add any samples but instead simulates an initialized
|
|
|
|
// writer with a mock.
|
2017-02-24 01:17:47 +00:00
|
|
|
TEST_F(TsSegmenterTest, FinalizeSegment) {
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<VideoStreamInfo> stream_info(new VideoStreamInfo(
|
2017-03-23 18:34:20 +00:00
|
|
|
kTrackId, kTimeScale, kDuration, kH264Codec,
|
|
|
|
H26xStreamFormat::kAnnexbByteStream, kCodecString, kExtraData,
|
2016-07-27 00:51:08 +00:00
|
|
|
arraysize(kExtraData), kWidth, kHeight, kPixelWidth, kPixelHeight,
|
2019-09-23 06:24:33 +00:00
|
|
|
kTransferCharacteristics, kTrickPlayFactor, kNaluLengthSize, kLanguage,
|
|
|
|
kIsEncrypted));
|
2016-03-21 19:16:58 +00:00
|
|
|
MuxerOptions options;
|
|
|
|
options.segment_template = "file$Number$.ts";
|
2016-04-16 22:58:47 +00:00
|
|
|
TsSegmenter segmenter(options, nullptr);
|
2016-03-21 19:16:58 +00:00
|
|
|
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, Initialize(_))
|
|
|
|
.WillOnce(Return(true));
|
|
|
|
|
|
|
|
InSequence s;
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, Flush()).WillOnce(Return(true));
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, NumberOfReadyPesPackets())
|
|
|
|
.WillOnce(Return(0u));
|
|
|
|
|
|
|
|
segmenter.InjectPesPacketGeneratorForTesting(
|
2016-08-17 17:41:40 +00:00
|
|
|
std::move(mock_pes_packet_generator_));
|
2017-03-11 02:49:55 +00:00
|
|
|
EXPECT_OK(segmenter.Initialize(*stream_info));
|
2017-10-23 22:59:30 +00:00
|
|
|
segmenter.InjectTsWriterForTesting(std::move(mock_ts_writer_));
|
2020-07-04 22:18:30 +00:00
|
|
|
|
|
|
|
EXPECT_OK(segmenter.FinalizeSegment(0, 100 /* arbitrary duration*/));
|
2016-03-21 19:16:58 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 02:49:55 +00:00
|
|
|
TEST_F(TsSegmenterTest, EncryptedSample) {
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<VideoStreamInfo> stream_info(new VideoStreamInfo(
|
2017-03-23 18:34:20 +00:00
|
|
|
kTrackId, kTimeScale, kDuration, kH264Codec,
|
|
|
|
H26xStreamFormat::kAnnexbByteStream, kCodecString, kExtraData,
|
2016-07-27 00:51:08 +00:00
|
|
|
arraysize(kExtraData), kWidth, kHeight, kPixelWidth, kPixelHeight,
|
2019-09-23 06:24:33 +00:00
|
|
|
kTransferCharacteristics, kTrickPlayFactor, kNaluLengthSize, kLanguage,
|
|
|
|
kIsEncrypted));
|
2016-07-05 23:32:19 +00:00
|
|
|
MuxerOptions options;
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
options.segment_template = "memory://file$Number$.ts";
|
2016-04-27 22:04:50 +00:00
|
|
|
|
|
|
|
MockMuxerListener mock_listener;
|
|
|
|
TsSegmenter segmenter(options, &mock_listener);
|
|
|
|
|
|
|
|
ON_CALL(*mock_ts_writer_, NewSegment(_)).WillByDefault(Return(true));
|
2020-07-04 22:18:30 +00:00
|
|
|
ON_CALL(*mock_ts_writer_, AddPesPacketMock(_,_)).WillByDefault(Return(true));
|
2016-04-27 22:04:50 +00:00
|
|
|
ON_CALL(*mock_pes_packet_generator_, Initialize(_))
|
|
|
|
.WillByDefault(Return(true));
|
|
|
|
ON_CALL(*mock_pes_packet_generator_, Flush()).WillByDefault(Return(true));
|
|
|
|
|
|
|
|
const uint8_t kAnyData[] = {
|
|
|
|
0x01, 0x0F, 0x3C,
|
|
|
|
};
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<MediaSample> sample1 =
|
2016-04-27 22:04:50 +00:00
|
|
|
MediaSample::CopyFrom(kAnyData, arraysize(kAnyData), kIsKeyFrame);
|
2017-02-24 01:17:47 +00:00
|
|
|
sample1->set_duration(kTimeScale * 2);
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<MediaSample> sample2 =
|
2016-04-27 22:04:50 +00:00
|
|
|
MediaSample::CopyFrom(kAnyData, arraysize(kAnyData), kIsKeyFrame);
|
|
|
|
sample2->set_duration(kTimeScale * 3);
|
|
|
|
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, PushSample(_))
|
|
|
|
.Times(2)
|
|
|
|
.WillRepeatedly(Return(true));
|
|
|
|
|
|
|
|
Sequence ready_pes_sequence;
|
|
|
|
// First AddSample().
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, NumberOfReadyPesPackets())
|
|
|
|
.InSequence(ready_pes_sequence)
|
|
|
|
.WillOnce(Return(1u));
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, NumberOfReadyPesPackets())
|
|
|
|
.InSequence(ready_pes_sequence)
|
|
|
|
.WillOnce(Return(0u));
|
|
|
|
// When Flush() is called, inside second AddSample().
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, NumberOfReadyPesPackets())
|
|
|
|
.InSequence(ready_pes_sequence)
|
|
|
|
.WillOnce(Return(0u));
|
|
|
|
// Still inside AddSample() but after Flush().
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, NumberOfReadyPesPackets())
|
|
|
|
.InSequence(ready_pes_sequence)
|
|
|
|
.WillOnce(Return(1u));
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, NumberOfReadyPesPackets())
|
|
|
|
.InSequence(ready_pes_sequence)
|
|
|
|
.WillOnce(Return(0u));
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_CALL(*mock_ts_writer_, AddPesPacketMock(_, _))
|
2016-04-27 22:04:50 +00:00
|
|
|
.Times(2)
|
|
|
|
.WillRepeatedly(Return(true));
|
|
|
|
|
|
|
|
// The pointers are released inside the segmenter.
|
|
|
|
Sequence pes_packet_sequence;
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, GetNextPesPacketMock())
|
|
|
|
.InSequence(pes_packet_sequence)
|
|
|
|
.WillOnce(Return(new PesPacket()));
|
|
|
|
EXPECT_CALL(*mock_pes_packet_generator_, GetNextPesPacketMock())
|
|
|
|
.InSequence(pes_packet_sequence)
|
|
|
|
.WillOnce(Return(new PesPacket()));
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_CALL(mock_listener, OnNewSegment("memory://file1.ts", _, _, _));
|
|
|
|
|
2016-04-27 22:04:50 +00:00
|
|
|
MockTsWriter* mock_ts_writer_raw = mock_ts_writer_.get();
|
|
|
|
|
|
|
|
segmenter.InjectPesPacketGeneratorForTesting(
|
2016-08-17 17:41:40 +00:00
|
|
|
std::move(mock_pes_packet_generator_));
|
2016-04-27 22:04:50 +00:00
|
|
|
|
2017-03-11 02:49:55 +00:00
|
|
|
EXPECT_OK(segmenter.Initialize(*stream_info));
|
2017-10-23 22:59:30 +00:00
|
|
|
segmenter.InjectTsWriterForTesting(std::move(mock_ts_writer_));
|
2017-09-12 17:24:24 +00:00
|
|
|
EXPECT_OK(segmenter.AddSample(*sample1));
|
2017-03-11 02:49:55 +00:00
|
|
|
EXPECT_OK(segmenter.FinalizeSegment(1, sample1->duration()));
|
|
|
|
// Signal encrypted if sample is encrypted.
|
2016-07-01 21:49:46 +00:00
|
|
|
EXPECT_CALL(*mock_ts_writer_raw, SignalEncrypted());
|
2017-03-11 02:49:55 +00:00
|
|
|
sample2->set_is_encrypted(true);
|
2017-09-12 17:24:24 +00:00
|
|
|
EXPECT_OK(segmenter.AddSample(*sample2));
|
2016-04-27 22:04:50 +00:00
|
|
|
}
|
|
|
|
|
2016-03-21 19:16:58 +00:00
|
|
|
} // namespace mp2t
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|