summaryrefslogtreecommitdiff
path: root/audio.c
blob: bb5ef2336866658a47e050ad3be1f4ac74957133 (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
/*
 * Copyright (C) 2024 Mikhail Burakov. This file is part of receiver.
 *
 * receiver is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * receiver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with receiver.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "audio.h"

#include <alsa/asoundlib.h>
#include <errno.h>
#include <stdatomic.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <threads.h>

#include "atomic_queue.h"
#include "toolbox/utils.h"

struct AudioContext {
  const char* device;
  atomic_bool running;
  struct AtomicQueue queue;
  thrd_t thread;
};

static int AudioContextThreadProc(void* arg) {
  struct AudioContext* context = arg;

  snd_pcm_t* pcm = NULL;
  int err = snd_pcm_open(&pcm, context->device, SND_PCM_STREAM_PLAYBACK, 0);
  if (err) {
    LOG("Failed to open pcm (%s)", snd_strerror(err));
    atomic_store_explicit(&context->running, 0, memory_order_relaxed);
    return 0;
  }

  // TODO(mburakov): Read audio configuration from the server.
  err = snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE,
                           SND_PCM_ACCESS_RW_INTERLEAVED, 2, 48000, 1, 10000);
  if (err) {
    LOG("Failed to set pcm params (%s)", snd_strerror(err));
    atomic_store_explicit(&context->running, 0, memory_order_relaxed);
    goto rollback_pcm;
  }

  while (atomic_load_explicit(&context->running, memory_order_relaxed)) {
    // TODO(mburakov): Frame size depends on dynamic audio configuration.
    static const unsigned frame_size = sizeof(int16_t) * 2;
    uint8_t buffer[480 * frame_size];

    size_t size = AtomicQueueRead(&context->queue, buffer, sizeof(buffer));
    if (size < sizeof(buffer)) {
      // LOG("Audio queue underflow!");
      memset(buffer + size, 0, sizeof(buffer) - size);
    }

    for (snd_pcm_uframes_t offset = 0; offset < sizeof(buffer) / frame_size;) {
      snd_pcm_sframes_t nframes =
          snd_pcm_writei(pcm, buffer + offset * frame_size,
                         sizeof(buffer) / frame_size - offset);
      if (nframes < 0) {
        LOG("Failed to write pcm (%s)", snd_strerror((int)nframes));
        atomic_store_explicit(&context->running, 0, memory_order_relaxed);
        goto rollback_pcm;
      }
      offset += (snd_pcm_uframes_t)nframes;
    }
  }

rollback_pcm:
  snd_pcm_close(pcm);
  return 0;
}

struct AudioContext* AudioContextCreate(const char* device) {
  struct AudioContext* audio_context = malloc(sizeof(struct AudioContext));
  if (!audio_context) {
    LOG("Failed to allocate context (%s)", strerror(errno));
    return NULL;
  }

  audio_context->device = device;
  atomic_init(&audio_context->running, 1);
  if (!AtomicQueueCreate(&audio_context->queue, 4800 * sizeof(int16_t) * 2)) {
    LOG("Failed to create queue (%s)", strerror(errno));
    goto rollback_context;
  }

  if (thrd_create(&audio_context->thread, AudioContextThreadProc,
                  audio_context) != thrd_success) {
    LOG("Failed to create thread (%s)", strerror(errno));
    goto rollback_queue;
  }
  return audio_context;

rollback_queue:
  AtomicQueueDestroy(&audio_context->queue);
rollback_context:
  free(audio_context);
  return NULL;
}

bool AudioContextDecode(struct AudioContext* audio_context, const void* buffer,
                        size_t size) {
  if (!atomic_load_explicit(&audio_context->running, memory_order_relaxed)) {
    LOG("Audio thread was stopped early!");
    return false;
  }
  if (AtomicQueueWrite(&audio_context->queue, buffer, size) < size)
    LOG("Audio queue overflow!");
  return true;
}

void AudioContextDestroy(struct AudioContext* audio_context) {
  atomic_store_explicit(&audio_context->running, 0, memory_order_relaxed);
  thrd_join(audio_context->thread, NULL);
  AtomicQueueDestroy(&audio_context->queue);
  free(audio_context);
}