Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | #include <media/MediaTrackTranscoder.h> |
| 18 | |
| 19 | #include <condition_variable> |
| 20 | #include <memory> |
| 21 | #include <mutex> |
| 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | // |
| 26 | // This file contains test utilities used by more than one track transcoder test. |
| 27 | // |
| 28 | |
| 29 | class TrackTranscoderTestUtils { |
| 30 | public: |
| 31 | static std::shared_ptr<AMediaFormat> getDefaultVideoDestinationFormat( |
| 32 | AMediaFormat* sourceFormat) { |
| 33 | // Default video destination format setup. |
| 34 | static constexpr float kFrameRate = 30.0f; |
| 35 | static constexpr float kIFrameInterval = 30.0f; |
| 36 | static constexpr int32_t kBitRate = 2 * 1000 * 1000; |
| 37 | static constexpr int32_t kColorFormatSurface = 0x7f000789; |
| 38 | |
| 39 | AMediaFormat* destinationFormat = AMediaFormat_new(); |
| 40 | AMediaFormat_copy(destinationFormat, sourceFormat); |
| 41 | AMediaFormat_setFloat(destinationFormat, AMEDIAFORMAT_KEY_FRAME_RATE, kFrameRate); |
| 42 | AMediaFormat_setFloat(destinationFormat, AMEDIAFORMAT_KEY_I_FRAME_INTERVAL, |
| 43 | kIFrameInterval); |
| 44 | AMediaFormat_setInt32(destinationFormat, AMEDIAFORMAT_KEY_BIT_RATE, kBitRate); |
| 45 | AMediaFormat_setInt32(destinationFormat, AMEDIAFORMAT_KEY_COLOR_FORMAT, |
| 46 | kColorFormatSurface); |
| 47 | |
| 48 | return std::shared_ptr<AMediaFormat>(destinationFormat, |
| 49 | std::bind(AMediaFormat_delete, std::placeholders::_1)); |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | class TestCallback : public MediaTrackTranscoderCallback { |
| 54 | public: |
| 55 | TestCallback() = default; |
| 56 | ~TestCallback() = default; |
| 57 | |
| 58 | // MediaTrackTranscoderCallback |
| 59 | void onTrackFinished(MediaTrackTranscoder* transcoder __unused) { |
| 60 | std::unique_lock<std::mutex> lock(mMutex); |
| 61 | mTranscodingFinished = true; |
| 62 | mCv.notify_all(); |
| 63 | } |
| 64 | |
| 65 | void onTrackError(MediaTrackTranscoder* transcoder __unused, media_status_t status) { |
| 66 | std::unique_lock<std::mutex> lock(mMutex); |
| 67 | mTranscodingFinished = true; |
| 68 | mStatus = status; |
| 69 | mCv.notify_all(); |
| 70 | } |
| 71 | // ~MediaTrackTranscoderCallback |
| 72 | |
| 73 | media_status_t waitUntilFinished() { |
| 74 | std::unique_lock<std::mutex> lock(mMutex); |
| 75 | while (!mTranscodingFinished) { |
| 76 | mCv.wait(lock); |
| 77 | } |
| 78 | return mStatus; |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | media_status_t mStatus = AMEDIA_OK; |
| 83 | std::mutex mMutex; |
| 84 | std::condition_variable mCv; |
| 85 | bool mTranscodingFinished = false; |
| 86 | }; |
| 87 | |
| 88 | }; // namespace android |