nsresult PostOutput(BufferInfo::Param aInfo, MediaFormat::Param aFormat, const TimeUnit& aDuration) override { RefPtr<layers::Image> img = new SurfaceTextureImage(mSurfaceTexture.get(), mConfig.mDisplay, gl::OriginPos::BottomLeft); nsresult rv; int32_t flags; NS_ENSURE_SUCCESS(rv = aInfo->Flags(&flags), rv); bool isSync = !!(flags & MediaCodec::BUFFER_FLAG_SYNC_FRAME); int32_t offset; NS_ENSURE_SUCCESS(rv = aInfo->Offset(&offset), rv); int64_t presentationTimeUs; NS_ENSURE_SUCCESS(rv = aInfo->PresentationTimeUs(&presentationTimeUs), rv); RefPtr<VideoData> v = VideoData::CreateFromImage(mConfig, mImageContainer, offset, presentationTimeUs, aDuration.ToMicroseconds(), img, isSync, presentationTimeUs, gfx::IntRect(0, 0, mConfig.mDisplay.width, mConfig.mDisplay.height)); INVOKE_CALLBACK(Output, v); return NS_OK; }
nsresult MediaCodecDataDecoder::InitDecoder(Surface::Param aSurface) { mDecoder = CreateDecoder(mMimeType); if (!mDecoder) { INVOKE_CALLBACK(Error, MediaResult(NS_ERROR_DOM_MEDIA_FATAL_ERR, __func__)); return NS_ERROR_FAILURE; } // Check if the video codec supports adaptive playback or not. if (aSurface && java::HardwareCodecCapabilityUtils::CheckSupportsAdaptivePlayback( mDecoder, nsCString(TranslateMimeType(mMimeType)))) { // TODO: may need to find a way to not use hard code to decide the max w/h. mFormat->SetInteger(MediaFormat::KEY_MAX_WIDTH, 1920); mFormat->SetInteger(MediaFormat::KEY_MAX_HEIGHT, 1080); } MediaCrypto::LocalRef crypto = MediaDrmProxy::GetMediaCrypto(mDrmStubId); bool hascrypto = !!crypto; LOG("Has(%d) MediaCrypto (%s)", hascrypto, NS_ConvertUTF16toUTF8(mDrmStubId).get()); nsresult rv; NS_ENSURE_SUCCESS(rv = mDecoder->Configure(mFormat, aSurface, crypto, 0), rv); NS_ENSURE_SUCCESS(rv = mDecoder->Start(), rv); NS_ENSURE_SUCCESS(rv = ResetInputBuffers(), rv); NS_ENSURE_SUCCESS(rv = ResetOutputBuffers(), rv); nsCOMPtr<nsIRunnable> r = NewRunnableMethod(this, &MediaCodecDataDecoder::DecoderLoop); rv = NS_NewNamedThread("MC Decoder", getter_AddRefs(mThread), r); return rv; }
bool MediaCodecDataDecoder::WaitForInput() { MonitorAutoLock lock(mMonitor); while (State() == kDecoding && mQueue.empty()) { // Signal that we require more input. INVOKE_CALLBACK(InputExhausted); lock.Wait(); } return State() != kStopping; }
nsresult Output(BufferInfo::Param aInfo, void* aBuffer, MediaFormat::Param aFormat, const TimeUnit& aDuration) { // The output on Android is always 16-bit signed nsresult rv; int32_t numChannels; NS_ENSURE_SUCCESS(rv = aFormat->GetInteger(NS_LITERAL_STRING("channel-count"), &numChannels), rv); AudioConfig::ChannelLayout layout(numChannels); if (!layout.IsValid()) { return NS_ERROR_FAILURE; } int32_t sampleRate; NS_ENSURE_SUCCESS(rv = aFormat->GetInteger(NS_LITERAL_STRING("sample-rate"), &sampleRate), rv); int32_t size; NS_ENSURE_SUCCESS(rv = aInfo->Size(&size), rv); int32_t offset; NS_ENSURE_SUCCESS(rv = aInfo->Offset(&offset), rv); #ifdef MOZ_SAMPLE_TYPE_S16 const int32_t numSamples = size / 2; #else #error We only support 16-bit integer PCM #endif const int32_t numFrames = numSamples / numChannels; AlignedAudioBuffer audio(numSamples); if (!audio) { return NS_ERROR_OUT_OF_MEMORY; } const uint8_t* bufferStart = static_cast<uint8_t*>(aBuffer) + offset; PodCopy(audio.get(), reinterpret_cast<const AudioDataValue*>(bufferStart), numSamples); int64_t presentationTimeUs; NS_ENSURE_SUCCESS(rv = aInfo->PresentationTimeUs(&presentationTimeUs), rv); RefPtr<AudioData> data = new AudioData(0, presentationTimeUs, aDuration.ToMicroseconds(), numFrames, Move(audio), numChannels, sampleRate); INVOKE_CALLBACK(Output, data); return NS_OK; }
void MediaCodecDataDecoder::HandleEOS(int32_t aOutputStatus) { MonitorAutoLock lock(mMonitor); if (State() == kDrainWaitEOS) { State(kDecoding); mMonitor.Notify(); INVOKE_CALLBACK(DrainComplete); } mDecoder->ReleaseOutputBuffer(aOutputStatus, false); }
static void H_delete(mc_PIPELINE *pipeline, mc_PACKET *packet, packet_info *response, lcb_error_t immerr) { lcb_t root = pipeline->parent->instance; lcb_error_t rc; lcb_remove_resp_t resp; resp.version = 0; SET_RESP_CAS(&resp, 0, PACKET_CAS(response)); MK_RESPKEY(&resp, 0, packet); MK_ERROR(root, rc, response, immerr); INVOKE_CALLBACK(packet, root->callbacks.remove, (root, MCREQ_PKT_COOKIE(packet), rc, &resp)); }
static void H_arithmetic(mc_PIPELINE *pipeline, mc_PACKET *request, packet_info *response, lcb_error_t immerr) { lcb_t root = pipeline->parent->instance; lcb_error_t rc; lcb_arithmetic_resp_t resp; MK_ERROR(root, rc, response, immerr); if (rc == LCB_SUCCESS) { memcpy(&resp.v.v0.value, response->payload, sizeof(resp.v.v0.value)); resp.v.v0.value = ntohll(resp.v.v0.value); } MK_RESPKEY(&resp, 0, request); SET_RESP_CAS(&resp, 0, PACKET_CAS(response)); INVOKE_CALLBACK(request, root->callbacks.arithmetic, (root, MCREQ_PKT_COOKIE(request), rc, &resp)); }
nsresult MediaCodecDataDecoder::InitDecoder(Surface::Param aSurface) { mDecoder = CreateDecoder(mMimeType); if (!mDecoder) { INVOKE_CALLBACK(Error); return NS_ERROR_FAILURE; } nsresult rv; NS_ENSURE_SUCCESS(rv = mDecoder->Configure(mFormat, aSurface, nullptr, 0), rv); NS_ENSURE_SUCCESS(rv = mDecoder->Start(), rv); NS_ENSURE_SUCCESS(rv = ResetInputBuffers(), rv); NS_ENSURE_SUCCESS(rv = ResetOutputBuffers(), rv); NS_NewNamedThread("MC Decoder", getter_AddRefs(mThread), NS_NewRunnableMethod(this, &MediaCodecDataDecoder::DecoderLoop)); return NS_OK; }
static void H_store(mc_PIPELINE *pipeline, mc_PACKET *request, packet_info *response, lcb_error_t immerr) { lcb_t root = pipeline->parent->instance; lcb_storage_t op; lcb_error_t rc; lcb_store_resp_t resp; MK_ERROR(root, rc, response, immerr); switch (PACKET_OPCODE(response)) { case PROTOCOL_BINARY_CMD_ADD: op = LCB_ADD; break; case PROTOCOL_BINARY_CMD_REPLACE: op = LCB_REPLACE; break; case PROTOCOL_BINARY_CMD_SET: op = LCB_SET; break; case PROTOCOL_BINARY_CMD_APPEND: op = LCB_APPEND; break; case PROTOCOL_BINARY_CMD_PREPEND: op = LCB_PREPEND; break; default: abort(); break; } resp.version = 0; MK_RESPKEY(&resp, 0, request); SET_RESP_CAS(&resp, 0, PACKET_CAS(response)); INVOKE_CALLBACK(request, root->callbacks.store, (root, MCREQ_PKT_COOKIE(request), op, rc, &resp)); }
static void H_get(mc_PIPELINE *pipeline, mc_PACKET *request, packet_info *response, lcb_error_t immerr) { lcb_error_t rc; lcb_t o; lcb_get_resp_t resp; void *freeptr = NULL; o = pipeline->parent->instance; MK_RESPKEY(&resp, 0, request); MK_ERROR(o, rc, response, immerr); resp.version = 0; if (rc == LCB_SUCCESS) { const protocol_binary_response_getq *getq = PACKET_EPHEMERAL_START(response); resp.v.v0.cas = PACKET_CAS(response); resp.v.v0.datatype = PACKET_DATATYPE(response); resp.v.v0.flags = ntohl(getq->message.body.flags); resp.v.v0.bytes = PACKET_VALUE(response); resp.v.v0.nbytes = PACKET_NVALUE(response); rc = LCB_SUCCESS; } else { resp.v.v0.cas = 0; resp.v.v0.nbytes = 0; resp.v.v0.bytes = NULL; resp.v.v0.flags = 0; } maybe_decompress(o, response, &resp, &freeptr); INVOKE_CALLBACK(request, o->callbacks.get, (o, MCREQ_PKT_COOKIE(request), rc, &resp)); free(freeptr); }
void MediaCodecDataDecoder::DecoderLoop() { bool isOutputDone = false; AutoLocalJNIFrame frame(jni::GetEnvForThread(), 1); MediaFormat::LocalRef outputFormat(frame.GetEnv()); nsresult res = NS_OK; while (WaitForInput()) { RefPtr<MediaRawData> sample = PeekNextSample(); { MonitorAutoLock lock(mMonitor); if (State() == kDrainDecoder) { MOZ_ASSERT(!sample, "Shouldn't have a sample when pushing EOF frame"); res = QueueEOS(); BREAK_ON_DECODER_ERROR(); } } if (sample) { res = QueueSample(sample); if (NS_SUCCEEDED(res)) { // We've fed this into the decoder, so remove it from the queue. MonitorAutoLock lock(mMonitor); MOZ_RELEASE_ASSERT(mQueue.size(), "Queue may not be empty"); mQueue.pop_front(); isOutputDone = false; } } if (isOutputDone) { continue; } BufferInfo::LocalRef bufferInfo; nsresult res = BufferInfo::New(&bufferInfo); BREAK_ON_DECODER_ERROR(); int32_t outputStatus = -1; res = mDecoder->DequeueOutputBuffer(bufferInfo, kDecoderTimeout, &outputStatus); BREAK_ON_DECODER_ERROR(); if (outputStatus == MediaCodec::INFO_TRY_AGAIN_LATER) { // We might want to call mCallback->InputExhausted() here, but there seems // to be some possible bad interactions here with the threading. } else if (outputStatus == MediaCodec::INFO_OUTPUT_BUFFERS_CHANGED) { res = ResetOutputBuffers(); BREAK_ON_DECODER_ERROR(); } else if (outputStatus == MediaCodec::INFO_OUTPUT_FORMAT_CHANGED) { res = mDecoder->GetOutputFormat(ReturnTo(&outputFormat)); BREAK_ON_DECODER_ERROR(); } else if (outputStatus < 0) { NS_WARNING("Unknown error from decoder!"); INVOKE_CALLBACK(Error); // Don't break here just in case it's recoverable. If it's not, other // stuff will fail later and we'll bail out. } else { // We have a valid buffer index >= 0 here. int32_t flags; nsresult res = bufferInfo->Flags(&flags); BREAK_ON_DECODER_ERROR(); if (flags & MediaCodec::BUFFER_FLAG_END_OF_STREAM) { HandleEOS(outputStatus); isOutputDone = true; // We only queue empty EOF frames, so we're done for now. continue; } res = ProcessOutput(bufferInfo, outputFormat, outputStatus); BREAK_ON_DECODER_ERROR(); } } Cleanup(); // We're done. MonitorAutoLock lock(mMonitor); State(kShutdown); mMonitor.Notify(); }