hc
2023-11-06 15ade055295d13f95d49e3d99b09f3bbfb4a43e7
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
// 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 "mp2_server_media_subsession.hh"
#include "MPEG1or2AudioRTPSink.hh"
 
#include "utils.h"
namespace easymedia
{
    MP2ServerMediaSubsession* MP2ServerMediaSubsession::createNew(UsageEnvironment& env, Live555MediaInput& wisInput)
    {
        return new MP2ServerMediaSubsession(env, wisInput);
    }
 
    MP2ServerMediaSubsession::MP2ServerMediaSubsession(UsageEnvironment& env, Live555MediaInput& mediaInput)
        : OnDemandServerMediaSubsession(env, True /*reuse the first source*/), fMediaInput(mediaInput)
    {
    }
 
    MP2ServerMediaSubsession::~MP2ServerMediaSubsession()
    {
        LOG_FILE_FUNC_LINE();
    }
 
    FramedSource* MP2ServerMediaSubsession::createNewStreamSource(unsigned /*clientSessionId*/, unsigned& estBitrate)
    {
        LOG_FILE_FUNC_LINE();
        estBitrate = 128; // kbps, estimate
        return fMediaInput.audioSource();
    }
 
    RTPSink* MP2ServerMediaSubsession::createNewRTPSink(Groupsock* rtpGroupsock,
                                                        unsigned char /*rtpPayloadTypeIfDynamic*/,
                                                        FramedSource* inputSource)
    {
        if (!inputSource) {
            LOG("inputSource is not ready, can not create new rtp sink\n");
            return NULL;
        }
        setAudioRTPSinkBufferSize();
        RTPSink* rtpsink = MPEG1or2AudioRTPSink::createNew(envir(), rtpGroupsock);
        setVideoRTPSinkBufferSize();
        return rtpsink;
    }
 
    // std::mutex MP2ServerMediaSubsession::kMutex;
    void MP2ServerMediaSubsession::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 (kSessionIdList.empty()) {
            fMediaInput.Start(envir());
        }
        if (fMediaInput.GetStartAudioStreamCallback() != NULL) {
            fMediaInput.GetStartAudioStreamCallback()();
        }
        LOG("%s - clientSessionId: 0x%08x\n", __func__, clientSessionId);
        kSessionIdList.push_back(clientSessionId);
        // kMutex.unlock();
    }
    void MP2ServerMediaSubsession::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);
    }
 
} // namespace easymedia