You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
5.2 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*******************************************************************************
  2. mp4_process.c - .
  3. Copyright (C) 2007-2009 CodeShop B.V.
  4. http://www.code-shop.com
  5. For licensing see the LICENSE file
  6. ******************************************************************************/
  7. #ifdef HAVE_CONFIG_H
  8. #include "config.h"
  9. #endif
  10. #ifdef _MSC_VER
  11. #define _CRTDBG_MAP_ALLOC
  12. #include <stdlib.h>
  13. #include <crtdbg.h>
  14. #endif
  15. #if !defined(BUILDING_NGINX)
  16. #include "mp4_process.h"
  17. #endif
  18. #include "moov.h"
  19. #include "mp4_io.h"
  20. #include "output_bucket.h"
  21. #if defined(BUILDING_H264_STREAMING) || defined(BUILDING_MP4SPLIT)
  22. #include "output_mp4.h"
  23. #endif
  24. #if defined(BUILDING_MP4SPLIT)
  25. #include "output_mov.h"
  26. #endif
  27. #if defined(BUILDING_SMOOTH_STREAMING) || defined(BUILDING_MP4SPLIT)
  28. #include "output_ismv.h"
  29. #endif
  30. #if defined(BUILDING_FLV_STREAMING) || defined(BUILDING_MP4SPLIT)
  31. #include "output_flv.h"
  32. #endif
  33. #if defined(BUILDING_SMOOTH_STREAMING) || defined(BUILDING_MP4SPLIT)
  34. #include "output_ts.h"
  35. #endif
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #ifdef WIN32
  39. #define snprintf _snprintf
  40. #endif
  41. static int rxs_get_bucket(char const* filename, bucket_t** buckets,
  42. mp4_split_options_t* options)
  43. {
  44. // fragment request?
  45. if(!options->fragments)
  46. return 0;
  47. {
  48. char rxs_filename[256];
  49. mem_range_t* mem_range;
  50. unsigned char const* first;
  51. unsigned char const* last;
  52. // create rxs filename
  53. snprintf(rxs_filename, sizeof(rxs_filename), "%s.%u.rxs",
  54. filename, options->fragment_track_id);
  55. mem_range = mem_range_init_read(rxs_filename);
  56. if(!mem_range)
  57. {
  58. // TODO: CHANGE BACK TO 0, otherwise VOD won't work
  59. // return 409; // 409 Conflict
  60. return 0;
  61. }
  62. first = (unsigned char const*)(mem_range_map(mem_range, 0, (uint32_t)mem_range->filesize_));
  63. last = first + mem_range->filesize_;
  64. while(first != last)
  65. {
  66. uint64_t time = read_64(first);
  67. if(time == options->fragment_start)
  68. {
  69. uint64_t offset = read_64(first + 8);
  70. uint64_t size = read_64(first + 16);
  71. bucket_insert_tail(buckets, bucket_init_file(offset, size));
  72. break;
  73. }
  74. first += sizeof(rxs_t);
  75. }
  76. mem_range_exit(mem_range);
  77. return first == last ? 404 : 200;
  78. }
  79. }
  80. #if defined(BUILDING_NGINX)
  81. static
  82. #else
  83. extern
  84. #endif
  85. int mp4_process(const char* filename, uint64_t filesize, int verbose,
  86. bucket_t** buckets,
  87. mp4_split_options_t* options)
  88. {
  89. int result = 1;
  90. // implement mod_flv_streaming for convenience
  91. if(ends_with(filename, ".flv") || options->input_format == INPUT_FORMAT_FLV)
  92. {
  93. static const unsigned char flv_header[13] = {
  94. 'F', 'L', 'V', 0x01, 0x01, 0x00, 0x00, 0x00, 0x09,
  95. 0x00, 0x00, 0x00, 0x09
  96. };
  97. uint64_t start = options->start_integer;
  98. if(start != 0)
  99. {
  100. bucket_insert_tail(buckets, bucket_init_memory(flv_header, 13));
  101. }
  102. bucket_insert_tail(buckets, bucket_init_file(start, filesize - start));
  103. return 200; // HTTP_OK;
  104. }
  105. // check for serving fragments using the fast random access file
  106. {
  107. int http_status = rxs_get_bucket(filename, buckets, options);
  108. if(http_status)
  109. {
  110. return http_status;
  111. }
  112. }
  113. #ifdef HAVE_OUTPUT_TS
  114. if(options->fragments && options->output_format == OUTPUT_FORMAT_TS)
  115. {
  116. result = output_ts(filename, buckets, options);
  117. return result == 0 ? 415 : 200;
  118. }
  119. #endif
  120. // Open the file
  121. {
  122. mp4_open_flags flags = options->fragments ? MP4_OPEN_MFRA : MP4_OPEN_ALL;
  123. mp4_context_t* mp4_context =
  124. mp4_open(filename, filesize, flags, verbose);
  125. if(mp4_context == NULL)
  126. {
  127. result = 0;
  128. }
  129. if(result)
  130. {
  131. #ifdef HAVE_OUTPUT_ISMV
  132. if(options->fragments)
  133. {
  134. result = output_ismv(mp4_context, buckets, options);
  135. }
  136. else
  137. #endif
  138. {
  139. // split the movie
  140. unsigned int trak_sample_start[MAX_TRACKS];
  141. unsigned int trak_sample_end[MAX_TRACKS];
  142. result = mp4_split(mp4_context, trak_sample_start, trak_sample_end,
  143. options);
  144. if(result)
  145. {
  146. if(0)
  147. {
  148. }
  149. #ifdef HAVE_OUTPUT_FLV
  150. else if(options->output_format == OUTPUT_FORMAT_FLV)
  151. {
  152. result = output_flv(mp4_context,
  153. trak_sample_start,
  154. trak_sample_end,
  155. buckets, options);
  156. }
  157. #endif
  158. #ifdef HAVE_OUTPUT_MP4
  159. else if(options->output_format == OUTPUT_FORMAT_MP4)
  160. {
  161. result = output_mp4(mp4_context,
  162. trak_sample_start,
  163. trak_sample_end,
  164. buckets, options);
  165. }
  166. #endif
  167. #ifdef HAVE_OUTPUT_MOV
  168. else if(options->output_format == OUTPUT_FORMAT_MOV)
  169. {
  170. result = output_mov(mp4_context,
  171. trak_sample_start,
  172. trak_sample_end,
  173. buckets, options);
  174. }
  175. #endif
  176. }
  177. }
  178. // close the file
  179. mp4_close(mp4_context);
  180. }
  181. }
  182. if(!result)
  183. {
  184. return 415; // HTTP_UNSUPPORTED_MEDIA_TYPE;
  185. }
  186. return 200; // HTTP_OK;
  187. }
  188. // End Of File