3

I started a voice chat server project, using boost::asio and SMFL audio library. I think my mutex logic is not correct, because I get this error:

Unhandled exception at 0x00B7871D in TCPAudioReceiver.exe: 0xC0000005: Access violation reading location 0x00000000.

The debugger stops at the if(mServer.samplesAvailable()) line in the main and I also notice that the if(mSamplesQueue.front().size() > 256) return true; line in the server reads a size of {???}. I thought that locking the server's mutex would be enough to protect against this kind of problem, but I think I'm still one or two concepts away from the good design.

I used recursive_mutex for no particular reason a this point, it was more an attempt of getting farther in my troubleshooting.

How would you suggest I solve this problem?

main

#include "TcpAsyncServer.h"
#include "TcpAsyncSession.h"
#include "AudioStream.h"
#include <thread>

int main()
{
  io_service ioService;
  deque<vector<Int16>> mSamplesQueue;
  SoundBuffer mSoundBuffer;
  AudioStream mAudioStream;

  cout << "Starting server..." << endl;
  Server mServer(ioService, PORT);
  thread serviceThread([&](){ioService.run();}); 

  cout << "Starting reception loop..." << endl;
  for(;;) { 
    {
      lock_guard<recursive_mutex> lock(mServer.mServerMutex);
      // Look for new samples
      if(mServer.samplesAvailable()) {            // <-- debugger stops here
        cout << "Samples available..." << endl;
        vector<Int16> wSamples;
        mServer.getNextSamples(wSamples);
        mSamplesQueue.push_back(wSamples);
      }
    }

    // Loading and playing samples
    if((mAudioStream.getStatus() != AudioStream::Playing) && !mSamplesQueue.empty()) {
      cout << "Loading and playing audio stream..." << endl;
      if(mSoundBuffer.loadFromSamples(
        reinterpret_cast<Int16*>(mSamplesQueue.front().data()), 
        mSamplesQueue.front().size(), 2, 48000)
      )
      {
        cout << "SoundBuffer loaded successfully..." << endl;
        mAudioStream.load(mSoundBuffer);
        mAudioStream.play();
        mSamplesQueue.pop_front();
      }
      else cout << "SoundBuffer failed to load..." << endl;
    }

    // Give it some room to play the sound
    while (mAudioStream.getStatus() == AudioStream::Playing) {
      sleep(milliseconds(50));
    }
  }
  serviceThread.join();
}

Server header

#include <mutex>
#include <deque>
#include <vector>
#include <iostream>
#include <boost/asio.hpp>
#include <SFML/Audio.hpp>

using namespace sf;
using namespace boost::asio;
using namespace boost::asio::ip;
using namespace std;

#define PORT 2112
#define SAMPLE_BUFFER 512

class Server
{

public:
  // Ctor
  Server(io_service & iService, short iPort);

  // Methods
  bool samplesAvailable();
  void getNextSamples(vector<Int16> oSamples);
  void pushSamples(vector<Int16> iSamples);

  // Variables
  recursive_mutex mServerMutex;

private:

  // Methods
  void doAccept();

  // Variables
  tcp::acceptor mAcceptor;
  tcp::socket   mSocket;
  deque<vector<Int16>> mSamplesQueue;
};

Server class

#include "TcpAsyncServer.h"
#include "TcpAsyncSession.h"

Server::Server(io_service & service, short port)
  : mAcceptor(service, tcp::endpoint(tcp::v4(), port)),
    mSocket(service) {
  doAccept();
}

void Server::doAccept()
{
  mAcceptor.async_accept(mSocket,
    [this]
    (boost::system::error_code error) {
      if(!error) make_shared<Session>(move(mSocket),this)->start();
      doAccept();
    }
  );
}

bool Server::samplesAvailable() {
  lock_guard<recursive_mutex> lock(mServerMutex);
  if(mSamplesQueue.front().size() > 256) return true; // <-- mSamplesQueue.front() has undefined size
  return false;
}

void Server::getNextSamples(vector<Int16> oSamples) {
  lock_guard<recursive_mutex> lock(mServerMutex);
  oSamples = mSamplesQueue.front();   
  mSamplesQueue.pop_front();
}

void Server::pushSamples(vector<Int16> iSamples) {
  lock_guard<recursive_mutex> lock(mServerMutex);
  mSamplesQueue.push_back(iSamples);
}

Session header

#include <iostream>
#include <mutex>
#include <deque>
#include <vector>
#include <boost/asio.hpp>
#include <SFML/Audio.hpp>

using namespace std;
using namespace sf;
using namespace boost::asio;
using namespace boost::asio::ip;

#define BUFFER_SIZE 1024

class Server;

class Session : public enable_shared_from_this<Session>
{

public:
  // Ctor
  Session(tcp::socket & iSocket, Server* iServerPtr);

  // Methods
  void start();

private:
  // Methods
  void doRead();

  // Variables
  tcp::socket   mSocket;
  char          mData[BUFFER_SIZE];
  Server*       mServerPtr; 
  deque<vector<Int16>> mSampleBufferQueue;
};

Session class

#include "TcpAsyncSession.h"
#include "TcpAsyncServer.h"

Session::Session(tcp::socket & iSocket, Server* iServerPtr)
  : mSocket(move(iSocket)),
    mServerPtr(iServerPtr) 
{}

void Session::start() {
  doRead();
}

void Session::doRead() {
  shared_ptr<Session> self(shared_from_this());

  mSocket.async_read_some(buffer(mData,BUFFER_SIZE),
    [this,self]
    (boost::system::error_code error, size_t iBytesReceived) {
      if(!error) {
        cout << "Receiving " << iBytesReceived << " bytes..." << endl; 
        vector<Int16> wSamples;

        for(unsigned int i = 0; i < iBytesReceived; i+=2) {
          wSamples.push_back(static_cast<Int16>( mData[i]<<8 | mData[i] ));
        }

        {
          lock_guard<recursive_mutex> lock(mServerPtr->mServerMutex);
          mServerPtr->pushSamples(move(wSamples));
        }

        doRead();
      }
    }
  );
}
sehe
  • 374,641
  • 47
  • 450
  • 633
VincentDM
  • 469
  • 6
  • 17
  • Voting to close, because there's way too much code. Anyhow, one advise: Document which data are shared and which mutex must be locked to access those data. Then, audit your code that this rule is not broken anywhere. – Ulrich Eckhardt Feb 27 '18 at 22:00
  • @UlrichEckhardt Different standards :) – sehe Feb 27 '18 at 22:11

1 Answers1

4

Wow that's lengthy.

Your most conspicuous error is here:

if (mSamplesQueue.front().size() > 256)
    return true; // <-- mSamplesQueue.front() has undefined size

You should check that mSamplesQueue.empty() is not true, otherwise using front() is UB.

Instead of

bool Server::samplesAvailable() {
    std::lock_guard<std::recursive_mutex> lock(mServerMutex);

    if (mSamplesQueue.front().size() > 256)
        return true;
    return false;
}

You could simply write

bool Server::samplesAvailable() {
    std::lock_guard<std::recursive_mutex> lock(mServerMutex);
    return mSamplesQueue.size() && mSamplesQueue.front().size()>256;
}

Reviewing:

  • there's UB in

    wSamples.push_back(static_cast<Int16>(mData[i] << 8 | mData[i]));
    

    you needed parentheses, and on many compilers char is signed,which leads to UB with left-shift.

  • never use using-directives in header files. Especially not using namespace std; (Why is "using namespace std" considered bad practice?)

  • there's a quite bit of unnecessary buffer copying, why not just reinterpret cast the char[] buffer on platforms with the appropriate endianness, and/or why not std::move in pushSamples():

    mSamplesQueue.push_back(std::move(iSamples));
    
  • using class members directly is a code smell (Law of Demeter). In particular, it's a sure sign you're mixing levels of abstraction when you write:

    {
        std::lock_guard<std::recursive_mutex> lock(mServerPtr->mServerMutex);
        mServerPtr->pushSamples(std::move(wSamples));
    }
    

    Especially since you already have

    void Server::pushSamples(std::vector<sf::Int16> iSamples) {
        std::lock_guard<std::recursive_mutex> lock(mServerMutex);
        mSamplesQueue.push_back(std::move(iSamples));
    }
    

    This means that at this time you require a recursive mutex, because otherwise you'd have a deadlock.

  • Session::mSampleBufferQueue is unused

  • The signature of getNextSamples is wrong. You will never see any effect because the parameter is passed by value. Either declare it:

    void getNextSamples(std::vector<sf::Int16>& oSamples);
    

    or

    std::vector<sf::Int16> getNextSamples();
    
  • consider making methods like samplesAvailable() const. To do this, you'll need to mark the mutex mutable (Always declare std::mutex as mutable in C++11?)

Conceptual Problems

Conceptually, there's a problem with the accepting of concurrent clients. You will end up playing streams randomly interleaved if that actually happens.

Fixed Up Version

  • main.cpp

    //#include "AudioStream.h"
    #include "TcpAsyncServer.h"
    #include "TcpAsyncSession.h"
    #include <thread>
    
    struct AudioStream {
        enum Status { Playing, Buffering };
        Status getStatus() const { return Buffering; }
        void load(sf::SoundBuffer const& buf) {
            // 
            std::cout << __PRETTY_FUNCTION__ 
                << " rate=" << buf.getSampleRate() 
                << " channels=" << buf.getChannelCount() 
                << " duration=" << buf.getDuration().asSeconds() << "s "
                << " samples=" << buf.getSampleCount()
                << "\n";
        }
        void play() {}
    };
    
    int main() {
        boost::asio::io_service ioService;
        std::deque<std::vector<sf::Int16> > mSamplesQueue;
        sf::SoundBuffer mSoundBuffer;
        AudioStream mAudioStream;
    
        std::cout << "Starting server..." << std::endl;
        Server mServer(ioService, PORT); // start async accept loop
        std::thread serviceThread([&]() { ioService.run(); });
    
        std::cout << "Starting reception loop..." << std::endl;
        for (;;) {
            {
                std::lock_guard<std::recursive_mutex> lock(mServer.mServerMutex);
    
                // Look for new samples
                if (mServer.samplesAvailable()) {
                    std::cout << "Samples available..." << std::endl;
                    std::vector<sf::Int16> wSamples;
                    mServer.getNextSamples(wSamples);
                    mSamplesQueue.push_back(wSamples);
                }
            }
    
            // Loading and playing samples
            if ((mAudioStream.getStatus() != AudioStream::Playing) && !mSamplesQueue.empty()) {
                std::cout << "Loading and playing audio stream..." << std::endl;
                if (mSoundBuffer.loadFromSamples(reinterpret_cast<sf::Int16 *>(mSamplesQueue.front().data()),
                                                 mSamplesQueue.front().size(), 2, 48000)) {
                    std::cout << "SoundBuffer loaded successfully..." << std::endl;
                    mAudioStream.load(mSoundBuffer);
                    mAudioStream.play();
                    mSamplesQueue.pop_front();
                } else
                    std::cout << "SoundBuffer failed to load..." << std::endl;
            }
    
            // Give it some room to play the sound
            while (mAudioStream.getStatus() == AudioStream::Playing) {
                sleep(sf::milliseconds(50));
            }
        }
        serviceThread.join();
    }
    
  • TcpAsyncServer.h

    #include <SFML/Audio.hpp>
    #include <boost/asio.hpp>
    #include <deque>
    #include <iostream>
    #include <mutex>
    #include <vector>
    
    #define PORT 2113
    #define SAMPLE_BUFFER 5120000
    
    class Server {
        using tcp = boost::asio::ip::tcp;
    
      public:
        // Ctor
        Server(boost::asio::io_service &iService, short iPort);
    
        // Methods
        bool samplesAvailable() const;
        void getNextSamples(std::vector<sf::Int16>& oSamples);
        void pushSamples(std::vector<sf::Int16> iSamples);
    
        // Variables
        mutable std::recursive_mutex mServerMutex;
    
      private:
        // Methods
        void doAccept();
    
        // Variables
        tcp::acceptor mAcceptor;
        tcp::socket mSocket;
        std::deque<std::vector<sf::Int16> > mSamplesQueue;
    };
    
  • TcpAsyncSession.h

    #include <boost/asio.hpp>
    
    #define BUFFER_SIZE 10240000
    
    class Server;
    
    class Session : public std::enable_shared_from_this<Session> {
        using tcp = boost::asio::ip::tcp;
    
      public:
        // Ctor
        Session(tcp::socket &&iSocket, Server *iServerPtr);
    
        // Methods
        void start();
    
      private:
        // Methods
        void doRead();
    
        // Variables
        tcp::socket mSocket;
        uint8_t mData[BUFFER_SIZE];
        Server *mServerPtr;
    };
    
  • TcpAsyncServer.cpp

    #include "TcpAsyncServer.h"
    #include "TcpAsyncSession.h"
    
    Server::Server(boost::asio::io_service &service, short port) : mAcceptor(service, tcp::endpoint(tcp::v4(), port)), mSocket(service) {
        mAcceptor.set_option(tcp::acceptor::reuse_address());
        doAccept();
    }
    
    void Server::doAccept() {
        mAcceptor.async_accept(mSocket, [this](boost::system::error_code error) {
            if (!error)
                std::make_shared<Session>(std::move(mSocket), this)->start();
            doAccept();
        });
    }
    
    bool Server::samplesAvailable() const {
        std::lock_guard<std::recursive_mutex> lock(mServerMutex);
        return mSamplesQueue.size() && mSamplesQueue.front().size()>256;
    }
    
    void Server::getNextSamples(std::vector<sf::Int16>& oSamples) {
        std::lock_guard<std::recursive_mutex> lock(mServerMutex);
        oSamples = std::move(mSamplesQueue.front());
        mSamplesQueue.pop_front();
    }
    
    void Server::pushSamples(std::vector<sf::Int16> iSamples) {
        std::lock_guard<std::recursive_mutex> lock(mServerMutex);
        mSamplesQueue.push_back(std::move(iSamples));
    }
    
  • TcpAsyncSession.cpp

    #include "TcpAsyncServer.h"
    #include "TcpAsyncSession.h"
    
    //#include <SFML/Audio.hpp>
    #include <iostream>
    
    Session::Session(tcp::socket &&iSocket, Server *iServerPtr) : mSocket(std::move(iSocket)), mServerPtr(iServerPtr) {}
    
    void Session::start() { doRead(); }
    
    void Session::doRead() {
    
        std::shared_ptr<Session> self(shared_from_this());
    
        mSocket.async_read_some(
                boost::asio::buffer(mData, BUFFER_SIZE),
                [this, self](boost::system::error_code error, size_t iBytesReceived) {
                    if (error)
                        return;
                    std::cout << "Receiving " << iBytesReceived << " bytes..." << std::endl;
                    std::vector<sf::Int16> wSamples;
    
                    for (unsigned int i = 0; i < iBytesReceived; i += 2) {
                        wSamples.push_back(static_cast<sf::Int16>((mData[i] << 8) | mData[i]));
                    }
    
                    mServerPtr->pushSamples(std::move(wSamples));
    
                    doRead();
            });
    }
    

Demo Run

Feeding it a 138M mp3:

Starting server...
Starting reception loop...
Receiving 8192 bytes...
Samples available...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=0.042666s  samples=4096
Receiving 562829 bytes...
Samples available...
Receiving 745525 bytes...
Loading and playing audio stream...
An internal OpenAL call failed in SoundBuffer.cpp(265).
Expression:
   alBufferData(m_buffer, format, &m_samples[0], size, sampleRate)
Error description:
   AL_INVALID_VALUE
   A numeric argument is out of range.

SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=2.93141s  samples=281415
Samples available...
Loading and playing audio stream...
Receiving 2815769 bytes...
An internal OpenAL call failed in SoundBuffer.cpp(265).
Expression:
   alBufferData(m_buffer, format, &m_samples[0], size, sampleRate)
Error description:
   AL_INVALID_VALUE
   A numeric argument is out of range.

SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=3.88295s  samples=372763
Samples available...
Receiving 4978211 bytes...
Loading and playing audio stream...
An internal OpenAL call failed in SoundBuffer.cpp(265).
Expression:
   alBufferData(m_buffer, format, &m_samples[0], size, sampleRate)
Error description:
   AL_INVALID_VALUE
   A numeric argument is out of range.

SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=14.6655s  samples=1407885
Samples available...
Receiving 5632954 bytes...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=25.9282s  samples=2489106
Samples available...
Receiving 5893470 bytes...
Loading and playing audio stream...
An internal OpenAL call failed in SoundBuffer.cpp(265).
Expression:
   alBufferData(m_buffer, format, &m_samples[0], size, sampleRate)
Error description:
   AL_INVALID_VALUE
   A numeric argument is out of range.

SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=29.3383s  samples=2816477
Samples available...
Receiving 5895401 bytes...
Loading and playing audio stream...
An internal OpenAL call failed in SoundBuffer.cpp(265).
Expression:
   alBufferData(m_buffer, format, &m_samples[0], size, sampleRate)
Error description:
   AL_INVALID_VALUE
   A numeric argument is out of range.

SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=30.6952s  samples=2946735
Samples available...
Receiving 5894091 bytes...
Loading and playing audio stream...
An internal OpenAL call failed in SoundBuffer.cpp(265).
Expression:
   alBufferData(m_buffer, format, &m_samples[0], size, sampleRate)
Error description:
   AL_INVALID_VALUE
   A numeric argument is out of range.

SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=30.7052s  samples=2947701
Samples available...
Receiving 5894197 bytes...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=30.6984s  samples=2947046
Samples available...
Receiving 5894303 bytes...
Loading and playing audio stream...
An internal OpenAL call failed in SoundBuffer.cpp(265).
Expression:
   alBufferData(m_buffer, format, &m_samples[0], size, sampleRate)
Error description:
   AL_INVALID_VALUE
   A numeric argument is out of range.

SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=30.6989s  samples=2947099
Samples available...
Receiving 5894144 bytes...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=30.6995s  samples=2947152
Samples available...
Receiving 5896192 bytes...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=30.6987s  samples=2947072
Samples available...
Receiving 5961675 bytes...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=30.7093s  samples=2948096
Samples available...
Receiving 5961728 bytes...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=31.0504s  samples=2980838
Samples available...
Receiving 5960615 bytes...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=31.0507s  samples=2980864
Samples available...
Receiving 5960793 bytes...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=31.0449s  samples=2980308
Samples available...
Receiving 5960668 bytes...
Loading and playing audio stream...
An internal OpenAL call failed in SoundBuffer.cpp(265).
Expression:
   alBufferData(m_buffer, format, &m_samples[0], size, sampleRate)
Error description:
   AL_INVALID_VALUE
   A numeric argument is out of range.

SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=31.0458s  samples=2980397
Samples available...
Receiving 5960740 bytes...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=31.0451s  samples=2980334
Samples available...
Receiving 5960668 bytes...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=31.0455s  samples=2980370
Samples available...
Receiving 5960740 bytes...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=31.0451s  samples=2980334
Samples available...
Receiving 5960668 bytes...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=31.0455s  samples=2980370
Samples available...
Receiving 5960740 bytes...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=31.0451s  samples=2980334
Samples available...
Receiving 5960668 bytes...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=31.0455s  samples=2980370
Samples available...
Receiving 5960740 bytes...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=31.0451s  samples=2980334
Samples available...
Receiving 5960668 bytes...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=31.0455s  samples=2980370
Samples available...
Receiving 5960740 bytes...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=31.0451s  samples=2980334
Samples available...
Receiving 4770135 bytes...
Loading and playing audio stream...
SoundBuffer loaded successfully...
void AudioStream::load(const sf::SoundBuffer &) rate=48000 channels=2 duration=31.0455s  samples=2980370
Samples available...
Loading and playing audio stream...
SoundBuffer loaded successfully...

Apart from the fact that my mock AudioStream doesn't initialize the audio-library properly, that looks fine to me.

Note that I increased the buffer sizes a lot so the log would not be too large.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    [Added](https://stackoverflow.com/posts/49018787/revisions) one more review hint about const-correctness. – sehe Feb 27 '18 at 22:21