-
Notifications
You must be signed in to change notification settings - Fork 7
feat: initial packet-stats implementation #185
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
Draft
vroad
wants to merge
2
commits into
main
Choose a base branch
from
packet-stats-new
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
|
|
||
| // mirakc-arib | ||
| // Copyright (C) 2019 masnagam | ||
| // | ||
| // This program is free software; you can redistribute it and/or modify it under the terms of the | ||
| // GNU General Public License as published by the Free Software Foundation; either version 2 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
| // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | ||
| // the GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License along with this program; if | ||
| // not, write to the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA | ||
| // 02110-1301, USA. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <cstdint> | ||
| #include <optional> | ||
| #include <vector> | ||
|
|
||
| #include <tsduck/tsduck.h> | ||
|
|
||
| #include "base.hh" | ||
| #include "tsduck_helper.hh" | ||
|
|
||
| namespace { | ||
|
|
||
| enum class PacketCategory : uint8_t { | ||
| kVideo, | ||
| kAudio, | ||
| kSubtitle, | ||
| kPmt, | ||
| kNumCategories, | ||
| }; | ||
|
|
||
| constexpr size_t kNumPacketCategories = static_cast<size_t>(PacketCategory::kNumCategories); | ||
|
|
||
| constexpr std::array<const char*, kNumPacketCategories> kPacketCategoryNames = { | ||
| "video", | ||
| "audio", | ||
| "subtitle", | ||
| "pmt", | ||
| }; | ||
|
|
||
| class PacketStatsCollector final { | ||
| public: | ||
| PacketStatsCollector() = default; | ||
|
|
||
| // Changes the PMT PID tracked for packet statistics after a PAT update. | ||
| void SetPmtPid(ts::PID pid) { | ||
| if (pmt_pid_ == pid) { | ||
| return; | ||
| } | ||
|
|
||
| const auto old_pid = pmt_pid_; | ||
| pmt_pid_ = pid; | ||
|
|
||
| if (old_pid.has_value()) { | ||
| RemoveCategory(*old_pid); | ||
| } | ||
|
|
||
| SetCategory(pid, PacketCategory::kPmt); | ||
| } | ||
|
|
||
| // Classifies each PID carried by `pmt` into a PacketCategory. | ||
| void UpdatePidCategories(const ts::PMT& pmt) { | ||
| RebuildPidCategories(pmt); | ||
| } | ||
|
|
||
| void CollectPacketStats(const ts::TSPacket& packet) { | ||
| // Packets with TEI set may have invalid PID or CC values. | ||
| // Count them as errors and return early. | ||
| if (packet.getTEI()) { | ||
| ++error_packets_; | ||
| return; | ||
| } | ||
|
|
||
| auto pid = packet.getPID(); | ||
| if (pid == ts::PID_NULL) { | ||
| return; | ||
| } | ||
|
|
||
| if (packet.getScrambling() != 0) { | ||
| ++scrambled_packets_; | ||
| } | ||
|
|
||
| auto& stat = stats_[pid]; | ||
| if (!stat.category.has_value()) { | ||
| return; | ||
| } | ||
|
|
||
| auto cc = packet.getCC(); | ||
|
|
||
| // Although MPEG-2 TS permits a packet to be transmitted twice, we intentionally do not retain | ||
| // the previous packet to detect duplicates. | ||
| // | ||
| // We are not aware of any reports of duplicate packets in Japanese digital broadcasts, and | ||
| // we see little value in handling them. If a duplicate payload packet is ever transmitted, | ||
| // its repeated CC is counted as 15 dropped packets. | ||
| if (stat.last_cc != ts::INVALID_CC && !packet.getDiscontinuityIndicator()) { | ||
| // The continuity counter increments only when the packet has a payload. | ||
| // Keep the expected CC unchanged for packets without payload. | ||
| uint8_t expected_cc = | ||
| packet.hasPayload() ? ((stat.last_cc + 1) & ts::CC_MASK) : stat.last_cc; | ||
|
|
||
| uint8_t missed = (cc - expected_cc) & ts::CC_MASK; | ||
| if (missed != 0) { | ||
| dropped_packets_[static_cast<size_t>(*stat.category)] += missed; | ||
| } | ||
| } | ||
|
|
||
| stat.last_cc = cc; | ||
| } | ||
|
|
||
| void ResetPacketStats() { | ||
| error_packets_ = 0; | ||
| scrambled_packets_ = 0; | ||
| dropped_packets_.fill(0); | ||
| } | ||
|
|
||
| uint64_t GetErrorPackets() const { | ||
| return error_packets_; | ||
| } | ||
|
|
||
| uint64_t GetScrambledPackets() const { | ||
| return scrambled_packets_; | ||
| } | ||
|
|
||
| uint64_t GetDroppedPackets(PacketCategory category) const { | ||
| MIRAKC_ARIB_ASSERT(static_cast<size_t>(category) < kNumPacketCategories); | ||
| return dropped_packets_[static_cast<size_t>(category)]; | ||
| } | ||
|
|
||
| private: | ||
| struct PacketStat { | ||
| uint8_t last_cc = ts::INVALID_CC; | ||
| std::optional<PacketCategory> category; | ||
| }; | ||
|
|
||
| static void ClearCategory(PacketStat& stat) { | ||
| stat.category.reset(); | ||
| stat.last_cc = ts::INVALID_CC; | ||
| } | ||
|
|
||
| void SetCategory(ts::PID pid, PacketCategory category) { | ||
| auto& stat = stats_[pid]; | ||
| if (!stat.category.has_value()) { | ||
| categorized_pids_.push_back(pid); | ||
| } | ||
| stat.category = category; | ||
| } | ||
|
|
||
| void RemoveCategory(ts::PID pid) { | ||
| ClearCategory(stats_[pid]); | ||
| const auto it = std::find(categorized_pids_.begin(), categorized_pids_.end(), pid); | ||
| if (it != categorized_pids_.end()) { | ||
| categorized_pids_.erase(it); | ||
| } | ||
| } | ||
|
|
||
| // Rebuilds packet-statistics PID categories from the tracked PMT PID and PMT. | ||
| void RebuildPidCategories(const ts::PMT& pmt) { | ||
| std::vector<ts::PID> previous_pids; | ||
| previous_pids.swap(categorized_pids_); | ||
|
|
||
| // Clear the previous category assignments before rebuilding them from the current PMT. | ||
| // Do not clear the last_cc of existing stats here, as it may still be used by the current PMT. | ||
| for (auto pid : previous_pids) { | ||
| stats_[pid].category.reset(); | ||
| } | ||
|
|
||
| if (pmt_pid_.has_value()) { | ||
| SetCategory(*pmt_pid_, PacketCategory::kPmt); | ||
| } | ||
|
|
||
| RebuildStreamPidCategories(pmt); | ||
|
|
||
| // Clear both the category and saved CC for PIDs that are no longer tracked. | ||
| for (auto pid : previous_pids) { | ||
| if (!stats_[pid].category.has_value()) { | ||
| ClearCategory(stats_[pid]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Assigns categories to the PIDs referenced by `pmt`. | ||
| void RebuildStreamPidCategories(const ts::PMT& pmt) { | ||
| for (const auto& [pid, stream] : pmt.streams) { | ||
| // Do not classify the PMT PID as other categories. | ||
| if (pmt_pid_.has_value() && pid == *pmt_pid_) { | ||
| continue; | ||
| } | ||
|
|
||
| if (stream.isVideo()) { | ||
| SetCategory(pid, PacketCategory::kVideo); | ||
| } else if (stream.isAudio()) { | ||
| SetCategory(pid, PacketCategory::kAudio); | ||
| } else if (stream.isSubtitles() || IsAribSubtitle(stream) || | ||
| IsAribSuperimposedText(stream)) { | ||
| SetCategory(pid, PacketCategory::kSubtitle); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| MIRAKC_ARIB_NON_COPYABLE(PacketStatsCollector); | ||
| std::array<PacketStat, ts::PID_MAX> stats_; | ||
| std::vector<ts::PID> categorized_pids_; | ||
| std::optional<ts::PID> pmt_pid_; | ||
| uint64_t error_packets_ = 0; | ||
| uint64_t scrambled_packets_ = 0; | ||
| std::array<uint64_t, kNumPacketCategories> dropped_packets_{}; | ||
| }; | ||
|
|
||
| } // namespace | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@masnagam
自分で見ていて気づきましたが、
isSubtitlesはARIBのMPEG2-TSでは絶対にtrueにはならないかと思うので、削除しようと思います。
tsduck-aribの
isSubtitles()は、ARIBでは使用しないDVBのDescriptorの内容を見ています。mirakc-aribの既存コードにも使用箇所はありますが、同じ理由でtrueにはならないのではと思いました。あとカテゴリ名がsubtitleなのにsuperimposed textも含めてしまっていますが、
名称に偽りのある状態なので
これは対象外としても良いかと思いました。
なら別途superimposed textのカテゴリを作るか?と言う論点も出てくると思いますが
一旦カウントしない方針にしようかと思っています。
めったにsuperimposed textは実TSに入らないとは言え視聴に影響あるとは思いますが、
(めったに実TSに入らないので)確認が難しいためです。
https://github.com/mirakc/tsduck-arib/blob/c400025b7d31e26c0c15471e81adf2ad50632281/src/libtsduck/dtv/tables/tsPMT.cpp#L228-L233