mirror of https://github.com/ossrs/srs.git
Merge branch 'develop' into 4.0release
commit
96565acd0f
@ -1,370 +0,0 @@
|
||||
/**
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2013-2020 John
|
||||
*
|
||||
* 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_app_rtc.hpp>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <unistd.h>
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
using namespace std;
|
||||
|
||||
#include <srs_kernel_buffer.hpp>
|
||||
#include <srs_kernel_error.hpp>
|
||||
#include <srs_kernel_codec.hpp>
|
||||
#include <srs_kernel_flv.hpp>
|
||||
#include <srs_kernel_rtc_rtp.hpp>
|
||||
#include <srs_app_config.hpp>
|
||||
#include <srs_app_source.hpp>
|
||||
#include <srs_core_autofree.hpp>
|
||||
#include <srs_app_pithy_print.hpp>
|
||||
#include <srs_kernel_utility.hpp>
|
||||
#include <srs_kernel_codec.hpp>
|
||||
#include <srs_kernel_file.hpp>
|
||||
#include <srs_app_utility.hpp>
|
||||
#include <srs_app_http_hooks.hpp>
|
||||
#include <srs_protocol_format.hpp>
|
||||
#include <srs_rtmp_stack.hpp>
|
||||
#include <openssl/rand.h>
|
||||
#include <srs_app_rtc_codec.hpp>
|
||||
|
||||
// TODO: Add this function into SrsRtpMux class.
|
||||
srs_error_t aac_raw_append_adts_header(SrsSharedPtrMessage* shared_audio, SrsFormat* format, char** pbuf, int* pnn_buf)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
if (format->is_aac_sequence_header()) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (format->audio->nb_samples != 1) {
|
||||
return srs_error_new(ERROR_RTC_RTP_MUXER, "adts");
|
||||
}
|
||||
|
||||
int nb_buf = format->audio->samples[0].size + 7;
|
||||
char* buf = new char[nb_buf];
|
||||
SrsBuffer stream(buf, nb_buf);
|
||||
|
||||
// TODO: Add comment.
|
||||
stream.write_1bytes(0xFF);
|
||||
stream.write_1bytes(0xF9);
|
||||
stream.write_1bytes(((format->acodec->aac_object - 1) << 6) | ((format->acodec->aac_sample_rate & 0x0F) << 2) | ((format->acodec->aac_channels & 0x04) >> 2));
|
||||
stream.write_1bytes(((format->acodec->aac_channels & 0x03) << 6) | ((nb_buf >> 11) & 0x03));
|
||||
stream.write_1bytes((nb_buf >> 3) & 0xFF);
|
||||
stream.write_1bytes(((nb_buf & 0x07) << 5) | 0x1F);
|
||||
stream.write_1bytes(0xFC);
|
||||
|
||||
stream.write_bytes(format->audio->samples[0].bytes, format->audio->samples[0].size);
|
||||
|
||||
*pbuf = buf;
|
||||
*pnn_buf = nb_buf;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
SrsRtpH264Muxer::SrsRtpH264Muxer()
|
||||
{
|
||||
discard_bframe = false;
|
||||
}
|
||||
|
||||
SrsRtpH264Muxer::~SrsRtpH264Muxer()
|
||||
{
|
||||
}
|
||||
|
||||
srs_error_t SrsRtpH264Muxer::filter(SrsSharedPtrMessage* shared_frame, SrsFormat* format)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
// If IDR, we will insert SPS/PPS before IDR frame.
|
||||
if (format->video && format->video->has_idr) {
|
||||
shared_frame->set_has_idr(true);
|
||||
}
|
||||
|
||||
// Update samples to shared frame.
|
||||
for (int i = 0; i < format->video->nb_samples; ++i) {
|
||||
SrsSample* sample = &format->video->samples[i];
|
||||
|
||||
// Because RTC does not support B-frame, so we will drop them.
|
||||
// TODO: Drop B-frame in better way, which not cause picture corruption.
|
||||
if (discard_bframe) {
|
||||
if ((err = sample->parse_bframe()) != srs_success) {
|
||||
return srs_error_wrap(err, "parse bframe");
|
||||
}
|
||||
if (sample->bframe) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (format->video->nb_samples <= 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
shared_frame->set_samples(format->video->samples, format->video->nb_samples);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
SrsRtpOpusMuxer::SrsRtpOpusMuxer()
|
||||
{
|
||||
codec = NULL;
|
||||
}
|
||||
|
||||
SrsRtpOpusMuxer::~SrsRtpOpusMuxer()
|
||||
{
|
||||
srs_freep(codec);
|
||||
}
|
||||
|
||||
srs_error_t SrsRtpOpusMuxer::initialize()
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
codec = new SrsAudioRecode(kChannel, kSamplerate);
|
||||
if (!codec) {
|
||||
return srs_error_new(ERROR_RTC_RTP_MUXER, "SrsAacOpus init failed");
|
||||
}
|
||||
|
||||
if ((err = codec->initialize()) != srs_success) {
|
||||
return srs_error_wrap(err, "init codec");
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
// An AAC packet may be transcoded to many OPUS packets.
|
||||
const int kMaxOpusPackets = 8;
|
||||
// The max size for each OPUS packet.
|
||||
const int kMaxOpusPacketSize = 4096;
|
||||
|
||||
srs_error_t SrsRtpOpusMuxer::transcode(SrsSharedPtrMessage* shared_audio, char* adts_audio, int nn_adts_audio)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
// Opus packet cache.
|
||||
static char* opus_payloads[kMaxOpusPackets];
|
||||
|
||||
static bool initialized = false;
|
||||
if (!initialized) {
|
||||
initialized = true;
|
||||
|
||||
static char opus_packets_cache[kMaxOpusPackets][kMaxOpusPacketSize];
|
||||
opus_payloads[0] = &opus_packets_cache[0][0];
|
||||
for (int i = 1; i < kMaxOpusPackets; i++) {
|
||||
opus_payloads[i] = opus_packets_cache[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Transcode an aac packet to many opus packets.
|
||||
SrsSample aac;
|
||||
aac.bytes = adts_audio;
|
||||
aac.size = nn_adts_audio;
|
||||
|
||||
int nn_opus_packets = 0;
|
||||
int opus_sizes[kMaxOpusPackets];
|
||||
if ((err = codec->recode(&aac, opus_payloads, opus_sizes, nn_opus_packets)) != srs_success) {
|
||||
return srs_error_wrap(err, "recode error");
|
||||
}
|
||||
|
||||
// Save OPUS packets in shared message.
|
||||
if (nn_opus_packets <= 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
int nn_max_extra_payload = 0;
|
||||
SrsSample samples[nn_opus_packets];
|
||||
for (int i = 0; i < nn_opus_packets; i++) {
|
||||
SrsSample* p = samples + i;
|
||||
p->size = opus_sizes[i];
|
||||
p->bytes = new char[p->size];
|
||||
memcpy(p->bytes, opus_payloads[i], p->size);
|
||||
|
||||
nn_max_extra_payload = srs_max(nn_max_extra_payload, p->size);
|
||||
}
|
||||
|
||||
shared_audio->set_extra_payloads(samples, nn_opus_packets);
|
||||
shared_audio->set_max_extra_payload(nn_max_extra_payload);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
SrsRtc::SrsRtc()
|
||||
{
|
||||
req = NULL;
|
||||
hub = NULL;
|
||||
|
||||
enabled = false;
|
||||
disposable = false;
|
||||
last_update_time = 0;
|
||||
|
||||
discard_aac = false;
|
||||
}
|
||||
|
||||
SrsRtc::~SrsRtc()
|
||||
{
|
||||
srs_freep(rtp_h264_muxer);
|
||||
}
|
||||
|
||||
void SrsRtc::dispose()
|
||||
{
|
||||
if (enabled) {
|
||||
on_unpublish();
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: FIXME: Dead code?
|
||||
srs_error_t SrsRtc::cycle()
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
srs_error_t SrsRtc::initialize(SrsOriginHub* h, SrsRequest* r)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
hub = h;
|
||||
req = r;
|
||||
|
||||
rtp_h264_muxer = new SrsRtpH264Muxer();
|
||||
rtp_h264_muxer->discard_bframe = _srs_config->get_rtc_bframe_discard(req->vhost);
|
||||
// TODO: FIXME: Support reload and log it.
|
||||
discard_aac = _srs_config->get_rtc_aac_discard(req->vhost);
|
||||
|
||||
rtp_opus_muxer = new SrsRtpOpusMuxer();
|
||||
if (!rtp_opus_muxer) {
|
||||
return srs_error_wrap(err, "rtp_opus_muxer nullptr");
|
||||
}
|
||||
|
||||
return rtp_opus_muxer->initialize();
|
||||
}
|
||||
|
||||
srs_error_t SrsRtc::on_publish()
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
// update the hls time, for hls_dispose.
|
||||
last_update_time = srs_get_system_time();
|
||||
|
||||
// support multiple publish.
|
||||
if (enabled) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (!_srs_config->get_rtc_enabled(req->vhost)) {
|
||||
return err;
|
||||
}
|
||||
|
||||
// if enabled, open the muxer.
|
||||
enabled = true;
|
||||
|
||||
// ok, the hls can be dispose, or need to be dispose.
|
||||
disposable = true;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
void SrsRtc::on_unpublish()
|
||||
{
|
||||
// support multiple unpublish.
|
||||
if (!enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
srs_error_t SrsRtc::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
if (!enabled) {
|
||||
return err;
|
||||
}
|
||||
|
||||
// Ignore if no format->acodec, it means the codec is not parsed, or unknown codec.
|
||||
// @issue https://github.com/ossrs/srs/issues/1506#issuecomment-562079474
|
||||
if (!format->acodec) {
|
||||
return err;
|
||||
}
|
||||
|
||||
// update the hls time, for hls_dispose.
|
||||
last_update_time = srs_get_system_time();
|
||||
|
||||
// ts support audio codec: aac/mp3
|
||||
SrsAudioCodecId acodec = format->acodec->id;
|
||||
if (acodec != SrsAudioCodecIdAAC && acodec != SrsAudioCodecIdMP3) {
|
||||
return err;
|
||||
}
|
||||
|
||||
// When drop aac audio packet, never transcode.
|
||||
if (discard_aac && acodec == SrsAudioCodecIdAAC) {
|
||||
return err;
|
||||
}
|
||||
|
||||
// ignore sequence header
|
||||
srs_assert(format->audio);
|
||||
|
||||
char* adts_audio = NULL;
|
||||
int nn_adts_audio = 0;
|
||||
// TODO: FIXME: Reserve 7 bytes header when create shared message.
|
||||
if ((err = aac_raw_append_adts_header(shared_audio, format, &adts_audio, &nn_adts_audio)) != srs_success) {
|
||||
return srs_error_wrap(err, "aac append header");
|
||||
}
|
||||
|
||||
if (adts_audio) {
|
||||
err = rtp_opus_muxer->transcode(shared_audio, adts_audio, nn_adts_audio);
|
||||
srs_freep(adts_audio);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
srs_error_t SrsRtc::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
// TODO: FIXME: Maybe it should config on vhost level.
|
||||
if (!enabled) {
|
||||
return err;
|
||||
}
|
||||
|
||||
// Ignore if no format->vcodec, it means the codec is not parsed, or unknown codec.
|
||||
// @issue https://github.com/ossrs/srs/issues/1506#issuecomment-562079474
|
||||
if (!format->vcodec) {
|
||||
return err;
|
||||
}
|
||||
|
||||
// update the hls time, for hls_dispose.
|
||||
last_update_time = srs_get_system_time();
|
||||
|
||||
// ignore info frame,
|
||||
// @see https://github.com/ossrs/srs/issues/288#issuecomment-69863909
|
||||
srs_assert(format->video);
|
||||
return rtp_h264_muxer->filter(shared_video, format);
|
||||
}
|
@ -1,105 +0,0 @@
|
||||
/**
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2013-2020 John
|
||||
*
|
||||
* 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_APP_RTC_HPP
|
||||
#define SRS_APP_RTC_HPP
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
class SrsFormat;
|
||||
class SrsSample;
|
||||
class SrsSharedPtrMessage;
|
||||
class SrsRequest;
|
||||
class SrsOriginHub;
|
||||
class SrsAudioRecode;
|
||||
class SrsBuffer;
|
||||
|
||||
// The RTP packet max size, should never exceed this size.
|
||||
const int kRtpPacketSize = 1500;
|
||||
|
||||
// Payload type will rewrite in srs_app_rtc_conn.cpp when send to client.
|
||||
const uint8_t kOpusPayloadType = 111;
|
||||
const uint8_t kH264PayloadType = 102;
|
||||
|
||||
const int kChannel = 2;
|
||||
const int kSamplerate = 48000;
|
||||
|
||||
// SSRC will rewrite in srs_app_rtc_conn.cpp when send to client.
|
||||
const uint32_t kAudioSSRC = 1;
|
||||
const uint32_t kVideoSSRC = 2;
|
||||
|
||||
// TODO: Define interface class like ISrsRtpMuxer
|
||||
class SrsRtpH264Muxer
|
||||
{
|
||||
public:
|
||||
bool discard_bframe;
|
||||
public:
|
||||
SrsRtpH264Muxer();
|
||||
virtual ~SrsRtpH264Muxer();
|
||||
public:
|
||||
srs_error_t filter(SrsSharedPtrMessage* shared_video, SrsFormat* format);
|
||||
};
|
||||
|
||||
// TODO: FIXME: It's not a muxer, but a transcoder.
|
||||
class SrsRtpOpusMuxer
|
||||
{
|
||||
private:
|
||||
SrsAudioRecode* codec;
|
||||
public:
|
||||
SrsRtpOpusMuxer();
|
||||
virtual ~SrsRtpOpusMuxer();
|
||||
virtual srs_error_t initialize();
|
||||
public:
|
||||
srs_error_t transcode(SrsSharedPtrMessage* shared_audio, char* adts_audio, int nn_adts_audio);
|
||||
};
|
||||
|
||||
class SrsRtc
|
||||
{
|
||||
private:
|
||||
SrsRequest* req;
|
||||
bool enabled;
|
||||
bool disposable;
|
||||
bool discard_aac;
|
||||
srs_utime_t last_update_time;
|
||||
SrsRtpH264Muxer* rtp_h264_muxer;
|
||||
SrsRtpOpusMuxer* rtp_opus_muxer;
|
||||
SrsOriginHub* hub;
|
||||
public:
|
||||
SrsRtc();
|
||||
virtual ~SrsRtc();
|
||||
public:
|
||||
virtual void dispose();
|
||||
virtual srs_error_t cycle();
|
||||
public:
|
||||
virtual srs_error_t initialize(SrsOriginHub* h, SrsRequest* r);
|
||||
virtual srs_error_t on_publish();
|
||||
virtual void on_unpublish();
|
||||
virtual srs_error_t on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format);
|
||||
virtual srs_error_t on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format);
|
||||
};
|
||||
|
||||
#endif
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,201 @@
|
||||
/**
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2013-2020 John
|
||||
*
|
||||
* 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_APP_RTC_SOURCE_HPP
|
||||
#define SRS_APP_RTC_SOURCE_HPP
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
#include <srs_service_st.hpp>
|
||||
#include <srs_app_source.hpp>
|
||||
|
||||
class SrsRequest;
|
||||
class SrsConnection;
|
||||
class SrsMetaCache;
|
||||
class SrsRtcPublisher;
|
||||
class SrsSharedPtrMessage;
|
||||
class SrsCommonMessage;
|
||||
class SrsMessageArray;
|
||||
class SrsRtcSource;
|
||||
class SrsRtcFromRtmpBridger;
|
||||
class SrsAudioRecode;
|
||||
class SrsRtpPacket2;
|
||||
class SrsSample;
|
||||
|
||||
class SrsRtcConsumer : public ISrsConsumerQueue
|
||||
{
|
||||
private:
|
||||
SrsRtcSource* source;
|
||||
std::vector<SrsRtpPacket2*> queue;
|
||||
// when source id changed, notice all consumers
|
||||
bool should_update_source_id;
|
||||
// The cond wait for mw.
|
||||
// @see https://github.com/ossrs/srs/issues/251
|
||||
srs_cond_t mw_wait;
|
||||
bool mw_waiting;
|
||||
int mw_min_msgs;
|
||||
public:
|
||||
SrsRtcConsumer(SrsRtcSource* s);
|
||||
virtual ~SrsRtcConsumer();
|
||||
public:
|
||||
// When source id changed, notice client to print.
|
||||
virtual void update_source_id();
|
||||
// Put or get RTP packet in queue.
|
||||
virtual srs_error_t enqueue(SrsSharedPtrMessage* shared_msg, bool atc, SrsRtmpJitterAlgorithm ag);
|
||||
srs_error_t enqueue2(SrsRtpPacket2* pkt);
|
||||
virtual srs_error_t dump_packets(std::vector<SrsRtpPacket2*>& pkts);
|
||||
// Wait for at-least some messages incoming in queue.
|
||||
virtual void wait(int nb_msgs);
|
||||
};
|
||||
|
||||
class SrsRtcSourceManager
|
||||
{
|
||||
private:
|
||||
srs_mutex_t lock;
|
||||
std::map<std::string, SrsRtcSource*> pool;
|
||||
public:
|
||||
SrsRtcSourceManager();
|
||||
virtual ~SrsRtcSourceManager();
|
||||
public:
|
||||
// create source when fetch from cache failed.
|
||||
// @param r the client request.
|
||||
// @param pps the matched source, if success never be NULL.
|
||||
virtual srs_error_t fetch_or_create(SrsRequest* r, SrsRtcSource** pps);
|
||||
private:
|
||||
// Get the exists source, NULL when not exists.
|
||||
// update the request and return the exists source.
|
||||
virtual SrsRtcSource* fetch(SrsRequest* r);
|
||||
};
|
||||
|
||||
// Global singleton instance.
|
||||
extern SrsRtcSourceManager* _srs_rtc_sources;
|
||||
|
||||
class SrsRtcSource
|
||||
{
|
||||
private:
|
||||
// For publish, it's the publish client id.
|
||||
// For edge, it's the edge ingest id.
|
||||
// when source id changed, for example, the edge reconnect,
|
||||
// invoke the on_source_id_changed() to let all clients know.
|
||||
int _source_id;
|
||||
// previous source id.
|
||||
int _pre_source_id;
|
||||
SrsRequest* req;
|
||||
SrsRtcPublisher* rtc_publisher_;
|
||||
// Transmux RTMP to RTC.
|
||||
SrsRtcFromRtmpBridger* bridger_;
|
||||
// The metadata cache.
|
||||
SrsMetaCache* meta;
|
||||
private:
|
||||
// To delivery stream to clients.
|
||||
std::vector<SrsRtcConsumer*> consumers;
|
||||
// Whether source is avaiable for publishing.
|
||||
bool _can_publish;
|
||||
public:
|
||||
SrsRtcSource();
|
||||
virtual ~SrsRtcSource();
|
||||
public:
|
||||
virtual srs_error_t initialize(SrsRequest* r);
|
||||
// Update the authentication information in request.
|
||||
virtual void update_auth(SrsRequest* r);
|
||||
// The source id changed.
|
||||
virtual srs_error_t on_source_id_changed(int id);
|
||||
// Get current source id.
|
||||
virtual int source_id();
|
||||
virtual int pre_source_id();
|
||||
// Get the bridger.
|
||||
ISrsSourceBridger* bridger();
|
||||
// For RTC, we need to package SPS/PPS(in cached meta) before each IDR.
|
||||
SrsMetaCache* cached_meta();
|
||||
public:
|
||||
// Create consumer
|
||||
// @param consumer, output the create consumer.
|
||||
virtual srs_error_t create_consumer(SrsRtcConsumer*& consumer);
|
||||
// Dumps packets in cache to consumer.
|
||||
// @param ds, whether dumps the sequence header.
|
||||
// @param dm, whether dumps the metadata.
|
||||
// @param dg, whether dumps the gop cache.
|
||||
virtual srs_error_t consumer_dumps(SrsRtcConsumer* consumer, bool ds = true, bool dm = true, bool dg = true);
|
||||
virtual void on_consumer_destroy(SrsRtcConsumer* consumer);
|
||||
// TODO: FIXME: Remove the param is_edge.
|
||||
virtual bool can_publish(bool is_edge);
|
||||
// When start publish stream.
|
||||
virtual srs_error_t on_publish();
|
||||
// When stop publish stream.
|
||||
virtual void on_unpublish();
|
||||
public:
|
||||
// Get and set the publisher, passed to consumer to process requests such as PLI.
|
||||
SrsRtcPublisher* rtc_publisher();
|
||||
void set_rtc_publisher(SrsRtcPublisher* v);
|
||||
srs_error_t on_rtp(SrsRtpPacket2* pkt);
|
||||
virtual srs_error_t on_audio_imp(SrsSharedPtrMessage* audio);
|
||||
// When got RTC audio message, which is encoded in opus.
|
||||
// TODO: FIXME: Merge with on_audio.
|
||||
virtual srs_error_t on_video(SrsCommonMessage* video);
|
||||
virtual srs_error_t on_video_imp(SrsSharedPtrMessage* video);
|
||||
private:
|
||||
// The format, codec information.
|
||||
// TODO: FIXME: Remove it.
|
||||
SrsRtmpFormat* format;
|
||||
srs_error_t filter(SrsSharedPtrMessage* shared_video, SrsFormat* format);
|
||||
};
|
||||
|
||||
class SrsRtcFromRtmpBridger : public ISrsSourceBridger
|
||||
{
|
||||
private:
|
||||
SrsRequest* req;
|
||||
SrsRtcSource* source_;
|
||||
// The format, codec information.
|
||||
SrsRtmpFormat* format;
|
||||
private:
|
||||
bool discard_aac;
|
||||
SrsAudioRecode* codec;
|
||||
bool discard_bframe;
|
||||
bool merge_nalus;
|
||||
public:
|
||||
SrsRtcFromRtmpBridger(SrsRtcSource* source);
|
||||
virtual ~SrsRtcFromRtmpBridger();
|
||||
public:
|
||||
virtual srs_error_t initialize(SrsRequest* r);
|
||||
virtual srs_error_t on_publish();
|
||||
virtual void on_unpublish();
|
||||
virtual srs_error_t on_audio(SrsSharedPtrMessage* msg);
|
||||
private:
|
||||
srs_error_t transcode(char* adts_audio, int nn_adts_audio);
|
||||
srs_error_t package_opus(char* data, int size, SrsRtpPacket2** ppkt);
|
||||
public:
|
||||
virtual srs_error_t on_video(SrsSharedPtrMessage* msg);
|
||||
private:
|
||||
srs_error_t filter(SrsSharedPtrMessage* msg, SrsFormat* format);
|
||||
srs_error_t package_stap_a(SrsRtcSource* source, SrsSharedPtrMessage* msg, SrsRtpPacket2** ppkt);
|
||||
srs_error_t package_nalus(SrsSharedPtrMessage* msg, std::vector<SrsRtpPacket2*>& pkts);
|
||||
srs_error_t package_single_nalu(SrsSharedPtrMessage* msg, SrsSample* sample, std::vector<SrsRtpPacket2*>& pkts);
|
||||
srs_error_t package_fu_a(SrsSharedPtrMessage* msg, SrsSample* sample, int fu_payload_size, std::vector<SrsRtpPacket2*>& pkts);
|
||||
srs_error_t consume_packets(std::vector<SrsRtpPacket2*>& pkts);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue