Пример #1
0
        private void Setup()
        {
            WebRtcAudioDsp dsp = gameObject.GetComponent <WebRtcAudioDsp>();

            if (dsp != null && dsp.isActiveAndEnabled)
            {
                forceShort = true;
                if (this.Logger.IsInfoEnabled)
                {
                    this.Logger.LogInfo("Type Conversion set to Short. Audio samples will be converted if source samples types differ.");
                }
            }
            else if (forceShort)
            {
                forceShort = false;
                if (this.Logger.IsInfoEnabled)
                {
                    this.Logger.LogInfo("Type Conversion disabled. Audio samples will not be converted if source samples types differ.");
                }
            }
            this.voice = CreateLocalVoiceAudioAndSource();
            if (this.voice == LocalVoiceAudioDummy.Dummy)
            {
                if (this.Logger.IsErrorEnabled)
                {
                    this.Logger.LogError("Local input source setup and voice stream creation failed. No recording or transmission will be happening. See previous error log messages for more details.");
                }
                return;
            }
            if (this.ReactOnSystemChanges && !this.subscribedToSystemChanges)
            {
                AudioSettings.OnAudioConfigurationChanged += this.OnAudioConfigChanged;
                this.subscribedToSystemChanges             = true;
            }
            if (this.VoiceDetector != null)
            {
                this.VoiceDetector.Threshold       = this.voiceDetectionThreshold;
                this.VoiceDetector.ActivityDelayMs = this.voiceDetectionDelayMs;
                this.VoiceDetector.On = this.voiceDetection;
            }
            this.voice.InterestGroup = this.InterestGroup;
            this.voice.DebugEchoMode = DebugEchoMode;
            this.voice.Encrypt       = Encrypt;
            this.voice.Reliable      = this.ReliableMode;
            this.RequiresRestart     = false;
            this.isRecording         = true;
            SendPhotonVoiceCreatedMessage();
            this.voice.TransmitEnabled = this.TransmitEnabled;
        }
Пример #2
0
        private LocalVoice CreateLocalVoiceAudioAndSource()
        {
            switch (SourceType)
            {
            case InputSourceType.Microphone:
            {
                if (this.MicrophoneType == MicType.Photon)
                {
                        #if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_EDITOR_WIN
                    var hwMicDev = this.PhotonMicrophoneDeviceId;
                    if (this.Logger.IsInfoEnabled)
                    {
                        this.Logger.LogInfo("Setting recorder's source to Photon microphone device [{0}] \"{1}\"", hwMicDev, PhotonMicrophoneEnumerator.NameAtIndex(hwMicDev));
                    }
                        #else
                    if (this.Logger.IsInfoEnabled)
                    {
                        this.Logger.LogInfo("Setting recorder's source to Photon microphone device");
                    }
                        #endif
                        #if UNITY_STANDALONE_WIN && !UNITY_EDITOR || UNITY_EDITOR_WIN
                    if (this.Logger.IsInfoEnabled)
                    {
                        this.Logger.LogInfo("Setting recorder's source to WindowsAudioInPusher");
                    }
                    inputSource = new Windows.WindowsAudioInPusher(hwMicDev, this.Logger);
                        #elif UNITY_IOS && !UNITY_EDITOR
                    if (this.Logger.IsInfoEnabled)
                    {
                        this.Logger.LogInfo("Setting recorder's source to IOS.AudioInPusher with session {0}", audioSessionParameters);
                    }
                    inputSource = new IOS.AudioInPusher(audioSessionParameters, this.Logger);
                        #elif UNITY_STANDALONE_OSX && !UNITY_EDITOR || UNITY_EDITOR_OSX
                    if (this.Logger.IsInfoEnabled)
                    {
                        this.Logger.LogInfo("Setting recorder's source to MacOS.AudioInPusher");
                    }
                    inputSource = new MacOS.AudioInPusher(hwMicDev, this.Logger);
                        #elif UNITY_ANDROID && !UNITY_EDITOR
                    if (this.Logger.IsInfoEnabled)
                    {
                        this.Logger.LogInfo("Setting recorder's source to UnityAndroidAudioInAEC");
                    }
                    inputSource = new UnityAndroidAudioInAEC(this.Logger);
                        #else
                    inputSource = new AudioDesc(0, 0, "Photon microphone type is not supported for the current platform.");
                        #endif
                    if (inputSource.Error == null)
                    {
                        break;
                    }
                    if (this.Logger.IsErrorEnabled)
                    {
                        this.Logger.LogError("Photon microphone input source creation failure: {0}. Falling back to Unity microphone", inputSource.Error);
                    }
                }
                if (Microphone.devices.Length < 1)
                {
                    if (this.Logger.IsInfoEnabled)
                    {
                        this.Logger.LogInfo("No Microphone");
                    }
                    return(LocalVoiceAudioDummy.Dummy);
                }
                var micDev = this.UnityMicrophoneDevice;
                if (this.Logger.IsInfoEnabled)
                {
                    this.Logger.LogInfo("Setting recorder's source to Unity microphone device {0}", micDev);
                }
                // mic can ignore passed sampling rate and set its own
                inputSource = new MicWrapper(micDev, (int)SamplingRate, this.Logger);
                if (inputSource.Error != null && this.Logger.IsErrorEnabled)
                {
                    this.Logger.LogError("Unity microphone input source creation failure: {0}.", inputSource.Error);
                }
            }
            break;

            case InputSourceType.AudioClip:
            {
                if (AudioClip == null)
                {
                    if (this.Logger.IsErrorEnabled)
                    {
                        this.Logger.LogError("AudioClip property must be set for AudioClip audio source");
                    }
                    return(LocalVoiceAudioDummy.Dummy);
                }
                AudioClipWrapper audioClipWrapper = new AudioClipWrapper(this.AudioClip);     // never fails, no need to check Error
                audioClipWrapper.Loop = this.LoopAudioClip;
                this.inputSource      = audioClipWrapper;
            }
            break;

            case InputSourceType.Factory:
            {
                if (InputFactory == null)
                {
                    if (this.Logger.IsErrorEnabled)
                    {
                        this.Logger.LogError("Recorder.InputFactory must be specified if Recorder.Source set to Factory");
                    }
                    return(LocalVoiceAudioDummy.Dummy);
                }
                inputSource = InputFactory();
                if (inputSource.Error != null && this.Logger.IsErrorEnabled)
                {
                    this.Logger.LogError("InputFactory creation failure: {0}.", inputSource.Error);
                }
            }
            break;

            default:
                if (this.Logger.IsErrorEnabled)
                {
                    this.Logger.LogError("unknown Source value {0}", SourceType);
                }
                return(LocalVoiceAudioDummy.Dummy);
            }
            if (this.inputSource == null || this.inputSource.Error != null)
            {
                return(LocalVoiceAudioDummy.Dummy);
            }
            if (inputSource.Channels == 0)
            {
                if (this.Logger.IsErrorEnabled)
                {
                    this.Logger.LogError("inputSource.Channels is zero");
                }
                return(LocalVoiceAudioDummy.Dummy);
            }
            VoiceInfo      voiceInfo  = VoiceInfo.CreateAudioOpus(SamplingRate, inputSource.Channels, FrameDuration, Bitrate, UserData);
            bool           forceShort = false;
            WebRtcAudioDsp dsp        = this.GetComponent <WebRtcAudioDsp>();
            if (dsp != null && dsp.enabled)
            {
                forceShort = true;
                if (this.Logger.IsInfoEnabled)
                {
                    this.Logger.LogInfo("Type Conversion set to Short. Audio samples will be converted if source samples types differ.");
                }
            }
            return(client.CreateLocalVoiceAudioFromSource(voiceInfo, inputSource, forceShort));
        }