| 1 |
/* |
| 2 |
* Copyright (C) 2005-2010 Team Kodi |
| 3 |
* https://kodi.tv |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#if defined LIVE555 |
| 20 |
|
| 21 |
#include "MepoRTSPClient.h" |
| 22 |
#include "MemorySink.h" |
| 23 |
#include <kodi/General.h> //for kodi::Log |
| 24 |
#include "utils.h" |
| 25 |
#include "os-dependent.h" |
| 26 |
|
| 27 |
#include <thread> |
| 28 |
|
| 29 |
CRTSPClient::CRTSPClient() |
| 30 |
{ |
| 31 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::CRTSPClient()"); |
| 32 |
allowProxyServers = false; |
| 33 |
controlConnectionUsesTCP = true; |
| 34 |
supportCodecSelection = false; |
| 35 |
clientProtocolName = "RTSP"; |
| 36 |
tunnelOverHTTPPortNum = 0; |
| 37 |
statusCode = 0; |
| 38 |
singleMedium = NULL; |
| 39 |
desiredPortNum = 0; |
| 40 |
createReceivers = true; |
| 41 |
simpleRTPoffsetArg = -1; |
| 42 |
socketInputBufferSize = 0; |
| 43 |
streamUsingTCP = false; |
| 44 |
fileSinkBufferSize = 20000; |
| 45 |
oneFilePerFrame = false; |
| 46 |
m_BufferThreadActive = false; |
| 47 |
m_duration = 7200*1000; |
| 48 |
m_fStart = 0.0f; |
| 49 |
m_session = NULL; |
| 50 |
m_ourClient = NULL; |
| 51 |
m_bPaused = false; |
| 52 |
m_outFileName[0] = '\0'; |
| 53 |
m_buffer = NULL; |
| 54 |
m_env = NULL; |
| 55 |
m_fDuration = 0.0f; |
| 56 |
m_url[0] = '\0'; |
| 57 |
m_running = false; |
| 58 |
} |
| 59 |
|
| 60 |
CRTSPClient::~CRTSPClient() |
| 61 |
{ |
| 62 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::~CRTSPClient()"); |
| 63 |
Medium::close(m_ourClient); |
| 64 |
m_ourClient = NULL; |
| 65 |
|
| 66 |
if (m_env) |
| 67 |
{ |
| 68 |
TaskScheduler *scheduler = &m_env->taskScheduler(); |
| 69 |
m_env->reclaim(); |
| 70 |
m_env = NULL; |
| 71 |
delete scheduler; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
Medium* CRTSPClient::createClient(UsageEnvironment& env,int verbosityLevel, char const* applicationName) |
| 77 |
{ |
| 78 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::createClient()"); |
| 79 |
return RTSPClient::createNew(env, verbosityLevel, applicationName,tunnelOverHTTPPortNum); |
| 80 |
} |
| 81 |
|
| 82 |
char* CRTSPClient::getOptionsResponse(Medium* client, char const* url,char* username, char* password) |
| 83 |
{ |
| 84 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::getOptionsResponse()"); |
| 85 |
RTSPClient* rtspClient = (RTSPClient*)client; |
| 86 |
char* optionsResponse = rtspClient->sendOptionsCmd(url, username, password); |
| 87 |
|
| 88 |
if (optionsResponse == NULL) |
| 89 |
{ |
| 90 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::getOptionsResponse(): \"OPTIONS\" request failed: %s", m_env->getResultMsg()); |
| 91 |
} else { |
| 92 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::getOptionsResponse(): \"OPTIONS\" request returned: %s", optionsResponse); |
| 93 |
} |
| 94 |
|
| 95 |
return optionsResponse; |
| 96 |
} |
| 97 |
|
| 98 |
char* CRTSPClient::getSDPDescriptionFromURL(Medium* client, char const* url, |
| 99 |
char const* username, char const* password, |
| 100 |
char const* /*proxyServerName*/, |
| 101 |
unsigned short /*proxyServerPortNum*/, |
| 102 |
unsigned short /*clientStartPort*/) |
| 103 |
{ |
| 104 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::getSDPDescriptionFromURL()"); |
| 105 |
RTSPClient* rtspClient = (RTSPClient*)client; |
| 106 |
char* result; |
| 107 |
if (username != NULL && password != NULL) |
| 108 |
{ |
| 109 |
result = rtspClient->describeWithPassword(url, username, password); |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
result = rtspClient->describeURL(url); |
| 114 |
} |
| 115 |
|
| 116 |
statusCode = rtspClient->describeStatus(); |
| 117 |
return result; |
| 118 |
} |
| 119 |
|
| 120 |
char* CRTSPClient::getSDPDescription() |
| 121 |
{ |
| 122 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::getSDPDescription()"); |
| 123 |
RTSPClient *client = (RTSPClient*)m_ourClient; |
| 124 |
RTSPClient *rtspClient = RTSPClient::createNew(client->envir(), 0, "TSFileSource", tunnelOverHTTPPortNum); |
| 125 |
char* result; |
| 126 |
result = rtspClient->describeURL(m_url); |
| 127 |
|
| 128 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::getSDPDescription() statusCode = %d", rtspClient->describeStatus()); |
| 129 |
Medium::close(rtspClient); |
| 130 |
|
| 131 |
return result; |
| 132 |
} |
| 133 |
|
| 134 |
bool CRTSPClient::clientSetupSubsession(Medium* client, MediaSubsession* subsession, bool streamUsingTCP) |
| 135 |
{ |
| 136 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::clientSetupSubsession()"); |
| 137 |
if (client == NULL || subsession == NULL) |
| 138 |
return false; |
| 139 |
RTSPClient* rtspClient = (RTSPClient*) client; |
| 140 |
return ( rtspClient->setupMediaSubsession(*subsession, False, (streamUsingTCP ? True : False)) ? true : false); |
| 141 |
} |
| 142 |
|
| 143 |
bool CRTSPClient::clientStartPlayingSession(Medium* client, MediaSession* session) |
| 144 |
{ |
| 145 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::clientStartPlayingSession()"); |
| 146 |
if (client == NULL || session == NULL) |
| 147 |
return false; |
| 148 |
RTSPClient* rtspClient = (RTSPClient*) client; |
| 149 |
|
| 150 |
long dur = m_duration/1000; |
| 151 |
double fStart = m_fStart; |
| 152 |
|
| 153 |
if (m_fDuration > 0.0) |
| 154 |
{ |
| 155 |
double fStartToEnd = m_fDuration-m_fStart; |
| 156 |
if (fStartToEnd<0) |
| 157 |
fStartToEnd = 0; |
| 158 |
fStart = dur - fStartToEnd; |
| 159 |
if (fStart<0) |
| 160 |
fStart = 0; |
| 161 |
} |
| 162 |
|
| 163 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::clientStartPlayingSession() play from %.3f / %.3f", fStart, (float) m_duration/1000); |
| 164 |
return (rtspClient->playMediaSession(*session,fStart) ? true : false); |
| 165 |
} |
| 166 |
|
| 167 |
bool CRTSPClient::clientTearDownSession(Medium* client,MediaSession* session) |
| 168 |
{ |
| 169 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::clientTearDownSession()"); |
| 170 |
if (client == NULL || session == NULL) |
| 171 |
return false; |
| 172 |
RTSPClient* rtspClient = (RTSPClient*)client; |
| 173 |
return (rtspClient->teardownMediaSession(*session) ? true : false); |
| 174 |
} |
| 175 |
|
| 176 |
void my_subsessionAfterPlaying(void* UNUSED(clientData)) |
| 177 |
{ |
| 178 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::subsessionAfterPlaying()"); |
| 179 |
} |
| 180 |
|
| 181 |
void my_subsessionByeHandler(void* UNUSED(clientData)) |
| 182 |
{ |
| 183 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::subsessionByeHandler()"); |
| 184 |
} |
| 185 |
|
| 186 |
void CRTSPClient::closeMediaSinks() |
| 187 |
{ |
| 188 |
if (m_session == NULL) |
| 189 |
return; |
| 190 |
|
| 191 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::closeMediaSinks()"); |
| 192 |
|
| 193 |
MediaSubsessionIterator iter(*m_session); |
| 194 |
MediaSubsession* subsession; |
| 195 |
|
| 196 |
while ((subsession = iter.next()) != NULL) |
| 197 |
{ |
| 198 |
Medium::close(subsession->sink); |
| 199 |
subsession->sink = NULL; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
void CRTSPClient::tearDownStreams() |
| 204 |
{ |
| 205 |
if (m_session == NULL) |
| 206 |
return; |
| 207 |
|
| 208 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::tearDownStreams()"); |
| 209 |
|
| 210 |
clientTearDownSession(m_ourClient, m_session); |
| 211 |
} |
| 212 |
bool CRTSPClient::setupStreams() |
| 213 |
{ |
| 214 |
// Setup streams |
| 215 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::setupStreams()"); |
| 216 |
|
| 217 |
bool madeProgress = false; |
| 218 |
MediaSubsessionIterator iter(*m_session); |
| 219 |
MediaSubsession *subsession; |
| 220 |
|
| 221 |
while ((subsession = iter.next()) != NULL) |
| 222 |
{ |
| 223 |
if (subsession->clientPortNum() == 0) |
| 224 |
continue; // port # was not set |
| 225 |
|
| 226 |
if (!clientSetupSubsession(m_ourClient, subsession, streamUsingTCP)) |
| 227 |
{ |
| 228 |
kodi::Log(ADDON_LOG_ERROR, "Failed to setup %s %s %s", subsession->mediumName(), subsession->codecName(), m_env->getResultMsg() ); |
| 229 |
} |
| 230 |
else |
| 231 |
{ |
| 232 |
kodi::Log(ADDON_LOG_DEBUG, "Setup %s %s %d %d", subsession->mediumName(), subsession->codecName(), subsession->clientPortNum(), subsession->clientPortNum() + 1); |
| 233 |
madeProgress = true; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
if (!madeProgress) |
| 238 |
{ |
| 239 |
shutdown(); |
| 240 |
return false; |
| 241 |
} |
| 242 |
return true; |
| 243 |
} |
| 244 |
|
| 245 |
bool CRTSPClient::startPlayingStreams() |
| 246 |
{ |
| 247 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::startPlayingStreams()"); |
| 248 |
|
| 249 |
if (!clientStartPlayingSession(m_ourClient, m_session)) |
| 250 |
{ |
| 251 |
kodi::Log(ADDON_LOG_ERROR, "Failed to start playing session :%s", m_env ->getResultMsg() ); |
| 252 |
shutdown(); |
| 253 |
return false; |
| 254 |
} |
| 255 |
else |
| 256 |
{ |
| 257 |
kodi::Log(ADDON_LOG_DEBUG, "Started playing session"); |
| 258 |
} |
| 259 |
return true; |
| 260 |
} |
| 261 |
|
| 262 |
void CRTSPClient::shutdown() |
| 263 |
{ |
| 264 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::shutdown()"); |
| 265 |
|
| 266 |
// Close our output files: |
| 267 |
closeMediaSinks(); |
| 268 |
|
| 269 |
// Teardown, then shutdown, any outstanding RTP/RTCP subsessions |
| 270 |
tearDownStreams(); |
| 271 |
Medium::close(m_session); |
| 272 |
|
| 273 |
// Finally, shut down our client: |
| 274 |
Medium::close(m_ourClient); |
| 275 |
m_session = NULL; |
| 276 |
m_ourClient = NULL; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
bool CRTSPClient::Initialize(CMemoryBuffer* buffer) |
| 281 |
{ |
| 282 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::Initialize()"); |
| 283 |
|
| 284 |
m_buffer = buffer; |
| 285 |
m_duration = 7200*1000; |
| 286 |
|
| 287 |
TaskScheduler* scheduler = BasicTaskScheduler::createNew(); |
| 288 |
m_env = BasicUsageEnvironment::createNew(*scheduler); |
| 289 |
|
| 290 |
m_ourClient = createClient(*m_env, 0/*verbosityLevel*/, "TSFileSource"); |
| 291 |
|
| 292 |
if (m_ourClient == NULL) |
| 293 |
{ |
| 294 |
kodi::Log(ADDON_LOG_ERROR, "Failed to create %s %s", clientProtocolName, m_env->getResultMsg() ); |
| 295 |
shutdown(); |
| 296 |
return false; |
| 297 |
} |
| 298 |
return true; |
| 299 |
} |
| 300 |
|
| 301 |
bool CRTSPClient::OpenStream(const char* url) |
| 302 |
{ |
| 303 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::OpenStream()"); |
| 304 |
m_session = NULL; |
| 305 |
|
| 306 |
strncpy(m_url, url, RTSP_URL_BUFFERSIZE - 1); |
| 307 |
m_url[RTSP_URL_BUFFERSIZE - 1] = '\0'; |
| 308 |
|
| 309 |
// Open the URL, to get a SDP description: |
| 310 |
char* sdpDescription = getSDPDescriptionFromURL(m_ourClient, url, ""/*username*/, ""/*password*/,""/*proxyServerName*/, 0/*proxyServerPortNum*/,1234/*desiredPortNum*/); |
| 311 |
|
| 312 |
if (sdpDescription == NULL) |
| 313 |
{ |
| 314 |
kodi::Log(ADDON_LOG_ERROR, "Failed to get a SDP description from URL %s %s", url, m_env->getResultMsg() ); |
| 315 |
shutdown(); |
| 316 |
return false; |
| 317 |
} |
| 318 |
kodi::Log(ADDON_LOG_DEBUG, "Opened URL %s %s", url, sdpDescription); |
| 319 |
|
| 320 |
char* range = strstr(sdpDescription, "a=range:npt="); |
| 321 |
if (range != NULL) |
| 322 |
{ |
| 323 |
char *pStart = range + strlen("a=range:npt="); |
| 324 |
char *pEnd = strstr(range, "-"); |
| 325 |
if (pEnd != NULL) |
| 326 |
{ |
| 327 |
pEnd++; |
| 328 |
double Start = atof(pStart); |
| 329 |
double End = atof(pEnd); |
| 330 |
|
| 331 |
kodi::Log(ADDON_LOG_DEBUG, "rangestart:%f rangeend:%f", Start, End); |
| 332 |
m_duration = (long) ((End-Start)*1000.0); |
| 333 |
} |
| 334 |
} |
| 335 |
// Create a media session object from this SDP description: |
| 336 |
m_session = MediaSession::createNew(*m_env, sdpDescription); |
| 337 |
delete[] sdpDescription; |
| 338 |
|
| 339 |
if (m_session == NULL) |
| 340 |
{ |
| 341 |
kodi::Log(ADDON_LOG_ERROR, "Failed to create a MediaSession object from the SDP description:%s ", m_env->getResultMsg()); |
| 342 |
shutdown(); |
| 343 |
return false; |
| 344 |
} |
| 345 |
else if (!m_session->hasSubsessions()) |
| 346 |
{ |
| 347 |
kodi::Log(ADDON_LOG_DEBUG, "This session has no media subsessions"); |
| 348 |
shutdown(); |
| 349 |
return false; |
| 350 |
} |
| 351 |
|
| 352 |
// Then, setup the "RTPSource"s for the session: |
| 353 |
MediaSubsessionIterator iter(*m_session); |
| 354 |
MediaSubsession *subsession; |
| 355 |
bool madeProgress = false; |
| 356 |
char const* singleMediumToTest = singleMedium; |
| 357 |
|
| 358 |
while ((subsession = iter.next()) != NULL) |
| 359 |
{ |
| 360 |
// If we've asked to receive only a single medium, then check this now: |
| 361 |
if (singleMediumToTest != NULL) |
| 362 |
{ |
| 363 |
if (strcmp(subsession->mediumName(), singleMediumToTest) != 0) |
| 364 |
{ |
| 365 |
kodi::Log(ADDON_LOG_DEBUG, "Ignoring %s %s %s", subsession->mediumName(), subsession->codecName(), singleMedium); |
| 366 |
continue; |
| 367 |
} |
| 368 |
else |
| 369 |
{ |
| 370 |
// Receive this subsession only |
| 371 |
singleMediumToTest = "xxxxx"; |
| 372 |
// this hack ensures that we get only 1 subsession of this type |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
if (desiredPortNum != 0) |
| 377 |
{ |
| 378 |
subsession->setClientPortNum(desiredPortNum); |
| 379 |
desiredPortNum += 2; |
| 380 |
} |
| 381 |
|
| 382 |
if (createReceivers) |
| 383 |
{ |
| 384 |
if (!subsession->initiate(simpleRTPoffsetArg)) |
| 385 |
{ |
| 386 |
kodi::Log(ADDON_LOG_ERROR, "Unable to create receiver for %s %s %s", subsession->mediumName(), subsession->codecName(), m_env->getResultMsg()); |
| 387 |
} |
| 388 |
else |
| 389 |
{ |
| 390 |
kodi::Log(ADDON_LOG_DEBUG, "Created receiver for type=%s codec=%s ports: %d %d ", subsession->mediumName(), subsession->codecName(), subsession->clientPortNum(), subsession->clientPortNum() + 1 ); |
| 391 |
madeProgress = true; |
| 392 |
|
| 393 |
if (subsession->rtpSource() != NULL) |
| 394 |
{ |
| 395 |
// Because we're saving the incoming data, rather than playing |
| 396 |
// it in real time, allow an especially large time threshold |
| 397 |
// (1 second) for reordering misordered incoming packets: |
| 398 |
int socketNum = subsession->rtpSource()->RTPgs()->socketNum(); |
| 399 |
|
| 400 |
kodi::Log(ADDON_LOG_DEBUG, "rtsp:increaseReceiveBufferTo to 2000000 for s:%d", socketNum); |
| 401 |
increaseReceiveBufferTo( *m_env, socketNum, 2000000 ); |
| 402 |
|
| 403 |
unsigned const thresh = 1000000; // 1 second |
| 404 |
subsession->rtpSource()->setPacketReorderingThresholdTime(thresh); |
| 405 |
|
| 406 |
if (socketInputBufferSize > 0) |
| 407 |
{ |
| 408 |
// Set the RTP source's input buffer size as specified: |
| 409 |
unsigned int curBufferSize = getReceiveBufferSize(*m_env, socketNum); |
| 410 |
unsigned int newBufferSize = setReceiveBufferTo(*m_env, socketNum, socketInputBufferSize); |
| 411 |
|
| 412 |
kodi::Log(ADDON_LOG_DEBUG, "Changed socket receive buffer size for the %s %s %d %d", subsession->mediumName(), subsession->codecName(), curBufferSize, newBufferSize); |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
} |
| 417 |
else |
| 418 |
{ |
| 419 |
if (subsession->clientPortNum() == 0) |
| 420 |
{ |
| 421 |
kodi::Log(ADDON_LOG_DEBUG, "No client port was specified for the %s %s", subsession->mediumName(), subsession->codecName()); |
| 422 |
} |
| 423 |
else |
| 424 |
{ |
| 425 |
madeProgress = true; |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
if (!madeProgress) |
| 431 |
{ |
| 432 |
shutdown(); |
| 433 |
return false; |
| 434 |
} |
| 435 |
|
| 436 |
// Perform additional 'setup' on each subsession, before playing them: |
| 437 |
if (!setupStreams()) |
| 438 |
{ |
| 439 |
return false; |
| 440 |
} |
| 441 |
|
| 442 |
// Create output files: |
| 443 |
// Create and start "FileSink"s for each subsession: |
| 444 |
madeProgress = false; |
| 445 |
iter.reset(); |
| 446 |
|
| 447 |
while ((subsession = iter.next()) != NULL) |
| 448 |
{ |
| 449 |
if (subsession->readSource() == NULL) continue; // was not initiated |
| 450 |
|
| 451 |
// Mediaportal: |
| 452 |
CMemorySink* fileSink = CMemorySink::createNew(*m_env, *m_buffer, fileSinkBufferSize); |
| 453 |
// XBMC test via file: |
| 454 |
//FileSink* fileSink = FileSink::createNew(*m_env, m_outFileName, fileSinkBufferSize, false); //oneFilePerFrame |
| 455 |
|
| 456 |
subsession->sink = fileSink; |
| 457 |
if (subsession->sink == NULL) |
| 458 |
{ |
| 459 |
kodi::Log(ADDON_LOG_DEBUG, "Failed to create FileSink %s", m_env->getResultMsg()); |
| 460 |
shutdown(); |
| 461 |
return false; |
| 462 |
} |
| 463 |
kodi::Log(ADDON_LOG_DEBUG, "Created output sink: %s", m_outFileName); |
| 464 |
subsession->sink->startPlaying(*(subsession->readSource()), my_subsessionAfterPlaying, subsession); |
| 465 |
|
| 466 |
// Also set a handler to be called if a RTCP "BYE" arrives |
| 467 |
// for this subsession: |
| 468 |
if (subsession->rtcpInstance() != NULL) |
| 469 |
{ |
| 470 |
subsession->rtcpInstance()->setByeHandler(my_subsessionByeHandler, subsession); |
| 471 |
} |
| 472 |
madeProgress = true; |
| 473 |
} |
| 474 |
|
| 475 |
return true; |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
void CRTSPClient::Stop() |
| 480 |
{ |
| 481 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient:Stop"); |
| 482 |
|
| 483 |
if (m_BufferThreadActive) |
| 484 |
{ |
| 485 |
StopBufferThread(); |
| 486 |
} |
| 487 |
|
| 488 |
shutdown(); |
| 489 |
m_buffer->Clear(); |
| 490 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient:Stop done"); |
| 491 |
} |
| 492 |
|
| 493 |
void CRTSPClient::StartBufferThread() |
| 494 |
{ |
| 495 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::StartBufferThread"); |
| 496 |
|
| 497 |
if (!m_BufferThreadActive) |
| 498 |
{ |
| 499 |
m_running = true; |
| 500 |
m_thread = std::thread([&] { Process(); }); |
| 501 |
|
| 502 |
m_BufferThreadActive = true; |
| 503 |
} |
| 504 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::StartBufferThread done"); |
| 505 |
} |
| 506 |
|
| 507 |
void CRTSPClient::StopBufferThread() |
| 508 |
{ |
| 509 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::StopBufferThread"); |
| 510 |
m_running = false; |
| 511 |
if (!m_BufferThreadActive) |
| 512 |
return; |
| 513 |
|
| 514 |
if (m_thread.joinable()) |
| 515 |
m_thread.join(); |
| 516 |
|
| 517 |
m_BufferThreadActive = false; |
| 518 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::StopBufferThread done"); |
| 519 |
} |
| 520 |
|
| 521 |
bool CRTSPClient::IsRunning() |
| 522 |
{ |
| 523 |
return m_BufferThreadActive; |
| 524 |
} |
| 525 |
|
| 526 |
long CRTSPClient::Duration() |
| 527 |
{ |
| 528 |
return m_duration; |
| 529 |
} |
| 530 |
|
| 531 |
void CRTSPClient::FillBuffer(unsigned long byteCount) |
| 532 |
{ |
| 533 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::Fillbuffer...%d\n", byteCount); |
| 534 |
unsigned long long tickCount = GetTickCount64(); |
| 535 |
|
| 536 |
while ( IsRunning() && m_buffer->Size() < byteCount) |
| 537 |
{ |
| 538 |
std::this_thread::sleep_for(std::chrono::milliseconds(5)); |
| 539 |
if (GetTickCount64() - tickCount > 3000) |
| 540 |
break; |
| 541 |
} |
| 542 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::Fillbuffer...%d/%d\n", byteCount, m_buffer->Size() ); |
| 543 |
} |
| 544 |
|
| 545 |
void CRTSPClient::Process() |
| 546 |
{ |
| 547 |
m_BufferThreadActive = true; |
| 548 |
|
| 549 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient:: thread started"); |
| 550 |
|
| 551 |
while (m_env != NULL && m_running) |
| 552 |
{ |
| 553 |
m_env->taskScheduler().doEventLoop(); |
| 554 |
if (m_running == false) |
| 555 |
break; |
| 556 |
} |
| 557 |
|
| 558 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient:: thread stopped"); |
| 559 |
|
| 560 |
m_BufferThreadActive = false; |
| 561 |
} |
| 562 |
|
| 563 |
void CRTSPClient::Continue() |
| 564 |
{ |
| 565 |
if (m_ourClient != NULL && m_session != NULL) |
| 566 |
{ |
| 567 |
RTSPClient* rtspClient = (RTSPClient*) m_ourClient; |
| 568 |
rtspClient->playMediaSession(*m_session, -1.0); |
| 569 |
StartBufferThread(); |
| 570 |
m_bPaused = false; |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
bool CRTSPClient::IsPaused() |
| 575 |
{ |
| 576 |
return m_bPaused; |
| 577 |
} |
| 578 |
|
| 579 |
bool CRTSPClient::Pause() |
| 580 |
{ |
| 581 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::Pause()"); |
| 582 |
if (m_ourClient != NULL && m_session != NULL) |
| 583 |
{ |
| 584 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::Pause() stopthread"); |
| 585 |
// Ambass : sometimes 100mS ( prev value ) is not enough and thread is not stopped. |
| 586 |
// now stopping takes around 5 secs ?!?! why ???? |
| 587 |
m_running = false; |
| 588 |
if (m_thread.joinable()) |
| 589 |
m_thread.join(); |
| 590 |
|
| 591 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::Pause() thread stopped"); |
| 592 |
RTSPClient* rtspClient=(RTSPClient*)m_ourClient; |
| 593 |
rtspClient->pauseMediaSession(*m_session); |
| 594 |
m_bPaused = true; |
| 595 |
} |
| 596 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::Pause() done"); |
| 597 |
|
| 598 |
return true; |
| 599 |
} |
| 600 |
bool CRTSPClient::Play(double fStart,double fDuration) |
| 601 |
{ |
| 602 |
kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::Play from %f / %f", (float)fStart, (float)fDuration); |
| 603 |
|
| 604 |
m_bPaused = false; |
| 605 |
m_fStart = fStart; |
| 606 |
m_fDuration = fDuration; |
| 607 |
|
| 608 |
if (m_BufferThreadActive) |
| 609 |
{ |
| 610 |
Stop(); |
| 611 |
m_buffer->Clear(); |
| 612 |
if (Initialize(m_buffer) == false) |
| 613 |
{ |
| 614 |
shutdown(); |
| 615 |
return false; |
| 616 |
} |
| 617 |
if (OpenStream(m_url) == false) |
| 618 |
{ |
| 619 |
shutdown(); |
| 620 |
return false; |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
if (m_ourClient == NULL || m_session == NULL) |
| 625 |
{ |
| 626 |
m_buffer->Clear(); |
| 627 |
if (Initialize(m_buffer) == false) |
| 628 |
{ |
| 629 |
shutdown(); |
| 630 |
return false; |
| 631 |
} |
| 632 |
if (OpenStream(m_url) == false) |
| 633 |
{ |
| 634 |
shutdown(); |
| 635 |
return false; |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
if (!startPlayingStreams()) |
| 640 |
{ |
| 641 |
shutdown(); |
| 642 |
return false; |
| 643 |
} |
| 644 |
StartBufferThread(); |
| 645 |
|
| 646 |
return true; |
| 647 |
} |
| 648 |
|
| 649 |
bool CRTSPClient::UpdateDuration() |
| 650 |
{ |
| 651 |
char* sdpDescription = getSDPDescription(); |
| 652 |
if (sdpDescription == NULL) |
| 653 |
{ |
| 654 |
kodi::Log(ADDON_LOG_ERROR, "UpdateStreamDuration: Failed to get a SDP description from URL %s %s", m_url, m_env->getResultMsg() ); |
| 655 |
return false; |
| 656 |
} |
| 657 |
//kodi::Log(ADDON_LOG_DEBUG, "Opened URL %s %s",url,sdpDescription); |
| 658 |
|
| 659 |
char* range = strstr(sdpDescription, "a=range:npt="); |
| 660 |
if (range != NULL) |
| 661 |
{ |
| 662 |
char *pStart = range + strlen("a=range:npt="); |
| 663 |
char *pEnd = strstr(range,"-"); |
| 664 |
|
| 665 |
if (pEnd != NULL) |
| 666 |
{ |
| 667 |
pEnd++; |
| 668 |
double Start = atof(pStart); |
| 669 |
double End = atof(pEnd); |
| 670 |
|
| 671 |
//kodi::Log(ADDON_LOG_DEBUG, "rangestart:%f rangeend:%f", Start,End); |
| 672 |
m_duration = (long) ((End-Start)*1000.0); |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
return true; |
| 677 |
} |
| 678 |
#endif //LIVE555 |