2
0

srs_aac_raw_publish.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2013-2015 SRS(ossrs)
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. the Software, and to permit persons to whom the Software is furnished to do so,
  9. subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. */
  19. /**
  20. gcc srs_aac_raw_publish.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_aac_raw_publish
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. // for open audio raw file.
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <fcntl.h>
  29. #include "../../objs/include/srs_librtmp.h"
  30. // https://github.com/ossrs/srs/issues/212#issuecomment-64145910
  31. int read_audio_frame(char* data, int size, char** pp, char** frame, int* frame_size)
  32. {
  33. char* p = *pp;
  34. // @remark, for this demo, to publish aac raw file to SRS,
  35. // we search the adts frame from the buffer which cached the aac data.
  36. // please get aac adts raw data from device, it always a encoded frame.
  37. if (!srs_aac_is_adts(p, size - (p - data))) {
  38. srs_human_trace("aac adts raw data invalid.");
  39. return -1;
  40. }
  41. // @see srs_audio_write_raw_frame
  42. // each frame prefixed aac adts header, '1111 1111 1111'B, that is 0xFFF.,
  43. // for instance, frame = FF F1 5C 80 13 A0 FC 00 D0 33 83 E8 5B
  44. *frame = p;
  45. // skip some data.
  46. // @remark, user donot need to do this.
  47. p += srs_aac_adts_frame_size(p, size - (p - data));
  48. *pp = p;
  49. *frame_size = p - *frame;
  50. if (*frame_size <= 0) {
  51. srs_human_trace("aac adts raw data invalid.");
  52. return -1;
  53. }
  54. return 0;
  55. }
  56. int main(int argc, char** argv)
  57. {
  58. printf("publish raw audio as rtmp stream to server like FMLE/FFMPEG/Encoder\n");
  59. printf("SRS(ossrs) client librtmp library.\n");
  60. printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision());
  61. if (argc <= 2) {
  62. printf("Usage: %s <audio_raw_file> <rtmp_publish_url>\n", argv[0]);
  63. printf(" audio_raw_file: the audio raw steam file.\n");
  64. printf(" rtmp_publish_url: the rtmp publish url.\n");
  65. printf("For example:\n");
  66. printf(" %s ./audio.raw.aac rtmp://127.0.0.1:1935/live/livestream\n", argv[0]);
  67. printf("Where the file: http://winlinvip.github.io/srs.release/3rdparty/audio.raw.aac\n");
  68. printf("See: https://github.com/ossrs/srs/issues/212\n");
  69. exit(-1);
  70. }
  71. const char* raw_file = argv[1];
  72. const char* rtmp_url = argv[2];
  73. srs_human_trace("raw_file=%s, rtmp_url=%s", raw_file, rtmp_url);
  74. // open file
  75. int raw_fd = open(raw_file, O_RDONLY);
  76. if (raw_fd < 0) {
  77. srs_human_trace("open audio raw file %s failed.", raw_file);
  78. goto rtmp_destroy;
  79. }
  80. off_t file_size = lseek(raw_fd, 0, SEEK_END);
  81. if (file_size <= 0) {
  82. srs_human_trace("audio raw file %s empty.", raw_file);
  83. goto rtmp_destroy;
  84. }
  85. srs_human_trace("read entirely audio raw file, size=%dKB", (int)(file_size / 1024));
  86. char* audio_raw = (char*)malloc(file_size);
  87. if (!audio_raw) {
  88. srs_human_trace("alloc raw buffer failed for file %s.", raw_file);
  89. goto rtmp_destroy;
  90. }
  91. lseek(raw_fd, 0, SEEK_SET);
  92. ssize_t nb_read = 0;
  93. if ((nb_read = read(raw_fd, audio_raw, file_size)) != file_size) {
  94. srs_human_trace("buffer %s failed, expect=%dKB, actual=%dKB.",
  95. raw_file, (int)(file_size / 1024), (int)(nb_read / 1024));
  96. goto rtmp_destroy;
  97. }
  98. // connect rtmp context
  99. srs_rtmp_t rtmp = srs_rtmp_create(rtmp_url);
  100. if (srs_rtmp_handshake(rtmp) != 0) {
  101. srs_human_trace("simple handshake failed.");
  102. goto rtmp_destroy;
  103. }
  104. srs_human_trace("simple handshake success");
  105. if (srs_rtmp_connect_app(rtmp) != 0) {
  106. srs_human_trace("connect vhost/app failed.");
  107. goto rtmp_destroy;
  108. }
  109. srs_human_trace("connect vhost/app success");
  110. if (srs_rtmp_publish_stream(rtmp) != 0) {
  111. srs_human_trace("publish stream failed.");
  112. goto rtmp_destroy;
  113. }
  114. srs_human_trace("publish stream success");
  115. u_int32_t timestamp = 0;
  116. u_int32_t time_delta = 45;
  117. // @remark, to decode the file.
  118. char* p = audio_raw;
  119. for (;p < audio_raw + file_size;) {
  120. // @remark, read a frame from file buffer.
  121. char* data = NULL;
  122. int size = 0;
  123. if (read_audio_frame(audio_raw, file_size, &p, &data, &size) < 0) {
  124. srs_human_trace("read a frame from file buffer failed.");
  125. goto rtmp_destroy;
  126. }
  127. // 0 = Linear PCM, platform endian
  128. // 1 = ADPCM
  129. // 2 = MP3
  130. // 7 = G.711 A-law logarithmic PCM
  131. // 8 = G.711 mu-law logarithmic PCM
  132. // 10 = AAC
  133. // 11 = Speex
  134. char sound_format = 10;
  135. // 2 = 22 kHz
  136. char sound_rate = 2;
  137. // 1 = 16-bit samples
  138. char sound_size = 1;
  139. // 1 = Stereo sound
  140. char sound_type = 1;
  141. timestamp += time_delta;
  142. int ret = 0;
  143. if ((ret = srs_audio_write_raw_frame(rtmp,
  144. sound_format, sound_rate, sound_size, sound_type,
  145. data, size, timestamp)) != 0
  146. ) {
  147. srs_human_trace("send audio raw data failed. ret=%d", ret);
  148. goto rtmp_destroy;
  149. }
  150. srs_human_trace("sent packet: type=%s, time=%d, size=%d, codec=%d, rate=%d, sample=%d, channel=%d",
  151. srs_human_flv_tag_type2string(SRS_RTMP_TYPE_AUDIO), timestamp, size, sound_format, sound_rate, sound_size,
  152. sound_type);
  153. // @remark, when use encode device, it not need to sleep.
  154. usleep(1000 * time_delta);
  155. }
  156. rtmp_destroy:
  157. srs_rtmp_destroy(rtmp);
  158. close(raw_fd);
  159. free(audio_raw);
  160. return 0;
  161. }