1/**********
2This library is free software; you can redistribute it and/or modify it under
3the terms of the GNU Lesser General Public License as published by the
4Free Software Foundation; either version 3 of the License, or (at your
5option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
6
7This library is distributed in the hope that it will be useful, but WITHOUT
8ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10more details.
11
12You should have received a copy of the GNU Lesser General Public License
13along with this library; if not, write to the Free Software Foundation, Inc.,
1451 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15**********/
16// "liveMedia"
17// Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved.
18// A 'ServerMediaSubsession' object that creates new, unicast, "RTPSink"s
19// on demand, from a H265 video file.
20// Implementation
21
22#include "H265VideoFileServerMediaSubsession.hh"
23#include "H265VideoRTPSink.hh"
24#include "ByteStreamFileSource.hh"
25#include "H265VideoStreamFramer.hh"
26
27H265VideoFileServerMediaSubsession*
28H265VideoFileServerMediaSubsession::createNew(UsageEnvironment& env,
29 char const* fileName,
30 Boolean reuseFirstSource) {
31 return new H265VideoFileServerMediaSubsession(env, fileName, reuseFirstSource);
32}
33
34H265VideoFileServerMediaSubsession::H265VideoFileServerMediaSubsession(UsageEnvironment& env,
35 char const* fileName, Boolean reuseFirstSource)
36 : FileServerMediaSubsession(env, fileName, reuseFirstSource),
37 fAuxSDPLine(NULL), fDoneFlag(0), fDummyRTPSink(NULL) {
38}
39
40H265VideoFileServerMediaSubsession::~H265VideoFileServerMediaSubsession() {
41 delete[] fAuxSDPLine;
42}
43
44static void afterPlayingDummy(void* clientData) {
45 H265VideoFileServerMediaSubsession* subsess = (H265VideoFileServerMediaSubsession*)clientData;
46 subsess->afterPlayingDummy1();
47}
48
49void H265VideoFileServerMediaSubsession::afterPlayingDummy1() {
50 // Unschedule any pending 'checking' task:
51 envir().taskScheduler().unscheduleDelayedTask(nextTask());
52 // Signal the event loop that we're done:
53 setDoneFlag();
54}
55
56static void checkForAuxSDPLine(void* clientData) {
57 H265VideoFileServerMediaSubsession* subsess = (H265VideoFileServerMediaSubsession*)clientData;
58 subsess->checkForAuxSDPLine1();
59}
60
61void H265VideoFileServerMediaSubsession::checkForAuxSDPLine1() {
62 nextTask() = NULL;
63
64 char const* dasl;
65 if (fAuxSDPLine != NULL) {
66 // Signal the event loop that we're done:
67 setDoneFlag();
68 } else if (fDummyRTPSink != NULL && (dasl = fDummyRTPSink->auxSDPLine()) != NULL) {
69 fAuxSDPLine = strDup(dasl);
70 fDummyRTPSink = NULL;
71
72 // Signal the event loop that we're done:
73 setDoneFlag();
74 } else if (!fDoneFlag) {
75 // try again after a brief delay:
76 int uSecsToDelay = 100000; // 100 ms
77 nextTask() = envir().taskScheduler().scheduleDelayedTask(uSecsToDelay,
78 (TaskFunc*)checkForAuxSDPLine, this);
79 }
80}
81
82char const* H265VideoFileServerMediaSubsession::getAuxSDPLine(RTPSink* rtpSink, FramedSource* inputSource) {
83 if (fAuxSDPLine != NULL) return fAuxSDPLine; // it's already been set up (for a previous client)
84
85 if (fDummyRTPSink == NULL) { // we're not already setting it up for another, concurrent stream
86 // Note: For H265 video files, the 'config' information (used for several payload-format
87 // specific parameters in the SDP description) isn't known until we start reading the file.
88 // This means that "rtpSink"s "auxSDPLine()" will be NULL initially,
89 // and we need to start reading data from our file until this changes.
90 fDummyRTPSink = rtpSink;
91
92 // Start reading the file:
93 fDummyRTPSink->startPlaying(*inputSource, afterPlayingDummy, this);
94
95 // Check whether the sink's 'auxSDPLine()' is ready:
96 checkForAuxSDPLine(this);
97 }
98
99 envir().taskScheduler().doEventLoop(&fDoneFlag);
100
101 return fAuxSDPLine;
102}
103
104FramedSource* H265VideoFileServerMediaSubsession::createNewStreamSource(unsigned /*clientSessionId*/, unsigned& estBitrate) {
105 estBitrate = 500; // kbps, estimate
106
107 // Create the video source:
108 ByteStreamFileSource* fileSource = ByteStreamFileSource::createNew(envir(), fFileName);
109 if (fileSource == NULL) return NULL;
110 fFileSize = fileSource->fileSize();
111
112 // Create a framer for the Video Elementary Stream:
113 return H265VideoStreamFramer::createNew(envir(), fileSource);
114}
115
116RTPSink* H265VideoFileServerMediaSubsession
117::createNewRTPSink(Groupsock* rtpGroupsock,
118 unsigned char rtpPayloadTypeIfDynamic,
119 FramedSource* /*inputSource*/) {
120 return H265VideoRTPSink::createNew(envir(), rtpGroupsock, rtpPayloadTypeIfDynamic);
121}
122