コード例 #1
0
ファイル: port_audio.cpp プロジェクト: atmos/butt
void *snd_stream_thread(void *data)
{
    int sent;
    int rb_read_bytes;
	int encode_bytes_read;

    char *enc_buf = (char*)malloc(stream_rb.size * sizeof(char)*10);
    char *audio_buf = (char*)malloc(stream_rb.size * sizeof(short));

    int (*xc_send)(char *buf, int buf_len) = NULL;

    encode_bytes_read = 0;

    if(cfg.srv[cfg.selected_srv]->type == SHOUTCAST)
            xc_send = &sc_send;
    if(cfg.srv[cfg.selected_srv]->type == ICECAST)
        xc_send = &ic_send;

    while(connected)
    {
        pthread_cond_wait(&stream_cond, &stream_mut);
        if(!connected)
            break;

		rb_read_bytes = rb_read(&stream_rb, audio_buf);
		if(rb_read_bytes == 0)
			continue;

#if HAVE_LIBLAME
        if(!strcmp(cfg.audio.codec, "mp3"))
            encode_bytes_read = lame_enc_encode(&lame_stream, (short int*)audio_buf, enc_buf,
                                rb_read_bytes/(2*cfg.audio.channel), stream_rb.size*10);
#endif
#if HAVE_LIBVORBIS
        if(!strcmp(cfg.audio.codec, "ogg"))
            encode_bytes_read = vorbis_enc_encode(&vorbis_stream, (short int*)audio_buf, 
                    enc_buf, rb_read_bytes/(2*cfg.audio.channel));
#endif
        if((sent = xc_send(enc_buf, encode_bytes_read)) == -1)
            connected = 0; 
        else
            bytes_sent += encode_bytes_read;
    }

    free(enc_buf);
    free(audio_buf);

    return NULL;
}
コード例 #2
0
ファイル: port_audio.cpp プロジェクト: melchor629/butt
void *snd_stream_thread(void *data)
{
    int sent;
    int rb_bytes_read;
	int encode_bytes_read = 0;
    int bytes_to_read;

    char *enc_buf = (char*)malloc(stream_rb.size * sizeof(char)*10);
    char *audio_buf = (char*)malloc(stream_rb.size * sizeof(char)*10);

    int (*xc_send)(char *buf, int buf_len) = NULL;


    if(cfg.srv[cfg.selected_srv]->type == SHOUTCAST)
        xc_send = &sc_send;
    else //Icecast
        xc_send = &ic_send;
    
    while(connected)
    {

        pthread_cond_wait(&stream_cond, &stream_mut);
        if(!connected)
            break;

        if(!strcmp(cfg.audio.codec, "opus"))
        {
            
            // Read always chunks of 960 frames from the audio ringbuffer to be
            // compatible with OPUS
            bytes_to_read = 960 * sizeof(short)*cfg.audio.channel;

            while ((rb_filled(&stream_rb)) >= bytes_to_read)
            {
                // Read always chunks of 960 frames from the audio ringbuffer to be
                bytes_to_read = 960 * sizeof(short)*cfg.audio.channel;
                rb_read_len(&stream_rb, audio_buf, bytes_to_read);

                encode_bytes_read = opus_enc_encode(&opus_stream, (short*)audio_buf,
                        enc_buf, bytes_to_read/(2*cfg.audio.channel));

                if((sent = xc_send(enc_buf, encode_bytes_read)) == -1)
                {
                    connected = 0;
                }
                else
                    kbytes_sent += bytes_to_read/1024.0;

            }
        }
        else // ogg and mp3 need more data than opus in order to compress the audio data
        {
            if(rb_filled(&stream_rb) < framepacket_size*sizeof(short))
                continue;

            rb_bytes_read = rb_read(&stream_rb, audio_buf);
            if(rb_bytes_read == 0)
                continue;

            if(!strcmp(cfg.audio.codec, "mp3"))
                encode_bytes_read = lame_enc_encode(&lame_stream, (short*)audio_buf, enc_buf,
                        rb_bytes_read/(2*cfg.audio.channel), stream_rb.size*10);
            
            if(!strcmp(cfg.audio.codec, "ogg"))
                encode_bytes_read = vorbis_enc_encode(&vorbis_stream, (short*)audio_buf, 
                        enc_buf, rb_bytes_read/(2*cfg.audio.channel));
            
            if(!strcmp(cfg.audio.codec, "aac")) {
                encode_bytes_read = aac_enc_encode(&aac_stream, (short*) audio_buf,
                        enc_buf, rb_bytes_read/2/cfg.audio.channel, stream_rb.size*10);
            }

            if(encode_bytes_read != 0) {
                if((sent = xc_send(enc_buf, encode_bytes_read)) == -1)
                    connected = 0;
                else
                    kbytes_sent += encode_bytes_read/1024.0;
            }

        }
    }

    free(enc_buf);
    free(audio_buf);

    return NULL;
}