base.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef ALC_BACKENDS_BASE_H
  2. #define ALC_BACKENDS_BASE_H
  3. #include <chrono>
  4. #include <memory>
  5. #include <mutex>
  6. #include <string>
  7. #include "AL/alc.h"
  8. #include "alcmain.h"
  9. #include "albyte.h"
  10. struct ClockLatency {
  11. std::chrono::nanoseconds ClockTime;
  12. std::chrono::nanoseconds Latency;
  13. };
  14. /* Helper to get the current clock time from the device's ClockBase, and
  15. * SamplesDone converted from the sample rate.
  16. */
  17. inline std::chrono::nanoseconds GetDeviceClockTime(ALCdevice *device)
  18. {
  19. using std::chrono::seconds;
  20. using std::chrono::nanoseconds;
  21. auto ns = nanoseconds{seconds{device->SamplesDone}} / device->Frequency;
  22. return device->ClockBase + ns;
  23. }
  24. ClockLatency GetClockLatency(ALCdevice *device);
  25. struct BackendBase {
  26. virtual void open(const ALCchar *name) = 0;
  27. virtual bool reset();
  28. virtual bool start() = 0;
  29. virtual void stop() = 0;
  30. virtual ALCenum captureSamples(al::byte *buffer, ALCuint samples);
  31. virtual ALCuint availableSamples();
  32. virtual ClockLatency getClockLatency();
  33. virtual void lock() { mMutex.lock(); }
  34. virtual void unlock() { mMutex.unlock(); }
  35. ALCdevice *mDevice;
  36. std::recursive_mutex mMutex;
  37. BackendBase(ALCdevice *device) noexcept;
  38. virtual ~BackendBase();
  39. };
  40. using BackendPtr = std::unique_ptr<BackendBase>;
  41. using BackendUniqueLock = std::unique_lock<BackendBase>;
  42. using BackendLockGuard = std::lock_guard<BackendBase>;
  43. enum class BackendType {
  44. Playback,
  45. Capture
  46. };
  47. enum class DevProbe {
  48. Playback,
  49. Capture
  50. };
  51. struct BackendFactory {
  52. virtual bool init() = 0;
  53. virtual bool querySupport(BackendType type) = 0;
  54. virtual void probe(DevProbe type, std::string *outnames) = 0;
  55. virtual BackendPtr createBackend(ALCdevice *device, BackendType type) = 0;
  56. protected:
  57. virtual ~BackendFactory() = default;
  58. };
  59. #endif /* ALC_BACKENDS_BASE_H */