audio.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. #include "player.h"
  2. #include "packet.h"
  3. #include "frame.h"
  4. #ifdef _WIN32
  5. #include "Windows.h"
  6. #endif
  7. static void sdl_audio_callback(void *opaque, uint8_t*stream, int len);
  8. // 从packet_queue中取一个packet,解码生成frame
  9. static int audio_decode_frame(AVCodecContext *p_codec_ctx, packet_queue_t *p_pkt_queue, AVFrame *frame, CMediaHostApi* hostapi)
  10. {
  11. int ret = -1;
  12. if (NULL == p_codec_ctx){
  13. return ret;
  14. }
  15. while (0 == p_pkt_queue->abort_flag)
  16. {
  17. AVPacket pkt = {0};
  18. while (0 == p_pkt_queue->abort_flag)
  19. {
  20. // 3.2 一个音频packet含一至多个音频frame,每次avcodec_receive_frame()返回一个frame,此函数返回。
  21. // 下次进来此函数,继续获取一个frame,直到avcodec_receive_frame()返回AVERROR(EAGAIN),
  22. // 表示解码器需要填入新的音频packet
  23. ret = avcodec_receive_frame(p_codec_ctx, frame);
  24. if (ret >= 0)
  25. {
  26. // 时基转换,从d->avctx->pkt_timebase时基转换到1/frame->sample_rate时基
  27. AVRational tb = { 1, frame->sample_rate };
  28. if (frame->pts != AV_NOPTS_VALUE)
  29. {
  30. frame->pts = av_rescale_q(frame->pts, p_codec_ctx->pkt_timebase, tb);
  31. }
  32. else
  33. {
  34. //hostapi->Debug(MEDIA_LOG_DEBUG, "frame->pts no.");
  35. }
  36. return 1;
  37. }
  38. else if (ret == AVERROR_EOF)
  39. {
  40. avcodec_flush_buffers(p_codec_ctx);
  41. return -1;
  42. }
  43. else if (ret == AVERROR(EAGAIN))
  44. {
  45. break;
  46. }
  47. else
  48. {
  49. hostapi->Debug(MEDIA_LOG_DEBUG, "audio avcodec_receive_frame(): other errors.");
  50. continue;
  51. }
  52. }
  53. // 1. 取出一个packet。使用pkt对应的serial赋值给d->pkt_serial
  54. if (packet_queue_get(p_pkt_queue, &pkt, true, hostapi) < 0)
  55. {
  56. hostapi->Debug(MEDIA_LOG_DEBUG, "packet_queue_get return -1 exit audio_decode_frame function.");
  57. return -1;
  58. }
  59. // packet_queue中第一个总是flush_pkt。每次seek操作会插入flush_pkt,更新serial,开启新的播放序列
  60. if (NULL == pkt.data || 0 == pkt.size)
  61. {
  62. // 复位解码器内部状态/刷新内部缓冲区。当seek操作或切换流时应调用此函数。
  63. avcodec_flush_buffers(p_codec_ctx);
  64. return -2;
  65. }
  66. else
  67. {
  68. // 2. 将packet发送给解码器
  69. // 发送packet的顺序是按dts递增的顺序,如IPBBPBB
  70. // pkt.pos变量可以标识当前packet在视频文件中的地址偏移
  71. int iresult = avcodec_send_packet(p_codec_ctx, &pkt);
  72. if (AVERROR(EAGAIN) == iresult)
  73. {
  74. hostapi->Debug(MEDIA_LOG_DEBUG, "receive_frame and send_packet both returned EAGAIN, which is an API violation.");
  75. }
  76. if (0 == iresult)
  77. {
  78. av_packet_unref(&pkt);
  79. }
  80. }
  81. }
  82. return ret;
  83. }
  84. // 音频解码线程:从音频packet_queue中取数据,解码后放入音频frame_queue
  85. static int audio_decode_thread(void *arg)
  86. {
  87. player_stat_t *is = (player_stat_t *)arg;
  88. AVFrame *p_frame = av_frame_alloc();
  89. frame_t *af;
  90. int got_frame = 0;
  91. AVRational tb;
  92. int ret = 0;
  93. if (p_frame == NULL){
  94. return AVERROR(ENOMEM);
  95. }
  96. while (false == is->buser_stop)
  97. {
  98. got_frame = audio_decode_frame(is->m_pacodec_ctx[is->m_iaudio_dec_index], &is->audio_pkt_queue, p_frame, is->rvc_hostapi);
  99. if (got_frame < 0){
  100. if(-2 == got_frame){
  101. if (is->m_icurrent_index > is->m_iaudio_dec_index) {
  102. is->m_iaudio_dec_index++;
  103. }
  104. continue;
  105. }
  106. else {
  107. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, " audio_decode_frame < 0, goto end");
  108. goto the_end;
  109. }
  110. }
  111. if (got_frame)
  112. {
  113. tb.num = 1;
  114. tb.den = p_frame->sample_rate;
  115. //从frame队列找到一个可写的空间,若未停止则一直等待,已停止时返回NULL
  116. if (!(af = frame_queue_peek_writable(&is->audio_frm_queue))) {
  117. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "frame_queue_peek_writable return NULL, goto end.");
  118. goto the_end;
  119. }
  120. af->pts = (p_frame->pts == AV_NOPTS_VALUE) ? NAN : p_frame->pts * av_q2d(tb);
  121. af->pos = p_frame->pkt_pos;
  122. //-af->serial = is->auddec.pkt_serial;
  123. // 当前帧包含的(单个声道)采样数/采样率就是当前帧的播放时长
  124. AVRational tbdata = { p_frame->nb_samples, p_frame->sample_rate };
  125. //af->duration = av_q2d((AVRational) { p_frame->nb_samples, p_frame->sample_rate });
  126. af->duration = av_q2d(tbdata);
  127. // 将frame数据拷入af->frame,af->frame指向音频frame队列尾部
  128. av_frame_move_ref(af->frame, p_frame);
  129. // 更新音频frame队列大小及写指针
  130. frame_queue_push(&is->audio_frm_queue);
  131. }
  132. }
  133. the_end:
  134. av_frame_free(&p_frame);
  135. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "audio decode thread exit, thread id is %u, and user stop flag is %s.", SDL_ThreadID(), is->buser_stop ? "true":"false");
  136. is->m_baudio_decode_finished = true;
  137. return ret;
  138. }
  139. int open_audio_stream(player_stat_t *is)
  140. {
  141. AVCodecContext *p_codec_ctx = NULL;
  142. AVCodecParameters *p_codec_par = NULL;
  143. AVCodec* p_codec = NULL;
  144. int ret = -1;
  145. // 1. 为音频流构建解码器AVCodecContext
  146. for (size_t index = 0; index < is->m_uFilesCount; index++){
  147. // 1.1 获取解码器参数AVCodecParameters
  148. p_codec_par = is->m_paudio_stream[index]->codecpar;
  149. // 1.2 获取解码器
  150. p_codec = avcodec_find_decoder(p_codec_par->codec_id);
  151. if (NULL == p_codec) {
  152. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "Cann't find codec!");
  153. return ret;
  154. }
  155. // 1.3 构建解码器AVCodecContext
  156. // 1.3.1 p_codec_ctx初始化:分配结构体,使用p_codec初始化相应成员为默认值
  157. p_codec_ctx = avcodec_alloc_context3(p_codec);
  158. if (p_codec_ctx == NULL) {
  159. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "avcodec_alloc_context3() failed.");
  160. return ret;
  161. }
  162. // 1.3.2 p_codec_ctx初始化:p_codec_par ==> p_codec_ctx,初始化相应成员
  163. ret = avcodec_parameters_to_context(p_codec_ctx, p_codec_par);
  164. if (ret < 0) {
  165. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "avcodec_parameters_to_context() failed %d.", ret);
  166. avcodec_close(p_codec_ctx);
  167. avcodec_free_context(&p_codec_ctx);
  168. return ret;
  169. }
  170. // 1.3.3 p_codec_ctx初始化:使用p_codec初始化p_codec_ctx,初始化完成
  171. ret = avcodec_open2(p_codec_ctx, p_codec, NULL);
  172. if (ret < 0) {
  173. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "avcodec_open2() failed %d.", ret);
  174. avcodec_close(p_codec_ctx);
  175. avcodec_free_context(&p_codec_ctx);
  176. return ret;
  177. }
  178. p_codec_ctx->pkt_timebase = is->m_paudio_stream[index]->time_base;
  179. is->m_pacodec_ctx[index] = p_codec_ctx;
  180. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "%s:%d is->p_acodec_ctx[%d] = 0x%08x", __FUNCTION__, __LINE__, index, p_codec_ctx);
  181. }
  182. // 2. 创建音频解码线程
  183. is->m_audio_decode_tid = SDL_CreateThread(audio_decode_thread, "audio decode thread", is);
  184. if (NULL == is->m_audio_decode_tid) {
  185. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "SDL_Create audio decode thread failed: %s.", SDL_GetError());
  186. return -1;
  187. }
  188. else {
  189. ret = 0;
  190. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "create %s success, and thread id is %u.", SDL_GetThreadName(is->m_audio_decode_tid), SDL_GetThreadID(is->m_audio_decode_tid));
  191. }
  192. return ret;
  193. }
  194. static int audio_resample(player_stat_t *is, int64_t audio_callback_time)
  195. {
  196. int data_size = 0, resampled_data_size = 0;
  197. int64_t dec_channel_layout = 0;
  198. av_unused double audio_clock0 = 0.0;
  199. int wanted_nb_samples = 0;
  200. frame_t *af = NULL;
  201. while (frame_queue_nb_remaining(&is->audio_frm_queue) == 0){
  202. if ((av_gettime_relative() - audio_callback_time) > 1000000LL * is->audio_hw_buf_size / is->m_audio_param_tgt.bytes_per_sec / 2) {
  203. return -1;
  204. }
  205. av_usleep(1000);
  206. }
  207. // 若队列头部可读,则由af指向可读帧
  208. if (!(af = frame_queue_peek_readable(&is->audio_frm_queue))) {
  209. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "%s:%d user stop flag is %s, function return", __FUNCTION__, __LINE__, is->buser_stop ? "true" : "false");
  210. return -1;
  211. }
  212. frame_queue_next(&is->audio_frm_queue);
  213. // 根据frame中指定的音频参数获取缓冲区的大小
  214. data_size = av_samples_get_buffer_size(NULL, af->frame->channels, // 本行两参数:linesize,声道数
  215. af->frame->nb_samples, // 本行一参数:本帧中包含的单个声道中的样本数
  216. (AVSampleFormat)af->frame->format, 1); // 本行两参数:采样格式,不对齐
  217. // 获取声道布局
  218. dec_channel_layout =
  219. (af->frame->channel_layout && af->frame->channels == av_get_channel_layout_nb_channels(af->frame->channel_layout)) ?
  220. af->frame->channel_layout : av_get_default_channel_layout(af->frame->channels);
  221. wanted_nb_samples = af->frame->nb_samples;
  222. // is->audio_param_tgt是SDL可接受的音频帧数,是audio_open()中取得的参数
  223. // 在audio_open()函数中又有“is->audio_src = is->audio_param_tgt”
  224. // 此处表示:如果frame中的音频参数 == is->audio_src == is->audio_param_tgt,那音频重采样的过程就免了(因此时is->swr_ctr是NULL)
  225. //      否则使用frame(源)和is->audio_param_tgt(目标)中的音频参数来设置is->swr_ctx,并使用frame中的音频参数来赋值is->audio_src
  226. if (af->frame->format != is->m_audio_param_src.fmt ||
  227. dec_channel_layout != is->m_audio_param_src.channel_layout ||
  228. af->frame->sample_rate != is->m_audio_param_src.freq)
  229. {
  230. swr_free(&is->m_paudio_swr_ctx);
  231. // 使用frame(源)和is->audio_param_tgt(目标)中的音频参数来设置is->audio_swr_ctx
  232. is->m_paudio_swr_ctx = swr_alloc_set_opts(NULL,
  233. is->m_audio_param_tgt.channel_layout, (AVSampleFormat)is->m_audio_param_tgt.fmt, is->m_audio_param_tgt.freq,
  234. dec_channel_layout, (AVSampleFormat)af->frame->format, af->frame->sample_rate,
  235. 0, NULL);
  236. if (!is->m_paudio_swr_ctx || swr_init(is->m_paudio_swr_ctx) < 0)
  237. {
  238. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!",
  239. af->frame->sample_rate, av_get_sample_fmt_name((AVSampleFormat)af->frame->format), af->frame->channels,
  240. is->m_audio_param_tgt.freq, av_get_sample_fmt_name((AVSampleFormat)is->m_audio_param_tgt.fmt), is->m_audio_param_tgt.channels);
  241. swr_free(&is->m_paudio_swr_ctx);
  242. return -1;
  243. }
  244. // 使用frame中的参数更新is->audio_src,第一次更新后后面基本不用执行此if分支了,因为一个音频流中各frame通用参数一样
  245. is->m_audio_param_src.channel_layout = dec_channel_layout;
  246. is->m_audio_param_src.channels = af->frame->channels;
  247. is->m_audio_param_src.freq = af->frame->sample_rate;
  248. is->m_audio_param_src.fmt = (AVSampleFormat)af->frame->format;
  249. }
  250. if (is->m_paudio_swr_ctx)
  251. {
  252. // 重采样输入参数1:输入音频样本数是af->frame->nb_samples
  253. // 重采样输入参数2:输入音频缓冲区
  254. const uint8_t **in = (const uint8_t **)af->frame->extended_data;
  255. // 重采样输出参数1:输出音频缓冲区尺寸
  256. // 重采样输出参数2:输出音频缓冲区
  257. uint8_t **out = &is->m_paudio_frm_rwr;
  258. // 重采样输出参数:输出音频样本数(多加了256个样本)
  259. int out_count = (int64_t)wanted_nb_samples * is->m_audio_param_tgt.freq / af->frame->sample_rate + 256;
  260. // 重采样输出参数:输出音频缓冲区尺寸(以字节为单位)
  261. int out_size = av_samples_get_buffer_size(NULL, is->m_audio_param_tgt.channels, out_count, (AVSampleFormat)is->m_audio_param_tgt.fmt, 0);
  262. int len2 = 0;
  263. if (out_size < 0){
  264. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "av_samples_get_buffer_size() failed.");
  265. return -1;
  266. }
  267. av_fast_malloc(&is->m_paudio_frm_rwr, &is->audio_frm_rwr_size, out_size);
  268. if (!is->m_paudio_frm_rwr) {
  269. return AVERROR(ENOMEM);
  270. }
  271. // 音频重采样:返回值是重采样后得到的音频数据中单个声道的样本数
  272. len2 = swr_convert(is->m_paudio_swr_ctx, out, out_count, in, af->frame->nb_samples);
  273. if (len2 < 0){
  274. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "swr_convert() failed.");
  275. return -1;
  276. }
  277. if (len2 == out_count){
  278. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "audio buffer is probably too small.");
  279. if (swr_init(is->m_paudio_swr_ctx) < 0)
  280. swr_free(&is->m_paudio_swr_ctx);
  281. }
  282. is->m_paudio_frm = is->m_paudio_frm_rwr;
  283. // 重采样返回的一帧音频数据大小(以字节为单位)
  284. resampled_data_size = len2 * is->m_audio_param_tgt.channels * av_get_bytes_per_sample((AVSampleFormat)is->m_audio_param_tgt.fmt);
  285. }
  286. else
  287. {
  288. // 未经重采样,则将指针指向frame中的音频数据
  289. is->m_paudio_frm = af->frame->data[0];
  290. resampled_data_size = data_size;
  291. }
  292. audio_clock0 = is->audio_clock;
  293. /* update the audio clock with the pts */
  294. if (!isnan(af->pts)){
  295. is->audio_clock = af->pts + (double)af->frame->nb_samples / af->frame->sample_rate;
  296. }
  297. else{
  298. is->audio_clock = NAN;
  299. }
  300. is->audio_clock_serial = af->serial;
  301. #ifdef DEBUG
  302. {
  303. static double last_clock;
  304. //printf("audio: delay=%0.3f clock=%0.3f clock0=%0.3f\n",
  305. // is->audio_clock - last_clock,
  306. // is->audio_clock, audio_clock0);
  307. //is->rvc_log("audio: delay=%0.3f clock=%0.3f clock0=%0.3f\n",
  308. // is->audio_clock - last_clock,
  309. // is->audio_clock, audio_clock0);
  310. last_clock = is->audio_clock;
  311. }
  312. #endif
  313. return resampled_data_size;
  314. }
  315. #ifdef _WIN32
  316. static char* Utf8ToGB2312(const char* utf8)
  317. {
  318. int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
  319. wchar_t* wstr = new wchar_t[len + 1];
  320. memset(wstr, 0, len + 1);
  321. MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
  322. len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
  323. char* str = new char[len + 1];
  324. memset(str, 0, len + 1);
  325. WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
  326. if (wstr) {
  327. delete []wstr;
  328. wstr = NULL;
  329. }
  330. return str;
  331. }
  332. #endif
  333. static int open_audio_playing(void *arg)
  334. {
  335. player_stat_t *is = (player_stat_t *)arg;
  336. SDL_AudioSpec wanted_spec = {0};
  337. SDL_AudioSpec actual_spec = {0};
  338. wanted_spec.freq = is->m_pacodec_ctx[is->m_iaudio_dec_index]->sample_rate; // 采样率
  339. wanted_spec.format = AUDIO_S16SYS; // S表带符号,16是采样深度,SYS表采用系统字节序
  340. wanted_spec.channels = is->m_pacodec_ctx[is->m_iaudio_dec_index]->channels; // 声音通道数
  341. wanted_spec.silence = 0; // 静音值
  342. // wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE; // SDL声音缓冲区尺寸,单位是单声道采样点尺寸x通道数
  343. // SDL声音缓冲区尺寸,单位是单声道采样点尺寸x声道数
  344. wanted_spec.samples = FFMAX(SDL_AUDIO_MIN_BUFFER_SIZE, 2 << av_log2(wanted_spec.freq / SDL_AUDIO_MAX_CALLBACKS_PER_SEC));
  345. wanted_spec.callback = sdl_audio_callback; // 回调函数,若为NULL,则应使用SDL_QueueAudio()机制
  346. wanted_spec.userdata = is; // 提供给回调函数的参数
  347. if (NULL == is->m_straudiodev) {
  348. static bool blog = true;
  349. int iaudioapeaker = SDL_GetNumAudioDevices(0);
  350. if (blog) {
  351. is->rvc_hostapi->Debug(MEDIA_LOG_INFO, "audio output device count is %d.", iaudioapeaker);
  352. }
  353. int i = 0;
  354. for (; i < iaudioapeaker; i++) {
  355. #ifdef _WIN32
  356. char* strdevice = Utf8ToGB2312(SDL_GetAudioDeviceName(i, 0));
  357. if (blog) {
  358. is->rvc_hostapi->Debug(MEDIA_LOG_INFO, "device id %d audio device name is %s.", i, strdevice);
  359. }
  360. if (is->m_paudiodev && strstr(strdevice, is->m_paudiodev)) {
  361. const char* strdevname = SDL_GetAudioDeviceName(i, 0);
  362. is->m_straudiodev = av_strdup(strdevname);
  363. if (blog) {
  364. is->rvc_hostapi->Debug(MEDIA_LOG_INFO, "%s matched audio device name is %s.", is->m_paudiodev, strdevice);
  365. }
  366. delete []strdevice;
  367. break;
  368. }
  369. else {
  370. delete[] strdevice;
  371. #else
  372. if (blog) {
  373. is->rvc_hostapi->Debug(MEDIA_LOG_INFO, "device id %d audio device name is %s.", i, SDL_GetAudioDeviceName(i, 0));
  374. }
  375. if (is->m_paudiodev && strstr(SDL_GetAudioDeviceName(i, 0), is->m_paudiodev)) {
  376. const char* strdevname = SDL_GetAudioDeviceName(i, 0);
  377. is->m_straudiodev = av_strdup(strdevname);
  378. if (blog) {
  379. is->rvc_hostapi->Debug(MEDIA_LOG_INFO, "%s matched audio device name is %s.", is->m_paudiodev, strdevname);
  380. }
  381. break;
  382. #endif
  383. }
  384. }
  385. if (i == iaudioapeaker) {
  386. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "matched audio device name (%s) failed!", is->m_straudiodev ? is->m_straudiodev : "null");
  387. }
  388. {
  389. int inum = SDL_GetNumAudioDrivers();
  390. int i = 0;
  391. if (blog) {
  392. is->rvc_hostapi->Debug(MEDIA_LOG_INFO, "Audio Drivers number is %d.", inum);
  393. }
  394. for (; i < inum; i++) {
  395. const char* drivername = SDL_GetAudioDriver(i);
  396. if (blog) {
  397. is->rvc_hostapi->Debug(MEDIA_LOG_INFO, "[%d] Audio Drivers name is %s.", i, drivername);
  398. }
  399. }
  400. }
  401. if (blog) {
  402. is->rvc_hostapi->Debug(MEDIA_LOG_INFO, "current audio driver name is %s.", SDL_GetCurrentAudioDriver());
  403. }
  404. blog = false;
  405. }
  406. while (!(is->m_audio_dev = SDL_OpenAudioDevice(is->m_straudiodev, 0, &wanted_spec, &actual_spec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_CHANNELS_CHANGE))){
  407. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "SDL_OpenAudio (%d channels, %d Hz): %s",wanted_spec.channels, wanted_spec.freq, SDL_GetError());
  408. if (!wanted_spec.channels) {
  409. if (!wanted_spec.freq) {
  410. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR,"No more combinations to try, audio open failed!");
  411. return -1;
  412. }
  413. }
  414. return -1;
  415. }
  416. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "SDL_OpenAudioDevice success and audio_dev is %d.", is->m_audio_dev);
  417. is->m_audio_param_tgt.fmt = AV_SAMPLE_FMT_S16;
  418. is->m_audio_param_tgt.freq = actual_spec.freq;
  419. is->m_audio_param_tgt.channel_layout = av_get_default_channel_layout(actual_spec.channels);
  420. is->m_audio_param_tgt.channels = actual_spec.channels;
  421. is->m_audio_param_tgt.frame_size = av_samples_get_buffer_size(NULL, actual_spec.channels, 1, (AVSampleFormat)is->m_audio_param_tgt.fmt, 1);
  422. is->m_audio_param_tgt.bytes_per_sec = av_samples_get_buffer_size(NULL, actual_spec.channels, actual_spec.freq, (AVSampleFormat)is->m_audio_param_tgt.fmt, 1);
  423. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "audio param target (%d channels, %d Hz, channel_layout(%d), frame_size(%d), bytes_per_sec(%d)).", actual_spec.channels, actual_spec.freq, is->m_audio_param_tgt.channel_layout, is->m_audio_param_tgt.frame_size, is->m_audio_param_tgt.bytes_per_sec);
  424. if (is->m_audio_param_tgt.bytes_per_sec <= 0 || is->m_audio_param_tgt.frame_size <= 0){
  425. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "av_samples_get_buffer_size failed.");
  426. SDL_CloseAudioDevice(is->m_audio_dev);
  427. return -1;
  428. }
  429. is->m_audio_param_src = is->m_audio_param_tgt;
  430. is->audio_hw_buf_size = actual_spec.size; // SDL音频缓冲区大小
  431. is->audio_frm_size = 0;
  432. is->audio_cp_index = 0;
  433. SDL_PauseAudioDevice(is->m_audio_dev, 0);
  434. return 0;
  435. }
  436. // 音频处理回调函数。读队列获取音频包,解码,播放
  437. // 此函数被SDL按需调用,此函数不在用户主线程中,因此数据需要保护
  438. // \param[in] opaque 用户在注册回调函数时指定的参数
  439. // \param[out] stream 音频数据缓冲区地址,将解码后的音频数据填入此缓冲区
  440. // \param[out] len 音频数据缓冲区大小,单位字节
  441. // 回调函数返回后,stream指向的音频缓冲区将变为无效
  442. // 双声道采样点的顺序为LRLRLR
  443. static void sdl_audio_callback(void *opaque, uint8_t*stream, int len)
  444. {
  445. player_stat_t *is = (player_stat_t *)opaque;
  446. int audio_size = 0, len1 = 0;
  447. int64_t audio_callback_time = av_gettime_relative();
  448. while (len > 0 && false == is->buser_stop) // 输入参数len等于is->audio_hw_buf_size,是audio_open()中申请到的SDL音频缓冲区大小
  449. {
  450. if (is->audio_cp_index >= (int)is->audio_frm_size){
  451. // 1. 从音频frame队列中取出一个frame,转换为音频设备支持的格式,返回值是重采样音频帧的大小
  452. audio_size = audio_resample(is, audio_callback_time);
  453. if (audio_size < 0){
  454. if (-1 == audio_size) {
  455. if (is->m_baudio_decode_finished) {
  456. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "audio_size is -1 sdl_audio_callback return, and set abort flag to true.");
  457. is->on_audio_play_finished(is->user_data);
  458. is->rvc_hostapi->Debug(MEDIA_LOG_INFO, "SDL_SemPost m_audio_play_wait_sem.");
  459. SDL_SemPost(is->m_audio_play_wait_sem);
  460. return;
  461. }
  462. }
  463. /* if error, just output silence */
  464. is->m_paudio_frm = NULL;
  465. is->audio_frm_size = SDL_AUDIO_MIN_BUFFER_SIZE / is->m_audio_param_tgt.frame_size * is->m_audio_param_tgt.frame_size;
  466. }
  467. else{
  468. is->audio_frm_size = audio_size;
  469. }
  470. is->audio_cp_index = 0;
  471. }
  472. // 引入is->audio_cp_index的作用:防止一帧音频数据大小超过SDL音频缓冲区大小,这样一帧数据需要经过多次拷贝
  473. // 用is->audio_cp_index标识重采样帧中已拷入SDL音频缓冲区的数据位置索引,len1表示本次拷贝的数据量
  474. len1 = is->audio_frm_size - is->audio_cp_index;
  475. if (len1 > len){
  476. len1 = len;
  477. }
  478. // 2. 将转换后的音频数据拷贝到音频缓冲区stream中,之后的播放就是音频设备驱动程序的工作了
  479. if (is->m_paudio_frm != NULL){
  480. SDL_memset(stream, 0, len1);
  481. int ivolume = is->uVolume;
  482. if (0 == is->on_audio_volume(&ivolume,is->user_data)){
  483. //is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "on_audio_volume success.");
  484. }
  485. //is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "audio uVolume is %d.", ivolume);
  486. //SDL_MixAudio(stream, (uint8_t*)is->p_audio_frm + is->audio_cp_index, len1, ivolume);
  487. SDL_MixAudioFormat(stream, (uint8_t*)is->m_paudio_frm + is->audio_cp_index, AUDIO_S16SYS, len1, ivolume);
  488. //is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "%s:%d SDL_MixAudioFormat audio length is %d.", __FUNCTION__, __LINE__, len1);
  489. if (is->m_prvc_cb && is->m_prvc_cb->cb_playing_audiodata) {
  490. is->m_prvc_cb->cb_playing_audiodata(&(is->m_audio_param_tgt), (uint8_t*)is->m_paudio_frm + is->audio_cp_index, len1, is->m_prvc_cb->user_data);
  491. }
  492. }
  493. else{
  494. SDL_memset(stream, 0, len1);
  495. }
  496. len -= len1;
  497. stream += len1;
  498. is->audio_cp_index += len1;
  499. }
  500. // is->audio_write_buf_size是本帧中尚未拷入SDL音频缓冲区的数据量
  501. is->audio_write_buf_size = is->audio_frm_size - is->audio_cp_index;
  502. //is->rvc_hostapi->Debug("audio_write_buf_size == %d.", is->audio_write_buf_size);
  503. /* Let's assume the audio driver that is used by SDL has two periods. */
  504. // 3. 更新时钟
  505. if (!isnan(is->audio_clock))
  506. {
  507. // 更新音频时钟,更新时刻:每次往声卡缓冲区拷入数据后
  508. // 前面audio_decode_frame中更新的is->audio_clock是以音频帧为单位,所以此处第二个参数要减去未拷贝数据量占用的时间
  509. set_clock_at(&is->audio_clk,
  510. is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / is->m_audio_param_tgt.bytes_per_sec,
  511. is->audio_clock_serial,
  512. audio_callback_time / 1000000.0);
  513. }
  514. }
  515. int open_audio(player_stat_t *is)
  516. {
  517. int iret = -1;
  518. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "enter open_audio()");
  519. if (-1 == is->audio_idx[is->m_icurrent_index]){
  520. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "not find audio stream");
  521. }
  522. else {
  523. if (0 == open_audio_stream(is)) {
  524. iret = open_audio_playing(is);
  525. if (0 != iret) {
  526. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "open audio playing failed");
  527. }
  528. }
  529. else {
  530. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "open audio stream failed");
  531. }
  532. }
  533. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "exit open_audio()");
  534. return iret;
  535. }