1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/src/app/srs_app_dash.hpp

171 lines
5.3 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.
*/
2017-02-11 04:30:21 +00:00
#ifndef SRS_APP_DASH_HPP
#define SRS_APP_DASH_HPP
#include <srs_core.hpp>
2017-02-26 12:40:33 +00:00
#include <string>
#include <vector>
#include <srs_app_fragment.hpp>
2017-02-26 12:40:33 +00:00
2017-02-11 13:14:28 +00:00
class SrsRequest;
class SrsOriginHub;
class SrsSharedPtrMessage;
class SrsFormat;
class SrsFileWriter;
2017-03-19 04:30:02 +00:00
class SrsMpdWriter;
2017-06-04 07:10:35 +00:00
class SrsMp4M2tsInitEncoder;
class SrsMp4M2tsSegmentEncoder;
2019-04-30 00:24:52 +00:00
// The init mp4 for FMP4.
class SrsInitMp4 : public SrsFragment
{
private:
SrsFileWriter* fw;
2017-06-04 07:10:35 +00:00
SrsMp4M2tsInitEncoder* init;
public:
SrsInitMp4();
virtual ~SrsInitMp4();
public:
// Write the init mp4 file, with the tid(track id).
2018-01-01 11:39:57 +00:00
virtual srs_error_t write(SrsFormat* format, bool video, int tid);
};
2019-04-30 00:24:52 +00:00
// The FMP4(Fragmented MP4) for DASH streaming.
class SrsFragmentedMp4 : public SrsFragment
{
2017-03-19 04:30:02 +00:00
private:
SrsFileWriter* fw;
2017-06-04 07:10:35 +00:00
SrsMp4M2tsSegmentEncoder* enc;
public:
2017-02-19 14:03:51 +00:00
SrsFragmentedMp4();
virtual ~SrsFragmentedMp4();
2017-03-19 04:30:02 +00:00
public:
// Initialize the fragment, create the home dir, open the file.
2018-01-01 11:39:57 +00:00
virtual srs_error_t initialize(SrsRequest* r, bool video, SrsMpdWriter* mpd, uint32_t tid);
2017-03-19 04:30:02 +00:00
// Write media message to fragment.
2018-01-01 11:39:57 +00:00
virtual srs_error_t write(SrsSharedPtrMessage* shared_msg, SrsFormat* format);
2017-03-19 04:30:02 +00:00
// Reap the fragment, close the fd and rename tmp to official file.
2018-01-01 11:39:57 +00:00
virtual srs_error_t reap(uint64_t& dts);
2017-02-19 14:03:51 +00:00
};
2019-04-30 00:24:52 +00:00
// The writer to write MPD for DASH.
2017-02-19 14:03:51 +00:00
class SrsMpdWriter
{
2017-02-26 12:40:33 +00:00
private:
SrsRequest* req;
srs_utime_t last_update_mpd;
2017-02-26 12:40:33 +00:00
private:
// The duration of fragment in srs_utime_t.
2019-04-08 01:02:39 +00:00
srs_utime_t fragment;
// The period to update the mpd in srs_utime_t.
srs_utime_t update_period;
2019-04-08 01:20:02 +00:00
// The timeshift buffer depth in srs_utime_t.
srs_utime_t timeshit;
2017-02-26 12:40:33 +00:00
// The base or home dir for dash to write files.
std::string home;
// The MPD path template, from which to build the file path.
std::string mpd_file;
2017-03-19 04:30:02 +00:00
private:
// The home for fragment, relative to home.
std::string fragment_home;
2017-02-19 14:03:51 +00:00
public:
SrsMpdWriter();
virtual ~SrsMpdWriter();
2017-02-26 12:40:33 +00:00
public:
2017-06-11 01:40:07 +00:00
virtual srs_error_t initialize(SrsRequest* r);
virtual srs_error_t on_publish();
virtual void on_unpublish();
2017-02-26 12:40:33 +00:00
// Write MPD according to parsed format of stream.
2018-01-01 11:39:57 +00:00
virtual srs_error_t write(SrsFormat* format);
2017-03-19 04:30:02 +00:00
public:
2017-03-19 04:49:58 +00:00
// Get the fragment relative home and filename.
// The basetime is the absolute time in srs_utime_t, while the sn(sequence number) is basetime/fragment.
virtual srs_error_t get_fragment(bool video, std::string& home, std::string& filename, int64_t& sn, srs_utime_t& basetime);
2017-02-19 14:03:51 +00:00
};
2019-04-30 00:24:52 +00:00
// The controller for DASH, control the MPD and FMP4 generating system.
2017-02-19 14:03:51 +00:00
class SrsDashController
{
2017-02-26 12:40:33 +00:00
private:
2017-03-05 10:44:37 +00:00
SrsRequest* req;
2017-02-26 12:40:33 +00:00
SrsMpdWriter* mpd;
2017-03-19 04:30:02 +00:00
private:
SrsFragmentedMp4* vcurrent;
SrsFragmentWindow* vfragments;
SrsFragmentedMp4* acurrent;
SrsFragmentWindow* afragments;
2017-06-04 07:10:35 +00:00
uint64_t audio_dts;
uint64_t video_dts;
2017-03-19 04:30:02 +00:00
private:
// The fragment duration in srs_utime_t to reap it.
2019-04-08 01:02:39 +00:00
srs_utime_t fragment;
2017-03-05 10:44:37 +00:00
private:
std::string home;
int video_tack_id;
int audio_track_id;
2017-02-19 14:03:51 +00:00
public:
SrsDashController();
virtual ~SrsDashController();
2017-02-26 12:40:33 +00:00
public:
2017-06-11 01:40:07 +00:00
virtual srs_error_t initialize(SrsRequest* r);
virtual srs_error_t on_publish();
virtual void on_unpublish();
2018-01-01 11:39:57 +00:00
virtual srs_error_t on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format);
virtual srs_error_t on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format);
2017-02-26 12:40:33 +00:00
private:
2018-01-01 11:39:57 +00:00
virtual srs_error_t refresh_mpd(SrsFormat* format);
virtual srs_error_t refresh_init_mp4(SrsSharedPtrMessage* msg, SrsFormat* format);
};
2017-02-11 13:14:28 +00:00
2019-04-30 00:24:52 +00:00
// The MPEG-DASH encoder, transmux RTMP to DASH.
2017-02-19 14:03:51 +00:00
class SrsDash
2017-02-11 13:14:28 +00:00
{
private:
bool enabled;
private:
SrsRequest* req;
SrsOriginHub* hub;
2017-02-19 14:03:51 +00:00
SrsDashController* controller;
2017-02-11 13:14:28 +00:00
public:
2017-02-19 14:03:51 +00:00
SrsDash();
virtual ~SrsDash();
2017-02-11 13:14:28 +00:00
public:
// Initalize the encoder.
2017-06-11 01:40:07 +00:00
virtual srs_error_t initialize(SrsOriginHub* h, SrsRequest* r);
2017-02-11 13:14:28 +00:00
// When stream start publishing.
2018-01-01 11:39:57 +00:00
virtual srs_error_t on_publish();
2017-02-11 13:14:28 +00:00
// When got an shared audio message.
2018-01-01 11:39:57 +00:00
virtual srs_error_t on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format);
2017-02-11 13:14:28 +00:00
// When got an shared video message.
2018-01-01 11:39:57 +00:00
virtual srs_error_t on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format);
2017-02-11 13:14:28 +00:00
// When stream stop publishing.
virtual void on_unpublish();
};
2017-02-11 04:30:21 +00:00
#endif
2017-02-19 14:03:51 +00:00