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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
|
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//#define LOG_NDEBUG 0
#define LOG_TAG "GenericSource"
#include "GenericSource.h"
#include "AnotherPacketSource.h"
#include <media/IMediaHTTPService.h>
#include <media/stagefright/foundation/ABuffer.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/AMessage.h>
#include <media/stagefright/DataSource.h>
#include <media/stagefright/FileSource.h>
#include <media/stagefright/MediaBuffer.h>
#include <media/stagefright/MediaDefs.h>
#include <media/stagefright/MediaExtractor.h>
#include <media/stagefright/MediaSource.h>
#include <media/stagefright/MetaData.h>
#include "../../libstagefright/include/DRMExtractor.h"
#include "../../libstagefright/include/NuCachedSource2.h"
#include "../../libstagefright/include/WVMExtractor.h"
namespace android {
NuPlayer::GenericSource::GenericSource(
const sp<AMessage> ¬ify,
bool uidValid,
uid_t uid)
: Source(notify),
mFetchSubtitleDataGeneration(0),
mFetchTimedTextDataGeneration(0),
mDurationUs(0ll),
mAudioIsVorbis(false),
mIsWidevine(false),
mUIDValid(uidValid),
mUID(uid),
mDrmManagerClient(NULL),
mMetaDataSize(-1ll),
mBitrate(-1ll),
mPollBufferingGeneration(0) {
resetDataSource();
DataSource::RegisterDefaultSniffers();
}
void NuPlayer::GenericSource::resetDataSource() {
mAudioTimeUs = 0;
mVideoTimeUs = 0;
mHTTPService.clear();
mUri.clear();
mUriHeaders.clear();
mFd = -1;
mOffset = 0;
mLength = 0;
setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
mDecryptHandle = NULL;
mDrmManagerClient = NULL;
mStarted = false;
}
status_t NuPlayer::GenericSource::setDataSource(
const sp<IMediaHTTPService> &httpService,
const char *url,
const KeyedVector<String8, String8> *headers) {
resetDataSource();
mHTTPService = httpService;
mUri = url;
if (headers) {
mUriHeaders = *headers;
}
// delay data source creation to prepareAsync() to avoid blocking
// the calling thread in setDataSource for any significant time.
return OK;
}
status_t NuPlayer::GenericSource::setDataSource(
int fd, int64_t offset, int64_t length) {
resetDataSource();
mFd = dup(fd);
mOffset = offset;
mLength = length;
// delay data source creation to prepareAsync() to avoid blocking
// the calling thread in setDataSource for any significant time.
return OK;
}
status_t NuPlayer::GenericSource::initFromDataSource() {
sp<MediaExtractor> extractor;
CHECK(mDataSource != NULL);
if (mIsWidevine) {
String8 mimeType;
float confidence;
sp<AMessage> dummy;
bool success;
success = SniffWVM(mDataSource, &mimeType, &confidence, &dummy);
if (!success
|| strcasecmp(
mimeType.string(), MEDIA_MIMETYPE_CONTAINER_WVM)) {
ALOGE("unsupported widevine mime: %s", mimeType.string());
return UNKNOWN_ERROR;
}
mWVMExtractor = new WVMExtractor(mDataSource);
mWVMExtractor->setAdaptiveStreamingMode(true);
if (mUIDValid) {
mWVMExtractor->setUID(mUID);
}
extractor = mWVMExtractor;
} else {
extractor = MediaExtractor::Create(mDataSource,
mSniffedMIME.empty() ? NULL: mSniffedMIME.c_str());
}
if (extractor == NULL) {
return UNKNOWN_ERROR;
}
if (extractor->getDrmFlag()) {
checkDrmStatus(mDataSource);
}
sp<MetaData> fileMeta = extractor->getMetaData();
if (fileMeta != NULL) {
int64_t duration;
if (fileMeta->findInt64(kKeyDuration, &duration)) {
mDurationUs = duration;
}
}
int32_t totalBitrate = 0;
for (size_t i = 0; i < extractor->countTracks(); ++i) {
sp<MediaSource> track = extractor->getTrack(i);
sp<MetaData> meta = extractor->getTrackMetaData(i);
const char *mime;
CHECK(meta->findCString(kKeyMIMEType, &mime));
// Do the string compare immediately with "mime",
// we can't assume "mime" would stay valid after another
// extractor operation, some extractors might modify meta
// during getTrack() and make it invalid.
if (!strncasecmp(mime, "audio/", 6)) {
if (mAudioTrack.mSource == NULL) {
mAudioTrack.mIndex = i;
mAudioTrack.mSource = track;
if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) {
mAudioIsVorbis = true;
} else {
mAudioIsVorbis = false;
}
}
} else if (!strncasecmp(mime, "video/", 6)) {
if (mVideoTrack.mSource == NULL) {
mVideoTrack.mIndex = i;
mVideoTrack.mSource = track;
// check if the source requires secure buffers
int32_t secure;
if (meta->findInt32(kKeyRequiresSecureBuffers, &secure)
&& secure) {
mIsWidevine = true;
if (mUIDValid) {
extractor->setUID(mUID);
}
}
}
}
if (track != NULL) {
mSources.push(track);
int64_t durationUs;
if (meta->findInt64(kKeyDuration, &durationUs)) {
if (durationUs > mDurationUs) {
mDurationUs = durationUs;
}
}
int32_t bitrate;
if (totalBitrate >= 0 && meta->findInt32(kKeyBitRate, &bitrate)) {
totalBitrate += bitrate;
} else {
totalBitrate = -1;
}
}
}
mBitrate = totalBitrate;
return OK;
}
void NuPlayer::GenericSource::checkDrmStatus(const sp<DataSource>& dataSource) {
dataSource->getDrmInfo(mDecryptHandle, &mDrmManagerClient);
if (mDecryptHandle != NULL) {
CHECK(mDrmManagerClient);
if (RightsStatus::RIGHTS_VALID != mDecryptHandle->status) {
sp<AMessage> msg = dupNotify();
msg->setInt32("what", kWhatDrmNoLicense);
msg->post();
}
}
}
int64_t NuPlayer::GenericSource::getLastReadPosition() {
if (mAudioTrack.mSource != NULL) {
return mAudioTimeUs;
} else if (mVideoTrack.mSource != NULL) {
return mVideoTimeUs;
} else {
return 0;
}
}
status_t NuPlayer::GenericSource::setBuffers(
bool audio, Vector<MediaBuffer *> &buffers) {
if (mIsWidevine && !audio) {
return mVideoTrack.mSource->setBuffers(buffers);
}
return INVALID_OPERATION;
}
NuPlayer::GenericSource::~GenericSource() {
if (mLooper != NULL) {
mLooper->unregisterHandler(id());
mLooper->stop();
}
}
void NuPlayer::GenericSource::prepareAsync() {
if (mLooper == NULL) {
mLooper = new ALooper;
mLooper->setName("generic");
mLooper->start();
mLooper->registerHandler(this);
}
sp<AMessage> msg = new AMessage(kWhatPrepareAsync, id());
msg->post();
}
void NuPlayer::GenericSource::onPrepareAsync() {
// delayed data source creation
if (mDataSource == NULL) {
if (!mUri.empty()) {
mIsWidevine = !strncasecmp(mUri.c_str(), "widevine://", 11);
mDataSource = DataSource::CreateFromURI(
mHTTPService, mUri.c_str(), &mUriHeaders, &mContentType);
} else {
// set to false first, if the extractor
// comes back as secure, set it to true then.
mIsWidevine = false;
mDataSource = new FileSource(mFd, mOffset, mLength);
}
if (mDataSource == NULL) {
ALOGE("Failed to create data source!");
notifyPreparedAndCleanup(UNKNOWN_ERROR);
return;
}
if (mDataSource->flags() & DataSource::kIsCachingDataSource) {
mCachedSource = static_cast<NuCachedSource2 *>(mDataSource.get());
}
if (mIsWidevine || mCachedSource != NULL) {
schedulePollBuffering();
}
}
// check initial caching status
status_t err = prefillCacheIfNecessary();
if (err != OK) {
if (err == -EAGAIN) {
(new AMessage(kWhatPrepareAsync, id()))->post(200000);
} else {
ALOGE("Failed to prefill data cache!");
notifyPreparedAndCleanup(UNKNOWN_ERROR);
}
return;
}
// init extrator from data source
err = initFromDataSource();
if (err != OK) {
ALOGE("Failed to init from data source!");
notifyPreparedAndCleanup(err);
return;
}
if (mVideoTrack.mSource != NULL) {
notifyVideoSizeChanged(getFormat(false /* audio */));
}
notifyFlagsChanged(
(mIsWidevine ? FLAG_SECURE : 0)
| FLAG_CAN_PAUSE
| FLAG_CAN_SEEK_BACKWARD
| FLAG_CAN_SEEK_FORWARD
| FLAG_CAN_SEEK);
notifyPrepared();
}
void NuPlayer::GenericSource::notifyPreparedAndCleanup(status_t err) {
if (err != OK) {
mMetaDataSize = -1ll;
mContentType = "";
mSniffedMIME = "";
mDataSource.clear();
mCachedSource.clear();
cancelPollBuffering();
}
notifyPrepared(err);
}
status_t NuPlayer::GenericSource::prefillCacheIfNecessary() {
CHECK(mDataSource != NULL);
if (mCachedSource == NULL) {
// no prefill if the data source is not cached
return OK;
}
// We're not doing this for streams that appear to be audio-only
// streams to ensure that even low bandwidth streams start
// playing back fairly instantly.
if (!strncasecmp(mContentType.string(), "audio/", 6)) {
return OK;
}
// We're going to prefill the cache before trying to instantiate
// the extractor below, as the latter is an operation that otherwise
// could block on the datasource for a significant amount of time.
// During that time we'd be unable to abort the preparation phase
// without this prefill.
// Initially make sure we have at least 192 KB for the sniff
// to complete without blocking.
static const size_t kMinBytesForSniffing = 192 * 1024;
static const size_t kDefaultMetaSize = 200000;
status_t finalStatus;
size_t cachedDataRemaining =
mCachedSource->approxDataRemaining(&finalStatus);
if (finalStatus != OK || (mMetaDataSize >= 0
&& (off64_t)cachedDataRemaining >= mMetaDataSize)) {
ALOGV("stop caching, status %d, "
"metaDataSize %lld, cachedDataRemaining %zu",
finalStatus, mMetaDataSize, cachedDataRemaining);
return OK;
}
ALOGV("now cached %zu bytes of data", cachedDataRemaining);
if (mMetaDataSize < 0
&& cachedDataRemaining >= kMinBytesForSniffing) {
String8 tmp;
float confidence;
sp<AMessage> meta;
if (!mCachedSource->sniff(&tmp, &confidence, &meta)) {
return UNKNOWN_ERROR;
}
// We successfully identified the file's extractor to
// be, remember this mime type so we don't have to
// sniff it again when we call MediaExtractor::Create()
mSniffedMIME = tmp.string();
if (meta == NULL
|| !meta->findInt64("meta-data-size",
reinterpret_cast<int64_t*>(&mMetaDataSize))) {
mMetaDataSize = kDefaultMetaSize;
}
if (mMetaDataSize < 0ll) {
ALOGE("invalid metaDataSize = %lld bytes", mMetaDataSize);
return UNKNOWN_ERROR;
}
}
return -EAGAIN;
}
void NuPlayer::GenericSource::start() {
ALOGI("start");
if (mAudioTrack.mSource != NULL) {
CHECK_EQ(mAudioTrack.mSource->start(), (status_t)OK);
mAudioTrack.mPackets =
new AnotherPacketSource(mAudioTrack.mSource->getFormat());
readBuffer(MEDIA_TRACK_TYPE_AUDIO);
}
if (mVideoTrack.mSource != NULL) {
CHECK_EQ(mVideoTrack.mSource->start(), (status_t)OK);
mVideoTrack.mPackets =
new AnotherPacketSource(mVideoTrack.mSource->getFormat());
readBuffer(MEDIA_TRACK_TYPE_VIDEO);
}
setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
mStarted = true;
}
void NuPlayer::GenericSource::stop() {
// nothing to do, just account for DRM playback status
setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
mStarted = false;
}
void NuPlayer::GenericSource::pause() {
// nothing to do, just account for DRM playback status
setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
mStarted = false;
}
void NuPlayer::GenericSource::resume() {
// nothing to do, just account for DRM playback status
setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
mStarted = true;
}
void NuPlayer::GenericSource::setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position) {
if (mDecryptHandle != NULL) {
mDrmManagerClient->setPlaybackStatus(mDecryptHandle, playbackStatus, position);
}
}
status_t NuPlayer::GenericSource::feedMoreTSData() {
return OK;
}
void NuPlayer::GenericSource::schedulePollBuffering() {
sp<AMessage> msg = new AMessage(kWhatPollBuffering, id());
msg->setInt32("generation", mPollBufferingGeneration);
msg->post(1000000ll);
}
void NuPlayer::GenericSource::cancelPollBuffering() {
++mPollBufferingGeneration;
}
void NuPlayer::GenericSource::notifyBufferingUpdate(int percentage) {
sp<AMessage> msg = dupNotify();
msg->setInt32("what", kWhatBufferingUpdate);
msg->setInt32("percentage", percentage);
msg->post();
}
void NuPlayer::GenericSource::onPollBuffering() {
status_t finalStatus = UNKNOWN_ERROR;
int64_t cachedDurationUs = 0ll;
if (mCachedSource != NULL) {
size_t cachedDataRemaining =
mCachedSource->approxDataRemaining(&finalStatus);
if (finalStatus == OK) {
off64_t size;
int64_t bitrate = 0ll;
if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) {
bitrate = size * 8000000ll / mDurationUs;
} else if (mBitrate > 0) {
bitrate = mBitrate;
}
if (bitrate > 0) {
cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate;
}
}
} else if (mWVMExtractor != NULL) {
cachedDurationUs
= mWVMExtractor->getCachedDurationUs(&finalStatus);
}
if (finalStatus == ERROR_END_OF_STREAM) {
notifyBufferingUpdate(100);
cancelPollBuffering();
return;
} else if (cachedDurationUs > 0ll && mDurationUs > 0ll) {
int percentage = 100.0 * cachedDurationUs / mDurationUs;
if (percentage > 100) {
percentage = 100;
}
notifyBufferingUpdate(percentage);
}
schedulePollBuffering();
}
void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {
switch (msg->what()) {
case kWhatPrepareAsync:
{
onPrepareAsync();
break;
}
case kWhatFetchSubtitleData:
{
fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
break;
}
case kWhatFetchTimedTextData:
{
fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
break;
}
case kWhatSendSubtitleData:
{
sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
break;
}
case kWhatSendTimedTextData:
{
sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
break;
}
case kWhatChangeAVSource:
{
int32_t trackIndex;
CHECK(msg->findInt32("trackIndex", &trackIndex));
const sp<MediaSource> source = mSources.itemAt(trackIndex);
Track* track;
const char *mime;
media_track_type trackType, counterpartType;
sp<MetaData> meta = source->getFormat();
meta->findCString(kKeyMIMEType, &mime);
if (!strncasecmp(mime, "audio/", 6)) {
track = &mAudioTrack;
trackType = MEDIA_TRACK_TYPE_AUDIO;
counterpartType = MEDIA_TRACK_TYPE_VIDEO;;
} else {
CHECK(!strncasecmp(mime, "video/", 6));
track = &mVideoTrack;
trackType = MEDIA_TRACK_TYPE_VIDEO;
counterpartType = MEDIA_TRACK_TYPE_AUDIO;;
}
if (track->mSource != NULL) {
track->mSource->stop();
}
track->mSource = source;
track->mSource->start();
track->mIndex = trackIndex;
status_t avail;
if (!track->mPackets->hasBufferAvailable(&avail)) {
// sync from other source
TRESPASS();
break;
}
int64_t timeUs, actualTimeUs;
const bool formatChange = true;
sp<AMessage> latestMeta = track->mPackets->getLatestEnqueuedMeta();
CHECK(latestMeta != NULL && latestMeta->findInt64("timeUs", &timeUs));
readBuffer(trackType, timeUs, &actualTimeUs, formatChange);
readBuffer(counterpartType, -1, NULL, formatChange);
ALOGV("timeUs %lld actualTimeUs %lld", timeUs, actualTimeUs);
break;
}
case kWhatPollBuffering:
{
int32_t generation;
CHECK(msg->findInt32("generation", &generation));
if (generation == mPollBufferingGeneration) {
onPollBuffering();
}
break;
}
default:
Source::onMessageReceived(msg);
break;
}
}
void NuPlayer::GenericSource::fetchTextData(
uint32_t sendWhat,
media_track_type type,
int32_t curGen,
sp<AnotherPacketSource> packets,
sp<AMessage> msg) {
int32_t msgGeneration;
CHECK(msg->findInt32("generation", &msgGeneration));
if (msgGeneration != curGen) {
// stale
return;
}
int32_t avail;
if (packets->hasBufferAvailable(&avail)) {
return;
}
int64_t timeUs;
CHECK(msg->findInt64("timeUs", &timeUs));
int64_t subTimeUs;
readBuffer(type, timeUs, &subTimeUs);
int64_t delayUs = subTimeUs - timeUs;
if (msg->what() == kWhatFetchSubtitleData) {
const int64_t oneSecUs = 1000000ll;
delayUs -= oneSecUs;
}
sp<AMessage> msg2 = new AMessage(sendWhat, id());
msg2->setInt32("generation", msgGeneration);
msg2->post(delayUs < 0 ? 0 : delayUs);
}
void NuPlayer::GenericSource::sendTextData(
uint32_t what,
media_track_type type,
int32_t curGen,
sp<AnotherPacketSource> packets,
sp<AMessage> msg) {
int32_t msgGeneration;
CHECK(msg->findInt32("generation", &msgGeneration));
if (msgGeneration != curGen) {
// stale
return;
}
int64_t subTimeUs;
if (packets->nextBufferTime(&subTimeUs) != OK) {
return;
}
int64_t nextSubTimeUs;
readBuffer(type, -1, &nextSubTimeUs);
sp<ABuffer> buffer;
status_t dequeueStatus = packets->dequeueAccessUnit(&buffer);
if (dequeueStatus == OK) {
sp<AMessage> notify = dupNotify();
notify->setInt32("what", what);
notify->setBuffer("buffer", buffer);
notify->post();
const int64_t delayUs = nextSubTimeUs - subTimeUs;
msg->post(delayUs < 0 ? 0 : delayUs);
}
}
sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) {
sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
if (source == NULL) {
return NULL;
}
return source->getFormat();
}
status_t NuPlayer::GenericSource::dequeueAccessUnit(
bool audio, sp<ABuffer> *accessUnit) {
Track *track = audio ? &mAudioTrack : &mVideoTrack;
if (track->mSource == NULL) {
return -EWOULDBLOCK;
}
if (mIsWidevine && !audio) {
// try to read a buffer as we may not have been able to the last time
readBuffer(MEDIA_TRACK_TYPE_VIDEO, -1ll);
}
status_t finalResult;
if (!track->mPackets->hasBufferAvailable(&finalResult)) {
return (finalResult == OK ? -EWOULDBLOCK : finalResult);
}
status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
if (!track->mPackets->hasBufferAvailable(&finalResult)) {
readBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO, -1ll);
}
if (mSubtitleTrack.mSource == NULL && mTimedTextTrack.mSource == NULL) {
return result;
}
if (mSubtitleTrack.mSource != NULL) {
CHECK(mSubtitleTrack.mPackets != NULL);
}
if (mTimedTextTrack.mSource != NULL) {
CHECK(mTimedTextTrack.mPackets != NULL);
}
if (result != OK) {
if (mSubtitleTrack.mSource != NULL) {
mSubtitleTrack.mPackets->clear();
mFetchSubtitleDataGeneration++;
}
if (mTimedTextTrack.mSource != NULL) {
mTimedTextTrack.mPackets->clear();
mFetchTimedTextDataGeneration++;
}
return result;
}
int64_t timeUs;
status_t eosResult; // ignored
CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
if (mSubtitleTrack.mSource != NULL
&& !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
msg->setInt64("timeUs", timeUs);
msg->setInt32("generation", mFetchSubtitleDataGeneration);
msg->post();
}
if (mTimedTextTrack.mSource != NULL
&& !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, id());
msg->setInt64("timeUs", timeUs);
msg->setInt32("generation", mFetchTimedTextDataGeneration);
msg->post();
}
return result;
}
status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
*durationUs = mDurationUs;
return OK;
}
size_t NuPlayer::GenericSource::getTrackCount() const {
return mSources.size();
}
sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const {
size_t trackCount = mSources.size();
if (trackIndex >= trackCount) {
return NULL;
}
sp<AMessage> format = new AMessage();
sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat();
const char *mime;
CHECK(meta->findCString(kKeyMIMEType, &mime));
int32_t trackType;
if (!strncasecmp(mime, "video/", 6)) {
trackType = MEDIA_TRACK_TYPE_VIDEO;
} else if (!strncasecmp(mime, "audio/", 6)) {
trackType = MEDIA_TRACK_TYPE_AUDIO;
} else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
trackType = MEDIA_TRACK_TYPE_TIMEDTEXT;
} else {
trackType = MEDIA_TRACK_TYPE_UNKNOWN;
}
format->setInt32("type", trackType);
const char *lang;
if (!meta->findCString(kKeyMediaLanguage, &lang)) {
lang = "und";
}
format->setString("language", lang);
if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
format->setString("mime", mime);
int32_t isAutoselect = 1, isDefault = 0, isForced = 0;
meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect);
meta->findInt32(kKeyTrackIsDefault, &isDefault);
meta->findInt32(kKeyTrackIsForced, &isForced);
format->setInt32("auto", !!isAutoselect);
format->setInt32("default", !!isDefault);
format->setInt32("forced", !!isForced);
}
return format;
}
ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
const Track *track = NULL;
switch (type) {
case MEDIA_TRACK_TYPE_VIDEO:
track = &mVideoTrack;
break;
case MEDIA_TRACK_TYPE_AUDIO:
track = &mAudioTrack;
break;
case MEDIA_TRACK_TYPE_TIMEDTEXT:
track = &mTimedTextTrack;
break;
case MEDIA_TRACK_TYPE_SUBTITLE:
track = &mSubtitleTrack;
break;
default:
break;
}
if (track != NULL && track->mSource != NULL) {
return track->mIndex;
}
return -1;
}
status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select) {
ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
if (trackIndex >= mSources.size()) {
return BAD_INDEX;
}
if (!select) {
Track* track = NULL;
if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
track = &mSubtitleTrack;
mFetchSubtitleDataGeneration++;
} else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
track = &mTimedTextTrack;
mFetchTimedTextDataGeneration++;
}
if (track == NULL) {
return INVALID_OPERATION;
}
track->mSource->stop();
track->mSource = NULL;
track->mPackets->clear();
return OK;
}
const sp<MediaSource> source = mSources.itemAt(trackIndex);
sp<MetaData> meta = source->getFormat();
const char *mime;
CHECK(meta->findCString(kKeyMIMEType, &mime));
if (!strncasecmp(mime, "text/", 5)) {
bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
if (track->mSource != NULL && track->mIndex == trackIndex) {
return OK;
}
track->mIndex = trackIndex;
if (track->mSource != NULL) {
track->mSource->stop();
}
track->mSource = mSources.itemAt(trackIndex);
track->mSource->start();
if (track->mPackets == NULL) {
track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
} else {
track->mPackets->clear();
track->mPackets->setFormat(track->mSource->getFormat());
}
if (isSubtitle) {
mFetchSubtitleDataGeneration++;
} else {
mFetchTimedTextDataGeneration++;
}
return OK;
} else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
bool audio = !strncasecmp(mime, "audio/", 6);
Track *track = audio ? &mAudioTrack : &mVideoTrack;
if (track->mSource != NULL && track->mIndex == trackIndex) {
return OK;
}
sp<AMessage> msg = new AMessage(kWhatChangeAVSource, id());
msg->setInt32("trackIndex", trackIndex);
msg->post();
return OK;
}
return INVALID_OPERATION;
}
status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) {
if (mVideoTrack.mSource != NULL) {
int64_t actualTimeUs;
readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs);
seekTimeUs = actualTimeUs;
}
if (mAudioTrack.mSource != NULL) {
readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs);
}
setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000);
if (!mStarted) {
setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
}
return OK;
}
sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
MediaBuffer* mb,
media_track_type trackType,
int64_t *actualTimeUs) {
bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
size_t outLength = mb->range_length();
if (audio && mAudioIsVorbis) {
outLength += sizeof(int32_t);
}
sp<ABuffer> ab;
if (mIsWidevine && !audio) {
// data is already provided in the buffer
ab = new ABuffer(NULL, mb->range_length());
ab->meta()->setPointer("mediaBuffer", mb);
mb->add_ref();
} else {
ab = new ABuffer(outLength);
memcpy(ab->data(),
(const uint8_t *)mb->data() + mb->range_offset(),
mb->range_length());
}
if (audio && mAudioIsVorbis) {
int32_t numPageSamples;
if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) {
numPageSamples = -1;
}
uint8_t* abEnd = ab->data() + mb->range_length();
memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
}
sp<AMessage> meta = ab->meta();
int64_t timeUs;
CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs));
meta->setInt64("timeUs", timeUs);
if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
const char *mime;
CHECK(mTimedTextTrack.mSource != NULL
&& mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
meta->setString("mime", mime);
}
int64_t durationUs;
if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) {
meta->setInt64("durationUs", durationUs);
}
if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
meta->setInt32("trackIndex", mSubtitleTrack.mIndex);
}
if (actualTimeUs) {
*actualTimeUs = timeUs;
}
mb->release();
mb = NULL;
return ab;
}
void NuPlayer::GenericSource::readBuffer(
media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) {
Track *track;
switch (trackType) {
case MEDIA_TRACK_TYPE_VIDEO:
track = &mVideoTrack;
break;
case MEDIA_TRACK_TYPE_AUDIO:
track = &mAudioTrack;
break;
case MEDIA_TRACK_TYPE_SUBTITLE:
track = &mSubtitleTrack;
break;
case MEDIA_TRACK_TYPE_TIMEDTEXT:
track = &mTimedTextTrack;
break;
default:
TRESPASS();
}
if (track->mSource == NULL) {
return;
}
if (actualTimeUs) {
*actualTimeUs = seekTimeUs;
}
MediaSource::ReadOptions options;
bool seeking = false;
if (seekTimeUs >= 0) {
options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
seeking = true;
}
if (mIsWidevine && trackType != MEDIA_TRACK_TYPE_AUDIO) {
options.setNonBlocking();
}
for (;;) {
MediaBuffer *mbuf;
status_t err = track->mSource->read(&mbuf, &options);
options.clearSeekTo();
if (err == OK) {
int64_t timeUs;
CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
mAudioTimeUs = timeUs;
} else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
mVideoTimeUs = timeUs;
}
// formatChange && seeking: track whose source is changed during selection
// formatChange && !seeking: track whose source is not changed during selection
// !formatChange: normal seek
if ((seeking || formatChange)
&& (trackType == MEDIA_TRACK_TYPE_AUDIO
|| trackType == MEDIA_TRACK_TYPE_VIDEO)) {
ATSParser::DiscontinuityType type = formatChange
? (seeking
? ATSParser::DISCONTINUITY_FORMATCHANGE
: ATSParser::DISCONTINUITY_NONE)
: ATSParser::DISCONTINUITY_SEEK;
track->mPackets->queueDiscontinuity( type, NULL, true /* discard */);
}
sp<ABuffer> buffer = mediaBufferToABuffer(mbuf, trackType, actualTimeUs);
track->mPackets->queueAccessUnit(buffer);
break;
} else if (err == WOULD_BLOCK) {
break;
} else if (err == INFO_FORMAT_CHANGED) {
#if 0
track->mPackets->queueDiscontinuity(
ATSParser::DISCONTINUITY_FORMATCHANGE,
NULL,
false /* discard */);
#endif
} else {
track->mPackets->signalEOS(err);
break;
}
}
}
} // namespace android
|