-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSRBeep.cpp
More file actions
395 lines (355 loc) · 9.86 KB
/
Copy pathSRBeep.cpp
File metadata and controls
395 lines (355 loc) · 9.86 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/***********************************
A Docile Sloth adocilesloth@gmail.com
************************************/
#include <obs-module.h>
#include <obs-frontend-api/obs-frontend-api.h>
#include <thread>
#include <atomic>
#include <sstream>
#include <mutex>
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswresample/swresample.h"
#include "SDL.h"
#include "SDL_thread.h"
};
std::mutex audioMutex;
std::thread st_stt_Thread, st_sto_Thread, rc_stt_Thread, rc_sto_Thread, bf_stt_Thread, bf_sto_Thread, ps_stt_Thread, ps_sto_Thread;
#define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio
static Uint8 *audio_chunk;
static Uint32 audio_len;
static Uint8 *audio_pos;
OBS_DECLARE_MODULE()
#ifdef _WIN32
#include <windows.h>
void psleep(unsigned milliseconds)
{
Sleep(milliseconds);
}
#else
#include <unistd.h>
void psleep(unsigned milliseconds)
{
usleep(milliseconds * 1000); // takes microseconds
}
#endif
void obs_module_unload(void)
{
if(st_stt_Thread.joinable())
{
st_stt_Thread.join();
}
if(st_sto_Thread.joinable())
{
st_sto_Thread.join();
}
if(rc_stt_Thread.joinable())
{
rc_stt_Thread.join();
}
if(rc_sto_Thread.joinable())
{
rc_sto_Thread.join();
}
if(bf_stt_Thread.joinable())
{
bf_stt_Thread.join();
}
if(bf_sto_Thread.joinable())
{
bf_sto_Thread.join();
}
if(ps_stt_Thread.joinable())
{
ps_stt_Thread.join();
}
if(ps_sto_Thread.joinable())
{
ps_sto_Thread.join();
}
return;
}
const char *obs_module_author(void)
{
return "A Docile Sloth";
}
const char *obs_module_name(void)
{
return "Stream/Recording Start/Stop Beeps";
}
const char *obs_module_description(void)
{
return "Adds audio sound when streaming/recording/buffer starts/stops or when recording is paused/unpaused.";
}
void fill_audio(void *udata, Uint8 *stream, int len)
{
/*****************************************************************
From simplest_ffmpeg_audio_player by leixiaohua1020
Download at https://sourceforge.net/projects/simplestffmpegplayer/
*****************************************************************/
//SDL 2.0
SDL_memset(stream, 0, len);
if(audio_len == 0) /* Only play if we have data left */
return;
len = ((unsigned int)len > audio_len ? audio_len : len); /* Mix as much data as possible */
SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);
audio_pos += len;
audio_len -= len;
}
void play_clip(const char *filepath)
{
//fix problems with audio_len being assigned a value
static Uint32 fixer;
audio_len = fixer;
//carry on
/*****************************************************************
Adapted from simplest_ffmpeg_audio_player by leixiaohua1020
Download at https://sourceforge.net/projects/simplestffmpegplayer/
*****************************************************************/
AVFormatContext *stream_start = NULL;
const AVCodec *cdc = nullptr;
int audioStreamIndex = -1;
if(avformat_open_input(&stream_start, filepath, NULL, NULL) != 0)
{
blog(LOG_WARNING, "SRBeep: play_clip: Failed to open file");
blog(LOG_WARNING, filepath);
return;
}
if(avformat_find_stream_info(stream_start, NULL) < 0)
{
avformat_close_input(&stream_start);
blog(LOG_WARNING, "SRBeep: play_clip: Failed to find stream_start_sound.mp3's stream info");
return;
}
audioStreamIndex = av_find_best_stream(stream_start, AVMEDIA_TYPE_AUDIO, -1, -1, &cdc, 0);
if(audioStreamIndex < 0)
{
avformat_close_input(&stream_start);
blog(LOG_WARNING, "SRBeep: play_clip: Failed to find audio stream in stream_start_sound.mp3");
return;
}
//get audio stream
audioStreamIndex = -1;
for(unsigned int i = 0; i < stream_start->nb_streams; i++)
{
if(stream_start->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
{
audioStreamIndex = i;
break;
}
}
//get codec
AVCodecContext *cdx = avcodec_alloc_context3(NULL);
avcodec_parameters_to_context(cdx, stream_start->streams[audioStreamIndex]->codecpar);
const AVCodec *codec = avcodec_find_decoder(cdx->codec_id);
if(!codec)
{
blog(LOG_WARNING, "SRBeep: play_clip: Codec not supported");
return;
}
//openm codec
avcodec_open2(cdx, codec, NULL);
//audio packet
AVPacket* packet = av_packet_alloc();
//Audio Parameters
uint64_t out_channel_layout = AV_CH_LAYOUT_STEREO;
int out_nb_samples = cdx->frame_size;
AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;
int out_sample_rate = 44100;
int out_channels = av_get_channel_layout_nb_channels(out_channel_layout);
//Buffer Size
int out_buffer_size = av_samples_get_buffer_size(NULL, out_channels, out_nb_samples, out_sample_fmt, 1);
uint8_t *out_buffer = (uint8_t*)av_malloc(MAX_AUDIO_FRAME_SIZE * 2);
AVFrame *frame = av_frame_alloc();
audioMutex.lock();
//init SDL
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER))
{
audioMutex.unlock();
blog(LOG_WARNING, "SRBEEP: play_clip: SDL init failed");
return;
}
SDL_AudioSpec wanted_spec;
wanted_spec.freq = cdx->sample_rate;
wanted_spec.format = AUDIO_S16SYS;
wanted_spec.channels = cdx->channels;
wanted_spec.silence = 0;
wanted_spec.samples = out_nb_samples;
wanted_spec.callback = fill_audio;
wanted_spec.userdata = cdx;
if(SDL_OpenAudio(&wanted_spec, NULL) < 0)
{
audioMutex.unlock();
blog(LOG_WARNING, "SRBEEP: play_clip: SDL_OpenAudio failed");
return;
}
//FIX:Some Codec's Context Information is missing
int64_t in_channel_layout;
in_channel_layout = av_get_default_channel_layout(cdx->channels);
//Swr
struct SwrContext *au_convert_ctx;
au_convert_ctx = swr_alloc();
au_convert_ctx = swr_alloc_set_opts(au_convert_ctx, out_channel_layout, out_sample_fmt, out_sample_rate, in_channel_layout, cdx->sample_fmt, cdx->sample_rate, 0, NULL);
swr_init(au_convert_ctx);
int ret;
int index = 0;
while(av_read_frame(stream_start, packet) >= 0)
{
if(packet->stream_index == audioStreamIndex)
{
ret = avcodec_send_packet(cdx, packet);
if(ret == AVERROR(EAGAIN))
ret = 0;
if(ret == 0)
{
ret = avcodec_receive_frame(cdx, frame);
}
if(ret < 0)
{
audioMutex.unlock();
blog(LOG_WARNING, "SRBEEP: play_clip: Decoding audio frame error");
return;
}
else
{
swr_convert(au_convert_ctx, &out_buffer, MAX_AUDIO_FRAME_SIZE, (const uint8_t**)frame->data, frame->nb_samples);
index++;
}
while(audio_len > 0)//Wait until finish
SDL_Delay(1);
//Set audio buffer (PCM data)
audio_chunk = (Uint8*)out_buffer;
//Audio buffer length
audio_len = out_buffer_size;
audio_pos = audio_chunk;
//Play
SDL_PauseAudio(0);
}
}
av_packet_free(&packet);
swr_free(&au_convert_ctx);
//Close SDL
SDL_CloseAudio();
SDL_Quit();
//clean up
av_free(out_buffer);
avcodec_close(cdx);
av_frame_free(&frame);
avformat_close_input(&stream_start);
avcodec_free_context(&cdx);
audioMutex.unlock();
return;
}
std::string clean_path(std::string audio_path)
{
std::string cleaned_path;
//If relative path then the first 2 chars should be ".."
if(audio_path.find("..") != std::string::npos)
{
size_t pos = audio_path.find("..");
cleaned_path = audio_path.substr(pos);
}
//If absolute path, Windows will start with a capital, Linux/Mac will start with "/"
else
{
#ifdef _WIN32
while(islower(audio_path[0]) && audio_path.length() > 0)
{
audio_path = audio_path.substr(1);
}
#else
while(audio_path.substr(0, 1) != "/" && audio_path.length() > 0)
{
audio_path = audio_path.substr(1);
}
#endif
cleaned_path = audio_path;
}
return cleaned_path;
}
void play_sound(std::string file_name)
{
const char *obs_data_path = obs_get_module_data_path(obs_current_module());
std::stringstream audio_path;
std::string true_path;
audio_path << obs_data_path;
audio_path << file_name;
true_path = clean_path(audio_path.str());
play_clip(true_path.c_str());
audio_path.str("");
return;
}
void obsstudio_srbeep_frontend_event_callback(enum obs_frontend_event event, void *private_data)
{
if(event == OBS_FRONTEND_EVENT_STREAMING_STARTED)
{
if(st_stt_Thread.joinable())
{
st_stt_Thread.join();
}
st_stt_Thread = std::thread(play_sound, "/stream_start_sound.mp3");
}
else if(event == OBS_FRONTEND_EVENT_RECORDING_STARTED)
{
if (rc_stt_Thread.joinable())
{
rc_stt_Thread.join();
}
rc_stt_Thread = std::thread(play_sound, "/record_start_sound.mp3");
}
else if(event == OBS_FRONTEND_EVENT_REPLAY_BUFFER_STARTED)
{
if(bf_stt_Thread.joinable())
{
bf_stt_Thread.join();
}
bf_stt_Thread = std::thread(play_sound, "/buffer_start_sound.mp3");
}
else if(event == OBS_FRONTEND_EVENT_RECORDING_PAUSED)
{
if(ps_stt_Thread.joinable())
{
ps_stt_Thread.join();
}
ps_stt_Thread = std::thread(play_sound, "/pause_start_sound.mp3");
}
else if(event == OBS_FRONTEND_EVENT_STREAMING_STOPPED)
{
if(st_sto_Thread.joinable())
{
st_sto_Thread.join();
}
st_sto_Thread = std::thread(play_sound, "/stream_stop_sound.mp3");
}
else if(event == OBS_FRONTEND_EVENT_RECORDING_STOPPED)
{
if(rc_sto_Thread.joinable())
{
rc_sto_Thread.join();
}
rc_sto_Thread = std::thread(play_sound, "/record_stop_sound.mp3");
}
else if(event == OBS_FRONTEND_EVENT_REPLAY_BUFFER_STOPPED)
{
if(bf_sto_Thread.joinable())
{
bf_sto_Thread.join();
}
bf_sto_Thread = std::thread(play_sound, "/buffer_stop_sound.mp3");
}
else if(event == OBS_FRONTEND_EVENT_RECORDING_UNPAUSED)
{
if(ps_stt_Thread.joinable())
{
ps_stt_Thread.join();
}
ps_stt_Thread = std::thread(play_sound, "/pause_stop_sound.mp3");
}
}
bool obs_module_load(void)
{
obs_frontend_add_event_callback(obsstudio_srbeep_frontend_event_callback, 0);
return true;
}