📄 audio_sdl_old.cpp
字号:
/* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is MPEG4IP. * * The Initial Developer of the Original Code is Cisco Systems Inc. * Portions created by Cisco Systems Inc. are * Copyright (C) Cisco Systems Inc. 2000-2004. All Rights Reserved. * * Contributor(s): * Bill May wmay@cisco.com *//* * audio.cpp provides an interface (CAudioSync) between the codec and * the SDL audio APIs. */#include <stdlib.h>#include <string.h>#include "player_session.h"#include "audio_sdl.h"#include "player_util.h"#include "Our_SDL_audio.h"#include "our_config_file.h"//#define DEBUG_SYNC 1//#define DEBUG_SYNC_CHANGES 1//#define DEBUG_AUDIO_FILL 1//#define DEBUG_DELAY 1//#define TEST_MONO_TO_STEREO 1#ifdef _WIN32DEFINE_MESSAGE_MACRO(audio_message, "audiosync")#else#define audio_message(loglevel, fmt...) message(loglevel, "audiosync", fmt)#endif/* * c routine to call into the AudioSync class callback */static void c_audio_callback (void *userdata, Uint8 *stream, int len){ CSDLAudioSync *a = (CSDLAudioSync *)userdata; a->audio_callback(stream, len);}/* * Create an CSDLAudioSync for a session. Don't alloc any buffers until * config is called by codec */CSDLAudioSync::CSDLAudioSync (CPlayerSession *psptr, int volume) : CAudioSync(psptr){ //SDL_Init(SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE); Our_SDL_AudioInit(getenv("SDL_AUDIODRIVER")); m_fill_index = m_play_index = 0; for (int ix = 0; ix < DECODE_BUFFERS_MAX; ix++) { m_buffer_filled[ix] = 0; m_sample_buffer[ix] = NULL; } m_buffer_size = 0; m_config_set = 0; m_audio_initialized = 0; m_audio_paused = 1; m_resync_required = 0; m_dont_fill = 0; m_consec_no_buffers = 0; //SDL_Init(SDL_INIT_AUDIO); m_audio_waiting_buffer = 0; m_audio_waiting = SDL_CreateSemaphore(0); //NULL; // will be set by decode thread m_skipped_buffers = 0; m_didnt_fill_buffers = 0; m_play_time = 0 ; m_buffer_latency = 0; m_volume = (volume * SDL_MIX_MAXVOLUME)/100; m_first_time = 1; m_first_filled = 1; m_buffer_offset_on = 0; m_buffer_ts = 0; m_load_audio_do_next_resync = 0;}/* * Close out audio sync - stop and disconnect from SDL */CSDLAudioSync::~CSDLAudioSync (void){ Our_SDL_PauseAudio(1); Our_SDL_CloseAudio(); for (int ix = 0; ix < DECODE_BUFFERS_MAX; ix++) { if (m_sample_buffer[ix] != NULL) free(m_sample_buffer[ix]); m_sample_buffer[ix] = NULL; } audio_message(LOG_NOTICE, "Audio sync skipped %u buffers", m_skipped_buffers); audio_message(LOG_NOTICE, "didn't fill %u buffers", m_didnt_fill_buffers); SDL_DestroySemaphore(m_audio_waiting);}/* * codec api - set up information about the stream */void CSDLAudioSync::set_config (int freq, int channels, audio_format_t format, uint32_t sample_size) { if (m_config_set != 0) return 0; audio_message(LOG_DEBUG, "configure audio freq %d chan %d format %d", freq, channels, format); switch (format) { case AUDIO_FMT_U8: case AUDIO_FMT_S8: case AUDIO_FMT_HW_AC3: m_bytes_per_sample_input = sizeof(uint8_t); m_bytes_per_sample_output = sizeof(uint8_t); break; case AUDIO_FMT_FLOAT: // we convert float to S16 (native format) m_bytes_per_sample_input = sizeof(float); m_bytes_per_sample_output = sizeof(int16_t); break; default: m_bytes_per_sample_input = sizeof(int16_t); m_bytes_per_sample_output = sizeof(int16_t); break; } if (sample_size == 0) { int temp; temp = freq; while ((temp & 0x1) == 0) temp >>= 1; sample_size = temp; while (sample_size < 1024) sample_size *= 2; while (((sample_size * 1000) % freq) != 0) sample_size *= 2; } m_buffer_size = channels * sample_size * m_bytes_per_sample_input; for (int ix = 0; ix < DECODE_BUFFERS_MAX; ix++) { m_buffer_filled[ix] = 0; // I'm not sure where the 2 * comes in... Check this out m_sample_buffer[ix] = (uint8_t *)malloc(2 * m_buffer_size); } m_freq = freq; m_channels = channels; m_format = format; if (m_format == AUDIO_FMT_U8) { m_silence = 0x80; } else { m_silence = 0x00; } m_config_set = 1; m_msec_per_frame = (sample_size * 1000) / m_freq; audio_message(LOG_DEBUG, "buffer size %d msec per frame %d", m_buffer_size, m_msec_per_frame); return m_buffer_size;};/* * Codec api - get_audio_buffer - will wait if there are no available * buffers */uint8_t *CSDLAudioSync::get_audio_buffer (void){ int ret; int locked = 0; if (m_dont_fill == 1) {#ifdef DEBUG_AUDIO_FILL audio_message(LOG_DEBUG, "first dont fill");#endif return (NULL); } if (m_audio_initialized != 0) { locked = 1; Our_SDL_LockAudio(); } ret = m_buffer_filled[m_fill_index]; if (locked) Our_SDL_UnlockAudio(); if (ret == 1) { m_audio_waiting_buffer = 1; SDL_SemWait(m_audio_waiting); m_audio_waiting_buffer = 0; if (m_dont_fill != 0) {#ifdef DEBUG_AUDIO_FILL audio_message(LOG_DEBUG, "2nd don't fill");#endif return (NULL); } locked = 0; if (m_audio_initialized != 0) { Our_SDL_LockAudio(); locked = 1; } ret = m_buffer_filled[m_fill_index]; if (locked) Our_SDL_UnlockAudio(); if (ret == 1) {#ifdef DEBUG_AUDIO_FILL audio_message(LOG_DEBUG, "no buff");#endif return (NULL); } } return (m_sample_buffer[m_fill_index]);}void CSDLAudioSync::load_audio_buffer (const uint8_t *from, uint32_t bytes, uint64_t ts, int resync){ uint8_t *to; uint32_t copied; if (m_dont_fill == 1) {#ifdef DEBUG_AUDIO_FILL audio_message(LOG_DEBUG, "3rd dont fill");#endif return; }#ifdef DEBUG_AUDIO_FILL audio_message(LOG_DEBUG, "fill %d bytes at "U64", offset %d resync %d", bytes, ts, m_buffer_offset_on, resync);#endif copied = 0; if (m_buffer_offset_on == 0) { int64_t diff = ts - m_buffer_ts; if (m_buffer_ts != 0 && diff > 1) { m_load_audio_do_next_resync = 1; audio_message(LOG_DEBUG, "timeslot doesn't match - "U64" "U64, ts, m_buffer_ts); } m_buffer_ts = ts; } else { int64_t check; check = ts - m_loaded_next_ts; if (check > m_msec_per_frame) { audio_message(LOG_DEBUG, "potential resync at ts "U64" should be ts "U64, ts, m_loaded_next_ts); uint32_t left; left = m_buffer_size - m_buffer_offset_on; to = get_audio_buffer(); memset(to + m_buffer_offset_on, 0, left); filled_audio_buffer(m_buffer_ts, 0); m_buffer_offset_on = 0; m_load_audio_do_next_resync = 1; m_buffer_ts = ts; } } if (m_format == AUDIO_FMT_HW_AC3) { to = get_audio_buffer(); memcpy(to, from, bytes); m_buffer_len[m_fill_index] = bytes; filled_audio_buffer(ts, 0); m_loaded_next_ts = ts + m_msec_per_frame; return; } m_loaded_next_ts = bytes * TO_U64(1000); m_loaded_next_ts /= m_bytes_per_sample_input; m_loaded_next_ts /= m_freq; m_loaded_next_ts += ts; while ( bytes > 0) { to = get_audio_buffer(); if (to == NULL) { return; } int copy; uint32_t left; left = m_buffer_size - m_buffer_offset_on; copy = MIN(left, bytes); memcpy(to + m_buffer_offset_on, from, copy); bytes -= copy; copied += copy; from += copy; m_buffer_offset_on += copy; if (m_buffer_offset_on >= m_buffer_size) { m_buffer_offset_on = 0; filled_audio_buffer(m_buffer_ts, resync | m_load_audio_do_next_resync); m_buffer_ts += m_msec_per_frame; resync = 0; m_load_audio_do_next_resync = 0; } } return;} /* * filled_audio_buffer - codec API - after codec fills the buffer from * get_audio_buffer, it will call here. */void CSDLAudioSync::filled_audio_buffer (uint64_t ts, int resync){ uint32_t fill_index; int locked; // m_dont_fill will be set when we have a pause if (m_dont_fill == 1) { return; } // resync = 0; fill_index = m_fill_index; m_fill_index++; m_fill_index %= DECODE_BUFFERS_MAX; locked = 0; if (m_audio_initialized != 0) { Our_SDL_LockAudio(); locked = 1; } if (m_first_filled != 0) { m_first_filled = 0; resync = 0; m_resync_required = 0; } else { int64_t diff; diff = ts - m_last_fill_timestamp; if (diff - m_msec_per_frame > m_msec_per_frame) { // have a hole here - don't want to resync#ifdef DEBUG_AUDIO_FILL audio_message(LOG_DEBUG, "Filling - last "U64" new "U64, m_last_fill_timestamp, ts);#endif if (diff > ((m_msec_per_frame + 1) * 4) || m_format == AUDIO_FMT_HW_AC3) {#ifdef DEBUG_AUDIO_FILL audio_message(LOG_DEBUG, "resync required "D64, diff);#endif resync = 1; } else { // try to fill the holes m_last_fill_timestamp += m_msec_per_frame + 1; // fill plus extra if (locked) Our_SDL_UnlockAudio(); int64_t ts_diff; do { uint8_t *retbuffer; // Get and swap buffers. retbuffer = get_audio_buffer(); if (retbuffer == NULL) { return; } if (retbuffer != m_sample_buffer[m_fill_index]) { audio_message(LOG_ERR, "retbuffer not fill index in audio sync"); return; } locked = 0; if (m_audio_initialized != 0) { Our_SDL_LockAudio(); locked = 1; } m_sample_buffer[m_fill_index] = m_sample_buffer[fill_index]; m_sample_buffer[fill_index] = retbuffer; memset(retbuffer, m_silence, m_buffer_size); m_buffer_time[fill_index] = m_last_fill_timestamp; m_buffer_filled[fill_index] = 1; m_buffer_bytes_loaded += m_buffer_size;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -