Ejemplo n.º 1
0
 void DisposeAsync()
 {
     while (!RefCounter.Free)
     {
         // we're not in a hurry when disposing, give time to other tasks to finish
         Thread.Sleep(3);
     }
     decoder.Dispose();
     cts.Dispose();
 }
Ejemplo n.º 2
0
        public void Write(RtpPacket packet)
        {
            lock (_lock)
            {
                var delta = _codec != null ? (short)(packet.SequenceNumber - _lastSeqNumber) : (short)1;

                if (Math.Abs(delta) >= _bufferSize)
                {
                    ResetBuffer();
                    delta = 1;
                }

                if (_codec == null)
                {
                    _lastSeqNumber = packet.SequenceNumber - 1;
                    _readSeqNumber = packet.SequenceNumber;
                }

                if (_codec == null || _lastPayloadType != packet.PayloadType)
                {
                    _codec?.Dispose();
                    _codec = _codecFactory(packet.PayloadType);

                    _packetDuration = _codec.CalcSamplesNumber(packet.Payload, 0, packet.Payload.Length);
                    if (_samples == null || _samples.Length < _packetDuration)
                    {
                        _samples = new short[_packetDuration];
                    }

                    _bufferSize = (2 * _playBuffer + _packetDuration - 1) / _packetDuration;
                    if (_packets == null || _packets.Length < _bufferSize)
                    {
                        _packets = new RtpPacket[_bufferSize];
                    }

                    _lastPayloadType = packet.PayloadType;
                }

                _packets[(_lastSeqNumber + delta) % _bufferSize] = packet;
                if (delta > 0)
                {
                    _lastSeqNumber += delta;
                }
                if (_lastSeqNumber - _readSeqNumber >= _bufferSize)
                {
                    _readSeqNumber = _lastSeqNumber - _bufferSize + 1;
                }
            }
        }
Ejemplo n.º 3
0
 public void Dispose()
 {
     cts.Cancel();
     try
     {
         decoder.Dispose();
         if (needsDispose)
         {
             lock (streamLock)
             {
                 FMemoryPool.ManagedStreamPool.PutStream(memoryStream);
                 //memoryStream.Dispose();
             }
             needsDispose = false;
         }
     }
     catch (Exception e)
     {
         System.Diagnostics.Debug.WriteLine(e);
     }
     finally
     {
         decoder      = null;
         memoryStream = null;
     }
     cts.Dispose();
 }
Ejemplo n.º 4
0
 public void StopMusic()
 {
     if (MusicState == AudioPlayState.Playing)
     {
         MusicStreamer.Stop();
         MusicStreamer.Dispose();
         MusicDecoder.Dispose();
         MusicStreamer = null;
         if (Stream != null)
         {
             try {
                 Stream.Dispose();
                 // Analysis disable once EmptyGeneralCatchClause
             } catch { }
         }
     }
 }
Ejemplo n.º 5
0
        public void Dispose()
        {
            if (disposed)
            {
                return;
            }
            if (Decoder != null)
            {
                Decoder.Dispose();
            }
            if (DisposeCallback != null)
            {
                DisposeCallback();
            }

            disposed = true;
        }
Ejemplo n.º 6
0
 public void PlayMusic(string filename)
 {
     StopMusic();
     Stream        = File.OpenRead(filename);
     MusicDecoder  = DecoderFactory.GetDecoderFromStream(Stream);
     MusicStreamer = new StreamingAudio(Device, MusicDecoder.Format, MusicDecoder.SampleRate);
     MusicStreamer.BufferNeeded     += (instance, buffer) => MusicDecoder.Read(buffer.Length, buffer);
     MusicStreamer.PlaybackFinished += (sender, e) => {
         if (loopMusic)
         {
             PlayMusic(filename);
         }
         else
         {
             MusicStreamer.Dispose();
             MusicDecoder.Dispose();
             MusicStreamer = null;
         }
     };
     MusicStreamer.Play();
 }
Ejemplo n.º 7
0
        internal void EncodeOggAudio(AudioFormat data, FormatData destination, IFormat format, Stream stream, ProgressIndicator progress)
        {
            IDecoder decoder = data.Decoder;

            RawkAudio.Encoder encoder = new RawkAudio.Encoder(stream, decoder.Channels, decoder.SampleRate);

            AudioFormat.ProcessOffset(decoder, encoder, data.InitialOffset);

            progress.NewTask(1);

            AudioFormat.Transcode(encoder, decoder, progress);
            progress.Progress();

            encoder.Dispose();
            destination.CloseStream(format, AudioName);

            decoder.Dispose();

            data.Save(destination.AddStream(format, FormatName));
            destination.CloseStream(format, FormatName);

            progress.EndTask();
        }
Ejemplo n.º 8
0
        public AudioBuffer(IDecoder decoder)
        {
            if (decoder == null)
                throw new ArgumentNullException("decoder");

            Buffer = AL.GenBuffer();
            Util.CheckOpenAlErrors();

            var data = new float[decoder.TotalSamples];
            var castData = new short[decoder.TotalSamples];

            int read = 0;
            while (read < data.Length)
            {
                read += decoder.ReadSamples(data, read, data.Length - read);
            }

            Util.CastBuffer(data, castData, data.Length);

            AL.BufferData(Buffer, Util.ToOpenAL(decoder.Format), castData, castData.Length * sizeof(short), decoder.Frequency);
            Util.CheckOpenAlErrors();

            decoder.Dispose();
        }
Ejemplo n.º 9
0
 public void PlayMusic(string filename)
 {
     StopMusic ();
     Stream = File.OpenRead (filename);
     MusicDecoder = DecoderFactory.GetDecoderFromStream (Stream);
     MusicStreamer = new StreamingAudio (Device, MusicDecoder.Format, MusicDecoder.SampleRate);
     MusicStreamer.BufferNeeded += (instance, buffer) => MusicDecoder.Read (buffer.Length, buffer);
     MusicStreamer.PlaybackFinished += (sender, e) => {
         if (loopMusic)
             PlayMusic (filename);
         else {
             MusicStreamer.Dispose ();
             MusicDecoder.Dispose ();
             MusicStreamer = null;
         }
     };
     MusicStreamer.Play ();
 }
Ejemplo n.º 10
0
        void decodeThread(IDecoder decoder)
        {
            voiceClient.frontend.LogInfo(LogPrefix + ": Starting decode thread");
            try
            {
                decoder.Open(Info);

                while (!disposed)
                {
                    frameQueueReady.WaitOne(); // Wait until data is pushed to the queue or Dispose signals.

                    while (true)               // Dequeue and process while the queue is not empty
                    {
                        if (disposed)
                        {
                            break;           // early exit to save few resources
                        }
                        byte[] f  = null;
                        bool   ok = false;
                        lock (frameQueue)
                        {
                            if (frameQueue.Count > 0)
                            {
                                ok = true;
                                f  = frameQueue.Dequeue();
                            }
                        }
                        if (ok)
                        {
                            decodeFrame(decoder, f);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                voiceClient.frontend.LogError(LogPrefix + ": Exception in decode thread: " + e);
                throw e;
            }
            finally
            {
                lock (disposeLock)   // sync with receiveFrame/receiveNullFrames
                {
                    disposed = true; // set to disposing state if exiting due to exception
                }
                // cleaning up being sure that fields are not updated anymore
#if NETFX_CORE
                frameQueueReady.Dispose();
#else
                frameQueueReady.Close();
#endif
                lock (frameQueue)
                {
                    frameQueue.Clear();
                }
                decoder.Dispose();

                voiceClient.frontend.LogInfo(LogPrefix + ": Exiting decode thread");
            }
        }
Ejemplo n.º 11
0
 public void Dispose()
 {
     _encoder?.Dispose();
     _decoder?.Dispose();
 }
Ejemplo n.º 12
0
 public void Dispose()
 {
     Decoder.Dispose();
 }