1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-24 15:04:20 +00:00
srs/trunk/src/kernel/srs_kernel_mp3.cpp

131 lines
3.8 KiB
C++
Raw Normal View History

2017-03-25 09:21:39 +00:00
/**
* The MIT License (MIT)
*
2019-12-30 02:10:35 +00:00
* Copyright (c) 2013-2020 Winlin
2017-03-25 09:21:39 +00:00
*
* 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.
*/
2015-01-19 02:18:18 +00:00
#include <srs_kernel_mp3.hpp>
#if !defined(SRS_EXPORT_LIBRTMP)
2015-11-11 02:37:50 +00:00
// for srs-librtmp, @see https://github.com/ossrs/srs/issues/213
2015-01-19 02:18:18 +00:00
#ifndef _WIN32
#include <unistd.h>
#endif
#include <fcntl.h>
#include <sstream>
using namespace std;
#include <srs_kernel_log.hpp>
#include <srs_kernel_error.hpp>
2015-09-22 00:48:55 +00:00
#include <srs_kernel_buffer.hpp>
2015-01-19 02:18:18 +00:00
#include <srs_kernel_file.hpp>
#include <srs_kernel_codec.hpp>
#include <srs_core_autofree.hpp>
2015-01-19 02:18:18 +00:00
SrsMp3Transmuxer::SrsMp3Transmuxer()
2015-01-19 02:18:18 +00:00
{
2015-07-28 10:08:11 +00:00
writer = NULL;
2015-01-19 02:18:18 +00:00
}
SrsMp3Transmuxer::~SrsMp3Transmuxer()
2015-01-19 02:18:18 +00:00
{
}
srs_error_t SrsMp3Transmuxer::initialize(SrsFileWriter* fw)
2015-01-19 02:18:18 +00:00
{
srs_error_t err = srs_success;
2015-01-19 02:18:18 +00:00
2015-07-28 10:08:11 +00:00
srs_assert(fw);
2015-01-19 02:18:18 +00:00
2015-07-28 10:08:11 +00:00
if (!fw->is_open()) {
return srs_error_new(ERROR_KERNEL_MP3_STREAM_CLOSED, "stream is not open");
2015-01-19 02:18:18 +00:00
}
2015-07-28 10:08:11 +00:00
writer = fw;
2015-01-19 02:18:18 +00:00
return err;
2015-01-19 02:18:18 +00:00
}
srs_error_t SrsMp3Transmuxer::write_header()
2015-01-19 02:18:18 +00:00
{
srs_error_t err = srs_success;
char id3[] = {
2015-01-19 02:18:18 +00:00
(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
};
if ((err = writer->write(id3, sizeof(id3), NULL)) != srs_success) {
return srs_error_wrap(err, "write id3");
}
return err;
2015-01-19 02:18:18 +00:00
}
srs_error_t SrsMp3Transmuxer::write_audio(int64_t timestamp, char* data, int size)
2015-01-19 02:18:18 +00:00
{
srs_error_t err = srs_success;
2015-01-19 02:18:18 +00:00
srs_assert(data);
timestamp &= 0x7fffffff;
SrsBuffer* stream = new SrsBuffer(data, size);
SrsAutoFree(SrsBuffer, stream);
2017-03-25 09:21:39 +00:00
2015-01-19 02:18:18 +00:00
// audio decode
if (!stream->require(1)) {
return srs_error_new(ERROR_MP3_DECODE_ERROR, "decode sound_format");
2015-01-19 02:18:18 +00:00
}
// @see: E.4.2 Audio Tags, video_file_format_spec_v10_1.pdf, page 76
int8_t sound_format = stream->read_1bytes();
//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;
2017-02-12 12:38:39 +00:00
if ((SrsAudioCodecId)sound_format != SrsAudioCodecIdMP3) {
return srs_error_new(ERROR_MP3_DECODE_ERROR, "mp3 required, format=%d", sound_format);
2015-01-19 02:18:18 +00:00
}
2017-03-25 09:21:39 +00:00
2015-01-19 02:18:18 +00:00
if (!stream->require(1)) {
return srs_error_new(ERROR_MP3_DECODE_ERROR, "mp3 decode aac_packet_type failed");
}
if ((err = writer->write(data + stream->pos(), size - stream->pos(), NULL)) != srs_success) {
return srs_error_wrap(err, "write audio");
2015-01-19 02:18:18 +00:00
}
return err;
2015-01-19 02:18:18 +00:00
}
#endif