public void SetActiveCameraAndMicrophone(WebCamTexture activeWebCamTexture, AudioSource activeMicrophone)
{
// 기존의 비디오 트랙과 오디오 트랙을 제거합니다.
var senders = _peerConnection.GetSenders();
foreach (var sender in senders)
{
_peerConnection.RemoveTrack(sender);
}
// 비디오 트랙을 추가합니다.
var videoTrack = new VideoStreamTrack(activeWebCamTexture);
_peerConnection.AddTrack(videoTrack);
Debug.Log("Sender video track was set");
// 마이크 트랙을 추가합니다.
var audioTrack = new AudioStreamTrack(activeMicrophone);
audioTrack.Loopback = true;
_peerConnection.AddTrack(audioTrack);
if (activeMicrophone.clip == null)
{
Debug.Log("not activeMicrophone");
}
else
{
Debug.Log("good activeMicrophone");
}
Debug.Log("Sender audio track was set");
}
// 연결을 시작하는 메서드
public void Connect()
{
// _peerConnection이 null이라면 초기화
if (_peerConnection == null)
{
Debug.LogError("_peerConnection is null, attempting to reinitialize.");
var config = new RTCConfiguration
{
iceServers = new RTCIceServer[]
{
new RTCIceServer { urls = new string[] { "stun:stun.l.google.com:19302" } }
}
};
_peerConnection = new RTCPeerConnection(ref config);
if (_peerConnection == null)
{
Debug.LogError("Failed to reinitialize RTCPeerConnection.");
return;
}
}
// 연결을 시도하기 전에 _peerConnection이 null이 아닌지 확인
if (_peerConnection != null)
{
StartCoroutine(CreateAndSendLocalSdpOffer());
}
else
{
Debug.LogError("_peerConnection is null after reinitialization.");
}
}
// 연결을 종료하는 메서드
public void Disconnect()
{
if (!IsConnected)
{
return;
}
_peerConnection.Close();
_peerConnection.Dispose();
_peerConnection = null; // Disconnect 후 _peerConnection을 null로 설정
}
// WebRTC 이벤트 핸들러들
private void OnTrack(RTCTrackEvent trackEvent)
{
// 비디오 트랙을 처리하는 부분
if (trackEvent.Track is VideoStreamTrack videoStreamTrack)
{
videoStreamTrack.OnVideoReceived += OnVideoReceived;
}
// 오디오 트랙을 처리하는 부분
else if (trackEvent.Track is AudioStreamTrack audioStreamTrack)
{
OutputAudioSource.SetTrack(audioStreamTrack);
if(OutputAudioSource.clip==null)
{
Debug.Log("OutputAudioSource.clip is null");
}
OutputAudioSource.loop = true; // 필요 시 루프 설정
OutputAudioSource.Play();
Debug.Log("Audio track connected.");
Debug.Log("event register");
}
else
{
Debug.LogError($"Unhandled track of type: {trackEvent.Track.GetType()}. Only video and audio tracks are handled.");
}
}
“OutputAudioSource.clipisnull” log appears in this code, but I don’t know how to fix it. I checked that the audiosource has been added to the track, and I checked that the clip is notnull, but that log appears in the onTrack() method.