コード例 #1
0
ファイル: main.c プロジェクト: inn1983/cedarx-libs
int main(int argc, char** argv)
{
	int ret;
	pthread_t t0;
	pthread_t t1;
	pthread_t t2;
	pthread_t t3;

	printf("begin cedarx hardware init.\n");
	cedarx_hardware_init(0);
		
	printf("create four decode task.\n");

	pthread_create(&t0, NULL, decode_thread, (void*)0);
	//pthread_create(&t1, NULL, decode_thread, (void*)1);
	//pthread_create(&t2, NULL, decode_thread, (void*)2);
	//pthread_create(&t3, NULL, decode_thread, (void*)3);

	pthread_join(t0, (void**)&ret);
	//pthread_join(t1, (void**)&ret);
	//pthread_join(t2, (void**)&ret);
	//pthread_join(t3, (void**)&ret);
	printf("decode task all finish.\n");
	
	printf("begin cedarx hardware exit.\n");
	cedarx_hardware_exit(0);

	printf("success, quit\n");
	return 0;
}
コード例 #2
0
ファイル: videorecorder.c プロジェクト: commshare/live
int start_video_recorder(Video_Recorder* recorder)
{
	// init base config param
	recorder->base_cfg.codectype = VENC_CODEC_H264;

	recorder->base_cfg.framerate = CAP_FPS;
	recorder->base_cfg.input_width = recorder->width;
	recorder->base_cfg.input_height = recorder->height;
	recorder->base_cfg.dst_width = recorder->width;
	recorder->base_cfg.dst_height = recorder->height;
	recorder->base_cfg.maxKeyInterval = H264_IDR;
	recorder->base_cfg.inputformat = VENC_PIXEL_YUV420; //uv combined
	recorder->base_cfg.targetbitrate = H264_TB;

	// init allocate param
	recorder->alloc_parm.buffernum = 4;

	cedarx_hardware_init(0);
	CLOG("%s", "cedarx_hardware_init");
	CLOG("AWCodec version %s", getCodecVision());

	recorder->waterMark = malloc(sizeof(WaterMark));
	memset(recorder->waterMark, 0x0, sizeof(WaterMark));
	waterMark_init(recorder->waterMark);

	recorder->venc_device = cedarvEncInit();

	recorder->venc_device->ioctrl(recorder->venc_device, VENC_CMD_BASE_CONFIG, &recorder->base_cfg);
	recorder->venc_device->ioctrl(recorder->venc_device, VENC_CMD_ALLOCATE_INPUT_BUFFER, &recorder->alloc_parm);
	recorder->venc_device->ioctrl(recorder->venc_device, VENC_CMD_OPEN, 0);
	recorder->venc_device->ioctrl(recorder->venc_device, VENC_CMD_HEADER_DATA, &recorder->header_data);

	//callback header
	if (recorder->callback)
	{
		recorder->callback(0, recorder->cookie, &recorder->header_data);
	}

	recorder->cameraDevice = CreateCamera(recorder->width, recorder->height);
	CLOG("%s", "create camera ok");

	recorder->cameraDevice->setCameraDatacallback(recorder->cameraDevice, recorder, (void*) &cameraSourceCallback);

	recorder->cameraDevice->startCamera(recorder->cameraDevice);

	/* start encoder */
	recorder->mstart = 1;
	/* create encode thread */
	int err = pthread_create(&recorder->thread_enc_id, NULL, encoderThread, recorder);
	if (err || !recorder->thread_enc_id)
	{
		CLOG("%s", "Create thread_enc_id fail !");
	}
	else
	{
		void* status;
		pthread_join(recorder->thread_enc_id, &status);
	}

	return 1;
}
コード例 #3
0
ファイル: main.c プロジェクト: 379227806/cedarx-libs
int main()
{
	int ret = -1;
	unsigned long long lastTime = 0 ;
	FILE * pEncFile = NULL;
	char saveFile[128] = "h264.buf";
	int bFirstFrame = 1;

	ret = cedarx_hardware_init();
	if (ret < 0)
	{
		printf("cedarx_hardware_init failed\n");
	}

	ret = InitCapture();
	if(ret != 0)
	{
		printf("InitCapture failed\n");
		return -1;
	}

	ret = CedarvEncInit();
	if (ret != 0)
	{
		printf("CedarvEncInit failed\n");
		return -1;
	}

	pEncFile = fopen(saveFile, "wb+");
	if (pEncFile == NULL)
	{
		printf("open %s failed\n", saveFile);
		return -1;
	}

	printf("to stream on\n");
	StartStreaming();

	lastTime = gettimeofday_curr();
	while(1)
	{
		__vbv_data_ctrl_info_t data_info;
		unsigned long long curTime = gettimeofday_curr();

		printf("cru: %lld, last: %lld, %lld\n", curTime, lastTime, curTime - lastTime);
		if ((curTime - lastTime) > 1000*1000*5)	// 30s
		{
			ReleaseFrame(g_cur_id);
			goto EXIT;
		}

		ret = g_pCedarV->encode(g_pCedarV);
		if (ret != 0)
		{
			usleep(10000);
			printf("not encode, ret: %d\n", ret);
		}

		ReleaseFrame(g_cur_id);

		memset(&data_info, 0 , sizeof(__vbv_data_ctrl_info_t));
		ret = g_pCedarV->GetBitStreamInfo(g_pCedarV, &data_info);
		if(ret == 0)
		{
			if (1 == bFirstFrame)
			{
				bFirstFrame = 0;
				fwrite(data_info.privateData, data_info.privateDataLen, 1, pEncFile);
			}

			if (data_info.uSize0 != 0)
			{
				fwrite(data_info.pData0, data_info.uSize0, 1, pEncFile);
			}

			if (data_info.uSize1 != 0)
			{
				fwrite(data_info.pData1, data_info.uSize1, 1, pEncFile);
			}
			printf("v GetBS OK\n");
		}

		g_pCedarV->ReleaseBitStreamInfo(g_pCedarV, data_info.idx);
	}

EXIT:
	DeInitCapture();

	if (pEncFile != NULL)
	{
		fclose(pEncFile);
		pEncFile = 0;
	}

	if (g_pCedarV != NULL)
	{
		g_pCedarV->close(g_pCedarV);
		H264EncExit(g_pCedarV);
		g_pCedarV = NULL;
	}

	cedarx_hardware_exit();

	return 0;
}
コード例 #4
0
// 工作线程
void GrabThread::run()
{
	int ret = -1;
#ifdef ZQSENDER
	ZqSenderTcpCtx *sender = 0;

	ret = zqsnd_open_tcp_server(&sender, 4000, 0);
	if (ret < 0) {
		fprintf(stderr, "can't open zqsender for port %d\n", 4000);
		::exit(-1);
	}
	fprintf(stderr, "start tcp srv port 4000\n");
#endif // zqsender
	
	ret = cedarx_hardware_init();
	if (ret < 0) {
		printf("cedarx_hardware_init failed\n");
		::exit(-1);
	}
	
	ret = InitCapture(_cap);
	if(ret != 0) {
		printf("InitCapture failed\n");
		::exit(-1);
	}

	ret = CedarvEncInit(_cap->width_, _cap->height_, _cap->fps_, _cap->kbits_);
	if (ret != 0) {
		printf("CedarvEncInit failed\n");
		::exit(-1);
	}

	printf("to stream on\n");
	StartStreaming(_cap);

	while (!quit_) {
		__vbv_data_ctrl_info_t data_info;

		// FIXME: encode 需要消耗一定时间,这里不准确
		usleep(1000 * 1000 / _cap->fps_);	// 25fps
	
		ret = g_pCedarV->encode(g_pCedarV);
		if (ret != 0) {
			usleep(10000);
			printf("not encode, ret: %d\n", ret);
			::exit(-1);
		}

		ReleaseFrame(_cap, g_cur_id);

		memset(&data_info, 0 , sizeof(__vbv_data_ctrl_info_t));
		ret = g_pCedarV->GetBitStreamInfo(g_pCedarV, &data_info);
		if(ret == 0) {
			// 有数据
#ifdef ZQSENDER
			send_data(sender, &data_info);
#endif // zqsender

			if (data_info.privateDataLen > 0) {
				if (_cap->priv)
					slice_free(_cap->priv);
				_cap->priv = slice_alloc(data_info.privateData, data_info.privateDataLen, 0);
				fprintf(stderr, "save priv: len=%d\n", data_info.privateDataLen);

				//ost::MutexLock al(_cap->cs_fifo_);
				//_cap->fifo_.push_back(slice_alloc(data_info.privateData, data_info.privateDataLen, 0));
			}

			if (data_info.uSize0 > 0) {
				ost::MutexLock al(_cap->cs_fifo_);

				unsigned char *p = data_info.pData0;

				if (_cap->fifo_.size() > 100 && p[4] == 0x65) {
					fprintf(stderr, "fifo overflow ...\n");
					// 当积累的太多,并且收到关键帧清空
					while (!_cap->fifo_.empty()) {
						slice_t *s = _cap->fifo_.front();
						_cap->fifo_.pop_front();
						slice_free(s);
						fprintf(stderr, "E");
					}
				}

				// 为每个关键帧之前保存 sps. pps
				if (p[4] == 0x65) {
					fprintf(stderr, "patch pps/sps\n");
					_cap->fifo_.push_back(slice_alloc(_cap->priv->data_, _cap->priv->len_,
								data_info.pData0, data_info.uSize0, data_info.pts));
				}
				else {
					_cap->fifo_.push_back(slice_alloc(data_info.pData0, data_info.uSize0, data_info.pts));
				}

				_cap->sem_fifo_.post();
			}

			if (data_info.uSize1 > 0) {
				ost::MutexLock al(_cap->cs_fifo_);

				unsigned char *p = data_info.pData1;

				if (_cap->fifo_.size() > 100 && p[4] == 0x65) {
					fprintf(stderr, "fifo overflow ...\n");
					// 当积累的太多,并且收到关键帧清空
					while (!_cap->fifo_.empty()) {
						slice_t *s = _cap->fifo_.front();
						_cap->fifo_.pop_front();
						slice_free(s);
						fprintf(stderr, "E");
					}
				}

				// 为每个关键帧之前保存 sps. pps
				if (p[4] == 0x65) {
					fprintf(stderr, "patch pps/sps\n");
					_cap->fifo_.push_back(slice_alloc(_cap->priv->data_, _cap->priv->len_,
								data_info.pData1, data_info.uSize1, data_info.pts));
				}
				else {
					_cap->fifo_.push_back(slice_alloc(data_info.pData1, data_info.uSize1, data_info.pts));
				}

				_cap->sem_fifo_.post();
			}
		}

		g_pCedarV->ReleaseBitStreamInfo(g_pCedarV, data_info.idx);
	}
	
	DeInitCapture(_cap);

	if (g_pCedarV != NULL) {
		g_pCedarV->close(g_pCedarV);
		H264EncExit(g_pCedarV);
		g_pCedarV = NULL;
	}

	cedarx_hardware_exit();
}