blob: f2bb896f3564b6b4b316b04bbc3fa09d35864eec [file] [log] [blame]
Byeongjo Parkd157b792019-01-24 20:56:37 +09001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "RTPSource"
19#include <utils/Log.h>
20
21#include "RTPSource.h"
22
23
24
25
26#include <media/stagefright/MediaDefs.h>
27#include <media/stagefright/MetaData.h>
28#include <string.h>
29
30namespace android {
31
32const int64_t kNearEOSTimeoutUs = 2000000ll; // 2 secs
33static int32_t kMaxAllowedStaleAccessUnits = 20;
34
35NuPlayer::RTPSource::RTPSource(
36 const sp<AMessage> &notify,
37 const String8& rtpParams)
38 : Source(notify),
39 mRTPParams(rtpParams),
40 mFlags(0),
41 mState(DISCONNECTED),
42 mFinalResult(OK),
43 mBuffering(false),
44 mInPreparationPhase(true),
Kim Sungyeon2c987f12019-03-17 22:04:14 +090045 mRTPConn(new ARTPConnection(ARTPConnection::kViLTEConnection)),
Byeongjo Parkd157b792019-01-24 20:56:37 +090046 mEOSTimeoutAudio(0),
Byeongjo Parkb0225aa2018-04-20 14:00:15 +090047 mEOSTimeoutVideo(0),
48 mLastCVOUpdated(-1) {
Byeongjo Parkd157b792019-01-24 20:56:37 +090049 ALOGD("RTPSource initialized with rtpParams=%s", rtpParams.string());
50}
51
52NuPlayer::RTPSource::~RTPSource() {
53 if (mLooper != NULL) {
54 mLooper->unregisterHandler(id());
55 mLooper->unregisterHandler(mRTPConn->id());
56 mLooper->stop();
57 }
58}
59
60status_t NuPlayer::RTPSource::getBufferingSettings(
61 BufferingSettings* buffering /* nonnull */) {
62 Mutex::Autolock _l(mBufferingSettingsLock);
63 *buffering = mBufferingSettings;
64 return OK;
65}
66
67status_t NuPlayer::RTPSource::setBufferingSettings(const BufferingSettings& buffering) {
68 Mutex::Autolock _l(mBufferingSettingsLock);
69 mBufferingSettings = buffering;
70 return OK;
71}
72
73void NuPlayer::RTPSource::prepareAsync() {
74 if (mLooper == NULL) {
75 mLooper = new ALooper;
76 mLooper->setName("rtp");
77 mLooper->start();
78
79 mLooper->registerHandler(this);
80 mLooper->registerHandler(mRTPConn);
81 }
82
Lajos Molnar19829332020-03-17 08:13:10 -070083 CHECK_EQ(mState, (int)DISCONNECTED);
84 mState = CONNECTING;
85
Byeongjo Parkd157b792019-01-24 20:56:37 +090086 setParameters(mRTPParams);
87
88 TrackInfo *info = NULL;
89 unsigned i;
90 for (i = 0; i < mTracks.size(); i++) {
91 info = &mTracks.editItemAt(i);
92
93 if (info == NULL)
94 break;
95
96 AString sdp;
97 ASessionDescription::SDPStringFactory(sdp, info->mLocalIp,
98 info->mIsAudio, info->mLocalPort, info->mPayloadType, info->mAS, info->mCodecName,
Byeongjo Parkb0225aa2018-04-20 14:00:15 +090099 NULL, info->mWidth, info->mHeight, info->mCVOExtMap);
Byeongjo Parkd157b792019-01-24 20:56:37 +0900100 ALOGD("RTPSource SDP =>\n%s", sdp.c_str());
101
102 sp<ASessionDescription> desc = new ASessionDescription;
103 bool isValidSdp = desc->setTo(sdp.c_str(), sdp.size());
104 ALOGV("RTPSource isValidSdp => %d", isValidSdp);
105
106 int sockRtp, sockRtcp;
107 ARTPConnection::MakeRTPSocketPair(&sockRtp, &sockRtcp, info->mLocalIp, info->mRemoteIp,
Byeongjo Park88068dc2019-03-29 15:04:10 +0900108 info->mLocalPort, info->mRemotePort, info->mSocketNetwork);
Byeongjo Parkd157b792019-01-24 20:56:37 +0900109
110 sp<AMessage> notify = new AMessage('accu', this);
111
112 ALOGV("RTPSource addStream. track-index=%d", i);
113 notify->setSize("trackIndex", i);
114 // index(i) should be started from 1. 0 is reserved for [root]
115 mRTPConn->addStream(sockRtp, sockRtcp, desc, i + 1, notify, false);
Kim Sungyeond2875e92018-03-20 16:51:41 +0900116 mRTPConn->setSelfID(info->mSelfID);
Byeongjo Park7143c072019-09-23 13:31:37 +0900117 mRTPConn->setJbTime((info->mJbTime <= 3000 && info->mJbTime >= 40) ? info->mJbTime : 300);
Byeongjo Parkd157b792019-01-24 20:56:37 +0900118
119 info->mRTPSocket = sockRtp;
120 info->mRTCPSocket = sockRtcp;
121 info->mFirstSeqNumInSegment = 0;
122 info->mNewSegment = true;
123 info->mAllowedStaleAccessUnits = kMaxAllowedStaleAccessUnits;
124 info->mRTPAnchor = 0;
125 info->mNTPAnchorUs = -1;
126 info->mNormalPlayTimeRTP = 0;
127 info->mNormalPlayTimeUs = 0ll;
128
129 // index(i) should be started from 1. 0 is reserved for [root]
130 info->mPacketSource = new APacketSource(desc, i + 1);
131
132 int32_t timeScale;
133 sp<MetaData> format = getTrackFormat(i, &timeScale);
134 sp<AnotherPacketSource> source = new AnotherPacketSource(format);
135
136 if (info->mIsAudio) {
137 mAudioTrack = source;
138 } else {
139 mVideoTrack = source;
140 }
141
142 info->mSource = source;
143 }
144
Byeongjo Parkd157b792019-01-24 20:56:37 +0900145 if (mInPreparationPhase) {
146 mInPreparationPhase = false;
147 notifyPrepared();
148 }
149}
150
151void NuPlayer::RTPSource::start() {
152}
153
154void NuPlayer::RTPSource::pause() {
155 mState = PAUSED;
156}
157
158void NuPlayer::RTPSource::resume() {
159 mState = CONNECTING;
160}
161
162void NuPlayer::RTPSource::stop() {
163 if (mLooper == NULL) {
164 return;
165 }
166 sp<AMessage> msg = new AMessage(kWhatDisconnect, this);
167
168 sp<AMessage> dummy;
169 msg->postAndAwaitResponse(&dummy);
170}
171
172status_t NuPlayer::RTPSource::feedMoreTSData() {
173 Mutex::Autolock _l(mBufferingLock);
174 return mFinalResult;
175}
176
177sp<MetaData> NuPlayer::RTPSource::getFormatMeta(bool audio) {
178 sp<AnotherPacketSource> source = getSource(audio);
179
180 if (source == NULL) {
181 return NULL;
182 }
183
184 return source->getFormat();
185}
186
187bool NuPlayer::RTPSource::haveSufficientDataOnAllTracks() {
188 // We're going to buffer at least 2 secs worth data on all tracks before
189 // starting playback (both at startup and after a seek).
190
191 static const int64_t kMinDurationUs = 2000000ll;
192
193 int64_t mediaDurationUs = 0;
194 getDuration(&mediaDurationUs);
195 if ((mAudioTrack != NULL && mAudioTrack->isFinished(mediaDurationUs))
196 || (mVideoTrack != NULL && mVideoTrack->isFinished(mediaDurationUs))) {
197 return true;
198 }
199
200 status_t err;
201 int64_t durationUs;
202 if (mAudioTrack != NULL
203 && (durationUs = mAudioTrack->getBufferedDurationUs(&err))
204 < kMinDurationUs
205 && err == OK) {
206 ALOGV("audio track doesn't have enough data yet. (%.2f secs buffered)",
207 durationUs / 1E6);
208 return false;
209 }
210
211 if (mVideoTrack != NULL
212 && (durationUs = mVideoTrack->getBufferedDurationUs(&err))
213 < kMinDurationUs
214 && err == OK) {
215 ALOGV("video track doesn't have enough data yet. (%.2f secs buffered)",
216 durationUs / 1E6);
217 return false;
218 }
219
220 return true;
221}
222
223status_t NuPlayer::RTPSource::dequeueAccessUnit(
224 bool audio, sp<ABuffer> *accessUnit) {
225
226 sp<AnotherPacketSource> source = getSource(audio);
227
228 if (mState == PAUSED) {
229 ALOGV("-EWOULDBLOCK");
230 return -EWOULDBLOCK;
231 }
232
233 status_t finalResult;
234 if (!source->hasBufferAvailable(&finalResult)) {
235 if (finalResult == OK) {
236 int64_t mediaDurationUs = 0;
237 getDuration(&mediaDurationUs);
238 sp<AnotherPacketSource> otherSource = getSource(!audio);
239 status_t otherFinalResult;
240
241 // If other source already signaled EOS, this source should also signal EOS
242 if (otherSource != NULL &&
243 !otherSource->hasBufferAvailable(&otherFinalResult) &&
244 otherFinalResult == ERROR_END_OF_STREAM) {
245 source->signalEOS(ERROR_END_OF_STREAM);
246 return ERROR_END_OF_STREAM;
247 }
248
249 // If this source has detected near end, give it some time to retrieve more
250 // data before signaling EOS
251 if (source->isFinished(mediaDurationUs)) {
252 int64_t eosTimeout = audio ? mEOSTimeoutAudio : mEOSTimeoutVideo;
253 if (eosTimeout == 0) {
254 setEOSTimeout(audio, ALooper::GetNowUs());
255 } else if ((ALooper::GetNowUs() - eosTimeout) > kNearEOSTimeoutUs) {
256 setEOSTimeout(audio, 0);
257 source->signalEOS(ERROR_END_OF_STREAM);
258 return ERROR_END_OF_STREAM;
259 }
260 return -EWOULDBLOCK;
261 }
262
263 if (!(otherSource != NULL && otherSource->isFinished(mediaDurationUs))) {
264 // We should not enter buffering mode
265 // if any of the sources already have detected EOS.
266 // TODO: needs to be checked whether below line is needed or not.
267 // startBufferingIfNecessary();
268 }
269
270 return -EWOULDBLOCK;
271 }
272 return finalResult;
273 }
274
275 setEOSTimeout(audio, 0);
276
Byeongjo Parkb0225aa2018-04-20 14:00:15 +0900277 finalResult = source->dequeueAccessUnit(accessUnit);
278 if (finalResult != OK) {
279 return finalResult;
280 }
281
282 int32_t cvo;
Byeongjo Park0aedeb32019-05-20 13:15:04 +0900283 if ((*accessUnit) != NULL && (*accessUnit)->meta()->findInt32("cvo", &cvo) &&
284 cvo != mLastCVOUpdated) {
285 sp<AMessage> msg = new AMessage();
286 msg->setInt32("payload-type", NuPlayer::RTPSource::RTP_CVO);
287 msg->setInt32("cvo", cvo);
Byeongjo Parkb0225aa2018-04-20 14:00:15 +0900288
Byeongjo Park0aedeb32019-05-20 13:15:04 +0900289 sp<AMessage> notify = dupNotify();
290 notify->setInt32("what", kWhatIMSRxNotice);
291 notify->setMessage("message", msg);
292 notify->post();
Byeongjo Parkb0225aa2018-04-20 14:00:15 +0900293
Byeongjo Park0aedeb32019-05-20 13:15:04 +0900294 ALOGV("notify cvo updated (%d)->(%d) to upper layer", mLastCVOUpdated, cvo);
295 mLastCVOUpdated = cvo;
Byeongjo Parkb0225aa2018-04-20 14:00:15 +0900296 }
297
298 return finalResult;
Byeongjo Parkd157b792019-01-24 20:56:37 +0900299}
300
301sp<AnotherPacketSource> NuPlayer::RTPSource::getSource(bool audio) {
302 return audio ? mAudioTrack : mVideoTrack;
303}
304
305void NuPlayer::RTPSource::setEOSTimeout(bool audio, int64_t timeout) {
306 if (audio) {
307 mEOSTimeoutAudio = timeout;
308 } else {
309 mEOSTimeoutVideo = timeout;
310 }
311}
312
313status_t NuPlayer::RTPSource::getDuration(int64_t *durationUs) {
314 *durationUs = 0ll;
315
316 int64_t audioDurationUs;
317 if (mAudioTrack != NULL
318 && mAudioTrack->getFormat()->findInt64(
319 kKeyDuration, &audioDurationUs)
320 && audioDurationUs > *durationUs) {
321 *durationUs = audioDurationUs;
322 }
323
324 int64_t videoDurationUs;
325 if (mVideoTrack != NULL
326 && mVideoTrack->getFormat()->findInt64(
327 kKeyDuration, &videoDurationUs)
328 && videoDurationUs > *durationUs) {
329 *durationUs = videoDurationUs;
330 }
331
332 return OK;
333}
334
335status_t NuPlayer::RTPSource::seekTo(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
336 ALOGV("RTPSource::seekTo=%d, mode=%d", (int)seekTimeUs, mode);
337 return OK;
338}
339
340void NuPlayer::RTPSource::schedulePollBuffering() {
341 sp<AMessage> msg = new AMessage(kWhatPollBuffering, this);
Lajos Molnar19829332020-03-17 08:13:10 -0700342 msg->post(kBufferingPollIntervalUs); // 1 second intervals
Byeongjo Parkd157b792019-01-24 20:56:37 +0900343}
344
345void NuPlayer::RTPSource::onPollBuffering() {
346 schedulePollBuffering();
347}
348
349void NuPlayer::RTPSource::onMessageReceived(const sp<AMessage> &msg) {
350 ALOGV("onMessageReceived =%d", msg->what());
351
352 switch (msg->what()) {
353 case kWhatAccessUnitComplete:
354 {
355 if (mState == CONNECTING) {
356 mState = CONNECTED;
357 }
358
359 int32_t timeUpdate;
360 //"time-update" raised from ARTPConnection::parseSR()
361 if (msg->findInt32("time-update", &timeUpdate) && timeUpdate) {
362 size_t trackIndex;
363 CHECK(msg->findSize("trackIndex", &trackIndex));
364
365 uint32_t rtpTime;
366 uint64_t ntpTime;
367 CHECK(msg->findInt32("rtp-time", (int32_t *)&rtpTime));
368 CHECK(msg->findInt64("ntp-time", (int64_t *)&ntpTime));
369
370 onTimeUpdate(trackIndex, rtpTime, ntpTime);
371 break;
372 }
373
374 int32_t firstRTCP;
375 if (msg->findInt32("first-rtcp", &firstRTCP)) {
376 // There won't be an access unit here, it's just a notification
377 // that the data communication worked since we got the first
378 // rtcp packet.
379 ALOGV("first-rtcp");
380 break;
381 }
382
Kim Sungyeond3c6b322018-03-02 14:41:19 +0900383 int32_t IMSRxNotice;
Byeongjo Park73f6fc82018-08-23 16:19:58 +0900384 if (msg->findInt32("rtcp-event", &IMSRxNotice)) {
Kim Sungyeond3c6b322018-03-02 14:41:19 +0900385 int32_t payloadType, feedbackType;
386 CHECK(msg->findInt32("payload-type", &payloadType));
387 CHECK(msg->findInt32("feedback-type", &feedbackType));
388
389 sp<AMessage> notify = dupNotify();
390 notify->setInt32("what", kWhatIMSRxNotice);
391 notify->setMessage("message", msg);
392 notify->post();
393
394 ALOGV("IMSRxNotice \t\t payload : %d feedback : %d",
395 payloadType, feedbackType);
396 break;
397 }
398
Byeongjo Parkd157b792019-01-24 20:56:37 +0900399 size_t trackIndex;
400 CHECK(msg->findSize("trackIndex", &trackIndex));
401
402 sp<ABuffer> accessUnit;
403 if (msg->findBuffer("access-unit", &accessUnit) == false) {
404 break;
405 }
406
407 int32_t damaged;
408 if (accessUnit->meta()->findInt32("damaged", &damaged)
409 && damaged) {
410 ALOGD("dropping damaged access unit.");
411 break;
412 }
413
Lajos Molnar19829332020-03-17 08:13:10 -0700414 // Implicitly assert on valid trackIndex here, which we ensure by
415 // never removing tracks.
Byeongjo Parkd157b792019-01-24 20:56:37 +0900416 TrackInfo *info = &mTracks.editItemAt(trackIndex);
417
418 sp<AnotherPacketSource> source = info->mSource;
419 if (source != NULL) {
420 uint32_t rtpTime;
421 CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
422
423 /* AnotherPacketSource make an assertion if there is no ntp provided
424 RTPSource should provide ntpUs all the times.
425 if (!info->mNPTMappingValid) {
426 // This is a live stream, we didn't receive any normal
427 // playtime mapping. We won't map to npt time.
428 source->queueAccessUnit(accessUnit);
429 break;
430 }
431 */
432
433 int64_t nptUs =
434 ((double)rtpTime - (double)info->mRTPTime)
435 / info->mTimeScale
436 * 1000000ll
437 + info->mNormalPlaytimeUs;
438
439 accessUnit->meta()->setInt64("timeUs", nptUs);
440
441 source->queueAccessUnit(accessUnit);
442 }
443
444 break;
445 }
446 case kWhatDisconnect:
447 {
448 sp<AReplyToken> replyID;
449 CHECK(msg->senderAwaitsResponse(&replyID));
450
451 for (size_t i = 0; i < mTracks.size(); ++i) {
452 TrackInfo *info = &mTracks.editItemAt(i);
453
454 if (info->mIsAudio) {
455 mAudioTrack->signalEOS(ERROR_END_OF_STREAM);
456 mAudioTrack = NULL;
457 ALOGV("mAudioTrack disconnected");
458 } else {
459 mVideoTrack->signalEOS(ERROR_END_OF_STREAM);
460 mVideoTrack = NULL;
461 ALOGV("mVideoTrack disconnected");
462 }
463
464 mRTPConn->removeStream(info->mRTPSocket, info->mRTCPSocket);
465 close(info->mRTPSocket);
466 close(info->mRTCPSocket);
467 }
468
469 mTracks.clear();
470 mFirstAccessUnit = true;
471 mAllTracksHaveTime = false;
472 mNTPAnchorUs = -1;
473 mMediaAnchorUs = -1;
474 mLastMediaTimeUs = -1;
475 mNumAccessUnitsReceived = 0;
476 mReceivedFirstRTCPPacket = false;
477 mReceivedFirstRTPPacket = false;
478 mPausing = false;
479 mPauseGeneration = 0;
480
481 (new AMessage)->postReply(replyID);
482
483 break;
484 }
485 case kWhatPollBuffering:
486 break;
487 default:
488 TRESPASS();
489 }
490}
491
Kim Sungyeon23b23932019-07-18 17:48:32 +0900492void NuPlayer::RTPSource::setTargetBitrate(int32_t bitrate) {
493 mRTPConn->setTargetBitrate(bitrate);
494}
495
Byeongjo Parkd157b792019-01-24 20:56:37 +0900496void NuPlayer::RTPSource::onTimeUpdate(int32_t trackIndex, uint32_t rtpTime, uint64_t ntpTime) {
497 ALOGV("onTimeUpdate track %d, rtpTime = 0x%08x, ntpTime = %#016llx",
498 trackIndex, rtpTime, (long long)ntpTime);
499
Lajos Molnar19829332020-03-17 08:13:10 -0700500 // convert ntpTime in Q32 seconds to microseconds. Note: this will not lose precision
501 // because ntpTimeUs is at most 52 bits (double holds 53 bits)
Byeongjo Parkd157b792019-01-24 20:56:37 +0900502 int64_t ntpTimeUs = (int64_t)(ntpTime * 1E6 / (1ll << 32));
503
504 TrackInfo *track = &mTracks.editItemAt(trackIndex);
505
506 track->mRTPAnchor = rtpTime;
507 track->mNTPAnchorUs = ntpTimeUs;
508
509 if (mNTPAnchorUs < 0) {
510 mNTPAnchorUs = ntpTimeUs;
511 mMediaAnchorUs = mLastMediaTimeUs;
512 }
513
514 if (!mAllTracksHaveTime) {
515 bool allTracksHaveTime = (mTracks.size() > 0);
516 for (size_t i = 0; i < mTracks.size(); ++i) {
517 TrackInfo *track = &mTracks.editItemAt(i);
518 if (track->mNTPAnchorUs < 0) {
519 allTracksHaveTime = false;
520 break;
521 }
522 }
523 if (allTracksHaveTime) {
524 mAllTracksHaveTime = true;
525 ALOGI("Time now established for all tracks.");
526 }
527 }
528 if (mAllTracksHaveTime && dataReceivedOnAllChannels()) {
529 // Time is now established, lets start timestamping immediately
530 for (size_t i = 0; i < mTracks.size(); ++i) {
531 TrackInfo *trackInfo = &mTracks.editItemAt(i);
532 while (!trackInfo->mPackets.empty()) {
533 sp<ABuffer> accessUnit = *trackInfo->mPackets.begin();
534 trackInfo->mPackets.erase(trackInfo->mPackets.begin());
535
536 if (addMediaTimestamp(i, trackInfo, accessUnit)) {
537 postQueueAccessUnit(i, accessUnit);
538 }
539 }
540 }
541 }
542}
543
544bool NuPlayer::RTPSource::addMediaTimestamp(
545 int32_t trackIndex, const TrackInfo *track,
546 const sp<ABuffer> &accessUnit) {
547
548 uint32_t rtpTime;
549 CHECK(accessUnit->meta()->findInt32(
550 "rtp-time", (int32_t *)&rtpTime));
551
552 int64_t relRtpTimeUs =
553 (((int64_t)rtpTime - (int64_t)track->mRTPAnchor) * 1000000ll)
554 / track->mTimeScale;
555
556 int64_t ntpTimeUs = track->mNTPAnchorUs + relRtpTimeUs;
557
558 int64_t mediaTimeUs = mMediaAnchorUs + ntpTimeUs - mNTPAnchorUs;
559
560 if (mediaTimeUs > mLastMediaTimeUs) {
561 mLastMediaTimeUs = mediaTimeUs;
562 }
563
564 if (mediaTimeUs < 0) {
565 ALOGV("dropping early accessUnit.");
566 return false;
567 }
568
569 ALOGV("track %d rtpTime=%u mediaTimeUs = %lld us (%.2f secs)",
570 trackIndex, rtpTime, (long long)mediaTimeUs, mediaTimeUs / 1E6);
571
572 accessUnit->meta()->setInt64("timeUs", mediaTimeUs);
573
574 return true;
575}
576
577bool NuPlayer::RTPSource::dataReceivedOnAllChannels() {
578 TrackInfo *track;
579 for (size_t i = 0; i < mTracks.size(); ++i) {
580 track = &mTracks.editItemAt(i);
581 if (track->mPackets.empty()) {
582 return false;
583 }
584 }
585 return true;
586}
587
588void NuPlayer::RTPSource::postQueueAccessUnit(
589 size_t trackIndex, const sp<ABuffer> &accessUnit) {
590 sp<AMessage> msg = new AMessage(kWhatAccessUnit, this);
591 msg->setInt32("what", kWhatAccessUnit);
592 msg->setSize("trackIndex", trackIndex);
593 msg->setBuffer("accessUnit", accessUnit);
594 msg->post();
595}
596
597void NuPlayer::RTPSource::postQueueEOS(size_t trackIndex, status_t finalResult) {
598 sp<AMessage> msg = new AMessage(kWhatEOS, this);
599 msg->setInt32("what", kWhatEOS);
600 msg->setSize("trackIndex", trackIndex);
601 msg->setInt32("finalResult", finalResult);
602 msg->post();
603}
604
605sp<MetaData> NuPlayer::RTPSource::getTrackFormat(size_t index, int32_t *timeScale) {
606 CHECK_GE(index, 0u);
607 CHECK_LT(index, mTracks.size());
608
609 const TrackInfo &info = mTracks.itemAt(index);
610
611 *timeScale = info.mTimeScale;
612
613 return info.mPacketSource->getFormat();
614}
615
616void NuPlayer::RTPSource::onConnected() {
617 ALOGV("onConnected");
618 mState = CONNECTED;
619}
620
621void NuPlayer::RTPSource::onDisconnected(const sp<AMessage> &msg) {
622 if (mState == DISCONNECTED) {
623 return;
624 }
625
626 status_t err;
627 CHECK(msg->findInt32("result", &err));
628 CHECK_NE(err, (status_t)OK);
629
630// mLooper->unregisterHandler(mHandler->id());
631// mHandler.clear();
632
633 if (mState == CONNECTING) {
634 // We're still in the preparation phase, signal that it
635 // failed.
636 notifyPrepared(err);
637 }
638
639 mState = DISCONNECTED;
640// setError(err);
641
642}
643
644status_t NuPlayer::RTPSource::setParameter(const String8 &key, const String8 &value) {
645 ALOGV("setParameter: key (%s) => value (%s)", key.string(), value.string());
646
647 bool isAudioKey = key.contains("audio");
648 TrackInfo *info = NULL;
649 for (unsigned i = 0; i < mTracks.size(); ++i) {
650 info = &mTracks.editItemAt(i);
651 if (info != NULL && info->mIsAudio == isAudioKey) {
652 ALOGV("setParameter: %s track (%d) found", isAudioKey ? "audio" : "video" , i);
653 break;
654 }
655 }
656
657 if (info == NULL) {
658 TrackInfo newTrackInfo;
659 newTrackInfo.mIsAudio = isAudioKey;
660 mTracks.push(newTrackInfo);
661 info = &mTracks.editTop();
Byeongjo Park7143c072019-09-23 13:31:37 +0900662 info->mJbTime = 300;
Byeongjo Parkd157b792019-01-24 20:56:37 +0900663 }
664
665 if (key == "rtp-param-mime-type") {
666 info->mMimeType = value;
667
668 const char *mime = value.string();
669 const char *delimiter = strchr(mime, '/');
Lajos Molnar19829332020-03-17 08:13:10 -0700670 info->mCodecName = delimiter ? (delimiter + 1) : "<none>";
Byeongjo Parkd157b792019-01-24 20:56:37 +0900671
672 ALOGV("rtp-param-mime-type: mMimeType (%s) => mCodecName (%s)",
Lajos Molnar19829332020-03-17 08:13:10 -0700673 info->mMimeType.string(), info->mCodecName.string());
Byeongjo Parkd157b792019-01-24 20:56:37 +0900674 } else if (key == "video-param-decoder-profile") {
675 info->mCodecProfile = atoi(value);
676 } else if (key == "video-param-decoder-level") {
677 info->mCodecLevel = atoi(value);
678 } else if (key == "video-param-width") {
679 info->mWidth = atoi(value);
680 } else if (key == "video-param-height") {
681 info->mHeight = atoi(value);
682 } else if (key == "rtp-param-local-ip") {
683 info->mLocalIp = value;
684 } else if (key == "rtp-param-local-port") {
685 info->mLocalPort = atoi(value);
686 } else if (key == "rtp-param-remote-ip") {
687 info->mRemoteIp = value;
688 } else if (key == "rtp-param-remote-port") {
689 info->mRemotePort = atoi(value);
690 } else if (key == "rtp-param-payload-type") {
691 info->mPayloadType = atoi(value);
692 } else if (key == "rtp-param-as") {
693 //AS means guaranteed bit rate that negotiated from sdp.
694 info->mAS = atoi(value);
695 } else if (key == "rtp-param-rtp-timeout") {
696 } else if (key == "rtp-param-rtcp-timeout") {
697 } else if (key == "rtp-param-time-scale") {
Kim Sungyeond2875e92018-03-20 16:51:41 +0900698 } else if (key == "rtp-param-self-id") {
699 info->mSelfID = atoi(value);
Byeongjo Parkb0225aa2018-04-20 14:00:15 +0900700 } else if (key == "rtp-param-ext-cvo-extmap") {
701 info->mCVOExtMap = atoi(value);
Byeongjo Park88068dc2019-03-29 15:04:10 +0900702 } else if (key == "rtp-param-set-socket-network") {
703 int64_t networkHandle = atoll(value);
704 setSocketNetwork(networkHandle);
Byeongjo Park7143c072019-09-23 13:31:37 +0900705 } else if (key == "rtp-param-jitter-buffer-time") {
706 info->mJbTime = atoi(value);
Byeongjo Parkd157b792019-01-24 20:56:37 +0900707 }
708
709 return OK;
710}
711
712status_t NuPlayer::RTPSource::setParameters(const String8 &params) {
713 ALOGV("setParameters: %s", params.string());
714 const char *cparams = params.string();
715 const char *key_start = cparams;
716 for (;;) {
717 const char *equal_pos = strchr(key_start, '=');
718 if (equal_pos == NULL) {
719 ALOGE("Parameters %s miss a value", cparams);
720 return BAD_VALUE;
721 }
722 String8 key(key_start, equal_pos - key_start);
723 TrimString(&key);
724 if (key.length() == 0) {
725 ALOGE("Parameters %s contains an empty key", cparams);
726 return BAD_VALUE;
727 }
728 const char *value_start = equal_pos + 1;
729 const char *semicolon_pos = strchr(value_start, ';');
730 String8 value;
731 if (semicolon_pos == NULL) {
732 value.setTo(value_start);
733 } else {
734 value.setTo(value_start, semicolon_pos - value_start);
735 }
736 if (setParameter(key, value) != OK) {
737 return BAD_VALUE;
738 }
739 if (semicolon_pos == NULL) {
740 break; // Reaches the end
741 }
742 key_start = semicolon_pos + 1;
743 }
744 return OK;
745}
746
Byeongjo Park88068dc2019-03-29 15:04:10 +0900747void NuPlayer::RTPSource::setSocketNetwork(int64_t networkHandle) {
748 ALOGV("setSocketNetwork: %llu", (unsigned long long)networkHandle);
749
750 TrackInfo *info = NULL;
751 for (size_t i = 0; i < mTracks.size(); ++i) {
752 info = &mTracks.editItemAt(i);
753
754 if (info == NULL)
755 break;
756
757 info->mSocketNetwork = networkHandle;
758 }
759}
760
Byeongjo Parkd157b792019-01-24 20:56:37 +0900761// Trim both leading and trailing whitespace from the given string.
762//static
763void NuPlayer::RTPSource::TrimString(String8 *s) {
764 size_t num_bytes = s->bytes();
765 const char *data = s->string();
766
767 size_t leading_space = 0;
768 while (leading_space < num_bytes && isspace(data[leading_space])) {
769 ++leading_space;
770 }
771
772 size_t i = num_bytes;
773 while (i > leading_space && isspace(data[i - 1])) {
774 --i;
775 }
776
777 s->setTo(String8(&data[leading_space], i - leading_space));
778}
779
780} // namespace android