NuPlayer2DecoderBase.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright 2017 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. //#define LOG_NDEBUG 0
  17. #define LOG_TAG "NuPlayer2DecoderBase"
  18. #include <utils/Log.h>
  19. #include <inttypes.h>
  20. #include "NuPlayer2DecoderBase.h"
  21. #include "NuPlayer2Renderer.h"
  22. #include <media/MediaCodecBuffer.h>
  23. #include <media/stagefright/foundation/ADebug.h>
  24. #include <media/stagefright/foundation/AMessage.h>
  25. namespace android {
  26. NuPlayer2::DecoderBase::DecoderBase(const sp<AMessage> &notify)
  27. : mNotify(notify),
  28. mBufferGeneration(0),
  29. mPaused(false),
  30. mStats(new AMessage),
  31. mRequestInputBuffersPending(false) {
  32. // Every decoder has its own looper because MediaCodec operations
  33. // are blocking, but NuPlayer2 needs asynchronous operations.
  34. mDecoderLooper = new ALooper;
  35. mDecoderLooper->setName("NP2Decoder");
  36. mDecoderLooper->start(false, /* runOnCallingThread */
  37. true, /* canCallJava */
  38. ANDROID_PRIORITY_AUDIO);
  39. }
  40. NuPlayer2::DecoderBase::~DecoderBase() {
  41. stopLooper();
  42. }
  43. static
  44. status_t PostAndAwaitResponse(
  45. const sp<AMessage> &msg, sp<AMessage> *response) {
  46. status_t err = msg->postAndAwaitResponse(response);
  47. if (err != OK) {
  48. return err;
  49. }
  50. if (!(*response)->findInt32("err", &err)) {
  51. err = OK;
  52. }
  53. return err;
  54. }
  55. void NuPlayer2::DecoderBase::configure(const sp<AMessage> &format) {
  56. sp<AMessage> msg = new AMessage(kWhatConfigure, this);
  57. msg->setMessage("format", format);
  58. msg->post();
  59. }
  60. void NuPlayer2::DecoderBase::init() {
  61. mDecoderLooper->registerHandler(this);
  62. }
  63. void NuPlayer2::DecoderBase::stopLooper() {
  64. mDecoderLooper->unregisterHandler(id());
  65. mDecoderLooper->stop();
  66. }
  67. void NuPlayer2::DecoderBase::setParameters(const sp<AMessage> &params) {
  68. sp<AMessage> msg = new AMessage(kWhatSetParameters, this);
  69. msg->setMessage("params", params);
  70. msg->post();
  71. }
  72. void NuPlayer2::DecoderBase::setRenderer(const sp<Renderer> &renderer) {
  73. sp<AMessage> msg = new AMessage(kWhatSetRenderer, this);
  74. msg->setObject("renderer", renderer);
  75. msg->post();
  76. }
  77. void NuPlayer2::DecoderBase::pause() {
  78. sp<AMessage> msg = new AMessage(kWhatPause, this);
  79. sp<AMessage> response;
  80. PostAndAwaitResponse(msg, &response);
  81. }
  82. void NuPlayer2::DecoderBase::signalFlush() {
  83. (new AMessage(kWhatFlush, this))->post();
  84. }
  85. void NuPlayer2::DecoderBase::signalResume(bool notifyComplete) {
  86. sp<AMessage> msg = new AMessage(kWhatResume, this);
  87. msg->setInt32("notifyComplete", notifyComplete);
  88. msg->post();
  89. }
  90. void NuPlayer2::DecoderBase::initiateShutdown() {
  91. (new AMessage(kWhatShutdown, this))->post();
  92. }
  93. void NuPlayer2::DecoderBase::onRequestInputBuffers() {
  94. if (mRequestInputBuffersPending) {
  95. return;
  96. }
  97. // doRequestBuffers() return true if we should request more data
  98. if (doRequestBuffers()) {
  99. mRequestInputBuffersPending = true;
  100. sp<AMessage> msg = new AMessage(kWhatRequestInputBuffers, this);
  101. msg->post(10 * 1000LL);
  102. }
  103. }
  104. void NuPlayer2::DecoderBase::onMessageReceived(const sp<AMessage> &msg) {
  105. switch (msg->what()) {
  106. case kWhatConfigure:
  107. {
  108. sp<AMessage> format;
  109. CHECK(msg->findMessage("format", &format));
  110. onConfigure(format);
  111. break;
  112. }
  113. case kWhatSetParameters:
  114. {
  115. sp<AMessage> params;
  116. CHECK(msg->findMessage("params", &params));
  117. onSetParameters(params);
  118. break;
  119. }
  120. case kWhatSetRenderer:
  121. {
  122. sp<RefBase> obj;
  123. CHECK(msg->findObject("renderer", &obj));
  124. onSetRenderer(static_cast<Renderer *>(obj.get()));
  125. break;
  126. }
  127. case kWhatPause:
  128. {
  129. sp<AReplyToken> replyID;
  130. CHECK(msg->senderAwaitsResponse(&replyID));
  131. mPaused = true;
  132. (new AMessage)->postReply(replyID);
  133. break;
  134. }
  135. case kWhatRequestInputBuffers:
  136. {
  137. mRequestInputBuffersPending = false;
  138. onRequestInputBuffers();
  139. break;
  140. }
  141. case kWhatFlush:
  142. {
  143. onFlush();
  144. break;
  145. }
  146. case kWhatResume:
  147. {
  148. int32_t notifyComplete;
  149. CHECK(msg->findInt32("notifyComplete", &notifyComplete));
  150. onResume(notifyComplete);
  151. break;
  152. }
  153. case kWhatShutdown:
  154. {
  155. onShutdown(true);
  156. break;
  157. }
  158. default:
  159. TRESPASS();
  160. break;
  161. }
  162. }
  163. void NuPlayer2::DecoderBase::handleError(int32_t err)
  164. {
  165. // We cannot immediately release the codec due to buffers still outstanding
  166. // in the renderer. We signal to the player the error so it can shutdown/release the
  167. // decoder after flushing and increment the generation to discard unnecessary messages.
  168. ++mBufferGeneration;
  169. sp<AMessage> notify = mNotify->dup();
  170. notify->setInt32("what", kWhatError);
  171. notify->setInt32("err", err);
  172. notify->post();
  173. }
  174. } // namespace android