Statistics
| Branch: | Revision:

chunker-player / chunker_streamer.c @ e810bf7b

History | View | Annotate | Download (27 KB)

1
// ingestion.c
2
// Author 
3
// Diego Reforgiato
4
// Giuseppe Tropea
5
//
6
// Use the file compile to compile the program to build (assuming libavformat and libavcodec are 
7
// correctly installed your system).
8
//
9
// Run using
10
//
11
// ingestion myvideofile.mpg
12

    
13
#include <libavcodec/avcodec.h>
14
#include <libavformat/avformat.h>
15

    
16
#include <stdio.h>
17

    
18
#include <SDL.h>
19
#include <SDL_thread.h>
20

    
21
#include "chunker_streamer.h"
22
#include "codec_definitions.h"
23

    
24
#ifdef __MINGW32__
25
#undef main /* Prevents SDL from overriding main() */
26
#endif
27

    
28
#define DEBUG_AUDIO_FRAMES
29
#define DEBUG_VIDEO_FRAMES
30
//#define DEBUG_CHUNKER
31

    
32
#define SDL_AUDIO_BUFFER_SIZE 1024
33

    
34
int alphasortNew(const struct dirent **a, const struct dirent **b) {
35
        int idx1 = atoi((*a)->d_name+5);
36
        int idx2 = atoi((*b)->d_name+5);
37
        return (idx2<idx1);
38
//        return (strcmp((*a)->d_name,(*b)->d_name));
39
}
40

    
41
int chunkFilled(ExternalChunk *echunk, ChunkerMetadata *cmeta) {
42
        // different strategies to implement
43
        if(cmeta->strategy==0) // number of frames per chunk constant
44
                if(echunk->frames_num==cmeta->val_strategy)
45
                        return 1;
46
        
47
        if(cmeta->strategy==1) // constant size. Note that for now each chunk will have a size just greater or equal than the required value - It can be considered as constant size. If that is not good we need to change the code. Also, to prevent too low values of strategy_val. This choice is more robust
48
                if(echunk->payload_len>=cmeta->val_strategy)
49
                        return 1;
50
        
51
        return 0;
52
        
53
}
54

    
55
void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame) {
56
        FILE *pFile;
57
        char szFilename[32];
58
        int  y;
59
  
60
  // Open file
61
        sprintf(szFilename, "frame%d.ppm", iFrame);
62

    
63
          pFile=fopen(szFilename, "wb");
64
          if(pFile==NULL)
65
                    return;
66
  
67
  // Write header
68
        fprintf(pFile, "P5\n%d %d\n255\n", width, height);
69
  
70
  // Write pixel data
71
          for(y=0; y<height; y++)
72
                    fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width, pFile);
73
  
74
  // Close file
75
          fclose(pFile);
76
}
77

    
78
void saveChunkOnFile(ExternalChunk *chunk) {
79
        char buf[1024], outfile[1024];
80
/*        FILE *fp;
81
        
82
        strcpy(buf,"chunks//CHUNK");
83
        strcat(buf,"\0");
84
        sprintf(outfile,"%s%d",buf,chunk->seq);
85
        fp = fopen(outfile,"wb");
86
        fwrite(&(chunk->seq),sizeof(int),1,fp);
87
        fwrite(&(chunk->frames_num),sizeof(int),1,fp);
88
        fwrite(&(chunk->start_time),sizeof(struct timeval),1,fp);
89
        fwrite(&(chunk->end_time),sizeof(struct timeval),1,fp);
90
        fwrite(&(chunk->payload_len),sizeof(int),1,fp);
91
        fwrite(&(chunk->len),sizeof(int),1,fp);
92
        fwrite(&(chunk->category),sizeof(int),1,fp);
93
        fwrite(&(chunk->priority),sizeof(double),1,fp);
94
        fwrite(&(chunk->_refcnt),sizeof(int),1,fp);
95
        fwrite(chunk->data,sizeof(uint8_t),sizeof(uint8_t)*chunk->payload_len,fp);
96
        fclose(fp);
97
*/
98
        /* send the chunk to the GRAPES peer application via HTTP */
99
        sprintf(buf, "%slocalhost:%d%s", UL_HTTP_PREFIX, UL_DEFAULT_CHUNKBUFFER_PORT, UL_DEFAULT_CHUNKBUFFER_PATH);
100
        pushChunkHttp(chunk, buf);
101
}
102

    
103
void initChunk(ExternalChunk *chunk,int *seq_num) {
104
        chunk->seq = (*seq_num)++;
105
        chunk->frames_num = 0;
106
        chunk->payload_len = 0;
107
        chunk->len=0;
108
  if(chunk->data != NULL)
109
    free(chunk->data);
110
        chunk->data = NULL;
111
        chunk->start_time.tv_sec = -1;
112
        chunk->start_time.tv_usec = -1;
113
        chunk->end_time.tv_sec = -1;
114
        chunk->end_time.tv_usec = -1;
115
        chunk->priority = 0;
116
        chunk->category = 0;
117
        chunk->_refcnt = 0;
118
}
119

    
120
int main(int argc, char *argv[]) {
121
        int i, videoStream,outbuf_size,out_size,seq_current_chunk = 0,audioStream;
122
        int len1, data_size, stime;
123
        int frameFinished;
124
        int numBytes,outbuf_audio_size,audio_size;
125
        int sizeFrame = 0;
126
        int sizeChunk = 0;
127
        int dir_entries;
128
        int audio_bitrate;
129
        int video_bitrate;
130
        int contFrameAudio=0,contFrameVideo=0;
131
        
132
        uint8_t *buffer,*outbuf,*outbuf_audio;
133
        uint8_t *outbuf_audi_audio,*tempdata;
134
        //uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
135
        uint16_t *audio_buf = NULL;
136

    
137
        unsigned int audio_buf_size = 0;
138
        long double newtimestamp;
139
        
140
        AVFormatContext *pFormatCtx;
141
        AVCodecContext  *pCodecCtx,*pCodecCtxEnc,*aCodecCtxEnc,*aCodecCtx;
142
        AVCodec         *pCodec,*pCodecEnc,*aCodec,*aCodecEnc;
143
        AVFrame         *pFrame; 
144
        AVFrame         *pFrameRGB;
145
        AVPacket         packet;
146
        int64_t                last_pkt_dts=0,delta=0,delta_audio=0,last_pkt_dts_audio=0;
147

    
148
        Frame *frame=NULL;
149

    
150
        ExternalChunk *chunk=NULL;
151
        ExternalChunk *chunkaudio=NULL;
152
        ChunkerMetadata *cmeta=NULL;
153
        
154
        char buf[1024],outfile[1024], basedelfile[1024],delfile[1024];
155
        FILE *fp,*f1;
156

    
157
        long long Now;
158
        short int FirstTimeAudio=1, FirstTimeVideo=1;
159
        long long DeltaTimeAudio=0, DeltaTimeVideo=0, newTime;
160

    
161
        double ptsvideo1,ptsaudio1;
162
        
163
        struct dirent **namelist;
164
        
165
        if(argc < 4) {
166
                fprintf(stderr, "execute ./chunker_streamer moviefile audiobitrate videobitrate\n");
167
                return -1;
168
        }
169
        sscanf(argv[2],"%d",&audio_bitrate);
170
        sscanf(argv[3],"%d",&video_bitrate);
171

    
172
        audio_buf = (uint16_t *)av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
173
        outbuf_audio_size = 10000;
174
        outbuf_audio = malloc(outbuf_audio_size);
175

    
176

    
177
        cmeta = chunkerInit("configChunker.txt");
178
        
179
/*
180
        dir_entries = scandir("chunks",&namelist,NULL,alphasortNew);
181
        if(dir_entries>0) {
182
                strcpy(basedelfile,"chunks/");
183
                for(i=0;i<dir_entries;i++) {
184
                        if(!strcmp(namelist[i]->d_name,".") || !strcmp(namelist[i]->d_name,".."))
185
                                continue;
186
                        strcpy(delfile,basedelfile);
187
                        strcat(delfile,namelist[i]->d_name);
188
                        unlink(delfile);
189
                        free(namelist[i]);
190
                }
191
                free(namelist);
192
                rmdir("chunks");
193
        }
194
        mkdir("chunks",0777);
195
*/
196
        
197
        fprintf(stderr, "Chunkbuffer Strategy:%d Value Strategy:%d\n", cmeta->strategy, cmeta->val_strategy);
198
        f1 = fopen("original.mpg","wb");
199

    
200
        // Register all formats and codecs
201
        av_register_all();
202

    
203
        // Open video file
204
        if(av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL)!=0)
205
                return -1; // Couldn't open file
206
  
207
        // Retrieve stream information
208
        if(av_find_stream_info(pFormatCtx)<0)
209
                return -1; // Couldn't find stream information
210
  
211
        // Dump information about file onto standard error
212
        dump_format(pFormatCtx, 0, argv[1], 0);
213
  
214
        // Find the first video stream
215
        videoStream=-1;
216
        audioStream=-1;
217
        
218
        for(i=0; i<pFormatCtx->nb_streams; i++) {
219
                if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO && videoStream<0) {
220
                        videoStream=i;
221
                }
222
                if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO && audioStream<0) {
223
                        audioStream=i;
224
                }
225
        }
226
        fprintf(stderr, "Num streams : %d TBR: %d %d RFRAMERATE:%d %d Duration:%d\n",pFormatCtx->nb_streams,pFormatCtx->streams[videoStream]->time_base.num,pFormatCtx->streams[videoStream]->time_base.den,pFormatCtx->streams[videoStream]->r_frame_rate.num,pFormatCtx->streams[videoStream]->r_frame_rate.den,pFormatCtx->streams[videoStream]->duration);
227

    
228
        fprintf(stderr, "Video stream has id : %d\n",videoStream);
229
        fprintf(stderr, "Audio stream has id : %d\n",audioStream);
230

    
231
        if(videoStream==-1 && audioStream==-1)
232
                return -1; // Didn't find a video stream
233
  
234
        // Get a pointer to the codec context for the video stream
235
        pCodecCtx=pFormatCtx->streams[videoStream]->codec;
236
        pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
237

    
238
        fprintf(stderr, "Width:%d Height:%d\n",pCodecCtx->width,pCodecCtx->height);
239

    
240
        if(audioStream!=-1) {
241
                aCodecCtx=pFormatCtx->streams[audioStream]->codec;
242
                fprintf(stderr, "AUDIO Codecid: %d %d\n",aCodecCtx->codec_id,aCodecCtx->sample_rate);
243
                fprintf(stderr, "AUDIO channels %d samplerate %d\n",aCodecCtx->channels,aCodecCtx->sample_rate);
244
        }
245

    
246

    
247

    
248
        pCodecCtxEnc=avcodec_alloc_context();
249
#ifdef H264_VIDEO_ENCODER
250
        pCodecCtxEnc->me_range=16;
251
        pCodecCtxEnc->max_qdiff=4;
252
        pCodecCtxEnc->qmin=10;
253
        pCodecCtxEnc->qmax=51;
254
        pCodecCtxEnc->qcompress=0.6;
255
        pCodecCtxEnc->codec_type = CODEC_TYPE_VIDEO;
256
        pCodecCtxEnc->codec_id   = CODEC_ID_H264;//13;//pCodecCtx->codec_id;
257
        pCodecCtxEnc->bit_rate = video_bitrate;///400000;
258
        // resolution must be a multiple of two 
259
        pCodecCtxEnc->width = pCodecCtx->width;
260
        pCodecCtxEnc->height = pCodecCtx->height;
261
        // frames per second 
262
        pCodecCtxEnc->time_base= pCodecCtx->time_base;//(AVRational){1,25};
263
        pCodecCtxEnc->gop_size = 10; // emit one intra frame every ten frames 
264
        //pCodecCtxEnc->max_b_frames=1;
265
        pCodecCtxEnc->pix_fmt = PIX_FMT_YUV420P;
266
#else
267
        pCodecCtxEnc->codec_type = CODEC_TYPE_VIDEO;
268
        pCodecCtxEnc->codec_id   = CODEC_ID_MPEG4;
269
        pCodecCtxEnc->bit_rate = video_bitrate;
270
        pCodecCtxEnc->width = pCodecCtx->width;
271
        pCodecCtxEnc->height = pCodecCtx->height;
272
        // frames per second 
273
        pCodecCtxEnc->time_base= pCodecCtx->time_base;//(AVRational){1,25};
274
        pCodecCtxEnc->gop_size = 10; // emit one intra frame every ten frames 
275
        //pCodecCtxEnc->max_b_frames=1;
276
        pCodecCtxEnc->pix_fmt = PIX_FMT_YUV420P;
277
#endif
278
        fprintf(stderr, "CIC:%d %d %d %d\n", pCodecCtxEnc->time_base.num, pCodecCtxEnc->time_base.den, pCodecCtx->time_base.num, pCodecCtx->time_base.den);
279

    
280

    
281
        aCodecCtxEnc = avcodec_alloc_context();
282
        aCodecCtxEnc->bit_rate = audio_bitrate; //256000
283
        aCodecCtxEnc->sample_fmt = SAMPLE_FMT_S16;
284
        aCodecCtxEnc->sample_rate = aCodecCtx->sample_rate;
285
        aCodecCtxEnc->channels = aCodecCtx->channels;
286
        //fprintf(stderr, "InitAUDIOFRAMESIZE:%d %d\n",aCodecCtxEnc->frame_size,av_rescale(44100,1,25));
287
        fprintf(stderr, "bitrate:%d sample_rate:%d channels:%d\n",aCodecCtxEnc->bit_rate,aCodecCtxEnc->sample_rate,aCodecCtxEnc->channels);
288

    
289
        // Find the decoder for the video stream
290
        
291
        if(audioStream!=-1) {
292
                aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
293
#ifdef MP3_AUDIO_ENCODER
294
                aCodecEnc = avcodec_find_encoder(CODEC_ID_MP3);
295
#else
296
                aCodecEnc = avcodec_find_encoder(CODEC_ID_MP2);
297
#endif
298
                if(aCodec==NULL) {
299
                        fprintf(stderr,"Unsupported acodec!\n");
300
                        return -1;
301
                }
302
                if(aCodecEnc==NULL) {
303
                        fprintf(stderr,"Unsupported acodecEnc!\n");
304
                        return -1;
305
                }
306
        
307
                if(avcodec_open(aCodecCtx, aCodec)<0) {
308
                        fprintf(stderr, "could not open codec\n");
309
                        return -1; // Could not open codec
310
                }
311
                if(avcodec_open(aCodecCtxEnc, aCodecEnc)<0) {
312
                        fprintf(stderr, "could not open codec\n");
313
                        return -1; // Could not open codec
314
                }
315

    
316
        }
317
#ifdef H264_VIDEO_ENCODER
318
        fprintf(stderr, "Setting codecID to H264: %d %d\n",pCodecCtx->codec_id, CODEC_ID_H264);
319
        pCodecEnc = avcodec_find_encoder(CODEC_ID_H264);//pCodecCtx->codec_id);
320
#else
321
        fprintf(stderr, "Setting codecID to mpeg4: %d %d\n",pCodecCtx->codec_id, CODEC_ID_MPEG4);
322
        pCodecEnc = avcodec_find_encoder(CODEC_ID_MPEG4);
323
#endif
324
        if(pCodec==NULL) {
325
                fprintf(stderr, "Unsupported pcodec!\n");
326
                return -1; // Codec not found
327
        }
328
        if(pCodecEnc==NULL) {
329
                fprintf(stderr, "Unsupported pcodecenc!\n");
330
                return -1; // Codec not found
331
        }
332
        if(avcodec_open(pCodecCtx, pCodec)<0) {
333
                fprintf(stderr, "could not open codec\n");
334
                return -1; // Could not open codec
335
        }
336
        if(avcodec_open(pCodecCtxEnc, pCodecEnc)<0) {
337
                fprintf(stderr, "could not open codecEnc\n");
338
                return -1; // Could not open codec
339
        }
340

    
341
        // Allocate video frame
342
        pFrame=avcodec_alloc_frame();
343
        if(pFrame==NULL) {
344
                fprintf(stderr, "Memory error!!!\n");
345
                return -1;
346
        }
347
  
348
        i=0;
349
        outbuf_size = 100000;
350
        outbuf = malloc(outbuf_size);
351
        if(!outbuf) {
352
                fprintf(stderr, "Memory error!!!\n");
353
                return -1;
354
        }
355
        frame = (Frame *)malloc(sizeof(Frame));
356
        if(!frame) {
357
                fprintf(stderr, "Memory error!!!\n");
358
                return -1;
359
        }
360
        sizeFrame = 3*sizeof(int)+sizeof(struct timeval);
361
        chunk = (ExternalChunk *)malloc(sizeof(ExternalChunk));
362
        if(!chunk) {
363
                fprintf(stderr, "Memory error!!!\n");
364
                return -1;
365
        }
366
        sizeChunk = 6*sizeof(int)+2*sizeof(struct timeval)+sizeof(double);
367
    chunk->data=NULL;
368
        initChunk(chunk,&seq_current_chunk);
369
        chunkaudio = (ExternalChunk *)malloc(sizeof(ExternalChunk));
370
        if(!chunkaudio) {
371
                fprintf(stderr, "Memory error!!!\n");
372
                return -1;
373
        }
374
    chunkaudio->data=NULL;
375
        initChunk(chunkaudio,&seq_current_chunk);
376
        stime = -1;
377
        
378
        //av_init_packet(&packet);
379
        
380
        if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
381
                fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
382
                return -1;
383
        }
384

    
385

    
386
        /* initialize the HTTP chunk pusher */
387
        initChunkPusher(); //TRIPLO
388

    
389

    
390
        while(av_read_frame(pFormatCtx, &packet)>=0) {
391
                // Is this a packet from the video stream?
392
                if(packet.stream_index==videoStream) {
393
                        // Decode video frame
394
                        double PTS,glob_PTS;
395

    
396
                        PTS = 0;
397
                        glob_PTS = packet.pts;
398
                        if(avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet)>0) {
399
#ifdef DEBUG_VIDEO_FRAMES
400
                                fprintf(stderr, "Tipo Frame: %d %lld\n",pFrame->pict_type,AV_NOPTS_VALUE);
401
#endif
402
/*                                if(packet.dts==AV_NOPTS_VALUE && pFrame->opaque && *(uint64_t*)pFrame->opaque!=AV_NOPTS_VALUE) {
403
                                        PTS = *(uint64_t *)pFrame->opaque;
404
                                }
405
                                else if(packet.dts!=AV_NOPTS_VALUE) {
406
                                        PTS = packet.dts;
407
                                }
408
                                else
409
                                        PTS = 0;
410
                                PTS *= av_q2d(pCodecCtx->time_base);*/
411

    
412
                                // Did we get a video frame?
413
                                //stime = (int)packet.dts;
414
#ifdef DEBUG_VIDEO_FRAMES
415
                                fprintf(stderr, "VIDEO: stime DTS:%d %d\n",stime,(int)packet.dts);
416
#endif
417
                                if(frameFinished) { // it must be true all the time else error
418
                                        frame->number = contFrameVideo++;//pCodecCtx->frame_number;
419
#ifdef DEBUG_VIDEO_FRAMES
420
                                        fprintf(stderr, "DENTROFRAMEFINISHED %lf\n",PTS);
421
#endif
422
                                        if(frame->number==0) {
423
                                                if(packet.dts==AV_NOPTS_VALUE)
424
                                                        continue;
425
                                                last_pkt_dts = packet.dts;
426
                                                newTime = 0;
427
                                        }
428
                                        else {
429
                                                if(packet.dts!=AV_NOPTS_VALUE) {
430
                                                        delta = packet.dts-last_pkt_dts;
431
                                                        last_pkt_dts = packet.dts;
432
                                                }
433
                                                else if(delta==0)
434
                                                        continue;
435
                                        }
436
#ifdef DEBUG_VIDEO_FRAMES
437
                                        fprintf(stderr, "deltavideo : %lld\n",delta);
438
#endif
439
                                        out_size = avcodec_encode_video(pCodecCtxEnc, outbuf, outbuf_size, pFrame);
440
#ifdef DEBUG_VIDEO_FRAMES
441
                                        fprintf(stderr, "video frame size nuovo : %d\n",pCodecCtx->frame_number);
442
#endif
443
                                        
444
                                        Now=(long long)SDL_GetTicks();
445
#ifdef DEBUG_VIDEO_FRAMES
446
                                        fprintf(stderr, "ptsvideo:%lld %d %d %d %f %d\n", packet.pts, (int)packet.duration, pCodecCtxEnc->time_base.den, pCodecCtxEnc->time_base.num, (double)packet.dts, pCodecCtx->time_base.den);
447
#endif
448

    
449
/*                                        if(packet.duration>0)
450
                                                newtimestamp = ((double)((packet.dts)*pCodecCtxEnc->time_base.num))/((double)(packet.duration*(pCodecCtxEnc->time_base.den)));
451
                                        else
452
                                                newtimestamp =((double)((packet.dts)*pCodecCtxEnc->time_base.num)/((double)pCodecCtxEnc->time_base.den));
453
                                        //frame->timestamp.tv_sec = (long)packet.dts*1.e-5;
454
                                        //frame->timestamp.tv_usec = ((long)packet.dts%100000/100);
455
#ifdef DEBUG_VIDEO_FRAMES
456
                                        //fprintf(stderr, "ts:%llf\n",newtimestamp);
457
#endif
458
                                        if(FirstTimeVideo) {
459
                                                DeltaTimeVideo=Now-((unsigned int)newtimestamp*(unsigned long long)1000+(newtimestamp-(unsigned int)newtimestamp)*1000);
460
                                                FirstTimeVideo = 0;
461
#ifdef DEBUG_VIDEO_FRAMES
462
                                                //fprintf(stderr, "DeltaTimeVideo : %lld\n",DeltaTimeVideo);
463
                                                //printf("%lld\n",Now);
464
#endif
465
                                        }
466
        
467
                                        newTime = DeltaTimeVideo+((unsigned int)newtimestamp*(unsigned long long)1000+(newtimestamp-(unsigned int)newtimestamp)*1000);
468
*/
469
/*                                        char countzeros[10];
470
                                        int countz=0,ic;
471
                                        sprintf(countzeros,"%d",pCodecCtxEnc->time_base.den);
472
                                        for(ic=0;ic<strlen(countzeros);ic++) {
473

474
                                                if(countzeros[ic]=='0')
475
                                                        countz++;
476
                                        }*/
477

    
478
                                        //newTime = (((double)((packet.dts)-dtsvideo1))*1000.0*(double)pCodecCtxEnc->time_base.num)/(pCodecCtxEnc->time_base.den*((double)(packet.duration)));                                                
479
                                        
480
                                        if(FirstTimeVideo && packet.pts>0) {
481
                                                ptsvideo1 = packet.pts;
482
                                                FirstTimeVideo = 0;
483
                                        }
484

    
485
                                        if(frame->number>0)
486
                                                newTime = ((double)packet.pts-ptsvideo1)*1000.0/((double)delta*(double)av_q2d(pFormatCtx->streams[videoStream]->r_frame_rate));
487
                                        
488
                                        //newTime = ((double)((packet.dts)))/(1000*av_q2d(pCodecCtxEnc->time_base));
489
                                        
490
#ifdef DEBUG_VIDEO_FRAMES
491
                                        fprintf(stderr, "newTime : %d\n",newTime);
492
#endif
493
                                        if(newTime<0)
494
                                                continue; //SKIP THIS FRAME (E.G. INTERLACE)
495
        
496
                                        frame->timestamp.tv_sec = (long long)newTime/1000;
497
                                        frame->timestamp.tv_usec = newTime%1000;
498
        
499
                                        frame->size = out_size;
500
                                        frame->type = pFrame->pict_type;
501
#ifdef DEBUG_VIDEO_FRAMES
502
                                        //fprintf(stderr, "video newt:%lf dts:%ld dur:%d sec:%d usec%d pts:%d\n",newtimestamp,(long)packet.dts,packet.duration,frame->timestamp.tv_sec,frame->timestamp.tv_usec,packet.pts);
503
                                        fprintf(stderr, "num:%d size:%d type:%d\n",frame->number,frame->size,frame->type);
504
                                        fprintf(stderr, "VIDEO: tvsec:%d tvusec:%d\n",frame->timestamp.tv_sec,frame->timestamp.tv_usec);
505
#endif
506
                                        // Save the frame to disk
507
                                        ++i;
508
                                        //SaveFrame(pFrame, pCodecCtx->width, pCodecCtx->height, i);
509
#ifdef DEBUG_VIDEO_FRAMES        
510
                                        //fprintf(stderr, "out_size:%d outbuf_size:%d packet.size:%d\n",out_size,outbuf_size,packet.size);
511
                                        //fprintf(stderr, "%d %d\n",pCodecCtxEnc->width,pCodecCtxEnc->height);
512
#endif
513
                                        chunk->frames_num++; // number of frames in the current chunk
514
                                        chunk->data = (uint8_t *)realloc(chunk->data,sizeof(uint8_t)*(chunk->payload_len+out_size+sizeFrame));
515
                                        if(!chunk->data)  {
516
                                                fprintf(stderr, "Memory error!!!\n");
517
                                                return -1;
518
                                        }
519
#ifdef DEBUG_VIDEO_FRAMES
520
                                        //fprintf(stderr, "rialloco data di dim:%d con nuova dim:%d\n",chunk->payload_len,out_size);
521
#endif
522

    
523
                                        tempdata = chunk->data+chunk->payload_len;
524
                                        *((int *)tempdata) = frame->number;
525
                                        tempdata+=sizeof(int);
526
                                        *((struct timeval *)tempdata) = frame->timestamp;
527
                                        tempdata+=sizeof(struct timeval);
528
                                        *((int *)tempdata) = frame->size;
529
                                        tempdata+=sizeof(int);
530
                                        *((int *)tempdata) = frame->type;
531
                                        tempdata+=sizeof(int);
532
                                        
533
                                        memcpy(chunk->data+chunk->payload_len+sizeFrame,outbuf,out_size); // insert new data
534
                                        chunk->payload_len += out_size + sizeFrame; // update payload length
535
                                        //fprintf(stderr, "outsize:%d payload_len:%d\n",out_size,chunk->payload_len);
536
                                        chunk->len = sizeChunk+chunk->payload_len ; // update overall length
537
                                        
538
                                        if(((int)frame->timestamp.tv_sec < (int)chunk->start_time.tv_sec) || ((int)frame->timestamp.tv_sec==(int)chunk->start_time.tv_sec && (int)frame->timestamp.tv_usec < (int)chunk->start_time.tv_usec) || (int)chunk->start_time.tv_sec==-1) {
539
                                                chunk->start_time.tv_sec = frame->timestamp.tv_sec;
540
                                                chunk->start_time.tv_usec = frame->timestamp.tv_usec;
541
                                        }
542
                                        if(((int)frame->timestamp.tv_sec > (int)chunk->end_time.tv_sec) || ((int)frame->timestamp.tv_sec==(int)chunk->end_time.tv_sec && (int)frame->timestamp.tv_usec > (int)chunk->end_time.tv_usec) || (int)chunk->end_time.tv_sec==-1) {
543
                                                chunk->end_time.tv_sec = frame->timestamp.tv_sec;
544
                                                chunk->end_time.tv_usec = frame->timestamp.tv_usec;
545
                                        }
546
                                        stime = -1;
547
        
548
                                        //fprintf(stderr, "Frame video %d stime:%d frame num:%d\n",stime,pCodecCtxEnc->frame_number);
549
                                        if(chunkFilled(chunk,cmeta)) { // is chunk filled using current strategy?
550
                                                //chbAddChunk(chunkbuffer,chunk); // add a chunk to the chunkbuffer
551
                                                //SAVE ON FILE
552
                                                saveChunkOnFile(chunk);
553
                                                initChunk(chunk,&seq_current_chunk);
554
                                        }
555
                                        /* pict_type maybe 1 (I), 2 (P), 3 (B), 5 (AUDIO)*/
556
        
557
                                        fwrite(outbuf, 1, out_size, f1); // reconstructing original video
558
        
559
                                }
560
                        }
561
                }
562
                else if(packet.stream_index==audioStream) {
563
#ifdef DEBUG_AUDIO_FRAMES
564
                        //fprintf(stderr, "packet audio dts:%d dts:%d\n",(int)packet.dts,(int)packet.dts);
565
#endif
566
                        data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
567
                        if(avcodec_decode_audio3(aCodecCtx, audio_buf, &data_size, &packet)>0) {
568
#ifdef DEBUG_AUDIO_FRAMES
569
                                fprintf(stderr, "newTimeaudioSTART : %lf\n",(double)(packet.pts)*av_q2d(pFormatCtx->streams[audioStream]->time_base));
570
#endif
571
                                if(data_size>0) {
572
#ifdef DEBUG_AUDIO_FRAMES
573
                                        fprintf(stderr, "datasizeaudio:%d\n",data_size);
574
#endif
575
                                        /* if a frame has been decoded, output it */
576
                                        //fwrite(audio_buf, 1, data_size, outfileaudio);
577
                                }
578
                                else
579
                                        printf("Error data_size!!\n");
580
        
581
                                audio_size = avcodec_encode_audio(aCodecCtxEnc,outbuf_audio,data_size,audio_buf);
582
#ifdef DEBUG_AUDIO_FRAMES
583
                                //fprintf(stderr, "audio frame size nuovo pippo : %d %d datasize:%d %d %d\n",aCodecCtxEnc->frame_size,aCodecCtxEnc->time_base.den,data_size,packet.size, len1);
584
                                //fprintf(stderr, "oldaudiosize:%d Audio size : %d\n",len1,audio_size);
585
                                //fprintf(stderr, "stream_index:%d flags:%d pos:%d conv_duration:%d dts:%d dts:%d duration:%d\n",packet.stream_index,packet.flags,packet.pos,packet.convergence_duration,packet.dts,packet.dts,packet.duration);
586
#endif                                
587
                                frame->number = contFrameAudio++;//aCodecCtx->frame_number;
588
                                if(frame->number==0) {
589
                                        if(packet.dts==AV_NOPTS_VALUE)
590
                                                continue;
591
                                        last_pkt_dts_audio = packet.dts;
592
                                        newTime = 0;
593
                                }
594
                                else {
595
                                        if(packet.dts!=AV_NOPTS_VALUE) {
596
                                                delta_audio = packet.dts-last_pkt_dts_audio;
597
                                                last_pkt_dts_audio = packet.dts;
598
                                        }
599
                                        else if(delta_audio==0)
600
                                                continue;
601
                                }
602

    
603
                                //newtimestamp = (double)((float)packet.dts/(packet.duration*34.100));
604
                                Now=(long long)SDL_GetTicks();
605
/*
606
                                if(packet.duration>0)
607
                                        newtimestamp = ((double)((packet.dts)*aCodecCtxEnc->time_base.num))/((double)(packet.duration*(aCodecCtxEnc->time_base.den)));
608
                                else
609
                                        newtimestamp =((double)((packet.dts)*aCodecCtxEnc->time_base.num)/((double)aCodecCtxEnc->time_base.den));
610
                                newtimestamp = ((double)((packet.dts)*aCodecCtxEnc->time_base.num))/((double)(aCodecCtxEnc->time_base.den));
611
#ifdef DEBUG_AUDIO_FRAMES
612
                                fprintf(stderr, "ts:%llf\n",newtimestamp);
613
                                //frame->timestamp.tv_sec = (long)packet.dts*1.e-5;
614
                                //frame->timestamp.tv_usec = ((long)packet.dts%100000/100);
615
        
616
                                fprintf(stderr, "dtsaudio:%f %d %d %d\n",(double)packet.dts,(int)packet.duration,aCodecCtxEnc->time_base.den,aCodecCtxEnc->time_base.num);
617
#endif
618
                                if(FirstTimeAudio) {
619
                                        DeltaTimeAudio=Now-((unsigned int)newtimestamp*(unsigned long long)1000+(newtimestamp-(unsigned int)newtimestamp)*1000);
620
                                        FirstTimeAudio = 0;
621
                                }
622
        
623
                                newTime = DeltaTimeAudio+((unsigned int)newtimestamp*(unsigned long long)1000+(newtimestamp-(unsigned int)newtimestamp)*1000);
624
#ifdef DEBUG_AUDIO_FRAMES
625
                                //fprintf(stderr, "%lld %lld\n",newTime/1000,newTime%1000);
626
#endif
627
*/
628
                                if(FirstTimeAudio && packet.pts>0) {
629
                                        ptsaudio1 = packet.pts;
630
                                        FirstTimeAudio = 0;
631
                                }
632

    
633
                                if(frame->number>0)
634
                                        newTime = (((double)packet.pts-ptsaudio1)*1000.0*((double)av_q2d(pFormatCtx->streams[audioStream]->time_base)));//*(double)delta_audio;
635
                        
636
//                                newTime = (aCodecCtx->sample_rate/(2*aCodecCtx->channels))*(((double)((packet.pts)-ptsaudio1))/((double)(packet.duration)))*1000.0*(double)pFormatCtx->streams[audioStream]->time_base.num/(double)pFormatCtx->streams[audioStream]->time_base.den;
637
//                                newTime = (double)(packet.pts)*av_q2d(pFormatCtx->streams[audioStream]->time_base);
638

    
639
        
640
                                frame->timestamp.tv_sec = (unsigned int)newTime/1000;
641
                                frame->timestamp.tv_usec = newTime%1000;
642
#ifdef DEBUG_AUDIO
643
                                fprintf(stderr, "ptsaudio:%f %d %d %d %f\n",(double)packet.pts,(int)packet.duration,pFormatCtx->streams[audioStream]->time_base.num,pFormatCtx->streams[audioStream]->time_base.den,(double)packet.dts);
644
                                //fprintf(stderr, "newTio :  %lf\n",(((double)((packet.pts)-ptsaudio1))/((double)(packet.duration)))*1000.0*(double)pFormatCtx->streams[audioStream]->time_base.num/(double)pFormatCtx->streams[audioStream]->time_base.den);
645

    
646
                                fprintf(stderr, "AUDIO: tvsec:%d tvusec:%d\n",frame->timestamp.tv_sec,frame->timestamp.tv_usec);
647
                                fprintf(stderr, "newTimeaudio : %d\n",newTime);
648
                                fprintf(stderr, "deltaaudio : %lld\n",delta_audio);
649
                                
650
#endif
651
                                //frame->timestamp.tv_sec = (unsigned int)newtimestamp;
652
                                //frame->timestamp.tv_usec = (newtimestamp-frame->timestamp.tv_sec)*1000;
653
        
654
                                //fprintf(stderr, "Audio time nuovo :%lld %lld\n",Now,DeltaTimeAudio+((long long)frame->timestamp.tv_sec*(unsigned long long)1000+frame->timestamp.tv_usec));
655
        
656
                                //fprintf(stderr, "audio newt:%lf dts:%ld dur:%d sec:%d usec%d pts:%d\n",newtimestamp,(long)packet.dts,packet.duration,frame->timestamp.tv_sec,frame->timestamp.tv_usec,packet.pts);
657
                                frame->size = audio_size;
658
                                frame->type = 5; // 5 is audio type
659
                                
660
                                chunkaudio->frames_num++; // number of frames in the current chunk
661
                                
662
                                chunkaudio->data = (uint8_t *)realloc(chunkaudio->data,sizeof(uint8_t)*(chunkaudio->payload_len+audio_size+sizeFrame));
663
                                if(!chunkaudio->data) {
664
                                        fprintf(stderr, "Memory error!!!\n");
665
                                        return -1;
666
                                }
667
                                tempdata = chunkaudio->data+chunkaudio->payload_len;
668
                                *((int *)tempdata) = frame->number;
669
                                tempdata+=sizeof(int);
670
                                *((struct timeval *)tempdata) = frame->timestamp;
671
                                tempdata+=sizeof(struct timeval);
672
                                *((int *)tempdata) = frame->size;
673
                                tempdata+=sizeof(int);
674
                                *((int *)tempdata) = frame->type;
675
                                tempdata+=sizeof(int);
676
                                        
677
                                memcpy(chunkaudio->data+chunkaudio->payload_len+sizeFrame,outbuf_audio,audio_size);
678
                                chunkaudio->payload_len += audio_size + sizeFrame; // update payload length
679
                                        //fprintf(stderr, "outsize:%d payload_len:%d\n",out_size,chunk->payload_len);
680
                                chunkaudio->len = sizeChunk+chunkaudio->payload_len ; // update overall length
681
                                
682
        //                        stime = (int)packet.dts;
683
                                //fprintf(stderr, "AUDIO: stime:%d\n",stime);
684
                                if(((int)frame->timestamp.tv_sec < (int)chunkaudio->start_time.tv_sec) || ((int)frame->timestamp.tv_sec==(int)chunkaudio->start_time.tv_sec && (int)frame->timestamp.tv_usec < (int)chunkaudio->start_time.tv_usec) || (int)chunkaudio->start_time.tv_sec==-1) {
685
                                        chunkaudio->start_time.tv_sec = frame->timestamp.tv_sec;
686
                                        chunkaudio->start_time.tv_usec = frame->timestamp.tv_usec;
687
                                }
688
                                if(((int)frame->timestamp.tv_sec > (int)chunkaudio->end_time.tv_sec) || ((int)frame->timestamp.tv_sec==(int)chunkaudio->end_time.tv_sec && (int)frame->timestamp.tv_usec > (int)chunkaudio->end_time.tv_usec) || (int)chunkaudio->end_time.tv_sec==-1) {
689
                                        chunkaudio->end_time.tv_sec = frame->timestamp.tv_sec;
690
                                        chunkaudio->end_time.tv_usec = frame->timestamp.tv_usec;
691
                                }
692
                //                if(stime < chunkaudio->start_time.tv_usec || chunkaudio->start_time.tv_usec==-1)
693
                        //                chunkaudio->start_time.tv_usec = stime;
694
                        //        if(stime > chunkaudio->end_time.tv_usec || chunkaudio->end_time.tv_usec==-1)
695
                        //                chunkaudio->end_time.tv_usec = stime;
696
                                stime=-1;
697
                                chunkaudio->priority = 1;
698
#ifdef DEBUG_AUDIO_FRAMES
699
                                //fprintf(stderr, "Frame audio stime:%d frame_num:%d\n",stime,aCodecCtxEnc->frame_number);
700
#endif
701
                                if(chunkFilled(chunkaudio,cmeta)) { // is chunk filled using current strategy?
702
                                        //chbAddChunk(chunkbuffer,chunkaudio); // add a chunk to the chunkbuffer
703
                                        //SAVE ON FILE
704
                                        saveChunkOnFile(chunkaudio);
705
                                        initChunk(chunkaudio,&seq_current_chunk);
706
                                }
707
#ifdef DEBUG_AUDIO_FRAMES
708
                                //fprintf(stderr,"readingQUIPEPPE3\n");
709
#endif
710
                        }
711
                }
712
                else {
713
#ifdef DEBUG_AUDIO_FRAMES
714
                        fprintf(stderr,"Free the packet that was allocated by av_read_frame\n");
715
#endif
716
                        av_free_packet(&packet);
717
                }
718
        }
719

    
720
        if(chunk->frames_num>0) {
721
                //chbAddChunk(chunkbuffer,chunk);
722
                //SAVE ON FILE
723
                saveChunkOnFile(chunk);
724
        }
725
        if(chunkaudio->frames_num>0) {
726
                //chbAddChunk(chunkbuffer,chunkaudio);
727
                //SAVE ON FILE
728
                saveChunkOnFile(chunkaudio);
729
                
730
        }
731

    
732

    
733
        /* initialize the HTTP chunk pusher */
734
        finalizeChunkPusher();
735

    
736

    
737
        free(chunk);
738
        free(chunkaudio);
739
        free(frame);
740
        free(outbuf);
741
        free(outbuf_audio);
742
        fclose(f1);
743

    
744
        // Writing chunk files
745
#ifdef DEBUG_CHUNKER
746
        fprintf(stderr, "cmetasize:%d\n",cmeta->size);
747
        for(i=0;i<cmeta->size;i++) {
748
                fprintf(stderr, "seq:%d pay_len:%d frames_num:%d priority:%f ssec:%d susec:%d esec:%d eusec:%d\n",cmeta->echunk[i].seq,cmeta->echunk[i].payload_len,cmeta->echunk[i].frames_num,cmeta->echunk[i].priority,(int)cmeta->echunk[i].start_time.tv_sec,(int)cmeta->echunk[i].start_time.tv_usec,(int)cmeta->echunk[i].end_time.tv_sec,(int)cmeta->echunk[i].end_time.tv_usec);
749
        }
750
#endif
751

    
752
        // Free the YUV frame
753
        av_free(pFrame);
754
        av_free(audio_buf);
755
  
756
        // Close the codec
757
        avcodec_close(pCodecCtx);
758
        avcodec_close(pCodecCtxEnc);
759

    
760
        if(audioStream!=-1) {
761
                avcodec_close(aCodecCtx);
762
                avcodec_close(aCodecCtxEnc);
763
        }
764
  
765
        // Close the video file
766
        av_close_input_file(pFormatCtx);
767
  
768
    SDL_Quit();
769
        return 0;
770
}