-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtesthappy-mpd.cpp
131 lines (96 loc) · 3.2 KB
/
testhappy-mpd.cpp
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
// (C) 2022-2023 by folkert van heusden <[email protected]>, CC0 license
#include <signal.h>
#include <libmpd/libmpd.h>
#include "sip.h"
typedef struct {
// last (DTMF) key pressed (or 255)
uint8_t prev_key;
} mpd_sessions_t;
// invoked when a new session has started
// one can set 'session->private_data' to point to internal
// data of the callback. you need to free it yourself in
// e.g. the end_session callback.
bool cb_new_session(sip_session_t *const session, const std::string & from)
{
printf("cb_new_session, call-id: %s, caller: %s\n", session->call_id.c_str(), from.c_str());
session->private_data = new mpd_sessions_t;
mpd_sessions_t *p = reinterpret_cast<mpd_sessions_t *>(session->private_data);
p->prev_key = 255;
return true;
}
// no audio, just dtmf
// invoked when the peer produces audio and which is then
// received by us
bool cb_recv(const short *const samples, const size_t n_samples, sip_session_t *const session)
{
return true;
}
// invoked when the library wants to send audio to
// the peer
bool cb_send(short **const samples, size_t *const n_samples, sip_session_t *const session)
{
return true;
}
// called when we receive a 'BYE' from the peer (and
// the session thus ends)
void cb_end_session(sip_session_t *const session)
{
printf("cb_end_session, call-id: %s\n", session->call_id.c_str());
mpd_sessions_t *p = reinterpret_cast<mpd_sessions_t *>(session->private_data);
delete p;
}
bool cb_dtmf(const uint8_t dtmf_code, const bool is_end, const uint8_t volume, sip_session_t *const session)
{
printf("DTMF pressed: %d\n", dtmf_code);
mpd_sessions_t *p = reinterpret_cast<mpd_sessions_t *>(session->private_data);
MpdObj *mpd = reinterpret_cast<MpdObj *>(session->global_private_data);
int rc = mpd_connect(mpd);
if (rc != MPD_OK)
fprintf(stderr, "Cannot connect to MPD server: %d\n", rc);
if (is_end && dtmf_code != p->prev_key) {
int rc = -1;
if (dtmf_code == 4) { // previous
if ((rc = mpd_player_prev(mpd)) != MPD_OK)
printf("mpd_player_prev failed: %d\n", rc);
}
else if (dtmf_code == 5) { // pause / unpause
int state = mpd_player_get_state(mpd);
if (state == MPD_STATUS_STATE_PAUSE || state == MPD_STATUS_STATE_STOP)
rc = mpd_player_play(mpd);
else if (state == MPD_STATUS_STATE_PLAY)
rc = mpd_player_pause(mpd);
else
printf("Unknown MPD state: %d\n", state);
if (rc != MPD_OK)
printf("mpd_player_play/pause failed: %d\n", rc);
}
else if (dtmf_code == 6) { // next
if ((rc = mpd_player_next(mpd)) != MPD_OK)
printf("mpd_player_next failed: %d\n", rc);
}
else {
printf("Ignoring DTMF code %d\n", dtmf_code);
}
p->prev_key = dtmf_code;
}
mpd_disconnect(mpd);
if (!is_end)
p->prev_key = 255;
return true;
}
void sigh(int sig)
{
}
int main(int argc, char *argv[])
{
signal(SIGINT, sigh);
setlog("/tmp/testhappy.log", debug, debug);
MpdObj *mpd = mpd_new(const_cast<char *>("spacesound.vm.nurd.space"), 6600, const_cast<char *>(""));
if (mpd == nullptr) {
fprintf(stderr, "Cannot setup MPD session\n");
return 1;
}
sip s("10.208.11.13", "3737", "1234", { }, 0, 60, 44100, cb_new_session, cb_recv, cb_send, cb_end_session, cb_dtmf, mpd);
pause();
return 0;
}