winlin

for #293, support http mp3 streaming

  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013-2015 winlin
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 +this software and associated documentation files (the "Software"), to deal in
  8 +the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10 +the Software, and to permit persons to whom the Software is furnished to do so,
  11 +subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22 +*/
  23 +
  24 +#include <srs_kernel_mp3.hpp>
  25 +
  26 +// for srs-librtmp, @see https://github.com/winlinvip/simple-rtmp-server/issues/213
  27 +#ifndef _WIN32
  28 +#include <unistd.h>
  29 +#endif
  30 +
  31 +#include <fcntl.h>
  32 +#include <sstream>
  33 +using namespace std;
  34 +
  35 +#include <srs_kernel_log.hpp>
  36 +#include <srs_kernel_error.hpp>
  37 +#include <srs_kernel_stream.hpp>
  38 +#include <srs_kernel_file.hpp>
  39 +#include <srs_kernel_codec.hpp>
  40 +
  41 +SrsMp3Encoder::SrsMp3Encoder()
  42 +{
  43 + _fs = NULL;
  44 + tag_stream = new SrsStream();
  45 +}
  46 +
  47 +SrsMp3Encoder::~SrsMp3Encoder()
  48 +{
  49 + srs_freep(tag_stream);
  50 +}
  51 +
  52 +int SrsMp3Encoder::initialize(SrsFileWriter* fs)
  53 +{
  54 + int ret = ERROR_SUCCESS;
  55 +
  56 + srs_assert(fs);
  57 +
  58 + if (!fs->is_open()) {
  59 + ret = ERROR_KERNEL_MP3_STREAM_CLOSED;
  60 + srs_warn("stream is not open for encoder. ret=%d", ret);
  61 + return ret;
  62 + }
  63 +
  64 + _fs = fs;
  65 +
  66 + return ret;
  67 +}
  68 +
  69 +int SrsMp3Encoder::write_header()
  70 +{
  71 + static char id3[] = {
  72 + (char)0x49, (char)0x44, (char)0x33, // ID3
  73 + (char)0x03, (char)0x00, // version
  74 + (char)0x00, // flags
  75 + (char)0x00, (char)0x00, (char)0x00, (char)0x0a, // size
  76 +
  77 + (char)0x00, (char)0x00, (char)0x00, (char)0x00, // FrameID
  78 + (char)0x00, (char)0x00, (char)0x00, (char)0x00, // FrameSize
  79 + (char)0x00, (char)0x00 // Flags
  80 + };
  81 + return _fs->write(id3, sizeof(id3), NULL);
  82 +}
  83 +
  84 +int SrsMp3Encoder::write_audio(int64_t timestamp, char* data, int size)
  85 +{
  86 + int ret = ERROR_SUCCESS;
  87 +
  88 + srs_assert(data);
  89 +
  90 + timestamp &= 0x7fffffff;
  91 +
  92 + SrsStream* stream = tag_stream;
  93 + if ((ret = stream->initialize(data, size)) != ERROR_SUCCESS) {
  94 + return ret;
  95 + }
  96 +
  97 + // audio decode
  98 + if (!stream->require(1)) {
  99 + ret = ERROR_MP3_DECODE_ERROR;
  100 + srs_error("mp3 decode audio sound_format failed. ret=%d", ret);
  101 + return ret;
  102 + }
  103 +
  104 + // @see: E.4.2 Audio Tags, video_file_format_spec_v10_1.pdf, page 76
  105 + int8_t sound_format = stream->read_1bytes();
  106 +
  107 + // @see: SrsAvcAacCodec::audio_aac_demux
  108 + //int8_t sound_type = sound_format & 0x01;
  109 + //int8_t sound_size = (sound_format >> 1) & 0x01;
  110 + //int8_t sound_rate = (sound_format >> 2) & 0x03;
  111 + sound_format = (sound_format >> 4) & 0x0f;
  112 +
  113 + if ((SrsCodecAudio)sound_format != SrsCodecAudioMP3) {
  114 + ret = ERROR_MP3_DECODE_ERROR;
  115 + srs_error("mp3 required, format=%d. ret=%d", sound_format, ret);
  116 + return ret;
  117 + }
  118 +
  119 + if (!stream->require(1)) {
  120 + ret = ERROR_MP3_DECODE_ERROR;
  121 + srs_error("mp3 decode aac_packet_type failed. ret=%d", ret);
  122 + return ret;
  123 + }
  124 +
  125 + return _fs->write(data + stream->pos(), size - stream->pos(), NULL);
  126 +}
  127 +
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013-2015 winlin
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 +this software and associated documentation files (the "Software"), to deal in
  8 +the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10 +the Software, and to permit persons to whom the Software is furnished to do so,
  11 +subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22 +*/
  23 +
  24 +#ifndef SRS_KERNEL_MP3_HPP
  25 +#define SRS_KERNEL_MP3_HPP
  26 +
  27 +/*
  28 +#include <srs_kernel_mp3.hpp>
  29 +*/
  30 +#include <srs_core.hpp>
  31 +
  32 +#include <string>
  33 +
  34 +class SrsStream;
  35 +class SrsFileWriter;
  36 +class SrsFileReader;
  37 +
  38 +/**
  39 +* encode data to aac file.
  40 +*/
  41 +class SrsMp3Encoder
  42 +{
  43 +private:
  44 + SrsFileWriter* _fs;
  45 +private:
  46 + SrsStream* tag_stream;
  47 +public:
  48 + SrsMp3Encoder();
  49 + virtual ~SrsMp3Encoder();
  50 +public:
  51 + /**
  52 + * initialize the underlayer file stream.
  53 + * @remark user can initialize multiple times to encode multiple mp3 files.
  54 + * @remark, user must free the fs, mp3 encoder never close/free it.
  55 + */
  56 + virtual int initialize(SrsFileWriter* fs);
  57 +public:
  58 + /**
  59 + * write mp3 id3 v2.3 header.
  60 + * @see mp3.id3v2.3.0.pdf, http://id3.org/id3v2.3.0
  61 + */
  62 + virtual int write_header();
  63 + /**
  64 + * write audio/video packet.
  65 + * @remark assert data is not NULL.
  66 + */
  67 + virtual int write_audio(int64_t timestamp, char* data, int size);
  68 +};
  69 +
  70 +#endif
  71 +