From e6549b261c63858e984be7354c57fd3cab6b4854 Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 19 Jan 2015 10:18:18 +0800 Subject: [PATCH] for #293, support http mp3 streaming --- trunk/src/kernel/srs_kernel_mp3.cpp | 127 ++++++++++++++++++++++++++++ trunk/src/kernel/srs_kernel_mp3.hpp | 71 ++++++++++++++++ 2 files changed, 198 insertions(+) create mode 100644 trunk/src/kernel/srs_kernel_mp3.cpp create mode 100644 trunk/src/kernel/srs_kernel_mp3.hpp diff --git a/trunk/src/kernel/srs_kernel_mp3.cpp b/trunk/src/kernel/srs_kernel_mp3.cpp new file mode 100644 index 0000000000..b23b96185a --- /dev/null +++ b/trunk/src/kernel/srs_kernel_mp3.cpp @@ -0,0 +1,127 @@ +/* +The MIT License (MIT) + +Copyright (c) 2013-2015 winlin + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include + +// for srs-librtmp, @see https://github.com/winlinvip/simple-rtmp-server/issues/213 +#ifndef _WIN32 +#include +#endif + +#include +#include +using namespace std; + +#include +#include +#include +#include +#include + +SrsMp3Encoder::SrsMp3Encoder() +{ + _fs = NULL; + tag_stream = new SrsStream(); +} + +SrsMp3Encoder::~SrsMp3Encoder() +{ + srs_freep(tag_stream); +} + +int SrsMp3Encoder::initialize(SrsFileWriter* fs) +{ + int ret = ERROR_SUCCESS; + + srs_assert(fs); + + if (!fs->is_open()) { + ret = ERROR_KERNEL_MP3_STREAM_CLOSED; + srs_warn("stream is not open for encoder. ret=%d", ret); + return ret; + } + + _fs = fs; + + return ret; +} + +int SrsMp3Encoder::write_header() +{ + static char id3[] = { + (char)0x49, (char)0x44, (char)0x33, // ID3 + (char)0x03, (char)0x00, // version + (char)0x00, // flags + (char)0x00, (char)0x00, (char)0x00, (char)0x0a, // size + + (char)0x00, (char)0x00, (char)0x00, (char)0x00, // FrameID + (char)0x00, (char)0x00, (char)0x00, (char)0x00, // FrameSize + (char)0x00, (char)0x00 // Flags + }; + return _fs->write(id3, sizeof(id3), NULL); +} + +int SrsMp3Encoder::write_audio(int64_t timestamp, char* data, int size) +{ + int ret = ERROR_SUCCESS; + + srs_assert(data); + + timestamp &= 0x7fffffff; + + SrsStream* stream = tag_stream; + if ((ret = stream->initialize(data, size)) != ERROR_SUCCESS) { + return ret; + } + + // audio decode + if (!stream->require(1)) { + ret = ERROR_MP3_DECODE_ERROR; + srs_error("mp3 decode audio sound_format failed. ret=%d", ret); + return ret; + } + + // @see: E.4.2 Audio Tags, video_file_format_spec_v10_1.pdf, page 76 + int8_t sound_format = stream->read_1bytes(); + + // @see: SrsAvcAacCodec::audio_aac_demux + //int8_t sound_type = sound_format & 0x01; + //int8_t sound_size = (sound_format >> 1) & 0x01; + //int8_t sound_rate = (sound_format >> 2) & 0x03; + sound_format = (sound_format >> 4) & 0x0f; + + if ((SrsCodecAudio)sound_format != SrsCodecAudioMP3) { + ret = ERROR_MP3_DECODE_ERROR; + srs_error("mp3 required, format=%d. ret=%d", sound_format, ret); + return ret; + } + + if (!stream->require(1)) { + ret = ERROR_MP3_DECODE_ERROR; + srs_error("mp3 decode aac_packet_type failed. ret=%d", ret); + return ret; + } + + return _fs->write(data + stream->pos(), size - stream->pos(), NULL); +} + diff --git a/trunk/src/kernel/srs_kernel_mp3.hpp b/trunk/src/kernel/srs_kernel_mp3.hpp new file mode 100644 index 0000000000..20b1c7e7c4 --- /dev/null +++ b/trunk/src/kernel/srs_kernel_mp3.hpp @@ -0,0 +1,71 @@ +/* +The MIT License (MIT) + +Copyright (c) 2013-2015 winlin + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#ifndef SRS_KERNEL_MP3_HPP +#define SRS_KERNEL_MP3_HPP + +/* +#include +*/ +#include + +#include + +class SrsStream; +class SrsFileWriter; +class SrsFileReader; + +/** +* encode data to aac file. +*/ +class SrsMp3Encoder +{ +private: + SrsFileWriter* _fs; +private: + SrsStream* tag_stream; +public: + SrsMp3Encoder(); + virtual ~SrsMp3Encoder(); +public: + /** + * initialize the underlayer file stream. + * @remark user can initialize multiple times to encode multiple mp3 files. + * @remark, user must free the fs, mp3 encoder never close/free it. + */ + virtual int initialize(SrsFileWriter* fs); +public: + /** + * write mp3 id3 v2.3 header. + * @see mp3.id3v2.3.0.pdf, http://id3.org/id3v2.3.0 + */ + virtual int write_header(); + /** + * write audio/video packet. + * @remark assert data is not NULL. + */ + virtual int write_audio(int64_t timestamp, char* data, int size); +}; + +#endif +