-
Notifications
You must be signed in to change notification settings - Fork 58
/
droidmediacodec.cpp
1191 lines (1002 loc) · 34.8 KB
/
droidmediacodec.cpp
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
/*
* Copyright (C) 2021 Open Mobile Platform LLC.
* Copyright (C) 2014-2015 Jolla Ltd.
*
* 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.
*
* Authored by: Mohammed Hassan <[email protected]>
*/
#if ANDROID_MAJOR >= 9
#include <media/stagefright/omx/1.0/Omx.h>
#elif ANDROID_MAJOR >= 8
#include <media/stagefright/omx/OMX.h>
#else
#include <media/stagefright/OMXClient.h>
#endif
#include <media/stagefright/MediaCodecList.h>
// MediaCodecSource should be used instead of OMXCodec from Android 5
#if ANDROID_MAJOR >= 5
#include <media/stagefright/MediaBuffer.h>
#include <media/stagefright/MediaCodecSource.h>
#include <media/stagefright/MediaCodec.h>
#include <media/openmax/OMX_Video.h>
#include <media/openmax/OMX_Audio.h>
#include <media/stagefright/ACodec.h>
#include <media/stagefright/foundation/ALooper.h>
#include <media/stagefright/foundation/AMessage.h>
#endif
#include "mediabuffers.h"
#if ANDROID_MAJOR < 7
#include <media/stagefright/OMXCodec.h>
#else
#include <media/hardware/MetadataBufferType.h>
#include "AsyncCodecSource.h"
#endif
#if ANDROID_MAJOR >= 9 && ANDROID_MAJOR <= 10
#include <media/MediaSource.h>
#else
#include <media/stagefright/MediaSource.h>
#endif
#include <media/stagefright/MetaData.h>
#include <media/stagefright/MediaDefs.h>
#include <gui/BufferQueue.h>
#include <inttypes.h>
#include "droidmediacodec.h"
#if ANDROID_MAJOR < 8
#include "allocator.h"
#endif
#include "private.h"
#include "droidmediabuffer.h"
#undef LOG_TAG
#define LOG_TAG "DroidMediaCodec"
#define DROID_MEDIA_CODEC_MAX_INPUT_BUFFERS 5
#define SET_PARAM(k,v) \
if (meta->v >= 0) { \
md->setInt32(android::k, meta->v); \
}
struct DroidMediaCodecMetaDataKey {
const char *mime;
int key;
int type;
} metaDataKeys[] = {
#if ANDROID_MAJOR >= 10
{android::MEDIA_MIMETYPE_VIDEO_AV1, android::kKeyAV1C, android::kKeyAV1C},
#endif
#if ANDROID_MAJOR >= 5
{android::MEDIA_MIMETYPE_VIDEO_HEVC, android::kKeyHVCC, android::kKeyHVCC},
#endif
{android::MEDIA_MIMETYPE_VIDEO_MPEG4, android::kKeyESDS, android::kTypeESDS},
{android::MEDIA_MIMETYPE_AUDIO_AAC, android::kKeyESDS, android::kTypeESDS},
{android::MEDIA_MIMETYPE_VIDEO_AVC, android::kKeyAVCC, android::kTypeAVCC},
{NULL, 0, 0}
};
class Source : public android::MediaSource {
public:
Source(android::sp<android::MetaData>& metaData) :
m_metaData(metaData),
m_running(false),
m_draining(false)
{
}
void unlock() {
m_framesReceived.lock.lock();
m_framesReceived.cond.broadcast();
m_framesReceived.lock.unlock();
}
// After this is called all new buffers will be rejected. get() will still return any already
// queued buffers but once the queue has been emptied it will return a null buffer indicating
// the end of the stream. There is no coming back from this.
void drain() {
m_framesReceived.lock.lock();
m_draining = true;
m_framesReceived.cond.broadcast();
m_framesReceived.lock.unlock();
}
void add(android::MediaBuffer *buffer) {
m_framesReceived.lock.lock();
// drain buffer gets added without checking to avoid a deadlock
while (!m_draining) {
// TODO: I am not sure this is the right approach here.
if (m_framesReceived.buffers.size() < DROID_MEDIA_CODEC_MAX_INPUT_BUFFERS) {
m_framesReceived.buffers.push_back(buffer);
m_framesReceived.cond.signal();
m_framesReceived.lock.unlock();
return;
}
// either get() will signal us or we will be signaled in case of an error
m_framesReceived.cond.wait(m_framesReceived.lock);
}
m_framesReceived.lock.unlock();
buffer->release();
}
android::MediaBuffer *get() {
m_framesReceived.lock.lock();
for (;;) {
if (!m_running) {
m_framesReceived.lock.unlock();
return NULL;
}
if (!m_framesReceived.buffers.empty()) {
android::List<android::MediaBuffer *>::iterator iter = m_framesReceived.buffers.begin();
android::MediaBuffer *buffer = *iter;
m_framesReceived.buffers.erase(iter);
m_framesReceived.cond.signal();
m_framesReceived.lock.unlock();
m_framesBeingProcessed.lock.lock();
m_framesBeingProcessed.buffers.push_back(buffer);
m_framesBeingProcessed.lock.unlock();
return buffer;
}
if (m_draining) {
m_framesReceived.lock.unlock();
return NULL;
}
m_framesReceived.cond.wait(m_framesReceived.lock);
}
// unreachable
return NULL;
}
void removeProcessedBuffer(android::MediaBuffer *buffer) {
m_framesBeingProcessed.lock.lock();
for (android::List<android::MediaBuffer *>::iterator iter = m_framesBeingProcessed.buffers.begin();
iter != m_framesBeingProcessed.buffers.end(); iter++) {
if (*iter == buffer) {
m_framesBeingProcessed.buffers.erase(iter);
m_framesBeingProcessed.cond.signal();
m_framesBeingProcessed.lock.unlock();
return;
}
}
m_framesBeingProcessed.lock.unlock();
ALOGW("A buffer we don't know about is being finished!");
}
void flush() {
m_framesReceived.lock.lock();
while (!m_framesReceived.buffers.empty()) {
android::List<android::MediaBuffer *>::iterator iter = m_framesReceived.buffers.begin();
android::MediaBuffer *buffer = *iter;
m_framesReceived.buffers.erase(iter);
if (buffer) {
buffer->release();
}
}
m_framesReceived.lock.unlock();
}
private:
android::status_t start(android::MetaData *meta DM_UNUSED) {
m_running = true;
return android::OK;
}
android::sp<android::MetaData> getFormat() {
// TODO: The only way to pass rotation is via a key here kKeyRotation
return m_metaData;
}
android::status_t stop() {
// Mark as not running
m_framesReceived.lock.lock();
m_running = false;
// Just in case get() is waiting for a buffer
m_framesReceived.cond.signal();
m_framesReceived.lock.unlock();
// Now clear all the buffers:
flush();
m_framesBeingProcessed.lock.lock();
while (!m_framesBeingProcessed.buffers.empty()) {
ALOGW("stop(): waiting for %zu frames", m_framesBeingProcessed.buffers.size());
m_framesBeingProcessed.cond.wait(m_framesBeingProcessed.lock);
}
m_framesBeingProcessed.lock.unlock();
return android::OK;
}
#if ANDROID_MAJOR >= 9
android::status_t read(android::MediaBufferBase **buffer,
#else
android::status_t read(android::MediaBuffer **buffer,
#endif
const android::MediaSource::ReadOptions *options DM_UNUSED = NULL) {
*buffer = get();
if (*buffer) {
return android::OK;
} else {
return android::NOT_ENOUGH_DATA;
}
}
android::sp<android::MetaData> m_metaData;
bool m_running;
bool m_draining;
Buffers<android::MediaBuffer*> m_framesReceived;
Buffers<android::MediaBuffer*> m_framesBeingProcessed;
};
class InputBuffer : public android::MediaBuffer {
public:
InputBuffer(void *data, size_t size, void *cb_data, DroidMediaCallback unref) :
android::MediaBuffer(data, size),
m_cb_data(cb_data),
m_unref(unref) {
}
~InputBuffer() {
}
void *m_cb_data;
DroidMediaCallback m_unref;
};
struct _DroidMediaCodec : public android::MediaBufferObserver
{
_DroidMediaCodec() :
m_cb_data(0),
m_data_cb_data(0) {
memset(&m_cb, 0x0, sizeof(m_cb));
memset(&m_data_cb, 0x0, sizeof(m_data_cb));
}
~_DroidMediaCodec() {
m_codec.clear();
m_src.clear();
m_queue.clear();
m_window.clear();
m_thread.clear();
#if ANDROID_MAJOR >= 5
m_looper->stop();
#endif
}
#if ANDROID_MAJOR >= 9
void signalBufferReturned(android::MediaBufferBase *buff)
#else
void signalBufferReturned(android::MediaBuffer *buff)
#endif
{
InputBuffer *buffer = (InputBuffer *) buff;
m_src->removeProcessedBuffer(buffer);
buffer->m_unref(buffer->m_cb_data);
// TODO: android does that
buff->setObserver(0);
buff->release();
}
bool notifySizeChanged() {
android::sp<android::MetaData> meta = m_codec->getFormat();
int32_t width = 0, height = 0;
if (!meta->findInt32(android::kKeyWidth, &width) ||
!meta->findInt32(android::kKeyHeight, &height)) {
ALOGW("notifySizeChanged without dimensions");
return true;
}
ALOGI("notifySizeChanged: width = %d, height = %d", width, height);
if (m_cb.size_changed) {
return m_cb.size_changed (m_cb_data, width, height) == 0 ? true : false;
}
return true;
}
android::sp<android::MediaSource> m_codec;
android::sp<Source> m_src;
android::sp<DroidMediaBufferQueue> m_queue;
android::sp<ANativeWindow> m_window;
#if ANDROID_MAJOR >= 5
android::sp<android::ALooper> m_looper;
#endif
android::sp<android::Thread> m_thread;
bool m_useExternalLoop;
DroidMediaCodecCallbacks m_cb;
void *m_cb_data;
DroidMediaCodecDataCallbacks m_data_cb;
void *m_data_cb_data;
};
class DroidMediaCodecLoop : public android::Thread {
public:
DroidMediaCodecLoop(DroidMediaCodec *codec) :
Thread(false),
m_codec(codec) {
}
bool threadLoop() {
return droid_media_codec_loop (m_codec) == DROID_MEDIA_CODEC_LOOP_OK ? true : false;
}
private:
DroidMediaCodec *m_codec;
};
class DroidMediaCodecBuilder {
public:
DroidMediaCodecBuilder(DroidMediaCodecEncoderMetaData *meta) :
m_enc(meta), m_dec(NULL) {}
DroidMediaCodecBuilder(DroidMediaCodecDecoderMetaData *meta) :
m_enc(NULL), m_dec(meta) {}
android::sp<android::MediaSource> createCodec(android::sp<android::MediaSource> src,
android::sp<ANativeWindow> window,
#if ANDROID_MAJOR >= 5
android::sp<android::ALooper> looper = NULL,
#endif
android::sp<android::MetaData> md = NULL
) {
if (md == NULL) {
md = buildMetaData();
}
if (md == NULL) {
return NULL;
}
#if ANDROID_MAJOR >= 5
const char *mime;
if (!md->findCString(android::kKeyMIMEType, &mime)) {
ALOGE("No mime type declared for codec");
return NULL;
}
if (isEncoder()) {
if (!strncmp("video", mime, 5)) {
android::sp<android::AMessage> format = new android::AMessage();
format->setString("mime", mime);
ALOGW("Creating video encoder for %s", mime);
int32_t width, height, stride, sliceh, colour, frames, maxinput, bitrate, iframes;
if (md->findInt32(android::kKeyWidth, &width)) {
format->setInt32("width", width);
}
if (md->findInt32(android::kKeyHeight, &height)) {
format->setInt32("height", height);
}
if (md->findInt32(android::kKeyStride, &stride)) {
format->setInt32("stride", stride);
}
if (md->findInt32(android::kKeySliceHeight, &sliceh)) {
format->setInt32("slice-height", sliceh);
}
if (md->findInt32(android::kKeyColorFormat, &colour)) {
format->setInt32("color-format", colour);
}
if (md->findInt32(android::kKeyFrameRate, &frames)) {
format->setInt32("frame-rate", frames);
}
if (md->findInt32(android::kKeyMaxInputSize, &maxinput)) {
format->setInt32("max-input-size", maxinput);
}
if (md->findInt32(android::kKeyBitRate, &bitrate)) {
format->setInt32("bitrate", bitrate);
}
if (md->findInt32(android::kKeyIFramesInterval, &iframes)) {
format->setInt32("i-frame-interval", iframes);
}
#if ANDROID_MAJOR >= 7
if (m_enc->meta_data) {
format->setInt32("android._input-metadata-buffer-type", m_enc->meta_data);
format->setInt32("android._store-metadata-in-buffers-output", false);
}
format->setInt32("android._using-recorder", 1);
#endif
if (!strcmp (mime, android::MEDIA_MIMETYPE_VIDEO_AVC)) {
int32_t profile = m_enc->codec_specific.h264.profile;
int32_t level = m_enc->codec_specific.h264.level;
if (!profile) {
profile = OMX_VIDEO_AVCProfileBaseline;
}
if (!level) {
level = android::ACodec::getAVCLevelFor(width, height, frames, bitrate,
#if (ANDROID_MAJOR == 8 && ANDROID_MINOR >= 1) || ANDROID_MAJOR >= 9
(OMX_VIDEO_AVCPROFILEEXTTYPE)profile);
#else
(OMX_VIDEO_AVCPROFILETYPE)profile);
#endif
}
format->setInt32("profile", profile);
format->setInt32("level", level);
if (m_enc->codec_specific.h264.prepend_header_to_sync_frames) {
format->setInt32("prepend-sps-pps-to-idr-frames", 1);
}
if (m_enc->bitrate_mode != DROID_MEDIA_CODEC_BITRATE_CONTROL_DEFAULT) {
format->setInt32("bitrate-mode", m_enc->bitrate_mode);
}
}
//TODO: time-scale
#if ANDROID_MAJOR > 6
return android::AsyncCodecSource::Create(src, format, true /* isEncoder */,
flags(), window, looper);
#elif ANDROID_MAJOR == 6
return android::MediaCodecSource::Create(looper, format, src, NULL, flags());
#else
return android::MediaCodecSource::Create(looper, format, src, flags());
#endif
}
else {
android::sp<android::AMessage> format = new android::AMessage();
format->setString("mime", mime);
ALOGW("Creating audio encoder for %s", mime);
if (!strcmp (mime, android::MEDIA_MIMETYPE_AUDIO_AAC)) {
format->setInt32("aac-profile", OMX_AUDIO_AACObjectLC);
}
int32_t maxinput, channels, samplerate, bitrate;
if (md->findInt32(android::kKeyMaxInputSize, &maxinput)) {
format->setInt32("max-input-size", maxinput);
}
if (md->findInt32(android::kKeyChannelCount, &channels)) {
format->setInt32("channel-count", channels);
}
if (md->findInt32(android::kKeySampleRate, &samplerate)) {
format->setInt32("sample-rate", samplerate);
}
if (md->findInt32(android::kKeyBitRate, &bitrate)) {
format->setInt32("bitrate", bitrate);
}
return android::MediaCodecSource::Create(looper, format, src);
}
}
#endif
#if ANDROID_MAJOR < 7
android::OMXClient omx;
if (omx.connect() != android::OK) {
ALOGE("Failed to connect to OMX");
return NULL;
}
return android::OMXCodec::Create(omx.interface(),
md,
isEncoder(),
src,
NULL, flags(), window);
#else
return android::AsyncCodecSource::Create(src, nullptr, false /* isEncoder */,
flags(), window, looper, nullptr,
(OMX_COLOR_FORMATTYPE)m_dec->color_format);
#endif
}
bool isEncoder() { return m_enc != NULL; }
DroidMediaCodecMetaData *meta() {
if (m_enc) {
return (DroidMediaCodecMetaData *)m_enc;
} else if (m_dec) {
return (DroidMediaCodecMetaData *)m_dec;
} else {
return NULL;
}
}
android::sp<android::MetaData> buildMetaData() {
if (isEncoder()) {
return buildMetaData(m_enc);
} else {
return buildMetaData(m_dec);
}
}
uint32_t flags() {
if (isEncoder()) {
return flags(m_enc);
} else {
return flags(m_dec);
}
}
static uint32_t flags(DroidMediaCodecMetaData *meta, uint32_t currentFlags) {
// We will not do any validation for the flags. Stagefright should take care of that.
if (meta->flags & DROID_MEDIA_CODEC_SW_ONLY) {
#if ANDROID_MAJOR < 7
currentFlags |= android::OMXCodec::kSoftwareCodecsOnly;
#else
currentFlags |= android::MediaCodecList::kPreferSoftwareCodecs;
#endif
}
if (meta->flags & DROID_MEDIA_CODEC_HW_ONLY) {
#if ANDROID_MAJOR < 7
currentFlags |= android::OMXCodec::kHardwareCodecsOnly;
#else
currentFlags |= android::MediaCodecList::kHardwareCodecsOnly;
#endif
}
return currentFlags;
}
private:
android::sp<android::MetaData> buildMetaData(DroidMediaCodecEncoderMetaData *meta) {
android::sp<android::MetaData> md(new android::MetaData);
SET_PARAM(kKeyMaxInputSize, max_input_size);
SET_PARAM(kKeyBitRate, bitrate);
SET_PARAM(kKeyStride, stride);
SET_PARAM(kKeySliceHeight, slice_height);
SET_PARAM(kKeyColorFormat, color_format);
// TODO: This is hardcoded for now. Fix it.
md->setInt32(android::kKeyIFramesInterval, 2);
// TODO: kKeyHFR
return buildMetaData((DroidMediaCodecMetaData *)meta, md);
}
android::sp<android::MetaData> buildMetaData(DroidMediaCodecDecoderMetaData *meta) {
android::sp<android::MetaData> md(new android::MetaData);
if (meta->codec_data.size > 0) {
const char *mime = ((DroidMediaCodecMetaData *)meta)->type;
DroidMediaCodecMetaDataKey *key = metaDataKeys;
while (key->mime) {
if (!strcmp (key->mime, mime)) {
md->setData(key->key, key->type, meta->codec_data.data, meta->codec_data.size);
return buildMetaData((DroidMediaCodecMetaData *)meta, md);
}
++key;
}
ALOGE("No handler for codec data for %s", ((DroidMediaCodecMetaData *)meta)->type);
return NULL;
}
return buildMetaData((DroidMediaCodecMetaData *)meta, md);
}
uint32_t flags(DroidMediaCodecEncoderMetaData *meta) {
#if ANDROID_MAJOR >= 7
return flags((DroidMediaCodecMetaData *)meta, 0);
#else
return flags((DroidMediaCodecMetaData *)meta, meta->meta_data ?
#if ANDROID_MAJOR >= 5
android::MediaCodecSource::FLAG_USE_METADATA_INPUT
#else
android::OMXCodec::kStoreMetaDataInVideoBuffers
#endif
: 0);
#endif
}
uint32_t flags(DroidMediaCodecDecoderMetaData *meta) {
return flags((DroidMediaCodecMetaData *)meta, 0);
}
android::sp<android::MetaData> buildMetaData(DroidMediaCodecMetaData *meta,
android::sp<android::MetaData> md) {
md->setCString(android::kKeyMIMEType, meta->type);
SET_PARAM(kKeyWidth, width);
SET_PARAM(kKeyHeight, height);
SET_PARAM(kKeyDisplayWidth, width);
SET_PARAM(kKeyDisplayHeight, height);
SET_PARAM(kKeyFrameRate, fps);
SET_PARAM(kKeyChannelCount, channels);
SET_PARAM(kKeySampleRate, sample_rate);
return md;
}
DroidMediaCodecEncoderMetaData *m_enc;
DroidMediaCodecDecoderMetaData *m_dec;
};
android::sp<android::MediaSource> droid_media_codec_create_encoder_raw(DroidMediaCodecEncoderMetaData *meta,
#if ANDROID_MAJOR >=5
android::sp<android::ALooper> looper,
#endif
android::sp<android::MediaSource> src)
{
DroidMediaCodecBuilder builder(meta);
#if ANDROID_MAJOR >=5
return builder.createCodec(src, NULL, looper);
#else
return builder.createCodec(src, NULL);
#endif
}
extern "C" {
DroidMediaBufferQueue *droid_media_codec_get_buffer_queue (DroidMediaCodec *codec)
{
return codec->m_queue.get();
}
DroidMediaCodec *droid_media_codec_create(DroidMediaCodecBuilder& builder)
{
android::sp<android::MetaData> md(builder.buildMetaData());
if (md == NULL) {
return NULL;
}
android::sp<Source> src(new Source(md));
android::sp<DroidMediaBufferQueue> queue;
android::sp<ANativeWindow> window = NULL;
DroidMediaCodecMetaData *meta = builder.meta();
if (builder.isEncoder()
|| meta->flags & DROID_MEDIA_CODEC_SW_ONLY
|| meta->flags & DROID_MEDIA_CODEC_NO_MEDIA_BUFFER) {
// Nothing
} else {
queue = new DroidMediaBufferQueue("DroidMediaCodecBufferQueue");
window = queue->window();
#if ANDROID_MAJOR >= 7
if (!queue->connectListener()) {
ALOGE("Failed to connect buffer queue listener");
return NULL;
}
#endif
}
#if ANDROID_MAJOR >= 5
android::sp<android::ALooper> looper = new android::ALooper;
looper->setName("DroidMediaCodecLoop");
looper->start();
android::sp<android::MediaSource> codec = builder.createCodec(src, window, looper, md);
#else
android::sp<android::MediaSource> codec = builder.createCodec(src, window, md);
#endif
if (codec == NULL) {
ALOGE("Failed to create codec");
return NULL;
}
DroidMediaCodec *mediaCodec = new DroidMediaCodec;
mediaCodec->m_codec = codec;
mediaCodec->m_src = src;
mediaCodec->m_queue = queue;
mediaCodec->m_window = window;
#if ANDROID_MAJOR >= 5
mediaCodec->m_looper = looper;
#endif
mediaCodec->m_useExternalLoop = (meta->flags & DROID_MEDIA_CODEC_USE_EXTERNAL_LOOP) ? true : false;
return mediaCodec;
}
DroidMediaCodec *droid_media_codec_create_decoder(DroidMediaCodecDecoderMetaData *meta)
{
DroidMediaCodecBuilder builder(meta);
return droid_media_codec_create(builder);
}
DroidMediaCodec *droid_media_codec_create_encoder(DroidMediaCodecEncoderMetaData *meta)
{
DroidMediaCodecBuilder builder(meta);
return droid_media_codec_create(builder);
}
bool droid_media_codec_is_supported(DroidMediaCodecMetaData *meta, bool encoder)
{
#if ANDROID_MAJOR == 4 && ANDROID_MINOR < 2
android::Vector<android::String8> matchingCodecs;
#elif ANDROID_MAJOR < 7
android::Vector<android::OMXCodec::CodecNameAndQuirks> matchingCodecs;
#else
android::Vector<android::AString> matchingCodecs;
#endif
#if ANDROID_MAJOR < 7
android::OMXCodec::findMatchingCodecs(
meta->type, encoder, NULL,
#else
android::MediaCodecList::findMatchingCodecs(
meta->type, encoder,
#endif
DroidMediaCodecBuilder::flags(meta, 0),
&matchingCodecs);
return matchingCodecs.size() > 0;
}
unsigned int droid_media_codec_get_supported_color_formats(DroidMediaCodecMetaData *meta, int encoder, uint32_t *formats, unsigned int maxFormats)
{
#if ANDROID_MAJOR >= 5
android::sp<android::MediaCodecList::IMediaCodecList> list = android::MediaCodecList::getInstance();
#else
const android::MediaCodecList *list = android::MediaCodecList::getInstance();
#endif
#if ANDROID_MAJOR == 4 && ANDROID_MINOR < 2
android::Vector<android::String8> matchingCodecs;
#elif ANDROID_MAJOR < 7
android::Vector<android::OMXCodec::CodecNameAndQuirks> matchingCodecs;
#else
android::Vector<android::AString> matchingCodecs;
#endif
#if ANDROID_MAJOR < 7
android::OMXCodec::findMatchingCodecs(
meta->type, encoder, NULL,
#else
android::MediaCodecList::findMatchingCodecs(
meta->type, encoder,
#endif
DroidMediaCodecBuilder::flags(meta, 0),
&matchingCodecs);
if (matchingCodecs.size() > 0) {
const char *matchName;
#if ANDROID_MAJOR == 4 && ANDROID_MINOR < 2
matchName = matchingCodecs.itemAt(0).string();
#elif ANDROID_MAJOR < 7
matchName = matchingCodecs.itemAt(0).mName.string();
#else
matchName = matchingCodecs.itemAt(0).c_str();
#endif
ssize_t matchIndex = list->findCodecByName(matchName);
#if ANDROID_MAJOR >= 5
const android::sp<android::MediaCodecInfo> info = list->getCodecInfo(matchIndex);
if (info != NULL) {
const android::sp<android::MediaCodecInfo::Capabilities> caps = info->getCapabilitiesFor(meta->type);
if (caps != NULL) {
android::Vector<uint32_t> colorFormats;
caps->getSupportedColorFormats(&colorFormats);
if (maxFormats > colorFormats.size()) {
maxFormats = colorFormats.size();
}
for (unsigned int i = 0; i < maxFormats; i++) {
formats[i] = colorFormats.itemAt(i);
}
return maxFormats;
}
}
#else
android::Vector<android::MediaCodecList::ProfileLevel> profileLevels;
android::Vector<uint32_t> colorFormats;
uint32_t flags;
android::status_t err = android::MediaCodecList::getInstance()->getCodecCapabilities(
matchIndex, meta->type, &profileLevels, &colorFormats, &flags);
if (err == android::OK) {
if (maxFormats > colorFormats.size()) {
maxFormats = colorFormats.size();
}
for (unsigned int i = 0; i < maxFormats; i++) {
formats[i] = colorFormats.itemAt(i);
}
return maxFormats;
}
#endif
}
return 0;
}
bool droid_media_codec_start(DroidMediaCodec *codec)
{
#if ANDROID_MAJOR < 7
if (codec->m_queue.get() != NULL) {
if (!codec->m_queue->connectListener()) {
ALOGE("Failed to connect buffer queue listener");
return false;
}
android::status_t err = native_window_api_connect(codec->m_window.get(),
NATIVE_WINDOW_API_MEDIA);
if (err != android::NO_ERROR) {
ALOGE("Failed to connect window");
return false;
}
}
#endif
int err = codec->m_codec->start();
if (err != android::OK) {
ALOGE("error 0x%x starting codec", -err);
return false;
}
return true;
}
void droid_media_codec_stop(DroidMediaCodec *codec)
{
if (codec->m_queue.get()) {
codec->m_queue->disconnectListener();
}
if (codec->m_thread != NULL) {
codec->m_thread->requestExit();
// in case the thread is waiting in ->read()
droid_media_codec_drain (codec);
}
#if ANDROID_MAJOR < 7
// On older devices the reader thread must be stopped first.
if (codec->m_thread != NULL) {
int err = codec->m_thread->requestExitAndWait();
if (err != android::NO_ERROR) {
ALOGE("Error 0x%x stopping thread", -err);
}
codec->m_thread.clear();
}
// All buffers are returned to HAL, it is safe to stop now.
int err = codec->m_codec->stop();
if (err != android::OK) {
ALOGE("error 0x%x stopping codec", -err);
}
#else
// Wake up AsyncCodecSource and send EOS to it.
int err = codec->m_codec->stop();
if (err != android::OK) {
ALOGE("error 0x%x stopping codec", -err);
}
if (codec->m_thread != NULL) {
int err = codec->m_thread->requestExitAndWait();
if (err != android::NO_ERROR) {
ALOGE("Error 0x%x stopping thread", -err);
}
codec->m_thread.clear();
}
#endif
if (codec->m_queue.get()) {
codec->m_queue->buffersReleased();
}
}
void droid_media_codec_destroy(DroidMediaCodec *codec)
{
delete codec;
}
void droid_media_codec_queue(DroidMediaCodec *codec, DroidMediaCodecData *data, DroidMediaBufferCallbacks *cb)
{
InputBuffer *buffer = new InputBuffer(data->data.data, data->data.size, cb->data, cb->unref);
#if ANDROID_MAJOR >= 9
buffer->meta_data().setInt32(android::kKeyIsSyncFrame, data->sync ? 1 : 0);
buffer->meta_data().setInt64(android::kKeyTime, data->ts);
#else
buffer->meta_data()->setInt32(android::kKeyIsSyncFrame, data->sync ? 1 : 0);
buffer->meta_data()->setInt64(android::kKeyTime, data->ts);
#endif
buffer->setObserver(codec);
buffer->set_range(0, data->data.size);
buffer->add_ref();
codec->m_src->add(buffer);
if (codec->m_useExternalLoop) {
return;
}
// Now start our looping thread
if (codec->m_thread == NULL) {
codec->m_thread = new DroidMediaCodecLoop(codec);
int err = codec->m_thread->run("DroidMediaCodecLoop");
if (err != android::NO_ERROR) {
ALOGE("Error 0x%x starting thread", -err);
if (codec->m_cb.error) {
codec->m_cb.error(codec->m_cb_data, err);
}
codec->m_thread.clear();
}
}
}
DroidMediaCodecLoopReturn droid_media_codec_loop(DroidMediaCodec *codec)
{
int err;
android::MediaBuffer *buffer = NULL;
#if ANDROID_MAJOR >= 9
err = codec->m_codec->read((android::MediaBufferBase **)&buffer);
#else
err = codec->m_codec->read(&buffer);
#endif
if (err == android::INFO_FORMAT_CHANGED) {
ALOGI("Format changed from codec");
if (codec->notifySizeChanged()) {
return DROID_MEDIA_CODEC_LOOP_OK;
} else {
return DROID_MEDIA_CODEC_LOOP_ERROR;
}
}
if (err == -EWOULDBLOCK
#if ANDROID_MAJOR >= 7
|| err == -ENODATA
#endif
) {
ALOGI("retry reading again. error: 0x%x", -err);
return DROID_MEDIA_CODEC_LOOP_OK;
}
if (err != android::OK) {
if (err == android::ERROR_END_OF_STREAM
#if ANDROID_MAJOR < 7
|| err == -ENODATA
#endif
) {