This repository was archived by the owner on May 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrx_start.c
More file actions
191 lines (159 loc) · 4.34 KB
/
Copy pathrx_start.c
File metadata and controls
191 lines (159 loc) · 4.34 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include <netdb.h>
#include <string.h>
#include <alsa/asoundlib.h>
#include <opus/opus.h>
#include <ortp/ortp.h>
#include <stdlib.h>
#include <signal.h> //Recupération et envoi de signaux entre processus
#include "defaults.h"
#include "device.h"
#include "notice.h"
#include "sched.h"
#include "multi.h"
#include "rx_start.h"
extern unsigned int verbose;
static void timestamp_jump(RtpSession *session, ...)
{
if (verbose > 1)
fputc('|', stderr);
rtp_session_resync(session);
}
static int play_one_frame(void *packet,
size_t len,
OpusDecoder *decoder,
snd_pcm_t *snd,
const unsigned int channels)
{
int r;
float *pcm;
snd_pcm_sframes_t f, samples = 1920;
pcm = alloca(sizeof(float) * samples * channels);
if (packet == NULL) {
r = opus_decode_float(decoder, NULL, 0, pcm, samples, 1);
} else {
r = opus_decode_float(decoder, packet, len, pcm, samples, 0);
}
if (r < 0) {
fprintf(stderr, "opus_decode: %s\n", opus_strerror(r));
return -1;
}
f = snd_pcm_writei(snd, pcm, r);
if (f < 0) {
f = snd_pcm_recover(snd, f, 0);
if (f < 0) {
aerror("snd_pcm_writei", f);
return -1;
}
return 0;
}
if (f < r)
fprintf(stderr, "Short write %ld\n", f);
return r;
}
static int run_rx(RtpSession *session,
OpusDecoder *decoder,
snd_pcm_t *snd,
const unsigned int channels,
const unsigned int rate)
{
int ts = 0;
for (;;) {
int r, have_more;
char buf[32768];
void *packet;
r = rtp_session_recv_with_ts(session, (uint8_t*)buf,
sizeof(buf), ts, &have_more);
assert(r >= 0);
assert(have_more == 0);
if (r == 0) {
packet = NULL;
if (verbose > 1)
fputc('#', stderr);
} else {
packet = buf;
if (verbose > 1)
fputc('.', stderr);
}
r = play_one_frame(packet, r, decoder, snd, channels);
if (r == -1)
return -1;
/* Follow the RFC, payload 0 has 8kHz reference rate */
ts += r * 8000 / rate;
}
}
static RtpSession* create_rtp_recv(const char *addr_desc, const int port,
unsigned int jitter)
{
RtpSession *session;
session = rtp_session_new(RTP_SESSION_RECVONLY);
rtp_session_set_scheduling_mode(session, FALSE);
rtp_session_set_blocking_mode(session, FALSE);
rtp_session_set_local_addr(session, addr_desc, port, -1);
rtp_session_set_connected_mode(session, FALSE);
rtp_session_enable_adaptive_jitter_compensation(session, TRUE);
rtp_session_set_jitter_compensation(session, jitter); /* ms */
rtp_session_set_time_jump_limit(session, jitter * 16); /* ms */
if (rtp_session_set_payload_type(session, 0) != 0)
abort();
if (rtp_session_signal_connect(session, "timestamp_jump",
timestamp_jump, 0) != 0)
{
abort();
}
return session;
}
int server_start_rx(Slot* slot) {
pid_t rxpid;
RtpSession *session;
OpusDecoder *decoder = slot->param.decoder;
snd_pcm_t *snd;
int r;
const char *device = slot->param.device,
*addr = slot->param.addr,
*pid = slot->param.pid;
unsigned int buffer = slot->param.buffer,
rate = slot->param.rate,
jitter = slot->param.jitter,
channels = slot->param.channels,
port = slot->param.port;
rxpid = fork(); //nouveau processus
slot->pid = rxpid;
slot->start_time = time(NULL);
if(rxpid == -1) {
/* Erreur */
}
else if(rxpid == 0) { //On est dans le processus fils
//Creation d'une session de lecture
session = create_rtp_recv(addr, port, jitter); //1 Session oRTP créée
assert(session != NULL);
r = snd_pcm_open(&snd, device, SND_PCM_STREAM_PLAYBACK, 0); //Ouverture d'une liaison carte son
if (r < 0) {
aerror("snd_pcm_open", r);
return -1;
}
if (set_alsa_hw(snd, rate, channels, buffer * 1000) == -1)
return -1;
if (set_alsa_sw(snd) == -1)
return -1;
if (pid)
go_daemon(pid); //Mise du recepteur en daemon si presence pid (Pere / fils ?)
go_realtime();
r = run_rx(session, decoder, snd, channels, rate); // boucle infinie de lecture
if (snd_pcm_close(snd) < 0)
abort(); //Si erreur de liaison carte son, fin de la boucle
rtp_session_destroy(session); //Fin de la session oRTP
//Fin de la session oRTP
}
return 1;
}
int server_stop_rx(Slot* slot) {
kill(slot->pid, SIGTERM);
slot->pid = 0;
slot->start_time = 0;
return 1;
}