/*
 * Copyright (c) 2013 Stefano Sabatini
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * @file
 * libavformat/libavcodec demuxing and muxing API example.
 *
 * Remux streams from one container format to another.
 * @example remuxing.c
 */

#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>
#include <libavutil/file.h>
#include <unistd.h>
#include <pthread.h> /* POSIX Threads */

struct buffer_data {
  uint8_t *ptr;
  size_t size;  ///< size left in the buffer
  AVIOContext *avioctxt;
};

static int read_packet(void *opaque, uint8_t *buf, int buf_size) {
  return 0;
  struct buffer_data *bd = (struct buffer_data *)opaque;
  buf_size = FFMIN(buf_size, bd->size);

  printf("ptr:%p size:%zu\n", bd->ptr, bd->size);

  usleep(300);
  /* copy internal buffer data to buf */
  memcpy(buf, bd->ptr, buf_size);
  bd->ptr += buf_size;
  bd->size -= buf_size;

  return buf_size;
}

static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt,
                       const char *tag) {
  AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;

  printf(
      "%s: pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s "
      "stream_index:%d\n",
      tag, av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base),
      av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, time_base),
      av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, time_base),
      pkt->stream_index);
}
void threadFunc(void *opaque);
void threadFunc(void *opaque) {
  struct buffer_data *bd = (struct buffer_data *)opaque;
  int writeSize = 1024;
  printf("Thread function called.size:%d\n", bd->size);
  while (bd->size > 0) {
    avio_write(bd->avioctxt, bd->ptr, writeSize);

    bd->ptr += writeSize;
    bd->size -= writeSize;

    printf("%s---->ptr:%p size:%zu\n", __FUNCTION__, bd->ptr, bd->size);
    usleep(300);
  }
}

int main(int argc, char **argv) {
  AVOutputFormat *ofmt = NULL;
  AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
  AVPacket pkt;
  const char *in_filename, *out_filename;
  int ret, i;

  uint8_t *buffer = NULL, *avio_ctx_buffer = NULL;
  size_t buffer_size, avio_ctx_buffer_size = 4096;
  struct buffer_data bd = {0};
  AVIOContext *avio_ctx = NULL;

  pthread_t tReaderThread;

  if (argc < 3) {
    printf(
        "usage: %s input output\n"
        "API example program to remux a media file with libavformat and "
        "libavcodec.\n"
        "The output format is guessed according to the file extension.\n"
        "\n",
        argv[0]);
    return 1;
  }

  in_filename = argv[1];
  out_filename = argv[2];

  av_register_all();

  avformat_network_init();

  /* slurp file content into buffer */
  ret = av_file_map(in_filename, &buffer, &buffer_size, 0, NULL);
  if (ret < 0) goto end;

  /* fill opaque structure used by the AVIOContext read callback */
  bd.ptr = buffer;
  bd.size = buffer_size;

  if (!(ifmt_ctx = avformat_alloc_context())) {
    ret = AVERROR(ENOMEM);
    goto end;
  }

  avio_ctx_buffer = av_malloc(avio_ctx_buffer_size);
  if (!avio_ctx_buffer) {
    ret = AVERROR(ENOMEM);
    goto end;
  }
  avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, &bd,
                                NULL, NULL, NULL);
  if (!avio_ctx) {
    ret = AVERROR(ENOMEM);
    goto end;
  }
  ifmt_ctx->pb = avio_ctx;

  bd.avioctxt = avio_ctx;

  pthread_create(&tReaderThread, NULL, (void *)&threadFunc, (void *)&bd);

  //  sleep(3);

  ret = avformat_open_input(&ifmt_ctx, NULL, NULL, NULL);
  if (ret < 0) {
    fprintf(stderr, "Could not open input:%d\n", ret);
    goto end;
  }
  /*
    if ((ret = avformat_open_input(&ifmt_ctx, 0, 0, 0)) < 0) {
      fprintf(stderr, "Could not open input file '%s'", in_filename);
      goto end;
    }*/

  if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
    fprintf(stderr, "Failed to retrieve input stream information");
    goto end;
  }

  av_dump_format(ifmt_ctx, 0, in_filename, 0);

  avformat_alloc_output_context2(&ofmt_ctx, NULL, "flv", out_filename);
  if (!ofmt_ctx) {
    fprintf(stderr, "Could not create output context\n");
    ret = AVERROR_UNKNOWN;
    goto end;
  }

  ofmt = ofmt_ctx->oformat;

  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
    AVStream *in_stream = ifmt_ctx->streams[i];
    AVStream *out_stream =
        avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
    if (!out_stream) {
      fprintf(stderr, "Failed allocating output stream\n");
      ret = AVERROR_UNKNOWN;
      goto end;
    }

    ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
    if (ret < 0) {
      fprintf(
          stderr,
          "Failed to copy context from input to output stream codec context\n");
      goto end;
    }
    out_stream->codec->codec_tag = 0;
    if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
      out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  }
  av_dump_format(ofmt_ctx, 0, out_filename, 1);

  if (!(ofmt->flags & AVFMT_NOFILE)) {
    ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
    if (ret < 0) {
      fprintf(stderr, "Could not open output file '%s'", out_filename);
      goto end;
    }
  }

  ret = avformat_write_header(ofmt_ctx, NULL);
  if (ret < 0) {
    fprintf(stderr, "Error occurred when opening output file\n");
    goto end;
  }

  while (1) {
    AVStream *in_stream, *out_stream;

    ret = av_read_frame(ifmt_ctx, &pkt);
    if (ret < 0) break;

    in_stream = ifmt_ctx->streams[pkt.stream_index];
    out_stream = ofmt_ctx->streams[pkt.stream_index];

    //    log_packet(ifmt_ctx, &pkt, "in");

    /* copy packet */
    pkt.pts =
        av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base,
                         AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
    pkt.dts =
        av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base,
                         AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
    pkt.duration =
        av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
    pkt.pos = -1;
    //    log_packet(ofmt_ctx, &pkt, "out");

    ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
    if (ret < 0) {
      fprintf(stderr, "Error muxing packet\n");
      break;
    }
    av_packet_unref(&pkt);
  }

  av_write_trailer(ofmt_ctx);
end:

  avformat_close_input(&ifmt_ctx);

  /* close output */
  if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE)) avio_closep(&ofmt_ctx->pb);
  avformat_free_context(ofmt_ctx);

  if (ret < 0 && ret != AVERROR_EOF) {
    fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
    return 1;
  }

  pthread_join(tReaderThread, NULL);

  return 0;
}
