AiCPlayer
Interface of aic vm - for rendering aspect, sensors, video records
player_audio.c
Go to the documentation of this file.
1 
8 #include <errno.h> // for ENOMEM
9 #include <libavcodec/avcodec.h> // for AVCodecContext, AVPacket, AVCodec
10 #include <libavutil/avassert.h> // for av_assert0
11 #include <libavutil/avutil.h> // for AVMediaType::AVMEDIA_TYPE_AUDIO
12 #include <libavutil/channel_layout.h> // for AV_CH_LAYOUT_STEREO, av_get_ch...
13 #include <libavutil/common.h> // for FFMIN
14 #include <libavutil/dict.h> // for AVDictionary, av_dict_copy
15 #include <libavutil/error.h> // for AVERROR_EXIT, AVERROR, av_err2str
16 #include <libavutil/mem.h> // for av_free, av_freep
17 #include <libavutil/rational.h> // for AVRational
18 #include <libavutil/samplefmt.h> // for AVSampleFormat::AV_SAMPLE_FMT_FLT
19 #include <libswresample/swresample.h> // for swr_free, swr_init, SwrContext
20 #include <libswscale/swscale.h> // for sws_freeContext
21 #include <stdint.h> // for uint8_t, uint64_t
22 #include <stdio.h> // for printf, NULL, fprintf, stderr
23 #include <stdlib.h> // for exit, calloc, free, malloc
24 #include <string.h> // for memcpy, memmove
25 #include <unistd.h> // sleep, close
26 #include <libavformat/avformat.h> // for AVFormatContext, AVStream, AVO...
27 #include <libavformat/avio.h> // for avio_closep, avio_open, AVIO_F...
28 #include <libavutil/audio_fifo.h> // for av_audio_fifo_size, AVAudioFifo
29 #include <libavutil/frame.h> // for AVFrame, av_frame_free, av_fra...
30 #include <libavutil/opt.h> // for av_opt_set_int, av_opt_set_sam...
31 
32 #include "socket.h"
33 #include "logger.h"
34 #include "config_env.h"
35 
36 #define LOG_TAG "audio"
37 
39 #define ANDROIDINCLOUD_PCM_CLIENT_PORT 24296
40 
41 const char* get_error_text(const int error)
42 {
43  static char error_buffer[255];
44  av_strerror(error, error_buffer, sizeof(error_buffer));
45  return error_buffer;
46 }
47 
49 void init_packet(AVPacket* packet)
50 {
51  av_init_packet(packet);
53  packet->data = NULL;
54  packet->size = 0;
55 }
56 
58 int init_input_frame(AVFrame** frame)
59 {
60  if (!(*frame = av_frame_alloc()))
61  {
62  printf("Could not allocate input frame\n");
63  return AVERROR(ENOMEM);
64  }
65  return 0;
66 }
67 
73 int init_resampler(AVCodecContext* input_codec_context, AVCodecContext* output_codec_context,
74  SwrContext** resample_context)
75 {
76  int error;
77 
85  *resample_context = swr_alloc_set_opts(
86  NULL, av_get_default_channel_layout(output_codec_context->channels),
87  output_codec_context->sample_fmt, output_codec_context->sample_rate,
88  av_get_default_channel_layout(input_codec_context->channels),
89  input_codec_context->sample_fmt, input_codec_context->sample_rate, 0, NULL);
90  if (!*resample_context)
91  {
92  printf("Could not allocate resample context\n");
93  return AVERROR(ENOMEM);
94  }
100  av_assert0(output_codec_context->sample_rate == input_codec_context->sample_rate);
101 
103  if ((error = swr_init(*resample_context)) < 0)
104  {
105  printf("Could not open resample context\n");
106  swr_free(resample_context);
107  return error;
108  }
109  return 0;
110 }
111 
113 int init_fifo(AVAudioFifo** fifo, AVCodecContext* output_codec_context)
114 {
116  if (!(*fifo = av_audio_fifo_alloc(output_codec_context->sample_fmt,
117  output_codec_context->channels, 1)))
118  {
119  printf("Could not allocate FIFO\n");
120  return AVERROR(ENOMEM);
121  }
122  return 0;
123 }
124 
126 int decode_audio_frame(AVFrame* frame, AVFormatContext* input_format_context,
127  AVCodecContext* input_codec_context, int* data_present, int* finished)
128 {
130  AVPacket input_packet;
131  int error;
132  init_packet(&input_packet);
133 
135  if ((error = av_read_frame(input_format_context, &input_packet)) < 0)
136  {
138  if (error == AVERROR_EOF)
139  *finished = 1;
140  else
141  {
142  printf("Could not read frame (error '%s')\n", get_error_text(error));
143  return error;
144  }
145  }
146 
153  if ((error = avcodec_decode_audio4(input_codec_context, frame, data_present, &input_packet)) <
154  0)
155  {
156  printf("Could not decode frame (error '%s')\n", get_error_text(error));
157  av_free_packet(&input_packet);
158  return error;
159  }
160 
165  if (*finished && *data_present)
166  *finished = 0;
167  av_free_packet(&input_packet);
168  return 0;
169 }
170 
176 int init_converted_samples(uint8_t*** converted_input_samples, AVCodecContext* output_codec_context,
177  int frame_size)
178 {
179  int error;
180 
186  if (!(*converted_input_samples =
187  calloc(output_codec_context->channels, sizeof(**converted_input_samples))))
188  {
189  printf("Could not allocate converted input sample pointers\n");
190  return AVERROR(ENOMEM);
191  }
192 
197  if ((error = av_samples_alloc(*converted_input_samples, NULL, output_codec_context->channels,
198  frame_size, output_codec_context->sample_fmt, 0)) < 0)
199  {
200  fprintf(stderr, "Could not allocate converted input samples (error '%s')\n",
201  get_error_text(error));
202  av_freep(&(*converted_input_samples)[0]);
203  free(*converted_input_samples);
204  return error;
205  }
206  return 0;
207 }
208 
214 int convert_samples(const uint8_t** input_data, uint8_t** converted_data, const int frame_size,
215  SwrContext* resample_context)
216 {
217  int error;
218 
220  if ((error =
221  swr_convert(resample_context, converted_data, frame_size, input_data, frame_size)) < 0)
222  {
223  printf("Could not convert input samples (error '%s')\n", get_error_text(error));
224  return error;
225  }
226 
227  return 0;
228 }
229 
231 int add_samples_to_fifo(AVAudioFifo* fifo, uint8_t** converted_input_samples, const int frame_size)
232 {
233  int error;
234 
239  if ((error = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) + frame_size)) < 0)
240  {
241  printf("Could not reallocate FIFO\n");
242  return error;
243  }
244 
246  if (av_audio_fifo_write(fifo, (void**) converted_input_samples, frame_size) < frame_size)
247  {
248  printf("Could not write data to FIFO\n");
249  return AVERROR_EXIT;
250  }
251  return 0;
252 }
253 
258 int init_output_frame(AVFrame** frame, AVCodecContext* output_codec_context, int frame_size)
259 {
260  int error;
261 
263  if (!(*frame = av_frame_alloc()))
264  {
265  printf("Could not allocate output frame\n");
266  return AVERROR_EXIT;
267  }
268 
276  (*frame)->nb_samples = frame_size;
277  (*frame)->channel_layout = output_codec_context->channel_layout;
278  (*frame)->format = output_codec_context->sample_fmt;
279  (*frame)->sample_rate = output_codec_context->sample_rate;
280 
285  if ((error = av_frame_get_buffer(*frame, 0)) < 0)
286  {
287  printf("Could allocate output frame samples (error '%s')\n", get_error_text(error));
288  av_frame_free(frame);
289  return error;
290  }
291 
292  return 0;
293 }
294 
296 static int64_t pts = 0;
297 
299 int encode_audio_frame(AVFrame* frame, AVFormatContext* output_format_context,
300  AVCodecContext* output_codec_context, int* data_present)
301 {
303  AVPacket output_packet;
304  int error;
305  init_packet(&output_packet);
306 
308  if (frame)
309  {
310  frame->pts = pts;
311  pts += frame->nb_samples;
312  }
313 
318  if ((error = avcodec_encode_audio2(output_codec_context, &output_packet, frame, data_present)) <
319  0)
320  {
321  printf("Could not encode frame (error '%s')\n", get_error_text(error));
322  av_free_packet(&output_packet);
323  return error;
324  }
325 
327  if (*data_present)
328  {
329  if ((error = av_write_frame(output_format_context, &output_packet)) < 0)
330  {
331  printf("Could not write frame (error '%s')\n", get_error_text(error));
332  av_free_packet(&output_packet);
333  return error;
334  }
335 
336  av_free_packet(&output_packet);
337  }
338 
339  return 0;
340 }
341 
346 int load_encode_and_write(AVAudioFifo* fifo, AVFormatContext* output_format_context,
347  AVCodecContext* output_codec_context)
348 {
350  AVFrame* output_frame;
356  const int frame_size = FFMIN(av_audio_fifo_size(fifo), output_codec_context->frame_size);
357  int data_written;
358 
360  if (init_output_frame(&output_frame, output_codec_context, frame_size))
361  return AVERROR_EXIT;
362 
367  if (av_audio_fifo_read(fifo, (void**) output_frame->data, frame_size) < frame_size)
368  {
369  printf("Could not read data from FIFO\n");
370  av_frame_free(&output_frame);
371  return AVERROR_EXIT;
372  }
373 
375  if (encode_audio_frame(output_frame, output_format_context, output_codec_context,
376  &data_written))
377  {
378  av_frame_free(&output_frame);
379  return AVERROR_EXIT;
380  }
381  av_frame_free(&output_frame);
382  return 0;
383 }
384 
386 int write_output_file_trailer(AVFormatContext* output_format_context)
387 {
388  int error;
389  if ((error = av_write_trailer(output_format_context)) < 0)
390  {
391  printf("Could not write output file trailer (error '%s')\n", get_error_text(error));
392  return error;
393  }
394  return 0;
395 }
396 
397 // a wrapper around a single output AVStream
398 typedef struct OutputStream
399 {
400  AVStream* st;
401 
402  /* pts of the next frame that will be generated */
403  int64_t next_pts;
404  int samples_count;
405 
406  AVFrame* frame;
407  AVFrame* tmp_frame;
408 
409  float t, tincr, tincr2;
410 
411  struct SwsContext* sws_ctx;
412  struct SwrContext* swr_ctx;
413 } OutputStream;
414 
415 #if 0
416 static void log_packet(const AVFormatContext* fmt_ctx, const AVPacket* pkt)
417 {
418  AVRational* time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;
419 
420  printf("pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
421  av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base), av_ts2str(pkt->dts),
422  av_ts2timestr(pkt->dts, time_base), av_ts2str(pkt->duration),
423  av_ts2timestr(pkt->duration, time_base), pkt->stream_index);
424 }
425 #endif
426 
427 /* Add an output stream. */
428 static void add_stream(OutputStream* ost, AVFormatContext* oc, AVCodec** codec,
429  enum AVCodecID codec_id)
430 {
431  AVCodecContext* c;
432  int i;
433 
434  /* find the encoder */
435  *codec = avcodec_find_encoder(codec_id);
436  if (!(*codec))
437  {
438  printf("Could not find encoder for '%s'\n", avcodec_get_name(codec_id));
439  exit(1);
440  }
441 
442  ost->st = avformat_new_stream(oc, *codec);
443  if (!ost->st)
444  {
445  printf("Could not allocate stream\n");
446  exit(1);
447  }
448  ost->st->id = oc->nb_streams - 1;
449  c = ost->st->codec;
450  switch ((*codec)->type)
451  {
452  case AVMEDIA_TYPE_AUDIO:
453  c->sample_fmt = (*codec)->sample_fmts ? (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLT;
454  c->bit_rate = 64000;
455  c->sample_rate = 44100;
456  if ((*codec)->supported_samplerates)
457  {
458  c->sample_rate = (*codec)->supported_samplerates[0];
459  for (i = 0; (*codec)->supported_samplerates[i]; i++)
460  {
461  if ((*codec)->supported_samplerates[i] == 44100)
462  c->sample_rate = 44100;
463  }
464  }
465  c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
466  c->channel_layout = AV_CH_LAYOUT_STEREO;
467  if ((*codec)->channel_layouts)
468  {
469  c->channel_layout = (*codec)->channel_layouts[0];
470  for (i = 0; (*codec)->channel_layouts[i]; i++)
471  {
472  if ((*codec)->channel_layouts[i] == AV_CH_LAYOUT_STEREO)
473  c->channel_layout = AV_CH_LAYOUT_STEREO;
474  }
475  }
476  ost->st->time_base = (AVRational){1, c->sample_rate};
477  break;
478 
479  default:
480  break;
481  }
482 
483  /* Some formats want stream headers to be separate. */
484  // if (oc->oformat->flags & AVFMT_GLOBALHEADER)
485  c->flags |= CODEC_FLAG_GLOBAL_HEADER;
486 }
487 
488 /**************************************************************/
489 /* audio output */
490 
491 static AVFrame* alloc_audio_frame(enum AVSampleFormat sample_fmt, uint64_t channel_layout,
492  int sample_rate, int nb_samples)
493 {
494  AVFrame* frame = av_frame_alloc();
495 
496  if (!frame)
497  {
498  printf("Error allocating an audio frame\n");
499  exit(1);
500  }
501 
502  frame->format = sample_fmt;
503  frame->channel_layout = channel_layout;
504  frame->sample_rate = sample_rate;
505  frame->nb_samples = nb_samples;
506 
507  if (nb_samples)
508  {
509  int ret;
510  ret = av_frame_get_buffer(frame, 0);
511  if (ret < 0)
512  {
513  printf("Error allocating an audio buffer\n");
514  exit(1);
515  }
516  }
517 
518  return frame;
519 }
520 
521 static void open_audio(AVCodec* codec, OutputStream* ost, AVDictionary* opt_arg)
522 {
523  AVCodecContext* c;
524  int nb_samples;
525  int ret;
526  AVDictionary* opt = NULL;
527 
528  c = ost->st->codec;
529 
530  /* open it */
531  av_dict_copy(&opt, opt_arg, 0);
532  ret = avcodec_open2(c, codec, &opt);
533  av_dict_free(&opt);
534  if (ret < 0)
535  {
536  printf("Could not open audio codec: %s\n", av_err2str(ret));
537  exit(1);
538  }
539 
540  /* init signal generator */
541  // ost->t = 0;
542  // ost->tincr = 2 * M_PI * 550.0 / c->sample_rate;
543  /* increment frequency by 110 Hz per second */
544  // ost->tincr2 = 2 * M_PI * 550.0 / c->sample_rate / c->sample_rate;
545 
546  nb_samples = c->frame_size;
547 
548  ost->frame = alloc_audio_frame(c->sample_fmt, c->channel_layout, c->sample_rate, nb_samples);
549  ost->tmp_frame =
550  alloc_audio_frame(AV_SAMPLE_FMT_FLT, c->channel_layout, c->sample_rate, nb_samples);
551 
552  /* create resampler context */
553  ost->swr_ctx = swr_alloc();
554  if (!ost->swr_ctx)
555  {
556  printf("Could not allocate resampler context\n");
557  exit(1);
558  }
559 
560  /* set options */
561  av_opt_set_int(ost->swr_ctx, "in_channel_count", c->channels, 0);
562  av_opt_set_int(ost->swr_ctx, "in_sample_rate", c->sample_rate, 0);
563  av_opt_set_sample_fmt(ost->swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_FLT, 0);
564  av_opt_set_int(ost->swr_ctx, "out_channel_count", c->channels, 0);
565  av_opt_set_int(ost->swr_ctx, "out_sample_rate", c->sample_rate, 0);
566  av_opt_set_sample_fmt(ost->swr_ctx, "out_sample_fmt", AV_SAMPLE_FMT_FLT, 0);
567 
568  /* initialize the resampling context */
569  if (swr_init(ost->swr_ctx) < 0)
570  {
571  printf("Failed to initialize the resampling context\n");
572  exit(1);
573  }
574 }
575 
576 void audiolive_decode_encode(AVFormatContext* oc, OutputStream* ost, unsigned char* inbuf,
577  int num_read, int size)
578 {
579  avcodec_register_all();
580 
581  AVCodec* codec;
582  AVCodecContext* codecContext = NULL;
583 
584  uint8_t memBuffer[size + FF_INPUT_BUFFER_PADDING_SIZE];
585  AVPacket memPkt;
586  AVFrame* decoded_frame_mem = NULL;
587 
588  av_init_packet(&memPkt);
589 
590  // printf("Audio decoding \n");
591 
592  /* find the mpeg audio decoder */
593  codec = avcodec_find_decoder(AV_CODEC_ID_PCM_S16LE);
594  if (!codec)
595  {
596  printf("codec not found\n");
597  exit(1);
598  }
599 
600  codecContext = avcodec_alloc_context3(codec);
601 
602  codecContext->bit_rate = 64000;
603  codecContext->sample_fmt = AV_SAMPLE_FMT_S16;
604  codecContext->sample_rate = 44100; // select_sample_rate(codec);
605  codecContext->channel_layout = AV_CH_LAYOUT_STEREO;
606  codecContext->channels = av_get_channel_layout_nb_channels(codecContext->channel_layout);
607 
608  /* open it */
609  if (avcodec_open2(codecContext, codec, NULL) < 0)
610  {
611  printf("could not open codec\n");
612  exit(1);
613  }
614 
615  /* decode until eof */
616  memPkt.data = memBuffer;
617  memPkt.size = num_read;
618  memcpy(memBuffer, inbuf, num_read);
619 
620  AVCodecContext* output_codec_context = NULL;
621  SwrContext* resample_context = NULL;
622  uint8_t** converted_input_samples = NULL;
623  AVAudioFifo* fifo = NULL;
624 
625  output_codec_context = ost->st->codec;
626  init_fifo(&fifo, output_codec_context);
627  init_resampler(codecContext, output_codec_context, &resample_context);
628 
629  while (memPkt.size > 0)
630  {
631  int got_frame_mem = 0;
632  int length;
633 
634  if (!decoded_frame_mem)
635  {
636  if (!(decoded_frame_mem = av_frame_alloc()))
637  {
638  printf("out of memory\n");
639  exit(1);
640  }
641  }
642  else
643  {
644  av_frame_unref(decoded_frame_mem);
645  }
646 
647  length = avcodec_decode_audio4(codecContext, decoded_frame_mem, &got_frame_mem, &memPkt);
648 
649  if (length < 0)
650  {
651  printf("Error while decoding\n");
652  exit(1);
653  }
654  if (got_frame_mem)
655  {
656  av_samples_get_buffer_size(NULL, codecContext->channels, decoded_frame_mem->nb_samples,
657  codecContext->sample_fmt, 1);
658 
659  output_codec_context->frame_size = decoded_frame_mem->nb_samples;
660 
662  const int output_frame_size = output_codec_context->frame_size;
663  int finished = 0;
664 
672  while (av_audio_fifo_size(fifo) < output_frame_size)
673  {
674  // printf("Audio decoding output_frame_size %d %d %d\n", output_frame_size,
675  // av_audio_fifo_size(fifo) , finished);
681  if (init_converted_samples(&converted_input_samples, output_codec_context,
682  decoded_frame_mem->nb_samples))
683  printf("Audio decoding A\n");
684 
690  if (convert_samples((const uint8_t**) decoded_frame_mem->extended_data,
691  converted_input_samples, decoded_frame_mem->nb_samples,
692  resample_context))
693  printf("Audio decoding B\n");
694 
696  if (add_samples_to_fifo(fifo, converted_input_samples,
697  decoded_frame_mem->nb_samples))
698  printf("Audio decoding C\n");
699 
704  if (finished)
705  break;
706  }
707 
714  while (av_audio_fifo_size(fifo) >= output_frame_size ||
715  (finished && av_audio_fifo_size(fifo) > 0))
720  // printf("Audio decoding output_frame_size %d %d %d\n", output_frame_size,
721  // av_audio_fifo_size(fifo) , finished);
722  if (load_encode_and_write(fifo, oc, output_codec_context))
723  printf("Audio decoding D\n");
724  }
725 
726  // write_output_file_trailer(oc);
727 
728  memPkt.size -= length;
729  memPkt.data += length;
730 
731  if (memPkt.size < 4090)
732  {
733  /* Refill the input buffer, to avoid trying to decode
734  * incomplete frames. Instead of this, one could also use
735  * a parser, or use a proper container format through
736  * libavformat. */
737  memmove(inbuf, memPkt.data, memPkt.size);
738  memPkt.data = inbuf;
739  length = 0; // size - memPkt.size;
740  if (length > 0)
741  memPkt.size += length;
742  }
743  }
744 
745  avcodec_close(codecContext);
746  av_free(codecContext);
747  // avcodec_free_frame(&decoded_frame_mem);
748 }
749 
750 /*
751  * encode one audio frame and send it to the muxer
752  * return 1 when encoding is finished, 0 otherwise
753  */
754 static int read_audio_frame(AVFormatContext* oc, OutputStream* ost, socket_t outsocket)
755 {
756  unsigned char* buffer;
757  int size = 65535;
758  int num_read = 1;
759 
760  buffer = malloc(size);
761 
763  do
764  {
765  num_read = recv(outsocket, buffer, size, 0);
766  audiolive_decode_encode(oc, ost, buffer, num_read, size);
767  } while (num_read > 0);
769 
770  return 0;
771 }
772 
773 static void close_stream(OutputStream* ost)
774 {
775  avcodec_close(ost->st->codec);
776  av_frame_free(&ost->frame);
777  av_frame_free(&ost->tmp_frame);
778  sws_freeContext(ost->sws_ctx);
779  swr_free(&ost->swr_ctx);
780 }
781 
782 /**************************************************************/
783 /* media file output */
784 void* aic_audioplayer(char* vmip)
785 {
786  OutputStream audio_st = {0};
787  const char* filename;
788  AVOutputFormat* fmt;
789  AVFormatContext* oc;
790  AVCodec* audio_codec;
791  int ret;
792  int have_audio = 0;
793  AVDictionary* opt = NULL;
794 
795  LOGI("avcodec - register all formats and codecs");
796  av_register_all();
797  avformat_network_init();
798 
799  filename = "http://ffserver:8090/audio.ffm";
800 
801  /* allocate the output media context */
802  avformat_alloc_output_context2(&oc, NULL, "ffm", filename);
803 
804  if (!oc)
805  {
806  printf("Could not deduce output format from file extension: using OGG.\n");
807  avformat_alloc_output_context2(&oc, NULL, "ogg", filename);
808  }
809  if (!oc)
810  return NULL;
811 
812  fmt = oc->oformat;
813 
814  /* Add the audio and video streams using the default format codecs
815  * and initialize the codecs. */
816  if (fmt->audio_codec != AV_CODEC_ID_NONE)
817  {
818  add_stream(&audio_st, oc, &audio_codec, AV_CODEC_ID_VORBIS);
819  have_audio = 1;
820  LOGW("No audio stream");
821  }
822 
823  /* Now that all the parameters are set, we can open the audio and
824  * video codecs and allocate the necessary encode buffers. */
825  if (have_audio)
826  open_audio(audio_codec, &audio_st, opt);
827  else
828  return NULL;
829 
830  /* open the output file, if needed */
831  if (!(fmt->flags & AVFMT_NOFILE))
832  {
833  ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);
834  if (ret < 0)
835  {
836  printf("Could not open '%s': %s\n", filename, av_err2str(ret));
837  return NULL;
838  }
839  }
840 
841  /* Write the stream header, if any. */
842  ret = avformat_write_header(oc, &opt);
843  if (ret < 0)
844  {
845  printf("Error occurred when opening output file: %s\n", av_err2str(ret));
846  return NULL;
847  }
848  av_dump_format(oc, 0, filename, 1);
849 
851 
852  while (outsocket == -1)
853  {
854  LOGI(" B outsocket -> %d", outsocket);
855  close(outsocket);
856  sleep(1);
857  outsocket = open_socket(vmip, ANDROIDINCLOUD_PCM_CLIENT_PORT);
858  }
859 
860  read_audio_frame(oc, &audio_st, outsocket);
861  close(outsocket);
862 
863  /* Write the trailer, if any. The trailer must be written before you
864  * close the CodecContexts open when you wrote the header; otherwise
865  * av_write_trailer() may try to use memory that was freed on
866  * av_codec_close(). */
868 
869  /* Close each codec. */
870  if (have_audio)
871  close_stream(&audio_st);
872 
873  if (!(fmt->flags & AVFMT_NOFILE))
874  /* Close the output file. */
875  avio_closep(&oc->pb);
876 
877  /* free the stream */
878  avformat_free_context(oc);
879 
880  return NULL;
881 }
882 
883 int main()
884 {
885  char* g_vmip = NULL;
886  g_vmip = configvar_string("AIC_PLAYER_VM_HOST");
887  aic_audioplayer(g_vmip);
888 
889  return 0;
890 }
Utilities to get config values from the environment.
A wrapper around a single output AVStream.
Definition: grabber.h:69
int init_output_frame(AVFrame **frame, AVCodecContext *output_codec_context, int frame_size)
Definition: player_audio.c:258
int convert_samples(const uint8_t **input_data, uint8_t **converted_data, const int frame_size, SwrContext *resample_context)
Definition: player_audio.c:214
void * aic_audioplayer(char *vmip)
Definition: player_audio.c:784
int load_encode_and_write(AVAudioFifo *fifo, AVFormatContext *output_format_context, AVCodecContext *output_codec_context)
Definition: player_audio.c:346
char * g_vmip
Definition: testSensors.c:41
float t
Definition: grabber.h:80
int init_fifo(AVAudioFifo **fifo, AVCodecContext *output_codec_context)
Definition: player_audio.c:113
float tincr
Definition: grabber.h:80
struct OutputStream OutputStream
AVFrame * frame
Definition: grabber.h:77
int socket_t
Alias to differenciate between regular ints and socket fds.
Definition: socket.h:13
const char * get_error_text(const int error)
Definition: player_audio.c:41
int main()
Definition: player_audio.c:883
int samples_count
Definition: grabber.h:75
void audiolive_decode_encode(AVFormatContext *oc, OutputStream *ost, unsigned char *inbuf, int num_read, int size)
Definition: player_audio.c:576
socket_t open_socket(const char *ip, short port)
Connect to a host:port couple.
Definition: socket.c:29
float tincr2
Definition: grabber.h:80
char * configvar_string(char *varname)
Get the value of a config variable from the env.
Definition: config_env.c:29
int encode_audio_frame(AVFrame *frame, AVFormatContext *output_format_context, AVCodecContext *output_codec_context, int *data_present)
Definition: player_audio.c:299
void init_packet(AVPacket *packet)
Definition: player_audio.c:49
int decode_audio_frame(AVFrame *frame, AVFormatContext *input_format_context, AVCodecContext *input_codec_context, int *data_present, int *finished)
Definition: player_audio.c:126
struct SwrContext * swr_ctx
Definition: grabber.h:83
int init_input_frame(AVFrame **frame)
Definition: player_audio.c:58
int init_resampler(AVCodecContext *input_codec_context, AVCodecContext *output_codec_context, SwrContext **resample_context)
Definition: player_audio.c:73
int add_samples_to_fifo(AVAudioFifo *fifo, uint8_t **converted_input_samples, const int frame_size)
Definition: player_audio.c:231
Logging macros.
int init_converted_samples(uint8_t ***converted_input_samples, AVCodecContext *output_codec_context, int frame_size)
Definition: player_audio.c:176
#define ANDROIDINCLOUD_PCM_CLIENT_PORT
Definition: player_audio.c:39
#define LOGW(...)
Log at WARNING level.
Definition: logger.h:27
AVFrame * tmp_frame
Definition: grabber.h:78
Define socket utilities to simplify networking.
int64_t next_pts
Definition: grabber.h:74
struct SwsContext * sws_ctx
Definition: grabber.h:82
int write_output_file_trailer(AVFormatContext *output_format_context)
Definition: player_audio.c:386
#define LOGI(...)
Log at INFO level.
Definition: logger.h:23
AVStream * st
Definition: grabber.h:71