Example #1
0
int libmediacb_start(msm_ctx *ctx, int channels, int samplerate) {
	 __android_log_print(ANDROID_LOG_INFO,"liblossless","libmedia_ START REEEEACHED REACHEDDDDDD1");
   status_t status;
   int chans; 

   if(!ctx) return LIBLOSSLESS_ERR_NOCTX;

  __android_log_print(ANDROID_LOG_INFO,"liblossless","libmediacb_start ctx=%p chans=%d rate=%d afd=%d atrack=%p",
                ctx, channels, samplerate,ctx->afd,ctx->track);

   AudioTrack* atrack = (AudioTrack *) ctx->track;

   if(atrack && ctx->samplerate == samplerate && ctx->channels == channels) {
  __android_log_print(ANDROID_LOG_INFO,"liblossless","same audio track parameters, restarting");
	atrack->stop();
	atrack->flush();
	ctx->cbstart = 0; ctx->cbend = 0;
	atrack->start();
	return 0; 
   }	

   if(!ctx->cbbuf) {
	ctx->cbbuf = (unsigned char *) malloc(DEFAULT_CB_BUFSZ);
	if(!ctx->cbbuf) return LIBLOSSLESS_ERR_NOMEM;
	ctx->cbbuf_size = DEFAULT_CB_BUFSZ;
   }		

   ctx->cbstart = 0; ctx->cbend = 0;	

   if(!atrack) {
   	atrack = new AudioTrack();
	if(!atrack) {
		__android_log_print(ANDROID_LOG_ERROR,"liblossless","could not create AudioTrack!");
		return LIBLOSSLESS_ERR_INIT;
	}
	 __android_log_print(ANDROID_LOG_INFO,"liblossless","AudioTrack created at %p. Now trying to setup (buffsz %d)", 
		atrack, DEFAULT_ATRACK_CONF_BUFSZ);
	if(!sdk_version) {	
		char c[PROP_VALUE_MAX];
		if(__system_property_get("ro.build.version.sdk",c) > 0) sscanf(c,"%d",&sdk_version);
		else sdk_version = 8;
		__android_log_print(ANDROID_LOG_INFO,"liblossless","got sdk_version %d", sdk_version);		
	} 	

	if(sdk_version > 13) chans = (channels == 2) ? 3 : 1;
	else if(sdk_version > 6) chans = (channels == 2) ? 12 : 4;
	else chans = channels;

#ifdef BUILD_JB
	status = atrack->set(_MUSIC, samplerate, FMTBPS, chans, DEFAULT_ATRACK_CONF_BUFSZ/(2*channels),AUDIO_OUTPUT_FLAG_NONE,cbf,ctx);
#else
	status = atrack->set(_MUSIC, samplerate, FMTBPS, chans, DEFAULT_ATRACK_CONF_BUFSZ/(2*channels),0,cbf,ctx);
#endif
	
   	if(status != NO_ERROR) {
		__android_log_print(ANDROID_LOG_INFO,"liblossless","AudioTrack setup failed");
		delete atrack;
		return LIBLOSSLESS_ERR_INIT;  
	}
	ctx->track = atrack;
   } else {
        atrack->stop();
	atrack->flush();
        ctx->cbstart = 0; ctx->cbend = 0;
	__android_log_print(ANDROID_LOG_INFO,"liblossless","trying to reconfigure old AudioTrack");
	status = atrack->setSampleRate(samplerate);
        if(status != NO_ERROR) {
                __android_log_print(ANDROID_LOG_INFO,"liblossless","could not set AudioTrack sample rate");
                return LIBLOSSLESS_ERR_INIT;
        }
   }		

  __android_log_print(ANDROID_LOG_INFO,"liblossless","AudioTrack setup OK, starting audio!");
   ctx->conf_size = DEFAULT_CONF_BUFSZ; 	
   atrack->start();	
  __android_log_print(ANDROID_LOG_INFO,"liblossless","playback started!");

   atrack->setPositionUpdatePeriod(0);
   atrack->setMarkerPosition(0); 	

   static int s(0); if(!s) { print_priority(__FUNCTION__); s = 1; } 		

   return 0; 
}