blob: 601f6b5b166269be1b5aa166913429205553ebb3 [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 }
Lajos Molnar09524832014-07-17 14:29:51 -0700192}
193
Lajos Molnar1cd13982014-01-17 15:12:51 -0800194void NuPlayer::Decoder::requestCodecNotification() {
195 if (mCodec != NULL) {
196 sp<AMessage> reply = new AMessage(kWhatCodecNotify, id());
197 reply->setInt32("generation", mBufferGeneration);
198 mCodec->requestActivityNotification(reply);
199 }
200}
201
202bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) {
203 int32_t generation;
204 CHECK(msg->findInt32("generation", &generation));
205 return generation != mBufferGeneration;
206}
207
208void NuPlayer::Decoder::init() {
209 mDecoderLooper->registerHandler(this);
210}
211
212void NuPlayer::Decoder::configure(const sp<AMessage> &format) {
213 sp<AMessage> msg = new AMessage(kWhatConfigure, id());
214 msg->setMessage("format", format);
215 msg->post();
216}
217
Lajos Molnar87603c02014-08-20 19:25:30 -0700218void NuPlayer::Decoder::signalUpdateFormat(const sp<AMessage> &format) {
219 sp<AMessage> msg = new AMessage(kWhatUpdateFormat, id());
220 msg->setMessage("format", format);
221 msg->post();
222}
223
Lajos Molnar09524832014-07-17 14:29:51 -0700224status_t NuPlayer::Decoder::getInputBuffers(Vector<sp<ABuffer> > *buffers) const {
225 sp<AMessage> msg = new AMessage(kWhatGetInputBuffers, id());
226 msg->setPointer("buffers", buffers);
227
228 sp<AMessage> response;
229 return PostAndAwaitResponse(msg, &response);
230}
231
Lajos Molnar1cd13982014-01-17 15:12:51 -0800232void NuPlayer::Decoder::handleError(int32_t err)
233{
Andy Hungcf31f1e2014-09-23 14:59:01 -0700234 // We cannot immediately release the codec due to buffers still outstanding
235 // in the renderer. We signal to the player the error so it can shutdown/release the
236 // decoder after flushing and increment the generation to discard unnecessary messages.
237
238 ++mBufferGeneration;
Wei Jiac22c6952014-08-29 14:47:50 -0700239
Lajos Molnar1cd13982014-01-17 15:12:51 -0800240 sp<AMessage> notify = mNotify->dup();
241 notify->setInt32("what", kWhatError);
242 notify->setInt32("err", err);
243 notify->post();
244}
245
246bool NuPlayer::Decoder::handleAnInputBuffer() {
247 size_t bufferIx = -1;
248 status_t res = mCodec->dequeueInputBuffer(&bufferIx);
249 ALOGV("[%s] dequeued input: %d",
250 mComponentName.c_str(), res == OK ? (int)bufferIx : res);
251 if (res != OK) {
252 if (res != -EAGAIN) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700253 ALOGE("Failed to dequeue input buffer for %s (err=%d)",
254 mComponentName.c_str(), res);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800255 handleError(res);
256 }
257 return false;
Andreas Huber078cfcf2011-09-15 12:25:04 -0700258 }
259
Lajos Molnar1cd13982014-01-17 15:12:51 -0800260 CHECK_LT(bufferIx, mInputBuffers.size());
Andreas Huberf9334412010-12-15 15:17:42 -0800261
Lajos Molnar09524832014-07-17 14:29:51 -0700262 if (mMediaBuffers[bufferIx] != NULL) {
263 mMediaBuffers[bufferIx]->release();
264 mMediaBuffers.editItemAt(bufferIx) = NULL;
265 }
266 mInputBufferIsDequeued.editItemAt(bufferIx) = true;
267
Lajos Molnar1cd13982014-01-17 15:12:51 -0800268 sp<AMessage> reply = new AMessage(kWhatInputBufferFilled, id());
269 reply->setSize("buffer-ix", bufferIx);
270 reply->setInt32("generation", mBufferGeneration);
271
Lajos Molnar87603c02014-08-20 19:25:30 -0700272 if (!mCSDsToSubmit.isEmpty()) {
273 sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0);
274 ALOGI("[%s] resubmitting CSD", mComponentName.c_str());
275 reply->setBuffer("buffer", buffer);
276 mCSDsToSubmit.removeAt(0);
277 reply->post();
278 return true;
279 }
280
Lajos Molnar1cd13982014-01-17 15:12:51 -0800281 sp<AMessage> notify = mNotify->dup();
282 notify->setInt32("what", kWhatFillThisBuffer);
283 notify->setBuffer("buffer", mInputBuffers[bufferIx]);
284 notify->setMessage("reply", reply);
285 notify->post();
286 return true;
287}
288
289void android::NuPlayer::Decoder::onInputBufferFilled(const sp<AMessage> &msg) {
290 size_t bufferIx;
291 CHECK(msg->findSize("buffer-ix", &bufferIx));
292 CHECK_LT(bufferIx, mInputBuffers.size());
293 sp<ABuffer> codecBuffer = mInputBuffers[bufferIx];
294
295 sp<ABuffer> buffer;
296 bool hasBuffer = msg->findBuffer("buffer", &buffer);
Lajos Molnar09524832014-07-17 14:29:51 -0700297
298 // handle widevine classic source - that fills an arbitrary input buffer
299 MediaBuffer *mediaBuffer = NULL;
Wei Jia96e92b52014-09-18 17:36:20 -0700300 if (hasBuffer) {
301 mediaBuffer = (MediaBuffer *)(buffer->getMediaBufferBase());
302 if (mediaBuffer != NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700303 // likely filled another buffer than we requested: adjust buffer index
304 size_t ix;
305 for (ix = 0; ix < mInputBuffers.size(); ix++) {
306 const sp<ABuffer> &buf = mInputBuffers[ix];
307 if (buf->data() == mediaBuffer->data()) {
308 // all input buffers are dequeued on start, hence the check
309 CHECK(mInputBufferIsDequeued[ix]);
310 ALOGV("[%s] received MediaBuffer for #%zu instead of #%zu",
311 mComponentName.c_str(), ix, bufferIx);
312
313 // TRICKY: need buffer for the metadata, so instead, set
314 // codecBuffer to the same (though incorrect) buffer to
315 // avoid a memcpy into the codecBuffer
316 codecBuffer = buffer;
317 codecBuffer->setRange(
318 mediaBuffer->range_offset(),
319 mediaBuffer->range_length());
320 bufferIx = ix;
321 break;
322 }
323 }
324 CHECK(ix < mInputBuffers.size());
325 }
326 }
327
Andy Hungcf31f1e2014-09-23 14:59:01 -0700328
Lajos Molnar09524832014-07-17 14:29:51 -0700329
Lajos Molnar1cd13982014-01-17 15:12:51 -0800330 if (buffer == NULL /* includes !hasBuffer */) {
331 int32_t streamErr = ERROR_END_OF_STREAM;
332 CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
333
334 if (streamErr == OK) {
335 /* buffers are returned to hold on to */
336 return;
337 }
338
339 // attempt to queue EOS
340 status_t err = mCodec->queueInputBuffer(
341 bufferIx,
342 0,
343 0,
344 0,
345 MediaCodec::BUFFER_FLAG_EOS);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700346 if (err == OK) {
347 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
348 } else if (streamErr == ERROR_END_OF_STREAM) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800349 streamErr = err;
350 // err will not be ERROR_END_OF_STREAM
351 }
352
353 if (streamErr != ERROR_END_OF_STREAM) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700354 ALOGE("Stream error for %s (err=%d), EOS %s queued",
355 mComponentName.c_str(),
356 streamErr,
357 err == OK ? "successfully" : "unsuccessfully");
Lajos Molnar1cd13982014-01-17 15:12:51 -0800358 handleError(streamErr);
359 }
360 } else {
361 int64_t timeUs = 0;
362 uint32_t flags = 0;
363 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
364
Lajos Molnar87603c02014-08-20 19:25:30 -0700365 int32_t eos, csd;
366 // we do not expect SYNCFRAME for decoder
Lajos Molnar1cd13982014-01-17 15:12:51 -0800367 if (buffer->meta()->findInt32("eos", &eos) && eos) {
368 flags |= MediaCodec::BUFFER_FLAG_EOS;
Lajos Molnar87603c02014-08-20 19:25:30 -0700369 } else if (buffer->meta()->findInt32("csd", &csd) && csd) {
370 flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800371 }
372
373 // copy into codec buffer
374 if (buffer != codecBuffer) {
375 CHECK_LE(buffer->size(), codecBuffer->capacity());
376 codecBuffer->setRange(0, buffer->size());
377 memcpy(codecBuffer->data(), buffer->data(), buffer->size());
378 }
379
380 status_t err = mCodec->queueInputBuffer(
381 bufferIx,
382 codecBuffer->offset(),
383 codecBuffer->size(),
384 timeUs,
385 flags);
386 if (err != OK) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700387 if (mediaBuffer != NULL) {
388 mediaBuffer->release();
389 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800390 ALOGE("Failed to queue input buffer for %s (err=%d)",
391 mComponentName.c_str(), err);
392 handleError(err);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700393 } else {
394 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
395 if (mediaBuffer != NULL) {
396 CHECK(mMediaBuffers[bufferIx] == NULL);
397 mMediaBuffers.editItemAt(bufferIx) = mediaBuffer;
398 }
Lajos Molnar09524832014-07-17 14:29:51 -0700399 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800400 }
401}
402
403bool NuPlayer::Decoder::handleAnOutputBuffer() {
404 size_t bufferIx = -1;
405 size_t offset;
406 size_t size;
407 int64_t timeUs;
408 uint32_t flags;
409 status_t res = mCodec->dequeueOutputBuffer(
410 &bufferIx, &offset, &size, &timeUs, &flags);
411
412 if (res != OK) {
413 ALOGV("[%s] dequeued output: %d", mComponentName.c_str(), res);
414 } else {
415 ALOGV("[%s] dequeued output: %d (time=%lld flags=%" PRIu32 ")",
416 mComponentName.c_str(), (int)bufferIx, timeUs, flags);
417 }
418
419 if (res == INFO_OUTPUT_BUFFERS_CHANGED) {
420 res = mCodec->getOutputBuffers(&mOutputBuffers);
421 if (res != OK) {
422 ALOGE("Failed to get output buffers for %s after INFO event (err=%d)",
423 mComponentName.c_str(), res);
424 handleError(res);
425 return false;
426 }
427 // NuPlayer ignores this
428 return true;
429 } else if (res == INFO_FORMAT_CHANGED) {
430 sp<AMessage> format = new AMessage();
431 res = mCodec->getOutputFormat(&format);
432 if (res != OK) {
433 ALOGE("Failed to get output format for %s after INFO event (err=%d)",
434 mComponentName.c_str(), res);
435 handleError(res);
436 return false;
437 }
438
439 sp<AMessage> notify = mNotify->dup();
440 notify->setInt32("what", kWhatOutputFormatChanged);
441 notify->setMessage("format", format);
442 notify->post();
443 return true;
444 } else if (res == INFO_DISCONTINUITY) {
445 // nothing to do
446 return true;
447 } else if (res != OK) {
448 if (res != -EAGAIN) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700449 ALOGE("Failed to dequeue output buffer for %s (err=%d)",
450 mComponentName.c_str(), res);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800451 handleError(res);
452 }
453 return false;
454 }
455
456 CHECK_LT(bufferIx, mOutputBuffers.size());
457 sp<ABuffer> buffer = mOutputBuffers[bufferIx];
458 buffer->setRange(offset, size);
459 buffer->meta()->clear();
460 buffer->meta()->setInt64("timeUs", timeUs);
461 if (flags & MediaCodec::BUFFER_FLAG_EOS) {
462 buffer->meta()->setInt32("eos", true);
463 }
464 // we do not expect CODECCONFIG or SYNCFRAME for decoder
465
466 sp<AMessage> reply = new AMessage(kWhatRenderBuffer, id());
467 reply->setSize("buffer-ix", bufferIx);
468 reply->setInt32("generation", mBufferGeneration);
469
470 sp<AMessage> notify = mNotify->dup();
471 notify->setInt32("what", kWhatDrainThisBuffer);
472 notify->setBuffer("buffer", buffer);
473 notify->setMessage("reply", reply);
474 notify->post();
475
476 // FIXME: This should be handled after rendering is complete,
477 // but Renderer needs it now
478 if (flags & MediaCodec::BUFFER_FLAG_EOS) {
479 ALOGV("queueing eos [%s]", mComponentName.c_str());
480 sp<AMessage> notify = mNotify->dup();
481 notify->setInt32("what", kWhatEOS);
482 notify->setInt32("err", ERROR_END_OF_STREAM);
483 notify->post();
484 }
485 return true;
486}
487
488void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) {
489 status_t err;
490 int32_t render;
491 size_t bufferIx;
492 CHECK(msg->findSize("buffer-ix", &bufferIx));
493 if (msg->findInt32("render", &render) && render) {
Lajos Molnardc43dfa2014-05-07 15:33:04 -0700494 int64_t timestampNs;
495 CHECK(msg->findInt64("timestampNs", &timestampNs));
496 err = mCodec->renderOutputBufferAndRelease(bufferIx, timestampNs);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800497 } else {
498 err = mCodec->releaseOutputBuffer(bufferIx);
499 }
500 if (err != OK) {
501 ALOGE("failed to release output buffer for %s (err=%d)",
502 mComponentName.c_str(), err);
503 handleError(err);
504 }
505}
506
507void NuPlayer::Decoder::onFlush() {
508 status_t err = OK;
509 if (mCodec != NULL) {
510 err = mCodec->flush();
Lajos Molnar87603c02014-08-20 19:25:30 -0700511 mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator
Lajos Molnar1cd13982014-01-17 15:12:51 -0800512 ++mBufferGeneration;
513 }
514
515 if (err != OK) {
516 ALOGE("failed to flush %s (err=%d)", mComponentName.c_str(), err);
517 handleError(err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700518 // finish with posting kWhatFlushCompleted.
519 // we attempt to release the buffers even if flush fails.
Lajos Molnar1cd13982014-01-17 15:12:51 -0800520 }
Lajos Molnar09524832014-07-17 14:29:51 -0700521 releaseAndResetMediaBuffers();
522
Lajos Molnar1cd13982014-01-17 15:12:51 -0800523 sp<AMessage> notify = mNotify->dup();
524 notify->setInt32("what", kWhatFlushCompleted);
525 notify->post();
Wei Jia704e7262014-06-04 16:21:56 -0700526 mPaused = true;
527}
528
529void NuPlayer::Decoder::onResume() {
530 mPaused = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800531}
532
533void NuPlayer::Decoder::onShutdown() {
534 status_t err = OK;
535 if (mCodec != NULL) {
536 err = mCodec->release();
537 mCodec = NULL;
538 ++mBufferGeneration;
539
540 if (mNativeWindow != NULL) {
541 // reconnect to surface as MediaCodec disconnected from it
Wei Jia3fb9f682014-08-20 14:30:09 -0700542 status_t error =
Lajos Molnar1cd13982014-01-17 15:12:51 -0800543 native_window_api_connect(
544 mNativeWindow->getNativeWindow().get(),
Wei Jia3fb9f682014-08-20 14:30:09 -0700545 NATIVE_WINDOW_API_MEDIA);
546 ALOGW_IF(error != NO_ERROR,
547 "[%s] failed to connect to native window, error=%d",
548 mComponentName.c_str(), error);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800549 }
550 mComponentName = "decoder";
551 }
552
Lajos Molnar09524832014-07-17 14:29:51 -0700553 releaseAndResetMediaBuffers();
554
Lajos Molnar1cd13982014-01-17 15:12:51 -0800555 if (err != OK) {
556 ALOGE("failed to release %s (err=%d)", mComponentName.c_str(), err);
557 handleError(err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700558 // finish with posting kWhatShutdownCompleted.
Lajos Molnar1cd13982014-01-17 15:12:51 -0800559 }
560
561 sp<AMessage> notify = mNotify->dup();
562 notify->setInt32("what", kWhatShutdownCompleted);
563 notify->post();
Wei Jia704e7262014-06-04 16:21:56 -0700564 mPaused = true;
Andreas Huberf9334412010-12-15 15:17:42 -0800565}
566
567void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800568 ALOGV("[%s] onMessage: %s", mComponentName.c_str(), msg->debugString().c_str());
569
Andreas Huberf9334412010-12-15 15:17:42 -0800570 switch (msg->what()) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800571 case kWhatConfigure:
572 {
573 sp<AMessage> format;
574 CHECK(msg->findMessage("format", &format));
575 onConfigure(format);
576 break;
577 }
578
Lajos Molnar87603c02014-08-20 19:25:30 -0700579 case kWhatUpdateFormat:
580 {
581 sp<AMessage> format;
582 CHECK(msg->findMessage("format", &format));
583 rememberCodecSpecificData(format);
584 break;
585 }
586
Lajos Molnar09524832014-07-17 14:29:51 -0700587 case kWhatGetInputBuffers:
588 {
589 uint32_t replyID;
590 CHECK(msg->senderAwaitsResponse(&replyID));
591
592 Vector<sp<ABuffer> > *dstBuffers;
593 CHECK(msg->findPointer("buffers", (void **)&dstBuffers));
594
595 dstBuffers->clear();
596 for (size_t i = 0; i < mInputBuffers.size(); i++) {
597 dstBuffers->push(mInputBuffers[i]);
598 }
599
600 (new AMessage)->postReply(replyID);
601 break;
602 }
603
Andreas Huberf9334412010-12-15 15:17:42 -0800604 case kWhatCodecNotify:
605 {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800606 if (!isStaleReply(msg)) {
Chong Zhange47d4442014-09-30 13:12:22 -0700607 int32_t numInput, numOutput;
608
609 if (!msg->findInt32("input-buffers", &numInput)) {
610 numInput = INT32_MAX;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800611 }
Andreas Huberf9334412010-12-15 15:17:42 -0800612
Chong Zhange47d4442014-09-30 13:12:22 -0700613 if (!msg->findInt32("output-buffers", &numOutput)) {
614 numOutput = INT32_MAX;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800615 }
Chong Zhange47d4442014-09-30 13:12:22 -0700616
617 if (!mPaused) {
618 while (numInput-- > 0 && handleAnInputBuffer()) {}
619 }
620
621 while (numOutput-- > 0 && handleAnOutputBuffer()) {}
Andreas Huberf9334412010-12-15 15:17:42 -0800622 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800623
624 requestCodecNotification();
625 break;
626 }
627
628 case kWhatInputBufferFilled:
629 {
630 if (!isStaleReply(msg)) {
631 onInputBufferFilled(msg);
632 }
Lajos Molnarb9b87fe2014-09-10 13:53:21 -0700633
Lajos Molnar1cd13982014-01-17 15:12:51 -0800634 break;
635 }
636
637 case kWhatRenderBuffer:
638 {
639 if (!isStaleReply(msg)) {
640 onRenderBuffer(msg);
641 }
642 break;
643 }
644
645 case kWhatFlush:
646 {
Lajos Molnar87603c02014-08-20 19:25:30 -0700647 sp<AMessage> format;
648 if (msg->findMessage("new-format", &format)) {
649 rememberCodecSpecificData(format);
650 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800651 onFlush();
652 break;
653 }
654
Wei Jia704e7262014-06-04 16:21:56 -0700655 case kWhatResume:
656 {
657 onResume();
658 break;
659 }
660
Lajos Molnar1cd13982014-01-17 15:12:51 -0800661 case kWhatShutdown:
662 {
663 onShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -0800664 break;
665 }
666
667 default:
668 TRESPASS();
669 break;
670 }
671}
672
Lajos Molnar87603c02014-08-20 19:25:30 -0700673void NuPlayer::Decoder::signalFlush(const sp<AMessage> &format) {
674 sp<AMessage> msg = new AMessage(kWhatFlush, id());
675 if (format != NULL) {
676 msg->setMessage("new-format", format);
677 }
678 msg->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800679}
680
681void NuPlayer::Decoder::signalResume() {
Wei Jia704e7262014-06-04 16:21:56 -0700682 (new AMessage(kWhatResume, id()))->post();
Andreas Huberf9334412010-12-15 15:17:42 -0800683}
684
Andreas Huber3831a062010-12-21 10:22:33 -0800685void NuPlayer::Decoder::initiateShutdown() {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800686 (new AMessage(kWhatShutdown, id()))->post();
Andreas Huber3831a062010-12-21 10:22:33 -0800687}
688
Robert Shih6d0a94e2014-01-23 16:18:22 -0800689bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(const sp<AMessage> &targetFormat) const {
690 if (targetFormat == NULL) {
691 return true;
692 }
693
694 AString mime;
695 if (!targetFormat->findString("mime", &mime)) {
696 return false;
697 }
698
699 if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
700 // field-by-field comparison
701 const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
702 for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
703 int32_t oldVal, newVal;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800704 if (!mOutputFormat->findInt32(keys[i], &oldVal) ||
705 !targetFormat->findInt32(keys[i], &newVal) ||
706 oldVal != newVal) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800707 return false;
708 }
709 }
710
711 sp<ABuffer> oldBuf, newBuf;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800712 if (mOutputFormat->findBuffer("csd-0", &oldBuf) &&
713 targetFormat->findBuffer("csd-0", &newBuf)) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800714 if (oldBuf->size() != newBuf->size()) {
715 return false;
716 }
717 return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size());
718 }
719 }
720 return false;
721}
722
723bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800724 if (mOutputFormat == NULL) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800725 return false;
726 }
727
728 if (targetFormat == NULL) {
729 return true;
730 }
731
732 AString oldMime, newMime;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800733 if (!mOutputFormat->findString("mime", &oldMime)
Robert Shih6d0a94e2014-01-23 16:18:22 -0800734 || !targetFormat->findString("mime", &newMime)
735 || !(oldMime == newMime)) {
736 return false;
737 }
738
739 bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
740 bool seamless;
741 if (audio) {
742 seamless = supportsSeamlessAudioFormatChange(targetFormat);
743 } else {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800744 int32_t isAdaptive;
745 seamless = (mCodec != NULL &&
746 mInputFormat->findInt32("adaptive-playback", &isAdaptive) &&
747 isAdaptive);
Robert Shih6d0a94e2014-01-23 16:18:22 -0800748 }
749
750 ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
751 return seamless;
752}
753
Chong Zhangb86e68f2014-08-01 13:46:53 -0700754struct CCData {
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700755 CCData(uint8_t type, uint8_t data1, uint8_t data2)
756 : mType(type), mData1(data1), mData2(data2) {
757 }
Chong Zhangb86e68f2014-08-01 13:46:53 -0700758 bool getChannel(size_t *channel) const {
759 if (mData1 >= 0x10 && mData1 <= 0x1f) {
760 *channel = (mData1 >= 0x18 ? 1 : 0) + (mType ? 2 : 0);
761 return true;
762 }
763 return false;
764 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700765
766 uint8_t mType;
767 uint8_t mData1;
768 uint8_t mData2;
769};
770
Chong Zhangb86e68f2014-08-01 13:46:53 -0700771static bool isNullPad(CCData *cc) {
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700772 return cc->mData1 < 0x10 && cc->mData2 < 0x10;
773}
774
Chong Zhangb86e68f2014-08-01 13:46:53 -0700775static void dumpBytePair(const sp<ABuffer> &ccBuf) {
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700776 size_t offset = 0;
777 AString out;
778
779 while (offset < ccBuf->size()) {
780 char tmp[128];
781
782 CCData *cc = (CCData *) (ccBuf->data() + offset);
783
784 if (isNullPad(cc)) {
785 // 1 null pad or XDS metadata, ignore
786 offset += sizeof(CCData);
787 continue;
788 }
789
790 if (cc->mData1 >= 0x20 && cc->mData1 <= 0x7f) {
791 // 2 basic chars
792 sprintf(tmp, "[%d]Basic: %c %c", cc->mType, cc->mData1, cc->mData2);
793 } else if ((cc->mData1 == 0x11 || cc->mData1 == 0x19)
794 && cc->mData2 >= 0x30 && cc->mData2 <= 0x3f) {
795 // 1 special char
796 sprintf(tmp, "[%d]Special: %02x %02x", cc->mType, cc->mData1, cc->mData2);
797 } else if ((cc->mData1 == 0x12 || cc->mData1 == 0x1A)
798 && cc->mData2 >= 0x20 && cc->mData2 <= 0x3f){
799 // 1 Spanish/French char
800 sprintf(tmp, "[%d]Spanish: %02x %02x", cc->mType, cc->mData1, cc->mData2);
801 } else if ((cc->mData1 == 0x13 || cc->mData1 == 0x1B)
802 && cc->mData2 >= 0x20 && cc->mData2 <= 0x3f){
803 // 1 Portuguese/German/Danish char
804 sprintf(tmp, "[%d]German: %02x %02x", cc->mType, cc->mData1, cc->mData2);
805 } else if ((cc->mData1 == 0x11 || cc->mData1 == 0x19)
806 && cc->mData2 >= 0x20 && cc->mData2 <= 0x2f){
807 // Mid-Row Codes (Table 69)
808 sprintf(tmp, "[%d]Mid-row: %02x %02x", cc->mType, cc->mData1, cc->mData2);
809 } else if (((cc->mData1 == 0x14 || cc->mData1 == 0x1c)
810 && cc->mData2 >= 0x20 && cc->mData2 <= 0x2f)
811 ||
812 ((cc->mData1 == 0x17 || cc->mData1 == 0x1f)
813 && cc->mData2 >= 0x21 && cc->mData2 <= 0x23)){
814 // Misc Control Codes (Table 70)
815 sprintf(tmp, "[%d]Ctrl: %02x %02x", cc->mType, cc->mData1, cc->mData2);
816 } else if ((cc->mData1 & 0x70) == 0x10
817 && (cc->mData2 & 0x40) == 0x40
818 && ((cc->mData1 & 0x07) || !(cc->mData2 & 0x20)) ) {
819 // Preamble Address Codes (Table 71)
820 sprintf(tmp, "[%d]PAC: %02x %02x", cc->mType, cc->mData1, cc->mData2);
821 } else {
822 sprintf(tmp, "[%d]Invalid: %02x %02x", cc->mType, cc->mData1, cc->mData2);
823 }
824
825 if (out.size() > 0) {
826 out.append(", ");
827 }
828
829 out.append(tmp);
830
831 offset += sizeof(CCData);
832 }
833
834 ALOGI("%s", out.c_str());
835}
836
Chong Zhangb86e68f2014-08-01 13:46:53 -0700837NuPlayer::CCDecoder::CCDecoder(const sp<AMessage> &notify)
838 : mNotify(notify),
839 mCurrentChannel(0),
840 mSelectedTrack(-1) {
841 for (size_t i = 0; i < sizeof(mTrackIndices)/sizeof(mTrackIndices[0]); ++i) {
842 mTrackIndices[i] = -1;
843 }
844}
845
846size_t NuPlayer::CCDecoder::getTrackCount() const {
847 return mFoundChannels.size();
848}
849
850sp<AMessage> NuPlayer::CCDecoder::getTrackInfo(size_t index) const {
851 if (!isTrackValid(index)) {
852 return NULL;
853 }
854
855 sp<AMessage> format = new AMessage();
856
857 format->setInt32("type", MEDIA_TRACK_TYPE_SUBTITLE);
858 format->setString("language", "und");
859 format->setString("mime", MEDIA_MIMETYPE_TEXT_CEA_608);
860 //CC1, field 0 channel 0
861 bool isDefaultAuto = (mFoundChannels[index] == 0);
862 format->setInt32("auto", isDefaultAuto);
863 format->setInt32("default", isDefaultAuto);
864 format->setInt32("forced", 0);
865
866 return format;
867}
868
869status_t NuPlayer::CCDecoder::selectTrack(size_t index, bool select) {
870 if (!isTrackValid(index)) {
871 return BAD_VALUE;
872 }
873
874 if (select) {
875 if (mSelectedTrack == (ssize_t)index) {
876 ALOGE("track %zu already selected", index);
877 return BAD_VALUE;
878 }
879 ALOGV("selected track %zu", index);
880 mSelectedTrack = index;
881 } else {
882 if (mSelectedTrack != (ssize_t)index) {
883 ALOGE("track %zu is not selected", index);
884 return BAD_VALUE;
885 }
886 ALOGV("unselected track %zu", index);
887 mSelectedTrack = -1;
888 }
889
890 return OK;
891}
892
893bool NuPlayer::CCDecoder::isSelected() const {
894 return mSelectedTrack >= 0 && mSelectedTrack < (int32_t) getTrackCount();
895}
896
897bool NuPlayer::CCDecoder::isTrackValid(size_t index) const {
898 return index < getTrackCount();
899}
900
901int32_t NuPlayer::CCDecoder::getTrackIndex(size_t channel) const {
902 if (channel < sizeof(mTrackIndices)/sizeof(mTrackIndices[0])) {
903 return mTrackIndices[channel];
904 }
905 return -1;
906}
907
908// returns true if a new CC track is found
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700909bool NuPlayer::CCDecoder::extractFromSEI(const sp<ABuffer> &accessUnit) {
910 int64_t timeUs;
911 CHECK(accessUnit->meta()->findInt64("timeUs", &timeUs));
912
913 sp<ABuffer> sei;
914 if (!accessUnit->meta()->findBuffer("sei", &sei) || sei == NULL) {
915 return false;
916 }
917
Chong Zhangb86e68f2014-08-01 13:46:53 -0700918 bool trackAdded = false;
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700919
Chong Zhang862f8452014-06-26 19:55:23 -0700920 NALBitReader br(sei->data() + 1, sei->size() - 1);
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700921 // sei_message()
Chong Zhang862f8452014-06-26 19:55:23 -0700922 while (br.atLeastNumBitsLeft(16)) { // at least 16-bit for sei_message()
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700923 uint32_t payload_type = 0;
924 size_t payload_size = 0;
925 uint8_t last_byte;
926
927 do {
928 last_byte = br.getBits(8);
929 payload_type += last_byte;
930 } while (last_byte == 0xFF);
931
932 do {
933 last_byte = br.getBits(8);
934 payload_size += last_byte;
935 } while (last_byte == 0xFF);
936
937 // sei_payload()
938 if (payload_type == 4) {
939 // user_data_registered_itu_t_t35()
940
941 // ATSC A/72: 6.4.2
942 uint8_t itu_t_t35_country_code = br.getBits(8);
943 uint16_t itu_t_t35_provider_code = br.getBits(16);
944 uint32_t user_identifier = br.getBits(32);
945 uint8_t user_data_type_code = br.getBits(8);
946
947 payload_size -= 1 + 2 + 4 + 1;
948
949 if (itu_t_t35_country_code == 0xB5
950 && itu_t_t35_provider_code == 0x0031
951 && user_identifier == 'GA94'
952 && user_data_type_code == 0x3) {
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700953 // MPEG_cc_data()
954 // ATSC A/53 Part 4: 6.2.3.1
955 br.skipBits(1); //process_em_data_flag
956 bool process_cc_data_flag = br.getBits(1);
957 br.skipBits(1); //additional_data_flag
958 size_t cc_count = br.getBits(5);
959 br.skipBits(8); // em_data;
960 payload_size -= 2;
961
962 if (process_cc_data_flag) {
963 AString out;
964
965 sp<ABuffer> ccBuf = new ABuffer(cc_count * sizeof(CCData));
966 ccBuf->setRange(0, 0);
967
968 for (size_t i = 0; i < cc_count; i++) {
969 uint8_t marker = br.getBits(5);
970 CHECK_EQ(marker, 0x1f);
971
972 bool cc_valid = br.getBits(1);
973 uint8_t cc_type = br.getBits(2);
974 // remove odd parity bit
975 uint8_t cc_data_1 = br.getBits(8) & 0x7f;
976 uint8_t cc_data_2 = br.getBits(8) & 0x7f;
977
978 if (cc_valid
979 && (cc_type == 0 || cc_type == 1)) {
980 CCData cc(cc_type, cc_data_1, cc_data_2);
981 if (!isNullPad(&cc)) {
Chong Zhangb86e68f2014-08-01 13:46:53 -0700982 size_t channel;
983 if (cc.getChannel(&channel) && getTrackIndex(channel) < 0) {
984 mTrackIndices[channel] = mFoundChannels.size();
985 mFoundChannels.push_back(channel);
986 trackAdded = true;
987 }
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700988 memcpy(ccBuf->data() + ccBuf->size(),
989 (void *)&cc, sizeof(cc));
990 ccBuf->setRange(0, ccBuf->size() + sizeof(CCData));
991 }
992 }
993 }
994 payload_size -= cc_count * 3;
995
996 mCCMap.add(timeUs, ccBuf);
997 break;
998 }
999 } else {
1000 ALOGV("Malformed SEI payload type 4");
1001 }
1002 } else {
1003 ALOGV("Unsupported SEI payload type %d", payload_type);
1004 }
1005
1006 // skipping remaining bits of this payload
1007 br.skipBits(payload_size * 8);
1008 }
1009
Chong Zhangb86e68f2014-08-01 13:46:53 -07001010 return trackAdded;
1011}
1012
1013sp<ABuffer> NuPlayer::CCDecoder::filterCCBuf(
1014 const sp<ABuffer> &ccBuf, size_t index) {
1015 sp<ABuffer> filteredCCBuf = new ABuffer(ccBuf->size());
1016 filteredCCBuf->setRange(0, 0);
1017
1018 size_t cc_count = ccBuf->size() / sizeof(CCData);
1019 const CCData* cc_data = (const CCData*)ccBuf->data();
1020 for (size_t i = 0; i < cc_count; ++i) {
1021 size_t channel;
1022 if (cc_data[i].getChannel(&channel)) {
1023 mCurrentChannel = channel;
1024 }
1025 if (mCurrentChannel == mFoundChannels[index]) {
1026 memcpy(filteredCCBuf->data() + filteredCCBuf->size(),
1027 (void *)&cc_data[i], sizeof(CCData));
1028 filteredCCBuf->setRange(0, filteredCCBuf->size() + sizeof(CCData));
1029 }
1030 }
1031
1032 return filteredCCBuf;
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001033}
1034
1035void NuPlayer::CCDecoder::decode(const sp<ABuffer> &accessUnit) {
Chong Zhangb86e68f2014-08-01 13:46:53 -07001036 if (extractFromSEI(accessUnit)) {
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001037 ALOGI("Found CEA-608 track");
1038 sp<AMessage> msg = mNotify->dup();
1039 msg->setInt32("what", kWhatTrackAdded);
1040 msg->post();
1041 }
1042 // TODO: extract CC from other sources
1043}
1044
1045void NuPlayer::CCDecoder::display(int64_t timeUs) {
Chong Zhangb86e68f2014-08-01 13:46:53 -07001046 if (!isTrackValid(mSelectedTrack)) {
1047 ALOGE("Could not find current track(index=%d)", mSelectedTrack);
1048 return;
1049 }
1050
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001051 ssize_t index = mCCMap.indexOfKey(timeUs);
1052 if (index < 0) {
1053 ALOGV("cc for timestamp %" PRId64 " not found", timeUs);
1054 return;
1055 }
1056
Chong Zhangb86e68f2014-08-01 13:46:53 -07001057 sp<ABuffer> ccBuf = filterCCBuf(mCCMap.valueAt(index), mSelectedTrack);
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001058
1059 if (ccBuf->size() > 0) {
1060#if 0
1061 dumpBytePair(ccBuf);
1062#endif
1063
1064 ccBuf->meta()->setInt32("trackIndex", mSelectedTrack);
1065 ccBuf->meta()->setInt64("timeUs", timeUs);
1066 ccBuf->meta()->setInt64("durationUs", 0ll);
1067
1068 sp<AMessage> msg = mNotify->dup();
1069 msg->setInt32("what", kWhatClosedCaptionData);
1070 msg->setBuffer("buffer", ccBuf);
1071 msg->post();
1072 }
1073
1074 // remove all entries before timeUs
1075 mCCMap.removeItemsAt(0, index + 1);
1076}
1077
Chong Zhangb86e68f2014-08-01 13:46:53 -07001078void NuPlayer::CCDecoder::flush() {
1079 mCCMap.clear();
1080}
1081
Andreas Huberf9334412010-12-15 15:17:42 -08001082} // namespace android
1083