hc
2023-05-26 a23f51ed7a39e452c1037343a84d7db1ca2c5bd7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2019 Fuzhou Rockchip Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
#include "h265_server_media_subsession.hh"
 
#include <liveMedia/H265VideoRTPSink.hh>
#include <liveMedia/H265VideoStreamDiscreteFramer.hh>
 
#include "media_type.h"
#include "utils.h"
 
namespace easymedia
{
    H265ServerMediaSubsession* H265ServerMediaSubsession::createNew(UsageEnvironment& env, Live555MediaInput& wisInput)
    {
        return new H265ServerMediaSubsession(env, wisInput);
    }
 
    H265ServerMediaSubsession::H265ServerMediaSubsession(UsageEnvironment& env, Live555MediaInput& mediaInput)
        : OnDemandServerMediaSubsession(env, True /*reuse the first source*/), fMediaInput(mediaInput),
          fEstimatedKbps(1000), fDoneFlag(0), fDummyRTPSink(NULL), fGetSdpCount(10), fAuxSDPLine(NULL)
    {
    }
 
    H265ServerMediaSubsession::~H265ServerMediaSubsession()
    {
        LOG_FILE_FUNC_LINE();
        if (fAuxSDPLine != NULL) {
            delete[] fAuxSDPLine;
            fAuxSDPLine = NULL;
        }
    }
 
    // std::mutex H265ServerMediaSubsession::kMutex;
    // std::list<unsigned int> H265ServerMediaSubsession::kSessionIdList;
 
    void H265ServerMediaSubsession::startStream(
        unsigned clientSessionId, void* streamToken, TaskFunc* rtcpRRHandler, void* rtcpRRHandlerClientData,
        unsigned short& rtpSeqNum, unsigned& rtpTimestamp,
        ServerRequestAlternativeByteHandler* serverRequestAlternativeByteHandler,
        void* serverRequestAlternativeByteHandlerClientData)
    {
        OnDemandServerMediaSubsession::startStream(clientSessionId, streamToken, rtcpRRHandler, rtcpRRHandlerClientData,
                                                   rtpSeqNum, rtpTimestamp, serverRequestAlternativeByteHandler,
                                                   serverRequestAlternativeByteHandlerClientData);
        // kMutex.lock();
        if (fMediaInput.GetStartVideoStreamCallback() != NULL) {
            fMediaInput.GetStartVideoStreamCallback()();
        }
        if (kSessionIdList.empty()) {
            fMediaInput.Start(envir());
        }
        LOG("%s:%s:%p - clientSessionId: 0x%08x\n", __FILE__, __func__, this, clientSessionId);
        kSessionIdList.push_back(clientSessionId);
        // kMutex.unlock();
    }
    void H265ServerMediaSubsession::deleteStream(unsigned clientSessionId, void*& streamToken)
    {
        // kMutex.lock();
        LOG("%s - clientSessionId: 0x%08x\n", __func__, clientSessionId);
        kSessionIdList.remove(clientSessionId);
        if (kSessionIdList.empty()) {
            fMediaInput.Stop(envir());
        }
        // kMutex.unlock();
        OnDemandServerMediaSubsession::deleteStream(clientSessionId, streamToken);
    }
 
    static void afterPlayingDummy(void* clientData)
    {
        H265ServerMediaSubsession* subsess = (H265ServerMediaSubsession*)clientData;
        LOG("%s, set done.\n", __func__);
        // Signal the event loop that we're done:
        subsess->afterPlayingDummy1();
    }
 
    void H265ServerMediaSubsession::afterPlayingDummy1()
    {
        // Unschedule any pending 'checking' task:
        envir().taskScheduler().unscheduleDelayedTask(nextTask());
        // Signal the event loop that we're done:
        setDoneFlag();
    }
 
    static void checkForAuxSDPLine(void* clientData)
    {
        H265ServerMediaSubsession* subsess = (H265ServerMediaSubsession*)clientData;
        subsess->checkForAuxSDPLine1();
    }
 
    void H265ServerMediaSubsession::checkForAuxSDPLine1()
    {
        nextTask() = NULL;
 
        char const* dasl;
        if (fAuxSDPLine != NULL) {
            // Signal the event loop that we're done:
            setDoneFlag();
        } else if (fDummyRTPSink != NULL && (dasl = fDummyRTPSink->auxSDPLine()) != NULL) {
            fAuxSDPLine = strDup(dasl);
            fDummyRTPSink = NULL;
 
            // Signal the event loop that we're done:
            setDoneFlag();
        } else if (!fDoneFlag) {
            if (fGetSdpCount-- < 0) {
                setDoneFlag();
                LOG("%s:%s:%p: get sdp time out.\n", __FILE__, __func__, this);
            } else {
                // try again after a brief delay:
                int uSecsToDelay = 100000; // 100 ms
                LOG_FILE_FUNC_LINE();
                nextTask() =
                    envir().taskScheduler().scheduleDelayedTask(uSecsToDelay, (TaskFunc*)checkForAuxSDPLine, this);
            }
        }
    }
 
    char const* H265ServerMediaSubsession::getAuxSDPLine(RTPSink* rtpSink, FramedSource* inputSource)
    {
        // Note: For MPEG-4 video buffer, the 'config' information isn't known
        // until we start reading the Buffer.  This means that "rtpSink"s
        // "auxSDPLine()" will be NULL initially, and we need to start reading
        // data from our buffer until this changes.
        if (fAuxSDPLine != NULL) {
            return fAuxSDPLine;
        }
        if (fDummyRTPSink == NULL) {
            // force I framed
            if (fMediaInput.GetStartVideoStreamCallback() != NULL) {
                fMediaInput.GetStartVideoStreamCallback()();
            }
            fDummyRTPSink = rtpSink;
            fGetSdpCount = 10;
            fDummyRTPSink->startPlaying(*inputSource, afterPlayingDummy, this);
            checkForAuxSDPLine(this);
        }
        envir().taskScheduler().doEventLoop(&fDoneFlag);
        return fAuxSDPLine;
    }
 
    FramedSource* H265ServerMediaSubsession::createNewStreamSource(unsigned clientSessionId, unsigned& estBitrate)
    {
        LOG("%s:%s:%p - clientSessionId: 0x%08x\n", __FILE__, __func__, this, clientSessionId);
        estBitrate = fMediaInput.getMaxIdrSize();
        if (estBitrate < fEstimatedKbps) {
            estBitrate = fEstimatedKbps;
        }
        // Create a framer for the Video Elementary Stream:
        FramedSource* source =
            H265VideoStreamDiscreteFramer::createNew(envir(), fMediaInput.videoSource(CODEC_TYPE_H265));
        LOG("H265 framedsource : %p, estBitrate = %u\n", source, estBitrate);
        return source;
    }
 
    RTPSink* H265ServerMediaSubsession::createNewRTPSink(Groupsock* rtpGroupsock, unsigned char rtpPayloadTypeIfDynamic,
                                                         FramedSource* inputSource)
    {
        if (!inputSource) {
            LOG("inputSource is not ready, can not create new rtp sink\n");
            return NULL;
        }
        setVideoRTPSinkBufferSize();
        LOG_FILE_FUNC_LINE();
        RTPSink* rtp_sink = H265VideoRTPSink::createNew(envir(), rtpGroupsock, rtpPayloadTypeIfDynamic);
        LOG("H265 rtp sink : %p\n", rtp_sink);
        return rtp_sink;
    }
 
} // namespace easymedia