srs_audio_raw_publish.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_audio_raw_publish.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_audio_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-63648892
  31. // allspace:
  32. // Take this file as an example: https://github.com/allspace/files/blob/master/srs.pcm
  33. // It's captured using SDK callback method. I have filtered out h264 video, so it's audio only now.
  34. // For every frame, it's a 8 bytes vendor specific header, following 160 bytes audio frame.
  35. // The header part can be ignored.
  36. int read_audio_frame(char* audio_raw, int file_size, char** pp, char** pdata, int* psize)
  37. {
  38. char* p = *pp;
  39. if (file_size - (p - audio_raw) < 168) {
  40. srs_human_trace("audio must be 160+8 bytes. left %d bytes.",
  41. (int)(file_size - (p - audio_raw)));
  42. return - 1;
  43. }
  44. // ignore 8bytes vendor specific header.
  45. p += 8;
  46. // 160 bytes audio frame
  47. *pdata = p;
  48. *psize = 160;
  49. // next frame.
  50. *pp = p + *psize;
  51. return 0;
  52. }
  53. int main(int argc, char** argv)
  54. {
  55. printf("publish raw audio as rtmp stream to server like FMLE/FFMPEG/Encoder\n");
  56. printf("SRS(ossrs) client librtmp library.\n");
  57. printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision());
  58. if (argc <= 2) {
  59. printf("Usage: %s <audio_raw_file> <rtmp_publish_url>\n", argv[0]);
  60. printf(" audio_raw_file: the audio raw steam file.\n");
  61. printf(" rtmp_publish_url: the rtmp publish url.\n");
  62. printf("For example:\n");
  63. printf(" %s ./audio.raw.pcm rtmp://127.0.0.1:1935/live/livestream\n", argv[0]);
  64. printf("Where the file: http://winlinvip.github.io/srs.release/3rdparty/audio.raw.pcm\n");
  65. printf("See: https://github.com/ossrs/srs/issues/212\n");
  66. exit(-1);
  67. }
  68. const char* raw_file = argv[1];
  69. const char* rtmp_url = argv[2];
  70. srs_human_trace("raw_file=%s, rtmp_url=%s", raw_file, rtmp_url);
  71. // open file
  72. int raw_fd = open(raw_file, O_RDONLY);
  73. if (raw_fd < 0) {
  74. srs_human_trace("open audio raw file %s failed.", raw_file);
  75. goto rtmp_destroy;
  76. }
  77. off_t file_size = lseek(raw_fd, 0, SEEK_END);
  78. if (file_size <= 0) {
  79. srs_human_trace("audio raw file %s empty.", raw_file);
  80. goto rtmp_destroy;
  81. }
  82. srs_human_trace("read entirely audio raw file, size=%dKB", (int)(file_size / 1024));
  83. char* audio_raw = (char*)malloc(file_size);
  84. if (!audio_raw) {
  85. srs_human_trace("alloc raw buffer failed for file %s.", raw_file);
  86. goto rtmp_destroy;
  87. }
  88. lseek(raw_fd, 0, SEEK_SET);
  89. ssize_t nb_read = 0;
  90. if ((nb_read = read(raw_fd, audio_raw, file_size)) != file_size) {
  91. srs_human_trace("buffer %s failed, expect=%dKB, actual=%dKB.",
  92. raw_file, (int)(file_size / 1024), (int)(nb_read / 1024));
  93. goto rtmp_destroy;
  94. }
  95. // connect rtmp context
  96. srs_rtmp_t rtmp = srs_rtmp_create(rtmp_url);
  97. if (srs_rtmp_handshake(rtmp) != 0) {
  98. srs_human_trace("simple handshake failed.");
  99. goto rtmp_destroy;
  100. }
  101. srs_human_trace("simple handshake success");
  102. if (srs_rtmp_connect_app(rtmp) != 0) {
  103. srs_human_trace("connect vhost/app failed.");
  104. goto rtmp_destroy;
  105. }
  106. srs_human_trace("connect vhost/app success");
  107. if (srs_rtmp_publish_stream(rtmp) != 0) {
  108. srs_human_trace("publish stream failed.");
  109. goto rtmp_destroy;
  110. }
  111. srs_human_trace("publish stream success");
  112. u_int32_t timestamp = 0;
  113. u_int32_t time_delta = 17;
  114. // @remark, to decode the file.
  115. char* p = audio_raw;
  116. for (;p < audio_raw + file_size;) {
  117. // @remark, read a frame from file buffer.
  118. char* data = NULL;
  119. int size = 0;
  120. if (read_audio_frame(audio_raw, file_size, &p, &data, &size) < 0) {
  121. srs_human_trace("read a frame from file buffer failed.");
  122. goto rtmp_destroy;
  123. }
  124. // 0 = Linear PCM, platform endian
  125. // 1 = ADPCM
  126. // 2 = MP3
  127. // 7 = G.711 A-law logarithmic PCM
  128. // 8 = G.711 mu-law logarithmic PCM
  129. // 10 = AAC
  130. // 11 = Speex
  131. char sound_format = 1;
  132. // 3 = 44 kHz
  133. char sound_rate = 3;
  134. // 1 = 16-bit samples
  135. char sound_size = 1;
  136. // 1 = Stereo sound
  137. char sound_type = 1;
  138. timestamp += time_delta;
  139. if (srs_audio_write_raw_frame(rtmp,
  140. sound_format, sound_rate, sound_size, sound_type,
  141. data, size, timestamp) != 0
  142. ) {
  143. srs_human_trace("send audio raw data failed.");
  144. goto rtmp_destroy;
  145. }
  146. srs_human_trace("sent packet: type=%s, time=%d, size=%d, codec=%d, rate=%d, sample=%d, channel=%d",
  147. srs_human_flv_tag_type2string(SRS_RTMP_TYPE_AUDIO), timestamp, size, sound_format, sound_rate, sound_size,
  148. sound_type);
  149. // @remark, when use encode device, it not need to sleep.
  150. usleep(1000 * time_delta);
  151. }
  152. rtmp_destroy:
  153. srs_rtmp_destroy(rtmp);
  154. close(raw_fd);
  155. free(audio_raw);
  156. return 0;
  157. }