2016-03-21 00:04:54 +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
|
|
|
|
|
2016-04-14 05:40:24 +00:00
|
|
|
#include <gmock/gmock.h>
|
2016-03-21 00:04:54 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include "packager/base/files/file_path.h"
|
|
|
|
#include "packager/base/files/file_util.h"
|
|
|
|
#include "packager/media/base/audio_stream_info.h"
|
2016-04-14 05:40:24 +00:00
|
|
|
#include "packager/media/base/buffer_writer.h"
|
2016-03-21 00:04:54 +00:00
|
|
|
#include "packager/media/base/video_stream_info.h"
|
|
|
|
#include "packager/media/formats/mp2t/pes_packet.h"
|
2017-10-23 22:59:30 +00:00
|
|
|
#include "packager/media/formats/mp2t/program_map_table_writer.h"
|
2016-03-21 00:04:54 +00:00
|
|
|
#include "packager/media/formats/mp2t/ts_writer.h"
|
|
|
|
|
2016-04-14 05:40:24 +00:00
|
|
|
using ::testing::InSequence;
|
|
|
|
using ::testing::Return;
|
|
|
|
using ::testing::_;
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2016-03-21 00:04:54 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp2t {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const int kTsPacketSize = 188;
|
2017-10-23 22:59:30 +00:00
|
|
|
const Codec kCodecForTesting = kCodecH264;
|
2016-04-14 05:40:24 +00:00
|
|
|
|
|
|
|
class MockProgramMapTableWriter : public ProgramMapTableWriter {
|
|
|
|
public:
|
2017-10-23 22:59:30 +00:00
|
|
|
MockProgramMapTableWriter() : ProgramMapTableWriter(kCodecForTesting) {}
|
|
|
|
~MockProgramMapTableWriter() override = default;
|
2016-04-14 05:40:24 +00:00
|
|
|
|
|
|
|
MOCK_METHOD1(EncryptedSegmentPmt, bool(BufferWriter* writer));
|
|
|
|
MOCK_METHOD1(ClearSegmentPmt, bool(BufferWriter* writer));
|
2017-10-18 20:25:20 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
MockProgramMapTableWriter(const MockProgramMapTableWriter&) = delete;
|
|
|
|
MockProgramMapTableWriter& operator=(const MockProgramMapTableWriter&) =
|
|
|
|
delete;
|
|
|
|
|
|
|
|
bool WriteDescriptors(BufferWriter* writer) const override { return true; }
|
2016-04-14 05:40:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// This is not a real TS Packet. But is used to check that the result from the
|
|
|
|
// MockProgramMapTableWriter is used at the right place.
|
|
|
|
const uint8_t kMockPmtWriterData[] = {
|
|
|
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
|
|
|
|
0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
|
|
|
0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
|
|
|
|
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
|
|
|
|
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
|
|
|
|
0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
|
|
|
|
0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53,
|
|
|
|
0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
|
|
|
|
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B,
|
|
|
|
0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
|
|
|
|
0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83,
|
|
|
|
0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
|
|
|
|
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B,
|
|
|
|
0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
|
|
|
|
0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3,
|
|
|
|
0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB,
|
2016-03-21 00:04:54 +00:00
|
|
|
};
|
|
|
|
|
2016-04-14 05:40:24 +00:00
|
|
|
ACTION(WriteOnePmt) {
|
|
|
|
BufferWriter* writer = arg0;
|
|
|
|
writer->AppendArray(kMockPmtWriterData, arraysize(kMockPmtWriterData));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ACTION(WriteTwoPmts) {
|
|
|
|
BufferWriter* writer = arg0;
|
|
|
|
writer->AppendArray(kMockPmtWriterData, arraysize(kMockPmtWriterData));
|
|
|
|
writer->AppendArray(kMockPmtWriterData, arraysize(kMockPmtWriterData));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-21 00:04:54 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
class TsWriterTest : public ::testing::Test {
|
|
|
|
protected:
|
2020-07-04 22:18:30 +00:00
|
|
|
|
2016-03-21 00:04:54 +00:00
|
|
|
// Checks whether |actual|'s prefix matches with |prefix| and the suffix
|
|
|
|
// matches with |suffix|. If there is padding, then padding_length specifies
|
|
|
|
// how long the padding is between prefix and suffix.
|
|
|
|
// |actual| must be at least 188 bytes long.
|
2016-04-14 05:40:24 +00:00
|
|
|
void ExpectTsPacketEqual(const uint8_t* prefix,
|
|
|
|
size_t prefix_size,
|
2016-03-21 00:04:54 +00:00
|
|
|
int padding_length,
|
2016-04-14 05:40:24 +00:00
|
|
|
const uint8_t* suffix,
|
|
|
|
size_t suffix_size,
|
2016-03-21 00:04:54 +00:00
|
|
|
const uint8_t* actual) {
|
|
|
|
std::vector<uint8_t> actual_prefix(actual, actual + prefix_size);
|
|
|
|
EXPECT_EQ(std::vector<uint8_t>(prefix, prefix + prefix_size),
|
|
|
|
actual_prefix);
|
|
|
|
|
|
|
|
// Padding until the payload.
|
|
|
|
for (size_t i = prefix_size; i < kTsPacketSize - suffix_size; ++i) {
|
|
|
|
EXPECT_EQ(0xFF, actual[i]) << "at index " << i;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint8_t> actual_suffix(actual + prefix_size + padding_length,
|
|
|
|
actual + kTsPacketSize);
|
|
|
|
EXPECT_EQ(std::vector<uint8_t>(suffix, suffix + suffix_size),
|
|
|
|
actual_suffix);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-04-14 05:40:24 +00:00
|
|
|
// Verify that PAT and PMT are correct for clear segment.
|
|
|
|
// This test covers verifies the PAT, and since it doesn't change, other tests
|
|
|
|
// shouldn't have to check this.
|
|
|
|
TEST_F(TsWriterTest, ClearH264Psi) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<MockProgramMapTableWriter> mock_pmt_writer(
|
2016-04-14 05:40:24 +00:00
|
|
|
new MockProgramMapTableWriter());
|
|
|
|
EXPECT_CALL(*mock_pmt_writer, ClearSegmentPmt(_)).WillOnce(WriteOnePmt());
|
2020-07-04 22:18:30 +00:00
|
|
|
BufferWriter buffer_writer;
|
2016-04-14 05:40:24 +00:00
|
|
|
|
2017-10-23 22:59:30 +00:00
|
|
|
TsWriter ts_writer(std::move(mock_pmt_writer));
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_TRUE(ts_writer.NewSegment(&buffer_writer));
|
2016-04-14 05:40:24 +00:00
|
|
|
// 2 TS Packets one for PAT and the fake PMT data.
|
2020-07-04 22:18:30 +00:00
|
|
|
ASSERT_EQ(376u, buffer_writer.Size());
|
2016-03-21 00:04:54 +00:00
|
|
|
|
|
|
|
const uint8_t kExpectedPatPrefix[] = {
|
2016-04-14 05:40:24 +00:00
|
|
|
0x47, // Sync byte.
|
|
|
|
0x40, // payload_unit_start_indicator set.
|
|
|
|
0x00, // pid.
|
|
|
|
0x30, // Adaptation field and payload are both present. counter = 0.
|
|
|
|
0xA6, // Adaptation Field length.
|
|
|
|
0x00, // All adaptation field flags 0.
|
2016-03-21 00:04:54 +00:00
|
|
|
};
|
|
|
|
const int kExpectedPatPrefixSize = arraysize(kExpectedPatPrefix);
|
|
|
|
const uint8_t kExpectedPatPayload[] = {
|
|
|
|
0x00, // pointer field
|
|
|
|
0x00,
|
|
|
|
0xB0, // The last 2 '00' assumes that this PAT is not very long.
|
|
|
|
0x0D, // Length of the rest of this array.
|
|
|
|
0x00, 0x00, // Transport stream ID is 0.
|
|
|
|
0xC1, // version number 0, current next indicator 1.
|
|
|
|
0x00, // section number
|
|
|
|
0x00, // last section number
|
|
|
|
// program number -> PMT PID mapping.
|
|
|
|
0x00, 0x01, // program number is 1.
|
|
|
|
0xE0, // first 3 bits is reserved.
|
|
|
|
0x20, // PMT PID.
|
|
|
|
// CRC32.
|
2016-04-14 04:25:15 +00:00
|
|
|
0xf9, 0x62, 0xf5, 0x8b,
|
2016-03-21 00:04:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
EXPECT_NO_FATAL_FAILURE(ExpectTsPacketEqual(
|
|
|
|
kExpectedPatPrefix, kExpectedPatPrefixSize, 165, kExpectedPatPayload,
|
2020-07-04 22:18:30 +00:00
|
|
|
arraysize(kExpectedPatPayload), buffer_writer.Buffer()));
|
2016-03-21 00:04:54 +00:00
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_EQ(0, memcmp(kMockPmtWriterData, buffer_writer.Buffer() + kTsPacketSize,
|
2016-04-14 05:40:24 +00:00
|
|
|
kTsPacketSize));
|
|
|
|
}
|
2016-03-21 00:04:54 +00:00
|
|
|
|
2016-04-14 05:40:24 +00:00
|
|
|
TEST_F(TsWriterTest, ClearAacPmt) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<MockProgramMapTableWriter> mock_pmt_writer(
|
2016-04-14 05:40:24 +00:00
|
|
|
new MockProgramMapTableWriter());
|
|
|
|
EXPECT_CALL(*mock_pmt_writer, ClearSegmentPmt(_)).WillOnce(WriteOnePmt());
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
BufferWriter buffer_writer;
|
|
|
|
|
2017-10-23 22:59:30 +00:00
|
|
|
TsWriter ts_writer(std::move(mock_pmt_writer));
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_TRUE(ts_writer.NewSegment(&buffer_writer));
|
2016-04-14 05:40:24 +00:00
|
|
|
|
|
|
|
// 2 TS Packets. PAT, PMT.
|
2020-07-04 22:18:30 +00:00
|
|
|
ASSERT_EQ(376u, buffer_writer.Size());
|
2016-04-14 05:40:24 +00:00
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_EQ(0, memcmp(kMockPmtWriterData, buffer_writer.Buffer() + kTsPacketSize,
|
2016-04-14 05:40:24 +00:00
|
|
|
kTsPacketSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
// The stream is flaged with will_be_encrypted. Verify that 2 PMTs are created.
|
|
|
|
// One for clear lead and another for encrypted segments that follow.
|
|
|
|
TEST_F(TsWriterTest, ClearLeadH264Pmt) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<MockProgramMapTableWriter> mock_pmt_writer(
|
2016-04-14 05:40:24 +00:00
|
|
|
new MockProgramMapTableWriter());
|
2016-06-29 21:24:40 +00:00
|
|
|
EXPECT_CALL(*mock_pmt_writer, ClearSegmentPmt(_))
|
2016-04-14 05:40:24 +00:00
|
|
|
.WillOnce(WriteTwoPmts());
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
BufferWriter buffer_writer;
|
2017-10-23 22:59:30 +00:00
|
|
|
TsWriter ts_writer(std::move(mock_pmt_writer));
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_TRUE(ts_writer.NewSegment(&buffer_writer));
|
|
|
|
|
|
|
|
ASSERT_EQ(564u, buffer_writer.Size());
|
2016-04-14 05:40:24 +00:00
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_EQ(0, memcmp(kMockPmtWriterData, buffer_writer.Buffer() + kTsPacketSize,
|
2016-04-14 05:40:24 +00:00
|
|
|
kTsPacketSize));
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_EQ(0, memcmp(kMockPmtWriterData, buffer_writer.Buffer() + 2 * kTsPacketSize,
|
2016-04-14 05:40:24 +00:00
|
|
|
kTsPacketSize));
|
|
|
|
}
|
|
|
|
|
2017-10-18 20:25:20 +00:00
|
|
|
TEST_F(TsWriterTest, ClearSegmentPmtFailure) {
|
|
|
|
std::unique_ptr<MockProgramMapTableWriter> mock_pmt_writer(
|
|
|
|
new MockProgramMapTableWriter());
|
|
|
|
EXPECT_CALL(*mock_pmt_writer, ClearSegmentPmt(_)).WillOnce(Return(false));
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
BufferWriter buffer_writer;
|
2017-10-23 22:59:30 +00:00
|
|
|
TsWriter ts_writer(std::move(mock_pmt_writer));
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_FALSE(ts_writer.NewSegment(&buffer_writer));
|
2017-10-18 20:25:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-14 05:40:24 +00:00
|
|
|
// Check the encrypted segments' PMT (after clear lead).
|
|
|
|
TEST_F(TsWriterTest, EncryptedSegmentsH264Pmt) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<MockProgramMapTableWriter> mock_pmt_writer(
|
2016-04-14 05:40:24 +00:00
|
|
|
new MockProgramMapTableWriter());
|
|
|
|
InSequence s;
|
2016-06-29 21:24:40 +00:00
|
|
|
EXPECT_CALL(*mock_pmt_writer, ClearSegmentPmt(_)).WillOnce(Return(true));
|
2016-04-14 05:40:24 +00:00
|
|
|
EXPECT_CALL(*mock_pmt_writer, EncryptedSegmentPmt(_)).WillOnce(WriteOnePmt());
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
BufferWriter buffer_writer;
|
2017-10-23 22:59:30 +00:00
|
|
|
TsWriter ts_writer(std::move(mock_pmt_writer));
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_TRUE(ts_writer.NewSegment(&buffer_writer));
|
2016-04-14 05:40:24 +00:00
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
buffer_writer.Clear();
|
2016-04-14 05:40:24 +00:00
|
|
|
// Overwrite the file but as encrypted segment.
|
2017-10-23 22:59:30 +00:00
|
|
|
ts_writer.SignalEncrypted();
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_TRUE(ts_writer.NewSegment(&buffer_writer));
|
2016-04-14 05:40:24 +00:00
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
ASSERT_EQ(376u, buffer_writer.Size());
|
2016-04-14 05:40:24 +00:00
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_EQ(0, memcmp(kMockPmtWriterData, buffer_writer.Buffer() + kTsPacketSize,
|
2016-04-14 05:40:24 +00:00
|
|
|
kTsPacketSize));
|
|
|
|
}
|
|
|
|
|
2017-10-18 20:25:20 +00:00
|
|
|
TEST_F(TsWriterTest, EncryptedSegmentPmtFailure) {
|
|
|
|
std::unique_ptr<MockProgramMapTableWriter> mock_pmt_writer(
|
|
|
|
new MockProgramMapTableWriter());
|
|
|
|
InSequence s;
|
|
|
|
EXPECT_CALL(*mock_pmt_writer, ClearSegmentPmt(_)).WillOnce(Return(true));
|
|
|
|
EXPECT_CALL(*mock_pmt_writer, EncryptedSegmentPmt(_)).WillOnce(Return(false));
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
BufferWriter buffer_writer;
|
2017-10-23 22:59:30 +00:00
|
|
|
TsWriter ts_writer(std::move(mock_pmt_writer));
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_TRUE(ts_writer.NewSegment(&buffer_writer));
|
2017-10-18 20:25:20 +00:00
|
|
|
|
2017-10-23 22:59:30 +00:00
|
|
|
ts_writer.SignalEncrypted();
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_FALSE(ts_writer.NewSegment(&buffer_writer));
|
2017-10-18 20:25:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-14 05:40:24 +00:00
|
|
|
// Same as ClearLeadH264Pmt but for AAC.
|
|
|
|
TEST_F(TsWriterTest, ClearLeadAacPmt) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<MockProgramMapTableWriter> mock_pmt_writer(
|
2016-04-14 05:40:24 +00:00
|
|
|
new MockProgramMapTableWriter());
|
2016-06-29 21:24:40 +00:00
|
|
|
EXPECT_CALL(*mock_pmt_writer, ClearSegmentPmt(_))
|
2016-04-14 05:40:24 +00:00
|
|
|
.WillOnce(WriteTwoPmts());
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
BufferWriter buffer_writer;
|
2017-10-23 22:59:30 +00:00
|
|
|
TsWriter ts_writer(std::move(mock_pmt_writer));
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_TRUE(ts_writer.NewSegment(&buffer_writer));
|
|
|
|
|
|
|
|
ASSERT_EQ(564u, buffer_writer.Size());
|
2016-04-14 05:40:24 +00:00
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_EQ(0, memcmp(kMockPmtWriterData, buffer_writer.Buffer() + kTsPacketSize,
|
2016-04-14 05:40:24 +00:00
|
|
|
kTsPacketSize));
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_EQ(0, memcmp(kMockPmtWriterData, buffer_writer.Buffer() + 2 * kTsPacketSize,
|
2016-04-14 05:40:24 +00:00
|
|
|
kTsPacketSize));
|
2016-03-21 00:04:54 +00:00
|
|
|
}
|
|
|
|
|
2016-04-14 05:40:24 +00:00
|
|
|
// Same as EncryptedSegmentsH264Pmt but for AAC.
|
|
|
|
TEST_F(TsWriterTest, EncryptedSegmentsAacPmt) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<MockProgramMapTableWriter> mock_pmt_writer(
|
2016-04-14 05:40:24 +00:00
|
|
|
new MockProgramMapTableWriter());
|
|
|
|
InSequence s;
|
2016-06-29 21:24:40 +00:00
|
|
|
EXPECT_CALL(*mock_pmt_writer, ClearSegmentPmt(_)).WillOnce(Return(true));
|
2016-04-14 05:40:24 +00:00
|
|
|
EXPECT_CALL(*mock_pmt_writer, EncryptedSegmentPmt(_)).WillOnce(WriteOnePmt());
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
BufferWriter buffer_writer;
|
2017-10-23 22:59:30 +00:00
|
|
|
TsWriter ts_writer(std::move(mock_pmt_writer));
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_TRUE(ts_writer.NewSegment(&buffer_writer));
|
|
|
|
|
|
|
|
buffer_writer.Clear();
|
2016-04-14 05:40:24 +00:00
|
|
|
// Overwrite the file but as encrypted segment.
|
2017-10-23 22:59:30 +00:00
|
|
|
ts_writer.SignalEncrypted();
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_TRUE(ts_writer.NewSegment(&buffer_writer));
|
|
|
|
|
|
|
|
ASSERT_EQ(376u, buffer_writer.Size());
|
2016-04-14 05:40:24 +00:00
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_EQ(0, memcmp(kMockPmtWriterData, buffer_writer.Buffer() + kTsPacketSize,
|
2016-04-14 05:40:24 +00:00
|
|
|
kTsPacketSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-21 00:04:54 +00:00
|
|
|
TEST_F(TsWriterTest, AddPesPacket) {
|
2017-10-23 22:59:30 +00:00
|
|
|
TsWriter ts_writer(std::unique_ptr<ProgramMapTableWriter>(
|
|
|
|
new VideoProgramMapTableWriter(kCodecForTesting)));
|
2020-07-04 22:18:30 +00:00
|
|
|
BufferWriter buffer_writer;
|
|
|
|
EXPECT_TRUE(ts_writer.NewSegment(&buffer_writer));
|
2016-03-21 00:04:54 +00:00
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<PesPacket> pes(new PesPacket());
|
2016-03-21 00:04:54 +00:00
|
|
|
pes->set_stream_id(0xE0);
|
|
|
|
pes->set_pts(0x900);
|
|
|
|
pes->set_dts(0x900);
|
|
|
|
const uint8_t kAnyData[] = {
|
|
|
|
0x12, 0x88, 0x4f, 0x4a,
|
|
|
|
};
|
|
|
|
pes->mutable_data()->assign(kAnyData, kAnyData + arraysize(kAnyData));
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_TRUE(ts_writer.AddPesPacket(std::move(pes), &buffer_writer));
|
|
|
|
|
2016-03-21 00:04:54 +00:00
|
|
|
// 3 TS Packets. PAT, PMT, and PES.
|
2020-07-04 22:18:30 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(564u, buffer_writer.Size());
|
2016-03-21 00:04:54 +00:00
|
|
|
|
|
|
|
const int kPesStartPosition = 376;
|
|
|
|
|
|
|
|
// Prefix of the expected output. Rest of the packet should be filled with
|
|
|
|
// padding.
|
|
|
|
const uint8_t kExpectedOutputPrefix[] = {
|
|
|
|
0x47, // Sync byte.
|
|
|
|
0x40, // payload_unit_start_indicator set.
|
|
|
|
0x50, // pid.
|
|
|
|
0x30, // Adaptation field and payload are both present. counter = 0.
|
|
|
|
0xA0, // Adaptation Field length.
|
|
|
|
0x10, // pcr flag.
|
|
|
|
0x00, 0x00, 0x04, 0x80, 0x00, 0x00, // PCR.
|
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t kExpectedPayload[] = {
|
|
|
|
0x00, 0x00, 0x01, // Start code.
|
|
|
|
0xE0, // stream id.
|
|
|
|
0x00, 0x11, // PES_packet_length.
|
|
|
|
0x80, // Flags.
|
|
|
|
0xC0, // PTS and DTS both present.
|
|
|
|
0x0A, // PES_header_data_length.
|
|
|
|
0x31, // Since PTS is 0 this is '0011' (fixed) and marker bit at LSB.
|
|
|
|
0x00, // PTS leading bits 0.
|
|
|
|
0x01, // PTS 0 followed by marker bit.
|
|
|
|
0x12, // PTS 0x900 shifted.
|
|
|
|
0x01, // PTS 0 followed by marker bit.
|
|
|
|
0x11, // Fixed '0001' followed by marker bit at LSB.
|
|
|
|
0x00, // DTS leading bits 0.
|
|
|
|
0x01, // DTS 0 followed by marker bit.
|
|
|
|
0x12, // DTS 0x900 shifted.
|
|
|
|
0x01, // DTS 0 followed by marker bit.
|
|
|
|
0x12, 0x88, 0x4f, 0x4a, // Payload.
|
|
|
|
};
|
|
|
|
EXPECT_NO_FATAL_FAILURE(ExpectTsPacketEqual(
|
|
|
|
kExpectedOutputPrefix, arraysize(kExpectedOutputPrefix), 153,
|
|
|
|
kExpectedPayload, arraysize(kExpectedPayload),
|
2020-07-04 22:18:30 +00:00
|
|
|
buffer_writer.Buffer() + kPesStartPosition));
|
2016-03-21 00:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that PES packet > 64KiB can be handled.
|
|
|
|
TEST_F(TsWriterTest, BigPesPacket) {
|
2017-10-23 22:59:30 +00:00
|
|
|
TsWriter ts_writer(std::unique_ptr<ProgramMapTableWriter>(
|
|
|
|
new VideoProgramMapTableWriter(kCodecForTesting)));
|
2020-07-04 22:18:30 +00:00
|
|
|
|
|
|
|
BufferWriter buffer_writer;
|
|
|
|
EXPECT_TRUE(ts_writer.NewSegment(&buffer_writer));
|
2016-03-21 00:04:54 +00:00
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<PesPacket> pes(new PesPacket());
|
2016-03-21 00:04:54 +00:00
|
|
|
pes->set_pts(0);
|
|
|
|
pes->set_dts(0);
|
|
|
|
// A little over 2 TS Packets (3 TS Packets).
|
|
|
|
const std::vector<uint8_t> big_data(400, 0x23);
|
|
|
|
*pes->mutable_data() = big_data;
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_TRUE(ts_writer.AddPesPacket(std::move(pes), &buffer_writer));
|
2016-03-21 00:04:54 +00:00
|
|
|
|
|
|
|
// The first TsPacket can only carry
|
|
|
|
// 177 (TS packet size - header - adaptation_field) - 19 (PES header data) =
|
|
|
|
// 158 bytes of the PES packet payload.
|
|
|
|
// So this should create
|
|
|
|
// 2 + 1 + ceil((400 - 158) / 184) = 5 TsPackets.
|
|
|
|
// Where 184 is the maxium payload of a TS packet.
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_EQ(5u * 188, buffer_writer.Size());
|
2016-03-21 00:04:54 +00:00
|
|
|
|
|
|
|
// Check continuity counter.
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_EQ(0, (buffer_writer.Buffer()[2 * 188 + 3] & 0xF));
|
|
|
|
EXPECT_EQ(1, (buffer_writer.Buffer()[3 * 188 + 3] & 0xF));
|
|
|
|
EXPECT_EQ(2, (buffer_writer.Buffer()[4 * 188 + 3] & 0xF));
|
2016-03-21 00:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bug found in code review. It should check whether PTS is present not whether
|
|
|
|
// PTS (implicilty) cast to bool is true.
|
|
|
|
TEST_F(TsWriterTest, PesPtsZeroNoDts) {
|
2017-10-23 22:59:30 +00:00
|
|
|
TsWriter ts_writer(std::unique_ptr<ProgramMapTableWriter>(
|
|
|
|
new VideoProgramMapTableWriter(kCodecForTesting)));
|
2020-07-04 22:18:30 +00:00
|
|
|
|
|
|
|
BufferWriter buffer_writer;
|
|
|
|
EXPECT_TRUE(ts_writer.NewSegment(&buffer_writer));
|
2016-03-21 00:04:54 +00:00
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<PesPacket> pes(new PesPacket());
|
2016-03-21 00:04:54 +00:00
|
|
|
pes->set_stream_id(0xE0);
|
|
|
|
pes->set_pts(0x0);
|
|
|
|
const uint8_t kAnyData[] = {
|
|
|
|
0x12, 0x88, 0x4F, 0x4A,
|
|
|
|
};
|
|
|
|
pes->mutable_data()->assign(kAnyData, kAnyData + arraysize(kAnyData));
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_TRUE(ts_writer.AddPesPacket(std::move(pes), &buffer_writer));
|
2016-03-21 00:04:54 +00:00
|
|
|
|
|
|
|
// 3 TS Packets. PAT, PMT, and PES.
|
2020-07-04 22:18:30 +00:00
|
|
|
ASSERT_EQ(564u, buffer_writer.Size());
|
2016-03-21 00:04:54 +00:00
|
|
|
|
|
|
|
const int kPesStartPosition = 376;
|
|
|
|
|
|
|
|
// Prefix of the expected output. Rest of the packet should be filled with
|
|
|
|
// padding.
|
|
|
|
const uint8_t kExpectedOutputPrefix[] = {
|
|
|
|
0x47, // Sync byte.
|
|
|
|
0x40, // payload_unit_start_indicator set.
|
|
|
|
0x50, // pid.
|
|
|
|
0x30, // Adaptation field and payload are both present. counter = 0.
|
|
|
|
0xA5, // Adaptation Field length.
|
|
|
|
0x10, // pcr flag.
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // PCR.
|
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t kExpectedPayload[] = {
|
|
|
|
0x00, 0x00, 0x01, // Start code.
|
|
|
|
0xE0, // stream id.
|
|
|
|
0x00, 0x0C, // PES_packet_length.
|
|
|
|
0x80, // Flags.
|
|
|
|
0x80, // Only PTS present.
|
|
|
|
0x05, // PES_header_data_length.
|
|
|
|
0x21, // Since PTS is 0 this is '0010' (fixed) and marker bit at LSB.
|
|
|
|
0x00, // PTS 0.
|
|
|
|
0x01, // PTS 0 followed by marker bit.
|
|
|
|
0x00, // PTS 0.
|
|
|
|
0x01, // PTS 0 followed by marker bit.
|
|
|
|
0x12, 0x88, 0x4F, 0x4A, // Payload.
|
|
|
|
};
|
|
|
|
EXPECT_NO_FATAL_FAILURE(ExpectTsPacketEqual(
|
|
|
|
kExpectedOutputPrefix, arraysize(kExpectedOutputPrefix), 158,
|
|
|
|
kExpectedPayload, arraysize(kExpectedPayload),
|
2020-07-04 22:18:30 +00:00
|
|
|
buffer_writer.Buffer() + kPesStartPosition));
|
2016-03-21 00:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that TS packet with payload 183 is handled correctly, e.g.
|
|
|
|
// adaptation_field_length should be 0.
|
|
|
|
TEST_F(TsWriterTest, TsPacketPayload183Bytes) {
|
2017-10-23 22:59:30 +00:00
|
|
|
TsWriter ts_writer(std::unique_ptr<ProgramMapTableWriter>(
|
|
|
|
new VideoProgramMapTableWriter(kCodecForTesting)));
|
2020-07-04 22:18:30 +00:00
|
|
|
BufferWriter buffer_writer;
|
|
|
|
EXPECT_TRUE(ts_writer.NewSegment(&buffer_writer));
|
2016-03-21 00:04:54 +00:00
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<PesPacket> pes(new PesPacket());
|
2016-03-21 00:04:54 +00:00
|
|
|
pes->set_stream_id(0xE0);
|
|
|
|
pes->set_pts(0x00);
|
|
|
|
pes->set_dts(0x00);
|
|
|
|
|
|
|
|
// Note that first TS packet will have adaptation fields with PCR, so make
|
|
|
|
// payload big enough so that second PES packet's payload is 183.
|
|
|
|
// First TS packet can carry 157 bytes of PES payload. The next one should
|
|
|
|
// carry 183 bytes.
|
|
|
|
std::vector<uint8_t> pes_payload(157 + 183, 0xAF);
|
|
|
|
*pes->mutable_data() = pes_payload;
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
EXPECT_TRUE(ts_writer.AddPesPacket(std::move(pes), &buffer_writer));
|
2016-03-21 00:04:54 +00:00
|
|
|
|
|
|
|
const uint8_t kExpectedOutputPrefix[] = {
|
|
|
|
0x47, // Sync byte.
|
|
|
|
0x00, // payload_unit_start_indicator set.
|
|
|
|
0x50, // pid.
|
|
|
|
0x31, // Adaptation field and payload are both present. counter = 0.
|
|
|
|
0x00, // Adaptation Field length, 1 byte padding.
|
|
|
|
};
|
|
|
|
|
|
|
|
// 4 TsPackets. PAT, PMT, TsPacket with PES header, TsPacket rest of PES
|
|
|
|
// payload.
|
2020-07-04 22:18:30 +00:00
|
|
|
ASSERT_EQ(752u, buffer_writer.Size());
|
2016-03-21 00:04:54 +00:00
|
|
|
|
|
|
|
const int kPesStartPosition = 564;
|
2020-07-04 22:18:30 +00:00
|
|
|
std::vector<uint8_t> actual_prefix(buffer_writer.Buffer() + kPesStartPosition,
|
|
|
|
buffer_writer.Buffer() + kPesStartPosition + 5);
|
2016-03-21 00:04:54 +00:00
|
|
|
EXPECT_EQ(
|
|
|
|
std::vector<uint8_t>(kExpectedOutputPrefix, kExpectedOutputPrefix + 5),
|
|
|
|
actual_prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp2t
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|