summaryrefslogtreecommitdiff
path: root/Audio/AudioInputSource.hpp
blob: 220e130964828081d3412cb04afbe49e994abe81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// -*- Mode: C++ -*-
#ifndef AUDIO_INPUT_SOURCE_HPP__
#define AUDIO_INPUT_SOURCE_HPP__

#include <QObject>
#include <QMutex>
#include <QMutexLocker>
#include <QString>

#include "Audio/AudioDevice.hpp"
#include "Audio/AudioStreamDescriptor.hpp"

class QAudioDeviceInfo;

class AudioInputSource
  : public QObject
{
  Q_OBJECT

public:
  explicit AudioInputSource (QObject * parent = nullptr)
    : QObject {parent}
  {
  }

  ~AudioInputSource () override = default;

  // Return a consistent snapshot that is safe to read from another thread.
  AudioStreamDescriptor streamDescriptor () const
  {
    QMutexLocker locker {&stream_descriptor_mutex_};
    return stream_descriptor_;
  }

  Q_SLOT virtual void start (QAudioDeviceInfo const&, int framesPerBuffer,
                             AudioDevice * sink, unsigned downSampleFactor,
                             AudioDevice::Channel = AudioDevice::Mono) = 0;
  Q_SLOT virtual void suspend () = 0;
  Q_SLOT virtual void resume () = 0;
  Q_SLOT virtual void stop () = 0;
  Q_SLOT virtual void reset (bool reportDroppedFrames) = 0;

  Q_SIGNAL void error (QString message) const;
  Q_SIGNAL void status (QString message) const;
  Q_SIGNAL void streamDescriptorChanged (AudioStreamDescriptor descriptor) const;

protected:
  void setStreamDescriptor (AudioStreamDescriptor descriptor)
  {
    {
      QMutexLocker locker {&stream_descriptor_mutex_};
      if (descriptor == stream_descriptor_)
        {
          return;
        }
      stream_descriptor_ = descriptor;
    }
    Q_EMIT streamDescriptorChanged (descriptor);
  }

  void clearStreamDescriptor ()
  {
    setStreamDescriptor ({});
  }

private:
  mutable QMutex stream_descriptor_mutex_;
  AudioStreamDescriptor stream_descriptor_;
};

#endif