Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions endpoint/stream/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_subdirectory(stream)

option(PRIVMX_BUILD_WITH_WEBRTC "Build Stream module with additional layer of the native WebRTC library" OFF)
option(USE_PREBUILT_WEBRTC "Use libwebrtc provided by package manager" ON)
message(STATUS "Build Stream module with additional layer of the native WebRTC library - PRIVMX_BUILD_WITH_WEBRTC=${PRIVMX_BUILD_WITH_WEBRTC}")

set(LIBS "")
Expand Down
15 changes: 10 additions & 5 deletions endpoint/stream/webrtc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
set(INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(INCLUDE_PUB_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include_pub)

add_library(libwebrtc SHARED IMPORTED)
set_target_properties(libwebrtc PROPERTIES
IMPORTED_LOCATION "<path_to_libwebrtc>"
INTERFACE_INCLUDE_DIRECTORIES "<path_to_libwebrtc>"
)
if(USE_PREBUILT_WEBRTC)
find_package(libwebrtc REQUIRED CONFIG)
add_library(libwebrtc ALIAS libwebrtc::libwebrtc)
else()
add_library(libwebrtc SHARED IMPORTED)
set_target_properties(libwebrtc PROPERTIES
IMPORTED_LOCATION "<path_to_libwebrtc>"
INTERFACE_INCLUDE_DIRECTORIES "<path_to_libwebrtc>"
)
endif()

add_library(privmxendpointstream_webrtc OBJECT ${SOURCES})
target_include_directories(privmxendpointstream_webrtc PUBLIC ${INCLUDE_DIRS} PUBLIC ${INCLUDE_PUB_DIRS})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ enum ConnectionType {
struct AudioTrackInfo {
libwebrtc::scoped_refptr<libwebrtc::RTCAudioTrack> track;
libwebrtc::scoped_refptr<libwebrtc::RTCRtpSender> sender;
std::shared_ptr<privmx::webrtc::AudioLevelAnalyzer> audioLevelAnalyzer;
std::shared_ptr<privmx::webrtc::FrameCryptor> frameCryptor;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class PmxPeerConnectionObserver : public libwebrtc::RTCPeerConnectionObserver {
void OnIceGatheringState(libwebrtc::RTCIceGatheringState state) override;
void OnIceConnectionState(libwebrtc::RTCIceConnectionState state) override;
void OnIceCandidate(libwebrtc::scoped_refptr<libwebrtc::RTCIceCandidate> candidate) override;
void OnAddStream(libwebrtc::scoped_refptr<libwebrtc::RTCMediaStream> stream) override;
void OnAddStream([[maybe_unused]] libwebrtc::scoped_refptr<libwebrtc::RTCMediaStream> stream) override;
void OnRemoveStream(libwebrtc::scoped_refptr<libwebrtc::RTCMediaStream> stream) override;
void OnDataChannel(libwebrtc::scoped_refptr<libwebrtc::RTCDataChannel> data_channel) override;
void OnRenegotiationNeeded() override;
Expand All @@ -70,6 +70,7 @@ class PmxPeerConnectionObserver : public libwebrtc::RTCPeerConnectionObserver {
privmx::utils::ThreadSaveMap<std::string, std::shared_ptr<RTCVideoRendererImpl>> _RTCVideoRenderers;
privmx::utils::ThreadSaveMap<std::string, std::shared_ptr<AudioTrackSinkImpl>> _audioTrackSinks;
privmx::utils::ThreadSaveMap<std::string, std::shared_ptr<DataChannelImpl>> _dataChannels;
privmx::utils::ThreadSaveMap<std::string, std::shared_ptr<privmx::webrtc::AudioLevelAnalyzer>> _audioLevelAnalyzers;
privmx::utils::ThreadSaveMap<std::string, std::shared_ptr<privmx::webrtc::FrameCryptor>> _frameCryptors;

std::optional<std::function<void(libwebrtc::scoped_refptr<libwebrtc::RTCIceCandidate>)>> _onIceCandidate;
Expand Down
18 changes: 16 additions & 2 deletions endpoint/stream/webrtc/src/PmxPeerConnectionObserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void PmxPeerConnectionObserver::OnIceCandidate([[maybe_unused]] libwebrtc::scope
LOG_DEBUG("STREAMS ", "API ", _streamRoomId + ": ON ICE CANDIDATE")
if(_onIceCandidate.has_value()) _onIceCandidate.value()(candidate);
}
void PmxPeerConnectionObserver::OnAddStream(libwebrtc::scoped_refptr<libwebrtc::RTCMediaStream> stream) {
void PmxPeerConnectionObserver::OnAddStream([[maybe_unused]] libwebrtc::scoped_refptr<libwebrtc::RTCMediaStream> stream) {
LOG_DEBUG("STREAMS ", "API ", _streamRoomId + ": STREAM ADDED")
LOG_DEBUG("STREAMS ", "API ", "stream->video_tracks().size() -> " + std::to_string(stream->video_tracks().size()))
LOG_DEBUG("STREAMS ", "API ", "stream->audio_tracks().size() -> " + std::to_string(stream->audio_tracks().size()))
Expand Down Expand Up @@ -107,9 +107,21 @@ void PmxPeerConnectionObserver::OnTrack([[maybe_unused]] libwebrtc::scoped_refpt
void PmxPeerConnectionObserver::OnAddTrack([[maybe_unused]] libwebrtc::vector<libwebrtc::scoped_refptr<libwebrtc::RTCMediaStream>> streams, [[maybe_unused]] libwebrtc::scoped_refptr<libwebrtc::RTCRtpReceiver> receiver) {
LOG_DEBUG("STREAMS ", "API ", _streamRoomId + ": ON ADD TRACK")
// set frame crypto to decrypt track
auto audioLevelAnalyzer = receiver->track()->kind().std_string() == "audio"
? privmx::webrtc::FrameCryptorFactory::audioLevelAnalyzer()
: nullptr;
if (audioLevelAnalyzer) {
_audioLevelAnalyzers.set(receiver->track()->id().std_string(), audioLevelAnalyzer);
}
_frameCryptors.set(
receiver->track()->id().std_string(),
privmx::webrtc::FrameCryptorFactory::frameCryptorFromRtpReceiver(_peerConnectionFactory ,receiver, _currentKeys, _options)
privmx::webrtc::FrameCryptorFactory::frameCryptorFromRtpReceiver(
_peerConnectionFactory,
receiver,
_currentKeys,
audioLevelAnalyzer,
_options
)
);

DataType dataType = receiver->track()->kind().std_string() == "video" ? DataType::VIDEO : DataType::AUDIO;
Expand Down Expand Up @@ -171,6 +183,8 @@ void PmxPeerConnectionObserver::OnRemoveTrack([[maybe_unused]] libwebrtc::scoped
tmp.value()->OnRemoteTrack(Track{dataType, streamIds, track->id().std_string(), !track->enabled(), [track](bool mute) {return track->set_enabled(!mute);}}, TrackAction::ADDED);
}
}
if (dataType == DataType::AUDIO) _audioLevelAnalyzers.erase(receiver->track()->id().std_string());
_frameCryptors.erase(receiver->track()->id().std_string());
if(dataType == DataType::AUDIO) _audioTrackSinks.erase(receiver->track()->id().std_string());
if(dataType == DataType::VIDEO) _RTCVideoRenderers.erase(receiver->track()->id().std_string());
}
Expand Down
4 changes: 4 additions & 0 deletions endpoint/stream/webrtc/src/WebRTCImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,15 @@ void WebRTCImpl::updatePeerConnectionWithLocalStream(
void WebRTCImpl::AddAudioTrack(std::shared_ptr<privmx::endpoint::stream::JanusConnection> jc, libwebrtc::scoped_refptr<libwebrtc::RTCAudioTrack> audioTrack, std::string id) {
jc->peerConnection->mediaStream->AddTrack(audioTrack);
auto sender = jc->peerConnection->pc->AddTrack(audioTrack, libwebrtc::vector<libwebrtc::string>{std::vector<libwebrtc::string>{jc->peerConnection->mediaStream->id()}});
auto audioLevelAnalyzer = privmx::webrtc::FrameCryptorFactory::audioLevelAnalyzer();
std::shared_ptr<privmx::webrtc::FrameCryptor> frameCryptor;
{
std::shared_lock<std::shared_mutex> lock(jc->peerConnection->trackMutex);
frameCryptor = privmx::webrtc::FrameCryptorFactory::frameCryptorFromRtpSender(
_peerConnectionFactory,
sender,
jc->peerConnection->keys,
audioLevelAnalyzer,
_frameCryptorOptions
);
}
Expand All @@ -297,6 +299,7 @@ void WebRTCImpl::AddAudioTrack(std::shared_ptr<privmx::endpoint::stream::JanusCo
AudioTrackInfo{
.track=audioTrack,
.sender=sender,
.audioLevelAnalyzer=audioLevelAnalyzer,
.frameCryptor=frameCryptor
}
));
Expand All @@ -313,6 +316,7 @@ void WebRTCImpl::AddVideoTrack(std::shared_ptr<privmx::endpoint::stream::JanusCo
_peerConnectionFactory,
sender,
jc->peerConnection->keys,
nullptr,
_frameCryptorOptions
);
}
Expand Down
Loading