Revision e810bf7b chunker_player.c

View differences:

chunker_player.c
28 28
#undef main /* Prevents SDL from overriding main() */
29 29
#endif
30 30

  
31
#include <platform.h>
32
#include <microhttpd.h>
33
//#include <AntTweakBar.h>
34

  
31 35
#include "chunker_player.h"
36
#include "codec_definitions.h"
32 37

  
33 38
#define SDL_AUDIO_BUFFER_SIZE 1024
34 39

  
......
36 41
#define AUDIO	1
37 42
#define VIDEO	2
38 43

  
39
//#define DEBUG_AUDIO
40
//#define DEBUG_VIDEO
41
//#define DEBUG_QUEUE
42
//#define DEBUG_SOURCE
44
#define DEBUG_AUDIO
45
#define DEBUG_VIDEO
46
#define DEBUG_QUEUE
47
#define DEBUG_SOURCE
43 48

  
44 49
short int QueueFillingMode=1;
45 50
short int QueueStopped=0;
......
52 57
	SDL_cond *cond;
53 58
	short int queueType;
54 59
	int last_frame_extracted;
60
	int total_lost_frames;
55 61
} PacketQueue;
56 62

  
57 63
typedef struct threadVal {
......
85 91
	q->cond = SDL_CreateCond();
86 92
	QueueFillingMode=1;
87 93
	q->queueType=Type;
88
	q->last_frame_extracted = -1;
94
	q->last_frame_extracted = 0;
95
	q->total_lost_frames = 0;
89 96
}
90 97

  
91 98
int packet_queue_put(PacketQueue *q, AVPacket *pkt) {
......
138 145
			if(q->nb_packets>=QUEUE_FILLING_THRESHOLD && QueueFillingMode) // && q->queueType==AUDIO)
139 146
			{
140 147
				QueueFillingMode=0;
148
#ifdef DEBUG_QUEUE
149
			printf("PUT in Queue: FillingMode set to zero\n");
150
#endif
141 151
				//SDL_CondSignal(q->cond);
142 152
			}
143
			q->last_frame_extracted = pkt->stream_index;
153
//WHYYYYYYYYY			q->last_frame_extracted = pkt->stream_index;
154
//#ifdef DEBUG_QUEUE
155
//			printf("PUT LastFrameExtracted set to %d\n",q->last_frame_extracted);
156
//#endif
144 157
		}
145 158
	}
146 159

  
......
170 183
		if(av==1) {
171 184
			if(pkt1->pkt.size-AudioQueueOffset>dimAudioQ) {
172 185
#ifdef DEBUG_QUEUE
173
				printf("Extract from the same packet ");
186
				printf("  AV=1 and Extract from the same packet\n");
174 187
#endif
175 188
				//av_init_packet(&tmp);
176 189
				q->size -= dimAudioQ;
......
179 192
				memcpy(pkt->data,pkt1->pkt.data+AudioQueueOffset,dimAudioQ);
180 193
				pkt->dts = pkt1->pkt.dts;
181 194
				pkt->pts = pkt1->pkt.pts;
182
				pkt->stream_index = 1;
195
				pkt->stream_index = pkt1->pkt.stream_index;//1;
183 196
				pkt->flags = 1;
184 197
				pkt->pos = -1;
185 198
				pkt->convergence_duration = -1;
......
190 203
				pkt1->pkt.pts += deltaAudioQ;
191 204
				AudioQueueOffset += dimAudioQ;
192 205
#ifdef DEBUG_QUEUE
193
				printf("AudioQueueOffset = %d\n",AudioQueueOffset);
206
				printf("   AudioQueueOffset = %d\n",AudioQueueOffset);
194 207
#endif
195 208
				
196 209
				ret = 1;
210
				//compute lost frame statistics
211
			if(q->last_frame_extracted > 0 && pkt->stream_index > q->last_frame_extracted) {
212
#ifdef DEBUG_QUEUE
213
					printf("  stats: stidx %d last %d tot %d\n", pkt->stream_index, q->last_frame_extracted, q->total_lost_frames);
214
#endif
215
					q->total_lost_frames = q->total_lost_frames + pkt->stream_index - q->last_frame_extracted - 1;
216
				}
217
				//update index of last frame extracted
197 218
				q->last_frame_extracted = pkt->stream_index;
198 219
			}
199 220
			else {
200 221
#ifdef DEBUG_QUEUE
201
				printf("Extract from 2 packets ");
222
				printf("  AV = 1 and Extract from 2 packets\n");
202 223
#endif
203 224
				// Check for loss
204 225
				if(pkt1->next)
205 226
				{
227
#ifdef DEBUG_QUEUE
228
					printf("   we have a next...\n");
229
#endif
206 230
					//av_init_packet(&tmp);
207 231
					pkt->size = dimAudioQ;
208 232
					pkt->dts = pkt1->pkt.dts;
209 233
					pkt->pts = pkt1->pkt.pts;
210
					pkt->stream_index = 1;
234
					pkt->stream_index = pkt1->pkt.stream_index;//1;
211 235
					pkt->flags = 1;
212 236
					pkt->pos = -1;
213 237
					pkt->convergence_duration = -1;
......
216 240
					{
217 241
						SizeToCopy=pkt1->pkt.size-AudioQueueOffset;
218 242
#ifdef DEBUG_QUEUE
219
						printf("SizeToCopy=%d ",SizeToCopy);
243
						printf("      SizeToCopy=%d\n",SizeToCopy);
220 244
#endif
221 245
						memcpy(pkt->data,pkt1->pkt.data+AudioQueueOffset,SizeToCopy);
222 246
						memcpy(pkt->data+SizeToCopy,pkt1->next->pkt.data,(dimAudioQ-SizeToCopy)*sizeof(uint8_t));
......
228 252
					q->last_pkt = NULL;
229 253
				q->nb_packets--;
230 254
				q->size -= SizeToCopy;
255
#ifdef DEBUG_QUEUE
256
				printf("  about to free... ");
257
#endif
231 258
				free(pkt1->pkt.data);
232 259
				av_free(pkt1);
260
#ifdef DEBUG_QUEUE
261
				printf("freeed\n");
262
#endif
233 263
				// Adjust timestamps
234 264
				pkt1 = q->first_pkt;
235 265
				if(pkt1)
......
245 275
					AudioQueueOffset=0;
246 276
				}
247 277
#ifdef DEBUG_QUEUE
248
				printf("AudioQueueOffset = %d\n",AudioQueueOffset);
278
				printf("   AudioQueueOffset = %d\n",AudioQueueOffset);
249 279
#endif
280

  
281
				//compute lost frame statistics
282
			if(q->last_frame_extracted > 0 && pkt->stream_index > q->last_frame_extracted) {
283
#ifdef DEBUG_QUEUE
284
					printf("  stats: stidx %d last %d tot %d\n", pkt->stream_index, q->last_frame_extracted, q->total_lost_frames);
285
#endif
286
					q->total_lost_frames = q->total_lost_frames + pkt->stream_index - q->last_frame_extracted - 1;
287
				}
288
				//update index of last frame extracted
250 289
				q->last_frame_extracted = pkt->stream_index;
251 290
			}
252 291
		}
253 292
		else {
293
#ifdef DEBUG_QUEUE
294
				printf("  AV not 1\n");
295
#endif
254 296
			q->first_pkt = pkt1->next;
255 297
			if (!q->first_pkt)
256 298
				q->last_pkt = NULL;
......
265 307
			pkt->pos = pkt1->pkt.pos;
266 308
			pkt->convergence_duration = pkt1->pkt.convergence_duration;
267 309
			//*pkt = pkt1->pkt;
310
#ifdef DEBUG_QUEUE
311
				printf("  about to free... ");
312
#endif
268 313
			memcpy(pkt->data,pkt1->pkt.data,pkt1->pkt.size);
269 314
			free(pkt1->pkt.data);
270 315
			av_free(pkt1);
316
#ifdef DEBUG_QUEUE
317
				printf("freeed\n");
318
#endif
271 319
			ret = 1;
320
			//compute lost frame statistics
321
			if(q->last_frame_extracted > 0 && pkt->stream_index > q->last_frame_extracted) {
322
#ifdef DEBUG_QUEUE
323
					printf("  stats: stidx %d last %d tot %d\n", pkt->stream_index, q->last_frame_extracted, q->total_lost_frames);
324
#endif
325
					q->total_lost_frames = q->total_lost_frames + pkt->stream_index - q->last_frame_extracted - 1;
326
			}
327
			//update index of last frame extracted
272 328
			q->last_frame_extracted = pkt->stream_index;
273 329
		}
274 330
	}
275
	if(q->nb_packets==0 && q->queueType==AUDIO)
331
#ifdef DEBUG_QUEUE
332
	else {
333
		printf("  pk1 NULLLLLLLLLL!!!!\n");
334
	}
335
#endif
336

  
337
	if(q->nb_packets==0 && q->queueType==AUDIO) {
276 338
		QueueFillingMode=1;
339
#ifdef DEBUG_QUEUE
340
		printf("QUEUE Get FillingMode ON\n");
341
#endif
342
	}
343
#ifdef DEBUG_QUEUE
344
	printf("QUEUE Get LastFrameExtracted = %d\n",q->last_frame_extracted);
345
	printf("QUEUE Get Tot lost frames = %d\n",q->total_lost_frames);
346
#endif
277 347

  
278 348
	SDL_UnlockMutex(q->mutex);
279 349
	return ret;
......
305 375
			FirstTimeAudio = 0;
306 376
			FirstTime = 0;
307 377
#ifdef DEBUG_AUDIO 
308
		 	printf("audio_decode_frame - DeltaTimeAudio=%lld\n",DeltaTimeAudio);
378
		 	printf("audio_decode_frame - DeltaTimeAudio=%lld\n",DeltaTime);
309 379
#endif
310 380
		}
311 381
	}
......
408 478

  
409 479
	pCodecCtx=avcodec_alloc_context();
410 480
        pCodecCtx->codec_type = CODEC_TYPE_VIDEO;
481
#ifdef H264_VIDEO_ENCODER
411 482
        pCodecCtx->codec_id  = CODEC_ID_H264;
412
	pCodecCtx->me_range = 16;
483
        pCodecCtx->me_range = 16;
413 484
        pCodecCtx->max_qdiff = 4;
414 485
        pCodecCtx->qmin = 10;
415 486
        pCodecCtx->qmax = 51;
416 487
        pCodecCtx->qcompress = 0.6;
417
//        pCodecCtx->bit_rate = 400000;
488
#else
489
        pCodecCtx->codec_id  = CODEC_ID_MPEG4;
490
#endif
491
//pCodecCtx->bit_rate = 400000;
418 492
        // resolution must be a multiple of two
419 493
        pCodecCtx->width = tval->width;//176;//352;
420 494
        pCodecCtx->height = tval->height;//144;//288;
421 495
        // frames per second
422
		pCodecCtx->time_base = (AVRational){1,25};
423
        pCodecCtx->gop_size = 10; // emit one intra frame every ten frames
424
        pCodecCtx->max_b_frames=1;
496
//pCodecCtx->time_base = (AVRational){1,25};
497
//pCodecCtx->gop_size = 10; // emit one intra frame every ten frames
498
//pCodecCtx->max_b_frames=1;
425 499
        pCodecCtx->pix_fmt = PIX_FMT_YUV420P;
426 500
        pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
427 501

  
......
524 598

  
525 599
				avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &VideoPkt);
526 600

  
527
       				if(frameFinished) { // it must be true all the time else error
528
	// 				printf("FrameFinished\n");
601
				if(frameFinished) { // it must be true all the time else error
602
#ifdef DEBUG_VIDEO
603
				printf("FrameFinished\n");
604
#endif
529 605
					//SaveFrame(pFrame, pCodecCtx->width, pCodecCtx->height, cont++);
530 606
					//fwrite(pktvideo.data, 1, pktvideo.size, frecon);
531 607

  
532 608
                        		// Lock SDL_yuv_overlay
533 609
	                        	if ( SDL_MUSTLOCK(screen) ) {
534
		                        	if ( SDL_LockSurface(screen) < 0 ) break;
610
#ifdef DEBUG_VIDEO
611
				printf("-1 ");
612
#endif
613

  
614
		                        	if ( SDL_LockSurface(screen) < 0 ) {
615
#ifdef DEBUG_VIDEO
616
				printf("0 ");
617
#endif
618
																break;
619
															}
535 620
                	        	}
621
#ifdef DEBUG_VIDEO
622
				printf("1 ");
623
#endif
536 624
                        		if (SDL_LockYUVOverlay(yuv_overlay) < 0) break;
625
#ifdef DEBUG_VIDEO
626
				printf("2 ");
627
#endif
537 628

  
538 629
	                        	pict.data[0] = yuv_overlay->pixels[0];
539 630
        	                	pict.data[1] = yuv_overlay->pixels[2];
......
542 633
                        		pict.linesize[0] = yuv_overlay->pitches[0];
543 634
	                        	pict.linesize[1] = yuv_overlay->pitches[2];
544 635
        	                	pict.linesize[2] = yuv_overlay->pitches[1];
636
#ifdef DEBUG_VIDEO
637
				printf("3 ");
638
#endif
639

  
545 640
                	        	img_convert_ctx = sws_getContext(tval->width,tval->height,PIX_FMT_YUV420P,tval->width,tval->height,PIX_FMT_YUV420P,SWS_BICUBIC,NULL,NULL,NULL);
641
#ifdef DEBUG_VIDEO
642
				printf("4 ");
643
#endif
546 644

  
547 645
	                        	if(img_convert_ctx==NULL) {
548 646
		                        	fprintf(stderr,"Cannot initialize the conversion context!\n");
549 647
        		                        exit(1);
550 648
                        		}
551 649
	                        	sws_scale(img_convert_ctx,pFrame->data,pFrame->linesize,0,tval->height,pict.data,pict.linesize);
650
#ifdef DEBUG_VIDEO
651
				printf("5 ");
652
#endif
552 653

  
553 654
		                        // let's draw the data (*yuv[3]) on a SDL screen (*screen)
554 655
        	                	if ( SDL_MUSTLOCK(screen) ) {
555 656
			                        SDL_UnlockSurface(screen);
556 657
                        		}
658
#ifdef DEBUG_VIDEO
659
				printf("6 ");
660
#endif
661

  
557 662
                        		SDL_UnlockYUVOverlay(yuv_overlay);
558 663

  
559 664
	                        	// Show, baby, show!
560 665
        	                	SDL_DisplayYUVOverlay(yuv_overlay, &rect);
666
#ifdef DEBUG_VIDEO
667
				printf("7\n");
668
#endif
561 669
			
562 670
				}
563 671
			}
......
700 808
	int frameFinished, len_audio;
701 809
	int numBytes,outbuf_audio_size,audio_size;
702 810

  
703
	int dir_entries,y;
811
	int y;
704 812
	
705 813
	uint8_t *outbuf,*outbuf_audio;
706 814
	uint8_t *outbuf_audi_audio;
......
717 825
	//SDL_mutex   *lock;
718 826
	SDL_AudioSpec wanted_spec, spec;
719 827
	
720
		
828
	struct MHD_Daemon *daemon = NULL;	
829

  
721 830
	char buf[1024],outfile[1024], basereadfile[1024],readfile[1024];
722 831
	FILE *fp;	
723
	struct dirent **namelist;
832
//	struct dirent **namelist;
724 833
	int width,height,asample_rate,achannels;
725 834
	//double framerate;
726 835

  
727

  
836
//  TwBar *bar;
728 837

  
729 838
	ThreadVal *tval;
730 839
	tval = (ThreadVal *)malloc(sizeof(ThreadVal));
......
757 866
	//aCodecCtx->bit_rate = 64000;
758 867
	aCodecCtx->sample_rate = asample_rate;
759 868
	aCodecCtx->channels = achannels;
869
#ifdef MP3_AUDIO_ENCODER
760 870
	aCodec = avcodec_find_decoder(CODEC_ID_MP3); // codec audio
761
	printf("%d %d\n",CODEC_ID_MP2,CODEC_ID_MP3);
871
#else
872
	aCodec = avcodec_find_decoder(CODEC_ID_MP2);
873
#endif
874
	printf("MP2 codec id %d MP3 codec id %d\n",CODEC_ID_MP2,CODEC_ID_MP3);
762 875
	if(!aCodec) {
763 876
		printf("Codec not found!\n");
764 877
		return -1;
......
767 880
		fprintf(stderr, "could not open codec\n");
768 881
		return -1; // Could not open codec
769 882
	}
770
	printf("Codecid: %d %d",aCodecCtx->codec_id,aCodecCtx->sample_rate);
771
	printf("samplerate: %d",aCodecCtx->sample_rate);
772
	printf("channels: %d",aCodecCtx->channels);
883
	printf("using audio Codecid: %d ",aCodecCtx->codec_id);
884
	printf("samplerate: %d ",aCodecCtx->sample_rate);
885
	printf("channels: %d\n",aCodecCtx->channels);
773 886
	wanted_spec.freq = aCodecCtx->sample_rate;
774 887
	wanted_spec.format = AUDIO_S16SYS;
775 888
	wanted_spec.channels = aCodecCtx->channels;
......
817 930
		exit(1);
818 931
	}
819 932

  
933
/*
934
  // Initialize AntTweakBar
935
  TwInit(TW_OPENGL, NULL);
936
  // Tell the window size to AntTweakBar
937
  TwWindowSize(width, height);
938
  // Create a tweak bar
939
  bar = TwNewBar("TweakBar");
940
  // Add 'width' and 'height' to 'bar': they are read-only (RO) variables of type TW_TYPE_INT32.
941
  TwAddVarRO(bar, "Width", TW_TYPE_INT32, &width, " label='Wnd width' help='Width of the graphics window (in pixels)' ");
942
*/
943

  
820 944
	yuv_overlay = SDL_CreateYUVOverlay(width, height,SDL_YV12_OVERLAY, screen);
821 945

  
822 946
	if ( yuv_overlay == NULL ) {
......
852 976
	//lock = SDL_CreateMutex();
853 977
	//SDL_WaitThread(exit_thread2,NULL);
854 978

  
855
#ifdef DEBUG_SOURCE
856
	printf("SOURCE: Num entries=%d\n",dir_entries);
857
#endif
858 979

  
859 980

  
860
	initChunkPuller();
981
	daemon = initChunkPuller();
861 982

  
862 983

  
863 984

  
......
880 1001
	}
881 1002
	// Stop audio&video playback
882 1003

  
883
	
1004
printf("1\n");	
884 1005
	SDL_WaitThread(video_thread,NULL);
1006
printf("2\n");	
885 1007
	SDL_PauseAudio(1);
1008
printf("3\n");	
886 1009
	SDL_CloseAudio();
1010
printf("4\n");	
887 1011
	SDL_Quit();
888 1012

  
889 1013
	//SDL_DestroyMutex(lock);
1014
printf("5\n");	
890 1015
	av_free(aCodecCtx);
1016
printf("6\n");	
891 1017
	free(AudioPkt.data);
1018
printf("7\n");	
892 1019
	free(VideoPkt.data);
893
	free(namelist);
1020
//	free(namelist);
1021
printf("8\n");	
894 1022
  free(outbuf_audio);
895 1023

  
896
	finalizeChunkPuller();
1024
printf("9\n");	
1025
	finalizeChunkPuller(daemon);
1026
printf("10\n");	
897 1027

  
898 1028
	return 0;
899 1029
}
......
908 1038
	Frame *frame=NULL;
909 1039
	AVPacket packet, packetaudio;
910 1040

  
911
	uint8_t audio_bufQ[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
1041
	uint8_t *video_bufQ = NULL;
1042
	uint16_t *audio_bufQ = NULL;
1043
	//uint8_t audio_bufQ[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
912 1044
	int16_t *dataQ;
913 1045
	int data_sizeQ;
914 1046
	int lenQ;
915 1047
	int sizeFrame = 0;
916 1048
	sizeFrame = 3*sizeof(int)+sizeof(struct timeval);
917 1049

  
1050
	audio_bufQ = (uint16_t *)av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
918 1051
	gchunk = (Chunk *)malloc(sizeof(Chunk));
919 1052
	if(!gchunk) {
920
		printf("Memory error!\n");
1053
		printf("Memory error in gchunk!\n");
921 1054
		return PLAYER_FAIL_RETURN;
922 1055
	}
923 1056

  
924 1057
  decodeChunk(gchunk, block, block_size);
925 1058

  
926 1059
	echunk = grapesChunkToExternalChunk(gchunk);
1060
  if(echunk == NULL) {
1061
		printf("Memory error in echunk!\n");
1062
    free(gchunk->attributes);
1063
    free(gchunk->data);
1064
    free(gchunk);
1065
		return PLAYER_FAIL_RETURN;
1066
  }
1067
  free(gchunk->attributes);
927 1068
  free(gchunk);
928 1069

  
929 1070
	frame = (Frame *)malloc(sizeof(Frame));
......
951 1092

  
952 1093
			if(frame->type!=5) { // video frame
953 1094
				av_init_packet(&packet);
954
				packet.data = buffer;
955
				packet.size = frame->size;
956
				packet.pts = frame->timestamp.tv_sec*(unsigned long long)1000+frame->timestamp.tv_usec;
957
				packet.dts = frame->timestamp.tv_sec*(unsigned long long)1000+frame->timestamp.tv_usec;
958
				packet.stream_index = frame->number; // use of stream_index for number frame
959
				//packet.duration = frame->timestamp.tv_sec;
960
				packet_queue_put(&videoq,&packet);
1095
				video_bufQ = (uint8_t *)malloc(frame->size); //this gets freed at the time of packet_queue_get
1096
				if(video_bufQ) {
1097
					memcpy(video_bufQ, buffer, frame->size);
1098
					packet.data = video_bufQ;
1099
					packet.size = frame->size;
1100
					packet.pts = frame->timestamp.tv_sec*(unsigned long long)1000+frame->timestamp.tv_usec;
1101
					packet.dts = frame->timestamp.tv_sec*(unsigned long long)1000+frame->timestamp.tv_usec;
1102
					packet.stream_index = frame->number; // use of stream_index for number frame
1103
					//packet.duration = frame->timestamp.tv_sec;
1104
					packet_queue_put(&videoq,&packet);
961 1105
#ifdef DEBUG_SOURCE
962
				printf("SOURCE: Insert video in queue pts=%lld %d %d sindex:%d\n",packet.pts,(int)frame->timestamp.tv_sec,(int)frame->timestamp.tv_usec,packet.stream_index);
1106
					printf("SOURCE: Insert video in queue pts=%lld %d %d sindex:%d\n",packet.pts,(int)frame->timestamp.tv_sec,(int)frame->timestamp.tv_usec,packet.stream_index);
963 1107
#endif
1108
				}
964 1109
			}
965 1110
			else { // audio frame
966 1111
				av_init_packet(&packetaudio);
......
977 1122

  
978 1123
				// insert the audio frame into the queue
979 1124
				data_sizeQ = AVCODEC_MAX_AUDIO_FRAME_SIZE;
980
                        	lenQ = avcodec_decode_audio3(aCodecCtx, (int16_t *)audio_bufQ, &data_sizeQ, &packetaudio);
1125
        lenQ = avcodec_decode_audio3(aCodecCtx, (int16_t *)audio_bufQ, &data_sizeQ, &packetaudio);
981 1126
				if(lenQ>0)
982 1127
				{
983 1128
					// for freeing there is some memory still in tempdata to be freed
984
					dataQ = (int16_t *)malloc(data_sizeQ);
1129
					dataQ = (int16_t *)malloc(data_sizeQ); //this gets freed at the time of packet_queue_get
985 1130
					if(dataQ)
986 1131
					{
987 1132
						memcpy(dataQ,audio_bufQ,data_sizeQ);
......
1015 1160
		}
1016 1161
		ProcessKeys();
1017 1162
*/
1163
  free(echunk->data);
1018 1164
	free(echunk);
1019 1165
	free(frame);
1166
  av_free(audio_bufQ);
1020 1167
}

Also available in: Unified diff