Repository navigation
Expand file tree
/
Copy pathsrs_kernel_mp3.cpp
More file actions
130 lines (104 loc) · 3.72 KB
/
Copy pathsrs_kernel_mp3.cpp
File metadata and controls
130 lines (104 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* The MIT License (MIT)
*
* Copyright (c) 2013-2017 OSSRS(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 <srs_kernel_mp3.hpp>
#if !defined(SRS_EXPORT_LIBRTMP)
// for srs-librtmp, @see https://github.com/ossrs/srs/issues/213
#ifndef _WIN32
#include <unistd.h>
#endif
#include <fcntl.h>
#include <sstream>
using namespace std;
#include <srs_kernel_log.hpp>
#include <srs_kernel_error.hpp>
#include <srs_kernel_buffer.hpp>
#include <srs_kernel_file.hpp>
#include <srs_kernel_codec.hpp>
SrsMp3Transmuxer::SrsMp3Transmuxer()
{
writer = NULL;
tag_stream = new SrsBuffer();
}
SrsMp3Transmuxer::~SrsMp3Transmuxer()
{
srs_freep(tag_stream);
}
int SrsMp3Transmuxer::initialize(SrsFileWriter* fw)
{
int ret = ERROR_SUCCESS;
srs_assert(fw);
if (!fw->is_open()) {
ret = ERROR_KERNEL_MP3_STREAM_CLOSED;
srs_warn("stream is not open for encoder. ret=%d", ret);
return ret;
}
writer = fw;
return ret;
}
int SrsMp3Transmuxer::write_header()
{
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 writer->write(id3, sizeof(id3), NULL);
}
int SrsMp3Transmuxer::write_audio(int64_t timestamp, char* data, int size)
{
int ret = ERROR_SUCCESS;
srs_assert(data);
timestamp &= 0x7fffffff;
SrsBuffer* 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();
//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 ((SrsAudioCodecId)sound_format != SrsAudioCodecIdMP3) {
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 writer->write(data + stream->pos(), size - stream->pos(), NULL);
}
#endif