Skip to content

Commit

Permalink
sound ok
Browse files Browse the repository at this point in the history
  • Loading branch information
microcai committed Nov 28, 2012
1 parent eae8bb3 commit d72be47
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 13 deletions.
71 changes: 61 additions & 10 deletions audio/sdl_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string>

#include <queue>
#include <sys/socket.h>
#include <SDL/SDL_audio.h>
#include <boost/thread/condition.hpp>

#include <avplay.h>
#include "sdl_render.h"

Expand Down Expand Up @@ -80,18 +83,66 @@ EXPORT_API void sdl_destory_audio(void* ctx)
}
#endif

void sdl_audio_render::sdl_audio_callback(void* userdata, Uint8* stream, int len)
{
sdl_audio_render *my = reinterpret_cast<sdl_audio_render *>(userdata);
my->audio_callback(stream,len);
}

void sdl_audio_render::audio_callback(Uint8* stream, int len)
{
ssize_t readed=0;
logger("sdl ask for %d length of data %p\n",len,stream);

while(readed < len){

ssize_t ret = read(adfd[1],stream,len - readed);
if(ret >= 0){
readed += ret;
}else{
logger("unexpeted read return\n");
exit(1);
}
}
}

int sdl_audio_render::play_audio(uint8_t* data, uint32_t size)
{
// push to stack
ssize_t ret = write(adfd[0],data,size);
if(ret != size){
logger("write audio error\b");
exit(1);
}
return ret;
}

void sdl_audio_render::audio_control(int cmd, void* arg)
{

}

void sdl_audio_render::destory_audio()
{

}

bool sdl_audio_render::init_audio(void* ctx, int channels, int bits_per_sample, int sample_rate, int format)
{
SDL_AudioSpec fmt;
socketpair(AF_UNIX,SOCK_CLOEXEC|SOCK_STREAM,0,adfd);
logger("socket created for audio %d %d\n",adfd[0],adfd[1]);
SDL_AudioSpec fmt[1];// = new SDL_AudioSpec;

/* Set 16-bit stereo audio at 22Khz */
fmt.freq = sample_rate;
fmt.format = AUDIO_S16;
fmt.channels = channels;
fmt.samples = 512; /* A good value for games */
fmt.callback = mixaudio;
fmt.userdata = NULL;

SDL_OpenAudio();
fmt->silence = 0;
fmt->freq = sample_rate;
fmt->format = AUDIO_S16;
fmt->channels = channels;
fmt->samples = 1024; /* A good value for games */
fmt->callback = sdl_audio_callback;
fmt->userdata = this;

bool ret = SDL_OpenAudio(fmt,0)>=0;
SDL_PauseAudio(0);
return ret;
}
11 changes: 10 additions & 1 deletion audio/sdl_render.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#ifndef SDL_RENDER_H
#define SDL_RENDER_H

#include <boost/thread/mutex.hpp>
#include "audio_render.h"

class sdl_audio_render:public audio_render
Expand All @@ -37,6 +37,15 @@ class sdl_audio_render:public audio_render
// 销毁音频输出组件.
virtual void destory_audio();

static void sdl_audio_callback(void *userdata, Uint8 *stream, int len);
friend void sdl_audio_callback(void *userdata, Uint8 *stream, int len);
private:
void audio_callback(Uint8 *stream, int len);
private:
int adfd[2];
// boost::mutex adqueue_mutex;
// boost::condition_variable adqueue_mutex_cond;
// std::queue<std::pair<uint8_t*,uint32_t> > adqueue;
};

#endif // SDL_RENDER_H
6 changes: 5 additions & 1 deletion linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ int main(int argc, char* argv[])
//开始播放
ply.play();

ply.wait_for_completion();
while(true){
SDL_Event event[1];
SDL_WaitEvent(event);
}

//ply.wait_for_completion();

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion linux/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ int player::open(const char* movie, int media_type)

// 初始化音频和视频渲染器.
init_audio(m_audio);
// init_video(m_video);
init_video(m_video);

// 配置音频视频渲染器.
configure(m_avplay, m_video, VIDEO_RENDER);
Expand Down
1 change: 1 addition & 0 deletions video/sdl_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@ bool sdl_render::init_render(void* ctx, int w, int h, int pix_fmt)
this->m_yuv = SDL_CreateYUVOverlay(w,h,SDL_IYUV_OVERLAY,sfc);
SDL_UnlockSurface(sfc);
logger("%s is called %p \n",__func__, sfc);
return m_yuv;
}

0 comments on commit d72be47

Please sign in to comment.