1 | /********** |
2 | This library is free software; you can redistribute it and/or modify it under |
3 | the terms of the GNU Lesser General Public License as published by the |
4 | Free Software Foundation; either version 3 of the License, or (at your |
5 | option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.) |
6 | |
7 | This library is distributed in the hope that it will be useful, but WITHOUT |
8 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
9 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
10 | more details. |
11 | |
12 | You should have received a copy of the GNU Lesser General Public License |
13 | along with this library; if not, write to the Free Software Foundation, Inc., |
14 | 51 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 H264 video file. |
20 | // Implementation |
21 | |
22 | #include "H264VideoFileServerMediaSubsession.hh" |
23 | #include "H264VideoRTPSink.hh" |
24 | #include "ByteStreamFileSource.hh" |
25 | #include "H264VideoStreamFramer.hh" |
26 | |
27 | H264VideoFileServerMediaSubsession* |
28 | H264VideoFileServerMediaSubsession::createNew(UsageEnvironment& env, |
29 | char const* fileName, |
30 | Boolean reuseFirstSource) { |
31 | return new H264VideoFileServerMediaSubsession(env, fileName, reuseFirstSource); |
32 | } |
33 | |
34 | H264VideoFileServerMediaSubsession::H264VideoFileServerMediaSubsession(UsageEnvironment& env, |
35 | char const* fileName, Boolean reuseFirstSource) |
36 | : FileServerMediaSubsession(env, fileName, reuseFirstSource), |
37 | fAuxSDPLine(NULL), fDoneFlag(0), fDummyRTPSink(NULL) { |
38 | } |
39 | |
40 | H264VideoFileServerMediaSubsession::~H264VideoFileServerMediaSubsession() { |
41 | delete[] fAuxSDPLine; |
42 | } |
43 | |
44 | static void afterPlayingDummy(void* clientData) { |
45 | H264VideoFileServerMediaSubsession* subsess = (H264VideoFileServerMediaSubsession*)clientData; |
46 | subsess->afterPlayingDummy1(); |
47 | } |
48 | |
49 | void H264VideoFileServerMediaSubsession::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 | |
56 | static void checkForAuxSDPLine(void* clientData) { |
57 | H264VideoFileServerMediaSubsession* subsess = (H264VideoFileServerMediaSubsession*)clientData; |
58 | subsess->checkForAuxSDPLine1(); |
59 | } |
60 | |
61 | void H264VideoFileServerMediaSubsession::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 | |
82 | char const* H264VideoFileServerMediaSubsession::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 H264 video files, the 'config' information ("profile-level-id" and "sprop-parameter-sets") isn't known |
87 | // until we start reading the file. This means that "rtpSink"s "auxSDPLine()" will be NULL initially, |
88 | // and we need to start reading data from our file until this changes. |
89 | fDummyRTPSink = rtpSink; |
90 | |
91 | // Start reading the file: |
92 | fDummyRTPSink->startPlaying(*inputSource, afterPlayingDummy, this); |
93 | |
94 | // Check whether the sink's 'auxSDPLine()' is ready: |
95 | checkForAuxSDPLine(this); |
96 | } |
97 | |
98 | envir().taskScheduler().doEventLoop(&fDoneFlag); |
99 | |
100 | return fAuxSDPLine; |
101 | } |
102 | |
103 | FramedSource* H264VideoFileServerMediaSubsession::createNewStreamSource(unsigned /*clientSessionId*/, unsigned& estBitrate) { |
104 | estBitrate = 500; // kbps, estimate |
105 | |
106 | // Create the video source: |
107 | ByteStreamFileSource* fileSource = ByteStreamFileSource::createNew(envir(), fFileName); |
108 | if (fileSource == NULL) return NULL; |
109 | fFileSize = fileSource->fileSize(); |
110 | |
111 | // Create a framer for the Video Elementary Stream: |
112 | return H264VideoStreamFramer::createNew(envir(), fileSource); |
113 | } |
114 | |
115 | RTPSink* H264VideoFileServerMediaSubsession |
116 | ::createNewRTPSink(Groupsock* rtpGroupsock, |
117 | unsigned char rtpPayloadTypeIfDynamic, |
118 | FramedSource* /*inputSource*/) { |
119 | return H264VideoRTPSink::createNew(envir(), rtpGroupsock, rtpPayloadTypeIfDynamic); |
120 | } |
121 | |