blob: 57b6c59368db36cb917c9889807d55387c074e16 [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
83 setParameters(mRTPParams);
84
85 TrackInfo *info = NULL;
86 unsigned i;
87 for (i = 0; i < mTracks.size(); i++) {
88 info = &mTracks.editItemAt(i);
89
90 if (info == NULL)
91 break;
92
93 AString sdp;
94 ASessionDescription::SDPStringFactory(sdp, info->mLocalIp,
95 info->mIsAudio, info->mLocalPort, info->mPayloadType, info->mAS, info->mCodecName,
Byeongjo Parkb0225aa2018-04-20 14:00:15 +090096 NULL, info->mWidth, info->mHeight, info->mCVOExtMap);
Byeongjo Parkd157b792019-01-24 20:56:37 +090097 ALOGD("RTPSource SDP =>\n%s", sdp.c_str());
98
99 sp<ASessionDescription> desc = new ASessionDescription;
100 bool isValidSdp = desc->setTo(sdp.c_str(), sdp.size());
101 ALOGV("RTPSource isValidSdp => %d", isValidSdp);
102
103 int sockRtp, sockRtcp;
104 ARTPConnection::MakeRTPSocketPair(&sockRtp, &sockRtcp, info->mLocalIp, info->mRemoteIp,
105 info->mLocalPort, info->mRemotePort);
106
107 sp<AMessage> notify = new AMessage('accu', this);
108
109 ALOGV("RTPSource addStream. track-index=%d", i);
110 notify->setSize("trackIndex", i);
111 // index(i) should be started from 1. 0 is reserved for [root]
112 mRTPConn->addStream(sockRtp, sockRtcp, desc, i + 1, notify, false);
Kim Sungyeond2875e92018-03-20 16:51:41 +0900113 mRTPConn->setSelfID(info->mSelfID);
Kim Sungyeon2c987f12019-03-17 22:04:14 +0900114 mRTPConn->setMinMaxBitrate(videoMinBitrate, info->mAS * 1000);
Byeongjo Parkd157b792019-01-24 20:56:37 +0900115
116 info->mRTPSocket = sockRtp;
117 info->mRTCPSocket = sockRtcp;
118 info->mFirstSeqNumInSegment = 0;
119 info->mNewSegment = true;
120 info->mAllowedStaleAccessUnits = kMaxAllowedStaleAccessUnits;
121 info->mRTPAnchor = 0;
122 info->mNTPAnchorUs = -1;
123 info->mNormalPlayTimeRTP = 0;
124 info->mNormalPlayTimeUs = 0ll;
125
126 // index(i) should be started from 1. 0 is reserved for [root]
127 info->mPacketSource = new APacketSource(desc, i + 1);
128
129 int32_t timeScale;
130 sp<MetaData> format = getTrackFormat(i, &timeScale);
131 sp<AnotherPacketSource> source = new AnotherPacketSource(format);
132
133 if (info->mIsAudio) {
134 mAudioTrack = source;
135 } else {
136 mVideoTrack = source;
137 }
138
139 info->mSource = source;
140 }
141
142 CHECK_EQ(mState, (int)DISCONNECTED);
143 mState = CONNECTING;
144
145 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;
283 if ((*accessUnit) != NULL && (*accessUnit)->meta()->findInt32("cvo", &cvo)) {
284 if (cvo != mLastCVOUpdated) {
285 sp<AMessage> msg = new AMessage();
286 msg->setInt32("payload-type", NuPlayer::RTPSource::RTP_CVO);
287 msg->setInt32("cvo", cvo);
288
289 sp<AMessage> notify = dupNotify();
290 notify->setInt32("what", kWhatIMSRxNotice);
291 notify->setMessage("message", msg);
292 notify->post();
293
294 ALOGV("notify cvo updated (%d)->(%d) to upper layer", mLastCVOUpdated, cvo);
295 mLastCVOUpdated = cvo;
296 }
297 }
298
299 return finalResult;
Byeongjo Parkd157b792019-01-24 20:56:37 +0900300}
301
302sp<AnotherPacketSource> NuPlayer::RTPSource::getSource(bool audio) {
303 return audio ? mAudioTrack : mVideoTrack;
304}
305
306void NuPlayer::RTPSource::setEOSTimeout(bool audio, int64_t timeout) {
307 if (audio) {
308 mEOSTimeoutAudio = timeout;
309 } else {
310 mEOSTimeoutVideo = timeout;
311 }
312}
313
314status_t NuPlayer::RTPSource::getDuration(int64_t *durationUs) {
315 *durationUs = 0ll;
316
317 int64_t audioDurationUs;
318 if (mAudioTrack != NULL
319 && mAudioTrack->getFormat()->findInt64(
320 kKeyDuration, &audioDurationUs)
321 && audioDurationUs > *durationUs) {
322 *durationUs = audioDurationUs;
323 }
324
325 int64_t videoDurationUs;
326 if (mVideoTrack != NULL
327 && mVideoTrack->getFormat()->findInt64(
328 kKeyDuration, &videoDurationUs)
329 && videoDurationUs > *durationUs) {
330 *durationUs = videoDurationUs;
331 }
332
333 return OK;
334}
335
336status_t NuPlayer::RTPSource::seekTo(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
337 ALOGV("RTPSource::seekTo=%d, mode=%d", (int)seekTimeUs, mode);
338 return OK;
339}
340
341void NuPlayer::RTPSource::schedulePollBuffering() {
342 sp<AMessage> msg = new AMessage(kWhatPollBuffering, this);
343 msg->post(1000000ll); // 1 second intervals
344}
345
346void NuPlayer::RTPSource::onPollBuffering() {
347 schedulePollBuffering();
348}
349
350void NuPlayer::RTPSource::onMessageReceived(const sp<AMessage> &msg) {
351 ALOGV("onMessageReceived =%d", msg->what());
352
353 switch (msg->what()) {
354 case kWhatAccessUnitComplete:
355 {
356 if (mState == CONNECTING) {
357 mState = CONNECTED;
358 }
359
360 int32_t timeUpdate;
361 //"time-update" raised from ARTPConnection::parseSR()
362 if (msg->findInt32("time-update", &timeUpdate) && timeUpdate) {
363 size_t trackIndex;
364 CHECK(msg->findSize("trackIndex", &trackIndex));
365
366 uint32_t rtpTime;
367 uint64_t ntpTime;
368 CHECK(msg->findInt32("rtp-time", (int32_t *)&rtpTime));
369 CHECK(msg->findInt64("ntp-time", (int64_t *)&ntpTime));
370
371 onTimeUpdate(trackIndex, rtpTime, ntpTime);
372 break;
373 }
374
375 int32_t firstRTCP;
376 if (msg->findInt32("first-rtcp", &firstRTCP)) {
377 // There won't be an access unit here, it's just a notification
378 // that the data communication worked since we got the first
379 // rtcp packet.
380 ALOGV("first-rtcp");
381 break;
382 }
383
Kim Sungyeond3c6b322018-03-02 14:41:19 +0900384 int32_t IMSRxNotice;
385 if (msg->findInt32("IMS-Rx-notice", &IMSRxNotice)) {
386 int32_t payloadType, feedbackType;
387 CHECK(msg->findInt32("payload-type", &payloadType));
388 CHECK(msg->findInt32("feedback-type", &feedbackType));
389
390 sp<AMessage> notify = dupNotify();
391 notify->setInt32("what", kWhatIMSRxNotice);
392 notify->setMessage("message", msg);
393 notify->post();
394
395 ALOGV("IMSRxNotice \t\t payload : %d feedback : %d",
396 payloadType, feedbackType);
397 break;
398 }
399
Byeongjo Parkd157b792019-01-24 20:56:37 +0900400 size_t trackIndex;
401 CHECK(msg->findSize("trackIndex", &trackIndex));
402
403 sp<ABuffer> accessUnit;
404 if (msg->findBuffer("access-unit", &accessUnit) == false) {
405 break;
406 }
407
408 int32_t damaged;
409 if (accessUnit->meta()->findInt32("damaged", &damaged)
410 && damaged) {
411 ALOGD("dropping damaged access unit.");
412 break;
413 }
414
415 TrackInfo *info = &mTracks.editItemAt(trackIndex);
416
417 sp<AnotherPacketSource> source = info->mSource;
418 if (source != NULL) {
419 uint32_t rtpTime;
420 CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
421
422 /* AnotherPacketSource make an assertion if there is no ntp provided
423 RTPSource should provide ntpUs all the times.
424 if (!info->mNPTMappingValid) {
425 // This is a live stream, we didn't receive any normal
426 // playtime mapping. We won't map to npt time.
427 source->queueAccessUnit(accessUnit);
428 break;
429 }
430 */
431
432 int64_t nptUs =
433 ((double)rtpTime - (double)info->mRTPTime)
434 / info->mTimeScale
435 * 1000000ll
436 + info->mNormalPlaytimeUs;
437
438 accessUnit->meta()->setInt64("timeUs", nptUs);
439
440 source->queueAccessUnit(accessUnit);
441 }
442
443 break;
444 }
445 case kWhatDisconnect:
446 {
447 sp<AReplyToken> replyID;
448 CHECK(msg->senderAwaitsResponse(&replyID));
449
450 for (size_t i = 0; i < mTracks.size(); ++i) {
451 TrackInfo *info = &mTracks.editItemAt(i);
452
453 if (info->mIsAudio) {
454 mAudioTrack->signalEOS(ERROR_END_OF_STREAM);
455 mAudioTrack = NULL;
456 ALOGV("mAudioTrack disconnected");
457 } else {
458 mVideoTrack->signalEOS(ERROR_END_OF_STREAM);
459 mVideoTrack = NULL;
460 ALOGV("mVideoTrack disconnected");
461 }
462
463 mRTPConn->removeStream(info->mRTPSocket, info->mRTCPSocket);
464 close(info->mRTPSocket);
465 close(info->mRTCPSocket);
466 }
467
468 mTracks.clear();
469 mFirstAccessUnit = true;
470 mAllTracksHaveTime = false;
471 mNTPAnchorUs = -1;
472 mMediaAnchorUs = -1;
473 mLastMediaTimeUs = -1;
474 mNumAccessUnitsReceived = 0;
475 mReceivedFirstRTCPPacket = false;
476 mReceivedFirstRTPPacket = false;
477 mPausing = false;
478 mPauseGeneration = 0;
479
480 (new AMessage)->postReply(replyID);
481
482 break;
483 }
484 case kWhatPollBuffering:
485 break;
486 default:
487 TRESPASS();
488 }
489}
490
491void NuPlayer::RTPSource::onTimeUpdate(int32_t trackIndex, uint32_t rtpTime, uint64_t ntpTime) {
492 ALOGV("onTimeUpdate track %d, rtpTime = 0x%08x, ntpTime = %#016llx",
493 trackIndex, rtpTime, (long long)ntpTime);
494
495 int64_t ntpTimeUs = (int64_t)(ntpTime * 1E6 / (1ll << 32));
496
497 TrackInfo *track = &mTracks.editItemAt(trackIndex);
498
499 track->mRTPAnchor = rtpTime;
500 track->mNTPAnchorUs = ntpTimeUs;
501
502 if (mNTPAnchorUs < 0) {
503 mNTPAnchorUs = ntpTimeUs;
504 mMediaAnchorUs = mLastMediaTimeUs;
505 }
506
507 if (!mAllTracksHaveTime) {
508 bool allTracksHaveTime = (mTracks.size() > 0);
509 for (size_t i = 0; i < mTracks.size(); ++i) {
510 TrackInfo *track = &mTracks.editItemAt(i);
511 if (track->mNTPAnchorUs < 0) {
512 allTracksHaveTime = false;
513 break;
514 }
515 }
516 if (allTracksHaveTime) {
517 mAllTracksHaveTime = true;
518 ALOGI("Time now established for all tracks.");
519 }
520 }
521 if (mAllTracksHaveTime && dataReceivedOnAllChannels()) {
522 // Time is now established, lets start timestamping immediately
523 for (size_t i = 0; i < mTracks.size(); ++i) {
524 TrackInfo *trackInfo = &mTracks.editItemAt(i);
525 while (!trackInfo->mPackets.empty()) {
526 sp<ABuffer> accessUnit = *trackInfo->mPackets.begin();
527 trackInfo->mPackets.erase(trackInfo->mPackets.begin());
528
529 if (addMediaTimestamp(i, trackInfo, accessUnit)) {
530 postQueueAccessUnit(i, accessUnit);
531 }
532 }
533 }
534 }
535}
536
537bool NuPlayer::RTPSource::addMediaTimestamp(
538 int32_t trackIndex, const TrackInfo *track,
539 const sp<ABuffer> &accessUnit) {
540
541 uint32_t rtpTime;
542 CHECK(accessUnit->meta()->findInt32(
543 "rtp-time", (int32_t *)&rtpTime));
544
545 int64_t relRtpTimeUs =
546 (((int64_t)rtpTime - (int64_t)track->mRTPAnchor) * 1000000ll)
547 / track->mTimeScale;
548
549 int64_t ntpTimeUs = track->mNTPAnchorUs + relRtpTimeUs;
550
551 int64_t mediaTimeUs = mMediaAnchorUs + ntpTimeUs - mNTPAnchorUs;
552
553 if (mediaTimeUs > mLastMediaTimeUs) {
554 mLastMediaTimeUs = mediaTimeUs;
555 }
556
557 if (mediaTimeUs < 0) {
558 ALOGV("dropping early accessUnit.");
559 return false;
560 }
561
562 ALOGV("track %d rtpTime=%u mediaTimeUs = %lld us (%.2f secs)",
563 trackIndex, rtpTime, (long long)mediaTimeUs, mediaTimeUs / 1E6);
564
565 accessUnit->meta()->setInt64("timeUs", mediaTimeUs);
566
567 return true;
568}
569
570bool NuPlayer::RTPSource::dataReceivedOnAllChannels() {
571 TrackInfo *track;
572 for (size_t i = 0; i < mTracks.size(); ++i) {
573 track = &mTracks.editItemAt(i);
574 if (track->mPackets.empty()) {
575 return false;
576 }
577 }
578 return true;
579}
580
581void NuPlayer::RTPSource::postQueueAccessUnit(
582 size_t trackIndex, const sp<ABuffer> &accessUnit) {
583 sp<AMessage> msg = new AMessage(kWhatAccessUnit, this);
584 msg->setInt32("what", kWhatAccessUnit);
585 msg->setSize("trackIndex", trackIndex);
586 msg->setBuffer("accessUnit", accessUnit);
587 msg->post();
588}
589
590void NuPlayer::RTPSource::postQueueEOS(size_t trackIndex, status_t finalResult) {
591 sp<AMessage> msg = new AMessage(kWhatEOS, this);
592 msg->setInt32("what", kWhatEOS);
593 msg->setSize("trackIndex", trackIndex);
594 msg->setInt32("finalResult", finalResult);
595 msg->post();
596}
597
598sp<MetaData> NuPlayer::RTPSource::getTrackFormat(size_t index, int32_t *timeScale) {
599 CHECK_GE(index, 0u);
600 CHECK_LT(index, mTracks.size());
601
602 const TrackInfo &info = mTracks.itemAt(index);
603
604 *timeScale = info.mTimeScale;
605
606 return info.mPacketSource->getFormat();
607}
608
609void NuPlayer::RTPSource::onConnected() {
610 ALOGV("onConnected");
611 mState = CONNECTED;
612}
613
614void NuPlayer::RTPSource::onDisconnected(const sp<AMessage> &msg) {
615 if (mState == DISCONNECTED) {
616 return;
617 }
618
619 status_t err;
620 CHECK(msg->findInt32("result", &err));
621 CHECK_NE(err, (status_t)OK);
622
623// mLooper->unregisterHandler(mHandler->id());
624// mHandler.clear();
625
626 if (mState == CONNECTING) {
627 // We're still in the preparation phase, signal that it
628 // failed.
629 notifyPrepared(err);
630 }
631
632 mState = DISCONNECTED;
633// setError(err);
634
635}
636
637status_t NuPlayer::RTPSource::setParameter(const String8 &key, const String8 &value) {
638 ALOGV("setParameter: key (%s) => value (%s)", key.string(), value.string());
639
640 bool isAudioKey = key.contains("audio");
641 TrackInfo *info = NULL;
642 for (unsigned i = 0; i < mTracks.size(); ++i) {
643 info = &mTracks.editItemAt(i);
644 if (info != NULL && info->mIsAudio == isAudioKey) {
645 ALOGV("setParameter: %s track (%d) found", isAudioKey ? "audio" : "video" , i);
646 break;
647 }
648 }
649
650 if (info == NULL) {
651 TrackInfo newTrackInfo;
652 newTrackInfo.mIsAudio = isAudioKey;
653 mTracks.push(newTrackInfo);
654 info = &mTracks.editTop();
655 }
656
657 if (key == "rtp-param-mime-type") {
658 info->mMimeType = value;
659
660 const char *mime = value.string();
661 const char *delimiter = strchr(mime, '/');
662 info->mCodecName = (delimiter + 1);
663
664 ALOGV("rtp-param-mime-type: mMimeType (%s) => mCodecName (%s)",
665 info->mMimeType.string(), info->mCodecName.string());
666 } else if (key == "video-param-decoder-profile") {
667 info->mCodecProfile = atoi(value);
668 } else if (key == "video-param-decoder-level") {
669 info->mCodecLevel = atoi(value);
670 } else if (key == "video-param-width") {
671 info->mWidth = atoi(value);
672 } else if (key == "video-param-height") {
673 info->mHeight = atoi(value);
674 } else if (key == "rtp-param-local-ip") {
675 info->mLocalIp = value;
676 } else if (key == "rtp-param-local-port") {
677 info->mLocalPort = atoi(value);
678 } else if (key == "rtp-param-remote-ip") {
679 info->mRemoteIp = value;
680 } else if (key == "rtp-param-remote-port") {
681 info->mRemotePort = atoi(value);
682 } else if (key == "rtp-param-payload-type") {
683 info->mPayloadType = atoi(value);
684 } else if (key == "rtp-param-as") {
685 //AS means guaranteed bit rate that negotiated from sdp.
686 info->mAS = atoi(value);
687 } else if (key == "rtp-param-rtp-timeout") {
688 } else if (key == "rtp-param-rtcp-timeout") {
689 } else if (key == "rtp-param-time-scale") {
Kim Sungyeond2875e92018-03-20 16:51:41 +0900690 } else if (key == "rtp-param-self-id") {
691 info->mSelfID = atoi(value);
Byeongjo Parkb0225aa2018-04-20 14:00:15 +0900692 } else if (key == "rtp-param-ext-cvo-extmap") {
693 info->mCVOExtMap = atoi(value);
Byeongjo Parkd157b792019-01-24 20:56:37 +0900694 }
695
696 return OK;
697}
698
699status_t NuPlayer::RTPSource::setParameters(const String8 &params) {
700 ALOGV("setParameters: %s", params.string());
701 const char *cparams = params.string();
702 const char *key_start = cparams;
703 for (;;) {
704 const char *equal_pos = strchr(key_start, '=');
705 if (equal_pos == NULL) {
706 ALOGE("Parameters %s miss a value", cparams);
707 return BAD_VALUE;
708 }
709 String8 key(key_start, equal_pos - key_start);
710 TrimString(&key);
711 if (key.length() == 0) {
712 ALOGE("Parameters %s contains an empty key", cparams);
713 return BAD_VALUE;
714 }
715 const char *value_start = equal_pos + 1;
716 const char *semicolon_pos = strchr(value_start, ';');
717 String8 value;
718 if (semicolon_pos == NULL) {
719 value.setTo(value_start);
720 } else {
721 value.setTo(value_start, semicolon_pos - value_start);
722 }
723 if (setParameter(key, value) != OK) {
724 return BAD_VALUE;
725 }
726 if (semicolon_pos == NULL) {
727 break; // Reaches the end
728 }
729 key_start = semicolon_pos + 1;
730 }
731 return OK;
732}
733
734// Trim both leading and trailing whitespace from the given string.
735//static
736void NuPlayer::RTPSource::TrimString(String8 *s) {
737 size_t num_bytes = s->bytes();
738 const char *data = s->string();
739
740 size_t leading_space = 0;
741 while (leading_space < num_bytes && isspace(data[leading_space])) {
742 ++leading_space;
743 }
744
745 size_t i = num_bytes;
746 while (i > leading_space && isspace(data[i - 1])) {
747 --i;
748 }
749
750 s->setTo(String8(&data[leading_space], i - leading_space));
751}
752
753} // namespace android