remuxer_ffmpeg.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #include "remuxer_ffmpeg.h"
  2. #include <mutex>
  3. #include "headers_ffmpeg.h"
  4. #include "utils_string.h"
  5. #include "error_define.h"
  6. #include "log_helper.h"
  7. namespace am {
  8. static remuxer_ffmpeg *_g_instance = nullptr;
  9. static std::mutex _g_mutex;
  10. static void process_packet(AVPacket *pkt, AVStream *in_stream,
  11. AVStream *out_stream)
  12. {
  13. pkt->pts = av_rescale_q_rnd(pkt->pts, in_stream->time_base,
  14. out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
  15. pkt->dts = av_rescale_q_rnd(pkt->dts, in_stream->time_base,
  16. out_stream->time_base,
  17. (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
  18. pkt->duration = (int)av_rescale_q(pkt->duration, in_stream->time_base,
  19. out_stream->time_base);
  20. pkt->pos = -1;
  21. }
  22. static int remux_file(AVFormatContext *ctx_src, AVFormatContext *ctx_dst, REMUXER_PARAM *param)
  23. {
  24. AVPacket pkt;
  25. int ret, throttle = 0, error = AE_NO;
  26. for (;;) {
  27. ret = av_read_frame(ctx_src, &pkt);
  28. if (ret < 0) {
  29. if (ret != AVERROR_EOF)
  30. error = AE_FFMPEG_READ_FRAME_FAILED;
  31. break;
  32. }
  33. if (param->cb_progress != NULL && throttle++ > 10) {
  34. float progress = pkt.pos / (float)param->src_size * 100.f;
  35. param->cb_progress(param->src, progress, 100);
  36. throttle = 0;
  37. }
  38. process_packet(&pkt, ctx_src->streams[pkt.stream_index],
  39. ctx_dst->streams[pkt.stream_index]);
  40. ret = av_interleaved_write_frame(ctx_dst, &pkt);
  41. av_packet_unref(&pkt);
  42. // Sometimes the pts and dts will equal to last packet,
  43. // don not know why,may the time base issue?
  44. // So return -22 do not care for now
  45. if (ret < 0 && ret != -22) {
  46. error = AE_FFMPEG_WRITE_FRAME_FAILED;
  47. break;
  48. }
  49. }
  50. return error;
  51. }
  52. static int open_src(AVFormatContext **ctx, const char *path) {
  53. int ret = avformat_open_input(ctx, path, NULL, NULL);
  54. if (ret < 0) {
  55. return AE_FFMPEG_OPEN_INPUT_FAILED;
  56. }
  57. ret = avformat_find_stream_info(*ctx, NULL);
  58. if (ret < 0) {
  59. return AE_FFMPEG_FIND_STREAM_FAILED;
  60. }
  61. #ifdef _DEBUG
  62. av_dump_format(*ctx, 0, path, false);
  63. #endif
  64. return AE_NO;
  65. }
  66. int open_dst(AVFormatContext **ctx_dst, const char *path, AVFormatContext *ctx_src) {
  67. int ret;
  68. avformat_alloc_output_context2(ctx_dst, NULL, NULL,
  69. path);
  70. if (!*ctx_dst) {
  71. return AE_FFMPEG_ALLOC_CONTEXT_FAILED;
  72. }
  73. for (unsigned i = 0; i < ctx_src->nb_streams; i++) {
  74. AVStream *in_stream = ctx_src->streams[i];
  75. AVStream *out_stream = avformat_new_stream(
  76. *ctx_dst, in_stream->codec->codec);
  77. if (!out_stream) {
  78. return AE_FFMPEG_NEW_STREAM_FAILED;
  79. }
  80. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  81. AVCodecParameters *par = avcodec_parameters_alloc();
  82. ret = avcodec_parameters_from_context(par, in_stream->codec);
  83. if (ret == 0)
  84. ret = avcodec_parameters_to_context(out_stream->codec,
  85. par);
  86. avcodec_parameters_free(&par);
  87. #else
  88. ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
  89. #endif
  90. if (ret < 0) {
  91. return AE_FFMPEG_COPY_PARAMS_FAILED;
  92. }
  93. out_stream->time_base = out_stream->codec->time_base;
  94. av_dict_copy(&out_stream->metadata, in_stream->metadata, 0);
  95. out_stream->codec->codec_tag = 0;
  96. if ((*ctx_dst)->oformat->flags & AVFMT_GLOBALHEADER)
  97. out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  98. }
  99. #ifndef _NDEBUG
  100. av_dump_format(*ctx_dst, 0, path, true);
  101. #endif
  102. if (!((*ctx_dst)->oformat->flags & AVFMT_NOFILE)) {
  103. ret = avio_open(&(*ctx_dst)->pb, path,
  104. AVIO_FLAG_WRITE);
  105. if (ret < 0) {
  106. return AE_FFMPEG_OPEN_IO_FAILED;
  107. }
  108. }
  109. return AE_NO;
  110. }
  111. static void remuxing(REMUXER_PARAM *param) {
  112. al_debug("remuxing:%s", param->src);
  113. int error = AE_NO;
  114. AVFormatContext *ctx_src = nullptr, *ctx_dst = nullptr;
  115. //call back start
  116. if (param->cb_state)
  117. param->cb_state(param->src, 1, AE_NO);
  118. do {
  119. error = open_src(&ctx_src, utils_string::ascii_utf8(param->src).c_str());
  120. if (error != AE_NO) {
  121. break;
  122. }
  123. error = open_dst(&ctx_dst, utils_string::ascii_utf8(param->dst).c_str(), ctx_src);
  124. if (error != AE_NO) {
  125. break;
  126. }
  127. int ret = avformat_write_header(ctx_dst, NULL);
  128. if (ret < 0) {
  129. error = AE_FFMPEG_WRITE_HEADER_FAILED;
  130. break;
  131. }
  132. error = remux_file(ctx_src, ctx_dst, param);
  133. if (error != AE_NO) {
  134. av_write_trailer(ctx_dst);
  135. break;
  136. }
  137. ret = av_write_trailer(ctx_dst);
  138. if (ret < 0)
  139. error = AE_FFMPEG_WRITE_TRAILER_FAILED;
  140. } while (0);
  141. if (ctx_src) {
  142. avformat_close_input(&ctx_src);
  143. }
  144. if (ctx_dst && !(ctx_dst->oformat->flags & AVFMT_NOFILE))
  145. avio_close(ctx_dst->pb);
  146. if (ctx_dst)
  147. avformat_free_context(ctx_dst);
  148. al_debug("remux %s to %s end with error:%s",
  149. param->src, param->dst, err2str(error));
  150. //call back end
  151. if (param->cb_state)
  152. param->cb_state(param->src, 0, error);
  153. remuxer_ffmpeg::instance()->remove_remux(param->src);
  154. }
  155. remuxer_ffmpeg * remuxer_ffmpeg::instance()
  156. {
  157. std::lock_guard<std::mutex> lock(_g_mutex);
  158. if (_g_instance == nullptr) _g_instance = new remuxer_ffmpeg();
  159. return _g_instance;
  160. }
  161. void remuxer_ffmpeg::release()
  162. {
  163. std::lock_guard<std::mutex> lock(_g_mutex);
  164. if (_g_instance)
  165. delete _g_instance;
  166. _g_instance = nullptr;
  167. }
  168. int remuxer_ffmpeg::create_remux(const REMUXER_PARAM & param)
  169. {
  170. std::lock_guard<std::mutex> lock(_g_mutex);
  171. auto itr = _handlers.find(param.src);
  172. if (itr != _handlers.end() && itr->second->fn.joinable() == true) {
  173. return AE_REMUX_RUNNING;
  174. }
  175. if (!strlen(param.src) || !strlen(param.dst) || !strcmp(param.src, param.dst))
  176. return AE_REMUX_INVALID_INOUT;
  177. #ifdef _MSC_VER
  178. struct _stat64 st = { 0 };
  179. _stat64(param.src, &st);
  180. #else
  181. struct stat st = { 0 };
  182. stat(param.src, &st);
  183. #endif
  184. if (!st.st_size) return AE_REMUX_NOT_EXIST;
  185. if (itr != _handlers.end()) {
  186. delete itr->second;
  187. _handlers.erase(itr);
  188. }
  189. REMUXER_HANDLE *handle = new REMUXER_HANDLE;
  190. memcpy(&(handle->param), &param, sizeof(REMUXER_PARAM));
  191. handle->param.running = true;
  192. handle->param.src_size = st.st_size;
  193. handle->fn = std::thread(remuxing, &handle->param);
  194. _handlers[param.src] = handle;
  195. return AE_NO;
  196. }
  197. void remuxer_ffmpeg::remove_remux(std::string src)
  198. {
  199. std::lock_guard<std::mutex> lock(_g_mutex);
  200. auto itr = _handlers.find(src);
  201. if (itr != _handlers.end()) {
  202. itr->second->fn.detach();
  203. delete itr->second;
  204. _handlers.erase(itr);
  205. }
  206. }
  207. void remuxer_ffmpeg::destroy_remux()
  208. {
  209. std::lock_guard<std::mutex> lock(_g_mutex);
  210. for (auto itr = _handlers.begin(); itr != _handlers.end(); itr++)
  211. {
  212. itr->second->param.running = false;
  213. if (itr->second->fn.joinable())
  214. itr->second->fn.join();
  215. delete itr->second;
  216. _handlers.erase(itr);
  217. }
  218. }
  219. }