blob: ed6f674a2fda6bdaa2f97ee3ce2a197d7796aa98 [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() {
56}
57
Lajos Molnar09524832014-07-17 14:29:51 -070058static
59status_t PostAndAwaitResponse(
60 const sp<AMessage> &msg, sp<AMessage> *response) {
61 status_t err = msg->postAndAwaitResponse(response);
62
63 if (err != OK) {
64 return err;
65 }
66
67 if (!(*response)->findInt32("err", &err)) {
68 err = OK;
69 }
70
71 return err;
72}
73
Lajos Molnar87603c02014-08-20 19:25:30 -070074void NuPlayer::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) {
75 mCSDsForCurrentFormat.clear();
76 for (int32_t i = 0; ; ++i) {
77 AString tag = "csd-";
78 tag.append(i);
79 sp<ABuffer> buffer;
80 if (!format->findBuffer(tag.c_str(), &buffer)) {
81 break;
82 }
83 mCSDsForCurrentFormat.push(buffer);
84 }
85}
86
Lajos Molnar1cd13982014-01-17 15:12:51 -080087void NuPlayer::Decoder::onConfigure(const sp<AMessage> &format) {
Andreas Huberf9334412010-12-15 15:17:42 -080088 CHECK(mCodec == NULL);
Andreas Huberf9334412010-12-15 15:17:42 -080089
Lajos Molnar1cd13982014-01-17 15:12:51 -080090 ++mBufferGeneration;
91
Andreas Huber84066782011-08-16 09:34:26 -070092 AString mime;
93 CHECK(format->findString("mime", &mime));
Andreas Huberf9334412010-12-15 15:17:42 -080094
Lajos Molnar1cd13982014-01-17 15:12:51 -080095 sp<Surface> surface = NULL;
96 if (mNativeWindow != NULL) {
97 surface = mNativeWindow->getSurfaceTextureClient();
Andreas Huber84066782011-08-16 09:34:26 -070098 }
Andreas Huberf9334412010-12-15 15:17:42 -080099
Lajos Molnar1cd13982014-01-17 15:12:51 -0800100 mComponentName = mime;
101 mComponentName.append(" decoder");
102 ALOGV("[%s] onConfigure (surface=%p)", mComponentName.c_str(), surface.get());
103
104 mCodec = MediaCodec::CreateByType(mCodecLooper, mime.c_str(), false /* encoder */);
Lajos Molnar09524832014-07-17 14:29:51 -0700105 int32_t secure = 0;
106 if (format->findInt32("secure", &secure) && secure != 0) {
107 if (mCodec != NULL) {
108 mCodec->getName(&mComponentName);
109 mComponentName.append(".secure");
110 mCodec->release();
111 ALOGI("[%s] creating", mComponentName.c_str());
112 mCodec = MediaCodec::CreateByComponentName(
113 mCodecLooper, mComponentName.c_str());
114 }
115 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800116 if (mCodec == NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700117 ALOGE("Failed to create %s%s decoder",
118 (secure ? "secure " : ""), mime.c_str());
Lajos Molnar1cd13982014-01-17 15:12:51 -0800119 handleError(UNKNOWN_ERROR);
120 return;
121 }
122
123 mCodec->getName(&mComponentName);
124
Lajos Molnar14986f62014-09-15 11:04:44 -0700125 status_t err;
Glenn Kasten11731182011-02-08 17:26:17 -0800126 if (mNativeWindow != NULL) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800127 // disconnect from surface as MediaCodec will reconnect
Lajos Molnar14986f62014-09-15 11:04:44 -0700128 err = native_window_api_disconnect(
129 surface.get(), NATIVE_WINDOW_API_MEDIA);
130 // We treat this as a warning, as this is a preparatory step.
131 // Codec will try to connect to the surface, which is where
132 // any error signaling will occur.
133 ALOGW_IF(err != OK, "failed to disconnect from surface: %d", err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800134 }
Lajos Molnar14986f62014-09-15 11:04:44 -0700135 err = mCodec->configure(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800136 format, surface, NULL /* crypto */, 0 /* flags */);
137 if (err != OK) {
138 ALOGE("Failed to configure %s decoder (err=%d)", mComponentName.c_str(), err);
139 handleError(err);
140 return;
141 }
Lajos Molnar87603c02014-08-20 19:25:30 -0700142 rememberCodecSpecificData(format);
143
Lajos Molnar1cd13982014-01-17 15:12:51 -0800144 // the following should work in configured state
145 CHECK_EQ((status_t)OK, mCodec->getOutputFormat(&mOutputFormat));
146 CHECK_EQ((status_t)OK, mCodec->getInputFormat(&mInputFormat));
147
148 err = mCodec->start();
149 if (err != OK) {
150 ALOGE("Failed to start %s decoder (err=%d)", mComponentName.c_str(), err);
151 handleError(err);
152 return;
Andreas Huberf9334412010-12-15 15:17:42 -0800153 }
154
Lajos Molnar1cd13982014-01-17 15:12:51 -0800155 // the following should work after start
156 CHECK_EQ((status_t)OK, mCodec->getInputBuffers(&mInputBuffers));
Lajos Molnar09524832014-07-17 14:29:51 -0700157 releaseAndResetMediaBuffers();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800158 CHECK_EQ((status_t)OK, mCodec->getOutputBuffers(&mOutputBuffers));
159 ALOGV("[%s] got %zu input and %zu output buffers",
160 mComponentName.c_str(),
161 mInputBuffers.size(),
162 mOutputBuffers.size());
Andreas Huber078cfcf2011-09-15 12:25:04 -0700163
Lajos Molnar1cd13982014-01-17 15:12:51 -0800164 requestCodecNotification();
Wei Jia704e7262014-06-04 16:21:56 -0700165 mPaused = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800166}
Andreas Huber078cfcf2011-09-15 12:25:04 -0700167
Lajos Molnar09524832014-07-17 14:29:51 -0700168void NuPlayer::Decoder::releaseAndResetMediaBuffers() {
169 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
170 if (mMediaBuffers[i] != NULL) {
171 mMediaBuffers[i]->release();
172 mMediaBuffers.editItemAt(i) = NULL;
173 }
174 }
175 mMediaBuffers.resize(mInputBuffers.size());
Wei Jia81e50d02014-07-24 10:28:47 -0700176 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
177 mMediaBuffers.editItemAt(i) = NULL;
178 }
Lajos Molnar09524832014-07-17 14:29:51 -0700179 mInputBufferIsDequeued.clear();
180 mInputBufferIsDequeued.resize(mInputBuffers.size());
Wei Jia81e50d02014-07-24 10:28:47 -0700181 for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) {
182 mInputBufferIsDequeued.editItemAt(i) = false;
183 }
Lajos Molnar09524832014-07-17 14:29:51 -0700184}
185
Lajos Molnar1cd13982014-01-17 15:12:51 -0800186void NuPlayer::Decoder::requestCodecNotification() {
187 if (mCodec != NULL) {
188 sp<AMessage> reply = new AMessage(kWhatCodecNotify, id());
189 reply->setInt32("generation", mBufferGeneration);
190 mCodec->requestActivityNotification(reply);
191 }
192}
193
194bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) {
195 int32_t generation;
196 CHECK(msg->findInt32("generation", &generation));
197 return generation != mBufferGeneration;
198}
199
200void NuPlayer::Decoder::init() {
201 mDecoderLooper->registerHandler(this);
202}
203
204void NuPlayer::Decoder::configure(const sp<AMessage> &format) {
205 sp<AMessage> msg = new AMessage(kWhatConfigure, id());
206 msg->setMessage("format", format);
207 msg->post();
208}
209
Lajos Molnar87603c02014-08-20 19:25:30 -0700210void NuPlayer::Decoder::signalUpdateFormat(const sp<AMessage> &format) {
211 sp<AMessage> msg = new AMessage(kWhatUpdateFormat, id());
212 msg->setMessage("format", format);
213 msg->post();
214}
215
Lajos Molnar09524832014-07-17 14:29:51 -0700216status_t NuPlayer::Decoder::getInputBuffers(Vector<sp<ABuffer> > *buffers) const {
217 sp<AMessage> msg = new AMessage(kWhatGetInputBuffers, id());
218 msg->setPointer("buffers", buffers);
219
220 sp<AMessage> response;
221 return PostAndAwaitResponse(msg, &response);
222}
223
Lajos Molnar1cd13982014-01-17 15:12:51 -0800224void NuPlayer::Decoder::handleError(int32_t err)
225{
Andy Hungcf31f1e2014-09-23 14:59:01 -0700226 // We cannot immediately release the codec due to buffers still outstanding
227 // in the renderer. We signal to the player the error so it can shutdown/release the
228 // decoder after flushing and increment the generation to discard unnecessary messages.
229
230 ++mBufferGeneration;
Wei Jiac22c6952014-08-29 14:47:50 -0700231
Lajos Molnar1cd13982014-01-17 15:12:51 -0800232 sp<AMessage> notify = mNotify->dup();
233 notify->setInt32("what", kWhatError);
234 notify->setInt32("err", err);
235 notify->post();
236}
237
238bool NuPlayer::Decoder::handleAnInputBuffer() {
239 size_t bufferIx = -1;
240 status_t res = mCodec->dequeueInputBuffer(&bufferIx);
241 ALOGV("[%s] dequeued input: %d",
242 mComponentName.c_str(), res == OK ? (int)bufferIx : res);
243 if (res != OK) {
244 if (res != -EAGAIN) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700245 ALOGE("Failed to dequeue input buffer for %s (err=%d)",
246 mComponentName.c_str(), res);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800247 handleError(res);
248 }
249 return false;
Andreas Huber078cfcf2011-09-15 12:25:04 -0700250 }
251
Lajos Molnar1cd13982014-01-17 15:12:51 -0800252 CHECK_LT(bufferIx, mInputBuffers.size());
Andreas Huberf9334412010-12-15 15:17:42 -0800253
Lajos Molnar09524832014-07-17 14:29:51 -0700254 if (mMediaBuffers[bufferIx] != NULL) {
255 mMediaBuffers[bufferIx]->release();
256 mMediaBuffers.editItemAt(bufferIx) = NULL;
257 }
258 mInputBufferIsDequeued.editItemAt(bufferIx) = true;
259
Lajos Molnar1cd13982014-01-17 15:12:51 -0800260 sp<AMessage> reply = new AMessage(kWhatInputBufferFilled, id());
261 reply->setSize("buffer-ix", bufferIx);
262 reply->setInt32("generation", mBufferGeneration);
263
Lajos Molnar87603c02014-08-20 19:25:30 -0700264 if (!mCSDsToSubmit.isEmpty()) {
265 sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0);
266 ALOGI("[%s] resubmitting CSD", mComponentName.c_str());
267 reply->setBuffer("buffer", buffer);
268 mCSDsToSubmit.removeAt(0);
269 reply->post();
270 return true;
271 }
272
Lajos Molnar1cd13982014-01-17 15:12:51 -0800273 sp<AMessage> notify = mNotify->dup();
274 notify->setInt32("what", kWhatFillThisBuffer);
275 notify->setBuffer("buffer", mInputBuffers[bufferIx]);
276 notify->setMessage("reply", reply);
277 notify->post();
278 return true;
279}
280
281void android::NuPlayer::Decoder::onInputBufferFilled(const sp<AMessage> &msg) {
282 size_t bufferIx;
283 CHECK(msg->findSize("buffer-ix", &bufferIx));
284 CHECK_LT(bufferIx, mInputBuffers.size());
285 sp<ABuffer> codecBuffer = mInputBuffers[bufferIx];
286
287 sp<ABuffer> buffer;
288 bool hasBuffer = msg->findBuffer("buffer", &buffer);
Lajos Molnar09524832014-07-17 14:29:51 -0700289
290 // handle widevine classic source - that fills an arbitrary input buffer
291 MediaBuffer *mediaBuffer = NULL;
Wei Jia96e92b52014-09-18 17:36:20 -0700292 if (hasBuffer) {
293 mediaBuffer = (MediaBuffer *)(buffer->getMediaBufferBase());
294 if (mediaBuffer != NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700295 // likely filled another buffer than we requested: adjust buffer index
296 size_t ix;
297 for (ix = 0; ix < mInputBuffers.size(); ix++) {
298 const sp<ABuffer> &buf = mInputBuffers[ix];
299 if (buf->data() == mediaBuffer->data()) {
300 // all input buffers are dequeued on start, hence the check
301 CHECK(mInputBufferIsDequeued[ix]);
302 ALOGV("[%s] received MediaBuffer for #%zu instead of #%zu",
303 mComponentName.c_str(), ix, bufferIx);
304
305 // TRICKY: need buffer for the metadata, so instead, set
306 // codecBuffer to the same (though incorrect) buffer to
307 // avoid a memcpy into the codecBuffer
308 codecBuffer = buffer;
309 codecBuffer->setRange(
310 mediaBuffer->range_offset(),
311 mediaBuffer->range_length());
312 bufferIx = ix;
313 break;
314 }
315 }
316 CHECK(ix < mInputBuffers.size());
317 }
318 }
319
Andy Hungcf31f1e2014-09-23 14:59:01 -0700320
Lajos Molnar09524832014-07-17 14:29:51 -0700321
Lajos Molnar1cd13982014-01-17 15:12:51 -0800322 if (buffer == NULL /* includes !hasBuffer */) {
323 int32_t streamErr = ERROR_END_OF_STREAM;
324 CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
325
326 if (streamErr == OK) {
327 /* buffers are returned to hold on to */
328 return;
329 }
330
331 // attempt to queue EOS
332 status_t err = mCodec->queueInputBuffer(
333 bufferIx,
334 0,
335 0,
336 0,
337 MediaCodec::BUFFER_FLAG_EOS);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700338 if (err == OK) {
339 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
340 } else if (streamErr == ERROR_END_OF_STREAM) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800341 streamErr = err;
342 // err will not be ERROR_END_OF_STREAM
343 }
344
345 if (streamErr != ERROR_END_OF_STREAM) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700346 ALOGE("Stream error for %s (err=%d), EOS %s queued",
347 mComponentName.c_str(),
348 streamErr,
349 err == OK ? "successfully" : "unsuccessfully");
Lajos Molnar1cd13982014-01-17 15:12:51 -0800350 handleError(streamErr);
351 }
352 } else {
353 int64_t timeUs = 0;
354 uint32_t flags = 0;
355 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
356
Lajos Molnar87603c02014-08-20 19:25:30 -0700357 int32_t eos, csd;
358 // we do not expect SYNCFRAME for decoder
Lajos Molnar1cd13982014-01-17 15:12:51 -0800359 if (buffer->meta()->findInt32("eos", &eos) && eos) {
360 flags |= MediaCodec::BUFFER_FLAG_EOS;
Lajos Molnar87603c02014-08-20 19:25:30 -0700361 } else if (buffer->meta()->findInt32("csd", &csd) && csd) {
362 flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800363 }
364
365 // copy into codec buffer
366 if (buffer != codecBuffer) {
367 CHECK_LE(buffer->size(), codecBuffer->capacity());
368 codecBuffer->setRange(0, buffer->size());
369 memcpy(codecBuffer->data(), buffer->data(), buffer->size());
370 }
371
372 status_t err = mCodec->queueInputBuffer(
373 bufferIx,
374 codecBuffer->offset(),
375 codecBuffer->size(),
376 timeUs,
377 flags);
378 if (err != OK) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700379 if (mediaBuffer != NULL) {
380 mediaBuffer->release();
381 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800382 ALOGE("Failed to queue input buffer for %s (err=%d)",
383 mComponentName.c_str(), err);
384 handleError(err);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700385 } else {
386 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
387 if (mediaBuffer != NULL) {
388 CHECK(mMediaBuffers[bufferIx] == NULL);
389 mMediaBuffers.editItemAt(bufferIx) = mediaBuffer;
390 }
Lajos Molnar09524832014-07-17 14:29:51 -0700391 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800392 }
393}
394
395bool NuPlayer::Decoder::handleAnOutputBuffer() {
396 size_t bufferIx = -1;
397 size_t offset;
398 size_t size;
399 int64_t timeUs;
400 uint32_t flags;
401 status_t res = mCodec->dequeueOutputBuffer(
402 &bufferIx, &offset, &size, &timeUs, &flags);
403
404 if (res != OK) {
405 ALOGV("[%s] dequeued output: %d", mComponentName.c_str(), res);
406 } else {
407 ALOGV("[%s] dequeued output: %d (time=%lld flags=%" PRIu32 ")",
408 mComponentName.c_str(), (int)bufferIx, timeUs, flags);
409 }
410
411 if (res == INFO_OUTPUT_BUFFERS_CHANGED) {
412 res = mCodec->getOutputBuffers(&mOutputBuffers);
413 if (res != OK) {
414 ALOGE("Failed to get output buffers for %s after INFO event (err=%d)",
415 mComponentName.c_str(), res);
416 handleError(res);
417 return false;
418 }
419 // NuPlayer ignores this
420 return true;
421 } else if (res == INFO_FORMAT_CHANGED) {
422 sp<AMessage> format = new AMessage();
423 res = mCodec->getOutputFormat(&format);
424 if (res != OK) {
425 ALOGE("Failed to get output format for %s after INFO event (err=%d)",
426 mComponentName.c_str(), res);
427 handleError(res);
428 return false;
429 }
430
431 sp<AMessage> notify = mNotify->dup();
432 notify->setInt32("what", kWhatOutputFormatChanged);
433 notify->setMessage("format", format);
434 notify->post();
435 return true;
436 } else if (res == INFO_DISCONTINUITY) {
437 // nothing to do
438 return true;
439 } else if (res != OK) {
440 if (res != -EAGAIN) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700441 ALOGE("Failed to dequeue output buffer for %s (err=%d)",
442 mComponentName.c_str(), res);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800443 handleError(res);
444 }
445 return false;
446 }
447
448 CHECK_LT(bufferIx, mOutputBuffers.size());
449 sp<ABuffer> buffer = mOutputBuffers[bufferIx];
450 buffer->setRange(offset, size);
451 buffer->meta()->clear();
452 buffer->meta()->setInt64("timeUs", timeUs);
453 if (flags & MediaCodec::BUFFER_FLAG_EOS) {
454 buffer->meta()->setInt32("eos", true);
455 }
456 // we do not expect CODECCONFIG or SYNCFRAME for decoder
457
458 sp<AMessage> reply = new AMessage(kWhatRenderBuffer, id());
459 reply->setSize("buffer-ix", bufferIx);
460 reply->setInt32("generation", mBufferGeneration);
461
462 sp<AMessage> notify = mNotify->dup();
463 notify->setInt32("what", kWhatDrainThisBuffer);
464 notify->setBuffer("buffer", buffer);
465 notify->setMessage("reply", reply);
466 notify->post();
467
468 // FIXME: This should be handled after rendering is complete,
469 // but Renderer needs it now
470 if (flags & MediaCodec::BUFFER_FLAG_EOS) {
471 ALOGV("queueing eos [%s]", mComponentName.c_str());
472 sp<AMessage> notify = mNotify->dup();
473 notify->setInt32("what", kWhatEOS);
474 notify->setInt32("err", ERROR_END_OF_STREAM);
475 notify->post();
476 }
477 return true;
478}
479
480void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) {
481 status_t err;
482 int32_t render;
483 size_t bufferIx;
484 CHECK(msg->findSize("buffer-ix", &bufferIx));
485 if (msg->findInt32("render", &render) && render) {
Lajos Molnardc43dfa2014-05-07 15:33:04 -0700486 int64_t timestampNs;
487 CHECK(msg->findInt64("timestampNs", &timestampNs));
488 err = mCodec->renderOutputBufferAndRelease(bufferIx, timestampNs);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800489 } else {
490 err = mCodec->releaseOutputBuffer(bufferIx);
491 }
492 if (err != OK) {
493 ALOGE("failed to release output buffer for %s (err=%d)",
494 mComponentName.c_str(), err);
495 handleError(err);
496 }
497}
498
499void NuPlayer::Decoder::onFlush() {
500 status_t err = OK;
501 if (mCodec != NULL) {
502 err = mCodec->flush();
Lajos Molnar87603c02014-08-20 19:25:30 -0700503 mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator
Lajos Molnar1cd13982014-01-17 15:12:51 -0800504 ++mBufferGeneration;
505 }
506
507 if (err != OK) {
508 ALOGE("failed to flush %s (err=%d)", mComponentName.c_str(), err);
509 handleError(err);
510 return;
511 }
512
Lajos Molnar09524832014-07-17 14:29:51 -0700513 releaseAndResetMediaBuffers();
514
Lajos Molnar1cd13982014-01-17 15:12:51 -0800515 sp<AMessage> notify = mNotify->dup();
516 notify->setInt32("what", kWhatFlushCompleted);
517 notify->post();
Wei Jia704e7262014-06-04 16:21:56 -0700518 mPaused = true;
519}
520
521void NuPlayer::Decoder::onResume() {
522 mPaused = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800523}
524
525void NuPlayer::Decoder::onShutdown() {
526 status_t err = OK;
527 if (mCodec != NULL) {
528 err = mCodec->release();
529 mCodec = NULL;
530 ++mBufferGeneration;
531
532 if (mNativeWindow != NULL) {
533 // reconnect to surface as MediaCodec disconnected from it
Wei Jia3fb9f682014-08-20 14:30:09 -0700534 status_t error =
Lajos Molnar1cd13982014-01-17 15:12:51 -0800535 native_window_api_connect(
536 mNativeWindow->getNativeWindow().get(),
Wei Jia3fb9f682014-08-20 14:30:09 -0700537 NATIVE_WINDOW_API_MEDIA);
538 ALOGW_IF(error != NO_ERROR,
539 "[%s] failed to connect to native window, error=%d",
540 mComponentName.c_str(), error);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800541 }
542 mComponentName = "decoder";
543 }
544
Lajos Molnar09524832014-07-17 14:29:51 -0700545 releaseAndResetMediaBuffers();
546
Lajos Molnar1cd13982014-01-17 15:12:51 -0800547 if (err != OK) {
548 ALOGE("failed to release %s (err=%d)", mComponentName.c_str(), err);
549 handleError(err);
550 return;
551 }
552
553 sp<AMessage> notify = mNotify->dup();
554 notify->setInt32("what", kWhatShutdownCompleted);
555 notify->post();
Wei Jia704e7262014-06-04 16:21:56 -0700556 mPaused = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800557}
558
559void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800560 ALOGV("[%s] onMessage: %s", mComponentName.c_str(), msg->debugString().c_str());
561
Andreas Huberf9334412010-12-15 15:17:42 -0800562 switch (msg->what()) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800563 case kWhatConfigure:
564 {
565 sp<AMessage> format;
566 CHECK(msg->findMessage("format", &format));
567 onConfigure(format);
568 break;
569 }
570
Lajos Molnar87603c02014-08-20 19:25:30 -0700571 case kWhatUpdateFormat:
572 {
573 sp<AMessage> format;
574 CHECK(msg->findMessage("format", &format));
575 rememberCodecSpecificData(format);
576 break;
577 }
578
Lajos Molnar09524832014-07-17 14:29:51 -0700579 case kWhatGetInputBuffers:
580 {
581 uint32_t replyID;
582 CHECK(msg->senderAwaitsResponse(&replyID));
583
584 Vector<sp<ABuffer> > *dstBuffers;
585 CHECK(msg->findPointer("buffers", (void **)&dstBuffers));
586
587 dstBuffers->clear();
588 for (size_t i = 0; i < mInputBuffers.size(); i++) {
589 dstBuffers->push(mInputBuffers[i]);
590 }
591
592 (new AMessage)->postReply(replyID);
593 break;
594 }
595
Andreas Huberf9334412010-12-15 15:17:42 -0800596 case kWhatCodecNotify:
597 {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800598 if (!isStaleReply(msg)) {
Wei Jia704e7262014-06-04 16:21:56 -0700599 if (!mPaused) {
600 while (handleAnInputBuffer()) {
601 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800602 }
Andreas Huberf9334412010-12-15 15:17:42 -0800603
Lajos Molnar1cd13982014-01-17 15:12:51 -0800604 while (handleAnOutputBuffer()) {
605 }
Andreas Huberf9334412010-12-15 15:17:42 -0800606 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800607
608 requestCodecNotification();
609 break;
610 }
611
612 case kWhatInputBufferFilled:
613 {
614 if (!isStaleReply(msg)) {
615 onInputBufferFilled(msg);
616 }
Lajos Molnarb9b87fe2014-09-10 13:53:21 -0700617
Lajos Molnar1cd13982014-01-17 15:12:51 -0800618 break;
619 }
620
621 case kWhatRenderBuffer:
622 {
623 if (!isStaleReply(msg)) {
624 onRenderBuffer(msg);
625 }
626 break;
627 }
628
629 case kWhatFlush:
630 {
Lajos Molnar87603c02014-08-20 19:25:30 -0700631 sp<AMessage> format;
632 if (msg->findMessage("new-format", &format)) {
633 rememberCodecSpecificData(format);
634 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800635 onFlush();
636 break;
637 }
638
Wei Jia704e7262014-06-04 16:21:56 -0700639 case kWhatResume:
640 {
641 onResume();
642 break;
643 }
644
Lajos Molnar1cd13982014-01-17 15:12:51 -0800645 case kWhatShutdown:
646 {
647 onShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800648 break;
649 }
650
651 default:
652 TRESPASS();
653 break;
654 }
655}
656
Lajos Molnar87603c02014-08-20 19:25:30 -0700657void NuPlayer::Decoder::signalFlush(const sp<AMessage> &format) {
658 sp<AMessage> msg = new AMessage(kWhatFlush, id());
659 if (format != NULL) {
660 msg->setMessage("new-format", format);
661 }
662 msg->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800663}
664
665void NuPlayer::Decoder::signalResume() {
Wei Jia704e7262014-06-04 16:21:56 -0700666 (new AMessage(kWhatResume, id()))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800667}
668
Andreas Huber3831a062010-12-21 10:22:33 -0800669void NuPlayer::Decoder::initiateShutdown() {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800670 (new AMessage(kWhatShutdown, id()))->post();
Andreas Huber3831a062010-12-21 10:22:33 -0800671}
672
Robert Shih6d0a94e2014-01-23 16:18:22 -0800673bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(const sp<AMessage> &targetFormat) const {
674 if (targetFormat == NULL) {
675 return true;
676 }
677
678 AString mime;
679 if (!targetFormat->findString("mime", &mime)) {
680 return false;
681 }
682
683 if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
684 // field-by-field comparison
685 const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
686 for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
687 int32_t oldVal, newVal;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800688 if (!mOutputFormat->findInt32(keys[i], &oldVal) ||
689 !targetFormat->findInt32(keys[i], &newVal) ||
690 oldVal != newVal) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800691 return false;
692 }
693 }
694
695 sp<ABuffer> oldBuf, newBuf;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800696 if (mOutputFormat->findBuffer("csd-0", &oldBuf) &&
697 targetFormat->findBuffer("csd-0", &newBuf)) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800698 if (oldBuf->size() != newBuf->size()) {
699 return false;
700 }
701 return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size());
702 }
703 }
704 return false;
705}
706
707bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800708 if (mOutputFormat == NULL) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800709 return false;
710 }
711
712 if (targetFormat == NULL) {
713 return true;
714 }
715
716 AString oldMime, newMime;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800717 if (!mOutputFormat->findString("mime", &oldMime)
Robert Shih6d0a94e2014-01-23 16:18:22 -0800718 || !targetFormat->findString("mime", &newMime)
719 || !(oldMime == newMime)) {
720 return false;
721 }
722
723 bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
724 bool seamless;
725 if (audio) {
726 seamless = supportsSeamlessAudioFormatChange(targetFormat);
727 } else {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800728 int32_t isAdaptive;
729 seamless = (mCodec != NULL &&
730 mInputFormat->findInt32("adaptive-playback", &isAdaptive) &&
731 isAdaptive);
Robert Shih6d0a94e2014-01-23 16:18:22 -0800732 }
733
734 ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
735 return seamless;
736}
737
Chong Zhangb86e68f2014-08-01 13:46:53 -0700738struct CCData {
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700739 CCData(uint8_t type, uint8_t data1, uint8_t data2)
740 : mType(type), mData1(data1), mData2(data2) {
741 }
Chong Zhangb86e68f2014-08-01 13:46:53 -0700742 bool getChannel(size_t *channel) const {
743 if (mData1 >= 0x10 && mData1 <= 0x1f) {
744 *channel = (mData1 >= 0x18 ? 1 : 0) + (mType ? 2 : 0);
745 return true;
746 }
747 return false;
748 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700749
750 uint8_t mType;
751 uint8_t mData1;
752 uint8_t mData2;
753};
754
Chong Zhangb86e68f2014-08-01 13:46:53 -0700755static bool isNullPad(CCData *cc) {
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700756 return cc->mData1 < 0x10 && cc->mData2 < 0x10;
757}
758
Chong Zhangb86e68f2014-08-01 13:46:53 -0700759static void dumpBytePair(const sp<ABuffer> &ccBuf) {
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700760 size_t offset = 0;
761 AString out;
762
763 while (offset < ccBuf->size()) {
764 char tmp[128];
765
766 CCData *cc = (CCData *) (ccBuf->data() + offset);
767
768 if (isNullPad(cc)) {
769 // 1 null pad or XDS metadata, ignore
770 offset += sizeof(CCData);
771 continue;
772 }
773
774 if (cc->mData1 >= 0x20 && cc->mData1 <= 0x7f) {
775 // 2 basic chars
776 sprintf(tmp, "[%d]Basic: %c %c", cc->mType, cc->mData1, cc->mData2);
777 } else if ((cc->mData1 == 0x11 || cc->mData1 == 0x19)
778 && cc->mData2 >= 0x30 && cc->mData2 <= 0x3f) {
779 // 1 special char
780 sprintf(tmp, "[%d]Special: %02x %02x", cc->mType, cc->mData1, cc->mData2);
781 } else if ((cc->mData1 == 0x12 || cc->mData1 == 0x1A)
782 && cc->mData2 >= 0x20 && cc->mData2 <= 0x3f){
783 // 1 Spanish/French char
784 sprintf(tmp, "[%d]Spanish: %02x %02x", cc->mType, cc->mData1, cc->mData2);
785 } else if ((cc->mData1 == 0x13 || cc->mData1 == 0x1B)
786 && cc->mData2 >= 0x20 && cc->mData2 <= 0x3f){
787 // 1 Portuguese/German/Danish char
788 sprintf(tmp, "[%d]German: %02x %02x", cc->mType, cc->mData1, cc->mData2);
789 } else if ((cc->mData1 == 0x11 || cc->mData1 == 0x19)
790 && cc->mData2 >= 0x20 && cc->mData2 <= 0x2f){
791 // Mid-Row Codes (Table 69)
792 sprintf(tmp, "[%d]Mid-row: %02x %02x", cc->mType, cc->mData1, cc->mData2);
793 } else if (((cc->mData1 == 0x14 || cc->mData1 == 0x1c)
794 && cc->mData2 >= 0x20 && cc->mData2 <= 0x2f)
795 ||
796 ((cc->mData1 == 0x17 || cc->mData1 == 0x1f)
797 && cc->mData2 >= 0x21 && cc->mData2 <= 0x23)){
798 // Misc Control Codes (Table 70)
799 sprintf(tmp, "[%d]Ctrl: %02x %02x", cc->mType, cc->mData1, cc->mData2);
800 } else if ((cc->mData1 & 0x70) == 0x10
801 && (cc->mData2 & 0x40) == 0x40
802 && ((cc->mData1 & 0x07) || !(cc->mData2 & 0x20)) ) {
803 // Preamble Address Codes (Table 71)
804 sprintf(tmp, "[%d]PAC: %02x %02x", cc->mType, cc->mData1, cc->mData2);
805 } else {
806 sprintf(tmp, "[%d]Invalid: %02x %02x", cc->mType, cc->mData1, cc->mData2);
807 }
808
809 if (out.size() > 0) {
810 out.append(", ");
811 }
812
813 out.append(tmp);
814
815 offset += sizeof(CCData);
816 }
817
818 ALOGI("%s", out.c_str());
819}
820
Chong Zhangb86e68f2014-08-01 13:46:53 -0700821NuPlayer::CCDecoder::CCDecoder(const sp<AMessage> &notify)
822 : mNotify(notify),
823 mCurrentChannel(0),
824 mSelectedTrack(-1) {
825 for (size_t i = 0; i < sizeof(mTrackIndices)/sizeof(mTrackIndices[0]); ++i) {
826 mTrackIndices[i] = -1;
827 }
828}
829
830size_t NuPlayer::CCDecoder::getTrackCount() const {
831 return mFoundChannels.size();
832}
833
834sp<AMessage> NuPlayer::CCDecoder::getTrackInfo(size_t index) const {
835 if (!isTrackValid(index)) {
836 return NULL;
837 }
838
839 sp<AMessage> format = new AMessage();
840
841 format->setInt32("type", MEDIA_TRACK_TYPE_SUBTITLE);
842 format->setString("language", "und");
843 format->setString("mime", MEDIA_MIMETYPE_TEXT_CEA_608);
844 //CC1, field 0 channel 0
845 bool isDefaultAuto = (mFoundChannels[index] == 0);
846 format->setInt32("auto", isDefaultAuto);
847 format->setInt32("default", isDefaultAuto);
848 format->setInt32("forced", 0);
849
850 return format;
851}
852
853status_t NuPlayer::CCDecoder::selectTrack(size_t index, bool select) {
854 if (!isTrackValid(index)) {
855 return BAD_VALUE;
856 }
857
858 if (select) {
859 if (mSelectedTrack == (ssize_t)index) {
860 ALOGE("track %zu already selected", index);
861 return BAD_VALUE;
862 }
863 ALOGV("selected track %zu", index);
864 mSelectedTrack = index;
865 } else {
866 if (mSelectedTrack != (ssize_t)index) {
867 ALOGE("track %zu is not selected", index);
868 return BAD_VALUE;
869 }
870 ALOGV("unselected track %zu", index);
871 mSelectedTrack = -1;
872 }
873
874 return OK;
875}
876
877bool NuPlayer::CCDecoder::isSelected() const {
878 return mSelectedTrack >= 0 && mSelectedTrack < (int32_t) getTrackCount();
879}
880
881bool NuPlayer::CCDecoder::isTrackValid(size_t index) const {
882 return index < getTrackCount();
883}
884
885int32_t NuPlayer::CCDecoder::getTrackIndex(size_t channel) const {
886 if (channel < sizeof(mTrackIndices)/sizeof(mTrackIndices[0])) {
887 return mTrackIndices[channel];
888 }
889 return -1;
890}
891
892// returns true if a new CC track is found
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700893bool NuPlayer::CCDecoder::extractFromSEI(const sp<ABuffer> &accessUnit) {
894 int64_t timeUs;
895 CHECK(accessUnit->meta()->findInt64("timeUs", &timeUs));
896
897 sp<ABuffer> sei;
898 if (!accessUnit->meta()->findBuffer("sei", &sei) || sei == NULL) {
899 return false;
900 }
901
Chong Zhangb86e68f2014-08-01 13:46:53 -0700902 bool trackAdded = false;
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700903
Chong Zhang862f8452014-06-26 19:55:23 -0700904 NALBitReader br(sei->data() + 1, sei->size() - 1);
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700905 // sei_message()
Chong Zhang862f8452014-06-26 19:55:23 -0700906 while (br.atLeastNumBitsLeft(16)) { // at least 16-bit for sei_message()
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700907 uint32_t payload_type = 0;
908 size_t payload_size = 0;
909 uint8_t last_byte;
910
911 do {
912 last_byte = br.getBits(8);
913 payload_type += last_byte;
914 } while (last_byte == 0xFF);
915
916 do {
917 last_byte = br.getBits(8);
918 payload_size += last_byte;
919 } while (last_byte == 0xFF);
920
921 // sei_payload()
922 if (payload_type == 4) {
923 // user_data_registered_itu_t_t35()
924
925 // ATSC A/72: 6.4.2
926 uint8_t itu_t_t35_country_code = br.getBits(8);
927 uint16_t itu_t_t35_provider_code = br.getBits(16);
928 uint32_t user_identifier = br.getBits(32);
929 uint8_t user_data_type_code = br.getBits(8);
930
931 payload_size -= 1 + 2 + 4 + 1;
932
933 if (itu_t_t35_country_code == 0xB5
934 && itu_t_t35_provider_code == 0x0031
935 && user_identifier == 'GA94'
936 && user_data_type_code == 0x3) {
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700937 // MPEG_cc_data()
938 // ATSC A/53 Part 4: 6.2.3.1
939 br.skipBits(1); //process_em_data_flag
940 bool process_cc_data_flag = br.getBits(1);
941 br.skipBits(1); //additional_data_flag
942 size_t cc_count = br.getBits(5);
943 br.skipBits(8); // em_data;
944 payload_size -= 2;
945
946 if (process_cc_data_flag) {
947 AString out;
948
949 sp<ABuffer> ccBuf = new ABuffer(cc_count * sizeof(CCData));
950 ccBuf->setRange(0, 0);
951
952 for (size_t i = 0; i < cc_count; i++) {
953 uint8_t marker = br.getBits(5);
954 CHECK_EQ(marker, 0x1f);
955
956 bool cc_valid = br.getBits(1);
957 uint8_t cc_type = br.getBits(2);
958 // remove odd parity bit
959 uint8_t cc_data_1 = br.getBits(8) & 0x7f;
960 uint8_t cc_data_2 = br.getBits(8) & 0x7f;
961
962 if (cc_valid
963 && (cc_type == 0 || cc_type == 1)) {
964 CCData cc(cc_type, cc_data_1, cc_data_2);
965 if (!isNullPad(&cc)) {
Chong Zhangb86e68f2014-08-01 13:46:53 -0700966 size_t channel;
967 if (cc.getChannel(&channel) && getTrackIndex(channel) < 0) {
968 mTrackIndices[channel] = mFoundChannels.size();
969 mFoundChannels.push_back(channel);
970 trackAdded = true;
971 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700972 memcpy(ccBuf->data() + ccBuf->size(),
973 (void *)&cc, sizeof(cc));
974 ccBuf->setRange(0, ccBuf->size() + sizeof(CCData));
975 }
976 }
977 }
978 payload_size -= cc_count * 3;
979
980 mCCMap.add(timeUs, ccBuf);
981 break;
982 }
983 } else {
984 ALOGV("Malformed SEI payload type 4");
985 }
986 } else {
987 ALOGV("Unsupported SEI payload type %d", payload_type);
988 }
989
990 // skipping remaining bits of this payload
991 br.skipBits(payload_size * 8);
992 }
993
Chong Zhangb86e68f2014-08-01 13:46:53 -0700994 return trackAdded;
995}
996
997sp<ABuffer> NuPlayer::CCDecoder::filterCCBuf(
998 const sp<ABuffer> &ccBuf, size_t index) {
999 sp<ABuffer> filteredCCBuf = new ABuffer(ccBuf->size());
1000 filteredCCBuf->setRange(0, 0);
1001
1002 size_t cc_count = ccBuf->size() / sizeof(CCData);
1003 const CCData* cc_data = (const CCData*)ccBuf->data();
1004 for (size_t i = 0; i < cc_count; ++i) {
1005 size_t channel;
1006 if (cc_data[i].getChannel(&channel)) {
1007 mCurrentChannel = channel;
1008 }
1009 if (mCurrentChannel == mFoundChannels[index]) {
1010 memcpy(filteredCCBuf->data() + filteredCCBuf->size(),
1011 (void *)&cc_data[i], sizeof(CCData));
1012 filteredCCBuf->setRange(0, filteredCCBuf->size() + sizeof(CCData));
1013 }
1014 }
1015
1016 return filteredCCBuf;
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001017}
1018
1019void NuPlayer::CCDecoder::decode(const sp<ABuffer> &accessUnit) {
Chong Zhangb86e68f2014-08-01 13:46:53 -07001020 if (extractFromSEI(accessUnit)) {
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001021 ALOGI("Found CEA-608 track");
1022 sp<AMessage> msg = mNotify->dup();
1023 msg->setInt32("what", kWhatTrackAdded);
1024 msg->post();
1025 }
1026 // TODO: extract CC from other sources
1027}
1028
1029void NuPlayer::CCDecoder::display(int64_t timeUs) {
Chong Zhangb86e68f2014-08-01 13:46:53 -07001030 if (!isTrackValid(mSelectedTrack)) {
1031 ALOGE("Could not find current track(index=%d)", mSelectedTrack);
1032 return;
1033 }
1034
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001035 ssize_t index = mCCMap.indexOfKey(timeUs);
1036 if (index < 0) {
1037 ALOGV("cc for timestamp %" PRId64 " not found", timeUs);
1038 return;
1039 }
1040
Chong Zhangb86e68f2014-08-01 13:46:53 -07001041 sp<ABuffer> ccBuf = filterCCBuf(mCCMap.valueAt(index), mSelectedTrack);
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001042
1043 if (ccBuf->size() > 0) {
1044#if 0
1045 dumpBytePair(ccBuf);
1046#endif
1047
1048 ccBuf->meta()->setInt32("trackIndex", mSelectedTrack);
1049 ccBuf->meta()->setInt64("timeUs", timeUs);
1050 ccBuf->meta()->setInt64("durationUs", 0ll);
1051
1052 sp<AMessage> msg = mNotify->dup();
1053 msg->setInt32("what", kWhatClosedCaptionData);
1054 msg->setBuffer("buffer", ccBuf);
1055 msg->post();
1056 }
1057
1058 // remove all entries before timeUs
1059 mCCMap.removeItemsAt(0, index + 1);
1060}
1061
Chong Zhangb86e68f2014-08-01 13:46:53 -07001062void NuPlayer::CCDecoder::flush() {
1063 mCCMap.clear();
1064}
1065
Andreas Huberf9334412010-12-15 15:17:42 -08001066} // namespace android
1067