Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: port media/formats/* to cmake #1258

Merged
merged 14 commits into from
Aug 31, 2023
4 changes: 4 additions & 0 deletions packager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ if(MSVC)
add_compile_options(/wd4251)
# Silence a warning about constant conditional expressions.
add_compile_options(/wd4127)
# We use long-jumps in subtitle_composer.cc due to API of libpng
add_compile_options(/wd4611)
# need /bigobj for box definitions
add_compile_options(/bigobj)

# Packager's macro for Windows-specific code.
add_definitions(-DOS_WIN)
Expand Down
9 changes: 8 additions & 1 deletion packager/file/file_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ std::string TempFileName() {
} // namespace

bool TempFilePath(const std::string& temp_dir, std::string* temp_file_path) {
auto temp_dir_path = std::filesystem::u8path(temp_dir);
std::filesystem::path temp_dir_path;

if (temp_dir.empty()) {
temp_dir_path = std::filesystem::temp_directory_path();
} else {
temp_dir_path = std::filesystem::u8path(temp_dir);
}

*temp_file_path = (temp_dir_path / TempFileName()).string();
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion packager/file/local_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ bool LocalFile::Open() {
// first if it needs to be created.
auto parent_path = file_path.parent_path();
std::error_code ec;
if (!std::filesystem::is_directory(parent_path, ec)) {
if (parent_path != "" && !std::filesystem::is_directory(parent_path, ec)) {
if (!std::filesystem::create_directories(parent_path, ec)) {
return false;
}
Expand Down
8 changes: 5 additions & 3 deletions packager/kv_pairs/kv_pairs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@

namespace shaka {

std::vector<KVPair> SplitStringIntoKeyValuePairs(std::string_view str) {
std::vector<KVPair> SplitStringIntoKeyValuePairs(std::string_view str,
char kv_separator,
char list_separator) {
std::vector<KVPair> kv_pairs;

// Edge case: 0 pairs.
if (str.size() == 0) {
return kv_pairs;
}

std::vector<std::string> kv_strings = absl::StrSplit(str, '&');
std::vector<std::string> kv_strings = absl::StrSplit(str, list_separator);
for (const auto& kv_string : kv_strings) {
KVPair pair = absl::StrSplit(kv_string, absl::MaxSplits('=', 1));
KVPair pair = absl::StrSplit(kv_string, absl::MaxSplits(kv_separator, 1));
kv_pairs.push_back(pair);
}

Expand Down
4 changes: 3 additions & 1 deletion packager/kv_pairs/kv_pairs.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace shaka {

typedef std::pair<std::string, std::string> KVPair;
std::vector<KVPair> SplitStringIntoKeyValuePairs(std::string_view str);
std::vector<KVPair> SplitStringIntoKeyValuePairs(std::string_view str,
char kv_separator = '=',
char list_separator = '&');

} // namespace shaka
3 changes: 3 additions & 0 deletions packager/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@
/// AES block size in bytes, regardless of key size.
#define AES_BLOCK_SIZE 16

#define DVLOG_IF(verboselevel, condition) \
static_cast<void>(0), !(condition) ? (void)0 : VLOG(verboselevel)

#endif // PACKAGER_MACROS_H_
2 changes: 2 additions & 0 deletions packager/media/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ target_link_libraries(media_base
glog
hex_parser
mbedtls
mpd_media_info_proto
utils_clock
status
widevine_protos
LibXml2)
Expand Down
2 changes: 1 addition & 1 deletion packager/media/base/aes_cryptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AesCryptor {
virtual bool InitializeWithIv(const std::vector<uint8_t>& key,
const std::vector<uint8_t>& iv) = 0;

virtual size_t RequiredCiphertextSize(size_t plaintext_size) {
virtual size_t RequiredOutputSize(size_t plaintext_size) {
return plaintext_size;
}

Expand Down
5 changes: 5 additions & 0 deletions packager/media/base/aes_decryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ bool AesCbcDecryptor::InitializeWithIv(const std::vector<uint8_t>& key,
return SetIv(iv);
}

size_t AesCbcDecryptor::RequiredOutputSize(size_t plaintext_size) {
// mbedtls requires a buffer large enough for one extra block.
return plaintext_size + AES_BLOCK_SIZE;
}

bool AesCbcDecryptor::CryptInternal(const uint8_t* ciphertext,
size_t ciphertext_size,
uint8_t* plaintext,
Expand Down
2 changes: 2 additions & 0 deletions packager/media/base/aes_decryptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class AesCbcDecryptor : public AesCryptor {
/// @{
bool InitializeWithIv(const std::vector<uint8_t>& key,
const std::vector<uint8_t>& iv) override;

size_t RequiredOutputSize(size_t plaintext_size) override;
/// @}

private:
Expand Down
5 changes: 2 additions & 3 deletions packager/media/base/aes_encryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ bool AesCbcEncryptor::InitializeWithIv(const std::vector<uint8_t>& key,
return SetIv(iv);
}

size_t AesCbcEncryptor::RequiredCiphertextSize(size_t plaintext_size) {
size_t AesCbcEncryptor::RequiredOutputSize(size_t plaintext_size) {
// mbedtls requires a buffer large enough for one extra block.
return plaintext_size + NumPaddingBytes(plaintext_size) + AES_BLOCK_SIZE;
}
Expand All @@ -136,8 +136,7 @@ bool AesCbcEncryptor::CryptInternal(const uint8_t* plaintext,
size_t* ciphertext_size) {
const size_t residual_block_size = plaintext_size % AES_BLOCK_SIZE;
const size_t num_padding_bytes = NumPaddingBytes(plaintext_size);
const size_t required_ciphertext_size =
RequiredCiphertextSize(plaintext_size);
const size_t required_ciphertext_size = RequiredOutputSize(plaintext_size);

if (*ciphertext_size < required_ciphertext_size) {
LOG(ERROR) << "Expecting output size of at least "
Expand Down
2 changes: 1 addition & 1 deletion packager/media/base/aes_encryptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class AesCbcEncryptor : public AesCryptor {
bool InitializeWithIv(const std::vector<uint8_t>& key,
const std::vector<uint8_t>& iv) override;

size_t RequiredCiphertextSize(size_t plaintext_size) override;
size_t RequiredOutputSize(size_t plaintext_size) override;

private:
bool CryptInternal(const uint8_t* plaintext,
Expand Down
6 changes: 1 addition & 5 deletions packager/media/base/muxer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ const bool kInitialEncryptionInfo = true;
const int64_t kStartTime = 0;
} // namespace

uint64_t DefaultMuxerClock() {
return std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
}

Muxer::Muxer(const MuxerOptions& options)
: options_(options), clock_(DefaultMuxerClock) {
: options_(options), clock_(new Clock) {
// "$" is only allowed if the output file name is a template, which is used to
// support one file per Representation per Period when there are Ad Cues.
if (options_.output_file_name.find("$") != std::string::npos)
Expand Down
17 changes: 10 additions & 7 deletions packager/media/base/muxer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@
#include "packager/media/base/muxer_options.h"
#include "packager/media/event/muxer_listener.h"
#include "packager/media/event/progress_listener.h"
#include "packager/mpd/base/mpd_builder.h"
#include "packager/status/status.h"

namespace shaka {
namespace media {

class MediaSample;

/// Returns seconds since January 1, 1970, UTC.
typedef uint64_t (*MuxerClock)();

/// Muxer is responsible for taking elementary stream samples and producing
/// media containers. An optional KeySource can be provided to Muxer
/// to generate encrypted outputs.
Expand Down Expand Up @@ -56,7 +54,7 @@ class Muxer : public MediaHandler {
/// If no clock is injected, the code uses std::chrone::system_clock::now()
/// to generate the time-stamps.
/// @param clock is the Clock to be injected.
void set_clock(MuxerClock clock) { clock_ = clock; }
void set_clock(Clock* clock) { clock_.reset(clock); }

protected:
/// @name MediaHandler implementation overrides.
Expand All @@ -69,7 +67,13 @@ class Muxer : public MediaHandler {
const MuxerOptions& options() const { return options_; }
MuxerListener* muxer_listener() { return muxer_listener_.get(); }
ProgressListener* progress_listener() { return progress_listener_.get(); }
uint64_t Now() const { return clock_(); }

uint64_t Now() const {
auto duration = clock_->now().time_since_epoch();
auto seconds =
std::chrono::duration_cast<std::chrono::seconds>(duration).count();
return static_cast<uint64_t>(seconds);
}

private:
Muxer(const Muxer&) = delete;
Expand Down Expand Up @@ -108,8 +112,7 @@ class Muxer : public MediaHandler {

std::unique_ptr<MuxerListener> muxer_listener_;
std::unique_ptr<ProgressListener> progress_listener_;
// An external injected clock, can be NULL.
MuxerClock clock_ = nullptr;
std::unique_ptr<Clock> clock_;

// In VOD single segment case with Ad Cues, |output_file_name| is allowed to
// be a template. In this case, there will be NumAdCues + 1 files generated.
Expand Down
2 changes: 1 addition & 1 deletion packager/media/crypto/encryption_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ Status EncryptionHandler::ProcessMediaSample(
}

size_t ciphertext_size =
encryptor_->RequiredCiphertextSize(clear_sample->data_size());
encryptor_->RequiredOutputSize(clear_sample->data_size());

std::shared_ptr<uint8_t> cipher_sample_data(new uint8_t[ciphertext_size],
std::default_delete<uint8_t[]>());
Expand Down
2 changes: 1 addition & 1 deletion packager/media/crypto/sample_aes_ec3_cryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "packager/media/crypto/sample_aes_ec3_cryptor.h"

#include <glog/logging.h>
#include <algorithm>

#include "glog/logging.h"
#include "packager/media/base/buffer_reader.h"

namespace shaka {
Expand Down
7 changes: 7 additions & 0 deletions packager/media/formats/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@
# https://developers.google.com/open-source/licenses/bsd

# Subdirectories with their own CMakeLists.txt, all of whose targets are built.
add_subdirectory(dvb)
add_subdirectory(packed_audio)
add_subdirectory(ttml)
add_subdirectory(mp4)
add_subdirectory(mp2t)
add_subdirectory(webm)
add_subdirectory(webvtt)
add_subdirectory(wvm)
53 changes: 53 additions & 0 deletions packager/media/formats/dvb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2020 Google LLC. 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


# Copyright 2016 Google LLC. 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


add_library(dvb STATIC
dvb_image.cc
dvb_image.h
dvb_sub_parser.cc
dvb_sub_parser.h
subtitle_composer.cc
subtitle_composer.h
)
target_link_libraries(dvb
file
media_base
widevine_protos
manifest_base
mpd_media_info_proto
png_static
absl::flags
absl::strings
absl::str_format
glog
)

add_executable(dvb_unittest
dvb_image_unittest.cc
dvb_sub_parser_unittest.cc
subtitle_composer_unittest.cc
)

target_link_libraries(dvb_unittest
file
file_test_util
test_data_util
absl::flags
media_event
dvb
gmock
gtest
gtest_main)

add_test(NAME dvb_unittest COMMAND dvb_unittest)
45 changes: 0 additions & 45 deletions packager/media/formats/dvb/dvb.gyp

This file was deleted.

6 changes: 3 additions & 3 deletions packager/media/formats/dvb/dvb_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <cstring>
#include <tuple>

#include "packager/base/logging.h"
#include <glog/logging.h>

namespace shaka {
namespace media {
Expand Down Expand Up @@ -134,7 +134,7 @@ RgbaColor DvbImageColorSpace::GetColor(BitDepth bit_depth,
break;
default:
// Windows can't detect that all enums are handled and doesn't like
// NOTREACHED.
// NOTIMPLEMENTED.
return kNoColor;
}

Expand Down Expand Up @@ -186,7 +186,7 @@ RgbaColor DvbImageColorSpace::GetColorRaw(BitDepth bit_depth,
case BitDepth::k8Bit:
return color_map_8_[entry_id];
}
// Not reached, but Windows doesn't like NOTREACHED.
// Not reached, but Windows doesn't like NOTIMPLEMENTED.
return kNoColor;
}

Expand Down
2 changes: 1 addition & 1 deletion packager/media/formats/dvb/dvb_sub_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <algorithm>

#include "packager/base/logging.h"
#include <glog/logging.h>
#include "packager/media/formats/mp2t/mp2t_common.h"

namespace shaka {
Expand Down
2 changes: 1 addition & 1 deletion packager/media/formats/dvb/subtitle_composer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <png.h>
#include <string.h>

#include "packager/base/logging.h"
#include <glog/logging.h>

namespace shaka {
namespace media {
Expand Down
2 changes: 1 addition & 1 deletion packager/media/formats/dvb/subtitle_composer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <unordered_map>
#include <vector>

#include "packager/base/macros.h"
#include "packager/macros.h"
#include "packager/media/base/text_sample.h"
#include "packager/media/formats/dvb/dvb_image.h"

Expand Down
Loading
Loading