-
Notifications
You must be signed in to change notification settings - Fork 26
/
RTMPStream.cpp
212 lines (176 loc) · 5.47 KB
/
RTMPStream.cpp
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright (C) 2016-2020 Arne Morten Kvarving
* Copyright (C) 2018-2020 Team Kodi
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* <http://www.gnu.org/licenses/>.
*
*/
#include "utils/Log.h"
#include "timer/Timer.h"
#include <iostream>
#include <map>
#include <string.h>
#include <sstream>
#include <librtmp/log.h>
#include <librtmp/rtmp.h>
#include <kodi/addon-instance/Inputstream.h>
/***********************************************************
* InputSteam Client AddOn specific public library functions
***********************************************************/
#define SetAVal(av, cstr) av.av_val = (char *)cstr; av.av_len = strlen(cstr)
#undef AVC
#define AVC(str) {(char *)str,sizeof(str)-1}
/* librtmp option names are slightly different */
namespace
{
std::map<std::string, AVal> options =
{{ "SWFPlayer", AVC("swfUrl") },
{ "PageURL", AVC("pageUrl") },
{ "PlayPath", AVC("playpath")},
{ "TcUrl", AVC("tcUrl") },
{ "IsLive", AVC("live") }};
}
class ATTRIBUTE_HIDDEN CInputStreamRTMP
: public kodi::addon::CInstanceInputStream,
public rtmpstream::ITimerCallback
{
public:
CInputStreamRTMP(KODI_HANDLE instance);
bool Open(INPUTSTREAM& props) override;
void Close() override;
void GetCapabilities(INPUTSTREAM_CAPABILITIES& caps) override;
INPUTSTREAM_IDS GetStreamIds() override;
INPUTSTREAM_INFO GetStream(int streamid) override;
void EnableStream(int streamid, bool enable) override;
bool OpenStream(int streamid) override;
int ReadStream(uint8_t* buffer, unsigned int bufferSize) override;
bool PosTime(int ms) override;
int GetTotalTime() override { return 20; }
int GetTime() override { return 0; }
private:
void OnTimeout() override;
RTMP* m_session = nullptr;
bool m_readPauseDetected = false;
mutable std::recursive_mutex m_critSection;
rtmpstream::CTimer m_readPauseDetectTimer;
};
CInputStreamRTMP::CInputStreamRTMP(KODI_HANDLE instance)
: CInstanceInputStream(instance),
m_readPauseDetectTimer(this)
{
}
bool CInputStreamRTMP::Open(INPUTSTREAM& props)
{
rtmpstream::Log(ADDON_LOG_DEBUG, "InputStream.rtmp: OpenStream()");
m_session = RTMP_Alloc();
RTMP_Init(m_session);
RTMP_SetupURL(m_session, const_cast<char*>(props.m_strURL));
for (auto& it : options)
{
for (size_t i = 0; i < props.m_nCountInfoValues; ++i)
{
if (it.first == props.m_ListItemProperties[i].m_strKey)
{
AVal av_tmp;
SetAVal(av_tmp, props.m_ListItemProperties[i].m_strValue);
RTMP_SetOpt(m_session, &it.second, &av_tmp);
}
}
}
if (!RTMP_Connect(m_session, nullptr) || !RTMP_ConnectStream(m_session, 0))
{
RTMP_Close(m_session);
m_session = nullptr;
return false;
}
return true;
}
bool CInputStreamRTMP::OpenStream(int streamid)
{
return false;
}
void CInputStreamRTMP::Close()
{
m_readPauseDetectTimer.Stop();
if (m_session)
{
std::unique_lock<std::recursive_mutex> lock(m_critSection);
RTMP_Close(m_session);
RTMP_Free(m_session);
}
m_session = nullptr;
m_readPauseDetected = false;
}
void CInputStreamRTMP::GetCapabilities(INPUTSTREAM_CAPABILITIES &caps)
{
caps.m_mask |= INPUTSTREAM_CAPABILITIES::SUPPORTS_IPOSTIME;
caps.m_mask |= INPUTSTREAM_CAPABILITIES::SUPPORTS_SEEK;
caps.m_mask |= INPUTSTREAM_CAPABILITIES::SUPPORTS_PAUSE;
}
INPUTSTREAM_IDS CInputStreamRTMP::GetStreamIds()
{
INPUTSTREAM_IDS ids = { 0 };
return ids;
}
INPUTSTREAM_INFO CInputStreamRTMP::GetStream(int streamid)
{
INPUTSTREAM_INFO info = { INPUTSTREAM_INFO::STREAM_TYPE::TYPE_NONE };
return info;
}
void CInputStreamRTMP::EnableStream(int streamid, bool enable)
{
}
int CInputStreamRTMP::ReadStream(uint8_t* buf, unsigned int size)
{
std::unique_lock<std::recursive_mutex> lock(m_critSection);
if (m_readPauseDetected)
{
m_readPauseDetected = false;
RTMP_Pause(m_session, false);
rtmpstream::Log(ADDON_LOG_DEBUG, "InputStream.rtmp: Read resume detected");
}
if (m_readPauseDetectTimer.IsRunning())
m_readPauseDetectTimer.RestartAsync(2 * 1000);
else
m_readPauseDetectTimer.Start(2 * 1000);
return RTMP_Read(m_session, reinterpret_cast<char*>(buf), size);
}
void CInputStreamRTMP::OnTimeout()
{
std::unique_lock<std::recursive_mutex> lock(m_critSection);
m_readPauseDetected = true;
rtmpstream::Log(ADDON_LOG_DEBUG, "InputStream.rtmp: Read pause detected");
RTMP_Pause(m_session, true);
}
bool CInputStreamRTMP::PosTime(int ms)
{
std::unique_lock<std::recursive_mutex> lock(m_critSection);
return RTMP_SendSeek(m_session, ms);
}
/*****************************************************************************************************/
class ATTRIBUTE_HIDDEN CMyAddon
: public kodi::addon::CAddonBase
{
public:
CMyAddon() = default;
ADDON_STATUS CreateInstance(int instanceType, std::string instanceID, KODI_HANDLE instance, KODI_HANDLE& addonInstance) override
{
if (instanceType == ADDON_INSTANCE_INPUTSTREAM)
{
addonInstance = new CInputStreamRTMP(instance);
return ADDON_STATUS_OK;
}
return ADDON_STATUS_NOT_IMPLEMENTED;
}
};
ADDONCREATOR(CMyAddon)