blob: 27f61318af4179c41d84e95cfca6259c7eb55114 [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
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 "NuPlayerDecoder"
19#include <utils/Log.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080020#include <inttypes.h>
Andreas Huberf9334412010-12-15 15:17:42 -080021
22#include "NuPlayerDecoder.h"
23
Lajos Molnar1cd13982014-01-17 15:12:51 -080024#include <media/ICrypto.h>
Chong Zhanga7fa1d92014-06-11 14:49:23 -070025#include <media/stagefright/foundation/ABitReader.h>
Andreas Huberf9334412010-12-15 15:17:42 -080026#include <media/stagefright/foundation/ABuffer.h>
27#include <media/stagefright/foundation/ADebug.h>
Andreas Huber5bc087c2010-12-23 10:27:40 -080028#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070029#include <media/stagefright/MediaBuffer.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080030#include <media/stagefright/MediaCodec.h>
Andreas Huberf9334412010-12-15 15:17:42 -080031#include <media/stagefright/MediaDefs.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080032#include <media/stagefright/MediaErrors.h>
Andreas Huberf9334412010-12-15 15:17:42 -080033
34namespace android {
35
36NuPlayer::Decoder::Decoder(
Glenn Kasten11731182011-02-08 17:26:17 -080037 const sp<AMessage> &notify,
38 const sp<NativeWindowWrapper> &nativeWindow)
Andreas Huberf9334412010-12-15 15:17:42 -080039 : mNotify(notify),
Lajos Molnar1cd13982014-01-17 15:12:51 -080040 mNativeWindow(nativeWindow),
41 mBufferGeneration(0),
Wei Jia704e7262014-06-04 16:21:56 -070042 mPaused(true),
Lajos Molnar1cd13982014-01-17 15:12:51 -080043 mComponentName("decoder") {
44 // Every decoder has its own looper because MediaCodec operations
45 // are blocking, but NuPlayer needs asynchronous operations.
46 mDecoderLooper = new ALooper;
Marco Nelissen9e2b7912014-08-18 16:13:03 -070047 mDecoderLooper->setName("NPDecoder");
Lajos Molnar1cd13982014-01-17 15:12:51 -080048 mDecoderLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
49
50 mCodecLooper = new ALooper;
Marco Nelissen9e2b7912014-08-18 16:13:03 -070051 mCodecLooper->setName("NPDecoder-CL");
Lajos Molnar1cd13982014-01-17 15:12:51 -080052 mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
Andreas Huberf9334412010-12-15 15:17:42 -080053}
54
55NuPlayer::Decoder::~Decoder() {
Wei Jia4923cee2014-09-24 14:25:19 -070056 mDecoderLooper->unregisterHandler(id());
57 mDecoderLooper->stop();
58
59 releaseAndResetMediaBuffers();
Andreas Huberf9334412010-12-15 15:17:42 -080060}
61
Lajos Molnar09524832014-07-17 14:29:51 -070062static
63status_t PostAndAwaitResponse(
64 const sp<AMessage> &msg, sp<AMessage> *response) {
65 status_t err = msg->postAndAwaitResponse(response);
66
67 if (err != OK) {
68 return err;
69 }
70
71 if (!(*response)->findInt32("err", &err)) {
72 err = OK;
73 }
74
75 return err;
76}
77
Lajos Molnar87603c02014-08-20 19:25:30 -070078void NuPlayer::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) {
79 mCSDsForCurrentFormat.clear();
80 for (int32_t i = 0; ; ++i) {
81 AString tag = "csd-";
82 tag.append(i);
83 sp<ABuffer> buffer;
84 if (!format->findBuffer(tag.c_str(), &buffer)) {
85 break;
86 }
87 mCSDsForCurrentFormat.push(buffer);
88 }
89}
90
Lajos Molnar1cd13982014-01-17 15:12:51 -080091void NuPlayer::Decoder::onConfigure(const sp<AMessage> &format) {
Andreas Huberf9334412010-12-15 15:17:42 -080092 CHECK(mCodec == NULL);
Andreas Huberf9334412010-12-15 15:17:42 -080093
Lajos Molnar1cd13982014-01-17 15:12:51 -080094 ++mBufferGeneration;
95
Andreas Huber84066782011-08-16 09:34:26 -070096 AString mime;
97 CHECK(format->findString("mime", &mime));
Andreas Huberf9334412010-12-15 15:17:42 -080098
Lajos Molnar1cd13982014-01-17 15:12:51 -080099 sp<Surface> surface = NULL;
100 if (mNativeWindow != NULL) {
101 surface = mNativeWindow->getSurfaceTextureClient();
Andreas Huber84066782011-08-16 09:34:26 -0700102 }
Andreas Huberf9334412010-12-15 15:17:42 -0800103
Lajos Molnar1cd13982014-01-17 15:12:51 -0800104 mComponentName = mime;
105 mComponentName.append(" decoder");
106 ALOGV("[%s] onConfigure (surface=%p)", mComponentName.c_str(), surface.get());
107
108 mCodec = MediaCodec::CreateByType(mCodecLooper, mime.c_str(), false /* encoder */);
Lajos Molnar09524832014-07-17 14:29:51 -0700109 int32_t secure = 0;
110 if (format->findInt32("secure", &secure) && secure != 0) {
111 if (mCodec != NULL) {
112 mCodec->getName(&mComponentName);
113 mComponentName.append(".secure");
114 mCodec->release();
115 ALOGI("[%s] creating", mComponentName.c_str());
116 mCodec = MediaCodec::CreateByComponentName(
117 mCodecLooper, mComponentName.c_str());
118 }
119 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800120 if (mCodec == NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700121 ALOGE("Failed to create %s%s decoder",
122 (secure ? "secure " : ""), mime.c_str());
Lajos Molnar1cd13982014-01-17 15:12:51 -0800123 handleError(UNKNOWN_ERROR);
124 return;
125 }
126
127 mCodec->getName(&mComponentName);
128
Lajos Molnar14986f62014-09-15 11:04:44 -0700129 status_t err;
Glenn Kasten11731182011-02-08 17:26:17 -0800130 if (mNativeWindow != NULL) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800131 // disconnect from surface as MediaCodec will reconnect
Lajos Molnar14986f62014-09-15 11:04:44 -0700132 err = native_window_api_disconnect(
133 surface.get(), NATIVE_WINDOW_API_MEDIA);
134 // We treat this as a warning, as this is a preparatory step.
135 // Codec will try to connect to the surface, which is where
136 // any error signaling will occur.
137 ALOGW_IF(err != OK, "failed to disconnect from surface: %d", err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800138 }
Lajos Molnar14986f62014-09-15 11:04:44 -0700139 err = mCodec->configure(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800140 format, surface, NULL /* crypto */, 0 /* flags */);
141 if (err != OK) {
142 ALOGE("Failed to configure %s decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700143 mCodec->release();
144 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800145 handleError(err);
146 return;
147 }
Lajos Molnar87603c02014-08-20 19:25:30 -0700148 rememberCodecSpecificData(format);
149
Lajos Molnar1cd13982014-01-17 15:12:51 -0800150 // the following should work in configured state
151 CHECK_EQ((status_t)OK, mCodec->getOutputFormat(&mOutputFormat));
152 CHECK_EQ((status_t)OK, mCodec->getInputFormat(&mInputFormat));
153
154 err = mCodec->start();
155 if (err != OK) {
156 ALOGE("Failed to start %s decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700157 mCodec->release();
158 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800159 handleError(err);
160 return;
Andreas Huberf9334412010-12-15 15:17:42 -0800161 }
162
Lajos Molnar1cd13982014-01-17 15:12:51 -0800163 // the following should work after start
164 CHECK_EQ((status_t)OK, mCodec->getInputBuffers(&mInputBuffers));
Lajos Molnar09524832014-07-17 14:29:51 -0700165 releaseAndResetMediaBuffers();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800166 CHECK_EQ((status_t)OK, mCodec->getOutputBuffers(&mOutputBuffers));
167 ALOGV("[%s] got %zu input and %zu output buffers",
168 mComponentName.c_str(),
169 mInputBuffers.size(),
170 mOutputBuffers.size());
Andreas Huber078cfcf2011-09-15 12:25:04 -0700171
Lajos Molnar1cd13982014-01-17 15:12:51 -0800172 requestCodecNotification();
Wei Jia704e7262014-06-04 16:21:56 -0700173 mPaused = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800174}
Andreas Huber078cfcf2011-09-15 12:25:04 -0700175
Lajos Molnar09524832014-07-17 14:29:51 -0700176void NuPlayer::Decoder::releaseAndResetMediaBuffers() {
177 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
178 if (mMediaBuffers[i] != NULL) {
179 mMediaBuffers[i]->release();
180 mMediaBuffers.editItemAt(i) = NULL;
181 }
182 }
183 mMediaBuffers.resize(mInputBuffers.size());
Wei Jia81e50d02014-07-24 10:28:47 -0700184 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
185 mMediaBuffers.editItemAt(i) = NULL;
186 }
Lajos Molnar09524832014-07-17 14:29:51 -0700187 mInputBufferIsDequeued.clear();
188 mInputBufferIsDequeued.resize(mInputBuffers.size());
Wei Jia81e50d02014-07-24 10:28:47 -0700189 for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) {
190 mInputBufferIsDequeued.editItemAt(i) = false;
191 }
Wei Jia2245fc62014-10-02 15:12:25 -0700192
193 mPendingInputMessages.clear();
Lajos Molnar09524832014-07-17 14:29:51 -0700194}
195
Lajos Molnar1cd13982014-01-17 15:12:51 -0800196void NuPlayer::Decoder::requestCodecNotification() {
197 if (mCodec != NULL) {
198 sp<AMessage> reply = new AMessage(kWhatCodecNotify, id());
199 reply->setInt32("generation", mBufferGeneration);
200 mCodec->requestActivityNotification(reply);
201 }
202}
203
204bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) {
205 int32_t generation;
206 CHECK(msg->findInt32("generation", &generation));
207 return generation != mBufferGeneration;
208}
209
210void NuPlayer::Decoder::init() {
211 mDecoderLooper->registerHandler(this);
212}
213
214void NuPlayer::Decoder::configure(const sp<AMessage> &format) {
215 sp<AMessage> msg = new AMessage(kWhatConfigure, id());
216 msg->setMessage("format", format);
217 msg->post();
218}
219
Lajos Molnar87603c02014-08-20 19:25:30 -0700220void NuPlayer::Decoder::signalUpdateFormat(const sp<AMessage> &format) {
221 sp<AMessage> msg = new AMessage(kWhatUpdateFormat, id());
222 msg->setMessage("format", format);
223 msg->post();
224}
225
Lajos Molnar09524832014-07-17 14:29:51 -0700226status_t NuPlayer::Decoder::getInputBuffers(Vector<sp<ABuffer> > *buffers) const {
227 sp<AMessage> msg = new AMessage(kWhatGetInputBuffers, id());
228 msg->setPointer("buffers", buffers);
229
230 sp<AMessage> response;
231 return PostAndAwaitResponse(msg, &response);
232}
233
Lajos Molnar1cd13982014-01-17 15:12:51 -0800234void NuPlayer::Decoder::handleError(int32_t err)
235{
Andy Hungcf31f1e2014-09-23 14:59:01 -0700236 // We cannot immediately release the codec due to buffers still outstanding
237 // in the renderer. We signal to the player the error so it can shutdown/release the
238 // decoder after flushing and increment the generation to discard unnecessary messages.
239
240 ++mBufferGeneration;
Wei Jiac22c6952014-08-29 14:47:50 -0700241
Lajos Molnar1cd13982014-01-17 15:12:51 -0800242 sp<AMessage> notify = mNotify->dup();
243 notify->setInt32("what", kWhatError);
244 notify->setInt32("err", err);
245 notify->post();
246}
247
248bool NuPlayer::Decoder::handleAnInputBuffer() {
249 size_t bufferIx = -1;
250 status_t res = mCodec->dequeueInputBuffer(&bufferIx);
251 ALOGV("[%s] dequeued input: %d",
252 mComponentName.c_str(), res == OK ? (int)bufferIx : res);
253 if (res != OK) {
254 if (res != -EAGAIN) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700255 ALOGE("Failed to dequeue input buffer for %s (err=%d)",
256 mComponentName.c_str(), res);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800257 handleError(res);
258 }
259 return false;
Andreas Huber078cfcf2011-09-15 12:25:04 -0700260 }
261
Lajos Molnar1cd13982014-01-17 15:12:51 -0800262 CHECK_LT(bufferIx, mInputBuffers.size());
Andreas Huberf9334412010-12-15 15:17:42 -0800263
Lajos Molnar09524832014-07-17 14:29:51 -0700264 if (mMediaBuffers[bufferIx] != NULL) {
265 mMediaBuffers[bufferIx]->release();
266 mMediaBuffers.editItemAt(bufferIx) = NULL;
267 }
268 mInputBufferIsDequeued.editItemAt(bufferIx) = true;
269
Lajos Molnar1cd13982014-01-17 15:12:51 -0800270 sp<AMessage> reply = new AMessage(kWhatInputBufferFilled, id());
271 reply->setSize("buffer-ix", bufferIx);
272 reply->setInt32("generation", mBufferGeneration);
273
Lajos Molnar87603c02014-08-20 19:25:30 -0700274 if (!mCSDsToSubmit.isEmpty()) {
275 sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0);
276 ALOGI("[%s] resubmitting CSD", mComponentName.c_str());
277 reply->setBuffer("buffer", buffer);
278 mCSDsToSubmit.removeAt(0);
Wei Jia2245fc62014-10-02 15:12:25 -0700279 CHECK(onInputBufferFilled(reply));
280 return true;
281 }
282
283 while (!mPendingInputMessages.empty()) {
284 sp<AMessage> msg = *mPendingInputMessages.begin();
285 if (!onInputBufferFilled(msg)) {
286 break;
287 }
288 mPendingInputMessages.erase(mPendingInputMessages.begin());
289 }
290
291 if (!mInputBufferIsDequeued.editItemAt(bufferIx)) {
Lajos Molnar87603c02014-08-20 19:25:30 -0700292 return true;
293 }
294
Lajos Molnar1cd13982014-01-17 15:12:51 -0800295 sp<AMessage> notify = mNotify->dup();
296 notify->setInt32("what", kWhatFillThisBuffer);
297 notify->setBuffer("buffer", mInputBuffers[bufferIx]);
298 notify->setMessage("reply", reply);
299 notify->post();
300 return true;
301}
302
Wei Jia2245fc62014-10-02 15:12:25 -0700303bool android::NuPlayer::Decoder::onInputBufferFilled(const sp<AMessage> &msg) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800304 size_t bufferIx;
305 CHECK(msg->findSize("buffer-ix", &bufferIx));
306 CHECK_LT(bufferIx, mInputBuffers.size());
307 sp<ABuffer> codecBuffer = mInputBuffers[bufferIx];
308
309 sp<ABuffer> buffer;
310 bool hasBuffer = msg->findBuffer("buffer", &buffer);
Lajos Molnar09524832014-07-17 14:29:51 -0700311
312 // handle widevine classic source - that fills an arbitrary input buffer
313 MediaBuffer *mediaBuffer = NULL;
Wei Jia96e92b52014-09-18 17:36:20 -0700314 if (hasBuffer) {
315 mediaBuffer = (MediaBuffer *)(buffer->getMediaBufferBase());
316 if (mediaBuffer != NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700317 // likely filled another buffer than we requested: adjust buffer index
318 size_t ix;
319 for (ix = 0; ix < mInputBuffers.size(); ix++) {
320 const sp<ABuffer> &buf = mInputBuffers[ix];
321 if (buf->data() == mediaBuffer->data()) {
322 // all input buffers are dequeued on start, hence the check
Wei Jia2245fc62014-10-02 15:12:25 -0700323 if (!mInputBufferIsDequeued[ix]) {
324 ALOGV("[%s] received MediaBuffer for #%zu instead of #%zu",
325 mComponentName.c_str(), ix, bufferIx);
326 mediaBuffer->release();
327 return false;
328 }
Lajos Molnar09524832014-07-17 14:29:51 -0700329
330 // TRICKY: need buffer for the metadata, so instead, set
331 // codecBuffer to the same (though incorrect) buffer to
332 // avoid a memcpy into the codecBuffer
333 codecBuffer = buffer;
334 codecBuffer->setRange(
335 mediaBuffer->range_offset(),
336 mediaBuffer->range_length());
337 bufferIx = ix;
338 break;
339 }
340 }
341 CHECK(ix < mInputBuffers.size());
342 }
343 }
344
Andy Hungcf31f1e2014-09-23 14:59:01 -0700345
Lajos Molnar09524832014-07-17 14:29:51 -0700346
Lajos Molnar1cd13982014-01-17 15:12:51 -0800347 if (buffer == NULL /* includes !hasBuffer */) {
348 int32_t streamErr = ERROR_END_OF_STREAM;
349 CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
350
351 if (streamErr == OK) {
352 /* buffers are returned to hold on to */
Wei Jia2245fc62014-10-02 15:12:25 -0700353 return true;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800354 }
355
356 // attempt to queue EOS
357 status_t err = mCodec->queueInputBuffer(
358 bufferIx,
359 0,
360 0,
361 0,
362 MediaCodec::BUFFER_FLAG_EOS);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700363 if (err == OK) {
364 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
365 } else if (streamErr == ERROR_END_OF_STREAM) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800366 streamErr = err;
367 // err will not be ERROR_END_OF_STREAM
368 }
369
370 if (streamErr != ERROR_END_OF_STREAM) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700371 ALOGE("Stream error for %s (err=%d), EOS %s queued",
372 mComponentName.c_str(),
373 streamErr,
374 err == OK ? "successfully" : "unsuccessfully");
Lajos Molnar1cd13982014-01-17 15:12:51 -0800375 handleError(streamErr);
376 }
377 } else {
378 int64_t timeUs = 0;
379 uint32_t flags = 0;
380 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
381
Lajos Molnar87603c02014-08-20 19:25:30 -0700382 int32_t eos, csd;
383 // we do not expect SYNCFRAME for decoder
Lajos Molnar1cd13982014-01-17 15:12:51 -0800384 if (buffer->meta()->findInt32("eos", &eos) && eos) {
385 flags |= MediaCodec::BUFFER_FLAG_EOS;
Lajos Molnar87603c02014-08-20 19:25:30 -0700386 } else if (buffer->meta()->findInt32("csd", &csd) && csd) {
387 flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800388 }
389
390 // copy into codec buffer
391 if (buffer != codecBuffer) {
392 CHECK_LE(buffer->size(), codecBuffer->capacity());
393 codecBuffer->setRange(0, buffer->size());
394 memcpy(codecBuffer->data(), buffer->data(), buffer->size());
395 }
396
397 status_t err = mCodec->queueInputBuffer(
398 bufferIx,
399 codecBuffer->offset(),
400 codecBuffer->size(),
401 timeUs,
402 flags);
403 if (err != OK) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700404 if (mediaBuffer != NULL) {
405 mediaBuffer->release();
406 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800407 ALOGE("Failed to queue input buffer for %s (err=%d)",
408 mComponentName.c_str(), err);
409 handleError(err);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700410 } else {
411 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
412 if (mediaBuffer != NULL) {
413 CHECK(mMediaBuffers[bufferIx] == NULL);
414 mMediaBuffers.editItemAt(bufferIx) = mediaBuffer;
415 }
Lajos Molnar09524832014-07-17 14:29:51 -0700416 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800417 }
Wei Jia2245fc62014-10-02 15:12:25 -0700418 return true;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800419}
420
421bool NuPlayer::Decoder::handleAnOutputBuffer() {
422 size_t bufferIx = -1;
423 size_t offset;
424 size_t size;
425 int64_t timeUs;
426 uint32_t flags;
427 status_t res = mCodec->dequeueOutputBuffer(
428 &bufferIx, &offset, &size, &timeUs, &flags);
429
430 if (res != OK) {
431 ALOGV("[%s] dequeued output: %d", mComponentName.c_str(), res);
432 } else {
433 ALOGV("[%s] dequeued output: %d (time=%lld flags=%" PRIu32 ")",
434 mComponentName.c_str(), (int)bufferIx, timeUs, flags);
435 }
436
437 if (res == INFO_OUTPUT_BUFFERS_CHANGED) {
438 res = mCodec->getOutputBuffers(&mOutputBuffers);
439 if (res != OK) {
440 ALOGE("Failed to get output buffers for %s after INFO event (err=%d)",
441 mComponentName.c_str(), res);
442 handleError(res);
443 return false;
444 }
445 // NuPlayer ignores this
446 return true;
447 } else if (res == INFO_FORMAT_CHANGED) {
448 sp<AMessage> format = new AMessage();
449 res = mCodec->getOutputFormat(&format);
450 if (res != OK) {
451 ALOGE("Failed to get output format for %s after INFO event (err=%d)",
452 mComponentName.c_str(), res);
453 handleError(res);
454 return false;
455 }
456
457 sp<AMessage> notify = mNotify->dup();
458 notify->setInt32("what", kWhatOutputFormatChanged);
459 notify->setMessage("format", format);
460 notify->post();
461 return true;
462 } else if (res == INFO_DISCONTINUITY) {
463 // nothing to do
464 return true;
465 } else if (res != OK) {
466 if (res != -EAGAIN) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700467 ALOGE("Failed to dequeue output buffer for %s (err=%d)",
468 mComponentName.c_str(), res);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800469 handleError(res);
470 }
471 return false;
472 }
473
474 CHECK_LT(bufferIx, mOutputBuffers.size());
475 sp<ABuffer> buffer = mOutputBuffers[bufferIx];
476 buffer->setRange(offset, size);
477 buffer->meta()->clear();
478 buffer->meta()->setInt64("timeUs", timeUs);
479 if (flags & MediaCodec::BUFFER_FLAG_EOS) {
480 buffer->meta()->setInt32("eos", true);
481 }
482 // we do not expect CODECCONFIG or SYNCFRAME for decoder
483
484 sp<AMessage> reply = new AMessage(kWhatRenderBuffer, id());
485 reply->setSize("buffer-ix", bufferIx);
486 reply->setInt32("generation", mBufferGeneration);
487
488 sp<AMessage> notify = mNotify->dup();
489 notify->setInt32("what", kWhatDrainThisBuffer);
490 notify->setBuffer("buffer", buffer);
491 notify->setMessage("reply", reply);
492 notify->post();
493
494 // FIXME: This should be handled after rendering is complete,
495 // but Renderer needs it now
496 if (flags & MediaCodec::BUFFER_FLAG_EOS) {
497 ALOGV("queueing eos [%s]", mComponentName.c_str());
498 sp<AMessage> notify = mNotify->dup();
499 notify->setInt32("what", kWhatEOS);
500 notify->setInt32("err", ERROR_END_OF_STREAM);
501 notify->post();
502 }
503 return true;
504}
505
506void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) {
507 status_t err;
508 int32_t render;
509 size_t bufferIx;
510 CHECK(msg->findSize("buffer-ix", &bufferIx));
511 if (msg->findInt32("render", &render) && render) {
Lajos Molnardc43dfa2014-05-07 15:33:04 -0700512 int64_t timestampNs;
513 CHECK(msg->findInt64("timestampNs", &timestampNs));
514 err = mCodec->renderOutputBufferAndRelease(bufferIx, timestampNs);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800515 } else {
516 err = mCodec->releaseOutputBuffer(bufferIx);
517 }
518 if (err != OK) {
519 ALOGE("failed to release output buffer for %s (err=%d)",
520 mComponentName.c_str(), err);
521 handleError(err);
522 }
523}
524
525void NuPlayer::Decoder::onFlush() {
526 status_t err = OK;
527 if (mCodec != NULL) {
528 err = mCodec->flush();
Lajos Molnar87603c02014-08-20 19:25:30 -0700529 mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator
Lajos Molnar1cd13982014-01-17 15:12:51 -0800530 ++mBufferGeneration;
531 }
532
533 if (err != OK) {
534 ALOGE("failed to flush %s (err=%d)", mComponentName.c_str(), err);
535 handleError(err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700536 // finish with posting kWhatFlushCompleted.
537 // we attempt to release the buffers even if flush fails.
Lajos Molnar1cd13982014-01-17 15:12:51 -0800538 }
Lajos Molnar09524832014-07-17 14:29:51 -0700539 releaseAndResetMediaBuffers();
540
Lajos Molnar1cd13982014-01-17 15:12:51 -0800541 sp<AMessage> notify = mNotify->dup();
542 notify->setInt32("what", kWhatFlushCompleted);
543 notify->post();
Wei Jia704e7262014-06-04 16:21:56 -0700544 mPaused = true;
545}
546
547void NuPlayer::Decoder::onResume() {
548 mPaused = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800549}
550
551void NuPlayer::Decoder::onShutdown() {
552 status_t err = OK;
553 if (mCodec != NULL) {
554 err = mCodec->release();
555 mCodec = NULL;
556 ++mBufferGeneration;
557
558 if (mNativeWindow != NULL) {
559 // reconnect to surface as MediaCodec disconnected from it
Wei Jia3fb9f682014-08-20 14:30:09 -0700560 status_t error =
Lajos Molnar1cd13982014-01-17 15:12:51 -0800561 native_window_api_connect(
562 mNativeWindow->getNativeWindow().get(),
Wei Jia3fb9f682014-08-20 14:30:09 -0700563 NATIVE_WINDOW_API_MEDIA);
564 ALOGW_IF(error != NO_ERROR,
565 "[%s] failed to connect to native window, error=%d",
566 mComponentName.c_str(), error);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800567 }
568 mComponentName = "decoder";
569 }
570
Lajos Molnar09524832014-07-17 14:29:51 -0700571 releaseAndResetMediaBuffers();
572
Lajos Molnar1cd13982014-01-17 15:12:51 -0800573 if (err != OK) {
574 ALOGE("failed to release %s (err=%d)", mComponentName.c_str(), err);
575 handleError(err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700576 // finish with posting kWhatShutdownCompleted.
Lajos Molnar1cd13982014-01-17 15:12:51 -0800577 }
578
579 sp<AMessage> notify = mNotify->dup();
580 notify->setInt32("what", kWhatShutdownCompleted);
581 notify->post();
Wei Jia704e7262014-06-04 16:21:56 -0700582 mPaused = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800583}
584
585void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800586 ALOGV("[%s] onMessage: %s", mComponentName.c_str(), msg->debugString().c_str());
587
Andreas Huberf9334412010-12-15 15:17:42 -0800588 switch (msg->what()) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800589 case kWhatConfigure:
590 {
591 sp<AMessage> format;
592 CHECK(msg->findMessage("format", &format));
593 onConfigure(format);
594 break;
595 }
596
Lajos Molnar87603c02014-08-20 19:25:30 -0700597 case kWhatUpdateFormat:
598 {
599 sp<AMessage> format;
600 CHECK(msg->findMessage("format", &format));
601 rememberCodecSpecificData(format);
602 break;
603 }
604
Lajos Molnar09524832014-07-17 14:29:51 -0700605 case kWhatGetInputBuffers:
606 {
607 uint32_t replyID;
608 CHECK(msg->senderAwaitsResponse(&replyID));
609
610 Vector<sp<ABuffer> > *dstBuffers;
611 CHECK(msg->findPointer("buffers", (void **)&dstBuffers));
612
613 dstBuffers->clear();
614 for (size_t i = 0; i < mInputBuffers.size(); i++) {
615 dstBuffers->push(mInputBuffers[i]);
616 }
617
618 (new AMessage)->postReply(replyID);
619 break;
620 }
621
Andreas Huberf9334412010-12-15 15:17:42 -0800622 case kWhatCodecNotify:
623 {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800624 if (!isStaleReply(msg)) {
Chong Zhange47d4442014-09-30 13:12:22 -0700625 int32_t numInput, numOutput;
626
627 if (!msg->findInt32("input-buffers", &numInput)) {
628 numInput = INT32_MAX;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800629 }
Andreas Huberf9334412010-12-15 15:17:42 -0800630
Chong Zhange47d4442014-09-30 13:12:22 -0700631 if (!msg->findInt32("output-buffers", &numOutput)) {
632 numOutput = INT32_MAX;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800633 }
Chong Zhange47d4442014-09-30 13:12:22 -0700634
635 if (!mPaused) {
636 while (numInput-- > 0 && handleAnInputBuffer()) {}
637 }
638
639 while (numOutput-- > 0 && handleAnOutputBuffer()) {}
Andreas Huberf9334412010-12-15 15:17:42 -0800640 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800641
642 requestCodecNotification();
643 break;
644 }
645
646 case kWhatInputBufferFilled:
647 {
648 if (!isStaleReply(msg)) {
Wei Jia2245fc62014-10-02 15:12:25 -0700649 if (!mPendingInputMessages.empty()
650 || !onInputBufferFilled(msg)) {
651 mPendingInputMessages.push_back(msg);
652 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800653 }
Lajos Molnarb9b87fe2014-09-10 13:53:21 -0700654
Lajos Molnar1cd13982014-01-17 15:12:51 -0800655 break;
656 }
657
658 case kWhatRenderBuffer:
659 {
660 if (!isStaleReply(msg)) {
661 onRenderBuffer(msg);
662 }
663 break;
664 }
665
666 case kWhatFlush:
667 {
Lajos Molnar87603c02014-08-20 19:25:30 -0700668 sp<AMessage> format;
669 if (msg->findMessage("new-format", &format)) {
670 rememberCodecSpecificData(format);
671 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800672 onFlush();
673 break;
674 }
675
Wei Jia704e7262014-06-04 16:21:56 -0700676 case kWhatResume:
677 {
678 onResume();
679 break;
680 }
681
Lajos Molnar1cd13982014-01-17 15:12:51 -0800682 case kWhatShutdown:
683 {
684 onShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800685 break;
686 }
687
688 default:
689 TRESPASS();
690 break;
691 }
692}
693
Lajos Molnar87603c02014-08-20 19:25:30 -0700694void NuPlayer::Decoder::signalFlush(const sp<AMessage> &format) {
695 sp<AMessage> msg = new AMessage(kWhatFlush, id());
696 if (format != NULL) {
697 msg->setMessage("new-format", format);
698 }
699 msg->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800700}
701
702void NuPlayer::Decoder::signalResume() {
Wei Jia704e7262014-06-04 16:21:56 -0700703 (new AMessage(kWhatResume, id()))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800704}
705
Andreas Huber3831a062010-12-21 10:22:33 -0800706void NuPlayer::Decoder::initiateShutdown() {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800707 (new AMessage(kWhatShutdown, id()))->post();
Andreas Huber3831a062010-12-21 10:22:33 -0800708}
709
Robert Shih6d0a94e2014-01-23 16:18:22 -0800710bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(const sp<AMessage> &targetFormat) const {
711 if (targetFormat == NULL) {
712 return true;
713 }
714
715 AString mime;
716 if (!targetFormat->findString("mime", &mime)) {
717 return false;
718 }
719
720 if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
721 // field-by-field comparison
722 const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
723 for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
724 int32_t oldVal, newVal;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800725 if (!mOutputFormat->findInt32(keys[i], &oldVal) ||
726 !targetFormat->findInt32(keys[i], &newVal) ||
727 oldVal != newVal) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800728 return false;
729 }
730 }
731
732 sp<ABuffer> oldBuf, newBuf;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800733 if (mOutputFormat->findBuffer("csd-0", &oldBuf) &&
734 targetFormat->findBuffer("csd-0", &newBuf)) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800735 if (oldBuf->size() != newBuf->size()) {
736 return false;
737 }
738 return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size());
739 }
740 }
741 return false;
742}
743
744bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800745 if (mOutputFormat == NULL) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800746 return false;
747 }
748
749 if (targetFormat == NULL) {
750 return true;
751 }
752
753 AString oldMime, newMime;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800754 if (!mOutputFormat->findString("mime", &oldMime)
Robert Shih6d0a94e2014-01-23 16:18:22 -0800755 || !targetFormat->findString("mime", &newMime)
756 || !(oldMime == newMime)) {
757 return false;
758 }
759
760 bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
761 bool seamless;
762 if (audio) {
763 seamless = supportsSeamlessAudioFormatChange(targetFormat);
764 } else {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800765 int32_t isAdaptive;
766 seamless = (mCodec != NULL &&
767 mInputFormat->findInt32("adaptive-playback", &isAdaptive) &&
768 isAdaptive);
Robert Shih6d0a94e2014-01-23 16:18:22 -0800769 }
770
771 ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
772 return seamless;
773}
774
Chong Zhangb86e68f2014-08-01 13:46:53 -0700775struct CCData {
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700776 CCData(uint8_t type, uint8_t data1, uint8_t data2)
777 : mType(type), mData1(data1), mData2(data2) {
778 }
Chong Zhangb86e68f2014-08-01 13:46:53 -0700779 bool getChannel(size_t *channel) const {
780 if (mData1 >= 0x10 && mData1 <= 0x1f) {
781 *channel = (mData1 >= 0x18 ? 1 : 0) + (mType ? 2 : 0);
782 return true;
783 }
784 return false;
785 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700786
787 uint8_t mType;
788 uint8_t mData1;
789 uint8_t mData2;
790};
791
Chong Zhangb86e68f2014-08-01 13:46:53 -0700792static bool isNullPad(CCData *cc) {
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700793 return cc->mData1 < 0x10 && cc->mData2 < 0x10;
794}
795
Chong Zhangb86e68f2014-08-01 13:46:53 -0700796static void dumpBytePair(const sp<ABuffer> &ccBuf) {
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700797 size_t offset = 0;
798 AString out;
799
800 while (offset < ccBuf->size()) {
801 char tmp[128];
802
803 CCData *cc = (CCData *) (ccBuf->data() + offset);
804
805 if (isNullPad(cc)) {
806 // 1 null pad or XDS metadata, ignore
807 offset += sizeof(CCData);
808 continue;
809 }
810
811 if (cc->mData1 >= 0x20 && cc->mData1 <= 0x7f) {
812 // 2 basic chars
813 sprintf(tmp, "[%d]Basic: %c %c", cc->mType, cc->mData1, cc->mData2);
814 } else if ((cc->mData1 == 0x11 || cc->mData1 == 0x19)
815 && cc->mData2 >= 0x30 && cc->mData2 <= 0x3f) {
816 // 1 special char
817 sprintf(tmp, "[%d]Special: %02x %02x", cc->mType, cc->mData1, cc->mData2);
818 } else if ((cc->mData1 == 0x12 || cc->mData1 == 0x1A)
819 && cc->mData2 >= 0x20 && cc->mData2 <= 0x3f){
820 // 1 Spanish/French char
821 sprintf(tmp, "[%d]Spanish: %02x %02x", cc->mType, cc->mData1, cc->mData2);
822 } else if ((cc->mData1 == 0x13 || cc->mData1 == 0x1B)
823 && cc->mData2 >= 0x20 && cc->mData2 <= 0x3f){
824 // 1 Portuguese/German/Danish char
825 sprintf(tmp, "[%d]German: %02x %02x", cc->mType, cc->mData1, cc->mData2);
826 } else if ((cc->mData1 == 0x11 || cc->mData1 == 0x19)
827 && cc->mData2 >= 0x20 && cc->mData2 <= 0x2f){
828 // Mid-Row Codes (Table 69)
829 sprintf(tmp, "[%d]Mid-row: %02x %02x", cc->mType, cc->mData1, cc->mData2);
830 } else if (((cc->mData1 == 0x14 || cc->mData1 == 0x1c)
831 && cc->mData2 >= 0x20 && cc->mData2 <= 0x2f)
832 ||
833 ((cc->mData1 == 0x17 || cc->mData1 == 0x1f)
834 && cc->mData2 >= 0x21 && cc->mData2 <= 0x23)){
835 // Misc Control Codes (Table 70)
836 sprintf(tmp, "[%d]Ctrl: %02x %02x", cc->mType, cc->mData1, cc->mData2);
837 } else if ((cc->mData1 & 0x70) == 0x10
838 && (cc->mData2 & 0x40) == 0x40
839 && ((cc->mData1 & 0x07) || !(cc->mData2 & 0x20)) ) {
840 // Preamble Address Codes (Table 71)
841 sprintf(tmp, "[%d]PAC: %02x %02x", cc->mType, cc->mData1, cc->mData2);
842 } else {
843 sprintf(tmp, "[%d]Invalid: %02x %02x", cc->mType, cc->mData1, cc->mData2);
844 }
845
846 if (out.size() > 0) {
847 out.append(", ");
848 }
849
850 out.append(tmp);
851
852 offset += sizeof(CCData);
853 }
854
855 ALOGI("%s", out.c_str());
856}
857
Chong Zhangb86e68f2014-08-01 13:46:53 -0700858NuPlayer::CCDecoder::CCDecoder(const sp<AMessage> &notify)
859 : mNotify(notify),
860 mCurrentChannel(0),
861 mSelectedTrack(-1) {
862 for (size_t i = 0; i < sizeof(mTrackIndices)/sizeof(mTrackIndices[0]); ++i) {
863 mTrackIndices[i] = -1;
864 }
865}
866
867size_t NuPlayer::CCDecoder::getTrackCount() const {
868 return mFoundChannels.size();
869}
870
871sp<AMessage> NuPlayer::CCDecoder::getTrackInfo(size_t index) const {
872 if (!isTrackValid(index)) {
873 return NULL;
874 }
875
876 sp<AMessage> format = new AMessage();
877
878 format->setInt32("type", MEDIA_TRACK_TYPE_SUBTITLE);
879 format->setString("language", "und");
880 format->setString("mime", MEDIA_MIMETYPE_TEXT_CEA_608);
881 //CC1, field 0 channel 0
882 bool isDefaultAuto = (mFoundChannels[index] == 0);
883 format->setInt32("auto", isDefaultAuto);
884 format->setInt32("default", isDefaultAuto);
885 format->setInt32("forced", 0);
886
887 return format;
888}
889
890status_t NuPlayer::CCDecoder::selectTrack(size_t index, bool select) {
891 if (!isTrackValid(index)) {
892 return BAD_VALUE;
893 }
894
895 if (select) {
896 if (mSelectedTrack == (ssize_t)index) {
897 ALOGE("track %zu already selected", index);
898 return BAD_VALUE;
899 }
900 ALOGV("selected track %zu", index);
901 mSelectedTrack = index;
902 } else {
903 if (mSelectedTrack != (ssize_t)index) {
904 ALOGE("track %zu is not selected", index);
905 return BAD_VALUE;
906 }
907 ALOGV("unselected track %zu", index);
908 mSelectedTrack = -1;
909 }
910
911 return OK;
912}
913
914bool NuPlayer::CCDecoder::isSelected() const {
915 return mSelectedTrack >= 0 && mSelectedTrack < (int32_t) getTrackCount();
916}
917
918bool NuPlayer::CCDecoder::isTrackValid(size_t index) const {
919 return index < getTrackCount();
920}
921
922int32_t NuPlayer::CCDecoder::getTrackIndex(size_t channel) const {
923 if (channel < sizeof(mTrackIndices)/sizeof(mTrackIndices[0])) {
924 return mTrackIndices[channel];
925 }
926 return -1;
927}
928
929// returns true if a new CC track is found
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700930bool NuPlayer::CCDecoder::extractFromSEI(const sp<ABuffer> &accessUnit) {
931 int64_t timeUs;
932 CHECK(accessUnit->meta()->findInt64("timeUs", &timeUs));
933
934 sp<ABuffer> sei;
935 if (!accessUnit->meta()->findBuffer("sei", &sei) || sei == NULL) {
936 return false;
937 }
938
Chong Zhangb86e68f2014-08-01 13:46:53 -0700939 bool trackAdded = false;
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700940
Chong Zhang862f8452014-06-26 19:55:23 -0700941 NALBitReader br(sei->data() + 1, sei->size() - 1);
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700942 // sei_message()
Chong Zhang862f8452014-06-26 19:55:23 -0700943 while (br.atLeastNumBitsLeft(16)) { // at least 16-bit for sei_message()
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700944 uint32_t payload_type = 0;
945 size_t payload_size = 0;
946 uint8_t last_byte;
947
948 do {
949 last_byte = br.getBits(8);
950 payload_type += last_byte;
951 } while (last_byte == 0xFF);
952
953 do {
954 last_byte = br.getBits(8);
955 payload_size += last_byte;
956 } while (last_byte == 0xFF);
957
958 // sei_payload()
959 if (payload_type == 4) {
960 // user_data_registered_itu_t_t35()
961
962 // ATSC A/72: 6.4.2
963 uint8_t itu_t_t35_country_code = br.getBits(8);
964 uint16_t itu_t_t35_provider_code = br.getBits(16);
965 uint32_t user_identifier = br.getBits(32);
966 uint8_t user_data_type_code = br.getBits(8);
967
968 payload_size -= 1 + 2 + 4 + 1;
969
970 if (itu_t_t35_country_code == 0xB5
971 && itu_t_t35_provider_code == 0x0031
972 && user_identifier == 'GA94'
973 && user_data_type_code == 0x3) {
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700974 // MPEG_cc_data()
975 // ATSC A/53 Part 4: 6.2.3.1
976 br.skipBits(1); //process_em_data_flag
977 bool process_cc_data_flag = br.getBits(1);
978 br.skipBits(1); //additional_data_flag
979 size_t cc_count = br.getBits(5);
980 br.skipBits(8); // em_data;
981 payload_size -= 2;
982
983 if (process_cc_data_flag) {
984 AString out;
985
986 sp<ABuffer> ccBuf = new ABuffer(cc_count * sizeof(CCData));
987 ccBuf->setRange(0, 0);
988
989 for (size_t i = 0; i < cc_count; i++) {
990 uint8_t marker = br.getBits(5);
991 CHECK_EQ(marker, 0x1f);
992
993 bool cc_valid = br.getBits(1);
994 uint8_t cc_type = br.getBits(2);
995 // remove odd parity bit
996 uint8_t cc_data_1 = br.getBits(8) & 0x7f;
997 uint8_t cc_data_2 = br.getBits(8) & 0x7f;
998
999 if (cc_valid
1000 && (cc_type == 0 || cc_type == 1)) {
1001 CCData cc(cc_type, cc_data_1, cc_data_2);
1002 if (!isNullPad(&cc)) {
Chong Zhangb86e68f2014-08-01 13:46:53 -07001003 size_t channel;
1004 if (cc.getChannel(&channel) && getTrackIndex(channel) < 0) {
1005 mTrackIndices[channel] = mFoundChannels.size();
1006 mFoundChannels.push_back(channel);
1007 trackAdded = true;
1008 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001009 memcpy(ccBuf->data() + ccBuf->size(),
1010 (void *)&cc, sizeof(cc));
1011 ccBuf->setRange(0, ccBuf->size() + sizeof(CCData));
1012 }
1013 }
1014 }
1015 payload_size -= cc_count * 3;
1016
1017 mCCMap.add(timeUs, ccBuf);
1018 break;
1019 }
1020 } else {
1021 ALOGV("Malformed SEI payload type 4");
1022 }
1023 } else {
1024 ALOGV("Unsupported SEI payload type %d", payload_type);
1025 }
1026
1027 // skipping remaining bits of this payload
1028 br.skipBits(payload_size * 8);
1029 }
1030
Chong Zhangb86e68f2014-08-01 13:46:53 -07001031 return trackAdded;
1032}
1033
1034sp<ABuffer> NuPlayer::CCDecoder::filterCCBuf(
1035 const sp<ABuffer> &ccBuf, size_t index) {
1036 sp<ABuffer> filteredCCBuf = new ABuffer(ccBuf->size());
1037 filteredCCBuf->setRange(0, 0);
1038
1039 size_t cc_count = ccBuf->size() / sizeof(CCData);
1040 const CCData* cc_data = (const CCData*)ccBuf->data();
1041 for (size_t i = 0; i < cc_count; ++i) {
1042 size_t channel;
1043 if (cc_data[i].getChannel(&channel)) {
1044 mCurrentChannel = channel;
1045 }
1046 if (mCurrentChannel == mFoundChannels[index]) {
1047 memcpy(filteredCCBuf->data() + filteredCCBuf->size(),
1048 (void *)&cc_data[i], sizeof(CCData));
1049 filteredCCBuf->setRange(0, filteredCCBuf->size() + sizeof(CCData));
1050 }
1051 }
1052
1053 return filteredCCBuf;
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001054}
1055
1056void NuPlayer::CCDecoder::decode(const sp<ABuffer> &accessUnit) {
Chong Zhangb86e68f2014-08-01 13:46:53 -07001057 if (extractFromSEI(accessUnit)) {
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001058 ALOGI("Found CEA-608 track");
1059 sp<AMessage> msg = mNotify->dup();
1060 msg->setInt32("what", kWhatTrackAdded);
1061 msg->post();
1062 }
1063 // TODO: extract CC from other sources
1064}
1065
1066void NuPlayer::CCDecoder::display(int64_t timeUs) {
Chong Zhangb86e68f2014-08-01 13:46:53 -07001067 if (!isTrackValid(mSelectedTrack)) {
1068 ALOGE("Could not find current track(index=%d)", mSelectedTrack);
1069 return;
1070 }
1071
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001072 ssize_t index = mCCMap.indexOfKey(timeUs);
1073 if (index < 0) {
1074 ALOGV("cc for timestamp %" PRId64 " not found", timeUs);
1075 return;
1076 }
1077
Chong Zhangb86e68f2014-08-01 13:46:53 -07001078 sp<ABuffer> ccBuf = filterCCBuf(mCCMap.valueAt(index), mSelectedTrack);
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001079
1080 if (ccBuf->size() > 0) {
1081#if 0
1082 dumpBytePair(ccBuf);
1083#endif
1084
1085 ccBuf->meta()->setInt32("trackIndex", mSelectedTrack);
1086 ccBuf->meta()->setInt64("timeUs", timeUs);
1087 ccBuf->meta()->setInt64("durationUs", 0ll);
1088
1089 sp<AMessage> msg = mNotify->dup();
1090 msg->setInt32("what", kWhatClosedCaptionData);
1091 msg->setBuffer("buffer", ccBuf);
1092 msg->post();
1093 }
1094
1095 // remove all entries before timeUs
1096 mCCMap.removeItemsAt(0, index + 1);
1097}
1098
Chong Zhangb86e68f2014-08-01 13:46:53 -07001099void NuPlayer::CCDecoder::flush() {
1100 mCCMap.clear();
1101}
1102
Andreas Huberf9334412010-12-15 15:17:42 -08001103} // namespace android
1104