1 // Copyright 2020 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 #ifndef COMPUTEPIPE_RUNNER_INPUT_MANAGER_VIDEODECODER_H_ 15 #define COMPUTEPIPE_RUNNER_INPUT_MANAGER_VIDEODECODER_H_ 16 17 #include <media/NdkMediaExtractor.h> 18 19 #include <atomic> 20 #include <memory> 21 #include <queue> 22 #include <string> 23 #include <thread> 24 25 #include "InputManager.h" 26 #include "types/Status.h" 27 28 namespace android { 29 namespace automotive { 30 namespace computepipe { 31 namespace runner { 32 namespace input_manager { 33 34 class VideoDecoder { 35 public: 36 explicit VideoDecoder(const proto::InputStreamConfig& config, 37 std::shared_ptr<InputEngineInterface> engineInterface); 38 39 ~VideoDecoder(); 40 /** 41 * Get the platback frame rate (frame rate at source * speed multiplier). 42 */ 43 float getPlaybackFrameRate(); 44 /** 45 * Set the timestamp in micros since epoch, at which first frame must be generated. 46 * All subsequest frames must be generated at timestampMicros + 47 * (fraeIndex / framesPerMicrosecond). This ensures that frames generated by multiple 48 * VideoDecoder follow same timestamp when they have same initial timestamp and frame rate. 49 */ 50 void setInitialTimestamp(int64_t timestampMicros); 51 52 /** 53 * Starts video decoding. 54 */ 55 Status startDecoding(); 56 /** 57 * Blocking call to stop video decoding and release all resources. 58 */ 59 void stopDecoding(); 60 61 private: 62 Status initializeMediaExtractor(); 63 Status initializeMediaDecoder(); 64 void releaseResources(); 65 void sendEosFlag(); 66 67 void addFramesToCodec(); 68 void popFramesFromCodec(); 69 bool readDecodedFrame(int64_t frameTimeMicros); 70 71 void decoderThreadFunction(); 72 73 std::shared_ptr<InputEngineInterface> mEngine; 74 proto::InputStreamConfig mConfig; 75 std::string mVideoPath; 76 int64_t mStartTimeMicros; 77 78 // Thread to perform video decoding. 79 std::unique_ptr<std::thread> mDecoderThead; 80 81 std::atomic<bool> mStopThread = false; 82 83 // Media decoder resources - Owned by mDecoderThead when thread is running. 84 AMediaExtractor* mExtractor = nullptr; 85 AMediaCodec* mCodec = nullptr; 86 int mFd; 87 int mIsFdOpen = false; 88 bool mExtractorFinished = false; 89 90 // Count of buffers queued to decoder. 91 int mCountQueuedBuffers = 0; 92 // Stores pair of decoded buffer ix, and buffer info. 93 std::queue<std::pair<int, AMediaCodecBufferInfo>> mDecodedBuffers; 94 95 float mPlaybackFrameRate = 0; 96 int mLoopbackCount = 1; 97 }; 98 99 } // namespace input_manager 100 } // namespace runner 101 } // namespace computepipe 102 } // namespace automotive 103 } // namespace android 104 105 #endif // COMPUTEPIPE_RUNNER_INPUT_MANAGER_VIDEODECODER_H_ 106