Forum


Notice to all users

We are migrating towards a new forum system located at community.teamspeak.com, as such this forum will become read-only on January 29, 2020

Results 1 to 7 of 7
  1. #1
    Join Date
    July 2017
    Posts
    8

    TalkStatusChanged Event firing repeatedly with C# SDK

    I'm experiencing a strange error with the C# SDK v0.2 on .Net Framework 4.6.2. I've created a PTT client (Client #1), and another client (Client #2) that has an event handler for the TalkStatusChanged event. Client #2 is playing to a Custom Device.

    The event handler on Client #2 seems be incorrectly firing repeatedly when I hold down the PTT key on client #1. The server console correctly shows a single onClientStartTalkingEvent message when I press the PTT key, and correctly shows a single onClientStopTalkingEvent when I release the PTT key. However Client #2 seems to cycle through Start and Stop events repeatedly if I hold the PTT key down on Client #1. If I do a quick PTT button press, Client #2 will show a single Start/Stop cycle.

    My event handler code is fairly simple:

    Code:
            private void Connection_TalkStatusChanged(Client client, TalkStatus status, bool isReceivedWhisper)
            {
                switch (status)
                {
                    case TalkStatus.Talking:
                        Trace.WriteLine($"Client {client.ID} Started Talking");
                        break;
    
                    case TalkStatus.NotTalking:
                        Trace.WriteLine($"Client {client.ID} Stopped Talking");
                        // Create a task to process 
                        break;
                }
            }
    The only thing I can think of is the fact that I'm using a custom device for playback. Perhaps it raises a Talking event every time I acquire data from the custom device?
    Last edited by sjpritchard; July 27th, 2017 at 11:58 AM.

  2. #2
    Join Date
    September 2012
    Posts
    6,079
    What client IDs do you see in those rapid events in client 2?
    When sending PMs please make sure to include a reference link to the thread in question in the body of your message.

  3. #3
    Join Date
    July 2017
    Posts
    8
    Both the server and client #2 report that client #1 (ie clientID=1) is stopping and starting (which is as expected).

  4. #4
    Join Date
    July 2017
    Posts
    8
    Here is the full code of client #2.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using TeamSpeak.Sdk;
    using TeamSpeak.Sdk.Client;
    
    namespace simdjinni_voiceserver
    {
        class Server
        {
            static Connection Connection;
            const string IdentityFile = "identity.txt";
            private CustomDevice VirtualDevice;
            Dictionary<ushort, MemoryStream> VoiceStreams = new Dictionary<ushort, MemoryStream>();
            int counter = 0;
    
    
            public Server()
            {
    
                // Initialise TS3
                InitialiseTS3();
    
                // Register custom devices
    
                VirtualDevice = new CustomDevice(
                    "VirtualDevice",
                    SamplingRate.Hz16000,
                    1,
                    SamplingRate.Hz16000,
                    1);
                Connection.OpenPlayback(VirtualDevice);
                Connection.OpenCapture(VirtualDevice);
    
                string identity = ReadIdentity() ?? Library.CreateIdentity();
                Console.WriteLine($"Client Identity: {identity}");
    
                Task starting = Connection.Start(identity, "192.168.0.10", 9987, "Watson", serverPassword: "secret");
    
                Console.WriteLine("Client lib initialized and running");
                Console.WriteLine($"Client lib version: {Library.Version}({Library.VersionNumber})");
    
                try
                {
                    starting.Wait();
                }
                catch (AggregateException e)
                {
                    if (e.InnerException is TeamSpeakException)
                    {
                        Error errorCode = ((TeamSpeakException)e.InnerException).ErrorCode;
                        Console.WriteLine("Failed to connect to server: {0}", errorCode);
                        return;
                    }
                    else
                    {
                        throw;
                    }
                }
                Console.WriteLine();
                Console.WriteLine("TeamSpeak 3 client initialised");
                Connection.Preprocessor.Vad = false;
    
    
    
                var samples = (int)(16000 * 0.02);
                var buffer = new short[samples];
    
                Task.Run(() =>
                {
                    while (true)
                    {
                        // Drain the virtual playback buffer 20ms at a time
                        while (VirtualDevice.AcquireData(buffer, samples)) { }
                        Thread.Sleep(20);
                    }
                });
    
                // Main loop
                while (true);
            }
    
    
            private void InitialiseTS3()
    
            {
                // Pass in location of native SDK
                var parameters = new LibraryParameters("../../../ts3_sdk_3.0.4/bin/");
                parameters.UsedLogTypes = LogTypes.File | LogTypes.Console | LogTypes.Userlogging;
                Library.Initialize(parameters);
    
                Connection = Library.SpawnNewConnection();
                Connection.Self.IsInputDeactivated = true;
    
                // Configure event handlers
                Connection.TalkStatusChanged += Connection_TalkStatusChanged;
                Connection.EditPlaybackVoiceData += Connection_EditPlaybackVoiceData;
    
    
            }
    
            private void Connection_TalkStatusChanged(Client client, TalkStatus status, bool isReceivedWhisper)
            {
                counter++;
                Trace.WriteLine($"Status changed message #{counter}");
                switch (status)
                {
                    case TalkStatus.Talking:
                        Trace.WriteLine($"Client {client.ID} Started Talking");
                        // If we have a previous Voice Stream, delete it
                        if (this.VoiceStreams.ContainsKey(client.ID))
                        {
                            this.VoiceStreams.Remove(client.ID);
                        }
                        // Create a new VoiceStream
                        this.VoiceStreams.Add(client.ID, new MemoryStream());
    
                        break;
    
                    case TalkStatus.NotTalking:
                        Trace.WriteLine($"Client {client.ID} Stopped Talking");
                        // Create a task to process 
                        break;
                }
            }
    
            private string ReadIdentity()
            {
                try
                {
                    return File.ReadAllText(IdentityFile);
                }
                catch
                {
                    Console.WriteLine($"Could not read file '{IdentityFile}'.");
                    return null;
                }
            }
        }
    }
    Last edited by sjpritchard; July 29th, 2017 at 12:11 PM.

  5. #5
    Join Date
    July 2017
    Posts
    8
    I've been able to isolate the problem a bit. It only occurs when I use a custom device on client #2. When I use a standard PlayBack and Capture device, eg Connection.OpenPlayBack() and ConnectionOpenCapture(), it works perfectly. The only difference between the working and non-working versions is the custom vs normal device registration, and a Task in the custom device version that acquires playback data every 20ms (which is required otherwise it crashes with a stale decoder error).

    Bug in the SDK? Or am I doing something wrong here?

  6. #6
    Join Date
    May 2016
    Location
    Idar-Oberstein
    Posts
    163
    This happens because the talk status events are generated on the basis of having data for the client.
    Once you change
    Code:
    // Drain the virtual playback buffer 20ms at a time
    while (VirtualDevice.AcquireData(buffer, samples)) { }
    to
    Code:
    VirtualDevice.AcquireData(buffer, samples);
    the issue disappears.

    We'll look into making this more robust. I would have drained the buffer fully as my first approach on most situations, too.

  7. #7
    Join Date
    July 2017
    Posts
    8
    Thanks for that - works perfectly now!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: April 18th, 2016, 09:41 AM
  2. Server shutting down Repeatedly after applying NPL
    By Ravyns in forum Linux / FreeBSD
    Replies: 19
    Last Post: December 12th, 2014, 10:04 PM
  3. Replies: 1
    Last Post: September 5th, 2014, 09:52 AM
  4. Connection to server lost, repeatedly and randomly.
    By Hyperstrike in forum Server Support
    Replies: 0
    Last Post: March 28th, 2010, 06:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •