-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: locker + audio node manager wip
- Loading branch information
Showing
10 changed files
with
186 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
63 changes: 63 additions & 0 deletions
63
packages/react-native-audio-api/common/cpp/core/AudioNodeManager.cpp
This file contains 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,63 @@ | ||
|
||
#include "Locker.hpp" | ||
#include "AudioNode.h" | ||
#include "AudioNodeManager.h" | ||
|
||
namespace audioapi { | ||
|
||
AudioNodeManager::AudioNodeManager() {} | ||
|
||
AudioNodeManager::~AudioNodeManager() { | ||
audioNodesToConnect_.clear(); | ||
audioNodesToDelete_.clear(); | ||
} | ||
|
||
void AudioNodeManager::addPendingConnection(std::shared_ptr<AudioNode> from, std::shared_ptr<AudioNode> to, ConnectionType type) { | ||
Locker lock(getGraphLock()); | ||
|
||
audioNodesToConnect_.push_back(std::make_tuple(from, to, type)); | ||
} | ||
|
||
void AudioNodeManager::setNodeToDelete(std::shared_ptr<AudioNode> node) { | ||
Locker lock(getGraphLock()); | ||
|
||
audioNodesToDelete_.push_back(node); | ||
} | ||
|
||
void AudioNodeManager::preProcessGraph() { | ||
if (!Locker::tryLock(getGraphLock())) { | ||
return; | ||
} | ||
} | ||
|
||
void AudioNodeManager::postProcessGraph() { | ||
if (!Locker::tryLock(getGraphLock())) { | ||
return; | ||
} | ||
} | ||
|
||
std::mutex& AudioNodeManager::getGraphLock() { | ||
return graphLock_; | ||
} | ||
|
||
void AudioNodeManager::settlePendingConnections() { | ||
for (auto& connection : audioNodesToConnect_) { | ||
std::shared_ptr<AudioNode> from = std::get<0>(connection); | ||
std::shared_ptr<AudioNode> to = std::get<1>(connection); | ||
ConnectionType type = std::get<2>(connection); | ||
|
||
if (type == ConnectionType::CONNECT) { | ||
from->connectNode(to); | ||
} else { | ||
from->disconnectNode(to); | ||
} | ||
} | ||
|
||
audioNodesToConnect_.clear(); | ||
} | ||
|
||
void AudioNodeManager::settlePendingDeletions() { | ||
audioNodesToDelete_.clear(); | ||
} | ||
|
||
} // namespace audioapi |
36 changes: 36 additions & 0 deletions
36
packages/react-native-audio-api/common/cpp/core/AudioNodeManager.h
This file contains 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,36 @@ | ||
#pragma once | ||
|
||
#include <mutex> | ||
#include <tuple> | ||
#include <memory> | ||
#include <vector> | ||
|
||
namespace audioapi { | ||
|
||
class AudioNode; | ||
|
||
class AudioNodeManager { | ||
public: | ||
enum class ConnectionType { CONNECT, DISCONNECT }; | ||
AudioNodeManager(); | ||
~AudioNodeManager(); | ||
|
||
void preProcessGraph(); | ||
void postProcessGraph(); | ||
void addPendingConnection(std::shared_ptr<AudioNode> from, std::shared_ptr<AudioNode> to, ConnectionType type); | ||
|
||
void setNodeToDelete(std::shared_ptr<AudioNode> node); | ||
|
||
std::mutex& getGraphLock(); | ||
|
||
private: | ||
std::mutex graphLock_; | ||
|
||
std::vector<std::tuple<std::shared_ptr<AudioNode>, std::shared_ptr<AudioNode>, ConnectionType>> audioNodesToConnect_; | ||
std::vector<std::shared_ptr<AudioNode>> audioNodesToDelete_; | ||
|
||
void settlePendingConnections(); | ||
void settlePendingDeletions(); | ||
}; | ||
|
||
} // namespace audioapi |
This file contains 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 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 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
47 changes: 47 additions & 0 deletions
47
packages/react-native-audio-api/common/cpp/utils/Locker.hpp
This file contains 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,47 @@ | ||
#pragma once | ||
|
||
#include <mutex> | ||
|
||
namespace audioapi { | ||
|
||
// Small easy interface to manage locking | ||
class Locker { | ||
public: | ||
Locker(): lockPtr_(0) {} | ||
Locker(std::mutex& lockPtr): lockPtr_(&lockPtr) { | ||
lock(); | ||
} | ||
|
||
~Locker() { | ||
unlock(); | ||
} | ||
|
||
explicit operator bool() const { return !!lockPtr_; } | ||
|
||
void lock() { | ||
if (lockPtr_) { | ||
lockPtr_->lock(); | ||
} | ||
} | ||
|
||
void unlock() { | ||
if (lockPtr_) { | ||
lockPtr_->unlock(); | ||
} | ||
} | ||
|
||
static Locker tryLock(std::mutex& lock) { | ||
Locker result = Locker(); | ||
|
||
if (lock.try_lock()) { | ||
result.lockPtr_ = &lock; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private: | ||
std::mutex* lockPtr_; | ||
}; | ||
|
||
} // namespace audioapi |