-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeaker_receiver.c
174 lines (130 loc) · 4.44 KB
/
speaker_receiver.c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
This file is part of castspeaker
Copyright (C) 2022-2028 zwcway
This program 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.
This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include <pthread.h>
#include <common/connection.h>
#include <common/event/select.h>
#include <common/package/control.h>
#include "common/speaker_struct.h"
#include "speaker.h"
#include "common/error.h"
#include "speaker_receiver.h"
#include "speaker_multicast.h"
static uint16_t data_port = DEFAULT_RECEIVER_PORT;
static addr_t listen_ip = {AF_INET};
static output_send_fn output_fn = NULL;
static set_audio_format_fn format_fn = NULL;
static uint32_t ctrl_sample_chunk;
static audio_rate_t ctrl_sample_rate;
static audio_bits_t ctrl_sample_bits;
static pcm_header_t pcm_header = {0};
static connection_t conn = DEFAULT_CONNECTION_UDP_INIT;
LOG_TAG_DECLR("speaker");
void command(socket_t fd, const void *hd) {
control_package_t ctl;
CONTROL_PACKAGE_DECODE(&ctl, hd);
switch (ctl.cmd) {
case SPCMD_CHUNK:
ctrl_sample_chunk = ctl.chunk.size;
LOGI("command: chunk, %d", ctrl_sample_chunk);
break;
case SPCMD_SAMPLE:
ctrl_sample_bits = ctl.sample.bits;
ctrl_sample_rate = ctl.sample.rate;
LOGI("command: sample, %d/%d/%s", rate_name(ctrl_sample_rate), bits_name(ctrl_sample_bits),
channel_name(ctl.sample.channel));
if (NULL != format_fn) format_fn(ctrl_sample_rate, ctrl_sample_bits);
break;
case SPCMD_UNKNOWN_SP:
memset(&server_addr, 0, sizeof(addr_t));
break;
default:
LOGW("unknown command: %d", ctl.cmd);
break;
}
}
void pcm_receive() {
}
socket_t create_receiver_socket() {
struct sockaddr_storage group_addr = {0};
socket_t sockfd = socket(listen_ip.type, SOCK_DGRAM, IPPROTO_UDP);
if (sockfd < 0) {
LOGF("create socket error: %m");
sexit(EERR_SOCKET);
}
set_sockaddr(&group_addr, &listen_ip, data_port);
LOGI("Listen on %s", addr_ntop(&listen_ip));
if ((bind(sockfd, (struct sockaddr *) &group_addr, sizeof(group_addr))) < 0) {
LOGF("detect bind error: %m");
closesocket(sockfd);
sexit(EERR_SOCKET);
}
return sockfd;
}
int sp_receiver_read(connection_t *c, const struct sockaddr_storage *src, socklen_t src_len, const void *package,
uint32_t len) {
uint8_t *samples = (uint8_t *) package + PCM_HEADER_SIZE;
if (len == CONTROL_PACKAGE_SIZE) {
command(c->read_fd, package);
return 0;
}
PCM_HEADER_DECODE(&pcm_header, package);
if (len - PCM_HEADER_SIZE != pcm_header.len) {
LOGD("receiver recvfrom fail: %d(need %d)", len, pcm_header.len);
return -1;
}
LOGT("rate: %08d, bit: %03d, len: %05d", rate_name(pcm_header.sample.rate), bits_name(pcm_header.sample.bits),
pcm_header.len);
if (output_fn && output_fn(&pcm_header, samples) != 0)
return -1;
uint8_t time_sync = 1;
sendto(c->read_fd, &time_sync, sizeof(time_sync), 0, (struct sockaddr *) src, src_len);
return 0;
}
int receiver_stop() {
LOGD("exit receiver thread");
event_del(&conn);
shutdown(conn.read_fd, 0);
closesocket(conn.read_fd);
return 0;
}
int receiver_start() {
LOGT("receiver start");
conn.read_fd = create_receiver_socket();
event_add(&conn);
return 0;
}
int receiver_init(const struct receiver_config *cfg) {
LOGT("receiver init");
if (cfg == NULL || 0 == cfg->family) {
LOGF("family can not empty");
sexit(EERR_ARG);
}
listen_ip.type = cfg->family;
if (cfg->output_cb != NULL) output_fn = cfg->output_cb;
if (cfg->format_cb != NULL) format_fn = cfg->format_cb;
if (cfg->port) data_port = cfg->port;
if (cfg->ip) listen_ip = *cfg->ip;
else memset(&listen_ip.ipv6, 0, sizeof(struct in6_addr));
if (!data_port) data_port = DEFAULT_RECEIVER_PORT;
conn.family = cfg->family;
conn.read_cb = sp_receiver_read;
receiver_start();
return 0;
}
void receiver_deinit() {
LOGT("receiver deinit");
receiver_stop();
}