Skip to content

Commit

Permalink
Merge pull request #41 from Lowchicken/add-whip-into-joyrtc
Browse files Browse the repository at this point in the history
Implement WHIP (Unity) and WHEP (Web)
  • Loading branch information
a-wing authored Dec 25, 2023
2 parents 68ae993 + 389ca6d commit 4cc1da4
Show file tree
Hide file tree
Showing 16 changed files with 2,334 additions and 2,009 deletions.
2 changes: 2 additions & 0 deletions .github/CLA.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ Example:
- tulingcheng, @tulingcheng86, 2023/10/17
- yuzhitao, @Qixiu33, 2023/11/18
- Opacity, @zreren, 2023/11/2
- Rocket Aaron, @rocka, 2023/11/30
- guangyongChen,@Lowchicken,2023/12/25
100 changes: 46 additions & 54 deletions unity/Assets/joyrtc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
Expand All @@ -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()
Expand Down
Loading

0 comments on commit 4cc1da4

Please sign in to comment.