Skip to content

Commit 4c8965f

Browse files
authored
fix: Fix audio codec selector in Unity Editor (#777)
1 parent 9fe908e commit 4c8965f

File tree

4 files changed

+59
-14
lines changed

4 files changed

+59
-14
lines changed

com.unity.renderstreaming/Editor/PropertyDrawers/CodecDrawer.cs

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ interface Codec
1616
string name { get; }
1717
string mimeType { get; }
1818
string sdpFmtpLine { get; }
19+
int channelCount { get; }
20+
int sampleRate { get; }
1921
string optionTitle { get; }
2022
}
2123

@@ -24,6 +26,8 @@ class AudioCodec : Codec
2426
public string name { get { return codec_.name; } }
2527
public string mimeType { get { return codec_.mimeType; } }
2628
public string sdpFmtpLine { get { return codec_.sdpFmtpLine; } }
29+
public int channelCount { get { return codec_.channelCount; } }
30+
public int sampleRate { get { return codec_.sampleRate; } }
2731

2832
public string optionTitle
2933
{
@@ -60,6 +64,10 @@ public string optionTitle
6064
return null;
6165
}
6266
}
67+
68+
public int channelCount { get { throw new NotSupportedException(); } }
69+
public int sampleRate { get { throw new NotSupportedException(); } }
70+
6371
public VideoCodec(VideoCodecInfo codec)
6472
{
6573
codec_ = codec;
@@ -69,14 +77,17 @@ public VideoCodec(VideoCodecInfo codec)
6977

7078
SerializedProperty propertyMimeType;
7179
SerializedProperty propertySdpFmtpLine;
80+
SerializedProperty propertyChannelCount;
81+
SerializedProperty propertySampleRate;
82+
7283
IEnumerable<Codec> codecs;
7384
string[] codecNames = new string[] { "Default" };
7485
string[] codecOptions = new string[] {};
75-
string[] sdpFmtpLines = new string[] {};
86+
IEnumerable<Codec> selectedCodecs;
7687
GUIContent codecLabel;
7788

7889
int selectCodecIndex = 0;
79-
int selectSdpFmtpLineIndex = 0;
90+
int selectCodecOptionIndex = 0;
8091
bool hasCodecOptions = false;
8192
bool cache = false;
8293
bool changed = false;
@@ -120,27 +131,43 @@ static GUIContent GetCodecLabel(UnityEngine.Object target)
120131
throw new ArgumentException();
121132
}
122133

134+
int FindOptionIndex(IEnumerable<Codec> codecs)
135+
{
136+
return Array.FindIndex(codecs.ToArray(), codec =>
137+
{
138+
if (codec is VideoCodec)
139+
return codec.sdpFmtpLine == propertySdpFmtpLine.stringValue;
140+
else
141+
return
142+
codec.sdpFmtpLine == propertySdpFmtpLine.stringValue &&
143+
codec.channelCount == propertyChannelCount.intValue &&
144+
codec.sampleRate == propertySampleRate.intValue;
145+
});
146+
}
147+
123148
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
124149
{
125150
if (!cache)
126151
{
127152
propertyMimeType = property.FindPropertyInChildren("m_MimeType");
128153
propertySdpFmtpLine = property.FindPropertyInChildren("m_SdpFmtpLine");
154+
propertyChannelCount = property.FindPropertyInChildren("m_ChannelCount");
155+
propertySampleRate = property.FindPropertyInChildren("m_SampleRate");
156+
129157
codecs = GetAvailableCodecs(property.serializedObject.targetObject);
130158
codecNames = codecNames.Concat(codecs.Select(codec => codec.name)).Distinct().ToArray();
131159
var mimeType = propertyMimeType.stringValue;
132160
var codecName = mimeType.GetCodecName();
133-
var selectedCodecs = codecs.Where(codec => codec.name == codecName);
161+
selectedCodecs = codecs.Where(codec => codec.name == codecName);
134162
codecOptions = selectedCodecs.Select(codec => codec.optionTitle).ToArray();
135-
sdpFmtpLines = selectedCodecs.Select(codec => codec.sdpFmtpLine).ToArray();
136163
if (!selectedCodecs.Any())
137164
selectCodecIndex = 0;
138165
else
139166
selectCodecIndex = Array.FindIndex(codecNames, codec => codec == codecName);
140167
codecLabel = GetCodecLabel(property.serializedObject.targetObject);
141168
hasCodecOptions = codecOptions.Length > 1;
142169
if (hasCodecOptions)
143-
selectSdpFmtpLineIndex = Array.FindIndex(sdpFmtpLines, sdp => sdp == propertySdpFmtpLine.stringValue);
170+
selectCodecOptionIndex = FindOptionIndex(selectedCodecs);
144171
cache = true;
145172
}
146173

@@ -158,17 +185,25 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
158185
if(0 < selectCodecIndex)
159186
{
160187
string codecName = codecNames[selectCodecIndex];
161-
var selectedCodecs = codecs.Where(codec => codec.name == codecName);
188+
selectedCodecs = codecs.Where(codec => codec.name == codecName);
162189
codecOptions = selectedCodecs.Select(codec => codec.optionTitle).ToArray();
163-
sdpFmtpLines = selectedCodecs.Select(codec => codec.sdpFmtpLine).ToArray();
164190
hasCodecOptions = codecOptions.Length > 1;
165-
propertyMimeType.stringValue = selectedCodecs.First().mimeType;
166-
propertySdpFmtpLine.stringValue = sdpFmtpLines[0];
191+
var codec = selectedCodecs.First();
192+
propertyMimeType.stringValue = codec.mimeType;
193+
propertySdpFmtpLine.stringValue = codec.sdpFmtpLine;
194+
if(propertyChannelCount != null)
195+
propertyChannelCount.intValue = codec.channelCount;
196+
if (propertySampleRate != null)
197+
propertySampleRate.intValue = codec.sampleRate;
167198
}
168199
else
169200
{
170201
propertyMimeType.stringValue = null;
171202
propertySdpFmtpLine.stringValue = null;
203+
if (propertyChannelCount != null)
204+
propertyChannelCount.intValue = 0;
205+
if (propertySampleRate != null)
206+
propertySampleRate.intValue = 0;
172207
hasCodecOptions = false;
173208
}
174209
}
@@ -184,11 +219,16 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
184219
EditorGUI.BeginProperty(rect, label, propertySdpFmtpLine);
185220

186221
EditorGUI.BeginChangeCheck();
187-
selectSdpFmtpLineIndex = EditorGUI.Popup(rect, selectSdpFmtpLineIndex, codecOptions);
222+
selectCodecOptionIndex = EditorGUI.Popup(rect, selectCodecOptionIndex, codecOptions);
188223

189224
if (EditorGUI.EndChangeCheck())
190225
{
191-
propertySdpFmtpLine.stringValue = sdpFmtpLines[selectSdpFmtpLineIndex];
226+
var codec = selectedCodecs.ElementAt(selectCodecOptionIndex);
227+
propertySdpFmtpLine.stringValue = codec.sdpFmtpLine;
228+
if (propertyChannelCount != null)
229+
propertyChannelCount.intValue = codec.channelCount;
230+
if (propertySampleRate != null)
231+
propertySampleRate.intValue = codec.sampleRate;
192232
}
193233
EditorGUI.EndProperty();
194234
}

com.unity.renderstreaming/Runtime/Scripts/AudioCodecInfo.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public class AudioCodecInfo : IEquatable<AudioCodecInfo>
4444
/// </summary>
4545
public string sdpFmtpLine { get { return m_SdpFmtpLine; } }
4646

47+
static internal AudioCodecInfo Create(RTCRtpCodecCapability caps)
48+
{
49+
return new AudioCodecInfo(caps);
50+
}
51+
4752
/// <summary>
4853
///
4954
/// </summary>
@@ -107,7 +112,7 @@ public override int GetHashCode()
107112
return !(left == right);
108113
}
109114

110-
internal AudioCodecInfo(RTCRtpCodecCapability cap)
115+
protected AudioCodecInfo(RTCRtpCodecCapability cap)
111116
{
112117
m_MimeType = cap.mimeType;
113118
m_SdpFmtpLine = cap.sdpFmtpLine;

com.unity.renderstreaming/Runtime/Scripts/AudioStreamReceiver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static public IEnumerable<AudioCodecInfo> GetAvailableCodecs()
5454
{
5555
var excludeCodecMimeType = new[] { "audio/CN", "audio/telephone-event" };
5656
var capabilities = RTCRtpReceiver.GetCapabilities(TrackKind.Audio);
57-
return capabilities.codecs.Where(codec => !excludeCodecMimeType.Contains(codec.mimeType)).Select(codec => new AudioCodecInfo(codec));
57+
return capabilities.codecs.Where(codec => !excludeCodecMimeType.Contains(codec.mimeType)).Select(codec => AudioCodecInfo.Create(codec));
5858
}
5959

6060
/// <summary>

com.unity.renderstreaming/Runtime/Scripts/AudioStreamSender.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static public IEnumerable<AudioCodecInfo> GetAvailableCodecs()
154154
{
155155
var excludeCodecMimeType = new[] { "audio/CN", "audio/telephone-event" };
156156
var capabilities = RTCRtpSender.GetCapabilities(TrackKind.Audio);
157-
return capabilities.codecs.Where(codec => !excludeCodecMimeType.Contains(codec.mimeType)).Select(codec => new AudioCodecInfo(codec));
157+
return capabilities.codecs.Where(codec => !excludeCodecMimeType.Contains(codec.mimeType)).Select(codec => AudioCodecInfo.Create(codec));
158158
}
159159

160160
/// <summary>

0 commit comments

Comments
 (0)