Skip to content

Commit

Permalink
Add an example audio clip.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Sep 11, 2022
1 parent 5ab79c2 commit e513657
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
//
// Copyright (c) 2022 Winlin
//
// SPDX-License-Identifier: MIT
//
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//
// Copyright (c) 2022 Winlin
//
// SPDX-License-Identifier: MIT
//
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.WebRTC;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 576057005}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalPosition: {x: 0, y: 0, z: 4}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
Expand Down Expand Up @@ -392,7 +392,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 860006893}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: -10}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
Expand Down Expand Up @@ -476,7 +476,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1142846194}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: -10}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
Expand All @@ -496,3 +496,130 @@ MonoBehaviour:
m_EditorClassIdentifier:
url: http://localhost:1985/rtc/v1/whip/?app=live&stream=livestream
sourceCamera: {fileID: 860006895}
--- !u!1 &1603479294
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1603479296}
- component: {fileID: 1603479295}
m_Layer: 0
m_Name: AudioSource
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!82 &1603479295
AudioSource:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1603479294}
m_Enabled: 1
serializedVersion: 4
OutputAudioMixerGroup: {fileID: 0}
m_audioClip: {fileID: 8300000, guid: 03eacbc158a904bed8fb26961e2c44e2, type: 3}
m_PlayOnAwake: 1
m_Volume: 1
m_Pitch: 1
Loop: 1
Mute: 0
Spatialize: 0
SpatializePostEffects: 0
Priority: 128
DopplerLevel: 1
MinDistance: 1
MaxDistance: 500
Pan2D: 0
rolloffMode: 0
BypassEffects: 0
BypassListenerEffects: 0
BypassReverbZones: 0
rolloffCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
- serializedVersion: 3
time: 1
value: 0
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
panLevelCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 0
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
spreadCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 0
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
reverbZoneMixCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
--- !u!4 &1603479296
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1603479294}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//
// Copyright (c) 2022 Winlin
//
// SPDX-License-Identifier: MIT
//
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.WebRTC;

Expand All @@ -20,6 +24,11 @@ public class SrsStreamer : MonoBehaviour
private AudioStreamTrack audioStreamTrack;
private RTCPeerConnection pc;

// To make the work flow better to understand, not required. You can
// directly use the `OnAudioFilterRead` function to feed audio stream track.
private delegate void DelegateOnAudioFilterRead(float[] data, int channels);
private DelegateOnAudioFilterRead handleOnAudioFilterRead;

private void Awake()
{
WebRTC.Initialize();
Expand All @@ -31,6 +40,8 @@ private void OnDestroy()
videoStream?.Dispose();
videoStream = null;

handleOnAudioFilterRead = null;

audioStreamTrack?.Dispose();
audioStreamTrack = null;

Expand Down Expand Up @@ -104,6 +115,18 @@ IEnumerator GrabAudio()
pc.AddTrack(audioStreamTrack);
Debug.Log($"WebRTC: Add audio track, id={audioStreamTrack.Id}");

// When got audio data from listener, the gameObject this script
// attached to, generally the MainCamera object, we feed audio data to
// audio stream track.
handleOnAudioFilterRead = (float[] data, int channels) =>
{
if (audioStreamTrack != null)
{
const int sampleRate = 48000;
audioStreamTrack.SetData(data, channels, sampleRate);
}
};

yield return StartCoroutine(PeerNegotiationNeeded());
}

Expand Down Expand Up @@ -181,14 +204,10 @@ IEnumerator OnGotAnswerSuccess(string answer)

private void OnAudioFilterRead(float[] data, int channels)
{
// Ignore if disposed.
if (audioStreamTrack == null) return;

// When got audio data from listener, the gameObject this script
// attached to, generally the MainCamera object, we feed audio data to
// audio stream track.
const int sampleRate = 48000;
audioStreamTrack.SetData(data, channels, sampleRate);
if (handleOnAudioFilterRead != null)
{
handleOnAudioFilterRead(data, channels);
}
}

void Update()
Expand Down
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![](https://badgen.net/discord/members/yZ4BnPmHAd)](https://discord.gg/yZ4BnPmHAd)

Video Streaming and WebRTC Samples for Unity, please read this [link]() for detail.
SRS WebRTC Samples for Unity, please read this [link]() for detail.

Unity supports WebRTC, see [com.unity.webrtc](https://docs.unity3d.com/Packages/com.unity.webrtc@2.4/manual/index.html) or [github](https://github.com/Unity-Technologies/com.unity.webrtc). However, the demos only work in P2P mode, not with remote SFU or SRS.

Expand Down Expand Up @@ -56,7 +56,7 @@ To publish your WebCamera and Microphone using WebRTC. If you're stuck, please g

1. Download package [SRS.WebRTC.Samples.unitypackage](https://github.com/ossrs/srs-unity/releases/latest/download/SRS.WebRTC.Samples.unitypackage).
1. Click `Asserts > Import Package > Custom Package`, select the file `SRS.WebRTC.Samples.unitypackage`, then click `Import`.
1. From `Project` panel, open `Asserts > ossrs.io > Video Streaming and WebRTC Samples`, where you got all samples there.
1. From `Project` panel, open `Asserts > ossrs.io > SRS WebRTC Samples`, where you got all samples there.

**Step 5:** Start [SRS](https://ossrs.io/lts/en-us/docs/v5/doc/getting-started) WebRTC media server:

Expand All @@ -75,7 +75,7 @@ docker run --rm -it -p 1935:1935 -p 1985:1985 -p 8080:8080 \
**Step 6:** Work with `Publisher` sample.

1. From `Project` panel, open `Asserts > ossrs.io > Video Streaming and WebRTC Samples > Publisher`, then open the `Scene`.
1. From `Project` panel, open `Asserts > ossrs.io > SRS WebRTC Samples > Publisher`, then open the `Scene`.
1. Click `Edit > Play` to play Unity scene, which publish WebRTC stream to SRS.
1. Play the WebRTC stream by [H5](http://localhost:8080/players/rtc_player.html?autostart=true).

Expand Down Expand Up @@ -122,7 +122,7 @@ To publish your game camera and voice using WebRTC. If you're stuck, please get

1. Download package [SRS.WebRTC.Samples.unitypackage](https://github.com/ossrs/srs-unity/releases/latest/download/SRS.WebRTC.Samples.unitypackage).
1. Click `Asserts > Import Package > Custom Package`, select the file `SRS.WebRTC.Samples.unitypackage`, then click `Import`.
1. From `Project` panel, open `Asserts > ossrs.io > Video Streaming and WebRTC Samples`, where you got all samples there.
1. From `Project` panel, open `Asserts > ossrs.io > SRS WebRTC Samples`, where you got all samples there.

**Step 5:** Start [SRS](https://ossrs.io/lts/en-us/docs/v5/doc/getting-started) WebRTC media server:

Expand All @@ -141,11 +141,11 @@ docker run --rm -it -p 1935:1935 -p 1985:1985 -p 8080:8080 \
**Step 6:** Work with `Streamer` sample.

1. From `Project` panel, open `Asserts > ossrs.io > Video Streaming and WebRTC Samples > Streamer`, then open the `Scene`.
1. From `Project` panel, open `Asserts > ossrs.io > SRS WebRTC Samples > Streamer`, then open the `Scene`.
1. Click `Edit > Play` to play Unity scene, which publish WebRTC stream to SRS.
1. Play the WebRTC stream by [H5](http://localhost:8080/players/rtc_player.html?autostart=true).

> Note: Note that the `Streamer` scene grab an extra camera in game and voice of `Main Camera`, however there is no voice so you can add some audio clip to play.
> Note: Note that the `Streamer` scene grab an extra camera in game and voice of `Main Camera`, and we add an example audio clip from [hls](https://developer.apple.com/streaming/examples/basic-stream-osx-ios4-3.html).
The stream flows like this:

Expand Down Expand Up @@ -194,7 +194,7 @@ To play stream using WebRTC. If you're stuck, please get help from [Discord](htt

1. Download package [SRS.WebRTC.Samples.unitypackage](https://github.com/ossrs/srs-unity/releases/latest/download/SRS.WebRTC.Samples.unitypackage).
1. Click `Asserts > Import Package > Custom Package`, select the file `SRS.WebRTC.Samples.unitypackage`, then click `Import`.
1. From `Project` panel, open `Asserts > ossrs.io > Video Streaming and WebRTC Samples`, where you got all samples there.
1. From `Project` panel, open `Asserts > ossrs.io > SRS WebRTC Samples`, where you got all samples there.

**Step 5:** Start [SRS](https://ossrs.io/lts/en-us/docs/v5/doc/getting-started) WebRTC media server:

Expand All @@ -214,7 +214,7 @@ docker run --rm -it -p 1935:1935 -p 1985:1985 -p 8080:8080 \
**Step 6:** Work with `Player` sample.

1. Publish the WebRTC stream by [H5](http://localhost:8080/players/rtc_publisher.html?autostart=true).
1. From `Project` panel, open `Asserts > ossrs.io > Video Streaming and WebRTC Samples > Player`, then open the `Scene`.
1. From `Project` panel, open `Asserts > ossrs.io > SRS WebRTC Samples > Player`, then open the `Scene`.
1. Click `Edit > Play` to play Unity scene, which publish WebRTC stream to SRS.

> Note: You're also able to publish a live stream and play it in Unity using WebRTC, see [WebRTC for Live Streaming](https://ossrs.io/lts/en-us/docs/v5/doc/getting-started#webrtc-for-live-streaming).
Expand Down

0 comments on commit e513657

Please sign in to comment.