feat: port media/chunking to CMake (#1223)

Port media/chunking to CMake

Related to #1047
This commit is contained in:
Carlos Bentzen 2023-07-06 00:18:36 +02:00 committed by GitHub
parent bf6296c463
commit ee0922edf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 67 additions and 82 deletions

View File

@ -7,6 +7,7 @@
# Subdirectories with their own CMakeLists.txt, all of whose targets are built. # Subdirectories with their own CMakeLists.txt, all of whose targets are built.
add_subdirectory(base) add_subdirectory(base)
add_subdirectory(codecs) add_subdirectory(codecs)
add_subdirectory(chunking)
add_subdirectory(formats) add_subdirectory(formats)
add_subdirectory(origin) add_subdirectory(origin)
add_subdirectory(replicator) add_subdirectory(replicator)

View File

@ -0,0 +1,32 @@
# Copyright 2022 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(chunking STATIC
chunking_handler.cc
cue_alignment_handler.cc
sync_point_queue.cc
text_chunker.cc
)
target_link_libraries(chunking
media_base
)
add_executable(chunking_unittest
chunking_handler_unittest.cc
cue_alignment_handler_unittest.cc
text_chunker_unittest.cc
)
target_link_libraries(chunking_unittest
gmock
gtest
gtest_main
media_handler_test_base
chunking
)
add_test(NAME chunking_unittest COMMAND chunking_unittest)

View File

@ -1,47 +0,0 @@
# Copyright 2017 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
{
'variables': {
'shaka_code': 1,
},
'targets': [
{
'target_name': 'chunking',
'type': '<(component)',
'sources': [
'chunking_handler.cc',
'chunking_handler.h',
'cue_alignment_handler.cc',
'cue_alignment_handler.h',
'sync_point_queue.cc',
'sync_point_queue.h',
'text_chunker.cc',
'text_chunker.h',
],
'dependencies': [
'../base/media_base.gyp:media_base',
],
},
{
'target_name': 'chunking_unittest',
'type': '<(gtest_target_type)',
'sources': [
'chunking_handler_unittest.cc',
'cue_alignment_handler_unittest.cc',
'text_chunker_unittest.cc',
],
'dependencies': [
'../../testing/gtest.gyp:gtest',
'../../testing/gmock.gyp:gmock',
'../base/media_base.gyp:media_handler_test_base',
'../test/media_test.gyp:media_test_support',
'chunking',
]
},
],
}

View File

@ -8,9 +8,9 @@
#include <algorithm> #include <algorithm>
#include "packager/base/logging.h" #include "glog/logging.h"
#include "packager/media/base/media_sample.h" #include "packager/media/base/media_sample.h"
#include "packager/status_macros.h" #include "packager/status/status_macros.h"
namespace shaka { namespace shaka {
namespace media { namespace media {
@ -59,7 +59,7 @@ Status ChunkingHandler::Process(std::unique_ptr<StreamData> stream_data) {
} }
} }
Status ChunkingHandler::OnFlushRequest(size_t input_stream_index) { Status ChunkingHandler::OnFlushRequest(size_t /*input_stream_index*/) {
RETURN_IF_ERROR(EndSegmentIfStarted()); RETURN_IF_ERROR(EndSegmentIfStarted());
return FlushDownstream(kStreamIndex); return FlushDownstream(kStreamIndex);
} }
@ -79,7 +79,7 @@ Status ChunkingHandler::OnCueEvent(std::shared_ptr<const CueEvent> event) {
RETURN_IF_ERROR(DispatchCueEvent(kStreamIndex, std::move(event))); RETURN_IF_ERROR(DispatchCueEvent(kStreamIndex, std::move(event)));
// Force start new segment after cue event. // Force start new segment after cue event.
segment_start_time_ = base::nullopt; segment_start_time_ = std::nullopt;
// |cue_offset_| will be applied to sample timestamp so the segment after cue // |cue_offset_| will be applied to sample timestamp so the segment after cue
// point have duration ~= |segment_duration_|. // point have duration ~= |segment_duration_|.
cue_offset_ = event_time_in_seconds * time_scale_; cue_offset_ = event_time_in_seconds * time_scale_;

View File

@ -8,10 +8,10 @@
#define PACKAGER_MEDIA_CHUNKING_CHUNKING_HANDLER_ #define PACKAGER_MEDIA_CHUNKING_CHUNKING_HANDLER_
#include <atomic> #include <atomic>
#include <optional>
#include <queue> #include <queue>
#include "packager/base/logging.h" #include "glog/logging.h"
#include "packager/base/optional.h"
#include "packager/media/base/media_handler.h" #include "packager/media/base/media_handler.h"
#include "packager/media/public/chunking_params.h" #include "packager/media/public/chunking_params.h"
@ -78,8 +78,8 @@ class ChunkingHandler : public MediaHandler {
// Current subsegment index, useful to determine where to do chunking. // Current subsegment index, useful to determine where to do chunking.
int64_t current_subsegment_index_ = -1; int64_t current_subsegment_index_ = -1;
base::Optional<int64_t> segment_start_time_; std::optional<int64_t> segment_start_time_;
base::Optional<int64_t> subsegment_start_time_; std::optional<int64_t> subsegment_start_time_;
int64_t max_segment_time_ = 0; int64_t max_segment_time_ = 0;
int32_t time_scale_ = 0; int32_t time_scale_ = 0;

View File

@ -10,7 +10,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "packager/media/base/media_handler_test_base.h" #include "packager/media/base/media_handler_test_base.h"
#include "packager/status_test_util.h" #include "packager/status/status_test_util.h"
using ::testing::_; using ::testing::_;
using ::testing::ElementsAre; using ::testing::ElementsAre;

View File

@ -8,7 +8,7 @@
#include <algorithm> #include <algorithm>
#include "packager/status_macros.h" #include "packager/status/status_macros.h"
namespace shaka { namespace shaka {
namespace media { namespace media {
@ -29,7 +29,7 @@ int64_t GetScaledTime(const StreamInfo& info, const StreamData& data) {
// This class does not support splitting MediaSample at cue points, which is // This class does not support splitting MediaSample at cue points, which is
// required for text stream. This class expects MediaSample to be converted // required for text stream. This class expects MediaSample to be converted
// to TextSample before passing to this class. // to TextSample before passing to this class.
NOTREACHED() NOTIMPLEMENTED()
<< "A text streams should use text samples, not media samples."; << "A text streams should use text samples, not media samples.";
} }

View File

@ -4,16 +4,16 @@
// license that can be found in the LICENSE file or at // license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd // https://developers.google.com/open-source/licenses/bsd
#include "packager/media/chunking/chunking_handler.h" #include "packager/media/chunking/cue_alignment_handler.h"
#include <gmock/gmock.h> #include <gmock/gmock.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "packager/media/base/media_handler_test_base.h" #include "packager/media/base/media_handler_test_base.h"
#include "packager/media/chunking/cue_alignment_handler.h" #include "packager/media/chunking/chunking_handler.h"
#include "packager/media/public/ad_cue_generator_params.h" #include "packager/media/public/ad_cue_generator_params.h"
#include "packager/status_macros.h" #include "packager/status/status_macros.h"
#include "packager/status_test_util.h" #include "packager/status/status_test_util.h"
using ::testing::_; using ::testing::_;
using ::testing::ElementsAre; using ::testing::ElementsAre;

View File

@ -13,8 +13,7 @@
namespace shaka { namespace shaka {
namespace media { namespace media {
SyncPointQueue::SyncPointQueue(const AdCueGeneratorParams& params) SyncPointQueue::SyncPointQueue(const AdCueGeneratorParams& params) {
: sync_condition_(&lock_) {
for (const Cuepoint& point : params.cue_points) { for (const Cuepoint& point : params.cue_points) {
std::shared_ptr<CueEvent> event = std::make_shared<CueEvent>(); std::shared_ptr<CueEvent> event = std::make_shared<CueEvent>();
event->time_in_seconds = point.start_time_in_seconds; event->time_in_seconds = point.start_time_in_seconds;
@ -23,20 +22,20 @@ SyncPointQueue::SyncPointQueue(const AdCueGeneratorParams& params)
} }
void SyncPointQueue::AddThread() { void SyncPointQueue::AddThread() {
base::AutoLock auto_lock(lock_); absl::MutexLock lock(&mutex_);
thread_count_++; thread_count_++;
} }
void SyncPointQueue::Cancel() { void SyncPointQueue::Cancel() {
{ {
base::AutoLock auto_lock(lock_); absl::MutexLock lock(&mutex_);
cancelled_ = true; cancelled_ = true;
} }
sync_condition_.Broadcast(); sync_condition_.SignalAll();
} }
double SyncPointQueue::GetHint(double time_in_seconds) { double SyncPointQueue::GetHint(double time_in_seconds) {
base::AutoLock auto_lock(lock_); absl::MutexLock lock(&mutex_);
auto iter = promoted_.upper_bound(time_in_seconds); auto iter = promoted_.upper_bound(time_in_seconds);
if (iter != promoted_.end()) if (iter != promoted_.end())
@ -53,7 +52,7 @@ double SyncPointQueue::GetHint(double time_in_seconds) {
std::shared_ptr<const CueEvent> SyncPointQueue::GetNext( std::shared_ptr<const CueEvent> SyncPointQueue::GetNext(
double hint_in_seconds) { double hint_in_seconds) {
base::AutoLock auto_lock(lock_); absl::MutexLock lock(&mutex_);
while (!cancelled_) { while (!cancelled_) {
// Find the promoted cue that would line up with our hint, which is the // Find the promoted cue that would line up with our hint, which is the
// first cue that is not less than |hint_in_seconds|. // first cue that is not less than |hint_in_seconds|.
@ -75,7 +74,7 @@ std::shared_ptr<const CueEvent> SyncPointQueue::GetNext(
// and returned - see section above). Spurious signal events are possible // and returned - see section above). Spurious signal events are possible
// with most condition variable implementations, so if it returns, we go // with most condition variable implementations, so if it returns, we go
// back and check if a cue is actually promoted or not. // back and check if a cue is actually promoted or not.
sync_condition_.Wait(); sync_condition_.Wait(&mutex_);
waiting_thread_count_--; waiting_thread_count_--;
} }
return nullptr; return nullptr;
@ -83,7 +82,7 @@ std::shared_ptr<const CueEvent> SyncPointQueue::GetNext(
std::shared_ptr<const CueEvent> SyncPointQueue::PromoteAt( std::shared_ptr<const CueEvent> SyncPointQueue::PromoteAt(
double time_in_seconds) { double time_in_seconds) {
base::AutoLock auto_lock(lock_); absl::MutexLock lock(&mutex_);
return PromoteAtNoLocking(time_in_seconds); return PromoteAtNoLocking(time_in_seconds);
} }
@ -93,7 +92,7 @@ bool SyncPointQueue::HasMore(double hint_in_seconds) const {
std::shared_ptr<const CueEvent> SyncPointQueue::PromoteAtNoLocking( std::shared_ptr<const CueEvent> SyncPointQueue::PromoteAtNoLocking(
double time_in_seconds) { double time_in_seconds) {
lock_.AssertAcquired(); mutex_.AssertHeld();
// It is possible that |time_in_seconds| has been promoted. // It is possible that |time_in_seconds| has been promoted.
auto iter = promoted_.find(time_in_seconds); auto iter = promoted_.find(time_in_seconds);
@ -123,8 +122,8 @@ std::shared_ptr<const CueEvent> SyncPointQueue::PromoteAtNoLocking(
unpromoted_.erase(unpromoted_.begin(), iter); unpromoted_.erase(unpromoted_.begin(), iter);
// Wake up other threads that may be waiting. // Wake up other threads that may be waiting.
sync_condition_.Broadcast(); sync_condition_.SignalAll();
return std::move(cue); return cue;
} }
} // namespace media } // namespace media

View File

@ -7,8 +7,7 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include "packager/base/synchronization/condition_variable.h" #include "absl/synchronization/mutex.h"
#include "packager/base/synchronization/lock.h"
#include "packager/media/public/ad_cue_generator_params.h" #include "packager/media/public/ad_cue_generator_params.h"
namespace shaka { namespace shaka {
@ -60,8 +59,8 @@ class SyncPointQueue {
// functions that have locks. // functions that have locks.
std::shared_ptr<const CueEvent> PromoteAtNoLocking(double time_in_seconds); std::shared_ptr<const CueEvent> PromoteAtNoLocking(double time_in_seconds);
base::Lock lock_; absl::Mutex mutex_;
base::ConditionVariable sync_condition_; absl::CondVar sync_condition_ GUARDED_BY(mutex_);
size_t thread_count_ = 0; size_t thread_count_ = 0;
size_t waiting_thread_count_ = 0; size_t waiting_thread_count_ = 0;
bool cancelled_ = false; bool cancelled_ = false;

View File

@ -6,7 +6,7 @@
#include "packager/media/chunking/text_chunker.h" #include "packager/media/chunking/text_chunker.h"
#include "packager/status_macros.h" #include "packager/status/status_macros.h"
namespace shaka { namespace shaka {
namespace media { namespace media {
@ -31,7 +31,7 @@ Status TextChunker::Process(std::unique_ptr<StreamData> data) {
} }
} }
Status TextChunker::OnFlushRequest(size_t input_stream_index) { Status TextChunker::OnFlushRequest(size_t /*input_stream_index*/) {
// Keep outputting segments until all the samples leave the system. Calling // Keep outputting segments until all the samples leave the system. Calling
// |DispatchSegment| will remove samples over time. // |DispatchSegment| will remove samples over time.
while (samples_in_current_segment_.size()) { while (samples_in_current_segment_.size()) {

View File

@ -4,12 +4,13 @@
// license that can be found in the LICENSE file or at // license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd // https://developers.google.com/open-source/licenses/bsd
#include "packager/media/chunking/text_chunker.h"
#include <gmock/gmock.h> #include <gmock/gmock.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "packager/media/base/media_handler_test_base.h" #include "packager/media/base/media_handler_test_base.h"
#include "packager/media/chunking/text_chunker.h" #include "packager/status/status_test_util.h"
#include "packager/status_test_util.h"
using ::testing::_; using ::testing::_;