Statistics
| Branch: | Revision:

chunker-player / chunker_player / player_core.c @ 2f5ae7bb

History | View | Annotate | Download (44.2 KB)

1
/*
2
 *  Copyright (c) 2009-2011 Carmelo Daniele, Dario Marchese, Diego Reforgiato, Giuseppe Tropea
3
 *  developed for the Napa-Wine EU project. See www.napa-wine.eu
4
 *
5
 *  This is free software; see lgpl-2.1.txt
6
 */
7

    
8
#include "player_defines.h"
9
#include "chunker_player.h"
10
#include "player_gui.h"
11
#include "player_core.h"
12
#include "player_stats.h"
13
#include <assert.h>
14
#include <time.h>
15

    
16
static char *video_codec;
17

    
18
void SaveFrame(AVFrame *pFrame, int width, int height);
19
int VideoCallback(void *valthread);
20
int CollectStatisticsThread(void *params);
21
void AudioCallback(void *userdata, Uint8 *stream, int len);
22
void PacketQueueClearStats(PacketQueue *q);
23
void ChunkerPlayerCore_Pause();
24

    
25
//int lastCheckedVideoFrame = -1;
26
long int last_video_frame_extracted = -1;
27

    
28
int timeval_subtract(struct timeval* x, struct timeval* y, struct timeval* result)
29
{
30
  // Perform the carry for the later subtraction by updating y.
31
  if (x->tv_usec < y->tv_usec)
32
  {
33
    int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
34
    y->tv_usec -= 1000000 * nsec;
35
    y->tv_sec += nsec;
36
  }
37
  if (x->tv_usec - y->tv_usec > 1000000)
38
  {
39
    int nsec = (x->tv_usec - y->tv_usec) / 1000000;
40
    y->tv_usec += 1000000 * nsec;
41
    y->tv_sec -= nsec;
42
  }
43

    
44
  // Compute the time remaining to wait. tv_usec is certainly positive.
45
  result->tv_sec = x->tv_sec - y->tv_sec;
46
  result->tv_usec = x->tv_usec - y->tv_usec;
47

    
48
  // Return 1 if result is negative.
49
  return x->tv_sec < y->tv_sec;
50
}
51

    
52

    
53
void PacketQueueInit(PacketQueue *q, short int Type)
54
{
55
#ifdef DEBUG_QUEUE
56
        printf("QUEUE: INIT BEGIN: NPackets=%d Type=%s\n", q->nb_packets, (q->queueType==AUDIO) ? "AUDIO" : "VIDEO");
57
#endif
58
        memset(q,0,sizeof(PacketQueue));
59
        q->mutex = SDL_CreateMutex();
60
        QueueFillingMode=1;
61
        q->queueType=Type;
62
        q->last_frame_extracted = -1;
63
        q->first_pkt= NULL;
64
        //q->last_pkt = NULL;
65
        q->nb_packets = 0;
66
        q->size = 0;
67
        q->density= 0.0;
68
        FirstTime = 1;
69
        FirstTimeAudio = 1;
70
        //init up statistics
71
        
72
        q->PacketHistory.Mutex = SDL_CreateMutex();
73
        PacketQueueClearStats(q);
74
        
75
#ifdef DEBUG_QUEUE
76
        printf("QUEUE: INIT END: NPackets=%d Type=%s\n", q->nb_packets, (q->queueType==AUDIO) ? "AUDIO" : "VIDEO");
77
#endif
78
}
79

    
80
void PacketQueueReset(PacketQueue *q)
81
{
82
        AVPacketList *tmp,*tmp1;
83
#ifdef DEBUG_QUEUE
84
        printf("QUEUE: RESET BEGIN: NPackets=%d Type=%s LastExtr=%d\n", q->nb_packets, (q->queueType==AUDIO) ? "AUDIO" : "VIDEO", q->last_frame_extracted);
85
#endif
86
        SDL_LockMutex(q->mutex);
87

    
88
        tmp = q->first_pkt;
89
        while(tmp) {
90
                tmp1 = tmp;
91
                tmp = tmp->next;
92
                av_free_packet(&(tmp1->pkt));
93
                av_free(tmp1);
94
#ifdef DEBUG_QUEUE
95
                printf("F ");
96
#endif
97
                q->PacketHistory.LostCount++;
98
        }
99
#ifdef DEBUG_QUEUE
100
        printf("\n");
101
#endif
102

    
103
        QueueFillingMode=1;
104
        q->last_frame_extracted = -1;
105
        
106
        // on queue reset do not reset loss count
107
        // (loss count reset is done on queue init, ie channel switch)
108
        q->density=0.0;
109
        q->first_pkt= NULL;
110
        //q->last_pkt = NULL;
111
        q->nb_packets = 0;
112
        q->size = 0;
113
        FirstTime = 1;
114
        FirstTimeAudio = 1;
115
        //clean up statistics
116
        PacketQueueClearStats(q);
117
#ifdef DEBUG_QUEUE
118
        printf("QUEUE: RESET END: NPackets=%d Type=%s LastExtr=%d\n", q->nb_packets, (q->queueType==AUDIO) ? "AUDIO" : "VIDEO", q->last_frame_extracted);
119
#endif
120
        SDL_UnlockMutex(q->mutex);
121
}
122

    
123
void PacketQueueClearStats(PacketQueue *q)
124
{
125
        sprintf(q->stats_message, "%s", "\n");
126
        int i;
127
        memset((void*)q->PacketHistory.History, 0, sizeof(SHistoryElement)*QUEUE_HISTORY_SIZE);
128
        for(i=0; i<QUEUE_HISTORY_SIZE; i++)
129
        {
130
                q->PacketHistory.History[i].Statistics.LastIFrameDistance = -1;
131
                q->PacketHistory.History[i].Status = -1;
132
        }
133
        q->PacketHistory.Index = q->PacketHistory.LogIndex = 0;
134
        q->PacketHistory.Index = q->PacketHistory.QoEIndex = 0;
135
        q->PacketHistory.LostCount = q->PacketHistory.PlayedCount = q->PacketHistory.SkipCount = 0;
136
}
137

    
138
int ChunkerPlayerCore_PacketQueuePut(PacketQueue *q, AVPacket *pkt)
139
{
140
        //~ printf("\tSTREAM_INDEX=%d\n", pkt->stream_index);
141
        short int skip = 0;
142
        AVPacketList *pkt1, *tmp, *prevtmp;
143
        int res = 0;
144

    
145
        if(q->nb_packets > queue_filling_threshold*QUEUE_MAX_GROW_FACTOR) {
146
#ifdef DEBUG_QUEUE
147
                printf("QUEUE: PUT i have TOO MANY packets %d Type=%s, RESETTING\n", q->nb_packets, (q->queueType==AUDIO) ? "AUDIO" : "VIDEO");
148
#endif
149
                PacketQueueReset(q);
150
        }
151

    
152
        //make a copy of the incoming packet
153
        if(av_dup_packet(pkt) < 0) {
154
#ifdef DEBUG_QUEUE
155
                printf("QUEUE: PUT in Queue cannot duplicate in packet        : NPackets=%d Type=%s\n",q->nb_packets, (q->queueType==AUDIO) ? "AUDIO" : "VIDEO");
156
#endif
157
                return -1;
158
        }
159
        pkt1 = av_malloc(sizeof(AVPacketList));
160

    
161
        if(!pkt1) {
162
                av_free_packet(pkt);
163
                return -1;
164
        }
165
        pkt1->pkt = *pkt;
166
        pkt1->next = NULL;
167
        
168
        static time_t last_auto_switch = 0;
169

    
170
        // file streaming loop detected => re-tune channel and start grabbing statistics
171
        if(
172
                (pkt->stream_index < last_video_frame_extracted)
173
                && (pkt->stream_index <= RESTART_FRAME_NUMBER_THRESHOLD)
174
                && ((time(NULL) - last_auto_switch) > 10)
175
        )
176
        {
177
                last_auto_switch = time(NULL);
178
                SDL_LockMutex(q->mutex);
179
                ReTune(&(Channels[SelectedChannel]));
180
                SDL_UnlockMutex(q->mutex);
181
        }
182

    
183
        else
184
        {
185
                SDL_LockMutex(q->mutex);
186

    
187
                // INSERTION SORT ALGORITHM
188
                // before inserting pkt, check if pkt.stream_index is <= current_extracted_frame.
189
                if(pkt->stream_index > q->last_frame_extracted)
190
                {
191
                        // either checking starting from the first_pkt or needed other struct like AVPacketList with next and prev....
192
                        //if (!q->last_pkt)
193
                        if(!q->first_pkt) {
194
                                q->first_pkt = pkt1;
195
                                q->last_pkt = pkt1;
196
                        }
197
                        else if(pkt->stream_index < q->first_pkt->pkt.stream_index) {
198
                                //the packet that has arrived is earlier than the first we got some time ago!
199
                                //we need to put it at the head of the queue
200
                                pkt1->next = q->first_pkt;
201
                                q->first_pkt = pkt1;
202
                        }
203
                        else {
204
                                tmp = q->first_pkt;
205
                                while(tmp->pkt.stream_index < pkt->stream_index) {
206
                                        prevtmp = tmp;
207
                                        tmp = tmp->next;
208

    
209
                                        if(!tmp) {
210
                                                break;
211
                                        }
212
                                }
213
                                if(tmp && tmp->pkt.stream_index == pkt->stream_index) {
214
                                        //we already have a frame with that index
215
                                        skip = 1;
216
#ifdef DEBUG_QUEUE
217
                                        printf("%s QUEUE: PUT: we already have frame with index %d, skipping\n", ((q->queueType == AUDIO) ? "AUDIO" : "VIDEO"), pkt->stream_index);
218
#endif
219
                                }
220
                                else {
221
                                        prevtmp->next = pkt1;
222
                                        pkt1->next = tmp;
223
                                        if(pkt1->next == NULL)
224
                                                q->last_pkt = pkt1;
225
                                }
226
                                //q->last_pkt->next = pkt1; // It was uncommented when not insertion sort
227
                        }
228
                        if(skip == 0) {
229
                                //q->last_pkt = pkt1;
230
                                q->nb_packets++;
231
                                q->size += pkt1->pkt.size;
232
                                if(q->nb_packets>=queue_filling_threshold && QueueFillingMode) // && q->queueType==AUDIO)
233
                                {
234
                                        QueueFillingMode=0;
235
#ifdef DEBUG_QUEUE
236
                                        printf("QUEUE: PUT: FillingMode set to zero\n");
237
#endif
238
                                }
239
                        }
240
                }
241
                else {
242
                        av_free_packet(&pkt1->pkt);
243
                        av_free(pkt1);
244
#ifdef DEBUG_QUEUE
245
                        printf("QUEUE: PUT: NOT inserting because index %d <= last extracted %d\n", pkt->stream_index, q->last_frame_extracted);
246
#endif
247
                        res = 1;
248
                }
249
                SDL_UnlockMutex(q->mutex);
250
        }
251

    
252
        return res;
253
}
254

    
255
int ChunkerPlayerCore_InitCodecs(char *v_codec, int width, int height, char *audio_codec, int sample_rate, short int audio_channels)
256
{
257
        video_codec = v_codec;
258

    
259
        // some initializations
260
        QueueStopped = 0;
261
        AudioQueueOffset=0;
262
        AVPlaying = 0;
263
        GotSigInt = 0;
264
        FirstTimeAudio=1;
265
        FirstTime = 1;
266
        deltaAudioQError=0;
267
        InitRect = NULL;
268
        img_convert_ctx = NULL;
269
        
270
        SDL_AudioSpec wanted_spec;
271
        AVCodec         *aCodec;
272
        
273
        memset(&VideoCallbackThreadParams, 0, sizeof(ThreadVal));
274
        
275
        VideoCallbackThreadParams.width = width;
276
        VideoCallbackThreadParams.height = height;
277

    
278
        // Register all formats and codecs
279
        avcodec_init();
280
        av_register_all();
281

    
282
        aCodecCtx = avcodec_alloc_context();
283
        //aCodecCtx->bit_rate = 64000;
284
        aCodecCtx->sample_rate = sample_rate;
285
        aCodecCtx->channels = audio_channels;
286
        aCodec = avcodec_find_decoder_by_name(audio_codec);
287
        if(!aCodec) {
288
                printf("Codec not found!\n");
289
                return -1;
290
        }
291
        if(avcodec_open(aCodecCtx, aCodec)<0) {
292
                fprintf(stderr, "could not open codec\n");
293
                return -1; // Could not open codec
294
        }
295
        printf("using audio Codecid: %d ",aCodecCtx->codec_id);
296
        printf("samplerate: %d ",aCodecCtx->sample_rate);
297
        printf("channels: %d\n",aCodecCtx->channels);
298
        CurrentAudioFreq = wanted_spec.freq = aCodecCtx->sample_rate;
299
        wanted_spec.format = AUDIO_S16SYS;
300
        wanted_spec.channels = aCodecCtx->channels;
301
        wanted_spec.silence = 0;
302
        CurrentAudioSamples = wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
303
        wanted_spec.callback = AudioCallback;
304
        wanted_spec.userdata = aCodecCtx;
305
        if(SDL_OpenAudio(&wanted_spec,&AudioSpecification)<0)
306
        {
307
                fprintf(stderr,"SDL_OpenAudio: %s\n", SDL_GetError());
308
                return -1;
309
        }
310
        dimAudioQ = AudioSpecification.size;
311
        deltaAudioQ = (float)((float)AudioSpecification.samples)*1000/AudioSpecification.freq;
312

    
313
#ifdef DEBUG_AUDIO
314
        printf("freq:%d\n",AudioSpecification.freq);
315
        printf("format:%d\n",AudioSpecification.format);
316
        printf("channels:%d\n",AudioSpecification.channels);
317
        printf("silence:%d\n",AudioSpecification.silence);
318
        printf("samples:%d\n",AudioSpecification.samples);
319
        printf("size:%d\n",AudioSpecification.size);
320
        printf("deltaAudioQ: %f\n",deltaAudioQ);
321
#endif
322

    
323
        outbuf_audio = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
324

    
325
        //initialize the audio and the video queues
326
        PacketQueueInit(&audioq, AUDIO);
327
        PacketQueueInit(&videoq, VIDEO);
328
        
329
        // Init audio and video buffers
330
        av_init_packet(&AudioPkt);
331
        av_init_packet(&VideoPkt);
332
        //printf("AVCODEC_MAX_AUDIO_FRAME_SIZE=%d\n", AVCODEC_MAX_AUDIO_FRAME_SIZE);
333
        AudioPkt.data=(uint8_t *)malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
334
        if(!AudioPkt.data) return 1;
335
        VideoPkt.data=(uint8_t *)malloc(width*height*3/2);
336
        if(!VideoPkt.data) return 1;
337
        
338
        InitRect = (SDL_Rect*) malloc(sizeof(SDL_Rect));
339
        if(!InitRect)
340
        {
341
                printf("Memory error!!!\n");
342
                return -1;
343
        }
344
        InitRect->x = OverlayRect.x;
345
        InitRect->y = OverlayRect.y;
346
        InitRect->w = OverlayRect.w;
347
        InitRect->h = OverlayRect.h;
348
        
349
        char audio_stats[255], video_stats[255];
350
        sprintf(audio_stats, "waiting for incoming audio packets...");
351
        sprintf(video_stats, "waiting for incoming video packets...");
352
        ChunkerPlayerGUI_SetStatsText(audio_stats, video_stats,LED_GREEN);
353
        
354
        return 0;
355
}
356

    
357
int DecodeEnqueuedAudio(AVPacket *pkt, PacketQueue *q, int* size)
358
{
359
        uint16_t *audio_bufQ = NULL;
360
        int16_t *dataQ = NULL;
361
        int data_sizeQ = AVCODEC_MAX_AUDIO_FRAME_SIZE;
362
        int lenQ;
363
        int ret = 0;
364

    
365
        //set the flag to decoded anyway        
366
        pkt->convergence_duration = -1;
367

    
368
        audio_bufQ = (uint16_t *)av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
369
        if(audio_bufQ) {
370
#ifdef DEBUG_AUDIO_BUFFER
371
                printf("AUDIO_BUFFER: about to decode packet %d, size %d, data %d\n", pkt->stream_index, pkt->size, pkt->data);
372
#endif
373
                //decode the packet data
374
                lenQ = avcodec_decode_audio3(aCodecCtx, (int16_t *)audio_bufQ, &data_sizeQ, pkt);
375
                if(lenQ > 0) {
376
                        dataQ = (int16_t *)av_malloc(data_sizeQ); //this will be free later at the time of playback
377
                        if(dataQ) {
378
                                memcpy(dataQ, audio_bufQ, data_sizeQ);
379
                                if(pkt->data != NULL)
380
                                {
381
                                        //discard the old encoded bytes
382
                                        av_free(pkt->data);
383
                                }
384
                                //subtract them from queue size
385
                                q->size -= pkt->size;
386
                                *size = pkt->size;
387
                                pkt->data = (int8_t *)dataQ;
388
                                pkt->size = data_sizeQ;
389
                                //add new size to queue size
390
                                q->size += pkt->size;
391
                                ret = 1;
392
                        }
393
                        else {
394
#ifdef DEBUG_AUDIO_BUFFER
395
                                printf("AUDIO_BUFFER: cannot alloc space for decoded packet %d\n", pkt->stream_index);
396
#endif
397
                        }
398
                }
399
                else {
400
#ifdef DEBUG_AUDIO_BUFFER
401
                        printf("AUDIO_BUFFER: cannot decode packet %d\n", pkt->stream_index);
402
#endif
403
                }
404
                av_free(audio_bufQ);
405
        }
406
        else {
407
#ifdef DEBUG_AUDIO_BUFFER
408
                printf("AUDIO_BUFFER: cannot alloc decode buffer for packet %d\n", pkt->stream_index);
409
#endif
410
        }
411
        return ret; //problems occurred
412
}
413

    
414
/**
415
 * removes a packet from the list and returns the next
416
 * */
417
AVPacketList *RemoveFromQueue(PacketQueue *q, AVPacketList *p)
418
{
419
        AVPacketList *retpk = p->next;
420
        q->nb_packets--;
421
        //adjust size here and not in the various cases of the dequeue
422
        q->size -= p->pkt.size;
423
        if(&p->pkt)
424
        {
425
                av_free_packet(&p->pkt);
426
        }
427
        if(p)
428
                av_free(p);
429
        return retpk;
430
}
431

    
432
AVPacketList *SeekAndDecodePacketStartingFrom(AVPacketList *p, PacketQueue *q, int* size)
433
{
434
        while(p) {
435
                        //check if audio packet has been already decoded
436
                        if(p->pkt.convergence_duration == 0) {
437
                                //not decoded yet, try to decode it
438
                                if( !DecodeEnqueuedAudio(&(p->pkt), q, size) ) {
439
                                        //it was not possible to decode this packet, return next one
440
                                        p = RemoveFromQueue(q, p);
441
                                }
442
                                else
443
                                        return p;
444
                        }
445
                        else
446
                                return p;
447
        }
448
        return NULL;
449
}
450

    
451
int PacketQueueGet(PacketQueue *q, AVPacket *pkt, short int av, int* size)
452
{
453
        //AVPacket tmp;
454
        AVPacketList *pkt1 = NULL;
455
        int ret=-1;
456
        int SizeToCopy=0;
457
        struct timeval now_tv;
458

    
459
        SDL_LockMutex(q->mutex);
460

    
461
#ifdef DEBUG_QUEUE
462
        printf("QUEUE: Get NPackets=%d Type=%s\n", q->nb_packets, (q->queueType==AUDIO) ? "AUDIO" : "VIDEO");
463
#endif
464

    
465
        if((q->queueType==AUDIO && QueueFillingMode) || QueueStopped)
466
        {
467
                SDL_UnlockMutex(q->mutex);
468
                return -1;
469
        }
470

    
471
        if(av==1) { //somebody requested an audio packet, q is the audio queue
472
                //try to dequeue the first packet of the audio queue
473
                pkt1 = SeekAndDecodePacketStartingFrom(q->first_pkt, q, size);
474
                if(pkt1) { //yes we have them!
475
                        if(pkt1->pkt.size-AudioQueueOffset > dimAudioQ) {
476
                                //one packet is enough to give us the requested number of bytes by the audio_callback
477
#ifdef DEBUG_QUEUE_DEEP
478
                                printf("  AV=1 and Extract from the same packet\n");
479
#endif
480
                                pkt->size = dimAudioQ;
481
                                memcpy(pkt->data,pkt1->pkt.data+AudioQueueOffset,dimAudioQ);
482
                                pkt->dts = pkt1->pkt.dts;
483
                                pkt->pts = pkt1->pkt.pts;
484
                                pkt->stream_index = pkt1->pkt.stream_index;//1;
485
                                pkt->flags = 1;
486
                                pkt->pos = -1;
487
                                pkt->convergence_duration = -1;
488
#ifdef DEBUG_QUEUE_DEEP
489
                                printf("   Adjust timestamps Old = %lld New = %lld\n", pkt1->pkt.dts, (int64_t)(pkt1->pkt.dts + deltaAudioQ + deltaAudioQError));
490
#endif
491
                                int64_t Olddts=pkt1->pkt.dts;
492
                                pkt1->pkt.dts += deltaAudioQ + deltaAudioQError;
493
                                pkt1->pkt.pts += deltaAudioQ + deltaAudioQError;
494
                                deltaAudioQError=(float)Olddts + deltaAudioQ + deltaAudioQError - (float)pkt1->pkt.dts;
495
                                AudioQueueOffset += dimAudioQ;
496
#ifdef DEBUG_QUEUE_DEEP
497
                                printf("   deltaAudioQError = %f\n",deltaAudioQError);
498
#endif
499

    
500
                                ChunkerPlayerStats_UpdateAudioLossHistory(&(q->PacketHistory), pkt->stream_index, q->last_frame_extracted);
501
                                
502
                                //update index of last frame extracted
503
                                q->last_frame_extracted = pkt->stream_index;
504
#ifdef DEBUG_AUDIO_BUFFER
505
                                printf("1: idx %d    \taqo %d    \tstc %d    \taqe %f    \tpsz %d\n", pkt1->pkt.stream_index, AudioQueueOffset, SizeToCopy, deltaAudioQError, pkt1->pkt.size);
506
#endif
507
                                ret = 1; //OK
508
                        }
509
                        else {
510
                                //we need bytes from two consecutive packets to satisfy the audio_callback
511
#ifdef DEBUG_QUEUE_DEEP
512
                                printf("  AV = 1 and Extract from 2 packets\n");
513
#endif
514
                                //check for a valid next packet since we will finish the current packet
515
                                //and also take some bytes from the next one
516
                                pkt1->next = SeekAndDecodePacketStartingFrom(pkt1->next, q, size);
517
                                if(pkt1->next) {
518
#ifdef DEBUG_QUEUE_DEEP
519
                                        printf("   we have a next...\n");
520
#endif
521
                                        pkt->size = dimAudioQ;
522
                                        pkt->dts = pkt1->pkt.dts;
523
                                        pkt->pts = pkt1->pkt.pts;
524
                                        pkt->stream_index = pkt1->pkt.stream_index;//1;
525
                                        pkt->flags = 1;
526
                                        pkt->pos = -1;
527
                                        pkt->convergence_duration = -1;
528
                                        {
529
                                                SizeToCopy=pkt1->pkt.size-AudioQueueOffset;
530
#ifdef DEBUG_QUEUE_DEEP
531
                                                printf("      SizeToCopy=%d\n",SizeToCopy);
532
#endif
533
                                                memcpy(pkt->data, pkt1->pkt.data+AudioQueueOffset, SizeToCopy);
534
                                                memcpy(pkt->data+SizeToCopy, pkt1->next->pkt.data, (dimAudioQ-SizeToCopy)*sizeof(uint8_t));
535
                                        }
536
#ifdef DEBUG_AUDIO_BUFFER
537
                                        printf("2: idx %d    \taqo %d    \tstc %d    \taqe %f    \tpsz %d\n", pkt1->pkt.stream_index, AudioQueueOffset, SizeToCopy, deltaAudioQError, pkt1->pkt.size);
538
#endif
539
                                }
540
#ifdef DEBUG_AUDIO_BUFFER
541
                                else {
542
                                        printf("2: NONEXT\n");
543
                                }
544
#endif
545
                                //HINT SEE before q->size -= SizeToCopy;
546
                                q->first_pkt = RemoveFromQueue(q, pkt1);
547

    
548
                                // Adjust timestamps
549
                                pkt1 = q->first_pkt;
550
                                if(pkt1) {
551
                                        int Offset=(dimAudioQ-SizeToCopy)*1000/(AudioSpecification.freq*2*AudioSpecification.channels);
552
                                        int64_t LastDts=pkt1->pkt.dts;
553
                                        pkt1->pkt.dts += Offset + deltaAudioQError;
554
                                        pkt1->pkt.pts += Offset + deltaAudioQError;
555
                                        deltaAudioQError = (float)LastDts + (float)Offset + deltaAudioQError - (float)pkt1->pkt.dts;
556
#ifdef DEBUG_QUEUE_DEEP
557
                                        printf("   Adjust timestamps Old = %lld New = %lld\n", LastDts, pkt1->pkt.dts);
558
#endif
559
                                        AudioQueueOffset = dimAudioQ - SizeToCopy;
560
                                        //SEE BEFORE HINT q->size -= AudioQueueOffset;
561
                                        ret = 1;
562
                                        
563
                                        ChunkerPlayerStats_UpdateAudioLossHistory(&(q->PacketHistory), pkt->stream_index, q->last_frame_extracted);
564
                                }
565
                                else {
566
                                        AudioQueueOffset=0;
567
                                }
568
#ifdef DEBUG_QUEUE_DEEP
569
                                printf("   deltaAudioQError = %f\n",deltaAudioQError);
570
#endif
571
                                //update index of last frame extracted
572
                                q->last_frame_extracted = pkt->stream_index;
573
                        }
574
                }
575
        }
576
        else { //somebody requested a video packet, q is the video queue
577
                pkt1 = q->first_pkt;
578
                if(pkt1) {
579
#ifdef DEBUG_QUEUE_DEEP
580
                        printf("  AV not 1\n");
581
#endif
582
                        pkt->size = pkt1->pkt.size;
583
                        pkt->dts = pkt1->pkt.dts;
584
                        pkt->pts = pkt1->pkt.pts;
585
                        pkt->stream_index = pkt1->pkt.stream_index;
586
                        pkt->flags = pkt1->pkt.flags;
587
                        pkt->pos = pkt1->pkt.pos;
588
                        pkt->convergence_duration = pkt1->pkt.convergence_duration;
589
                        //*pkt = pkt1->pkt;
590
                        
591
                        if((pkt->data != NULL) && (pkt1->pkt.data != NULL))
592
                                memcpy(pkt->data, pkt1->pkt.data, pkt1->pkt.size);
593
                                
594
                        //HINT SEE BEFORE q->size -= pkt1->pkt.size;
595
                        q->first_pkt = RemoveFromQueue(q, pkt1);
596

    
597
                        ret = 1;
598
                        
599
                        ChunkerPlayerStats_UpdateVideoLossHistory(&(q->PacketHistory), pkt->stream_index, q->last_frame_extracted);
600
                        
601
                        //update index of last frame extracted
602
                        q->last_frame_extracted = pkt->stream_index;
603
                        last_video_frame_extracted = q->last_frame_extracted;
604
                }
605
#ifdef DEBUG_QUEUE
606
                else {
607
                        printf("  VIDEO pk1 NULL!!!!\n");
608
                }
609
#endif
610
        }
611

    
612
        if(q->nb_packets==0 && q->queueType==AUDIO) {
613
                QueueFillingMode=1;
614
#ifdef DEBUG_QUEUE
615
                printf("QUEUE: Get FillingMode ON\n");
616
#endif
617
        }
618
#ifdef DEBUG_QUEUE
619
        printf("QUEUE: Get Last %s Frame Extracted = %d\n", (q->queueType==AUDIO) ? "AUDIO" : "VIDEO", q->last_frame_extracted);
620
#endif
621

    
622
        SDL_UnlockMutex(q->mutex);
623
        return ret;
624
}
625

    
626
int AudioDecodeFrame(uint8_t *audio_buf, int buf_size) {
627
        //struct timeval now;
628
        int audio_pkt_size = 0;
629
        int compressed_size = 0;
630
        long long Now;
631
        short int DecodeAudio=0, SkipAudio=0;
632
        //int len1, data_size;
633

    
634
        //gettimeofday(&now,NULL);
635
        //Now = (now.tv_sec)*1000+now.tv_usec/1000;
636
        Now=(long long)SDL_GetTicks();
637
        struct timeval now_tv;
638

    
639
        if(QueueFillingMode || QueueStopped)
640
        {
641
                //SDL_LockMutex(timing_mutex);
642
                FirstTimeAudio=1;
643
                FirstTime = 1;
644
                //SDL_UnlockMutex(timing_mutex);
645
                return -1;
646
        }
647

    
648
        if((FirstTime==1 || FirstTimeAudio==1) && audioq.size>0) {
649
                if(audioq.first_pkt->pkt.pts>0)
650
                {
651
                        //SDL_LockMutex(timing_mutex);
652
                        DeltaTime=Now-(long long)(audioq.first_pkt->pkt.pts);
653
                        FirstTimeAudio = 0;
654
                        FirstTime = 0;
655
                        //SDL_UnlockMutex(timing_mutex);
656
#ifdef DEBUG_AUDIO 
657
                         printf("AUDIO: audio_decode_frame - DeltaTimeAudio=%lld\n",DeltaTime);
658
#endif
659
                }
660
        }
661

    
662
#ifdef DEBUG_AUDIO 
663
        if(audioq.first_pkt)
664
        {
665
                printf("AUDIO: audio_decode_frame - Syncro params: Delta:%lld Now:%lld pts=%lld pts+Delta=%lld ",(long long)DeltaTime,Now,(long long)audioq.first_pkt->pkt.pts,(long long)audioq.first_pkt->pkt.pts+DeltaTime);
666
                printf("AUDIO: QueueLen=%d ",(int)audioq.nb_packets);
667
                printf("AUDIO: QueueSize=%d\n",(int)audioq.size);
668
        }
669
        else
670
                printf("AUDIO: audio_decode_frame - Empty queue\n");
671
#endif
672

    
673
        gettimeofday(&now_tv, NULL);
674
        if(audioq.nb_packets>0)
675
        {
676
                if((long long)audioq.first_pkt->pkt.pts+DeltaTime<Now-(long long)MAX_TOLLERANCE)
677
                {
678
                        SkipAudio = 1;
679
                        DecodeAudio = 0;
680
                }
681
                else if((long long)audioq.first_pkt->pkt.pts+DeltaTime>=Now-(long long)MAX_TOLLERANCE &&
682
                        (long long)audioq.first_pkt->pkt.pts+DeltaTime<=Now+(long long)MAX_TOLLERANCE) {
683
                                SkipAudio = 0;
684
                                DecodeAudio = 1;
685
                }
686
        }
687
        
688
        while(SkipAudio==1 && audioq.size>0)
689
        {
690
                SkipAudio = 0;
691
#ifdef DEBUG_AUDIO
692
                 printf("AUDIO: skipaudio: queue size=%d\n",audioq.size);
693
#endif
694
                if(PacketQueueGet(&audioq,&AudioPkt,1, &compressed_size) < 0) {
695
                        return -1;
696
                }
697
                if(audioq.first_pkt)
698
                {
699
                        ChunkerPlayerStats_UpdateAudioSkipHistory(&(audioq.PacketHistory), AudioPkt.stream_index, compressed_size);
700
                        
701
                        if((long long)audioq.first_pkt->pkt.pts+DeltaTime<Now-(long long)MAX_TOLLERANCE)
702
                        {
703
                                SkipAudio = 1;
704
                                DecodeAudio = 0;
705
                        }
706
                        else if((long long)audioq.first_pkt->pkt.pts+DeltaTime>=Now-(long long)MAX_TOLLERANCE &&
707
                                (long long)audioq.first_pkt->pkt.pts+DeltaTime<=Now+(long long)MAX_TOLLERANCE) {
708
                                        SkipAudio = 0;
709
                                        DecodeAudio = 1;
710
                        }
711
                }
712
        }
713
        if(DecodeAudio==1) {
714
                if(PacketQueueGet(&audioq,&AudioPkt,1, &compressed_size) < 0) {
715
                        return -1;
716
                }
717
                memcpy(audio_buf,AudioPkt.data,AudioPkt.size);
718
                audio_pkt_size = AudioPkt.size;
719
#ifdef DEBUG_AUDIO
720
                 printf("AUDIO: Decode audio\n");
721
#endif
722

    
723
                ChunkerPlayerStats_UpdateAudioPlayedHistory(&(audioq.PacketHistory), AudioPkt.stream_index, compressed_size);
724
        }
725

    
726
        return audio_pkt_size;
727
}
728

    
729
int VideoCallback(void *valthread)
730
{
731
        //AVPacket pktvideo;
732
        AVCodecContext  *pCodecCtx;
733
        AVCodec         *pCodec;
734
        AVFrame         *pFrame;
735
        int frameFinished;
736
        AVPicture pict;
737
        long long Now;
738
        short int SkipVideo, DecodeVideo;
739
        
740
#ifdef SAVE_YUV
741
        static AVFrame* lastSavedFrameBuffer = NULL;
742
        
743
        if(!lastSavedFrameBuffer)
744
                lastSavedFrameBuffer = (AVFrame*) malloc(sizeof(AVFrame));
745
#endif
746

    
747
        //double frame_rate = 0.0,time_between_frames=0.0;
748
        //struct timeval now;
749

    
750
        //int wait_for_sync = 1;
751
        ThreadVal *tval;
752
        tval = (ThreadVal *)valthread;
753

    
754
        //frame_rate = tval->framerate;
755
        //time_between_frames = 1.e6 / frame_rate;
756
        //gettimeofday(&time_now,0);
757

    
758
        //frecon = fopen("recondechunk.mpg","wb");
759

    
760
        //setup video decoder
761
        pCodec = avcodec_find_decoder_by_name(video_codec);
762
        if (pCodec) {
763
                fprintf(stderr, "INIT: Setting VIDEO codecID to: %d\n",pCodec->id);
764
        } else {
765
                fprintf(stderr, "INIT: Unknown VIDEO codec: %s!\n", video_codec);
766
                return -1; // Codec not found
767
        }
768

    
769
        pCodecCtx=avcodec_alloc_context();
770
        pCodecCtx->codec_type = CODEC_TYPE_VIDEO;
771
        //pCodecCtx->debug = FF_DEBUG_DCT_COEFF;
772
        pCodecCtx->codec_id = pCodec->id;
773

    
774
        //pCodecCtx->bit_rate = 400000;
775
        // resolution must be a multiple of two
776
        pCodecCtx->width = tval->width;//176;//352;
777
        pCodecCtx->height = tval->height;//144;//288;
778

    
779
        // frames per second
780
        //pCodecCtx->time_base = (AVRational){1,25};
781
        //pCodecCtx->gop_size = 10; // emit one intra frame every ten frames
782
        //pCodecCtx->max_b_frames=1;
783
        pCodecCtx->pix_fmt = PIX_FMT_YUV420P;
784
        pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
785

    
786
        if(pCodec==NULL) {
787
                fprintf(stderr, "Unsupported codec!\n");
788
                return -1; // Codec not found
789
        }
790
        if(avcodec_open(pCodecCtx, pCodec) < 0) {
791
                fprintf(stderr, "could not open codec\n");
792
                return -1; // Could not open codec
793
        }
794
        pFrame=avcodec_alloc_frame();
795
        if(pFrame==NULL) {
796
                printf("Memory error!!!\n");
797
                return -1;
798
        }
799
        
800
#ifdef DEBUG_VIDEO
801
         printf("VIDEO: video_callback entering main cycle\n");
802
#endif
803

    
804
        struct timeval now_tv;
805
        while(AVPlaying && !quit) {
806
                if(QueueFillingMode || QueueStopped)
807
                {
808
                        //SDL_LockMutex(timing_mutex);
809
                        FirstTime = 1;
810
                        //SDL_UnlockMutex(timing_mutex);
811
                        usleep(5000);
812
                        continue;
813
                }
814

    
815
                DecodeVideo = 0;
816
                SkipVideo = 0;
817
                Now=(long long)SDL_GetTicks();
818
                if(FirstTime==1 && videoq.size>0) {
819
                        if(videoq.first_pkt->pkt.pts>0)
820
                        {
821
                                //SDL_LockMutex(timing_mutex);
822
                                DeltaTime=Now-(long long)videoq.first_pkt->pkt.pts;
823
                                FirstTime = 0;
824
                                //SDL_UnlockMutex(timing_mutex);
825
                        }
826
#ifdef DEBUG_VIDEO 
827
                         printf("VIDEO: VideoCallback - DeltaTimeAudio=%lld\n",DeltaTime);
828
#endif
829
                }
830

    
831
#ifdef DEBUG_VIDEO 
832
                if(videoq.first_pkt)
833
                {
834
                        printf("VIDEO: VideoCallback - Syncro params: Delta:%lld Now:%lld pts=%lld pts+Delta=%lld ",(long long)DeltaTime,Now,(long long)videoq.first_pkt->pkt.pts,(long long)videoq.first_pkt->pkt.pts+DeltaTime);
835
                        printf("VIDEO: Index=%d ", (int)videoq.first_pkt->pkt.stream_index);
836
                        printf("VIDEO: QueueLen=%d ", (int)videoq.nb_packets);
837
                        printf("VIDEO: QueueSize=%d\n", (int)videoq.size);
838
                }
839
                else
840
                        printf("VIDEO: VideoCallback - Empty queue\n");
841
#endif
842

    
843
                if(videoq.nb_packets>0) {
844
                        if(((long long)videoq.first_pkt->pkt.pts+DeltaTime)<Now-(long long)MAX_TOLLERANCE)
845
                        {
846
                                SkipVideo = 1;
847
                                DecodeVideo = 0;
848
                        }
849
                        else 
850
                                if(((long long)videoq.first_pkt->pkt.pts+DeltaTime)>=Now-(long long)MAX_TOLLERANCE &&
851
                                   ((long long)videoq.first_pkt->pkt.pts+DeltaTime)<=Now+(long long)MAX_TOLLERANCE) {
852
                                        SkipVideo = 0;
853
                                        DecodeVideo = 1;
854
                                }
855
                                
856
                                // else (i.e. videoq.first_pkt->pkt.pts+DeltaTime>Now+MAX_TOLLERANCE)
857
                                // do nothing and continue
858
                }
859
#ifdef DEBUG_VIDEO
860
                printf("VIDEO: skipvideo:%d decodevideo:%d\n",SkipVideo,DecodeVideo);
861
#endif
862
                gettimeofday(&now_tv, NULL);
863
                
864
                while(SkipVideo==1 && videoq.size>0)
865
                {
866
                        SkipVideo = 0;
867
#ifdef DEBUG_VIDEO 
868
                         printf("VIDEO: Skip Video\n");
869
#endif
870
                        if(PacketQueueGet(&videoq,&VideoPkt,0, NULL) < 0) {
871
                                break;
872
                        }
873

    
874
                        avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &VideoPkt);
875
                        
876
                        // sometimes assertion fails, maybe the decoder change the frame type
877
                        //~ if(LastSourceIFrameDistance == 0)
878
                                //~ assert(pFrame->pict_type == 1);
879

    
880
                        if(videoq.first_pkt)
881
                        {
882
                                if((long long)videoq.first_pkt->pkt.pts+DeltaTime<Now-(long long)MAX_TOLLERANCE)
883
                                {
884
                                        SkipVideo = 1;
885
                                        DecodeVideo = 0;
886
                                }
887
                                else if((long long)videoq.first_pkt->pkt.pts+DeltaTime>=Now-(long long)MAX_TOLLERANCE &&
888
                                                                (long long)videoq.first_pkt->pkt.pts+DeltaTime<=Now+(long long)MAX_TOLLERANCE) {
889
                                        SkipVideo = 0;
890
                                        DecodeVideo = 1;
891
                                }
892
                        }
893
                        
894
                        ChunkerPlayerStats_UpdateVideoSkipHistory(&(videoq.PacketHistory), VideoPkt.stream_index, pFrame->pict_type, VideoPkt.size, pFrame);
895
                        
896
                        /*if(pFrame->pict_type == 1)
897
                        {
898
                                int i1;
899
                                // every 23 items (23 is the qstride field in the AVFrame struct) there is 1 zero.
900
                                // 396/23 = 17 => 396 macroblocks + 17 zeros = 413 items
901
                                for(i1=0; i1< 413; i1++)
902
                                        fprintf(qscaletable_file, "%d\t", (int)pFrame->qscale_table[i1]);
903
                                fprintf(qscaletable_file, "\n");
904
                        }*/
905
                        
906
                        //ChunkerPlayerStats_UpdateVideoPlayedHistory(&(videoq.PacketHistory), VideoPkt.stream_index, pFrame->pict_type, VideoPkt.size);
907
                }
908

    
909
                if(DecodeVideo==1) {
910
                        if(PacketQueueGet(&videoq,&VideoPkt,0, NULL) > 0) {
911
                                avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &VideoPkt);
912

    
913
                                if(frameFinished)
914
                                { // it must be true all the time else error
915
#ifdef DEBUG_VIDEO
916
                                        printf("VIDEO: FrameFinished\n");
917
#endif
918
                                        decoded_vframes++;
919
                                        
920
                                        // sometimes assertion fails, maybe the decoder change the frame type
921
                                        //~ if(LastSourceIFrameDistance == 0)
922
                                                //~ assert(pFrame->pict_type == 1);
923
#ifdef SAVE_YUV
924
                                        if(LastSavedVFrame == -1)
925
                                        {
926
                                                memcpy(lastSavedFrameBuffer, pFrame, sizeof(AVFrame));
927
                                                SaveFrame(pFrame, pCodecCtx->width, pCodecCtx->height);
928
                                                LastSavedVFrame = VideoPkt.stream_index;
929
                                        }
930
                                        else if(LastSavedVFrame == (VideoPkt.stream_index-1))
931
                                        {
932
                                                memcpy(lastSavedFrameBuffer, pFrame, sizeof(AVFrame));
933
                                                SaveFrame(pFrame, pCodecCtx->width, pCodecCtx->height);
934
                                                LastSavedVFrame = VideoPkt.stream_index;
935
                                        }
936
                                        else if(LastSavedVFrame >= 0)
937
                                        {
938
                                                while(LastSavedVFrame < (VideoPkt.stream_index-1))
939
                                                {
940
                                                        SaveFrame(lastSavedFrameBuffer, pCodecCtx->width, pCodecCtx->height);
941
                                                }
942

    
943
                                                memcpy(lastSavedFrameBuffer, pFrame, sizeof(AVFrame));
944
                                                SaveFrame(pFrame, pCodecCtx->width, pCodecCtx->height);
945
                                                LastSavedVFrame = VideoPkt.stream_index;
946
                                        }
947
#endif
948
                                        ChunkerPlayerStats_UpdateVideoPlayedHistory(&(videoq.PacketHistory), VideoPkt.stream_index, pFrame->pict_type, VideoPkt.size, pFrame);
949

    
950
                                        if(SilentMode)
951
                                                continue;
952

    
953
                                        // Lock SDL_yuv_overlay
954
                                        if(SDL_MUSTLOCK(MainScreen)) {
955
                                                if(SDL_LockSurface(MainScreen) < 0) {
956
                                                        continue;
957
                                                }
958
                                        }
959

    
960
                                        if(SDL_LockYUVOverlay(YUVOverlay) < 0) {
961
                                                if(SDL_MUSTLOCK(MainScreen)) {
962
                                                        SDL_UnlockSurface(MainScreen);
963
                                                }
964
                                                continue;
965
                                        }
966
                                        
967
                                        pict.data[0] = YUVOverlay->pixels[0];
968
                                        pict.data[1] = YUVOverlay->pixels[2];
969
                                        pict.data[2] = YUVOverlay->pixels[1];
970

    
971
                                        pict.linesize[0] = YUVOverlay->pitches[0];
972
                                        pict.linesize[1] = YUVOverlay->pitches[2];
973
                                        pict.linesize[2] = YUVOverlay->pitches[1];
974

    
975
                                        if(img_convert_ctx == NULL) {
976
                                                img_convert_ctx = sws_getContext(tval->width, tval->height, PIX_FMT_YUV420P, InitRect->w, InitRect->h, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
977
                                                if(img_convert_ctx == NULL) {
978
                                                        fprintf(stderr, "Cannot initialize the conversion context!\n");
979
                                                        exit(1);
980
                                                }
981
                                        }
982
                                        
983
#ifdef VIDEO_DEINTERLACE
984
                                        avpicture_deinterlace(
985
                                                (AVPicture*) pFrame,
986
                                                (const AVPicture*) pFrame,
987
                                                pCodecCtx->pix_fmt,
988
                                                tval->width, tval->height);
989
#endif
990
                                        
991
                                        // let's draw the data (*yuv[3]) on a SDL screen (*screen)
992
                                        sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize, 0, tval->height, pict.data, pict.linesize);
993
                                        SDL_UnlockYUVOverlay(YUVOverlay);
994
                                        // Show, baby, show!
995
                                        SDL_LockMutex(OverlayMutex);
996
                                        SDL_DisplayYUVOverlay(YUVOverlay, &OverlayRect);
997
                                        SDL_UnlockMutex(OverlayMutex);
998

    
999
                                        //redisplay logo
1000
                                        /**SDL_BlitSurface(image, NULL, MainScreen, &dest);*/
1001
                                        /* Update the screen area just changed */
1002
                                        /**SDL_UpdateRects(MainScreen, 1, &dest);*/
1003

    
1004
                                        if(SDL_MUSTLOCK(MainScreen)) {
1005
                                                SDL_UnlockSurface(MainScreen);
1006
                                        }
1007
                                } //if FrameFinished
1008
                                else
1009
                                {
1010
                                        ChunkerPlayerStats_UpdateVideoLossHistory(&(videoq.PacketHistory), VideoPkt.stream_index+1, videoq.last_frame_extracted-1);
1011
                                }
1012
                        } // if packet_queue_get
1013
                } //if DecodeVideo=1
1014

    
1015
                usleep(5000);
1016
        }
1017
        avcodec_close(pCodecCtx);
1018
        av_free(pCodecCtx);
1019
        av_free(pFrame);
1020
        //fclose(frecon);
1021
#ifdef DEBUG_VIDEO
1022
         printf("VIDEO: video callback end\n");
1023
#endif
1024

    
1025
#ifdef SAVE_YUV
1026
        if(!lastSavedFrameBuffer)
1027
                free(lastSavedFrameBuffer);
1028
        
1029
        lastSavedFrameBuffer = NULL;
1030
#endif
1031

    
1032
        return 0;
1033
}
1034

    
1035
void AudioCallback(void *userdata, Uint8 *stream, int len)
1036
{
1037
        //AVCodecContext *aCodecCtx = (AVCodecContext *)userdata;
1038
        int audio_size;
1039

    
1040
        static uint8_t audio_buf[AVCODEC_MAX_AUDIO_FRAME_SIZE];
1041

    
1042
        audio_size = AudioDecodeFrame(audio_buf, sizeof(audio_buf));
1043
        
1044
        if(SilentMode < 2)
1045
                if(audio_size != len) {
1046
                        memset(stream, 0, len);
1047
                } else {
1048
                        memcpy(stream, (uint8_t *)audio_buf, len);
1049
                }
1050
}
1051

    
1052
void SaveFrame(AVFrame *pFrame, int width, int height)
1053
{
1054
        FILE *pFile;
1055
        int  y;
1056
  
1057
         // Open file
1058
        pFile=fopen(YUVFileName, "ab");
1059
        if(pFile==NULL)
1060
                return;
1061
  
1062
        // Write header
1063
        //fprintf(pFile, "P5\n%d %d\n255\n", width, height);
1064
  
1065
        // Write Y data
1066
        for(y=0; y<height; y++)
1067
                  fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width, pFile);
1068
        // Write U data
1069
        for(y=0; y<height/2; y++)
1070
                  fwrite(pFrame->data[1]+y*pFrame->linesize[1], 1, width/2, pFile);
1071
        // Write V data
1072
        for(y=0; y<height/2; y++)
1073
                  fwrite(pFrame->data[2]+y*pFrame->linesize[2], 1, width/2, pFile);
1074
  
1075
        // Close file
1076
        fclose(pFile);
1077
}
1078

    
1079
int ChunkerPlayerCore_IsRunning()
1080
{
1081
        return AVPlaying;
1082
}
1083

    
1084
void ChunkerPlayerCore_Play()
1085
{
1086
        if(AVPlaying) return;
1087
        AVPlaying = 1;
1088
        
1089
        SDL_PauseAudio(0);
1090
        video_thread = SDL_CreateThread(VideoCallback, &VideoCallbackThreadParams);
1091
        ChunkerPlayerStats_Init();
1092
        stats_thread = SDL_CreateThread(CollectStatisticsThread, NULL);
1093
        
1094
        decoded_vframes = 0;
1095
        LastSavedVFrame = -1;
1096
}
1097

    
1098
void ChunkerPlayerCore_Stop()
1099
{
1100
        if(!AVPlaying) return;
1101
        
1102
        AVPlaying = 0;
1103
        
1104
        // Stop audio&video playback
1105
        SDL_WaitThread(video_thread, NULL);
1106
        SDL_WaitThread(stats_thread, NULL);
1107
        SDL_PauseAudio(1);        
1108
        SDL_CloseAudio();
1109
        
1110
        if(YUVOverlay != NULL)
1111
        {
1112
                SDL_FreeYUVOverlay(YUVOverlay);
1113
                YUVOverlay = NULL;
1114
        }
1115
        
1116
        PacketQueueReset(&audioq);
1117
        PacketQueueReset(&videoq);
1118
        
1119
        avcodec_close(aCodecCtx);
1120
        av_free(aCodecCtx);
1121
        free(AudioPkt.data);
1122
        free(VideoPkt.data);
1123
        free(outbuf_audio);
1124
        free(InitRect);
1125
        
1126
        /*
1127
        * Sleep two buffers' worth of audio before closing, in order
1128
        *  to allow the playback to finish. This isn't always enough;
1129
        *   perhaps SDL needs a way to explicitly wait for device drain?
1130
        */
1131
        int delay = 2 * 1000 * CurrentAudioSamples / CurrentAudioFreq;
1132
        // printf("SDL_Delay(%d)\n", delay*10);
1133
        SDL_Delay(delay*10);
1134
}
1135

    
1136
void ChunkerPlayerCore_Pause()
1137
{
1138
        if(!AVPlaying) return;
1139
        
1140
        AVPlaying = 0;
1141
        
1142
        // Stop audio&video playback
1143
        SDL_WaitThread(video_thread, NULL);
1144
        SDL_PauseAudio(1);
1145
        
1146
        PacketQueueReset(&audioq);
1147
        PacketQueueReset(&videoq);
1148
}
1149

    
1150
int ChunkerPlayerCore_AudioEnded()
1151
{
1152
        return (audioq.nb_packets==0 && audioq.last_frame_extracted>0);
1153
}
1154

    
1155
void ChunkerPlayerCore_ResetAVQueues()
1156
{
1157
#ifdef DEBUG_QUEUE
1158
        printf("QUEUE: MAIN SHOULD RESET\n");
1159
#endif
1160
        PacketQueueReset(&audioq);
1161
        PacketQueueReset(&videoq);
1162
}
1163

    
1164
int ChunkerPlayerCore_EnqueueBlocks(const uint8_t *block, const int block_size)
1165
{
1166
#ifdef EMULATE_CHUNK_LOSS
1167
        static time_t loss_cycle_start_time = 0, now = 0;
1168
        static int early_losses = 0;
1169
        static int clp_frames = 0;
1170
        
1171
        if(ScheduledChunkLosses)
1172
        {
1173
                static unsigned int random_threshold;
1174
                now=time(NULL);
1175
                if(!loss_cycle_start_time)
1176
                        loss_cycle_start_time = now;
1177
                        
1178
                if(((now-loss_cycle_start_time) >= ScheduledChunkLosses[((CurrChunkLossIndex+1)%NScheduledChunkLosses)].Time) && (NScheduledChunkLosses>1 || CurrChunkLossIndex==-1))
1179
                {
1180
                        CurrChunkLossIndex = ((CurrChunkLossIndex+1)%NScheduledChunkLosses);
1181
                        if(CurrChunkLossIndex == (NScheduledChunkLosses-1))
1182
                                loss_cycle_start_time = now;
1183
                        
1184
                        if(ScheduledChunkLosses[CurrChunkLossIndex].Value == -1)
1185
                                random_threshold = ScheduledChunkLosses[CurrChunkLossIndex].MinValue + (rand() % (ScheduledChunkLosses[CurrChunkLossIndex].MaxValue-ScheduledChunkLosses[CurrChunkLossIndex].MinValue));
1186
                        else
1187
                                random_threshold = ScheduledChunkLosses[CurrChunkLossIndex].Value;
1188
                        
1189
                        printf("new ScheduledChunkLoss, time: %d, value: %d\n", (int)ScheduledChunkLosses[CurrChunkLossIndex].Time, random_threshold);
1190
                }
1191
        
1192
                if(clp_frames > 0)
1193
                {
1194
                        clp_frames--;
1195
                        return PLAYER_FAIL_RETURN;
1196
                }
1197
                if((rand() % 100) < random_threshold)
1198
                {
1199
                        if(early_losses > 0)
1200
                early_losses--;
1201
            else
1202
            {
1203
                clp_frames=early_losses=(ScheduledChunkLosses[CurrChunkLossIndex].Burstiness-1);
1204
                return PLAYER_FAIL_RETURN;
1205
            }
1206
                }
1207
        }
1208
#endif
1209

    
1210
        Chunk *gchunk = NULL;
1211
        int decoded_size = -1;
1212
        uint8_t *tempdata, *buffer;
1213
        int j;
1214
        Frame *frame = NULL;
1215
        AVPacket packet, packetaudio;
1216

    
1217
        uint16_t *audio_bufQ = NULL;
1218

    
1219
        //the frame.h gets encoded into 5 slots of 32bits (3 ints plus 2 more for the timeval struct
1220
        static int sizeFrameHeader = 5*sizeof(int32_t);
1221
        static int ExternalChunk_header_size = 5*CHUNK_TRANSCODING_INT_SIZE + 2*CHUNK_TRANSCODING_INT_SIZE + 2*CHUNK_TRANSCODING_INT_SIZE + 1*CHUNK_TRANSCODING_INT_SIZE*2;
1222

    
1223
        static int chunks_out_of_order = 0;
1224
        static int last_chunk_id = -1;
1225

    
1226
        audio_bufQ = (uint16_t *)av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
1227
        if(!audio_bufQ) {
1228
                printf("Memory error in audio_bufQ!\n");
1229
                return PLAYER_FAIL_RETURN;
1230
        }
1231

    
1232
        gchunk = (Chunk *)malloc(sizeof(Chunk));
1233
        if(!gchunk) {
1234
                printf("Memory error in gchunk!\n");
1235
                av_free(audio_bufQ);
1236
                return PLAYER_FAIL_RETURN;
1237
        }
1238

    
1239
        decoded_size = decodeChunk(gchunk, block, block_size);
1240

    
1241
        if(last_chunk_id == -1)
1242
                last_chunk_id = gchunk->id;
1243

    
1244
        if(gchunk->id > (last_chunk_id+1)) {
1245
                chunks_out_of_order += gchunk->id - last_chunk_id - 1;
1246
        }
1247
        last_chunk_id = gchunk->id;
1248

    
1249
#ifdef DEBUG_CHUNKER
1250
        printf("CHUNKER: enqueueBlock: id %d decoded_size %d target size %d - out_of_order %d\n", gchunk->id, decoded_size, GRAPES_ENCODED_CHUNK_HEADER_SIZE + ExternalChunk_header_size + gchunk->size, chunks_out_of_order);
1251
#endif
1252
  if(decoded_size < 0) {
1253
                //HINT here i should differentiate between various return values of the decode
1254
                //in order to free what has been allocated there
1255
                printf("chunk probably corrupted!\n");
1256
                av_free(audio_bufQ);
1257
                free(gchunk);
1258
                return PLAYER_FAIL_RETURN;
1259
        }
1260

    
1261
        frame = (Frame *)malloc(sizeof(Frame));
1262
        if(!frame) {
1263
                printf("Memory error in Frame!\n");
1264
                if(gchunk) {
1265
                        if(gchunk->attributes) {
1266
                                free(gchunk->attributes);
1267
                        }
1268
                        free(gchunk);
1269
                }
1270
                av_free(audio_bufQ);
1271
                return PLAYER_FAIL_RETURN;
1272
        }
1273

    
1274
        tempdata = gchunk->data; //let it point to first frame of payload
1275
        j=gchunk->size;
1276
        while(j>0 && !quit) {
1277
                frame->number = bit32_encoded_pull(tempdata);
1278
                tempdata += CHUNK_TRANSCODING_INT_SIZE;
1279
                frame->timestamp.tv_sec = bit32_encoded_pull(tempdata);
1280
                tempdata += CHUNK_TRANSCODING_INT_SIZE;
1281
                frame->timestamp.tv_usec = bit32_encoded_pull(tempdata);
1282
                tempdata += CHUNK_TRANSCODING_INT_SIZE;
1283
                frame->size = bit32_encoded_pull(tempdata);
1284
                tempdata += CHUNK_TRANSCODING_INT_SIZE;
1285
                frame->type = bit32_encoded_pull(tempdata);
1286
                tempdata += CHUNK_TRANSCODING_INT_SIZE;
1287

    
1288
                buffer = tempdata; // here coded frame information
1289
                tempdata += frame->size; //let it point to the next frame
1290

    
1291
                if(frame->type < 5) { // video frame
1292
                        av_init_packet(&packet);
1293
                        packet.data = buffer;//video_bufQ;
1294
                        packet.size = frame->size;
1295
                        packet.pts = frame->timestamp.tv_sec*(unsigned long long)1000+frame->timestamp.tv_usec;
1296
                        packet.dts = frame->timestamp.tv_sec*(unsigned long long)1000+frame->timestamp.tv_usec;
1297
                        packet.stream_index = frame->number; // use of stream_index for number frame
1298
                        //packet.duration = frame->timestamp.tv_sec;
1299
                        if(packet.size > 0)
1300
                                ChunkerPlayerCore_PacketQueuePut(&videoq, &packet); //the _put makes a copy of the packet
1301

    
1302
#ifdef DEBUG_SOURCE
1303
                        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);
1304
#endif
1305
                }
1306
                else if(frame->type == 5) { // audio frame
1307
                        av_init_packet(&packetaudio);
1308
                        packetaudio.data = buffer;
1309
                        packetaudio.size = frame->size;
1310
                        packetaudio.pts = frame->timestamp.tv_sec*(unsigned long long)1000+frame->timestamp.tv_usec;
1311
                        packetaudio.dts = frame->timestamp.tv_sec*(unsigned long long)1000+frame->timestamp.tv_usec;
1312
                        //packetaudio.duration = frame->timestamp.tv_sec;
1313
                        packetaudio.stream_index = frame->number; // use of stream_index for number frame
1314
                        packetaudio.flags = 1;
1315
                        packetaudio.pos = -1;
1316

    
1317
                        //instead of -1, in order to signal it is not decoded yet
1318
                        packetaudio.convergence_duration = 0;
1319

    
1320
                        // insert the audio frame into the queue
1321
                        if(packetaudio.size > 0)
1322
                                ChunkerPlayerCore_PacketQueuePut(&audioq, &packetaudio);//makes a copy of the packet so i can free here
1323

    
1324
#ifdef DEBUG_SOURCE
1325
                        printf("SOURCE: Insert audio in queue pts=%lld sindex:%d\n", packetaudio.pts, packetaudio.stream_index);
1326
#endif
1327
                }
1328
                else {
1329
                        printf("SOURCE: Unknown frame type %d. Size %d\n", frame->type, frame->size);
1330
                }
1331
                if(frame->size > 0)
1332
                        j = j - sizeFrameHeader - frame->size;
1333
                else {
1334
                        printf("SOURCE: Corrupt frames (size %d) in chunk. Skipping it...\n", frame->size);
1335
                        j = -1;
1336
                }
1337
        }
1338
        //chunk ingestion terminated!
1339
        if(gchunk) {
1340
                if(gchunk->attributes) {
1341
                        free(gchunk->attributes);
1342
                }
1343
                if(gchunk->data)
1344
                        free(gchunk->data);
1345
                free(gchunk);
1346
        }
1347
        if(frame)
1348
                free(frame);
1349
        if(audio_bufQ)
1350
                av_free(audio_bufQ);
1351
                
1352
        return PLAYER_OK_RETURN;
1353
}
1354

    
1355
void ChunkerPlayerCore_SetupOverlay(int width, int height)
1356
{
1357
        // if(!MainScreen && !SilentMode)
1358
        // {
1359
                // printf("Cannot find main screen, exiting...\n");
1360
                // exit(1);
1361
        // }
1362
        
1363
        if(SilentMode)
1364
                return;
1365
                
1366
        SDL_LockMutex(OverlayMutex);
1367
        if(YUVOverlay != NULL)
1368
        {
1369
                SDL_FreeYUVOverlay(YUVOverlay);
1370
                YUVOverlay = NULL;
1371
        }
1372
        
1373
        // create video overlay for display of video frames
1374
        // printf("SDL_CreateYUVOverlay(%d, %d, SDL_YV12_OVERLAY, MainScreen)\n", width, height);
1375
        YUVOverlay = SDL_CreateYUVOverlay(width, height, SDL_YV12_OVERLAY, MainScreen);
1376
        // YUVOverlay = SDL_CreateYUVOverlay(OverlayRect.w, OverlayRect.h, SDL_YV12_OVERLAY, MainScreen);
1377
        if ( YUVOverlay == NULL )
1378
        {
1379
                fprintf(stderr,"SDL: Couldn't create SDL_yuv_overlay: %s", SDL_GetError());
1380
                exit(1);
1381
        }
1382

    
1383
        if ( YUVOverlay->hw_overlay )
1384
                fprintf(stderr,"SDL: Using hardware overlay.\n");
1385
        // OverlayRect.x = (screen_w - width) / 2;
1386
        
1387
        SDL_DisplayYUVOverlay(YUVOverlay, &OverlayRect);
1388
        
1389
        SDL_UnlockMutex(OverlayMutex);
1390
}
1391

    
1392
int CollectStatisticsThread(void *params)
1393
{
1394
        struct timeval last_stats_evaluation, now, last_trace, last_qoe_evaluation;
1395
        gettimeofday(&last_stats_evaluation, NULL);
1396
        last_trace = last_stats_evaluation;
1397
        last_qoe_evaluation = last_stats_evaluation;
1398
        
1399
        double video_qdensity;
1400
        double audio_qdensity;
1401
        char audio_stats_text[255];
1402
        char video_stats_text[255];
1403
        int loss_changed = 0;
1404
        int density_changed = 0;
1405
        SStats audio_statistics, video_statistics;
1406
        double qoe = 0;
1407
        int sleep_time = STATS_THREAD_GRANULARITY*1000;
1408
        
1409
        while(AVPlaying && !quit)
1410
        {
1411
                usleep(sleep_time);
1412
                
1413
                gettimeofday(&now, NULL);
1414
                
1415
                if((((now.tv_sec*1000)+(now.tv_usec/1000)) - ((last_stats_evaluation.tv_sec*1000)+(last_stats_evaluation.tv_usec/1000))) > GUI_PRINTSTATS_INTERVAL)
1416
                {
1417
                        // estimate audio queue stats
1418
                        int audio_stats_changed = ChunkerPlayerStats_GetStats(&(audioq.PacketHistory), &audio_statistics);
1419
                        
1420
                        // estimate video queue stats
1421
                        int video_stats_changed = ChunkerPlayerStats_GetStats(&(videoq.PacketHistory), &video_statistics);
1422

    
1423
#ifdef DEBUG_STATS
1424
                        printf("VIDEO: %d Kbit/sec; ", video_statistics.Bitrate);
1425
                        printf("AUDIO: %d Kbit/sec\n", audio_statistics.Bitrate);
1426
#endif
1427

    
1428
                        // QUEUE DENSITY EVALUATION
1429
                        if((audioq.last_pkt != NULL) && (audioq.first_pkt != NULL))
1430
                                if(audioq.last_pkt->pkt.stream_index >= audioq.first_pkt->pkt.stream_index)
1431
                                {
1432
                                        //plus 1 because if they are adjacent (difference 1) there really should be 2 packets in the queue
1433
                                        audio_qdensity = (double)audioq.nb_packets / (double)(audioq.last_pkt->pkt.stream_index - audioq.first_pkt->pkt.stream_index + 1) * 100.0;
1434
                                }
1435
                        
1436
                        if((videoq.last_pkt != NULL) && (videoq.first_pkt != NULL))
1437
                                if(videoq.last_pkt->pkt.stream_index >= videoq.first_pkt->pkt.stream_index)
1438
                                {
1439
                                        // plus 1 because if they are adjacent (difference 1) there really should be 2 packets in the queue
1440
                                        video_qdensity = (double)videoq.nb_packets / (double)(videoq.last_pkt->pkt.stream_index - videoq.first_pkt->pkt.stream_index + 1) * 100.0;
1441
                                }
1442
                        
1443
                        if(LogTraces)
1444
                        {
1445
                                ChunkerPlayerStats_PrintHistoryTrace(&(audioq.PacketHistory), AudioTraceFilename);
1446
                                ChunkerPlayerStats_PrintHistoryTrace(&(videoq.PacketHistory), VideoTraceFilename);
1447
                                
1448
                                //if(SilentMode != 1 && SilentMode != 2)
1449
                                        ChunkerPlayerStats_PrintContextFile();
1450
                        }
1451

    
1452
                        // PRINT STATISTICS ON GUI
1453
                        if(!Audio_ON)
1454
                                sprintf(audio_stats_text, "AUDIO MUTED");
1455
                        else if(audio_stats_changed)
1456
                                sprintf(audio_stats_text, "[AUDIO] qdensity: %d\%% - losses: %d/sec (%ld tot) - skips: %d/sec (%ld tot)", (int)audio_qdensity, (int)audio_statistics.Lossrate, audioq.PacketHistory.LostCount, audio_statistics.Skiprate, audioq.PacketHistory.SkipCount);
1457
                        else
1458
                                sprintf(audio_stats_text, "waiting for incoming audio packets...");
1459

    
1460
                        if(video_stats_changed)
1461
                        {
1462
                                char est_psnr_string[255];
1463
                                sprintf(est_psnr_string, "");
1464
                                if(qoe)
1465
                                {
1466
                                        sprintf(est_psnr_string, " - Est. Mean PSNR: %.1f db", (float)qoe);
1467
#ifdef PSNR_PUBLICATION
1468
                                        // Publish measure into repository
1469
                                        if(RepoAddress[0]!='\0')
1470
                                        {
1471
                                            MeasurementRecord r;
1472
                            r.originator = NetworkID;
1473
                            r.targetA = NetworkID;
1474
                            r.targetB = NULL;
1475
                            r.published_name = "PSNR_MEAN";
1476
                            r.value = qoe;
1477
                            r.string_value = NULL;
1478
                            r.channel = Channels[SelectedChannel].Title;
1479
                            gettimeofday(&(r.timestamp), NULL);
1480
                            // One update every REPO_UPDATE_INTERVALL seconds
1481
                            struct timeval ElapsedTime;
1482
                            timeval_subtract(&(r.timestamp),&LastTimeRepoPublish,&ElapsedTime);
1483
                        if(ElapsedTime.tv_sec>=PSNR_REPO_UPDATE_INTERVALL)
1484
                        {
1485
                            LastTimeRepoPublish=r.timestamp;
1486
                            if(repPublish(repoclient,NULL,NULL,&r)!=NULL) {
1487
#ifdef DEBUG_PSNR
1488
                               printf("PSNR publish: %s  %e  %s\n",r.originator,qoe,r.channel);
1489
#endif
1490
                                                                                                                }
1491
                        }
1492
                   }
1493
#endif
1494
                                }
1495

    
1496
                                sprintf(video_stats_text, "[VIDEO] qdensity: %d\%% - losses: %d/sec (%ld tot) - skips: %d/sec (%ld tot)%s", (int)video_qdensity, video_statistics.Lossrate, videoq.PacketHistory.LostCount, video_statistics.Skiprate, videoq.PacketHistory.SkipCount, est_psnr_string);
1497
                        }
1498
                        else
1499
                                sprintf(video_stats_text, "waiting for incoming video packets...");
1500
                        
1501
                        if(qoe)
1502
                            ChunkerPlayerGUI_SetStatsText(audio_stats_text, video_stats_text,(qoe>LED_THRS_YELLOW?LED_GREEN:((qoe<=LED_THRS_YELLOW && qoe>LED_THRS_RED)?LED_YELLOW:LED_RED)));
1503
                        else
1504
                            ChunkerPlayerGUI_SetStatsText(audio_stats_text, video_stats_text,LED_GREEN);
1505
                        
1506

    
1507
                        last_stats_evaluation = now;
1508
                }
1509
                
1510
                if((((now.tv_sec*1000)+(now.tv_usec/1000)) - ((last_qoe_evaluation.tv_sec*1000)+(last_qoe_evaluation.tv_usec/1000))) > EVAL_QOE_INTERVAL)
1511
                {
1512
                        // ESTIMATE QoE
1513
                        ChunkerPlayerStats_GetMeanVideoQuality(&(videoq.PacketHistory), &qoe);
1514
                        
1515
#ifdef DEBUG_STATS
1516
                        printf("QoE index: %f\n", (float) qoe);
1517
#endif
1518
                        last_qoe_evaluation = now;
1519
                }
1520
        }
1521
}