2020-05-12 05:19:31 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2020-05-12 11:53:21 +00:00
|
|
|
class SrsRtcFromRtmpBridger;
|
2020-05-13 07:09:36 +00:00
|
|
|
class SrsAudioRecode;
|
2020-05-13 10:43:25 +00:00
|
|
|
class SrsRtpPacket2;
|
2020-05-12 05:19:31 +00:00
|
|
|
|
|
|
|
class SrsRtcConsumer : public ISrsConsumerQueue
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
SrsRtcSource* source;
|
2020-05-13 12:13:25 +00:00
|
|
|
std::vector<SrsRtpPacket2*> queue;
|
2020-05-12 05:19:31 +00:00
|
|
|
// 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:
|
2020-05-13 12:13:25 +00:00
|
|
|
SrsRtcConsumer(SrsRtcSource* s);
|
2020-05-12 05:19:31 +00:00
|
|
|
virtual ~SrsRtcConsumer();
|
|
|
|
public:
|
2020-05-13 12:13:25 +00:00
|
|
|
// When source id changed, notice client to print.
|
2020-05-12 05:19:31 +00:00
|
|
|
virtual void update_source_id();
|
2020-05-13 12:13:25 +00:00
|
|
|
// Put or get RTP packet in queue.
|
2020-05-12 05:19:31 +00:00
|
|
|
virtual srs_error_t enqueue(SrsSharedPtrMessage* shared_msg, bool atc, SrsRtmpJitterAlgorithm ag);
|
2020-05-13 12:13:25 +00:00
|
|
|
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);
|
2020-05-12 05:19:31 +00:00
|
|
|
};
|
|
|
|
|
2020-05-13 07:46:49 +00:00
|
|
|
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;
|
|
|
|
|
2020-05-12 05:19:31 +00:00
|
|
|
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_;
|
2020-05-12 11:53:21 +00:00
|
|
|
// Transmux RTMP to RTC.
|
|
|
|
SrsRtcFromRtmpBridger* bridger_;
|
2020-05-13 08:21:42 +00:00
|
|
|
// The metadata cache.
|
|
|
|
SrsMetaCache* meta;
|
2020-05-12 05:19:31 +00:00
|
|
|
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();
|
2020-05-12 11:53:21 +00:00
|
|
|
// Get the bridger.
|
|
|
|
ISrsSourceBridger* bridger();
|
2020-05-13 08:21:42 +00:00
|
|
|
// For RTC, we need to package SPS/PPS(in cached meta) before each IDR.
|
|
|
|
SrsMetaCache* cached_meta();
|
2020-05-12 05:19:31 +00:00
|
|
|
public:
|
|
|
|
// Create consumer
|
|
|
|
// @param consumer, output the create consumer.
|
2020-05-13 12:13:25 +00:00
|
|
|
virtual srs_error_t create_consumer(SrsRtcConsumer*& consumer);
|
2020-05-12 05:19:31 +00:00
|
|
|
// 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);
|
2020-05-13 10:43:25 +00:00
|
|
|
virtual srs_error_t on_audio_imp(SrsSharedPtrMessage* audio);
|
|
|
|
srs_error_t on_audio2(SrsRtpPacket2* pkt);
|
2020-05-12 05:19:31 +00:00
|
|
|
// 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);
|
2020-05-13 08:21:42 +00:00
|
|
|
private:
|
|
|
|
// The format, codec information.
|
|
|
|
// TODO: FIXME: Remove it.
|
|
|
|
SrsRtmpFormat* format;
|
|
|
|
srs_error_t filter(SrsSharedPtrMessage* shared_video, SrsFormat* format);
|
2020-05-12 05:19:31 +00:00
|
|
|
};
|
|
|
|
|
2020-05-12 11:53:21 +00:00
|
|
|
class SrsRtcFromRtmpBridger : public ISrsSourceBridger
|
|
|
|
{
|
|
|
|
private:
|
2020-05-13 05:35:14 +00:00
|
|
|
SrsRequest* req;
|
2020-05-12 11:53:21 +00:00
|
|
|
SrsRtcSource* source_;
|
2020-05-13 05:35:14 +00:00
|
|
|
// The format, codec information.
|
|
|
|
SrsRtmpFormat* format;
|
2020-05-13 07:09:36 +00:00
|
|
|
private:
|
|
|
|
bool discard_aac;
|
|
|
|
SrsAudioRecode* codec;
|
2020-05-13 07:15:57 +00:00
|
|
|
bool discard_bframe;
|
2020-05-12 11:53:21 +00:00
|
|
|
public:
|
|
|
|
SrsRtcFromRtmpBridger(SrsRtcSource* source);
|
|
|
|
virtual ~SrsRtcFromRtmpBridger();
|
|
|
|
public:
|
2020-05-13 05:35:14 +00:00
|
|
|
virtual srs_error_t initialize(SrsRequest* r);
|
2020-05-12 11:53:21 +00:00
|
|
|
virtual srs_error_t on_publish();
|
2020-05-13 05:42:55 +00:00
|
|
|
virtual void on_unpublish();
|
2020-05-12 11:53:21 +00:00
|
|
|
virtual srs_error_t on_audio(SrsSharedPtrMessage* audio);
|
2020-05-13 07:09:36 +00:00
|
|
|
private:
|
2020-05-13 10:43:25 +00:00
|
|
|
srs_error_t transcode(char* adts_audio, int nn_adts_audio);
|
2020-05-13 07:09:36 +00:00
|
|
|
public:
|
2020-05-12 11:53:21 +00:00
|
|
|
virtual srs_error_t on_video(SrsSharedPtrMessage* video);
|
2020-05-13 07:15:57 +00:00
|
|
|
private:
|
|
|
|
srs_error_t filter(SrsSharedPtrMessage* shared_video, SrsFormat* format);
|
2020-05-12 11:53:21 +00:00
|
|
|
};
|
|
|
|
|
2020-05-12 05:19:31 +00:00
|
|
|
#endif
|
|
|
|
|