[4f61ff]: / content / media / omx / OmxDecoder.cpp  Maximize  Restore  History

Download this file

1035 lines (876 with data), 30.0 kB

   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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <unistd.h>
#include <fcntl.h>
#include "base/basictypes.h"
#include <cutils/properties.h>
#include <stagefright/foundation/ADebug.h>
#include <stagefright/foundation/AMessage.h>
#include <stagefright/MediaExtractor.h>
#include <stagefright/MetaData.h>
#include <stagefright/OMXClient.h>
#include <stagefright/OMXCodec.h>
#include <OMX.h>
#include "mozilla/Preferences.h"
#include "mozilla/Types.h"
#include "mozilla/Monitor.h"
#include "nsMimeTypes.h"
#include "MPAPI.h"
#include "prlog.h"
#include "GonkNativeWindow.h"
#include "GonkNativeWindowClient.h"
#include "OMXCodecProxy.h"
#include "OmxDecoder.h"
#ifdef PR_LOGGING
PRLogModuleInfo *gOmxDecoderLog;
#define LOG(type, msg...) PR_LOG(gOmxDecoderLog, type, (msg))
#else
#define LOG(x...)
#endif
using namespace MPAPI;
using namespace mozilla;
namespace mozilla {
class OmxDecoderProcessCachedDataTask : public Task
{
public:
OmxDecoderProcessCachedDataTask(android::OmxDecoder* aOmxDecoder, int64_t aOffset)
: mOmxDecoder(aOmxDecoder),
mOffset(aOffset)
{ }
void Run()
{
MOZ_ASSERT(!NS_IsMainThread());
MOZ_ASSERT(mOmxDecoder.get());
mOmxDecoder->ProcessCachedData(mOffset, false);
}
private:
android::sp<android::OmxDecoder> mOmxDecoder;
int64_t mOffset;
};
// When loading an MP3 stream from a file, we need to parse the file's
// content to find its duration. Reading files of 100 Mib or more can
// delay the player app noticably, so the file os read and decoded in
// smaller chunks.
//
// We first read on the decode thread, but parsing must be done on the
// main thread. After we read the file's initial MiBs in the decode
// thread, an instance of this class is scheduled to the main thread for
// parsing the MP3 stream. The decode thread waits until it has finished.
//
// If there is more data available from the file, the runnable dispatches
// a task to the IO thread for retrieving the next chunk of data, and
// the IO task dispatches a runnable to the main thread for parsing the
// data. This goes on until all of the MP3 file has been parsed.
class OmxDecoderNotifyDataArrivedRunnable : public nsRunnable
{
public:
OmxDecoderNotifyDataArrivedRunnable(android::OmxDecoder* aOmxDecoder, const char* aBuffer, uint64_t aLength, int64_t aOffset, uint64_t aFullLength)
: mOmxDecoder(aOmxDecoder),
mBuffer(aBuffer),
mLength(aLength),
mOffset(aOffset),
mFullLength(aFullLength),
mCompletedMonitor("OmxDecoderNotifyDataArrived.mCompleted"),
mCompleted(false)
{
MOZ_ASSERT(mOmxDecoder.get());
MOZ_ASSERT(mBuffer.get() || !mLength);
}
NS_IMETHOD Run()
{
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
const char* buffer = mBuffer.get();
while (mLength) {
uint32_t length = std::min<uint64_t>(mLength, UINT32_MAX);
mOmxDecoder->NotifyDataArrived(mBuffer.get(), mLength, mOffset);
buffer += length;
mLength -= length;
mOffset += length;
}
if (mOffset < mFullLength) {
// We cannot read data in the main thread because it
// might block for too long. Instead we post an IO task
// to the IO thread if there is more data available.
XRE_GetIOMessageLoop()->PostTask(FROM_HERE, new OmxDecoderProcessCachedDataTask(mOmxDecoder.get(), mOffset));
}
Completed();
return NS_OK;
}
void WaitForCompletion()
{
MOZ_ASSERT(!NS_IsMainThread());
MonitorAutoLock mon(mCompletedMonitor);
if (!mCompleted) {
mCompletedMonitor.Wait();
}
}
private:
// Call this function at the end of Run() to notify waiting
// threads.
void Completed()
{
MonitorAutoLock mon(mCompletedMonitor);
MOZ_ASSERT(!mCompleted);
mCompleted = true;
mCompletedMonitor.Notify();
}
android::sp<android::OmxDecoder> mOmxDecoder;
nsAutoArrayPtr<const char> mBuffer;
uint64_t mLength;
int64_t mOffset;
uint64_t mFullLength;
Monitor mCompletedMonitor;
bool mCompleted;
};
namespace layers {
VideoGraphicBuffer::VideoGraphicBuffer(const android::wp<android::OmxDecoder> aOmxDecoder,
android::MediaBuffer *aBuffer,
SurfaceDescriptor& aDescriptor)
: GraphicBufferLocked(aDescriptor),
mMediaBuffer(aBuffer),
mOmxDecoder(aOmxDecoder)
{
mMediaBuffer->add_ref();
}
VideoGraphicBuffer::~VideoGraphicBuffer()
{
if (mMediaBuffer) {
mMediaBuffer->release();
}
}
void
VideoGraphicBuffer::Unlock()
{
android::sp<android::OmxDecoder> omxDecoder = mOmxDecoder.promote();
if (omxDecoder.get()) {
// Post kNotifyPostReleaseVideoBuffer message to OmxDecoder via ALooper.
// The message is delivered to OmxDecoder on ALooper thread.
// MediaBuffer::release() could take a very long time.
// PostReleaseVideoBuffer() prevents long time locking.
omxDecoder->PostReleaseVideoBuffer(mMediaBuffer);
} else {
NS_WARNING("OmxDecoder is not present");
if (mMediaBuffer) {
mMediaBuffer->release();
}
}
mMediaBuffer = nullptr;
}
}
}
namespace android {
MediaStreamSource::MediaStreamSource(MediaResource *aResource,
AbstractMediaDecoder *aDecoder) :
mResource(aResource), mDecoder(aDecoder)
{
}
MediaStreamSource::~MediaStreamSource()
{
}
status_t MediaStreamSource::initCheck() const
{
return OK;
}
ssize_t MediaStreamSource::readAt(off64_t offset, void *data, size_t size)
{
char *ptr = static_cast<char *>(data);
size_t todo = size;
while (todo > 0) {
Mutex::Autolock autoLock(mLock);
uint32_t bytesRead;
if ((offset != mResource->Tell() &&
NS_FAILED(mResource->Seek(nsISeekableStream::NS_SEEK_SET, offset))) ||
NS_FAILED(mResource->Read(ptr, todo, &bytesRead))) {
return ERROR_IO;
}
if (bytesRead == 0) {
return size - todo;
}
offset += bytesRead;
todo -= bytesRead;
ptr += bytesRead;
}
return size;
}
status_t MediaStreamSource::getSize(off64_t *size)
{
uint64_t length = mResource->GetLength();
if (length == static_cast<uint64_t>(-1))
return ERROR_UNSUPPORTED;
*size = length;
return OK;
}
} // namespace android
using namespace android;
OmxDecoder::OmxDecoder(MediaResource *aResource,
AbstractMediaDecoder *aDecoder) :
mDecoder(aDecoder),
mResource(aResource),
mVideoWidth(0),
mVideoHeight(0),
mVideoColorFormat(0),
mVideoStride(0),
mVideoSliceHeight(0),
mVideoRotation(0),
mAudioChannels(-1),
mAudioSampleRate(-1),
mDurationUs(-1),
mMP3FrameParser(aResource->GetLength()),
mVideoBuffer(nullptr),
mAudioBuffer(nullptr),
mIsVideoSeeking(false),
mAudioMetadataRead(false),
mAudioPaused(false),
mVideoPaused(false)
{
mLooper = new ALooper;
mLooper->setName("OmxDecoder");
mReflector = new AHandlerReflector<OmxDecoder>(this);
// Register AMessage handler to ALooper.
mLooper->registerHandler(mReflector);
// Start ALooper thread.
mLooper->start();
}
OmxDecoder::~OmxDecoder()
{
ReleaseMediaResources();
// unregister AMessage handler from ALooper.
mLooper->unregisterHandler(mReflector->id());
// Stop ALooper thread.
mLooper->stop();
}
void OmxDecoder::statusChanged()
{
sp<AMessage> notify =
new AMessage(kNotifyStatusChanged, mReflector->id());
// post AMessage to OmxDecoder via ALooper.
notify->post();
}
static sp<IOMX> sOMX = nullptr;
static sp<IOMX> GetOMX()
{
if(sOMX.get() == nullptr) {
sOMX = new OMX;
}
return sOMX;
}
bool OmxDecoder::Init() {
#ifdef PR_LOGGING
if (!gOmxDecoderLog) {
gOmxDecoderLog = PR_NewLogModule("OmxDecoder");
}
#endif
//register sniffers, if they are not registered in this process.
DataSource::RegisterDefaultSniffers();
sp<DataSource> dataSource = new MediaStreamSource(mResource, mDecoder);
if (dataSource->initCheck()) {
NS_WARNING("Initializing DataSource for OMX decoder failed");
return false;
}
mResource->SetReadMode(MediaCacheStream::MODE_METADATA);
sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
if (extractor == nullptr) {
NS_WARNING("Could not create MediaExtractor");
return false;
}
ssize_t audioTrackIndex = -1;
ssize_t videoTrackIndex = -1;
for (size_t i = 0; i < extractor->countTracks(); ++i) {
sp<MetaData> meta = extractor->getTrackMetaData(i);
int32_t bitRate;
if (!meta->findInt32(kKeyBitRate, &bitRate))
bitRate = 0;
const char *mime;
if (!meta->findCString(kKeyMIMEType, &mime)) {
continue;
}
if (videoTrackIndex == -1 && !strncasecmp(mime, "video/", 6)) {
videoTrackIndex = i;
} else if (audioTrackIndex == -1 && !strncasecmp(mime, "audio/", 6)) {
audioTrackIndex = i;
}
}
if (videoTrackIndex == -1 && audioTrackIndex == -1) {
NS_WARNING("OMX decoder could not find video or audio tracks");
return false;
}
mResource->SetReadMode(MediaCacheStream::MODE_PLAYBACK);
if (videoTrackIndex != -1) {
mVideoTrack = extractor->getTrack(videoTrackIndex);
}
if (audioTrackIndex != -1) {
mAudioTrack = extractor->getTrack(audioTrackIndex);
}
return true;
}
bool OmxDecoder::TryLoad() {
if (!AllocateMediaResources()) {
return false;
}
//check if video is waiting resources
if (mVideoSource.get()) {
if (mVideoSource->IsWaitingResources()) {
return true;
}
}
// calculate duration
int64_t totalDurationUs = 0;
int64_t durationUs = 0;
if (mVideoTrack.get() && mVideoTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) {
if (durationUs > totalDurationUs)
totalDurationUs = durationUs;
}
if (mAudioTrack.get()) {
durationUs = -1;
const char* audioMime;
sp<MetaData> meta = mAudioTrack->getFormat();
if (meta->findCString(kKeyMIMEType, &audioMime) && !strcasecmp(audioMime, AUDIO_MP3)) {
// Feed MP3 parser with cached data. Local files will be fully
// cached already, network streams will update with sucessive
// calls to NotifyDataArrived.
if (ProcessCachedData(0, true)) {
durationUs = mMP3FrameParser.GetDuration();
if (durationUs > totalDurationUs) {
totalDurationUs = durationUs;
}
}
}
if ((durationUs == -1) && meta->findInt64(kKeyDuration, &durationUs)) {
if (durationUs > totalDurationUs) {
totalDurationUs = durationUs;
}
}
}
mDurationUs = totalDurationUs;
// read video metadata
if (mVideoSource.get() && !SetVideoFormat()) {
NS_WARNING("Couldn't set OMX video format");
return false;
}
// read audio metadata
if (mAudioSource.get()) {
// To reliably get the channel and sample rate data we need to read from the
// audio source until we get a INFO_FORMAT_CHANGE status
status_t err = mAudioSource->read(&mAudioBuffer);
if (err != INFO_FORMAT_CHANGED) {
if (err != OK) {
NS_WARNING("Couldn't read audio buffer from OMX decoder");
return false;
}
sp<MetaData> meta = mAudioSource->getFormat();
if (!meta->findInt32(kKeyChannelCount, &mAudioChannels) ||
!meta->findInt32(kKeySampleRate, &mAudioSampleRate)) {
NS_WARNING("Couldn't get audio metadata from OMX decoder");
return false;
}
mAudioMetadataRead = true;
}
else if (!SetAudioFormat()) {
NS_WARNING("Couldn't set audio format");
return false;
}
}
return true;
}
bool OmxDecoder::IsDormantNeeded()
{
if (mVideoTrack.get()) {
return true;
}
return false;
}
bool OmxDecoder::IsWaitingMediaResources()
{
if (mVideoSource.get()) {
return mVideoSource->IsWaitingResources();
}
return false;
}
bool OmxDecoder::AllocateMediaResources()
{
// OMXClient::connect() always returns OK and abort's fatally if
// it can't connect.
OMXClient client;
DebugOnly<status_t> err = client.connect();
NS_ASSERTION(err == OK, "Failed to connect to OMX in mediaserver.");
sp<IOMX> omx = client.interface();
if ((mVideoTrack != nullptr) && (mVideoSource == nullptr)) {
mNativeWindow = new GonkNativeWindow();
mNativeWindowClient = new GonkNativeWindowClient(mNativeWindow);
// Experience with OMX codecs is that only the HW decoders are
// worth bothering with, at least on the platforms where this code
// is currently used, and for formats this code is currently used
// for (h.264). So if we don't get a hardware decoder, just give
// up.
int flags = kHardwareCodecsOnly;
char propQemu[PROPERTY_VALUE_MAX];
property_get("ro.kernel.qemu", propQemu, "");
if (!strncmp(propQemu, "1", 1)) {
// If we are in emulator, allow to fall back to software.
flags = 0;
}
mVideoSource =
OMXCodecProxy::Create(omx,
mVideoTrack->getFormat(),
false, // decoder
mVideoTrack,
nullptr,
flags,
mNativeWindowClient);
if (mVideoSource == nullptr) {
NS_WARNING("Couldn't create OMX video source");
return false;
} else {
sp<OMXCodecProxy::EventListener> listener = this;
mVideoSource->setEventListener(listener);
mVideoSource->requestResource();
}
}
if ((mAudioTrack != nullptr) && (mAudioSource == nullptr)) {
const char *audioMime = nullptr;
sp<MetaData> meta = mAudioTrack->getFormat();
if (!meta->findCString(kKeyMIMEType, &audioMime)) {
return false;
}
if (!strcasecmp(audioMime, "audio/raw")) {
mAudioSource = mAudioTrack;
} else {
// try to load hardware codec in mediaserver process.
int flags = kHardwareCodecsOnly;
mAudioSource = OMXCodec::Create(omx,
mAudioTrack->getFormat(),
false, // decoder
mAudioTrack,
nullptr,
flags);
}
if (mAudioSource == nullptr) {
// try to load software codec in this process.
int flags = kSoftwareCodecsOnly;
mAudioSource = OMXCodec::Create(GetOMX(),
mAudioTrack->getFormat(),
false, // decoder
mAudioTrack,
nullptr,
flags);
if (mAudioSource == nullptr) {
NS_WARNING("Couldn't create OMX audio source");
return false;
}
}
if (mAudioSource->start() != OK) {
NS_WARNING("Couldn't start OMX audio source");
mAudioSource.clear();
return false;
}
}
return true;
}
void OmxDecoder::ReleaseMediaResources() {
{
// Free all pending video buffers.
Mutex::Autolock autoLock(mSeekLock);
ReleaseAllPendingVideoBuffersLocked();
}
ReleaseVideoBuffer();
ReleaseAudioBuffer();
if (mVideoSource.get()) {
mVideoSource->stop();
mVideoSource.clear();
}
if (mAudioSource.get()) {
mAudioSource->stop();
mAudioSource.clear();
}
mNativeWindowClient.clear();
mNativeWindow.clear();
}
bool OmxDecoder::SetVideoFormat() {
const char *componentName;
if (!mVideoSource->getFormat()->findInt32(kKeyWidth, &mVideoWidth) ||
!mVideoSource->getFormat()->findInt32(kKeyHeight, &mVideoHeight) ||
!mVideoSource->getFormat()->findCString(kKeyDecoderComponent, &componentName) ||
!mVideoSource->getFormat()->findInt32(kKeyColorFormat, &mVideoColorFormat) ) {
return false;
}
if (!mVideoSource->getFormat()->findInt32(kKeyStride, &mVideoStride)) {
mVideoStride = mVideoWidth;
NS_WARNING("stride not available, assuming width");
}
if (!mVideoSource->getFormat()->findInt32(kKeySliceHeight, &mVideoSliceHeight)) {
mVideoSliceHeight = mVideoHeight;
NS_WARNING("slice height not available, assuming height");
}
if (!mVideoSource->getFormat()->findInt32(kKeyRotation, &mVideoRotation)) {
mVideoRotation = 0;
NS_WARNING("rotation not available, assuming 0");
}
LOG(PR_LOG_DEBUG, "width: %d height: %d component: %s format: %d stride: %d sliceHeight: %d rotation: %d",
mVideoWidth, mVideoHeight, componentName, mVideoColorFormat,
mVideoStride, mVideoSliceHeight, mVideoRotation);
return true;
}
bool OmxDecoder::SetAudioFormat() {
// If the format changed, update our cached info.
if (!mAudioSource->getFormat()->findInt32(kKeyChannelCount, &mAudioChannels) ||
!mAudioSource->getFormat()->findInt32(kKeySampleRate, &mAudioSampleRate)) {
return false;
}
LOG(PR_LOG_DEBUG, "channelCount: %d sampleRate: %d",
mAudioChannels, mAudioSampleRate);
return true;
}
void OmxDecoder::NotifyDataArrived(const char* aBuffer, uint32_t aLength, int64_t aOffset)
{
if (!mMP3FrameParser.IsMP3()) {
return;
}
mMP3FrameParser.NotifyDataArrived(aBuffer, aLength, aOffset);
int64_t durationUs = mMP3FrameParser.GetDuration();
if (durationUs != mDurationUs) {
mDurationUs = durationUs;
MOZ_ASSERT(mDecoder);
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
mDecoder->UpdateMediaDuration(mDurationUs);
}
}
void OmxDecoder::ReleaseVideoBuffer() {
if (mVideoBuffer) {
mVideoBuffer->release();
mVideoBuffer = nullptr;
}
}
void OmxDecoder::ReleaseAudioBuffer() {
if (mAudioBuffer) {
mAudioBuffer->release();
mAudioBuffer = nullptr;
}
}
void OmxDecoder::PlanarYUV420Frame(VideoFrame *aFrame, int64_t aTimeUs, void *aData, size_t aSize, bool aKeyFrame) {
void *y = aData;
void *u = static_cast<uint8_t *>(y) + mVideoStride * mVideoSliceHeight;
void *v = static_cast<uint8_t *>(u) + mVideoStride/2 * mVideoSliceHeight/2;
aFrame->Set(aTimeUs, aKeyFrame,
aData, aSize, mVideoStride, mVideoSliceHeight, mVideoRotation,
y, mVideoStride, mVideoWidth, mVideoHeight, 0, 0,
u, mVideoStride/2, mVideoWidth/2, mVideoHeight/2, 0, 0,
v, mVideoStride/2, mVideoWidth/2, mVideoHeight/2, 0, 0);
}
void OmxDecoder::CbYCrYFrame(VideoFrame *aFrame, int64_t aTimeUs, void *aData, size_t aSize, bool aKeyFrame) {
aFrame->Set(aTimeUs, aKeyFrame,
aData, aSize, mVideoStride, mVideoSliceHeight, mVideoRotation,
aData, mVideoStride, mVideoWidth, mVideoHeight, 1, 1,
aData, mVideoStride, mVideoWidth/2, mVideoHeight/2, 0, 3,
aData, mVideoStride, mVideoWidth/2, mVideoHeight/2, 2, 3);
}
void OmxDecoder::SemiPlanarYUV420Frame(VideoFrame *aFrame, int64_t aTimeUs, void *aData, size_t aSize, bool aKeyFrame) {
void *y = aData;
void *uv = static_cast<uint8_t *>(y) + (mVideoStride * mVideoSliceHeight);
aFrame->Set(aTimeUs, aKeyFrame,
aData, aSize, mVideoStride, mVideoSliceHeight, mVideoRotation,
y, mVideoStride, mVideoWidth, mVideoHeight, 0, 0,
uv, mVideoStride, mVideoWidth/2, mVideoHeight/2, 0, 1,
uv, mVideoStride, mVideoWidth/2, mVideoHeight/2, 1, 1);
}
void OmxDecoder::SemiPlanarYVU420Frame(VideoFrame *aFrame, int64_t aTimeUs, void *aData, size_t aSize, bool aKeyFrame) {
SemiPlanarYUV420Frame(aFrame, aTimeUs, aData, aSize, aKeyFrame);
aFrame->Cb.mOffset = 1;
aFrame->Cr.mOffset = 0;
}
bool OmxDecoder::ToVideoFrame(VideoFrame *aFrame, int64_t aTimeUs, void *aData, size_t aSize, bool aKeyFrame) {
const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
aFrame->mGraphicBuffer = nullptr;
switch (mVideoColorFormat) {
case OMX_COLOR_FormatYUV420Planar:
PlanarYUV420Frame(aFrame, aTimeUs, aData, aSize, aKeyFrame);
break;
case OMX_COLOR_FormatCbYCrY:
CbYCrYFrame(aFrame, aTimeUs, aData, aSize, aKeyFrame);
break;
case OMX_COLOR_FormatYUV420SemiPlanar:
SemiPlanarYUV420Frame(aFrame, aTimeUs, aData, aSize, aKeyFrame);
break;
case OMX_QCOM_COLOR_FormatYVU420SemiPlanar:
SemiPlanarYVU420Frame(aFrame, aTimeUs, aData, aSize, aKeyFrame);
break;
default:
LOG(PR_LOG_DEBUG, "Unknown video color format %08x", mVideoColorFormat);
return false;
}
return true;
}
bool OmxDecoder::ToAudioFrame(AudioFrame *aFrame, int64_t aTimeUs, void *aData, size_t aDataOffset, size_t aSize, int32_t aAudioChannels, int32_t aAudioSampleRate)
{
aFrame->Set(aTimeUs, static_cast<char *>(aData) + aDataOffset, aSize, aAudioChannels, aAudioSampleRate);
return true;
}
bool OmxDecoder::ReadVideo(VideoFrame *aFrame, int64_t aTimeUs,
bool aKeyframeSkip, bool aDoSeek)
{
if (!mVideoSource.get())
return false;
ReleaseVideoBuffer();
status_t err;
if (aDoSeek) {
{
Mutex::Autolock autoLock(mSeekLock);
mIsVideoSeeking = true;
}
MediaSource::ReadOptions options;
options.setSeekTo(aTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
err = mVideoSource->read(&mVideoBuffer, &options);
{
Mutex::Autolock autoLock(mSeekLock);
mIsVideoSeeking = false;
ReleaseAllPendingVideoBuffersLocked();
}
aDoSeek = false;
} else {
err = mVideoSource->read(&mVideoBuffer);
}
aFrame->mSize = 0;
if (err == OK) {
int64_t timeUs;
int32_t unreadable;
int32_t keyFrame;
if (!mVideoBuffer->meta_data()->findInt64(kKeyTime, &timeUs) ) {
NS_WARNING("OMX decoder did not return frame time");
return false;
}
if (!mVideoBuffer->meta_data()->findInt32(kKeyIsSyncFrame, &keyFrame)) {
keyFrame = 0;
}
if (!mVideoBuffer->meta_data()->findInt32(kKeyIsUnreadable, &unreadable)) {
unreadable = 0;
}
mozilla::layers::SurfaceDescriptor *descriptor = nullptr;
if ((mVideoBuffer->graphicBuffer().get())) {
descriptor = mNativeWindow->getSurfaceDescriptorFromBuffer(mVideoBuffer->graphicBuffer().get());
}
if (descriptor) {
// Change the descriptor's size to video's size. There are cases that
// GraphicBuffer's size and actual video size is different.
// See Bug 850566.
mozilla::layers::SurfaceDescriptorGralloc newDescriptor = descriptor->get_SurfaceDescriptorGralloc();
newDescriptor.size() = nsIntSize(mVideoWidth, mVideoHeight);
mozilla::layers::SurfaceDescriptor descWrapper(newDescriptor);
aFrame->mGraphicBuffer = new mozilla::layers::VideoGraphicBuffer(this, mVideoBuffer, descWrapper);
aFrame->mRotation = mVideoRotation;
aFrame->mTimeUs = timeUs;
aFrame->mKeyFrame = keyFrame;
aFrame->Y.mWidth = mVideoWidth;
aFrame->Y.mHeight = mVideoHeight;
} else if (mVideoBuffer->range_length() > 0) {
char *data = static_cast<char *>(mVideoBuffer->data()) + mVideoBuffer->range_offset();
size_t length = mVideoBuffer->range_length();
if (unreadable) {
LOG(PR_LOG_DEBUG, "video frame is unreadable");
}
if (!ToVideoFrame(aFrame, timeUs, data, length, keyFrame)) {
return false;
}
}
if (aKeyframeSkip && timeUs < aTimeUs) {
aFrame->mShouldSkip = true;
}
}
else if (err == INFO_FORMAT_CHANGED) {
// If the format changed, update our cached info.
if (!SetVideoFormat()) {
return false;
} else {
return ReadVideo(aFrame, aTimeUs, aKeyframeSkip, aDoSeek);
}
}
else if (err == ERROR_END_OF_STREAM) {
return false;
}
else if (err == -ETIMEDOUT) {
LOG(PR_LOG_DEBUG, "OmxDecoder::ReadVideo timed out, will retry");
return true;
}
else {
// UNKNOWN_ERROR is sometimes is used to mean "out of memory", but
// regardless, don't keep trying to decode if the decoder doesn't want to.
LOG(PR_LOG_DEBUG, "OmxDecoder::ReadVideo failed, err=%d", err);
return false;
}
return true;
}
bool OmxDecoder::ReadAudio(AudioFrame *aFrame, int64_t aSeekTimeUs)
{
status_t err;
if (mAudioMetadataRead && aSeekTimeUs == -1) {
// Use the data read into the buffer during metadata time
err = OK;
}
else {
ReleaseAudioBuffer();
if (aSeekTimeUs != -1) {
MediaSource::ReadOptions options;
options.setSeekTo(aSeekTimeUs);
err = mAudioSource->read(&mAudioBuffer, &options);
} else {
err = mAudioSource->read(&mAudioBuffer);
}
}
mAudioMetadataRead = false;
aSeekTimeUs = -1;
aFrame->mSize = 0;
if (err == OK && mAudioBuffer && mAudioBuffer->range_length() != 0) {
int64_t timeUs;
if (!mAudioBuffer->meta_data()->findInt64(kKeyTime, &timeUs))
return false;
return ToAudioFrame(aFrame, timeUs,
mAudioBuffer->data(),
mAudioBuffer->range_offset(),
mAudioBuffer->range_length(),
mAudioChannels, mAudioSampleRate);
}
else if (err == INFO_FORMAT_CHANGED) {
// If the format changed, update our cached info.
if (!SetAudioFormat()) {
return false;
} else {
return ReadAudio(aFrame, aSeekTimeUs);
}
}
else if (err == ERROR_END_OF_STREAM) {
if (aFrame->mSize == 0) {
return false;
}
}
else if (err == -ETIMEDOUT) {
LOG(PR_LOG_DEBUG, "OmxDecoder::ReadAudio timed out, will retry");
return true;
}
else if (err != OK) {
LOG(PR_LOG_DEBUG, "OmxDecoder::ReadAudio failed, err=%d", err);
return false;
}
return true;
}
nsresult OmxDecoder::Play()
{
if (!mVideoPaused && !mAudioPaused) {
return NS_OK;
}
if (mVideoPaused && mVideoSource.get() && mVideoSource->start() != OK) {
return NS_ERROR_UNEXPECTED;
}
mVideoPaused = false;
if (mAudioPaused && mAudioSource.get() && mAudioSource->start() != OK) {
return NS_ERROR_UNEXPECTED;
}
mAudioPaused = false;
return NS_OK;
}
// AOSP didn't give implementation on OMXCodec::Pause() and not define
// OMXCodec::Start() should be called for resuming the decoding. Currently
// it is customized by a specific open source repository only.
// ToDo The one not supported OMXCodec::Pause() should return error code here,
// so OMXCodec::Start() doesn't be called again for resuming. But if someone
// implement the OMXCodec::Pause() and need a following OMXCodec::Read() with
// seek option (define in MediaSource.h) then it is still not supported here.
// We need to fix it until it is really happened.
void OmxDecoder::Pause()
{
if (mVideoPaused || mAudioPaused) {
return;
}
if (mVideoSource.get() && mVideoSource->pause() == OK) {
mVideoPaused = true;
}
if (mAudioSource.get() && mAudioSource->pause() == OK) {
mAudioPaused = true;
}
}
// Called on ALooper thread.
void OmxDecoder::onMessageReceived(const sp<AMessage> &msg)
{
switch (msg->what()) {
case kNotifyPostReleaseVideoBuffer:
{
Mutex::Autolock autoLock(mSeekLock);
// Free pending video buffers when OmxDecoder is not seeking video.
// If OmxDecoder is seeking video, the buffers are freed on seek exit.
if (!mIsVideoSeeking) {
ReleaseAllPendingVideoBuffersLocked();
}
break;
}
case kNotifyStatusChanged:
{
mozilla::ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
mDecoder->GetReentrantMonitor().NotifyAll();
break;
}
default:
TRESPASS();
break;
}
}
void OmxDecoder::PostReleaseVideoBuffer(MediaBuffer *aBuffer)
{
{
Mutex::Autolock autoLock(mPendingVideoBuffersLock);
mPendingVideoBuffers.push(aBuffer);
}
sp<AMessage> notify =
new AMessage(kNotifyPostReleaseVideoBuffer, mReflector->id());
// post AMessage to OmxDecoder via ALooper.
notify->post();
}
void OmxDecoder::ReleaseAllPendingVideoBuffersLocked()
{
Vector<MediaBuffer *> releasingVideoBuffers;
{
Mutex::Autolock autoLock(mPendingVideoBuffersLock);
int size = mPendingVideoBuffers.size();
for (int i = 0; i < size; i++) {
MediaBuffer *buffer = mPendingVideoBuffers[i];
releasingVideoBuffers.push(buffer);
}
mPendingVideoBuffers.clear();
}
// Free all pending video buffers without holding mPendingVideoBuffersLock.
int size = releasingVideoBuffers.size();
for (int i = 0; i < size; i++) {
MediaBuffer *buffer;
buffer = releasingVideoBuffers[i];
buffer->release();
}
releasingVideoBuffers.clear();
}
bool OmxDecoder::ProcessCachedData(int64_t aOffset, bool aWaitForCompletion)
{
// We read data in chunks of 8 MiB. We can reduce this
// value if media, such as sdcards, is too slow.
static const int64_t sReadSize = 8 * 1024 * 1024;
NS_ASSERTION(!NS_IsMainThread(), "Should not be on main thread.");
MOZ_ASSERT(mResource);
int64_t resourceLength = mResource->GetCachedDataEnd(0);
NS_ENSURE_TRUE(resourceLength >= 0, false);
if (aOffset >= resourceLength) {
return true; // Cache is empty, nothing to do
}
int64_t bufferLength = std::min<int64_t>(resourceLength-aOffset, sReadSize);
nsAutoArrayPtr<char> buffer(new char[bufferLength]);
nsresult rv = mResource->ReadFromCache(buffer.get(), aOffset, bufferLength);
NS_ENSURE_SUCCESS(rv, false);
nsRefPtr<OmxDecoderNotifyDataArrivedRunnable> runnable(
new OmxDecoderNotifyDataArrivedRunnable(this,
buffer.forget(),
bufferLength,
aOffset,
resourceLength));
rv = NS_DispatchToMainThread(runnable.get());
NS_ENSURE_SUCCESS(rv, false);
if (aWaitForCompletion) {
runnable->WaitForCompletion();
}
return true;
}