Valkka  1.6.1
OpenSource Video Management
liveserver.h
Go to the documentation of this file.
1 #ifndef SERVER_HEADER_GUARD
2 #define SERVER_HEADER_GUARD
3 
4 /*
5  * liveserver.h : Live555 interface for server side: streaming to udp sockets directly or by using an on-demand rtsp server
6  *
7  * (c) Copyright 2017-2024 Sampsa Riikonen
8  *
9  * Authors: Sampsa Riikonen <sampsa.riikonen@iki.fi>
10  *
11  * This file is part of the Valkka library.
12  *
13  * Valkka is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser General Public License as
15  * published by the Free Software Foundation, either version 3 of the
16  * License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with this program. If not, see <https://www.gnu.org/licenses/>
25  *
26  */
27 
38 // TODO! There is no way to close these suckers with Medium::close(). Then BasicUsageEnvironment can't be reclaimed at we get a (minor) memory leak upon exit.
39 
40 #include "livedep.h"
41 #include "framefifo.h"
42 #include "frame.h"
43 #include "logging.h"
44 
45 
50 class BufferSource: public FramedSource {
51 
52 public:
59  BufferSource(UsageEnvironment &env, FrameFifo &fifo, Boolean &canary, unsigned preferredFrameSize = 0, unsigned playTimePerFrame =0, unsigned offset=0);
60  virtual ~BufferSource();
61 
62 private:
63  virtual void doGetNextFrame();
64 
65 private:
67  Boolean &canary;
68  unsigned fPreferredFrameSize;
69  unsigned fPlayTimePerFrame;
70  unsigned offset;
71  std::mutex mutex;
72  long int prevtime;
73 
74 public:
75  std::deque<BasicFrame*> internal_fifo;
76  bool active;
77 
78 public:
79  void handleFrame(Frame* f);
80 
81 };
82 
83 
92 class Stream {
93 
94 public:
104  Stream(UsageEnvironment &env, FrameFifo &fifo, const std::string adr, unsigned short int portnum, const unsigned char ttl=255);
106  virtual ~Stream();
107 
108 protected:
109  UsageEnvironment &env;
111 
112  RTPSink *sink;
113  RTCPInstance *rtcp;
114 
115  Groupsock *rtpGroupsock;
116  Groupsock *rtcpGroupsock;
117  unsigned char cname[101];
118 
120  FramedSource *terminal;
121  Boolean source_alive;
122 
123 public:
124  void handleFrame(Frame *f);
125  void startPlaying();
126  static void afterPlaying(void* cdata);
127 };
128 
129 
130 
131 class ValkkaServerMediaSubsession: public OnDemandServerMediaSubsession {
132 
133 protected: // we're a virtual base class
134  ValkkaServerMediaSubsession(UsageEnvironment &env, FrameFifo &fifo, Boolean reuseFirstSource);
135  virtual ~ValkkaServerMediaSubsession();
136 
137 protected:
138  // char const* fFileName;
139  // u_int64_t fFileSize; // if known
141  FrameFifo &fifo;
142  Boolean source_alive;
143 
144 protected:
145  virtual void setDoneFlag() =0;
146 
147 public:
148  void handleFrame(Frame *f);
149 };
150 
151 
152 
154 
155 public:
156  static H264ServerMediaSubsession* createNew(UsageEnvironment& env, FrameFifo &fifo, Boolean reuseFirstSource);
157  // Used to implement "getAuxSDPLine()":
158  void checkForAuxSDPLine1();
159  void afterPlayingDummy1();
160 
161 protected:
162  static void afterPlayingDummy(void* clientData);
163  static void checkForAuxSDPLine(void* clientData);
164 
165 protected:
166  H264ServerMediaSubsession(UsageEnvironment& env, FrameFifo &fifo, Boolean reuseFirstSource); // called only by createNew();
167  virtual ~H264ServerMediaSubsession();
168  void setDoneFlag() { fDoneFlag = ~0; }
169 
170 protected: // redefined virtual functions
171  virtual char const* getAuxSDPLine(RTPSink* rtpSink, FramedSource* inputSource);
172  virtual FramedSource* createNewStreamSource(unsigned clientSessionId, unsigned& estBitrate); // returns BufferSource, framed with correct type
173  virtual RTPSink* createNewRTPSink(Groupsock* rtpGroupsock, unsigned char rtpPayloadTypeIfDynamic, FramedSource* inputSource); // returns H264RTPSink
174 
175 private:
176  char* fAuxSDPLine;
177  char fDoneFlag; // used when setting up "fAuxSDPLine"
178  RTPSink* fDummyRTPSink; // ditto
179 };
180 
181 
182 
183 class H264Stream : public Stream {
184 
185 public:
186  H264Stream(UsageEnvironment &env, FrameFifo &fifo, const std::string adr, unsigned short int portnum, const unsigned char ttl=255);
187  ~H264Stream();
188 
189 };
190 
191 #endif
192 
Implements a FramedSource for sending frames.
Definition: liveserver.h:50
void handleFrame(Frame *f)
Copies a Frame from BufferSource::fifo into BufferSource::internal_fifo. Sets BufferSource::active....
Definition: liveserver.cpp:58
BufferSource(UsageEnvironment &env, FrameFifo &fifo, Boolean &canary, unsigned preferredFrameSize=0, unsigned playTimePerFrame=0, unsigned offset=0)
Default constructor.
Definition: liveserver.cpp:37
std::deque< BasicFrame * > internal_fifo
Internal fifo BasicFrame, i.e. payload frames.
Definition: liveserver.h:75
virtual void doGetNextFrame()
All the fun happens here.
Definition: liveserver.cpp:92
FrameFifo & fifo
Frames are being read from here. This reference leads all the way down to LiveThread::fifo.
Definition: liveserver.h:66
Boolean & canary
If this instance of BufferSource get's annihilated, kill the canary (set it to false)
Definition: liveserver.h:67
bool active
If set, doGetNextFrame is currently re-scheduled.
Definition: liveserver.h:76
A thread-safe combination of a fifo (first-in-first-out) queue and an associated stack.
Definition: framefifo.h:83
Frame: An abstract queueable class.
Definition: frame.h:112
Definition: liveserver.h:153
void setDoneFlag()
call before removing this from the server .. informs the extra inner event loop (if any)
Definition: liveserver.h:168
Definition: liveserver.h:183
An outbound Stream.
Definition: liveserver.h:92
UsageEnvironment & env
Identifies the live555 event loop.
Definition: liveserver.h:109
FrameFifo & fifo
Frames are read from here. This reference leads all the way down to LiveThread::fifo.
Definition: liveserver.h:110
virtual ~Stream()
Default destructor.
Definition: liveserver.cpp:295
RTPSink * sink
Live555 class: queries frames from terminal.
Definition: liveserver.h:112
BufferSource * buffer_source
Reserved in the child classes (depends on the payload type)
Definition: liveserver.h:119
FramedSource * terminal
The final sink in the live555 filterchain.
Definition: liveserver.h:120
Boolean source_alive
A canary variable that tells us if live555 event loop has closed the buffer_source.
Definition: liveserver.h:121
Stream(UsageEnvironment &env, FrameFifo &fifo, const std::string adr, unsigned short int portnum, const unsigned char ttl=255)
Default constructor.
Definition: liveserver.cpp:228
Definition: liveserver.h:131
virtual void setDoneFlag()=0
call before removing this from the server .. informs the extra inner event loop (if any)
Boolean source_alive
A canary variable that tells us if live555 event loop has closed the buffer_source.
Definition: liveserver.h:142
BufferSource * buffer_source
Reserved in the child classes (depends on the payload type)
Definition: liveserver.h:140
void handleFrame(Frame *f)
Puts a frame into the buffer_source.
Definition: liveserver.cpp:321
Frame classes.
Thread safe system of fifo and a stack.
List of common header files.
Logging utilities.