comparison mplayer.c @ 20889:35f37142f3b8

Move audio playing code from main() into a separate function.
author uau
date Tue, 14 Nov 2006 03:13:45 +0000
parents 0c750c3c1aed
children 9e2d69c27ddb
comparison
equal deleted inserted replaced
20888:0c750c3c1aed 20889:35f37142f3b8
2914 if (!quiet) 2914 if (!quiet)
2915 print_status(0, 0, 0); 2915 print_status(0, 0, 0);
2916 } 2916 }
2917 } 2917 }
2918 2918
2919 int fill_audio_out_buffers(void)
2920 {
2921 unsigned int t;
2922 double tt;
2923 int playsize;
2924 int playflags=0;
2925 int audio_eof=0;
2926
2927 current_module="play_audio";
2928
2929 while (1) {
2930 ao_data.pts = ((sh_video?sh_video->timer:0)+sh_audio->delay)*90000.0;
2931 playsize = audio_out->get_space();
2932
2933 // handle audio-only case:
2934 if (playsize < ao_data.outburst && !sh_video) {
2935 // this is where mplayer sleeps during audio-only playback
2936 // to avoid 100% CPU use
2937 usec_sleep(10000); // Wait a tick before retry
2938 continue;
2939 }
2940
2941 if (playsize > MAX_OUTBURST)
2942 playsize = MAX_OUTBURST;
2943
2944 // Fill buffer if needed:
2945 current_module="decode_audio";
2946 t = GetTimer();
2947 while (sh_audio->a_out_buffer_len < playsize) {
2948 int buflen = sh_audio->a_out_buffer_len;
2949 int ret = decode_audio(sh_audio, &sh_audio->a_out_buffer[buflen],
2950 playsize - buflen, // min bytes
2951 sh_audio->a_out_buffer_size - buflen // max
2952 );
2953 if (ret <= 0) { // EOF?
2954 if (d_audio->eof) {
2955 audio_eof = 1;
2956 if (sh_audio->a_out_buffer_len == 0)
2957 return 0;
2958 }
2959 break;
2960 }
2961 sh_audio->a_out_buffer_len += ret;
2962 }
2963 t = GetTimer() - t;
2964 tt = t*0.000001f; audio_time_usage+=tt;
2965 if (playsize > sh_audio->a_out_buffer_len) {
2966 playsize = sh_audio->a_out_buffer_len;
2967 if (audio_eof)
2968 playflags |= AOPLAY_FINAL_CHUNK;
2969 }
2970 if (!playsize)
2971 break;
2972
2973 // play audio:
2974 current_module="play_audio";
2975 playsize = audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2976
2977 if (playsize > 0) {
2978 sh_audio->a_out_buffer_len -= playsize;
2979 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2980 sh_audio->a_out_buffer_len);
2981 sh_audio->delay += playback_speed*playsize/(double)ao_data.bps;
2982 }
2983 else if (audio_eof && audio_out->get_delay() < .04) {
2984 // Sanity check to avoid hanging in case current ao doesn't output
2985 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2986 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2987 sh_audio->a_out_buffer_len = 0;
2988 }
2989 break;
2990 }
2991 return 1;
2992 }
2919 2993
2920 int main(int argc,char* argv[]){ 2994 int main(int argc,char* argv[]){
2921 2995
2922 2996
2923 char * mem_ptr; 2997 char * mem_ptr;
4165 reinit_audio_chain(); 4239 reinit_audio_chain();
4166 } 4240 }
4167 4241
4168 /*========================== PLAY AUDIO ============================*/ 4242 /*========================== PLAY AUDIO ============================*/
4169 4243
4170 while(sh_audio){ 4244 if (sh_audio)
4171 unsigned int t; 4245 if (!fill_audio_out_buffers())
4172 double tt; 4246 // at eof, all audio at least written to ao
4173 int playsize; 4247 if (!sh_video)
4174 int playflags=0;
4175 int audio_eof=0;
4176
4177 current_module="play_audio";
4178
4179 ao_data.pts=((sh_video?sh_video->timer:0)+sh_audio->delay)*90000.0;
4180 playsize=audio_out->get_space();
4181
4182 // handle audio-only case:
4183 if(playsize < ao_data.outburst &&
4184 !sh_video) { // buffer is full, do not block here!!!
4185 usec_sleep(10000); // Wait a tick before retry
4186 continue;
4187 }
4188
4189 if(playsize>MAX_OUTBURST) playsize=MAX_OUTBURST; // we shouldn't exceed it!
4190
4191 // Fill buffer if needed:
4192 current_module="decode_audio"; // Enter AUDIO decoder module
4193 t=GetTimer();
4194 while (sh_audio->a_out_buffer_len < playsize) {
4195 int ret=decode_audio(sh_audio,&sh_audio->a_out_buffer[sh_audio->a_out_buffer_len],
4196 playsize-sh_audio->a_out_buffer_len,sh_audio->a_out_buffer_size-sh_audio->a_out_buffer_len);
4197 if(ret<=0) { // EOF?
4198 if (d_audio->eof) {
4199 audio_eof = 1;
4200 if (!sh_video && sh_audio->a_out_buffer_len == 0)
4201 eof = PT_NEXT_ENTRY; 4248 eof = PT_NEXT_ENTRY;
4202 } 4249
4203 break;
4204 }
4205 sh_audio->a_out_buffer_len+=ret;
4206 }
4207 t=GetTimer()-t;
4208 tt = t*0.000001f; audio_time_usage+=tt;
4209 if (playsize > sh_audio->a_out_buffer_len) {
4210 playsize = sh_audio->a_out_buffer_len;
4211 if (audio_eof)
4212 playflags |= AOPLAY_FINAL_CHUNK;
4213 }
4214 if (!playsize)
4215 break;
4216
4217 // play audio:
4218 current_module="play_audio";
4219 playsize = audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
4220
4221 if(playsize>0){
4222 sh_audio->a_out_buffer_len-=playsize;
4223 memmove(sh_audio->a_out_buffer,&sh_audio->a_out_buffer[playsize],sh_audio->a_out_buffer_len);
4224 sh_audio->delay+=playback_speed*playsize/(double)ao_data.bps;
4225 }
4226 else if (audio_eof && audio_out->get_delay() < .04) {
4227 // Sanity check to avoid hanging in case current ao doesn't output
4228 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
4229 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
4230 sh_audio->a_out_buffer_len = 0;
4231 }
4232 break;
4233 } // while(sh_audio)
4234 4250
4235 if(!sh_video) { 4251 if(!sh_video) {
4236 // handle audio-only case: 4252 // handle audio-only case:
4237 double a_pos=0; 4253 double a_pos=0;
4238 if(!quiet || end_at.type == END_AT_TIME ) 4254 if(!quiet || end_at.type == END_AT_TIME )