- Modified code to enable the broadcaster to act as a TURN server using Hyperswarm connections.
- Replaced external STUN/TURN servers with custom signaling over Hyperswarm.
- Configured RTCPeerConnection with an empty ICE servers array to prevent reliance on external servers.
- Implemented custom signaling protocol for exchanging offers, answers, and ICE candidates over Hyperswarm connections.
- Updated broadcaster to relay media streams, effectively mimicking TURN server behavior within the Hyperswarm network.
- Adjusted peer connection setup for both broadcaster and listener to use Hyperswarm for signaling and data transport.
- Removed dependency on third-party servers; all communication now occurs within the peer-to-peer network.
- Updated UI elements and controls to reflect the changes.
- Ensured that peer count updates correctly with new connection handling.
**Overview:**
This commit represents a comprehensive refactoring of the application to improve real-time audio streaming capabilities. The key change is the integration of WebRTC for peer-to-peer audio streaming while using Hyperswarm exclusively for signaling. This transition addresses efficiency, reliability, and scalability issues present in the original implementation.
**Old Method:**
- **Audio Streaming via Hyperswarm Data Channels:**
- The original code used Hyperswarm for both signaling and streaming audio data.
- Audio data was captured from the microphone, converted to binary, and transmitted over Hyperswarm connections.
- Listeners received the audio data chunks and processed them to play back the audio.
- **Issues:**
- Inefficient for real-time audio streaming due to Hyperswarm's limitations for media data.
- Higher latency and potential synchronization problems.
- Difficulty managing peer connections and media streams effectively.
**New Method:**
- **Integration of WebRTC for Audio Streaming:**
- Implemented `RTCPeerConnection` instances for efficient, real-time, peer-to-peer audio streaming.
- Used Hyperswarm solely for signaling to establish and manage connections.
- Audio tracks are now transmitted over WebRTC connections, leveraging optimized protocols for media.
- **Benefits:**
- Improved audio quality and reduced latency.
- Enhanced NAT traversal and firewall compatibility via ICE servers.
- Better management of media streams and peer connections.
**Key Changes:**
1. **WebRTC Implementation:**
- **Broadcaster Side:**
- Created `RTCPeerConnection` instances for each listener.
- Added local audio tracks from the microphone to the peer connections.
- Managed signaling messages (`offer`, `answer`, `candidate`) received via Hyperswarm.
- Handled ICE candidate exchange and connection state changes.
- **Listener Side:**
- Created an `RTCPeerConnection` to connect to the broadcaster.
- Added a transceiver with `recvonly` direction to receive audio streams.
- Managed signaling messages and ICE candidates.
- Played received audio streams using HTML `<audio>` elements.
2. **Signaling via Hyperswarm:**
- Utilized Hyperswarm connections for exchanging signaling messages in JSON format.
- Messages include `offer`, `answer`, and `candidate` types.
- Ensured proper serialization and deserialization of signaling data.
3. **ICE Candidate Handling:**
- Implemented ICE candidate queuing to handle candidates arriving before the remote description is set.
- Stored incoming ICE candidates in queues and processed them after setting the remote description.
- Added detailed logging for ICE candidate exchange and connection states.
4. **Peer Count Accuracy:**
- Updated the `updatePeerCount()` function to use `conns.length`, reflecting active Hyperswarm connections.
- Ensured the peer count updates immediately when connections are established or closed.
- Improved UI feedback regarding the number of connected peers.
5. **Audio Input Switching Without Disconnecting Peers:**
- Modified the `applyAudioSource()` function to replace audio tracks in existing peer connections without restarting the station.
- Obtained a new audio stream with the selected input device.
- Used `RTCRtpSender.replaceTrack()` to update the audio track in each peer connection.
- Stopped old audio tracks to free up resources.
- Allowed broadcasters to switch microphones seamlessly without interrupting listeners' audio.
6. **Error Handling and Debugging Improvements:**
- Added extensive logging throughout the code to trace execution flow and internal state.
- Wrapped asynchronous operations in `try...catch` blocks to handle errors gracefully.
- Provided informative console messages for successful operations and errors.
7. **User Interface Adjustments:**
- Retained existing UI elements and controls.
- Updated event listeners to align with the new logic.
- Provided real-time updates to station information and peer count.
**Benefits of the New Method:**
- **Enhanced Audio Quality and Performance:**
- Leveraging WebRTC provides better audio streaming capabilities optimized for real-time communication.
- Reduced latency and improved synchronization.
- **Scalability and Reliability:**
- Proper handling of peer connections and media streams improves the application's scalability.
- Robust error handling ensures better reliability under various network conditions.
- **Improved User Experience:**
- Listeners experience uninterrupted audio even when broadcasters change input devices.
- Accurate peer count provides broadcasters with immediate feedback on their audience size.
**Testing and Verification:**
- Tested the application with multiple broadcasters and listeners to ensure proper functionality.
- Verified that audio streams initiate correctly and continue even after input device changes.
- Confirmed that peer counts update accurately on both broadcaster and listener sides.
- Ensured that no errors appear in the console logs during normal operation.
revert feat(audio): migrate from ScriptProcessorNode to AudioWorkletNode for low-latency broadcasting
- Implemented `BroadcasterProcessor` for audio processing in a separate audio thread.
- Replaced deprecated `ScriptProcessorNode` with `AudioWorkletNode` in `startBroadcast`.
- Enhanced audio performance by reducing main thread interference and improving scalability.
- Added `broadcaster-processor.js` to handle custom audio processing logic.
This change ensures compatibility with modern browsers and improves broadcast audio quality.
- Implemented `BroadcasterProcessor` for audio processing in a separate audio thread.
- Replaced deprecated `ScriptProcessorNode` with `AudioWorkletNode` in `startBroadcast`.
- Enhanced audio performance by reducing main thread interference and improving scalability.
- Added `broadcaster-processor.js` to handle custom audio processing logic.
This change ensures compatibility with modern browsers and improves broadcast audio quality.