Skip to content

Commit

Permalink
feat: reduce iram usage in mdnsbrowser
Browse files Browse the repository at this point in the history
  • Loading branch information
feelfreelinux committed Jan 30, 2025
1 parent c8d1437 commit 90bac47
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 26 deletions.
43 changes: 43 additions & 0 deletions include/bell/dsp/MixerTransform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// #pragma once

// // Standard includes
// #include <utility>
// #include <vector>

// // Bell includes
// #include "TransformPipeline.h"

// namespace bell::dsp {
// /**
// * @brief MixerTransform allows for either downmixing or upmixing of audio channels.
// */
// class MixerTransform : public Transform {
// public:
// MixerTransform() = default;

// /**
// * @brief Configure the mixer with the given map of input channel to output channels
// *
// * For example, to downmix stereo (0, 1) to mono (0), use [ {0, 0}, {1, 0} ]
// * To upmix mono (0) to stereo (0, 1), use [ {0, 0}, {0, 1} ]
// *
// * @param mixerMap Vector of pairs of input channel to output channel
// */
// void configure(const std::vector<std::pair<int, int>>& mixerMapping);

// // Transform implementation, see Transform.h for details
// void process(DataSlots& sampleSlots) override;
// float calculateHeadroom() override;

// private:
// std::vector<std::pair<int, int>> mixerMapping;

// // Calculates the input and output size
// int sourceChannels = 0;
// int targetChannels = 0;
// };
// } // namespace bell::dsp

// namespace bell {
// using MixerTransform = dsp::MixerTransform;
// }
9 changes: 8 additions & 1 deletion include/bell/mdns/Browser.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,19 @@ class Browser {
std::vector<net::IpAddress> addresses;

DiscoveredRecord() = default;

bool operator==(const DiscoveredRecord& other) const {
return name == other.name && regType == other.regType &&
domain == other.domain && interfaceIndex == other.interfaceIndex;
}

size_t getHash() const {
return std::hash<std::string>{}(name) ^
std::hash<std::string>{}(regType) ^
std::hash<std::string>{}(domain) ^
std::hash<uint32_t>{}(interfaceIndex);
}

inline void parseTXTRecords(const unsigned char* txtRecord,
uint16_t txtLen) {
txtRecords.clear();
Expand Down
60 changes: 35 additions & 25 deletions main/platform/esp/MDNSBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,39 @@ class implMDNSBrowser : public mdns::Browser {

const int maxResults = 32;

std::vector<DiscoveredRecord> cachedRecords;
std::vector<DiscoveredRecord> receivedRecords;
std::vector<DiscoveredRecord> recordsCache;

void parseResults(mdns_result_t* results) {
mdns_result_t* r = results;
mdns_ip_addr_t* a = nullptr;
std::vector<uint32_t> discoveredRecordHashes;
DiscoveredRecord record; // currently discovered record

while (r) {
DiscoveredRecord record;

// Assign netif index
record.interfaceIndex = esp_netif_get_netif_impl_index(r->esp_netif);

if (r->instance_name) {
record.name = r->instance_name;
record.serviceResolved = true;
}

record.serviceResolved = r->instance_name != nullptr;

if (r->hostname) {
record.hostname = r->hostname;
record.port = r->port;
record.addressResolved = true;
}

record.addressResolved = r->hostname != nullptr;
record.regType = r->service_type;

discoveredRecordHashes.push_back(record.getHash());

if (std::find(recordsCache.begin(), recordsCache.end(), record) !=
recordsCache.end()) {
// Already processed this record
r = r->next;
continue;
}

a = r->addr;
Expand Down Expand Up @@ -100,30 +111,29 @@ class implMDNSBrowser : public mdns::Browser {
}

// Notify of new services
if (std::find(cachedRecords.begin(), cachedRecords.end(), record) ==
cachedRecords.end()) {
onEvent(mdns::EventType::ServiceAdded, record);
if (record.serviceResolved) {
onEvent(mdns::EventType::ServiceResolved, record);
}
if (record.addressResolved) {
onEvent(mdns::EventType::AddressResolved, record);
}
onEvent(mdns::EventType::ServiceAdded, record);
if (record.serviceResolved) {
onEvent(mdns::EventType::ServiceResolved, record);
}
if (record.addressResolved) {
onEvent(mdns::EventType::AddressResolved, record);
}

receivedRecords.push_back(record);
recordsCache.push_back(record);
r = r->next;
}

// Notify of removed services
for (auto& cachedRecord : cachedRecords) {
if (std::find(receivedRecords.begin(), receivedRecords.end(),
cachedRecord) == receivedRecords.end()) {
onEvent(mdns::EventType::ServiceRemoved, cachedRecord);
}
}

cachedRecords.assign(receivedRecords.begin(), receivedRecords.end());
// Notify of removed services, and remove
std::erase_if(
recordsCache, [this, &discoveredRecordHashes](const auto& record) {
if (std::find(discoveredRecordHashes.begin(),
discoveredRecordHashes.end(),
record.getHash()) == discoveredRecordHashes.end()) {
onEvent(mdns::EventType::ServiceRemoved, record);
return true;
}
return false;
});
}

void processEvents(int timeoutMs) override {
Expand Down

0 comments on commit 90bac47

Please sign in to comment.