comparison src/http/modules/ngx_http_mp4_module.c @ 7944:24f7904dbfa0

Mp4: added ngx_http_mp4_update_mdhd_atom() function. The function updates the duration field of mdhd atom. Previously it was updated in ngx_http_mp4_read_mdhd_atom(). The change makes it possible to alter track duration as a result of processing track frames.
author Roman Arutyunyan <arut@nginx.com>
date Thu, 28 Oct 2021 13:11:31 +0300
parents 7a55311b0dc3
children f17ba8ecaaf0
comparison
equal deleted inserted replaced
7943:2a7155733855 7944:24f7904dbfa0
68 ngx_uint_t end_chunk; 68 ngx_uint_t end_chunk;
69 ngx_uint_t start_chunk_samples; 69 ngx_uint_t start_chunk_samples;
70 ngx_uint_t end_chunk_samples; 70 ngx_uint_t end_chunk_samples;
71 uint64_t start_chunk_samples_size; 71 uint64_t start_chunk_samples_size;
72 uint64_t end_chunk_samples_size; 72 uint64_t end_chunk_samples_size;
73 uint64_t duration;
73 off_t start_offset; 74 off_t start_offset;
74 off_t end_offset; 75 off_t end_offset;
75 76
76 size_t tkhd_size; 77 size_t tkhd_size;
77 size_t mdhd_size; 78 size_t mdhd_size;
251 uint64_t atom_data_size); 252 uint64_t atom_data_size);
252 static void ngx_http_mp4_update_mdia_atom(ngx_http_mp4_file_t *mp4, 253 static void ngx_http_mp4_update_mdia_atom(ngx_http_mp4_file_t *mp4,
253 ngx_http_mp4_trak_t *trak); 254 ngx_http_mp4_trak_t *trak);
254 static ngx_int_t ngx_http_mp4_read_mdhd_atom(ngx_http_mp4_file_t *mp4, 255 static ngx_int_t ngx_http_mp4_read_mdhd_atom(ngx_http_mp4_file_t *mp4,
255 uint64_t atom_data_size); 256 uint64_t atom_data_size);
257 static void ngx_http_mp4_update_mdhd_atom(ngx_http_mp4_file_t *mp4,
258 ngx_http_mp4_trak_t *trak);
256 static ngx_int_t ngx_http_mp4_read_hdlr_atom(ngx_http_mp4_file_t *mp4, 259 static ngx_int_t ngx_http_mp4_read_hdlr_atom(ngx_http_mp4_file_t *mp4,
257 uint64_t atom_data_size); 260 uint64_t atom_data_size);
258 static ngx_int_t ngx_http_mp4_read_minf_atom(ngx_http_mp4_file_t *mp4, 261 static ngx_int_t ngx_http_mp4_read_minf_atom(ngx_http_mp4_file_t *mp4,
259 uint64_t atom_data_size); 262 uint64_t atom_data_size);
260 static void ngx_http_mp4_update_minf_atom(ngx_http_mp4_file_t *mp4, 263 static void ngx_http_mp4_update_minf_atom(ngx_http_mp4_file_t *mp4,
820 } 823 }
821 } 824 }
822 825
823 ngx_http_mp4_update_stbl_atom(mp4, &trak[i]); 826 ngx_http_mp4_update_stbl_atom(mp4, &trak[i]);
824 ngx_http_mp4_update_minf_atom(mp4, &trak[i]); 827 ngx_http_mp4_update_minf_atom(mp4, &trak[i]);
825 trak[i].size += trak[i].mdhd_size; 828 ngx_http_mp4_update_mdhd_atom(mp4, &trak[i]);
826 trak[i].size += trak[i].hdlr_size; 829 trak[i].size += trak[i].hdlr_size;
827 ngx_http_mp4_update_mdia_atom(mp4, &trak[i]); 830 ngx_http_mp4_update_mdia_atom(mp4, &trak[i]);
828 trak[i].size += trak[i].tkhd_size; 831 trak[i].size += trak[i].tkhd_size;
829 ngx_http_mp4_update_trak_atom(mp4, &trak[i]); 832 ngx_http_mp4_update_trak_atom(mp4, &trak[i]);
830 833
1747 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; 1750 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
1748 1751
1749 trak = ngx_mp4_last_trak(mp4); 1752 trak = ngx_mp4_last_trak(mp4);
1750 trak->mdhd_size = atom_size; 1753 trak->mdhd_size = atom_size;
1751 trak->timescale = timescale; 1754 trak->timescale = timescale;
1755 trak->duration = duration;
1752 1756
1753 ngx_mp4_set_32value(mdhd_atom->size, atom_size); 1757 ngx_mp4_set_32value(mdhd_atom->size, atom_size);
1754
1755 if (mdhd_atom->version[0] == 0) {
1756 ngx_mp4_set_32value(mdhd_atom->duration, duration);
1757
1758 } else {
1759 ngx_mp4_set_64value(mdhd64_atom->duration, duration);
1760 }
1761 1758
1762 atom = &trak->mdhd_atom_buf; 1759 atom = &trak->mdhd_atom_buf;
1763 atom->temporary = 1; 1760 atom->temporary = 1;
1764 atom->pos = atom_header; 1761 atom->pos = atom_header;
1765 atom->last = atom_header + atom_size; 1762 atom->last = atom_header + atom_size;
1767 trak->out[NGX_HTTP_MP4_MDHD_ATOM].buf = atom; 1764 trak->out[NGX_HTTP_MP4_MDHD_ATOM].buf = atom;
1768 1765
1769 ngx_mp4_atom_next(mp4, atom_data_size); 1766 ngx_mp4_atom_next(mp4, atom_data_size);
1770 1767
1771 return NGX_OK; 1768 return NGX_OK;
1769 }
1770
1771
1772 static void
1773 ngx_http_mp4_update_mdhd_atom(ngx_http_mp4_file_t *mp4,
1774 ngx_http_mp4_trak_t *trak)
1775 {
1776 ngx_buf_t *atom;
1777 ngx_mp4_mdhd_atom_t *mdhd_atom;
1778 ngx_mp4_mdhd64_atom_t *mdhd64_atom;
1779
1780 atom = trak->out[NGX_HTTP_MP4_MDHD_ATOM].buf;
1781 if (atom == NULL) {
1782 return;
1783 }
1784
1785 mdhd_atom = (ngx_mp4_mdhd_atom_t *) atom->pos;
1786 mdhd64_atom = (ngx_mp4_mdhd64_atom_t *) atom->pos;
1787
1788 if (mdhd_atom->version[0] == 0) {
1789 ngx_mp4_set_32value(mdhd_atom->duration, trak->duration);
1790
1791 } else {
1792 ngx_mp4_set_64value(mdhd64_atom->duration, trak->duration);
1793 }
1794
1795 trak->size += trak->mdhd_size;
1772 } 1796 }
1773 1797
1774 1798
1775 static ngx_int_t 1799 static ngx_int_t
1776 ngx_http_mp4_read_hdlr_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) 1800 ngx_http_mp4_read_hdlr_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)