diff --git a/mediapipe/framework/tool/switch_demux_calculator.cc b/mediapipe/framework/tool/switch_demux_calculator.cc index c066d470a..a326621db 100644 --- a/mediapipe/framework/tool/switch_demux_calculator.cc +++ b/mediapipe/framework/tool/switch_demux_calculator.cc @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include #include #include @@ -223,8 +224,8 @@ absl::Status SwitchDemuxCalculator::RecordPackets(CalculatorContext* cc) { // Returns the channel index for a Timestamp. int SwitchDemuxCalculator::ChannelIndex(Timestamp timestamp) { - auto it = std::prev(channel_history_.upper_bound(timestamp)); - return it->second; + auto it = channel_history_.upper_bound(timestamp); + return it == channel_history_.begin() ? -1 : std::prev(it)->second; } // Dispatches all queued input packets with known channels. @@ -237,10 +238,12 @@ absl::Status SwitchDemuxCalculator::SendActivePackets(CalculatorContext* cc) { auto& queue = input_queue_[input_id]; while (!queue.empty() && queue.front().Timestamp() <= channel_settled) { int channel_index = ChannelIndex(queue.front().Timestamp()); - std::string output_tag = tool::ChannelTag(tag, channel_index); - auto output_id = cc->Outputs().GetId(output_tag, index); - if (output_id.IsValid()) { - cc->Outputs().Get(output_id).AddPacket(queue.front()); + if (channel_index != -1) { + std::string output_tag = tool::ChannelTag(tag, channel_index); + auto output_id = cc->Outputs().GetId(output_tag, index); + if (output_id.IsValid()) { + cc->Outputs().Get(output_id).AddPacket(queue.front()); + } } queue.pop(); }