-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1210 lines (1024 loc) · 36.9 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>
#include <libswscale/swscale.h>
#include <libavutil/avstring.h>
#include <libavutil/avutil.h>
#include <libavutil/imgutils.h>
#include <time.h>
#include <assert.h>
#include <arpa/inet.h>
#include <sys/socket.h>
// Increase:
// analyzeduration
// probesize
#include <SDL2/SDL.h>
#include <SDL2/SDL_thread.h>
//// A global buffer size just to have one
//#define BUFFER_SIZE 640
//
//// The IP where the broadcast is sent to (the whole network)
//#define DISCOVER_BROADCAST_IP "255.255.255.255"
//// The port where the broadcast is sent to
//#define DISCOVER_BROADCAST_PORT 51205
//// The port we listen when waiting for streams
//#define REGISTER_BROADCAST_PORT 51206
//
//#define RPI_COMMAND_IP "192.168.0.100" // The ip of the rpi to send commands to
//#define RPI_COMMAND_PORT 51200 // The port of the rpi to send commands to
#define FF_REFRESH_EVENT (SDL_USEREVENT)
#define FF_QUIT_EVENT (SDL_USEREVENT + 1)
#define VIDEO_PICTURE_QUEUE_SIZE 1
#define MAX_VIDEOQ_SIZE (5 * 256 * 1024)
//#define SDL_AUDIO_BUFFER_SIZE 1024
#define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000
#define MAX_AUDIO_FRAME_SIZE 192000
typedef struct PacketQueue {
AVPacketList *first_pkt, *last_pkt;
int nb_packets;
int size;
SDL_mutex *mutex;
SDL_cond *cond;
} PacketQueue;
typedef struct VideoPicture {
//SDL_Overlay *bmp;
int width, height; /* source height & width */
int allocated;
} VideoPicture;
typedef struct VideoState {
AVFormatContext *pFormatCtx;
int videoStream;
int audioStream;
AVStream *audio_st;
AVCodecContext *audio_ctx;
PacketQueue audioq;
uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 2)];
unsigned int audio_buf_size;
unsigned int audio_buf_index;
AVFrame audio_frame;
AVPacket audio_pkt;
uint8_t *audio_pkt_data;
int audio_pkt_size;
AVStream *video_st;
AVCodecContext *video_ctx;
PacketQueue videoq;
struct SwsContext *sws_ctx;
VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
int pictq_size, pictq_rindex, pictq_windex;
SDL_mutex *pictq_mutex;
SDL_cond *pictq_cond;
SDL_Thread *parse_tid;
SDL_Thread *video_tid;
SDL_Texture *texture;
char url[1024];
int quit;
int audio_write_buf_size;
} VideoState;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//const int FRAMES_PER_SECOND = 10;
//
//bool isRunning;
//
bool initSDL();
bool createRenderer();
void setupRenderer();
void setupButtonPositions();
SDL_mutex *screen_mutex = NULL;
SDL_Window *gWindow = NULL;
SDL_Renderer *renderer = NULL;
SDL_Rect btnStart;
SDL_Rect btnStop;
// TODO: Put in VideoState
size_t yPlaneSz, uvPlaneSz;
Uint8 *yPlane, *uPlane, *vPlane;
int uvPitch;
static Uint8 *audio_chunk;
static Uint32 audio_len;
static Uint8 *audio_pos;
struct SwrContext *au_convert_ctx;
#define SCROOBY_IP "127.0.0.1"
#define SCROOBY_PORT 1235
#define SCROOBY_BUFFER_SIZE 5000
void network_send_udp(const void *data, size_t size) {
int s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (s < 0) {
printf("Could not create socket");
exit(-1);
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(SCROOBY_IP);
addr.sin_port = htons(SCROOBY_PORT);
printf("Send data with length: %d", size);
int sizeLeftToSend = size;
for (int i = 0; i < size; i+=SCROOBY_BUFFER_SIZE) {
int buffSizeToSend = SCROOBY_BUFFER_SIZE;
if (sizeLeftToSend < SCROOBY_BUFFER_SIZE) {
buffSizeToSend = sizeLeftToSend;
}
printf("Send: %d bytes\n", buffSizeToSend);
data = data + i;
int result = sendto(s, data, buffSizeToSend, 0, (struct sockaddr *)&addr, sizeof(addr));
if (result < 0) {
printf("Could not send data. Result: %d\n", result);
exit(-1);
}
sizeLeftToSend -= SCROOBY_BUFFER_SIZE;
}
}
// Since we only have one decoding thread, the Big Struct can be global in case we need it.
VideoState *global_video_state;
void packet_queue_init(PacketQueue *q) {
memset(q, 0, sizeof(PacketQueue));
q->mutex = SDL_CreateMutex();
q->cond = SDL_CreateCond();
}
int packet_queue_put(PacketQueue *q, AVPacket *pkt) {
pthread_t ptid = pthread_self();
//printf("[packet_queue_put] Queue: %p / Thread: %d\n", q, ptid);
AVPacketList *pkt1;
// TODO: use AVPacket tmp_pkt = {0}; instead
// TODO:
/*
ret = av_packet_ref(&tmp_pkt, pkt);
if (ret < 0)
exit_program(1);
*/
if(av_dup_packet(pkt) < 0) {
return -1;
}
pkt1 = av_malloc(sizeof(AVPacketList));
if (!pkt1)
return -1;
pkt1->pkt = *pkt;
pkt1->next = NULL;
SDL_LockMutex(q->mutex);
if (!q->last_pkt)
q->first_pkt = pkt1;
else
q->last_pkt->next = pkt1;
q->last_pkt = pkt1;
q->nb_packets++;
q->size += pkt1->pkt.size;
// if (q->nb_packets < 5) {
// SDL_UnlockMutex(q->mutex);
// return 0;
// }
//printf("[packet_queue_put] Packet at: %p\n", pkt1);
//printf("[packet_queue_put] Queue size: %d / %d. Signal waiter.\n", q->nb_packets, q->size);
SDL_CondSignal(q->cond);
//printf("[packet_queue_put] Unlock mutex\n");
SDL_UnlockMutex(q->mutex);
//printf("[packet_queue_put] OK\n");
return 0;
}
static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block) {
pthread_t ptid = pthread_self();
//printf("[packet_queue_get] Queue: %p / Thread: %d\n", q, ptid);
AVPacketList *pkt1 = NULL;
int ret = 0;
//printf("[packet_queue_get] Before lock\n");
SDL_LockMutex(q->mutex);
//printf("[packet_queue_get] After lock\n");
while(true) {
if(global_video_state->quit) {
ret = -1;
break;
}
pkt1 = q->first_pkt;
//printf("[packet_queue_get] pkt1 at: %p\n", pkt1);
if (pkt1) {
//printf("[packet_queue_get] 1\n");
q->first_pkt = pkt1->next;
if (!q->first_pkt)
q->last_pkt = NULL;
q->nb_packets--;
q->size -= pkt1->pkt.size;
*pkt = pkt1->pkt;
//printf("[packet_queue_get] Queue: %p / Thread: %d, Got packet at: %p\n", q,ptid, pkt);
av_free(pkt1);
ret = 1;
break;
} else if (!block) {
//printf("[packet_queue_get] 2\n");
ret = 0;
break;
} else {
//printf("[packet_queue_get] BLOCK\n");
SDL_CondWait(q->cond, q->mutex);
//printf("[packet_queue_get] UNBLOCK\n");
}
}
//printf("[packet_queue_get] Before unlock\n");
SDL_UnlockMutex(q->mutex);
//printf("[packet_queue_get] End.\n");
return ret;
}
int audio_decode_frame(VideoState *is, uint8_t *audio_buf, int buf_size) {
int len1, data_size = 0;
AVPacket *pkt = &is->audio_pkt;
for(;;) {
//printf("[audio_decode_frame] is->audio_pkt_size = %d\n", is->audio_pkt_size);
while(is->audio_pkt_size > 0) {
int got_frame = 0;
len1 = avcodec_decode_audio4(is->audio_ctx, &is->audio_frame, &got_frame, pkt);
if(len1 < 0) {
//printf("[audio_decode_frame] HANDLE ERROR\n");
/* if error, skip frame */
is->audio_pkt_size = 0;
break;
}
data_size = 0;
if(got_frame) {
//printf("[audio_decode_frame] We got a frame, resample it\n");
AVFrame *pFrame = &is->audio_frame;
printf("PTS (audio): %d\n", pFrame->pts);
swr_convert(au_convert_ctx,&audio_buf, MAX_AUDIO_FRAME_SIZE,(const uint8_t **)pFrame->data , pFrame->nb_samples);
data_size = av_samples_get_buffer_size(NULL,
is->audio_ctx->channels,
is->audio_frame.nb_samples,
is->audio_ctx->sample_fmt,
1);
assert(data_size <= buf_size);
//memcpy(audio_buf, is->audio_frame.data[0], data_size);
} else {
//printf("[audio_decode_frame] We NOT GOT a frame, wait for it\n");
}
is->audio_pkt_data += len1;
is->audio_pkt_size -= len1;
if(data_size <= 0) {
//printf("[audio_decode_frame] No data yet, get more frames\n");
/* No data yet, get more frames */
continue;
}
//printf("[audio_decode_frame] We have data. Size: %d\n", data_size);
/* We have data, return it and come back for more later */
return data_size;
}
if(pkt->data)
av_free_packet(pkt);
if(is->quit) {
return -1;
}
/* next packet */
//printf("[audio_decode_frame] Get next packet\n");
int ret = packet_queue_get(&is->audioq, pkt, 1);
//printf("[audio_decode_frame] Get next packet -\n");
if(ret < 0) {
return -1;
}
//printf("[audio_decode_frame] Get next packet: DONE. Size: %d\n", pkt->size);
is->audio_pkt_data = pkt->data;
is->audio_pkt_size = pkt->size;
}
}
void audio_callback(void *userdata, Uint8 *stream, int len) {
pthread_t ptid = pthread_self();
//printf("Thread: %d\n", ptid);
// printf("---> Callback called: %d\n", len);
//
//SDL 2.0
//SDL_memset(stream, 0, len);
//
// VideoState *is = (VideoState *)userdata;
// int len1, audio_size;
//
// while(len > 0) {
// if(is->audio_buf_index >= is->audio_buf_size) {
// /* We have already sent all our data; get more */
// audio_size = audio_decode_frame(is, is->audio_buf, sizeof(is->audio_buf));
// if(audio_size < 0) {
// printf("TODO: Handle error\n");
// /* If error, output silence */
// //is->audio_buf_size = 1152;
// //memset(is->audio_buf, 0, is->audio_buf_size);
// } else {
// printf("Success: Decoded with size %d\n", audio_size);
// is->audio_buf_size = audio_size;
// }
// is->audio_buf_index = 0;
// }
// len1 = is->audio_buf_size - is->audio_buf_index;
// if(len1 > len)
// len1 = len;
//
// printf("Copy stuff with length %d / %d / %d\n", len1, is->audio_buf_index, stream);
//
//
// //SDL_MixAudio(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1, SDL_MIX_MAXVOLUME);
// SDL_MixAudio(stream, (uint8_t *)is->audio_buf, len1, SDL_MIX_MAXVOLUME);
// //memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
//
//
// len -= len1;
// stream += len1;
// is->audio_buf_index += len1;
// }
//
// printf("<--- Callback done: %d\n", len);
//printf("---> Callback called: %d\n", len);
//SDL 2.0
SDL_memset(stream, 0, len);
VideoState *is = (VideoState *)userdata;
int got_frame = 0;
AVPacket *packet = &is->audio_pkt;
//AVFrame *pFrame = &is->audio_frame;
//printf("[audio_decode_frame] Get next packet\n");
int ret = packet_queue_get(&is->audioq, packet, 0);
//printf("[audio_decode_frame] Get next packet -\n");
if(ret < 0) {
printf("Error.\n");
return;
}
if (ret == 0) {
printf("[audio_decode_frame] Ignore packet.\n");
return;
}
ret = avcodec_decode_audio4( is->audio_ctx, &is->audio_frame, &got_frame, packet);
if ( ret < 0 ) {
printf("Error in decoding audio frame.\n");
av_packet_unref(packet);
return;
}
uint8_t *audio_buf = is->audio_buf;
if (got_frame > 0) {
AVFrame *pFrame = &is->audio_frame;
printf("PTS (audio): %d\n", pFrame->pts);
ret = swr_convert(au_convert_ctx, &audio_buf,MAX_AUDIO_FRAME_SIZE, (const uint8_t **)pFrame->data, pFrame->nb_samples);
//printf("index:%5d\t pts:%d / %d\n",packet->pts,packet->size, ret);
}
//SDL_MixAudio(stream, (uint8_t *)is->audio_buf, len, SDL_MIX_MAXVOLUME);
SDL_MixAudioFormat(stream, (uint8_t *)is->audio_buf, AUDIO_S16SYS, len, SDL_MIX_MAXVOLUME);
//SDL_Delay(10);
av_packet_unref(packet);
//printf("<--- Callback done: %d\n", len);
// printf("Callback called: %d\n", len);
//
// //SDL 2.0
// SDL_memset(stream, 0, len);
//
// if (audio_len ==0)
// return;
//
// len = ( len > audio_len ? audio_len : len );
// //SDL_memcpy (stream, audio_pos, len); // simply copy from one buffer into the other
// SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);// mix from one buffer into another
//
// audio_pos += len;
// audio_len -= len;
}
bool initSDL() {
// Initialization flag
bool success = true;
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0) {
printf("[initSDL] Error: SDL could not initialize. SDL_Error: %s\n", SDL_GetError());
success = false;
} else {
// Create window
gWindow = SDL_CreateWindow("Livestream", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == NULL) {
printf("[initSDL] Error: Window could not be created. SDL_Error: %s\n", SDL_GetError());
success = false;
} else {
// We're ready to go
printf("[initSDL] Success: We're ready to go\n");
createRenderer();
setupRenderer();
setupButtonPositions();
}
}
return success;
}
//
//void closeSDL() {
// //Destroy window
// SDL_DestroyWindow(gWindow);
// gWindow = NULL;
//
// //Quit SDL subsystems
// SDL_Quit();
//}
//
//void startTimer() {
// startTicks = SDL_GetTicks();
//}
//
//int getTicks() {
// return SDL_GetTicks() - startTicks;
//}
//
bool createRenderer() {
renderer = SDL_CreateRenderer(gWindow, -1, 0);
if (renderer == NULL) {
printf("[createRenderer] Failed to create renderer.\n");
return false;
}
return true;
}
void setupRenderer() {
// Set size of renderer to the same as window
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
// Set color of renderer to green
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
}
void setupButtonPositions() {
// Start
btnStart.x = 10;
btnStart.y = 10;
btnStart.w = 100;
btnStart.h = 50;
// Stop
btnStop.x = 120;
btnStop.y = 10;
btnStop.w = 100;
btnStop.h = 50;
}
static Uint32 sdl_refresh_timer_cb(Uint32 interval, void *opaque) {
SDL_Event event;
event.type = FF_REFRESH_EVENT;
event.user.data1 = opaque;
SDL_PushEvent(&event);
return 0; /* 0 means stop timer */
}
static void schedule_refresh(VideoState *is, int delay) {
//printf("[main] Schedule refresh with delay %d.\n", delay);
SDL_AddTimer(delay, sdl_refresh_timer_cb, is);
}
void showButton(SDL_Rect rect) {
// Change color to blue
SDL_SetRenderDrawColor(renderer, 0, 100, 200, 255);
// Render our "player"
SDL_RenderFillRect(renderer, &rect);
// Change color to green
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
}
int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt) {
int ret;
*got_frame = 0;
if (pkt) {
ret = avcodec_send_packet(avctx, pkt);
// In particular, we don't expect AVERROR(EAGAIN), because we read all
// decoded frames with avcodec_receive_frame() until done.
if (ret < 0)
return ret == AVERROR_EOF ? 0 : ret;
}
ret = avcodec_receive_frame(avctx, frame);
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
return ret;
if (ret >= 0)
*got_frame = 1;
return 0;
}
int queue_picture(VideoState *is, AVFrame *pFrame) {
//printf("[queue_picture]\n");
VideoPicture *vp;
int dst_pix_fmt;
//AVPicture pict;
/* wait until we have space for a new pic */
SDL_LockMutex(is->pictq_mutex);
while(is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE &&
!is->quit) {
SDL_CondWait(is->pictq_cond, is->pictq_mutex);
}
SDL_UnlockMutex(is->pictq_mutex);
if(is->quit)
return -1;
// windex is set to 0 initially
vp = &is->pictq[is->pictq_windex];
if(is->texture) {
//printf("YES, texture is set. display it\n");
AVFrame* pFrameYUV = av_frame_alloc();
int numBytes = av_image_get_buffer_size(
AV_PIX_FMT_YUV420P,//PIX_FMT_YUV420P,
is->video_ctx->width,
is->video_ctx->height, 16);
uint8_t* buffer = (uint8_t *)av_malloc( numBytes*sizeof(uint8_t) );
av_image_fill_arrays (pFrameYUV->data, pFrameYUV->linesize, buffer, AV_PIX_FMT_YUV420P, is->video_ctx->width, is->video_ctx->height, 1);
pFrameYUV->data[0] = yPlane;
pFrameYUV->data[1] = uPlane;
pFrameYUV->data[2] = vPlane;
pFrameYUV->linesize[0] = is->video_ctx->width;
pFrameYUV->linesize[1] = uvPitch;
pFrameYUV->linesize[2] = uvPitch;
// Convert the image into YUV format that SDL uses
sws_scale(is->sws_ctx, (uint8_t const * const *) pFrame->data,
pFrame->linesize, 0, is->video_ctx->height, pFrameYUV->data,
pFrameYUV->linesize);
av_frame_free(&pFrameYUV);
av_free(buffer);
/* now we inform our display thread that we have a pic ready */
if(++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE) {
is->pictq_windex = 0;
}
SDL_LockMutex(is->pictq_mutex);
is->pictq_size++;
SDL_UnlockMutex(is->pictq_mutex);
}
return 0;
}
int frame_to_jpeg(VideoState *is, AVFrame *frame, int frameNo) {
printf("Write frame to .jpg file\n");
AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
if (!jpegCodec) {
return -1;
}
AVCodecContext *jpegContext = avcodec_alloc_context3(jpegCodec);
if (!jpegContext) {
return -1;
}
printf("Codec ctx: %d, %d\n", jpegContext->pix_fmt, jpegContext->bit_rate);
//jpegContext->pix_fmt = is->video_ctx->pix_fmt;
jpegContext->pix_fmt = AV_PIX_FMT_YUVJ420P;
jpegContext->height = frame->height;
jpegContext->width = frame->width;
jpegContext->sample_aspect_ratio = is->video_ctx->sample_aspect_ratio;
jpegContext->time_base = is->video_ctx->time_base;
// jpegContext->compression_level = 100;
jpegContext->compression_level = 0;
jpegContext->thread_count = 1;
jpegContext->prediction_method = 1;
jpegContext->flags2 = 0;
//jpegContext->rc_max_rate = jpegContext->rc_min_rate = jpegContext->bit_rate = 80000000;
if (avcodec_open2(jpegContext, jpegCodec, NULL) < 0) {
return -1;
}
FILE *JPEGFile;
char JPEGFName[256];
AVPacket packet = {.data = NULL, .size = 0};
av_init_packet(&packet);
int gotFrame;
av_dump_format(is->pFormatCtx, 0, "", 0);
if (avcodec_encode_video2(jpegContext, &packet, frame, &gotFrame) < 0) {
return -1;
}
sprintf(JPEGFName, "dvr-%06d.jpg", frameNo);
JPEGFile = fopen(JPEGFName, "wb");
fwrite(packet.data, 1, packet.size, JPEGFile);
fclose(JPEGFile);
//network_send_udp(packet.data, packet.size);
av_free_packet(&packet);
avcodec_close(jpegContext);
return 0;
}
int video_thread(void *arg) {
//printf("[video_thread]\n");
VideoState *is = (VideoState *)arg;
AVFrame *pFrame = NULL;
// TODO: Try with = NULL instead of creating a dummy object
AVPacket pkt1;
AVPacket *packet = &pkt1;
int frameFinished = 0;
// Allocate video frame
pFrame = av_frame_alloc();
int count = 0;
while(true) {
if(packet_queue_get(&is->videoq, packet, 1) < 0) {
printf("[video_thread] We quit getting packages.\n");
// Means we quit getting packets
break;
}
//printf("[video_thread] Decode another frame.\n");
decode(is->video_ctx, pFrame, &frameFinished, packet);
// Did we get a video frame?
if (frameFinished) {
count++;
if (count == 10) {
frame_to_jpeg(is, pFrame, count);
}
printf("PTS (video): %d, %d\n", pFrame->pts, packet->dts);
//printf("[video_thread] Frame is finished.\n");
if(queue_picture(is, pFrame) < 0) {
break;
}
} else {
//printf("[video_thread] Frame is NOT finished.\n");
}
av_packet_unref(packet);
}
av_frame_free(&pFrame);
return 0;
}
int stream_component_open(VideoState *is, int stream_index) {
AVFormatContext *pFormatCtx = is->pFormatCtx;
AVCodecContext *codecCtx = NULL;
AVCodec *codec = NULL;
SDL_AudioSpec wanted_spec, spec;
if(stream_index < 0 || stream_index >= pFormatCtx->nb_streams) {
return -1;
}
codec = avcodec_find_decoder(pFormatCtx->streams[stream_index]->codec->codec_id);
if(!codec) {
fprintf(stderr, "Unsupported codec!\n");
return -1;
}
codecCtx = avcodec_alloc_context3(codec);
if(avcodec_copy_context(codecCtx, pFormatCtx->streams[stream_index]->codec) != 0) {
fprintf(stderr, "Couldn't copy codec context");
return -1; // Error copying codec context
}
if(codecCtx->codec_type == AVMEDIA_TYPE_AUDIO) {
// We want:
// * Stereo output (= 2 channels)
uint64_t out_channel_layout = AV_CH_LAYOUT_STEREO;
int out_channels=av_get_channel_layout_nb_channels(out_channel_layout);
// * Samples: AAC-1024 MP3-1152
int out_nb_samples=codecCtx->frame_size;
wanted_spec.freq = 44100;
wanted_spec.format = AUDIO_S16SYS;
wanted_spec.channels = out_channels;
wanted_spec.silence = 0;
wanted_spec.samples = out_nb_samples;
wanted_spec.callback = audio_callback;
wanted_spec.userdata = is;
/*if(SDL_OpenAudio(&wanted_spec, &spec) < 0) {
fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
return -1;
}*/
const char* d = SDL_GetAudioDeviceName(0, 1);
printf("Capture: %s", d);
const char* e = SDL_GetAudioDeviceName(0, 0);
printf("Device: %s", e);
// Returns a valid device ID that is > 0 on success or 0 on failure
//int audioDeviceId = SDL_OpenAudioDevice(d, 1, &wanted_spec, &spec, 0);
int audioDeviceId = SDL_OpenAudioDevice(NULL, 0, &wanted_spec, &spec, SDL_AUDIO_ALLOW_ANY_CHANGE);
printf("[stream_component_open] Device id: %d.", audioDeviceId);
if (audioDeviceId == 0) {
printf("[stream_component_open] Failed to open audio: %s.", SDL_GetError());
return -1;
} else {
if (spec.format != wanted_spec.format) {
printf("[stream_component_open] We didn't get Float32 audio format.");
}
// Start audio playing
SDL_PauseAudioDevice(audioDeviceId, 0);
}
}
if(avcodec_open2(codecCtx, codec, NULL) < 0) {
fprintf(stderr, "Unsupported codec!\n");
return -1;
}
switch(codecCtx->codec_type) {
case AVMEDIA_TYPE_AUDIO:
is->audioStream = stream_index;
is->audio_st = pFormatCtx->streams[stream_index];
is->audio_ctx = codecCtx;
is->audio_buf_size = 0;
is->audio_buf_index = 0;
memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
packet_queue_init(&is->audioq);
SDL_PauseAudio(0);
printf("Openend audio\n");
break;
case AVMEDIA_TYPE_VIDEO:
is->videoStream = stream_index;
is->video_st = pFormatCtx->streams[stream_index];
is->video_ctx = codecCtx;
packet_queue_init(&is->videoq);
// TODO: Do not forget to enable
is->video_tid = SDL_CreateThread(video_thread, "video_thread", is);
is->sws_ctx = sws_getContext(is->video_ctx->width, is->video_ctx->height,
is->video_ctx->pix_fmt, is->video_ctx->width,
is->video_ctx->height, AV_PIX_FMT_YUV420P,
SWS_BILINEAR, NULL, NULL, NULL
);
is->texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12,
SDL_TEXTUREACCESS_STREAMING, is->video_ctx->width, is->video_ctx->height);
uvPitch = is->video_ctx->width / 2;
// set up YV12 pixel array (12 bits per pixel)
yPlaneSz = is->video_ctx->width * is->video_ctx->height;
uvPlaneSz = is->video_ctx->width * is->video_ctx->height / 4;
yPlane = (Uint8*) malloc(yPlaneSz);
uPlane = (Uint8*) malloc(uvPlaneSz);
vPlane = (Uint8*) malloc(uvPlaneSz);
if (!yPlane || !uPlane || !vPlane) {
fprintf(stderr, "Could not allocate pixel buffers - exiting\n");
exit(1);
}
printf("Openend video\n");
break;
default:
break;
}
}
int decode_thread(void *arg) {
//printf("[decode_thread] Called.\n");
VideoState *is = (VideoState *)arg;
AVFormatContext *pFormatCtx = NULL;
//AVCodecContext *pCodecCtxOrig = NULL;
AVCodecContext *pCodecCtx = NULL;
AVCodec *pCodec = NULL;
AVPacket pkt1;
AVPacket *packet = &pkt1;
int video_index = -1;
int audio_index = -1;
int i = 0;
is->videoStream = -1;
global_video_state = is;
int testing = 1;
while (testing == 1) {
AVInputFormat *inputFormat =av_find_input_format("mpegts");
int result = avformat_open_input(&pFormatCtx, is->url, inputFormat, NULL);
if (result != 0) {
printf("[decode_thread] Error: Can't open input.\n");
return -1;
}
printf("[decode_thread] Success: Opened input stream to %s.\n", is->url);
is->pFormatCtx = pFormatCtx;
printf("[decode_thread] Get stream information\n");
// Retrieve stream information
result = avformat_find_stream_info(pFormatCtx, NULL);
if (result < 0) {
printf("[decode_thread] Error: Can't find stream information.\n");
return -1;
}
printf("[decode_thread] Success: Got stream information.\n");
av_dump_format(pFormatCtx, 0, is->url, 0);
printf("[decode_thread] Format name: %s.\n", pFormatCtx->iformat->name);
// Find the first video stream
video_index = -1;
for (i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
video_index < 0) {
video_index = i;
}
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
audio_index < 0) {
audio_index=i;
}
}
/*
* stream_component_open() start
*/
printf("[decode_thread] Video stream index: %d.\n", video_index);
if (video_index < 0) {
printf("[decode_thread] Could not find a video stream.\n");
return -1;
}
if (audio_index < 0) {
printf("[decode_thread] Could not find a audio stream.\n");
//return -1;
}
stream_component_open(is, video_index);
//stream_component_open(is, audio_index);
// Get a pointer to the codec context for the audio stream
/*pCodecCtx=pFormatCtx->streams[audio_index]->codec;
// Find the decoder for the audio stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL){
printf("Codec not found.\n");
return -1;
}
// Open codec
if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){
printf("Could not open codec.\n");
return -1;
}
printf("000\n");
AVFrame *pFrame;
uint8_t *out_buffer;
SDL_AudioSpec wanted_spec;
int64_t in_channel_layout;
//Out Audio Param
uint64_t out_channel_layout=AV_CH_LAYOUT_STEREO;
//nb_samples: AAC-1024 MP3-1152
int out_nb_samples=pCodecCtx->frame_size;
enum AVSampleFormat out_sample_fmt;
out_sample_fmt=AV_SAMPLE_FMT_S16;
int out_sample_rate=44100;
int out_channels=av_get_channel_layout_nb_channels(out_channel_layout);
//Out Buffer Size
int out_buffer_size=av_samples_get_buffer_size(NULL,out_channels ,out_nb_samples,out_sample_fmt, 1);
out_buffer=(uint8_t *)av_malloc(MAX_AUDIO_FRAME_SIZE*2);
pFrame=av_frame_alloc();
//SDL------------------
printf("Sample rate: %d / %d\n", pCodecCtx->sample_rate, out_sample_rate);
printf("Channels: %d / %d\n", pCodecCtx->channels, out_channels);
printf("Samples: %d\n", out_nb_samples);
//SDL_AudioSpec
// wanted_spec.freq = out_sample_rate;
// wanted_spec.format = AUDIO_S16SYS;
// wanted_spec.channels = out_channels;
// wanted_spec.silence = 0;
// wanted_spec.samples = out_nb_samples;
// wanted_spec.callback = audio_callback;
// wanted_spec.userdata = pCodecCtx;
//
// if (SDL_OpenAudio(&wanted_spec, NULL)<0){
// printf("can't open audio.\n");
// return -1;
// }
// SDL_PauseAudio(0);
//FIX:Some Codec's Context Information is missing
in_channel_layout=av_get_default_channel_layout(pCodecCtx->channels);
//Swr
au_convert_ctx = swr_alloc();
au_convert_ctx = swr_alloc_set_opts(au_convert_ctx,
out_channel_layout, out_sample_fmt, out_sample_rate,
in_channel_layout, pCodecCtx->sample_fmt, pCodecCtx->sample_rate,
0, NULL);
swr_init(au_convert_ctx);
avformat_flush(is->pFormatCtx);
int got_picture;
int index = 0;
packet=(AVPacket *)av_malloc(sizeof(AVPacket));
avformat_flush(is->pFormatCtx);*/
int c = 0;
/*
* stream_component_open() end
*/
while(true) {
//printf("Audio...1\n");
//printf("Do something\n");
if(is->quit) {
break;