-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from Lowchicken/add-whip-into-joyrtc
Implement WHIP (Unity) and WHEP (Web)
- Loading branch information
Showing
16 changed files
with
2,334 additions
and
2,009 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,61 +38,48 @@ public class joyrtc : MonoBehaviour | |
[SerializeField] private Camera cam; | ||
[SerializeField] private GameObject cube; | ||
[SerializeField] private AudioSource audioSource; | ||
private AudioStreamTrack audioStreamTrack; | ||
|
||
#pragma warning restore 0649 | ||
|
||
private bool enableCameraModeToggle = false; | ||
private bool connected; | ||
private bool whip = false; | ||
private WebSocket ws; | ||
private WhipClient whipClient; | ||
private RTCSessionDescription? sdp; | ||
private RTCPeerConnection _pc; | ||
private MediaStream videoStream; | ||
|
||
private List<RTCRtpSender> pcSenders = new List<RTCRtpSender>(); | ||
private MediaStream audioStream = new MediaStream(); | ||
|
||
private static RTCConfiguration GetSelectedSdpSemantics() | ||
{ | ||
RTCConfiguration config = default; | ||
var servers = new List<RTCIceServer>(); | ||
string envIceServers = System.Environment.GetEnvironmentVariable("ICE_SERVERS"); | ||
Debug.Log("Use WebRTC IceServers: " + envIceServers); | ||
if (!string.IsNullOrEmpty(envIceServers)) | ||
{ | ||
config.iceServers = new[] { new RTCIceServer { urls = new[] { envIceServers } } }; | ||
servers.Add(new RTCIceServer { urls = new[] { envIceServers } }); | ||
} | ||
|
||
string envTurnHostname = System.Environment.GetEnvironmentVariable("TURN_HOSTNAME"); | ||
Debug.Log("Use WebRTC Turn Server: " + envTurnHostname); | ||
if (!string.IsNullOrEmpty(envIceServers)) | ||
if (!string.IsNullOrEmpty(envTurnHostname)) | ||
{ | ||
string envTurnUsername = System.Environment.GetEnvironmentVariable("TURN_USERNAME"); | ||
string envTurnPassword = System.Environment.GetEnvironmentVariable("TURN_PASSWORD"); | ||
config.iceServers = new[] {new RTCIceServer { | ||
servers.Add(new RTCIceServer { | ||
urls = new[] {envTurnHostname}, | ||
username = envTurnUsername, | ||
credential = envTurnPassword, | ||
}}; | ||
} | ||
|
||
return config; | ||
} | ||
|
||
private void AddTracks() | ||
{ | ||
foreach (var track in videoStream.GetTracks()) | ||
{ | ||
Debug.Log("track: " + track); | ||
pcSenders.Add(_pc.AddTrack(track, videoStream)); | ||
}); | ||
} | ||
|
||
// Added the audio track | ||
foreach (var track in audioStream.GetTracks()) | ||
{ | ||
Debug.Log("track: " + track); | ||
pcSenders.Add(_pc.AddTrack(track, audioStream)); | ||
if (servers.Count > 0) { | ||
config.iceServers = servers.ToArray(); | ||
} | ||
|
||
return config; | ||
} | ||
|
||
private IEnumerator OnCreateOffer(RTCPeerConnection pc, RTCSessionDescription desc) | ||
|
@@ -147,11 +134,10 @@ private IEnumerator AsyncWebRTCCoroutine() | |
{ | ||
Debug.Log("=== WebRTC Start ==="); | ||
connected = false; | ||
var configuration = GetSelectedSdpSemantics(); | ||
_pc = new RTCPeerConnection(ref configuration); | ||
|
||
_pc.OnIceCandidate = candidate => { | ||
Debug.Log("ICE: " + candidate.Candidate); | ||
if (whip) return; | ||
|
||
// https://docs.unity3d.com/Packages/[email protected]/api/Unity.WebRTC.RTCIceCandidate.html#Unity_WebRTC_RTCIceCandidate_SdpMLineIndex | ||
//if (candidate.SdpMLineIndex.HasValue) { | ||
|
@@ -219,20 +205,11 @@ private IEnumerator AsyncWebRTCCoroutine() | |
} | ||
}; | ||
|
||
foreach (var track in videoStream.GetTracks()) | ||
if (whip) | ||
{ | ||
Debug.Log("track: " + track); | ||
_pc.AddTrack(track, videoStream); | ||
} | ||
|
||
// Calling the AddTracks() method to add audio tracks to WebRTC | ||
foreach (var track in audioStream.GetTracks()) | ||
{ | ||
Debug.Log("track: " + track); | ||
_pc.AddTrack(track, audioStream); | ||
yield return 0; | ||
} | ||
|
||
|
||
RTCSessionDescription offer; | ||
while (sdp == null) | ||
{ | ||
|
@@ -255,37 +232,45 @@ private IEnumerator AsyncWebRTCCoroutine() | |
void Start() | ||
{ | ||
Debug.Log("=== Start !! ==="); | ||
string whipServerUrl = System.Environment.GetEnvironmentVariable("WHIP_SERVER_URL"); | ||
whip = !string.IsNullOrEmpty(whipServerUrl); | ||
Debug.Log(cam); | ||
if (videoStream == null) | ||
{ | ||
videoStream = cam.CaptureStream(1280, 720); | ||
} | ||
StartCoroutine(WebRTC.Update()); | ||
StartCoroutine(AsyncWebRTCCoroutine()); | ||
|
||
videoStream = cam.CaptureStream(1280, 720); | ||
audioSource = GetComponent<AudioSource>(); | ||
|
||
pcSenders = new List<RTCRtpSender>(); | ||
audioStream = new MediaStream(); | ||
StartCoroutine(WebRTC.Update()); | ||
var configuration = GetSelectedSdpSemantics(); | ||
_pc = new RTCPeerConnection(ref configuration); | ||
|
||
// Check if audioSource is null | ||
if (audioSource != null) | ||
foreach (var track in videoStream.GetTracks()) | ||
{ | ||
Debug.Log("audioSource exists!"); | ||
// Create an AudioStreamTrack object and pass audioSource as a parameter | ||
audioStreamTrack = new AudioStreamTrack(audioSource); | ||
Debug.Log("video track: " + track); | ||
_pc.AddTrack(track, videoStream); | ||
} | ||
foreach (var track in audioStream.GetTracks()) | ||
{ | ||
Debug.Log("audio track: " + track); | ||
_pc.AddTrack(track, audioStream); | ||
} | ||
StartCoroutine(AsyncWebRTCCoroutine()); | ||
|
||
// Add the audio track to the audio stream of the WebRTC connection | ||
_pc.AddTrack(audioStreamTrack, audioStream); | ||
if (whip) | ||
{ | ||
StartWHIP(); | ||
} | ||
else | ||
{ | ||
Debug.Log("audioSource does not exist!"); | ||
StartWebSocket(); | ||
} | ||
|
||
Debug.Log("=== Start END ==="); | ||
} | ||
|
||
void StartWebSocket() { | ||
string envServerUrl = System.Environment.GetEnvironmentVariable("SERVER_URL"); | ||
string serverUrl = string.IsNullOrEmpty(envServerUrl) ? DefaultServer : envServerUrl; | ||
Debug.Log("=== StartWebSocket ==="); | ||
Debug.Log("serverUrl: " + serverUrl); | ||
ws = new WebSocket(serverUrl); | ||
ws.OnMessage += (sender, e) => { | ||
Debug.Log("Received message: " + e.Data); | ||
|
@@ -303,7 +288,14 @@ void Start() | |
}; | ||
|
||
ws.Connect(); | ||
Debug.Log("=== Start END ==="); | ||
} | ||
|
||
void StartWHIP() { | ||
Debug.Log("=== StartWHIP ==="); | ||
string whipServerUrl = System.Environment.GetEnvironmentVariable("WHIP_SERVER_URL"); | ||
Debug.Log("whipServerUrl: " + whipServerUrl); | ||
whipClient = new WhipClient(this); | ||
StartCoroutine(whipClient.Publish(_pc, whipServerUrl, "")); | ||
} | ||
|
||
void Update() | ||
|
Oops, something went wrong.