remuxer_ffmpeg.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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, AVStream *out_stream)
  11. {
  12. pkt->pts = av_rescale_q_rnd(pkt->pts,
  13. in_stream->time_base,
  14. out_stream->time_base,
  15. (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
  16. pkt->dts = av_rescale_q_rnd(pkt->dts,
  17. in_stream->time_base,
  18. out_stream->time_base,
  19. (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
  20. pkt->duration = (int) av_rescale_q(pkt->duration, in_stream->time_base, out_stream->time_base);
  21. pkt->pos = -1;
  22. }
  23. static int remux_file(AVFormatContext *ctx_src, AVFormatContext *ctx_dst, REMUXER_PARAM *param)
  24. {
  25. AVPacket pkt;
  26. int ret, throttle = 0, error = AE_NO;
  27. for (;;) {
  28. ret = av_read_frame(ctx_src, &pkt);
  29. if (ret < 0) {
  30. if (ret != AVERROR_EOF)
  31. error = AE_FFMPEG_READ_FRAME_FAILED;
  32. break;
  33. }
  34. if (param->cb_progress != NULL && throttle++ > 10) {
  35. float progress = pkt.pos / (float) param->src_size * 100.f;
  36. param->cb_progress(param->src, progress, 100);
  37. throttle = 0;
  38. }
  39. process_packet(&pkt, ctx_src->streams[pkt.stream_index], 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. {
  54. int ret = avformat_open_input(ctx, path, NULL, NULL);
  55. if (ret < 0) {
  56. return AE_FFMPEG_OPEN_INPUT_FAILED;
  57. }
  58. ret = avformat_find_stream_info(*ctx, NULL);
  59. if (ret < 0) {
  60. return AE_FFMPEG_FIND_STREAM_FAILED;
  61. }
  62. #ifdef _DEBUG
  63. av_dump_format(*ctx, 0, path, false);
  64. #endif
  65. return AE_NO;
  66. }
  67. int open_dst(AVFormatContext **ctx_dst, const char *path, AVFormatContext *ctx_src)
  68. {
  69. int ret;
  70. avformat_alloc_output_context2(ctx_dst, NULL, NULL, path);
  71. if (!*ctx_dst) {
  72. return AE_FFMPEG_ALLOC_CONTEXT_FAILED;
  73. }
  74. for (unsigned i = 0; i < ctx_src->nb_streams; i++) {
  75. AVStream *in_stream = ctx_src->streams[i];
  76. AVStream *out_stream = avformat_new_stream(*ctx_dst, NULL);
  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, ffmpeg_get_codec_context(in_stream));
  83. if (ret == 0)
  84. ret = avcodec_parameters_to_context(ffmpeg_get_codec_context(out_stream), par);
  85. avcodec_parameters_free(&par);
  86. #else
  87. ret = avcodec_copy_context(ffmpeg_get_codec_context(out_stream), ffmpeg_get_codec_context(in_stream));
  88. #endif
  89. if (ret < 0) {
  90. return AE_FFMPEG_COPY_PARAMS_FAILED;
  91. }
  92. out_stream->time_base = ffmpeg_get_codec_context(out_stream)->time_base;
  93. av_dict_copy(&out_stream->metadata, in_stream->metadata, 0);
  94. ffmpeg_get_codec_context(out_stream)->codec_tag = 0;
  95. if ((*ctx_dst)->oformat->flags & AVFMT_GLOBALHEADER)
  96. ffmpeg_get_codec_context(out_stream)->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  97. }
  98. #ifndef _NDEBUG
  99. av_dump_format(*ctx_dst, 0, path, true);
  100. #endif
  101. if (!((*ctx_dst)->oformat->flags & AVFMT_NOFILE)) {
  102. ret = avio_open(&(*ctx_dst)->pb, path, AVIO_FLAG_WRITE);
  103. if (ret < 0) {
  104. return AE_FFMPEG_OPEN_IO_FAILED;
  105. }
  106. }
  107. return AE_NO;
  108. }
  109. static void remuxing(REMUXER_PARAM *param)
  110. {
  111. al_debug("remuxing:%s", param->src);
  112. int error = AE_NO;
  113. AVFormatContext *ctx_src = nullptr, *ctx_dst = nullptr;
  114. //call back start
  115. if (param->cb_state)
  116. param->cb_state(param->src, 1, AE_NO);
  117. do {
  118. error = open_src(&ctx_src, utils_string::ascii_utf8(param->src).c_str());
  119. if (error != AE_NO) {
  120. break;
  121. }
  122. error = open_dst(&ctx_dst, utils_string::ascii_utf8(param->dst).c_str(), ctx_src);
  123. if (error != AE_NO) {
  124. break;
  125. }
  126. int ret = avformat_write_header(ctx_dst, NULL);
  127. if (ret < 0) {
  128. error = AE_FFMPEG_WRITE_HEADER_FAILED;
  129. break;
  130. }
  131. error = remux_file(ctx_src, ctx_dst, param);
  132. if (error != AE_NO) {
  133. av_write_trailer(ctx_dst);
  134. break;
  135. }
  136. ret = av_write_trailer(ctx_dst);
  137. if (ret < 0)
  138. error = AE_FFMPEG_WRITE_TRAILER_FAILED;
  139. } while (0);
  140. if (ctx_src) {
  141. avformat_close_input(&ctx_src);
  142. }
  143. if (ctx_dst && !(ctx_dst->oformat->flags & AVFMT_NOFILE))
  144. avio_close(ctx_dst->pb);
  145. if (ctx_dst)
  146. avformat_free_context(ctx_dst);
  147. al_debug("remux %s to %s end with error:%s", param->src, param->dst, err2str(error));
  148. //call back end
  149. if (param->cb_state)
  150. param->cb_state(param->src, 0, error);
  151. remuxer_ffmpeg::instance()->remove_remux(param->src);
  152. }
  153. remuxer_ffmpeg *remuxer_ffmpeg::instance()
  154. {
  155. std::lock_guard<std::mutex> lock(_g_mutex);
  156. if (_g_instance == nullptr)
  157. _g_instance = new remuxer_ffmpeg();
  158. return _g_instance;
  159. }
  160. void remuxer_ffmpeg::release()
  161. {
  162. std::lock_guard<std::mutex> lock(_g_mutex);
  163. if (_g_instance)
  164. delete _g_instance;
  165. _g_instance = nullptr;
  166. }
  167. int remuxer_ffmpeg::create_remux(const REMUXER_PARAM &param)
  168. {
  169. std::lock_guard<std::mutex> lock(_g_mutex);
  170. auto itr = _handlers.find(param.src);
  171. if (itr != _handlers.end() && itr->second->fn.joinable() == true) {
  172. return AE_REMUX_RUNNING;
  173. }
  174. if (!strlen(param.src) || !strlen(param.dst) || !strcmp(param.src, param.dst))
  175. return AE_REMUX_INVALID_INOUT;
  176. #ifdef _MSC_VER
  177. struct _stat64 st = {0};
  178. _stat64(param.src, &st);
  179. #else
  180. struct stat st = {0};
  181. stat(param.src, &st);
  182. #endif
  183. if (!st.st_size)
  184. 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. itr->second->param.running = false;
  212. if (itr->second->fn.joinable())
  213. itr->second->fn.join();
  214. delete itr->second;
  215. _handlers.erase(itr);
  216. }
  217. }
  218. } // namespace am