1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

SquashSRS4: Support RTC2RTMP.

This commit is contained in:
winlin 2021-05-01 22:15:57 +08:00
parent 0b62216999
commit 74bb47c13f
22 changed files with 1246 additions and 844 deletions

View file

@ -26,6 +26,8 @@
#include <srs_core.hpp>
#include <srs_kernel_codec.hpp>
#include <string>
#ifdef __cplusplus
@ -39,98 +41,59 @@ extern "C" {
#include <libavutil/channel_layout.h>
#include <libavutil/samplefmt.h>
#include <libswresample/swresample.h>
#include <libavutil/audio_fifo.h>
#ifdef __cplusplus
}
#endif
class SrsSample;
class SrsAudioDecoder
class SrsAudioTranscoder
{
private:
AVFrame* frame_;
AVPacket* packet_;
AVCodecContext* codec_ctx_;
SrsAudioCodecId codec_id_;
public:
//Only support "aac","opus"
SrsAudioDecoder(SrsAudioCodecId codec);
virtual ~SrsAudioDecoder();
srs_error_t initialize();
virtual srs_error_t decode(SrsSample *pkt, char *buf, int &size);
AVCodecContext* codec_ctx();
};
AVCodecContext *dec_;
AVFrame *dec_frame_;
AVPacket *dec_packet_;
class SrsAudioEncoder
{
AVCodecContext *enc_;
AVFrame *enc_frame_;
AVPacket *enc_packet_;
SwrContext *swr_;
//buffer for swr out put
uint8_t **swr_data_;
AVAudioFifo *fifo_;
int64_t new_pkt_pts_;
int64_t next_out_pts_;
public:
SrsAudioTranscoder();
virtual ~SrsAudioTranscoder();
public:
// Initialize the transcoder, transcode from codec as to codec.
// The channels specifies the number of output channels for encoder, for example, 2.
// The sample_rate specifies the sample rate of encoder, for example, 48000.
// The bit_rate specifies the bitrate of encoder, for example, 48000.
srs_error_t initialize(SrsAudioCodecId from, SrsAudioCodecId to, int channels, int sample_rate, int bit_rate);
// Transcode the input audio frame in, as output audio frames outs.
virtual srs_error_t transcode(SrsAudioFrame* in, std::vector<SrsAudioFrame*>& outs);
// Free the generated audio frames by transcode.
void free_frames(std::vector<SrsAudioFrame*>& frames);
public:
// Get the aac codec header, for example, FLV sequence header.
// @remark User should never free the data, it's managed by this transcoder.
void aac_codec_header(uint8_t** data, int* len);
private:
int channels_;
int sampling_rate_;
AVCodecContext* codec_ctx_;
SrsAudioCodecId codec_id_;
int want_bytes_;
AVFrame* frame_;
public:
//Only support "aac","opus"
SrsAudioEncoder(SrsAudioCodecId codec, int samplerate, int channelsy);
virtual ~SrsAudioEncoder();
srs_error_t initialize();
//The encoder wanted bytes to call encode, if > 0, caller must feed the same bytes
//Call after initialize successed
int want_bytes();
virtual srs_error_t encode(SrsSample *frame, char *buf, int &size);
AVCodecContext* codec_ctx();
};
srs_error_t init_dec(SrsAudioCodecId from);
srs_error_t init_enc(SrsAudioCodecId to, int channels, int samplerate, int bit_rate);
srs_error_t init_swr(AVCodecContext* decoder);
srs_error_t init_fifo();
class SrsAudioResample
{
private:
int src_rate_;
int src_ch_layout_;
int src_nb_channels_;
enum AVSampleFormat src_sample_fmt_;
int src_linesize_;
int src_nb_samples_;
uint8_t **src_data_;
srs_error_t decode_and_resample(SrsAudioFrame* pkt);
srs_error_t encode(std::vector<SrsAudioFrame*> &pkts);
int dst_rate_;
int dst_ch_layout_;
int dst_nb_channels_;
enum AVSampleFormat dst_sample_fmt_;
int dst_linesize_;
int dst_nb_samples_;
uint8_t **dst_data_;
int max_dst_nb_samples_;
struct SwrContext *swr_ctx_;
public:
SrsAudioResample(int src_rate, int src_layout, enum AVSampleFormat src_fmt,
int src_nb, int dst_rate, int dst_layout, enum AVSampleFormat dst_fmt);
virtual ~SrsAudioResample();
srs_error_t initialize();
virtual srs_error_t resample(SrsSample *pcm, char *buf, int &size);
};
// TODO: FIXME: Rename to Transcoder.
class SrsAudioRecode
{
private:
SrsAudioDecoder *dec_;
SrsAudioEncoder *enc_;
SrsAudioResample *resample_;
int dst_channels_;
int dst_samplerate_;
int size_;
char *data_;
SrsAudioCodecId src_codec_;
SrsAudioCodecId dst_codec_;
int enc_want_bytes_;
public:
SrsAudioRecode(SrsAudioCodecId src_codec, SrsAudioCodecId dst_codec,int channels, int samplerate);
virtual ~SrsAudioRecode();
srs_error_t initialize();
virtual srs_error_t transcode(SrsSample *pkt, char **buf, int *buf_len, int &n);
srs_error_t add_samples_to_fifo(uint8_t** samples, int frame_size);
void free_swr_samples();
};
#endif /* SRS_APP_AUDIO_RECODE_HPP */