Esempio n. 1
0
/**
        \brief Called from ACEReactor when there are events in our queue()
*/
int AuthLink::handle_output( ACE_HANDLE /*= ACE_INVALID_HANDLE*/ )
{
    SEGSEvent *ev;
    ACE_Time_Value nowait (ACE_OS::gettimeofday ());
    while (-1 != getq(ev, &nowait))
    {
        if(ev->type()==SEGS_EventTypes::evFinish)
        {
            ACE_DEBUG ((LM_DEBUG,ACE_TEXT ("(%P|%t) Error sent, closing connection\n")));
            return -1;
        }
        if(ev->type()==evContinue) // we have asked ourselves to send leftovers
        {
            assert(m_unsent_bytes_storage.GetReadableDataSize() > 0); // be sure we have some
        }
        else
        {
            size_t start_offset=m_unsent_bytes_storage.GetReadableDataSize();
            encode_buffer(static_cast<AuthLinkEvent *>(ev),start_offset);
        }
        if(!send_buffer()) // trying to send the contents of the buffer
        {
            ev->release(); // we have failed somehow
            break;
        }
        ev->release();
    }
    if (msg_queue()->is_empty ()) // we don't want to be woken up
        reactor()->cancel_wakeup(this, ACE_Event_Handler::WRITE_MASK);
    else // unless there is something to send still
        reactor()->schedule_wakeup(this, ACE_Event_Handler::WRITE_MASK);
    return 0;
}
Esempio n. 2
0
bool NXTMSWriter::append_buffer(NXPtr<NXBuffer>& buffer,NXIN NXSDKTrackType track)
{
	if(_fileObjt == NULL)
		return false ;

	return encode_buffer(buffer, track) ;
}
    /*
     * Run n encodings
     */
    void runEncode(char *buffer, const int n, const int bufferLength)
    {
        char *ptr = buffer;

        for (int i = 0; i < n; i++)
        {
            ptr += encode_buffer(ptr, bufferLength);
        }
    };
 /*
  * Run 1 encoding + decoding
  */
 void runEncodeAndDecode(char *buffer, const int bufferLength)
 {
     encode_buffer(buffer, bufferLength);
     decode_buffer(buffer, bufferLength);
 };
Esempio n. 5
0
File: merge_keys.c Progetto: vab/cks
int build_new_radix_data(struct openPGP_pubkey *pubkey)
{
	struct user_id *walk_id = NULL;
	struct key_signature *walk_sig = NULL;
	struct openPGP_subkey *walk_subkey = NULL;
	unsigned char *buffer = NULL;
	unsigned long buf_len = 0;
	unsigned long encoded = 0;
	unsigned char *decoded = NULL;
	unsigned int decoded_length = 0;
	unsigned char decoded_cksum[5];

	int checksum = 0;
	int result = 0;


	memset(decoded_cksum,0x00,5);
	buffer = (unsigned char *)malloc((strlen(pubkey->radix_data)) *2);
	if(buffer == NULL)
	{
		fprintf(stderr,_("merge_keys.c:  Malloc call failed: out of memory!\n"));
		pubkey->key_status = -1;

		return -1;
	}
	memset(buffer,0x00,((strlen(pubkey->radix_data)) *2));
	decoded = (unsigned char *)malloc((strlen(pubkey->radix_data)) *2);
	if(decoded == NULL)
	{
		fprintf(stderr,_("merge_keys.c:  Malloc call failed: out of memory!\n"));
		if(buffer != NULL)
		{
			free(buffer);
		}

		pubkey->key_status = -1;

		return -1;
	}
	memset(decoded,0x00,((strlen(pubkey->radix_data)) *2));
	decoded_length = decode_buffer(pubkey->radix_data,decoded);
	if(decoded_length == 0)
	{
		fprintf(stderr,_("merge_keys.c:  error null buffer decoded length is 0.\n"));
		if(buffer != NULL)
		{
			free(buffer);
		}
		if(decoded != NULL)
		{
			free(decoded);
		}
		pubkey->key_status = -1;

		return -1;
	}
   /*     for(i=0;i<decoded_length;i++)
        {
                printf("%d: 0x%0.2x\n",i, decoded[i]);
        }
*/
	memset(pubkey->radix_data,0x00,strlen(pubkey->radix_data));

	if(pubkey->the_packet == NULL)
	{
		fprintf(stderr,_("merge_keys.c: pubkey->the_packet is null.\n"));
		if(buffer != NULL)
		{
			free(buffer);
		}
		if(decoded != NULL)
		{
			free(decoded);
		}
		pubkey->key_status = -1;

		return -1;
	}
	
	/*  Now we have to walk the pubkey */
	result = append_packet_to_buffer(pubkey->the_packet,buffer,buf_len);
	if(result == -1)
	{
		fprintf(stderr,_("merge_keys.c: Failed to append packet to buffer in merge keys.\n"));
		if(buffer != NULL)
			free(buffer);
		if(decoded != NULL)
			free(decoded);
		pubkey->key_status = -1;

		return -1;
	}

	buf_len += pubkey->the_packet->full_packet_length;

	walk_id = (struct user_id *)get_first_uid(pubkey->ids);
	if(walk_id == NULL)
	{
		fprintf(stderr,_("merge_keys.c: walk_id returned NULL.\n"));
		if(buffer != NULL)
			free(buffer);
		if(decoded != NULL)
			free(decoded);
		pubkey->key_status = -1;

		return -1;
	}

	while(walk_id != NULL)
	{
		/* Appending a UID */
		result = append_packet_to_buffer(walk_id->the_packet,buffer,buf_len);
		if(result == -1)
		{
			fprintf(stderr,_("merge_keys.c: Failed to append packet to buffer (UID).\n"));
			if(buffer != NULL)
			{
				free(buffer);
			}
			if(decoded != NULL)
			{
				free(decoded);
			}
			pubkey->key_status = -1;

			return -1;
		}
		buf_len += walk_id->the_packet->full_packet_length;

		walk_sig = (struct key_signature *)get_first_sig(walk_id->signatures);
		if(walk_sig == NULL)
		{
			walk_id->signatures = NULL;
		}
		while(walk_sig != NULL)
		{
			/* Appending A Signature */
			/*    printf("appending a signature\n", walk_sig->key_id); */
			result = append_packet_to_buffer(walk_sig->the_packet,buffer,buf_len);
			if(result == -1)
			{
				fprintf(stderr,_("merge_keys.c: Failed to append packet to buffer (signature).\n"));
				if(buffer != NULL)
				{
					free(buffer);
				}
				if(decoded != NULL)
				{
					free(decoded);
				}
					pubkey->key_status = -1;

				return -1;
			}
			buf_len += walk_sig->the_packet->full_packet_length;

			walk_sig = walk_sig->next;
		}
		walk_id = walk_id->next;
	}

	walk_subkey = (struct openPGP_subkey *)get_first_subkey(pubkey->subkeys);
	if(walk_subkey == NULL)
	{
		pubkey->subkeys = NULL;
	}
	while(walk_subkey != NULL)
	{
		/* Appending A Subkey */
		/* printf("appending a Subkey: %s\n",walk_subkey->keyid_t); */
		result = append_packet_to_buffer(walk_subkey->the_packet,buffer,buf_len);
		if(result == -1)
		{
			fprintf(stderr,_("merge_keys.c: Failed to append packet to buffer (subkey).\n"));
			if(buffer != NULL)
			{
				free(buffer);
			}
			if(decoded != NULL)
			{
				free(decoded);
			}

			pubkey->key_status = -1;

			return -1;
		}

		buf_len += walk_subkey->the_packet->full_packet_length;

		/* Walk Subkey Binding Signatures */
		walk_sig = (struct key_signature *)get_first_sig(walk_subkey->binding_signatures);
		if(walk_sig == NULL)
		{
			walk_subkey->binding_signatures = NULL;
		}
		while(walk_sig != NULL)
		{
			/* Appending a Subkey Signature */
			/* printf("appending a subkey binding signature\n"); */
			result = append_packet_to_buffer(walk_sig->the_packet,buffer,buf_len);
			if(result == -1)
			{
				fprintf(stderr,_("merge_keys.c: Failed to append packet to buffer (subkey binding sig).\n"));
				if(buffer != NULL)
				{
					free(buffer);
				}
				if(decoded != NULL)
				{
					free(decoded);
				}

				pubkey->key_status = -1;

				return -1;
			}
			buf_len += walk_sig->the_packet->full_packet_length;

			walk_sig = walk_sig->next;
		}
		walk_subkey = walk_subkey->next;
	}

	/*  Radix encode the new key */
	encoded = encode_buffer(buffer, pubkey->radix_data,buf_len);

	/* Calc the checksum */
	checksum = radix_checksum(buffer,buf_len);

	/*Break the checksum down into bytes so that I can radix encode it */
	pubkey->encoded_cksum[0] = (checksum >> 16) & 0x000000FF;
	pubkey->encoded_cksum[1] = (checksum >> 8) & 0x000000FF;
	pubkey->encoded_cksum[2] = checksum & 0x000000FF;
	pubkey->encoded_cksum[3] = '\0';
	pubkey->encoded_cksum[4] = '\0';

	encoded = encode_buffer(pubkey->encoded_cksum, decoded_cksum,3);
	snprintf(pubkey->encoded_cksum, 5, "%c%c%c%c", decoded_cksum[0],decoded_cksum[1],decoded_cksum[2],decoded_cksum[3]);

	#ifdef DEBUG
	fprintf(stderr,"New Radix created with cksum: %s\n",pubkey->encoded_cksum);
	fprintf(stderr,"Radix Data:\n%s\n\n",pubkey->radix_data);
	#endif

	if(decoded != NULL)
	{
		free(decoded);
	}
	if(buffer != NULL)
	{
		free(buffer);
	}


	return 0;
}
Esempio n. 6
0
/**
 * Decode a frame to a packet, run the result through SwrContext, if desired, encode it via an appropriate
 * encoder, and write the results to the Java-side native buffer.
 *
 * @param aio       FFAudio context
 * @param cached    true or false
 * @return number of bytes placed into java buffer or a negative value, if something went wrong
 */
static int decode_packet(FFAudioIO *aio, int cached) {
    int res = 0;
    uint8_t **resample_buf = NULL;
    jobject byte_buffer = NULL;
    uint8_t *javaBuffer = NULL;
    uint32_t out_buf_size = 0;
    int out_buf_samples = 0;
    int64_t out_channel_count;
    int64_t out_sample_rate;
    int flush = aio->got_frame;
    enum AVSampleFormat out;
    int bytesConsumed = 0;

    init_ids(aio->env, aio->java_instance);

    av_opt_get_int(aio->swr_context, "out_channel_count", 0, &out_channel_count);
    av_opt_get_int(aio->swr_context, "out_sample_rate", 0, &out_sample_rate);
    av_opt_get_sample_fmt(aio->swr_context, "out_sample_fmt", 0, &out);

    resample_buf = av_mallocz(sizeof(uint8_t *) * 1); // one plane!

    // make sure we really have an audio packet
    if (aio->decode_packet.stream_index == aio->stream_index) {
        // decode frame
        // got_frame indicates whether we got a frame
        bytesConsumed = avcodec_decode_audio4(aio->decode_context, aio->decode_frame, &aio->got_frame, &aio->decode_packet);
        if (bytesConsumed < 0) {
            throwUnsupportedAudioFileExceptionIfError(aio->env, bytesConsumed, "Failed to decode audio frame.");
            return bytesConsumed;
        }

        if (aio->got_frame) {

            aio->decoded_samples += aio->decode_frame->nb_samples;
            out_buf_samples = aio->decode_frame->nb_samples;
#ifdef DEBUG
            fprintf(stderr, "samples%s n:%" PRIu64 " nb_samples:%d pts:%s\n",
                   cached ? "(cached)" : "",
                   aio->decoded_samples, aio->decode_frame->nb_samples,
                   av_ts2timestr(aio->decode_frame->pts, &aio->decode_context->time_base));
#endif

            // adjust out sample number for a different sample rate
            // this is an estimate!!
            out_buf_samples = av_rescale_rnd(
                    swr_get_delay(aio->swr_context, aio->stream->codecpar->sample_rate) + aio->decode_frame->nb_samples,
                    out_sample_rate,
                    aio->stream->codecpar->sample_rate,
                    AV_ROUND_UP
            );

            // allocate new aio->audio_data buffers
            res = av_samples_alloc(aio->audio_data, NULL, av_frame_get_channels(aio->decode_frame),
                                   aio->decode_frame->nb_samples, aio->decode_frame->format, 1);
            if (res < 0) {
                throwIOExceptionIfError(aio->env, res, "Could not allocate audio buffer.");
                return AVERROR(ENOMEM);
            }
            // copy audio data to aio->audio_data
            av_samples_copy(aio->audio_data, aio->decode_frame->data, 0, 0,
                            aio->decode_frame->nb_samples, av_frame_get_channels(aio->decode_frame), aio->decode_frame->format);

            res = resample(aio, resample_buf, out_buf_samples, (const uint8_t **)aio->audio_data, aio->decode_frame->nb_samples);
            if (res < 0) goto bail;
            else out_buf_samples = res;

        } else if (flush && swr_get_delay(aio->swr_context, aio->stream->codecpar->sample_rate)) {

            res = resample(aio, resample_buf, swr_get_delay(aio->swr_context, aio->stream->codecpar->sample_rate), NULL, 0);
            if (res < 0) goto bail;
            else out_buf_samples = res;
        } else {
#ifdef DEBUG
            fprintf(stderr, "Got no frame.\n");
#endif
        }

        if (out_buf_samples > 0) {

            res =  av_samples_get_buffer_size(NULL, (int)out_channel_count, out_buf_samples, out, 1);
            if (res < 0) goto bail;
            else out_buf_size = res;

            // ensure native buffer capacity
            if (aio->java_buffer_capacity < out_buf_size) {
                aio->java_buffer_capacity = (*aio->env)->CallIntMethod(aio->env, aio->java_instance, setNativeBufferCapacity_MID, (jint)out_buf_size);
            }
            // get java-managed byte buffer reference
            byte_buffer = (*aio->env)->GetObjectField(aio->env, aio->java_instance, nativeBuffer_FID);
            if (!byte_buffer) {
                res = -1;
                throwIOExceptionIfError(aio->env, 1, "Failed to get native buffer.");
                goto bail;
            }

            // we have some samples, let's copy them to the java buffer, using the desired encoding
            javaBuffer = (uint8_t *)(*aio->env)->GetDirectBufferAddress(aio->env, byte_buffer);
            if (!javaBuffer) {
                throwIOExceptionIfError(aio->env, 1, "Failed to get address for native buffer.");
                goto bail;
            }
            if (aio->encode_context) {
                aio->encode_frame->nb_samples = out_buf_samples;
                res = encode_buffer(aio, resample_buf[0], out_buf_size, javaBuffer);
                if (res < 0) {
                    out_buf_size = 0;
                    goto bail;
                }
                out_buf_size = res;
            } else {
                memcpy(javaBuffer, resample_buf[0], out_buf_size);
            }
            // we already wrote to the buffer, now we still need to
            // set new bytebuffer limit and position to 0.
            (*aio->env)->CallObjectMethod(aio->env, byte_buffer, rewind_MID);
            (*aio->env)->CallObjectMethod(aio->env, byte_buffer, limit_MID, out_buf_size);
        }
    }

    aio->resampled_samples += out_buf_size;

bail:

    if (resample_buf) {
        if (resample_buf[0]) av_freep(&resample_buf[0]);
        av_free(resample_buf);
    }
    if (aio->audio_data[0]) av_freep(&aio->audio_data[0]);

    return res;
}
Esempio n. 7
0
static void *encode_thread(void *arg) {
    struct GrooveEncoder *encoder = arg;
    struct GrooveEncoderPrivate *e = (struct GrooveEncoderPrivate *) encoder;

    struct GrooveBuffer *buffer;
    while (!e->abort_request) {
        pthread_mutex_lock(&e->encode_head_mutex);

        if (e->audioq_size >= encoder->encoded_buffer_size) {
            pthread_cond_wait(&e->drain_cond, &e->encode_head_mutex);
            pthread_mutex_unlock(&e->encode_head_mutex);
            continue;
        }

        // we definitely want to unlock the mutex while we wait for the
        // next buffer. Otherwise there will be a deadlock when sink_flush or
        // sink_purge is called.
        pthread_mutex_unlock(&e->encode_head_mutex);

        int result = groove_sink_buffer_get(e->sink, &buffer, 1);

        pthread_mutex_lock(&e->encode_head_mutex);

        if (result == GROOVE_BUFFER_END) {
            // flush encoder with empty packets
            while (encode_buffer(encoder, NULL) >= 0) {}
            // then flush format context with empty packets
            while (av_write_frame(e->fmt_ctx, NULL) == 0) {}

            // send trailer
            avio_flush(e->avio);
            av_log(NULL, AV_LOG_INFO, "encoder: writing trailer\n");
            if (av_write_trailer(e->fmt_ctx) < 0) {
                av_log(NULL, AV_LOG_ERROR, "could not write trailer\n");
            }
            avio_flush(e->avio);

            groove_queue_put(e->audioq, end_of_q_sentinel);

            cleanup_avcontext(e);
            init_avcontext(encoder);

            pthread_mutex_unlock(&e->encode_head_mutex);
            continue;
        }

        if (result != GROOVE_BUFFER_YES) {
            pthread_mutex_unlock(&e->encode_head_mutex);
            break;
        }

        if (!e->sent_header) {
            avio_flush(e->avio);

            // copy metadata to format context
            av_dict_free(&e->fmt_ctx->metadata);
            AVDictionaryEntry *tag = NULL;
            while((tag = av_dict_get(e->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
                av_dict_set(&e->fmt_ctx->metadata, tag->key, tag->value, AV_DICT_IGNORE_SUFFIX);
            }

            av_log(NULL, AV_LOG_INFO, "encoder: writing header\n");
            if (avformat_write_header(e->fmt_ctx, NULL) < 0) {
                av_log(NULL, AV_LOG_ERROR, "could not write header\n");
            }
            avio_flush(e->avio);
            e->sent_header = 1;
        }

        encode_buffer(encoder, buffer);
        pthread_mutex_unlock(&e->encode_head_mutex);
        groove_buffer_unref(buffer);
    }
    return NULL;
}