summaryrefslogtreecommitdiff
path: root/src/device_extern.cpp
blob: cf586c15795bfe4fba3fb13581705831dc54e3ca (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <algorithm>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <dlfcn.h>

#include "device_extern.h"
#include "debug.h"


namespace audiere {

#ifndef RTLD_LOCAL
#define RTLD_LOCAL 0
#endif

#ifndef RTLD_NOW
#define RTLD_NOW 0
#endif

  ExternAudioDevice::ExternModule::ExternModule() :
    m_open(0), m_start(0),
    m_stop(0), m_write(0),
    m_close(0), m_handle(0)
  {
  }

  ExternAudioDevice::ExternModule::ExternModule(const ExternModule &other) :
    m_open(other.m_open), m_start(other.m_start),
    m_stop(other.m_stop), m_write(other.m_write),
    m_close(other.m_close), m_handle(other.m_handle)
  {
  }

  bool ExternAudioDevice::ExternModule::init(const char *name)
  {
    void* m_handle = dlopen(name, RTLD_LOCAL | RTLD_NOW);
    if (!m_handle)
      return false;

    m_open = (DeviceOpenProc)dlsym(m_handle, "AudioDeviceOpen");
    m_start = (DeviceStartProc)dlsym(m_handle, "AudioDeviceStart");
    m_stop = (DeviceStopProc)dlsym(m_handle, "AudioDeviceStop");
    m_write = (DeviceWriteProc)dlsym(m_handle, "AudioDeviceWrite");
    m_close = (DeviceCloseProc)dlsym(m_handle, "AudioDeviceClose");
    if (!m_open || !m_start || !m_stop || !m_write || !m_close) {
      dlclose(m_handle);
      m_handle = 0;
      return false;
    }

    return true;
  }

  void ExternAudioDevice::ExternModule::uninit()
  {
    if (!m_handle)
      return;

    m_open = 0;
    m_start = 0;
    m_stop = 0;
    m_write = 0;
    m_close = 0;
    dlclose(m_handle);
    m_handle = 0;
  }

  ExternAudioDevice*
  ExternAudioDevice::create(const ParameterList& parameters) {
    int ret;

    ExternModule module;

    if (!module.init("./libExtAudioDevice.so") &&
	!module.init("libExtAudioDevice.so"))
      return 0;

    ret = module.open(44100, 2, 16, 1);
    if (ret != 0)
    {
      ADR_LOG("Unable to open audio device\n");
      module.uninit();
      return 0;
    }

    ret = module.start();
    if (ret != 0)
    {
      ADR_LOG("Unable to set audio device to play state\n");
      module.close();
      module.uninit();
      return 0;
    }

    return new ExternAudioDevice(module, 44100, 4096);
  }

  ExternAudioDevice::ExternAudioDevice(ExternModule module,
				       int rate,
				       int buffer_size)
    : MixerDevice(rate), m_module(module), m_rate(rate)
  {
    m_buffer_size = buffer_size;
    m_buffer = new char [buffer_size];
  }


  ExternAudioDevice::~ExternAudioDevice() {
    ADR_GUARD("ExternAudioDevice::~ExternAudioDevice");

    m_module.stop();
    m_module.close();
    m_module.uninit();
    delete [] m_buffer;
  }


  void ADR_CALL
  ExternAudioDevice::update() {
    int           sample_len;
    size_t        sample_left;
    char*         sample_buf;

    sample_buf = m_buffer;
    sample_len = m_buffer_size / 4;

    sample_left = read(sample_len, sample_buf);
    while (sample_left  > 0) {
      if (m_module.write(sample_buf, sample_left * 4) < 0) {
	AI_Sleep(2);
      } else {
	sample_left = 0;
      }
    }
  }


  const char* ADR_CALL
  ExternAudioDevice::getName() {
    return "extern";
  }

}