Revision 31e4e8ba chunker_player/player_core.c

View differences:

chunker_player/player_core.c
1 1
#include "player_defines.h"
2 2
#include "chunker_player.h"
3
// #include "player_commons.h"
3
#include "player_gui.h"
4 4
#include "player_core.h"
5 5
#include <assert.h>
6 6

  
7
void SaveFrame(AVFrame *pFrame, int width, int height);
8
int VideoCallback(void *valthread);
9
void AudioCallback(void *userdata, Uint8 *stream, int len);
10
void UpdateQueueStats(PacketQueue *q, int packet_index);
11
void UpdateLossTraces(int type, int first_lost, int n_lost);
12

  
7 13
void PacketQueueInit(PacketQueue *q, short int Type)
8 14
{
9 15
#ifdef DEBUG_QUEUE
......
51 57

  
52 58
	QueueFillingMode=1;
53 59
	q->last_frame_extracted = -1;
54
	q->total_lost_frames = 0;
60
	
61
	// on queue reset do not reset loss count
62
	// (loss count reset is done on queue init, ie channel switch)
63
	// q->total_lost_frames = 0;
64
	q->density=0.0;
55 65
	q->first_pkt= NULL;
56 66
	//q->last_pkt = NULL;
57 67
	q->nb_packets = 0;
58 68
	q->size = 0;
59
	q->density= 0.0;
60 69
	FirstTime = 1;
61 70
	FirstTimeAudio = 1;
62 71
#ifdef DEBUG_QUEUE
......
178 187
	img_convert_ctx = NULL;
179 188
	
180 189
	SDL_AudioSpec wanted_spec;
181
	
182
	AVFormatContext *pFormatCtx;
183 190
	AVCodec         *aCodec;
184 191
	
185 192
	memset(&VideoCallbackThreadParams, 0, sizeof(ThreadVal));
......
262 269
	InitRect->w = OverlayRect.w;
263 270
	InitRect->h = OverlayRect.h;
264 271
	
272
	char audio_stats[255], video_stats[255];
273
	sprintf(audio_stats, "waiting for incoming audio packets...");
274
	sprintf(video_stats, "waiting for incoming video packets...");
275
	ChunkerPlayerGUI_SetStatsText(audio_stats, video_stats);
276
	
265 277
	return 0;
266 278
}
267 279

  
......
356 368
void UpdateQueueStats(PacketQueue *q, int packet_index)
357 369
{
358 370
	static int N = 50;
359
	static int last_print;
371
	static int last_print = 0;
360 372
	
361 373
	if(q == NULL)
362 374
		return;
......
365 377
	if(q->last_pkt == NULL)
366 378
		return;
367 379
	
368
	// assert(q != NULL);
369
	// assert(q->last_pkt != NULL);
370
	// assert(q->first_pkt != NULL);
371
	
372 380
	if(q->last_pkt->pkt.stream_index > q->first_pkt->pkt.stream_index)
381
	{
373 382
		q->density = (double)q->nb_packets / (double)(q->last_pkt->pkt.stream_index - q->first_pkt->pkt.stream_index) * 100.0;
383
	}
374 384
	
375 385
#ifdef DEBUG_STATS
376 386
	if(q->queueType == AUDIO)
......
379 389
		printf("STATS: VIDEO QUEUE DENSITY percentage %f\n", q->density);
380 390
#endif
381 391
	
392
	if(!last_print)
393
		last_print = time(NULL);
394
		
395
	int now = time(NULL);
396
	int lost_frames = 0;
397
	
382 398
	if(packet_index != -1)
383 399
	{
384 400
		double percentage = 0.0;	
385 401
		//compute lost frame statistics
386 402
		if(q->last_frame_extracted > 0 && packet_index > q->last_frame_extracted)
387 403
		{
388
			int lost_frames = packet_index - q->last_frame_extracted - 1;
404
			lost_frames = packet_index - q->last_frame_extracted - 1;
389 405
			q->total_lost_frames += lost_frames ;
390 406
			percentage = (double)q->total_lost_frames / (double)q->last_frame_extracted * 100.0;
391 407
			
392
			q->loss_history[q->history_index] = lost_frames;
408
			q->instant_lost_frames += lost_frames;
409
			
410
			//save a trace of lost frames to file
411
			//we have lost "lost_frames" frames starting from the last extracted (excluded of course)
412
			UpdateLossTraces(q->queueType, q->last_frame_extracted+1, lost_frames);
413
			
414
			/**q->loss_history[q->history_index] = lost_frames;
393 415
			q->history_index = (q->history_index+1)%N;
394 416
			
395 417
			int i;
396 418
			q->instant_lost_frames = 0;
397 419
			for(i=0; i<N; i++)
398
				q->instant_lost_frames += q->loss_history[i];
420
				q->instant_lost_frames += q->loss_history[i];*/
399 421
			
400 422
#ifdef DEBUG_STATS
401 423
			if(q->queueType == AUDIO)
......
406 428
		}
407 429
	}
408 430
	
409
	int now = time(NULL);
410
	if((now-last_print) > 1)
431
	if((now-last_print) >= 1)
411 432
	{
412 433
		char stats[255];
413
		sprintf(stats, "queue density = %d, lost frames (50 frames window) = %d", (int)q->density, q->instant_lost_frames);
414
		ChunkerPlayerGUI_SetStatsText(stats);
434
		if(q->queueType == AUDIO)
435
		{
436
			sprintf(stats, "[AUDIO] queue density: %d --- lost_frames/sec: %d --- total_lost_frames: %d", (int)q->density, q->instant_lost_frames, q->total_lost_frames);
437
			ChunkerPlayerGUI_SetStatsText(stats, NULL);
438
		}
439
		else if(q->queueType == VIDEO)
440
		{
441
			sprintf(stats, "[VIDEO] queue density: %d --- lost_frames/sec: %d --- total_lost_frames: %d", (int)q->density, q->instant_lost_frames, q->total_lost_frames);
442
			ChunkerPlayerGUI_SetStatsText(NULL, stats);
443
		}
444
		
415 445
		last_print = now;
446
		q->instant_lost_frames = 0;
416 447
	}
417 448
}
418 449

  
450
void UpdateLossTraces(int type, int first_lost, int n_lost)
451
{
452
	FILE *lossFile;
453
	int i;
454

  
455
	// Open loss traces file
456
	char filename[255];
457
	if(type == AUDIO)
458
		sprintf(filename, "audio_%s", LossTracesFilename);
459
	else
460
		sprintf(filename, "video_%s", LossTracesFilename);
461

  
462
	lossFile=fopen(filename, "a");
463
	if(lossFile==NULL) {
464
		printf("STATS: UNABLE TO OPEN Loss FILE: %s\n", filename);
465
		return;
466
	}
467

  
468
	for(i=0; i<n_lost; i++) {
469
		fprintf(lossFile, "%d\n", first_lost+i);
470
	}
471

  
472
	fclose(lossFile);
473
}
474

  
419 475
int PacketQueueGet(PacketQueue *q, AVPacket *pkt, short int av) {
420 476
	//AVPacket tmp;
421 477
	AVPacketList *pkt1 = NULL;
......
535 591
				}
536 592
				else {
537 593
					AudioQueueOffset=0;
538
#ifdef DEBUG_AUDIO_BUFFER
539
					printf("0: idx %d    \taqo %d    \tstc %d    \taqe %f    \tpsz %d\n", pkt1->pkt.stream_index, AudioQueueOffset, SizeToCopy, deltaAudioQError, pkt1->pkt.size);
540
#endif
541 594
				}
542 595
#ifdef DEBUG_QUEUE
543 596
				printf("   deltaAudioQError = %f\n",deltaAudioQError);
......
692 745
	AVCodecContext  *pCodecCtx;
693 746
	AVCodec         *pCodec;
694 747
	AVFrame         *pFrame;
695
	AVPacket        packet;
696 748
	int frameFinished;
697
	int countexit;
698 749
	AVPicture pict;
699
	//FILE *frecon;
700
	SDL_Event event;
701 750
	long long Now;
702 751
	short int SkipVideo, DecodeVideo;
703 752

  
......
994 1043
	free(VideoPkt.data);
995 1044
	free(outbuf_audio);
996 1045
	free(InitRect);
997
	
998
	return 0;
999 1046
}
1000 1047

  
1001
int ChunkerPlayerCore_VideoEnded()
1048
int ChunkerPlayerCore_AudioEnded()
1002 1049
{
1003 1050
	return (audioq.nb_packets==0 && audioq.last_frame_extracted>0);
1004 1051
}
......
1018 1065
	ExternalChunk *echunk = NULL;
1019 1066
	int decoded_size = -1;
1020 1067
	uint8_t *tempdata, *buffer;
1021
	int i, j;
1068
	int j;
1022 1069
	Frame *frame = NULL;
1023 1070
	AVPacket packet, packetaudio;
1024 1071

  
......
1151 1198
		free(frame);
1152 1199
	if(audio_bufQ)
1153 1200
		av_free(audio_bufQ);
1201
		
1202
	return PLAYER_OK_RETURN;
1154 1203
}
1155 1204

  
1156 1205
void ChunkerPlayerCore_SetupOverlay(int width, int height)
1157 1206
{
1207
	// if(!MainScreen && !SilentMode)
1208
	// {
1209
		// printf("Cannot find main screen, exiting...\n");
1210
		// exit(1);
1211
	// }
1212
	
1213
	if(SilentMode)
1214
		return;
1215
		
1158 1216
	SDL_LockMutex(OverlayMutex);
1159 1217
	if(YUVOverlay != NULL)
1160 1218
	{

Also available in: Unified diff