/// <summary> /// Отдельный поток воспроизведения информации /// </summary> private void Playing() { while (true) { if (_packages.Count < 2) { Thread.Sleep(Constants.FragmentLenght); continue; } TimeSpan sleepTime; lock (_packages) { var firstFragment = _packages.First(); sleepTime = _packages.ElementAt(1).Key - firstFragment.Key; var audioData = firstFragment.Value.AudioData; if (audioData != null) { _audioPresenter.SetPlayFragment( new AudioFragment(AudioCodec.Decode(audioData, 0, audioData.Length))); } var videoData = firstFragment.Value.VideoData; if (videoData != null) { _videoPresenter.SetPlayFragment(new VideoFragment(videoData)); } _packages.Remove(firstFragment.Key); } Thread.Sleep(sleepTime); } }
/// <summary> /// This method is called when new RTP packet received. /// </summary> /// <param name="sender">Sender.</param> /// <param name="e">Event data.</param> private void m_pRTP_Stream_PacketReceived(object sender, RTP_PacketEventArgs e) { if (m_IsDisposed) { return; } try { AudioCodec codec = null; if (!m_pAudioCodecs.TryGetValue(e.Packet.PayloadType, out codec)) { // Unknown codec(payload value), skip it. return; } m_pActiveCodec = codec; // Audio-out not created yet, create it. if (m_pAudioOut == null) { m_pAudioOut = new AudioOut(m_pAudioOutDevice, codec.AudioFormat); } // Audio-out audio format not compatible to codec, recreate it. else if (!m_pAudioOut.AudioFormat.Equals(codec.AudioFormat)) { m_pAudioOut.Dispose(); m_pAudioOut = new AudioOut(m_pAudioOutDevice, codec.AudioFormat); } // Decode RTP audio frame and queue it for play out. byte[] decodedData = codec.Decode(e.Packet.Data, 0, e.Packet.Data.Length); m_pAudioOut.Write(decodedData, 0, decodedData.Length); } catch (Exception x) { if (!this.IsDisposed) { // Raise error event(We can't throw expection directly, we are on threadpool thread). OnError(x); } } }
// OnVoIPData public void OnVoIPData(byte[] bytes, int len, uLink.NetworkMessageInfo info) { if (GameManager.isServer) { if (info.sender != networkView.owner) { return; } networkView.RPC("VoIPData", uLink.RPCMode.OthersExceptOwner, bytes, len); // ExceptOwner return; } // Noise detection if (bytes.Length == 0) { samples = new float[0]; return; } samples = codec.Decode(bytes, len); // Play if (!networkViewIsMine || Config.instance.hearMyself) { LogManager.General.Log("playing " + samples.Length); if (!audioSources[0].isPlaying) { audioSources[0].Play(); } speaker.AddSamples(samples); } // Visualization visualizationSamples = nextVisualizationSamples; nextVisualizationSamples = CalculateNewVisualization(); visualizationTime = 0f; }