2014-04-15 06:01:57 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
|
|
|
Copyright (c) 2013-2014 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.
|
|
|
|
*/
|
|
|
|
|
2014-04-16 01:28:02 +00:00
|
|
|
#ifndef SRS_APP_DVR_HPP
|
|
|
|
#define SRS_APP_DVR_HPP
|
2014-04-15 06:01:57 +00:00
|
|
|
|
|
|
|
/*
|
2014-04-16 01:28:02 +00:00
|
|
|
#include <srs_app_dvr.hpp>
|
2014-04-15 06:01:57 +00:00
|
|
|
*/
|
|
|
|
#include <srs_core.hpp>
|
|
|
|
|
|
|
|
#ifdef SRS_AUTO_DVR
|
|
|
|
|
2014-04-15 06:24:03 +00:00
|
|
|
class SrsSource;
|
|
|
|
class SrsRequest;
|
2014-04-17 08:06:49 +00:00
|
|
|
class SrsStream;
|
|
|
|
class SrsOnMetaDataPacket;
|
2014-04-15 06:24:03 +00:00
|
|
|
class SrsSharedPtrMessage;
|
|
|
|
|
2014-04-17 08:06:49 +00:00
|
|
|
/**
|
|
|
|
* file stream to read/write file.
|
|
|
|
*/
|
|
|
|
class SrsFileStream
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::string _file;
|
|
|
|
int fd;
|
|
|
|
public:
|
|
|
|
SrsFileStream();
|
|
|
|
virtual ~SrsFileStream();
|
|
|
|
public:
|
|
|
|
virtual int open(std::string file);
|
|
|
|
virtual int close();
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @param pnread, return the read size. NULL to ignore.
|
|
|
|
*/
|
|
|
|
virtual int read(void* buf, size_t count, ssize_t* pnread);
|
|
|
|
/**
|
|
|
|
* @param pnwrite, return the write size. NULL to ignore.
|
|
|
|
*/
|
|
|
|
virtual int write(void* buf, size_t count, ssize_t* pnwrite);
|
|
|
|
};
|
|
|
|
|
2014-04-17 04:59:35 +00:00
|
|
|
/**
|
|
|
|
* encode data to flv file.
|
|
|
|
*/
|
|
|
|
class SrsFlvEncoder
|
|
|
|
{
|
2014-04-17 08:06:49 +00:00
|
|
|
private:
|
|
|
|
SrsFileStream* _fs;
|
|
|
|
private:
|
|
|
|
SrsStream* tag_stream;
|
|
|
|
public:
|
|
|
|
SrsFlvEncoder();
|
|
|
|
virtual ~SrsFlvEncoder();
|
|
|
|
public:
|
2014-04-17 08:16:17 +00:00
|
|
|
/**
|
|
|
|
* initialize the underlayer file stream,
|
|
|
|
* user can initialize multiple times to encode multiple flv files.
|
|
|
|
*/
|
2014-04-17 08:06:49 +00:00
|
|
|
virtual int initialize(SrsFileStream* fs);
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* write flv header.
|
|
|
|
* write following:
|
|
|
|
* 1. E.2 The FLV header
|
|
|
|
* 2. PreviousTagSize0 UI32 Always 0
|
|
|
|
* that is, 9+4=13bytes.
|
|
|
|
*/
|
|
|
|
virtual int write_header();
|
2014-04-17 08:16:17 +00:00
|
|
|
/**
|
|
|
|
* write flv metadata.
|
|
|
|
* serialize from:
|
|
|
|
* AMF0 string: onMetaData,
|
|
|
|
* AMF0 object: the metadata object.
|
|
|
|
*/
|
2014-04-17 08:06:49 +00:00
|
|
|
virtual int write_metadata(char* data, int size);
|
2014-04-17 08:16:17 +00:00
|
|
|
/**
|
|
|
|
* write audio/video packet.
|
|
|
|
*/
|
2014-04-17 08:06:49 +00:00
|
|
|
virtual int write_audio(int32_t timestamp, char* data, int size);
|
|
|
|
virtual int write_video(int32_t timestamp, char* data, int size);
|
|
|
|
private:
|
2014-04-17 08:16:17 +00:00
|
|
|
virtual int write_tag(char* header, int header_size, char* tag, int tag_size);
|
2014-04-17 04:59:35 +00:00
|
|
|
};
|
|
|
|
|
2014-04-15 06:24:03 +00:00
|
|
|
/**
|
|
|
|
* dvr(digital video recorder) to record RTMP stream to flv file.
|
|
|
|
* TODO: FIXME: add utest for it.
|
|
|
|
*/
|
|
|
|
class SrsDvr
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
SrsSource* _source;
|
2014-04-17 08:06:49 +00:00
|
|
|
private:
|
|
|
|
bool dvr_enabled;
|
|
|
|
SrsFileStream* fs;
|
|
|
|
SrsFlvEncoder* enc;
|
2014-04-15 06:24:03 +00:00
|
|
|
public:
|
|
|
|
SrsDvr(SrsSource* source);
|
|
|
|
virtual ~SrsDvr();
|
|
|
|
public:
|
|
|
|
/**
|
2014-04-16 09:54:41 +00:00
|
|
|
* publish stream event,
|
|
|
|
* when encoder start to publish RTMP stream.
|
2014-04-15 06:24:03 +00:00
|
|
|
*/
|
|
|
|
virtual int on_publish(SrsRequest* req);
|
|
|
|
/**
|
2014-04-16 09:54:41 +00:00
|
|
|
* the unpublish event.,
|
|
|
|
* when encoder stop(unpublish) to publish RTMP stream.
|
2014-04-15 06:24:03 +00:00
|
|
|
*/
|
|
|
|
virtual void on_unpublish();
|
|
|
|
/**
|
|
|
|
* get some information from metadata, it's optinal.
|
|
|
|
*/
|
2014-04-17 08:06:49 +00:00
|
|
|
virtual int on_meta_data(SrsOnMetaDataPacket* metadata);
|
2014-04-15 06:24:03 +00:00
|
|
|
/**
|
2014-04-16 09:54:41 +00:00
|
|
|
* mux the audio packets to dvr.
|
2014-04-15 06:24:03 +00:00
|
|
|
*/
|
|
|
|
virtual int on_audio(SrsSharedPtrMessage* audio);
|
|
|
|
/**
|
2014-04-16 09:54:41 +00:00
|
|
|
* mux the video packets to dvr.
|
2014-04-15 06:24:03 +00:00
|
|
|
*/
|
|
|
|
virtual int on_video(SrsSharedPtrMessage* video);
|
|
|
|
};
|
|
|
|
|
2014-04-15 06:01:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|