comparison src/http/modules/ngx_http_mp4_module.c @ 5630:02584b335727

Mp4: improved logging after adding "end" support. Despite introducing start and end crop operations existing log messages still mostly refer only to start. Logging is improved to match both cases. New debug logging is added to track entry count in atoms after cropping. Two format type mismatches are fixed as well.
author Roman Arutyunyan <arut@nginx.com>
date Mon, 31 Mar 2014 20:05:53 +0400
parents c7bbc5fcf27c
children 0c0dd1aacdf5
comparison
equal deleted inserted replaced
5629:c7bbc5fcf27c 5630:02584b335727
2082 2082
2083 if (ngx_http_mp4_crop_stts_data(mp4, trak, 0) != NGX_OK) { 2083 if (ngx_http_mp4_crop_stts_data(mp4, trak, 0) != NGX_OK) {
2084 return NGX_ERROR; 2084 return NGX_ERROR;
2085 } 2085 }
2086 2086
2087 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2088 "time-to-sample entries:%uD", trak->time_to_sample_entries);
2089
2087 atom_size = sizeof(ngx_mp4_stts_atom_t) + (data->last - data->pos); 2090 atom_size = sizeof(ngx_mp4_stts_atom_t) + (data->last - data->pos);
2088 trak->size += atom_size; 2091 trak->size += atom_size;
2089 2092
2090 atom = trak->out[NGX_HTTP_MP4_STTS_ATOM].buf; 2093 atom = trak->out[NGX_HTTP_MP4_STTS_ATOM].buf;
2091 stts_atom = (ngx_mp4_stts_atom_t *) atom->pos; 2094 stts_atom = (ngx_mp4_stts_atom_t *) atom->pos;
2107 ngx_mp4_stts_entry_t *entry, *end; 2110 ngx_mp4_stts_entry_t *entry, *end;
2108 2111
2109 if (start) { 2112 if (start) {
2110 start_sec = mp4->start; 2113 start_sec = mp4->start;
2111 2114
2115 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2116 "mp4 stts crop start_time:%ui", start_sec);
2117
2112 } else if (mp4->length) { 2118 } else if (mp4->length) {
2113 start_sec = mp4->length; 2119 start_sec = mp4->length;
2114 2120
2121 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2122 "mp4 stts crop end_time:%ui", start_sec);
2123
2115 } else { 2124 } else {
2116 return NGX_OK; 2125 return NGX_OK;
2117 } 2126 }
2118 2127
2119 data = trak->out[NGX_HTTP_MP4_STTS_DATA].buf; 2128 data = trak->out[NGX_HTTP_MP4_STTS_DATA].buf;
2120 2129
2121 start_time = (uint64_t) start_sec * trak->timescale / 1000; 2130 start_time = (uint64_t) start_sec * trak->timescale / 1000;
2122
2123 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2124 "time-to-sample start_time:%uL", start_time);
2125 2131
2126 entries = trak->time_to_sample_entries; 2132 entries = trak->time_to_sample_entries;
2127 start_sample = 0; 2133 start_sample = 0;
2128 entry = (ngx_mp4_stts_entry_t *) data->pos; 2134 entry = (ngx_mp4_stts_entry_t *) data->pos;
2129 end = (ngx_mp4_stts_entry_t *) data->last; 2135 end = (ngx_mp4_stts_entry_t *) data->last;
2130 2136
2131 while (entry < end) { 2137 while (entry < end) {
2132 count = ngx_mp4_get_32value(entry->count); 2138 count = ngx_mp4_get_32value(entry->count);
2133 duration = ngx_mp4_get_32value(entry->duration); 2139 duration = ngx_mp4_get_32value(entry->duration);
2134 2140
2135 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, 2141 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2136 "count:%uD, duration:%uD", count, duration); 2142 "time:%uL, count:%uD, duration:%uD",
2143 start_time, count, duration);
2137 2144
2138 if (start_time < (uint64_t) count * duration) { 2145 if (start_time < (uint64_t) count * duration) {
2139 start_sample += (ngx_uint_t) (start_time / duration); 2146 start_sample += (ngx_uint_t) (start_time / duration);
2140 rest = (uint32_t) (start_time / duration); 2147 rest = (uint32_t) (start_time / duration);
2141 goto found; 2148 goto found;
2146 entries--; 2153 entries--;
2147 entry++; 2154 entry++;
2148 } 2155 }
2149 2156
2150 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, 2157 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
2151 "start time is out mp4 stts samples in \"%s\"", 2158 "%s time is out mp4 stts samples in \"%s\"",
2152 mp4->file.name.data); 2159 start ? "start" : "end", mp4->file.name.data);
2153 2160
2154 return NGX_ERROR; 2161 return NGX_ERROR;
2155 2162
2156 found: 2163 found:
2157
2158 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2159 "start_sample:%ui, new count:%uD", start_sample, count);
2160 2164
2161 if (start) { 2165 if (start) {
2162 ngx_mp4_set_32value(entry->count, count - rest); 2166 ngx_mp4_set_32value(entry->count, count - rest);
2163 data->pos = (u_char *) entry; 2167 data->pos = (u_char *) entry;
2164 trak->time_to_sample_entries = entries; 2168 trak->time_to_sample_entries = entries;
2165 trak->start_sample = start_sample; 2169 trak->start_sample = start_sample;
2166 2170
2171 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2172 "start_sample:%ui, new count:%uD",
2173 trak->start_sample, count - rest);
2174
2167 } else { 2175 } else {
2168 ngx_mp4_set_32value(entry->count, rest); 2176 ngx_mp4_set_32value(entry->count, rest);
2169 data->last = (u_char *) (entry + 1); 2177 data->last = (u_char *) (entry + 1);
2170 trak->time_to_sample_entries -= entries - 1; 2178 trak->time_to_sample_entries -= entries - 1;
2171 trak->end_sample = trak->start_sample + start_sample; 2179 trak->end_sample = trak->start_sample + start_sample;
2180
2181 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2182 "end_sample:%ui, new count:%uD",
2183 trak->end_sample, rest);
2172 } 2184 }
2173 2185
2174 return NGX_OK; 2186 return NGX_OK;
2175 } 2187 }
2176 2188
2271 } 2283 }
2272 2284
2273 ngx_http_mp4_crop_stss_data(mp4, trak, 1); 2285 ngx_http_mp4_crop_stss_data(mp4, trak, 1);
2274 ngx_http_mp4_crop_stss_data(mp4, trak, 0); 2286 ngx_http_mp4_crop_stss_data(mp4, trak, 0);
2275 2287
2288 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2289 "sync sample entries:%uD", trak->sync_samples_entries);
2290
2276 if (trak->sync_samples_entries) { 2291 if (trak->sync_samples_entries) {
2277 entry = (uint32_t *) data->pos; 2292 entry = (uint32_t *) data->pos;
2278 end = (uint32_t *) data->last; 2293 end = (uint32_t *) data->last;
2279 2294
2280 start_sample = trak->start_sample; 2295 start_sample = trak->start_sample;
2314 /* sync samples starts from 1 */ 2329 /* sync samples starts from 1 */
2315 2330
2316 if (start) { 2331 if (start) {
2317 start_sample = trak->start_sample + 1; 2332 start_sample = trak->start_sample + 1;
2318 2333
2334 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2335 "mp4 stss crop start_sample:%uD", start_sample);
2336
2319 } else if (mp4->length) { 2337 } else if (mp4->length) {
2320 start_sample = trak->end_sample + 1; 2338 start_sample = trak->end_sample + 1;
2339
2340 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2341 "mp4 stss crop end_sample:%uD", start_sample);
2321 2342
2322 } else { 2343 } else {
2323 return; 2344 return;
2324 } 2345 }
2325 2346
2330 end = (uint32_t *) data->last; 2351 end = (uint32_t *) data->last;
2331 2352
2332 while (entry < end) { 2353 while (entry < end) {
2333 sample = ngx_mp4_get_32value(entry); 2354 sample = ngx_mp4_get_32value(entry);
2334 2355
2335 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, 2356 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2336 "start:%uD, sync:%uD", start_sample, sample); 2357 "sync:%uD", sample);
2337 2358
2338 if (sample >= start_sample) { 2359 if (sample >= start_sample) {
2339 goto found; 2360 goto found;
2340 } 2361 }
2341 2362
2342 entries--; 2363 entries--;
2343 entry++; 2364 entry++;
2344 } 2365 }
2345 2366
2346 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, 2367 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2347 "start sample is out of mp4 stss atom"); 2368 "sample is out of mp4 stss atom");
2348 2369
2349 found: 2370 found:
2350 2371
2351 if (start) { 2372 if (start) {
2352 data->pos = (u_char *) entry; 2373 data->pos = (u_char *) entry;
2459 } 2480 }
2460 2481
2461 ngx_http_mp4_crop_ctts_data(mp4, trak, 1); 2482 ngx_http_mp4_crop_ctts_data(mp4, trak, 1);
2462 ngx_http_mp4_crop_ctts_data(mp4, trak, 0); 2483 ngx_http_mp4_crop_ctts_data(mp4, trak, 0);
2463 2484
2485 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2486 "composition offset entries:%uD",
2487 trak->composition_offset_entries);
2488
2464 if (trak->composition_offset_entries == 0) { 2489 if (trak->composition_offset_entries == 0) {
2465 trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf = NULL; 2490 trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf = NULL;
2466 trak->out[NGX_HTTP_MP4_CTTS_DATA].buf = NULL; 2491 trak->out[NGX_HTTP_MP4_CTTS_DATA].buf = NULL;
2467 return; 2492 return;
2468 } 2493 }
2492 /* sync samples starts from 1 */ 2517 /* sync samples starts from 1 */
2493 2518
2494 if (start) { 2519 if (start) {
2495 start_sample = trak->start_sample + 1; 2520 start_sample = trak->start_sample + 1;
2496 2521
2522 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2523 "mp4 ctts crop start_sample:%uD", start_sample);
2524
2497 } else if (mp4->length) { 2525 } else if (mp4->length) {
2498 start_sample = trak->end_sample - trak->start_sample + 1; 2526 start_sample = trak->end_sample - trak->start_sample + 1;
2527
2528 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2529 "mp4 ctts crop end_sample:%uD", start_sample);
2499 2530
2500 } else { 2531 } else {
2501 return; 2532 return;
2502 } 2533 }
2503 2534
2509 2540
2510 while (entry < end) { 2541 while (entry < end) {
2511 count = ngx_mp4_get_32value(entry->count); 2542 count = ngx_mp4_get_32value(entry->count);
2512 2543
2513 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, 2544 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2514 "start:%uD, count:%uD, offset:%uD", 2545 "sample:%uD, count:%uD, offset:%uD",
2515 start_sample, count, ngx_mp4_get_32value(entry->offset)); 2546 start_sample, count, ngx_mp4_get_32value(entry->offset));
2516 2547
2517 if (start_sample <= count) { 2548 if (start_sample <= count) {
2518 rest = start_sample - 1; 2549 rest = start_sample - 1;
2519 goto found; 2550 goto found;
2657 2688
2658 if (ngx_http_mp4_crop_stsc_data(mp4, trak, 0) != NGX_OK) { 2689 if (ngx_http_mp4_crop_stsc_data(mp4, trak, 0) != NGX_OK) {
2659 return NGX_ERROR; 2690 return NGX_ERROR;
2660 } 2691 }
2661 2692
2693 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2694 "sample-to-chunk entries:%uD",
2695 trak->sample_to_chunk_entries);
2696
2662 entry = (ngx_mp4_stsc_entry_t *) data->pos; 2697 entry = (ngx_mp4_stsc_entry_t *) data->pos;
2663 end = (ngx_mp4_stsc_entry_t *) data->last; 2698 end = (ngx_mp4_stsc_entry_t *) data->last;
2664 2699
2665 while (entry < end) { 2700 while (entry < end) {
2666 chunk = ngx_mp4_get_32value(entry->chunk); 2701 chunk = ngx_mp4_get_32value(entry->chunk);
2697 entries = trak->sample_to_chunk_entries - 1; 2732 entries = trak->sample_to_chunk_entries - 1;
2698 2733
2699 if (start) { 2734 if (start) {
2700 start_sample = (uint32_t) trak->start_sample; 2735 start_sample = (uint32_t) trak->start_sample;
2701 2736
2737 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2738 "mp4 stsc crop start_sample:%uD", start_sample);
2739
2702 } else if (mp4->length) { 2740 } else if (mp4->length) {
2703 start_sample = (uint32_t) (trak->end_sample - trak->start_sample); 2741 start_sample = (uint32_t) (trak->end_sample - trak->start_sample);
2742 samples = 0;
2704 2743
2705 data = trak->out[NGX_HTTP_MP4_STSC_START].buf; 2744 data = trak->out[NGX_HTTP_MP4_STSC_START].buf;
2706 2745
2707 if (data) { 2746 if (data) {
2708 entry = (ngx_mp4_stsc_entry_t *) data->pos; 2747 entry = (ngx_mp4_stsc_entry_t *) data->pos;
2712 if (samples > start_sample) { 2751 if (samples > start_sample) {
2713 samples = start_sample; 2752 samples = start_sample;
2714 ngx_mp4_set_32value(entry->samples, samples); 2753 ngx_mp4_set_32value(entry->samples, samples);
2715 } 2754 }
2716 2755
2717 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2718 "mp4 stsc using %uD start samples", samples);
2719
2720 start_sample -= samples; 2756 start_sample -= samples;
2721 } 2757 }
2758
2759 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2760 "mp4 stsc crop end_sample:%uD, ext_samples:%uD",
2761 start_sample, samples);
2722 2762
2723 } else { 2763 } else {
2724 return NGX_OK; 2764 return NGX_OK;
2725 } 2765 }
2726 2766
2738 while (entry < end) { 2778 while (entry < end) {
2739 2779
2740 next_chunk = ngx_mp4_get_32value(entry->chunk); 2780 next_chunk = ngx_mp4_get_32value(entry->chunk);
2741 2781
2742 ngx_log_debug5(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, 2782 ngx_log_debug5(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2743 "start_sample:%uD, chunk:%uD, chunks:%uD, " 2783 "sample:%uD, chunk:%uD, chunks:%uD, "
2744 "samples:%uD, id:%uD", 2784 "samples:%uD, id:%uD",
2745 start_sample, chunk, next_chunk - chunk, samples, id); 2785 start_sample, chunk, next_chunk - chunk, samples, id);
2746 2786
2747 n = (next_chunk - chunk) * samples; 2787 n = (next_chunk - chunk) * samples;
2748 2788
2761 } 2801 }
2762 2802
2763 next_chunk = trak->chunks + 1; 2803 next_chunk = trak->chunks + 1;
2764 2804
2765 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, 2805 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2766 "start_sample:%uD, chunk:%uD, chunks:%uD, samples:%uD", 2806 "sample:%uD, chunk:%uD, chunks:%uD, samples:%uD",
2767 start_sample, chunk, next_chunk - chunk, samples); 2807 start_sample, chunk, next_chunk - chunk, samples);
2768 2808
2769 n = (next_chunk - chunk) * samples; 2809 n = (next_chunk - chunk) * samples;
2770 2810
2771 if (start_sample > n) { 2811 if (start_sample > n) {
2772 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, 2812 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
2773 "start time is out mp4 stsc chunks in \"%s\"", 2813 "%s time is out mp4 stsc chunks in \"%s\"",
2774 mp4->file.name.data); 2814 start ? "start" : "end", mp4->file.name.data);
2775 return NGX_ERROR; 2815 return NGX_ERROR;
2776 } 2816 }
2777 2817
2778 found: 2818 found:
2779 2819
2789 2829
2790 target_chunk = chunk - 1; 2830 target_chunk = chunk - 1;
2791 target_chunk += start_sample / samples; 2831 target_chunk += start_sample / samples;
2792 chunk_samples = start_sample % samples; 2832 chunk_samples = start_sample % samples;
2793 2833
2794 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2795 "start chunk:%ui, samples:%uD",
2796 target_chunk, chunk_samples);
2797
2798 if (start) { 2834 if (start) {
2799 data->pos = (u_char *) entry; 2835 data->pos = (u_char *) entry;
2800 2836
2801 trak->sample_to_chunk_entries = entries; 2837 trak->sample_to_chunk_entries = entries;
2802 trak->start_chunk = target_chunk; 2838 trak->start_chunk = target_chunk;
2803 trak->start_chunk_samples = chunk_samples; 2839 trak->start_chunk_samples = chunk_samples;
2804 2840
2805 ngx_mp4_set_32value(entry->chunk, trak->start_chunk + 1); 2841 ngx_mp4_set_32value(entry->chunk, trak->start_chunk + 1);
2806 2842
2807 samples -= chunk_samples; 2843 samples -= chunk_samples;
2844
2845 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2846 "start_chunk:%ui, start_chunk_samples:%ui",
2847 trak->start_chunk, trak->start_chunk_samples);
2808 2848
2809 } else { 2849 } else {
2810 if (start_sample) { 2850 if (start_sample) {
2811 data->last = (u_char *) (entry + 1); 2851 data->last = (u_char *) (entry + 1);
2812 trak->sample_to_chunk_entries -= entries - 1; 2852 trak->sample_to_chunk_entries -= entries - 1;
2826 trak->end_chunk = target_chunk; 2866 trak->end_chunk = target_chunk;
2827 } 2867 }
2828 2868
2829 samples = chunk_samples; 2869 samples = chunk_samples;
2830 next_chunk = chunk + 1; 2870 next_chunk = chunk + 1;
2871
2872 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
2873 "end_chunk:%ui, end_chunk_samples:%ui",
2874 trak->end_chunk, trak->end_chunk_samples);
2831 } 2875 }
2832 2876
2833 if (chunk_samples && next_chunk - target_chunk == 2) { 2877 if (chunk_samples && next_chunk - target_chunk == 2) {
2834 2878
2835 ngx_mp4_set_32value(entry->samples, samples); 2879 ngx_mp4_set_32value(entry->samples, samples);
3143 trak->start_offset = ngx_mp4_get_32value(data->pos); 3187 trak->start_offset = ngx_mp4_get_32value(data->pos);
3144 trak->start_offset += trak->start_chunk_samples_size; 3188 trak->start_offset += trak->start_chunk_samples_size;
3145 ngx_mp4_set_32value(data->pos, trak->start_offset); 3189 ngx_mp4_set_32value(data->pos, trak->start_offset);
3146 3190
3147 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, 3191 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
3148 "start chunk offset:%uD", trak->start_offset); 3192 "start chunk offset:%O", trak->start_offset);
3149 3193
3150 if (mp4->length) { 3194 if (mp4->length) {
3151 3195
3152 if (trak->end_chunk > trak->chunks) { 3196 if (trak->end_chunk > trak->chunks) {
3153 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, 3197 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
3327 trak->start_offset = ngx_mp4_get_64value(data->pos); 3371 trak->start_offset = ngx_mp4_get_64value(data->pos);
3328 trak->start_offset += trak->start_chunk_samples_size; 3372 trak->start_offset += trak->start_chunk_samples_size;
3329 ngx_mp4_set_64value(data->pos, trak->start_offset); 3373 ngx_mp4_set_64value(data->pos, trak->start_offset);
3330 3374
3331 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, 3375 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
3332 "start chunk offset:%uL", trak->start_offset); 3376 "start chunk offset:%O", trak->start_offset);
3333 3377
3334 if (mp4->length) { 3378 if (mp4->length) {
3335 3379
3336 if (trak->end_chunk > trak->chunks) { 3380 if (trak->end_chunk > trak->chunks) {
3337 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, 3381 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,