From 2d2825290c367c9b92454457e8a10cb9cc30781e Mon Sep 17 00:00:00 2001 From: Aaron Vaage Date: Fri, 9 Jun 2017 08:30:16 -0700 Subject: [PATCH] Added TextSample Added a text sample type that can be passed between media handlers. This will allow text samples to safely be moved between media handlers before converting to their final format. Bug: 36138902 Change-Id: Ic4946f774a7d37c43066b9ea46596d5c5f3c05a8 --- packager/media/base/media_base.gyp | 2 ++ packager/media/base/media_handler.h | 14 +++++++++ packager/media/base/text_sample.cc | 32 ++++++++++++++++++++ packager/media/base/text_sample.h | 47 +++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 packager/media/base/text_sample.cc create mode 100644 packager/media/base/text_sample.h diff --git a/packager/media/base/media_base.gyp b/packager/media/base/media_base.gyp index 283bcef768..729c1712a1 100644 --- a/packager/media/base/media_base.gyp +++ b/packager/media/base/media_base.gyp @@ -84,6 +84,8 @@ 'status.h', 'stream_info.cc', 'stream_info.h', + 'text_sample.cc', + 'text_sample.h', 'text_stream_info.cc', 'text_stream_info.h', 'text_track.h', diff --git a/packager/media/base/media_handler.h b/packager/media/base/media_handler.h index f8f21f66f8..b7d1024786 100644 --- a/packager/media/base/media_handler.h +++ b/packager/media/base/media_handler.h @@ -14,6 +14,7 @@ #include "packager/media/base/media_sample.h" #include "packager/media/base/status.h" #include "packager/media/base/stream_info.h" +#include "packager/media/base/text_sample.h" namespace shaka { namespace media { @@ -23,6 +24,7 @@ enum class StreamDataType { kPeriodInfo, kStreamInfo, kMediaSample, + kTextSample, kMediaEvent, kSegmentInfo, }; @@ -49,6 +51,7 @@ struct StreamData { std::shared_ptr period_info; std::shared_ptr stream_info; std::shared_ptr media_sample; + std::shared_ptr text_sample; std::shared_ptr media_event; std::shared_ptr segment_info; }; @@ -140,6 +143,17 @@ class MediaHandler { return Dispatch(std::move(stream_data)); } + /// Dispatch the text sample to downsream handlers. + // DispatchTextSample should only be override for testing. + Status DispatchTextSample(size_t stream_index, + std::shared_ptr text_sample) { + std::unique_ptr stream_data(new StreamData); + stream_data->stream_index = stream_index; + stream_data->stream_data_type = StreamDataType::kTextSample; + stream_data->text_sample = std::move(text_sample); + return Dispatch(std::move(stream_data)); + } + /// Dispatch the media event to downstream handlers. Status DispatchMediaEvent(size_t stream_index, std::shared_ptr media_event) { diff --git a/packager/media/base/text_sample.cc b/packager/media/base/text_sample.cc new file mode 100644 index 0000000000..9effc25357 --- /dev/null +++ b/packager/media/base/text_sample.cc @@ -0,0 +1,32 @@ +// Copyright 2017 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 "packager/media/base/text_sample.h" + +#include "packager/base/logging.h" + +namespace shaka { +namespace media { + +uint64_t TextSample::EndTime() const { + return start_time_ + duration_; +} + +void TextSample::SetTime(uint64_t start_time, uint64_t end_time) { + DCHECK_LT(start_time, end_time); + start_time_ = start_time; + duration_ = end_time - start_time; +} + +void TextSample::AppendPayload(const std::string& payload) { + if (payload_.length()) { + payload_ += "\n"; + } + payload_ += payload; +} + +} // namespace media +} // namespace shaka diff --git a/packager/media/base/text_sample.h b/packager/media/base/text_sample.h new file mode 100644 index 0000000000..330851e9d4 --- /dev/null +++ b/packager/media/base/text_sample.h @@ -0,0 +1,47 @@ +// Copyright 2017 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 + +#ifndef PACKAGER_MEDIA_BASE_TEXT_SAMPLE_H_ +#define PACKAGER_MEDIA_BASE_TEXT_SAMPLE_H_ + +#include + +#include + +namespace shaka { +namespace media { + +class TextSample { + public: + TextSample() = default; + + const std::string& id() const { return id_; } + uint64_t start_time() const { return start_time_; } + uint64_t duration() const { return duration_; } + const std::string& settings() const { return settings_; } + const std::string& payload() const { return payload_; } + uint64_t EndTime() const; + + void set_id(const std::string& id) { id_ = id; } + void set_settings(const std::string& settings) { settings_ = settings; } + void SetTime(uint64_t start_time, uint64_t end_time); + void AppendPayload(const std::string& payload); + + private: + TextSample(const TextSample&) = delete; + TextSample& operator=(const TextSample&) = delete; + + std::string id_; + uint64_t start_time_ = 0; + uint64_t duration_ = 0; + std::string settings_; + std::string payload_; +}; + +} // namespace media +} // namespace shaka + +#endif // MEDIA_BASE_TEXT_SAMPLE_H_