diff options
author | Jordan Bayles <jophba@chromium.org> | 2020-11-24 17:10:33 -0800 |
---|---|---|
committer | Jordan Bayles <jophba@chromium.org> | 2020-11-25 03:40:20 +0000 |
commit | fc4b62317d2522436f9a0d5f9b6f83144fa29529 (patch) | |
tree | 8ac6c8a6012685bcb6981235d0b46c84353f7fea /cast | |
parent | 842767b9e44e1b66ef668a88db21caa5f4f4ceba (diff) | |
download | openscreen-fc4b62317d2522436f9a0d5f9b6f83144fa29529.tar.gz |
Add new FlatMap utility class
This patch adds a new FlatMap class in our utility component. This class
is intended as a drop-in replacement for
std::vector<std::pair<key,value>>, which ends up getting used as a
lower-cost std::map<key, value>, with some additional convenience
functions to make it behave more like a map.
This patch also replaces all std::vector<std::pair<>> declarations in
favor of FlatMap.
Change-Id: I1cbdc70998c9cc4754d20799ca0b4a6d30cb397a
Reviewed-on: https://chromium-review.googlesource.com/c/openscreen/+/2552695
Commit-Queue: Jordan Bayles <jophba@chromium.org>
Reviewed-by: Ryan Keane <rwkeane@google.com>
Diffstat (limited to 'cast')
-rw-r--r-- | cast/streaming/receiver.h | 4 | ||||
-rw-r--r-- | cast/streaming/receiver_packet_router.cc | 42 | ||||
-rw-r--r-- | cast/streaming/receiver_packet_router.h | 9 | ||||
-rw-r--r-- | cast/streaming/session_messager.cc | 38 | ||||
-rw-r--r-- | cast/streaming/session_messager.h | 4 |
5 files changed, 38 insertions, 59 deletions
diff --git a/cast/streaming/receiver.h b/cast/streaming/receiver.h index abdc1852..4a927dd9 100644 --- a/cast/streaming/receiver.h +++ b/cast/streaming/receiver.h @@ -29,6 +29,7 @@ #include "cast/streaming/ssrc.h" #include "platform/api/time.h" #include "util/alarm.h" +#include "util/flat_map.h" namespace openscreen { namespace cast { @@ -321,8 +322,7 @@ class Receiver { // The target playout delay is the amount of time between a frame's // capture/recording on the Sender and when it should be played-out at the // Receiver. - std::vector<std::pair<FrameId, std::chrono::milliseconds>> - playout_delay_changes_; + FlatMap<FrameId, std::chrono::milliseconds> playout_delay_changes_; // The consumer to notify when there are one or more frames completed and // ready to be consumed. diff --git a/cast/streaming/receiver_packet_router.cc b/cast/streaming/receiver_packet_router.cc index 0ac79d3f..1ac4266a 100644 --- a/cast/streaming/receiver_packet_router.cc +++ b/cast/streaming/receiver_packet_router.cc @@ -25,7 +25,7 @@ ReceiverPacketRouter::~ReceiverPacketRouter() { void ReceiverPacketRouter::OnReceiverCreated(Ssrc sender_ssrc, Receiver* receiver) { - OSP_DCHECK(FindEntry(sender_ssrc) == receivers_.end()); + OSP_DCHECK(receivers_.find(sender_ssrc) == receivers_.end()); receivers_.emplace_back(sender_ssrc, receiver); // If there were no Receiver instances before, resume receiving packets for @@ -38,10 +38,7 @@ void ReceiverPacketRouter::OnReceiverCreated(Ssrc sender_ssrc, } void ReceiverPacketRouter::OnReceiverDestroyed(Ssrc sender_ssrc) { - const auto it = FindEntry(sender_ssrc); - OSP_DCHECK(it != receivers_.end()); - receivers_.erase(it); - + receivers_.erase_key(sender_ssrc); // If there are no longer any Receivers, suspend receiving packets. if (receivers_.empty()) { environment_->DropIncomingPackets(); @@ -82,29 +79,22 @@ void ReceiverPacketRouter::OnReceivedPacket(const IPEndpoint& source, 0, kMaxPartiaHexDumpSize)); return; } - const auto it = FindEntry(seems_like.second); - if (it != receivers_.end()) { - // At this point, a valid packet has been matched with a receiver. Lock-in - // the remote endpoint as the |source| of this |packet| so that only packets - // from the same source are permitted from here onwards. - if (environment_->remote_endpoint().port == 0) { - environment_->set_remote_endpoint(source); - } - - if (seems_like.first == ApparentPacketType::RTP) { - it->second->OnReceivedRtpPacket(arrival_time, std::move(packet)); - } else if (seems_like.first == ApparentPacketType::RTCP) { - it->second->OnReceivedRtcpPacket(arrival_time, std::move(packet)); - } + auto it = receivers_.find(seems_like.second); + if (it == receivers_.end()) { + return; + } + // At this point, a valid packet has been matched with a receiver. Lock-in + // the remote endpoint as the |source| of this |packet| so that only packets + // from the same source are permitted from here onwards. + if (environment_->remote_endpoint().port == 0) { + environment_->set_remote_endpoint(source); } -} -ReceiverPacketRouter::ReceiverEntries::iterator ReceiverPacketRouter::FindEntry( - Ssrc sender_ssrc) { - return std::find_if(receivers_.begin(), receivers_.end(), - [sender_ssrc](const ReceiverEntries::value_type& entry) { - return entry.first == sender_ssrc; - }); + if (seems_like.first == ApparentPacketType::RTP) { + it->second->OnReceivedRtpPacket(arrival_time, std::move(packet)); + } else if (seems_like.first == ApparentPacketType::RTCP) { + it->second->OnReceivedRtcpPacket(arrival_time, std::move(packet)); + } } } // namespace cast diff --git a/cast/streaming/receiver_packet_router.h b/cast/streaming/receiver_packet_router.h index d90e5335..fe33d7d4 100644 --- a/cast/streaming/receiver_packet_router.h +++ b/cast/streaming/receiver_packet_router.h @@ -13,6 +13,7 @@ #include "absl/types/span.h" #include "cast/streaming/environment.h" #include "cast/streaming/ssrc.h" +#include "util/flat_map.h" namespace openscreen { namespace cast { @@ -44,20 +45,14 @@ class ReceiverPacketRouter final : public Environment::PacketConsumer { void SendRtcpPacket(absl::Span<const uint8_t> packet); private: - using ReceiverEntries = std::vector<std::pair<Ssrc, Receiver*>>; - // Environment::PacketConsumer implementation. void OnReceivedPacket(const IPEndpoint& source, Clock::time_point arrival_time, std::vector<uint8_t> packet) final; - // Helper to return an iterator pointing to the entry corresponding to the - // given |sender_ssrc|, or "end" if not found. - ReceiverEntries::iterator FindEntry(Ssrc sender_ssrc); - Environment* const environment_; - ReceiverEntries receivers_; + FlatMap<Ssrc, Receiver*> receivers_; }; } // namespace cast diff --git a/cast/streaming/session_messager.cc b/cast/streaming/session_messager.cc index af4dec13..e46f3b24 100644 --- a/cast/streaming/session_messager.cc +++ b/cast/streaming/session_messager.cc @@ -28,13 +28,8 @@ SessionMessager::~SessionMessager() { void SessionMessager::SetHandler(std::string message_type, SessionMessager::MessageCallback cb) { - OSP_DCHECK(std::none_of( - callbacks_.begin(), callbacks_.end(), - [message_type](std::pair<std::string, MessageCallback> pair) { - return pair.first == message_type; - })); - - callbacks_.emplace_back(message_type, std::move(cb)); + OSP_DCHECK(callbacks_.find(message_type) == callbacks_.end()); + callbacks_.emplace_back(std::move(message_type), std::move(cb)); } Error SessionMessager::SendMessage(SessionMessager::Message message) { @@ -96,22 +91,21 @@ void SessionMessager::OnMessage(const std::string& source_id, result.clear(); } - for (const auto& pair : callbacks_) { - if (pair.first == type) { - // Currently all body keys are the lowercase version of the message type - // key. This may need to be refactored if this is no longer the case. - absl::AsciiStrToLower(&type); - Json::Value body; - if (result.empty() || result == kResultOk) { - body = message_json.value()[type]; - } else { - body = message_json.value()[kErrorMessageBody]; - } - pair.second(Message{source_id.data(), message_namespace.data(), - sequence_number, std::move(body)}); - return; - } + auto it = callbacks_.find(type); + if (it == callbacks_.end()) { + return; + } + // Currently all body keys are the lowercase version of the message type + // key. This may need to be refactored if this is no longer the case. + absl::AsciiStrToLower(&type); + Json::Value body; + if (result.empty() || result == kResultOk) { + body = message_json.value()[type]; + } else { + body = message_json.value()[kErrorMessageBody]; } + it->second(Message{source_id.data(), message_namespace.data(), + sequence_number, std::move(body)}); } void SessionMessager::OnError(Error error) { diff --git a/cast/streaming/session_messager.h b/cast/streaming/session_messager.h index eec8c01e..ea39a4eb 100644 --- a/cast/streaming/session_messager.h +++ b/cast/streaming/session_messager.h @@ -12,6 +12,7 @@ #include "cast/common/public/message_port.h" #include "json/value.h" +#include "util/flat_map.h" namespace openscreen { namespace cast { @@ -64,8 +65,7 @@ class SessionMessager : public MessagePort::Client { private: // Since the number of message callbacks is expected to be low, // we use a vector of key, value pairs instead of a map. - std::vector<std::pair<std::string, MessageCallback>> callbacks_; - + FlatMap<std::string, MessageCallback> callbacks_; MessagePort* const message_port_; ErrorCallback error_callback_; }; |