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.

301 lines
7.7 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*******************************************************************************
  2. mod_h264_streaming.c
  3. mod_h264_streaming - An Apache module for streaming Quicktime/MPEG4 files.
  4. Copyright (C) 2007-2009 CodeShop B.V.
  5. Licensing
  6. The Streaming Module is licened under a Creative Commons License. It
  7. allows you to use, modify and redistribute the module, but only for
  8. *noncommercial* purposes. For corporate use, please apply for a
  9. commercial license.
  10. Creative Commons License:
  11. http://creativecommons.org/licenses/by-nc-sa/3.0/
  12. Commercial License for H264 Streaming Module:
  13. http://h264.code-shop.com/trac/wiki/Mod-H264-Streaming-License-Version2
  14. Commercial License for Smooth Streaming Module:
  15. http://smoothstreaming.code-shop.com/trac/wiki/Mod-Smooth-Streaming-License
  16. ******************************************************************************/
  17. #include <httpd.h>
  18. #include <http_core.h>
  19. #include <http_config.h>
  20. #include <http_protocol.h>
  21. //#include <http_log.h>
  22. #include <http_request.h> // or ap_update_mtime
  23. #include <apr_version.h>
  24. #include <apr_strings.h> // for apr_itoa, apr_pstrcat
  25. #include <apr_buckets.h>
  26. #include "mp4_io.h"
  27. #include "mp4_process.h"
  28. #include "moov.h"
  29. #include "output_bucket.h"
  30. #ifdef BUILDING_H264_STREAMING
  31. #include "output_mp4.h"
  32. #define X_MOD_STREAMING_KEY X_MOD_H264_STREAMING_KEY
  33. #define X_MOD_STREAMING_VERSION X_MOD_H264_STREAMING_VERSION
  34. #define H264_STREAMING_HANDLER "h264-streaming.extensions"
  35. #endif
  36. #ifdef BUILDING_SMOOTH_STREAMING
  37. #include "ism_reader.h"
  38. #include "output_ismv.h"
  39. #define X_MOD_STREAMING_KEY X_MOD_SMOOTH_STREAMING_KEY
  40. #define X_MOD_STREAMING_VERSION X_MOD_SMOOTH_STREAMING_VERSION
  41. #define H264_STREAMING_HANDLER "smooth-streaming.extensions"
  42. #endif
  43. #ifdef BUILDING_FLV_STREAMING
  44. #define H264_STREAMING_HANDLER "flv-streaming.extensions"
  45. #include "output_flv.h"
  46. #endif
  47. #if 0
  48. /* Mod-H264-Streaming configuration
  49. [httpd.conf]
  50. LoadModule h264_streaming_module /usr/lib/apache2/modules/mod_h264_streaming.so
  51. AddHandler h264-streaming.extensions .mp4
  52. */
  53. /* Mod-Smooth-Streaming configuration
  54. [httpd.conf]
  55. LoadModule smooth_streaming_module /usr/lib/apache2/modules/mod_smooth_streaming.so
  56. AddHandler smooth-streaming.extensions .ism
  57. [.htaccess]
  58. RewriteEngine On
  59. RewriteRule ^(.*/)?(.*)\.([is])sm/[Mm]anifest$ $1$2.$3sm/$2.ismc [L]
  60. RewriteRule ^(.*/)?(.*)\.([is])sm/QualityLevels\(([0-9]+)\)/Fragments\((.*)=([0-9]+)\)(.*)$ $1$2.$3sm/$2.ism?bitrate=$4&$5=$6 [L]
  61. */
  62. #endif
  63. int drive_h264_streaming(request_rec *r)
  64. {
  65. apr_finfo_t fi;
  66. apr_bucket_brigade *bb;
  67. apr_file_t *fp = NULL;
  68. apr_status_t rv = APR_SUCCESS;
  69. struct mp4_split_options_t* options;
  70. struct bucket_t* buckets;
  71. char filename[256];
  72. // Module version info
  73. apr_table_set(r->headers_out, X_MOD_STREAMING_KEY, X_MOD_STREAMING_VERSION);
  74. options = mp4_split_options_init();
  75. if(r->args && !mp4_split_options_set(options, r->args, strlen(r->args)))
  76. {
  77. return HTTP_FORBIDDEN;
  78. }
  79. //strncpy(filename, r->filename, sizeof(filename) / sizeof(char) - 1);
  80. strncpy(filename, "/srv/web/stub.mp4", sizeof(filename) / sizeof(char) - 1);
  81. filename[sizeof(filename) / sizeof(char) - 1] = '\0';
  82. #ifdef BUILDING_SMOOTH_STREAMING
  83. // if it is a fragment request then we read the server manifest file
  84. // and based on the bitrate and track type we set the filename and track id
  85. if(ends_with(filename, ".ism"))
  86. {
  87. if(options->output_format != OUTPUT_FORMAT_TS)
  88. {
  89. ism_t* ism = ism_init(filename);
  90. if(ism == NULL)
  91. {
  92. return HTTP_NOT_FOUND;
  93. }
  94. {
  95. char* dir_end = strrchr(filename, '/');
  96. const char* src;
  97. if(!ism_get_source(ism, options->fragment_bitrate,
  98. options->fragment_type, &src, &options->fragment_track_id))
  99. {
  100. return HTTP_NOT_FOUND;
  101. }
  102. dir_end = dir_end == NULL ? filename : (dir_end + 1);
  103. strcpy(dir_end, src);
  104. ism_exit(ism);
  105. }
  106. }
  107. }
  108. #endif
  109. rv = apr_stat(&fi, filename, APR_FINFO_SIZE, r->pool);
  110. if(rv)
  111. {
  112. /* Let the core handle it. */
  113. return DECLINED;
  114. }
  115. /* Open the file */
  116. rv = apr_file_open(&fp, filename, APR_READ, APR_OS_DEFAULT, r->pool);
  117. if(rv)
  118. {
  119. //ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "file permissions deny server access: %s", r->filename);
  120. return HTTP_FORBIDDEN;
  121. }
  122. // our ouput bucket
  123. buckets = 0;
  124. {
  125. int verbose = 0;
  126. int http_status =
  127. mp4_process(filename, fi.size, verbose, &buckets, options);
  128. mp4_split_options_exit(options);
  129. if(http_status != 200)
  130. {
  131. if(buckets)
  132. {
  133. buckets_exit(buckets);
  134. }
  135. return http_status;
  136. }
  137. ap_set_content_type(r, "video/mp4");
  138. }
  139. {
  140. uint64_t content_length = 0;
  141. bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
  142. {
  143. bucket_t* bucket = buckets;
  144. if(bucket)
  145. {
  146. do
  147. {
  148. switch(bucket->type_)
  149. {
  150. case BUCKET_TYPE_MEMORY:
  151. rv = apr_brigade_write(bb, NULL, NULL,
  152. (const char*)bucket->buf_, bucket->size_);
  153. if(rv)
  154. {
  155. //ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "unable to write memory bucket in brigade");
  156. return HTTP_INTERNAL_SERVER_ERROR;
  157. }
  158. break;
  159. case BUCKET_TYPE_FILE:
  160. #if APR_MAJOR_VERSION >= 1 && APR_MINOR_VERSION >= 1
  161. apr_brigade_insert_file(bb, fp, bucket->offset_, bucket->size_, r->pool);
  162. #else
  163. {
  164. apr_bucket *e;
  165. e = apr_bucket_file_create(fp, bucket->offset_, bucket->size_, r->pool,
  166. r->connection->bucket_alloc);
  167. APR_BRIGADE_INSERT_TAIL(bb, e);
  168. }
  169. #endif
  170. break;
  171. }
  172. content_length += bucket->size_;
  173. bucket = bucket->next_;
  174. } while(bucket != buckets);
  175. buckets_exit(buckets);
  176. }
  177. }
  178. // Add EOS bucket for byterange_filter to work
  179. {
  180. apr_bucket* eos_bucket = apr_bucket_eos_create(bb->bucket_alloc);
  181. APR_BRIGADE_INSERT_TAIL(bb, eos_bucket);
  182. }
  183. ap_set_content_length(r, content_length);
  184. // Add last-modified headers
  185. ap_update_mtime(r, r->finfo.mtime);
  186. ap_set_last_modified(r);
  187. // Create an ETag with an additional mdat_offset and mdat_size. The first
  188. // character in a vlist_validator string is "W" for weak and also close the
  189. // ETag header with a ".
  190. // TODO: Make it strong
  191. r->vlist_validator = apr_pstrcat(r->pool, "X",
  192. apr_itoa(r->pool, content_length), "\"", NULL);
  193. ap_set_etag(r);
  194. // Allow byte range requests
  195. apr_table_setn(r->headers_out, "Accept-Ranges", "bytes");
  196. // Check for conditional requests
  197. {
  198. int errstatus;
  199. if((errstatus = ap_meets_conditions(r)) != OK)
  200. {
  201. return errstatus;
  202. }
  203. }
  204. }
  205. return ap_pass_brigade(r->output_filters, bb);
  206. }
  207. static int h264_streaming_handler(request_rec *r)
  208. {
  209. if ((!r->handler) || (strcmp(r->handler, H264_STREAMING_HANDLER)))
  210. {
  211. return DECLINED;
  212. }
  213. r->allowed |= (AP_METHOD_BIT << M_GET);
  214. if (r->method_number != M_GET)
  215. {
  216. return HTTP_METHOD_NOT_ALLOWED;
  217. }
  218. return drive_h264_streaming(r);
  219. }
  220. static const command_rec h264_streaming_cmds[] =
  221. {
  222. {NULL}
  223. };
  224. //static void register_hooks(apr_pool_t *p)
  225. //{
  226. // ap_hook_handler(h264_streaming_handler, NULL, NULL, APR_HOOK_MIDDLE);
  227. //}
  228. /*
  229. #ifdef __cplusplus
  230. extern "C" {
  231. #endif
  232. #ifdef BUILDING_H264_STREAMING
  233. module AP_MODULE_DECLARE_DATA h264_streaming_module =
  234. #elif BUILDING_SMOOTH_STREAMING
  235. module AP_MODULE_DECLARE_DATA smooth_streaming_module =
  236. #endif
  237. {
  238. STANDARD20_MODULE_STUFF,
  239. NULL,
  240. NULL,
  241. NULL,
  242. NULL,
  243. h264_streaming_cmds,
  244. register_hooks
  245. };
  246. #ifdef __cplusplus
  247. } // extern C definitions
  248. #endif
  249. */
  250. // End Of File