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> |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 18 | #include <media/MediaTrackTranscoderCallback.h> |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 19 | |
| 20 | #include <condition_variable> |
| 21 | #include <memory> |
| 22 | #include <mutex> |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | // |
| 27 | // This file contains test utilities used by more than one track transcoder test. |
| 28 | // |
| 29 | |
| 30 | class TrackTranscoderTestUtils { |
| 31 | public: |
| 32 | static std::shared_ptr<AMediaFormat> getDefaultVideoDestinationFormat( |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 33 | AMediaFormat* sourceFormat, bool includeBitrate = true) { |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 34 | // Default video destination format setup. |
| 35 | static constexpr float kFrameRate = 30.0f; |
| 36 | static constexpr float kIFrameInterval = 30.0f; |
| 37 | static constexpr int32_t kBitRate = 2 * 1000 * 1000; |
| 38 | static constexpr int32_t kColorFormatSurface = 0x7f000789; |
| 39 | |
| 40 | AMediaFormat* destinationFormat = AMediaFormat_new(); |
| 41 | AMediaFormat_copy(destinationFormat, sourceFormat); |
| 42 | AMediaFormat_setFloat(destinationFormat, AMEDIAFORMAT_KEY_FRAME_RATE, kFrameRate); |
| 43 | AMediaFormat_setFloat(destinationFormat, AMEDIAFORMAT_KEY_I_FRAME_INTERVAL, |
| 44 | kIFrameInterval); |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 45 | if (includeBitrate) { |
| 46 | AMediaFormat_setInt32(destinationFormat, AMEDIAFORMAT_KEY_BIT_RATE, kBitRate); |
| 47 | } |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 48 | AMediaFormat_setInt32(destinationFormat, AMEDIAFORMAT_KEY_COLOR_FORMAT, |
| 49 | kColorFormatSurface); |
| 50 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 51 | return std::shared_ptr<AMediaFormat>(destinationFormat, &AMediaFormat_delete); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 52 | } |
| 53 | }; |
| 54 | |
| 55 | class TestCallback : public MediaTrackTranscoderCallback { |
| 56 | public: |
| 57 | TestCallback() = default; |
| 58 | ~TestCallback() = default; |
| 59 | |
| 60 | // MediaTrackTranscoderCallback |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 61 | void onTrackFormatAvailable(const MediaTrackTranscoder* transcoder __unused) { |
| 62 | std::unique_lock<std::mutex> lock(mMutex); |
| 63 | mTrackFormatAvailable = true; |
| 64 | mTrackFormatAvailableCondition.notify_all(); |
| 65 | } |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 66 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 67 | void onTrackFinished(const MediaTrackTranscoder* transcoder __unused) { |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 68 | std::unique_lock<std::mutex> lock(mMutex); |
| 69 | mTranscodingFinished = true; |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 70 | mTranscodingFinishedCondition.notify_all(); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 73 | void onTrackError(const MediaTrackTranscoder* transcoder __unused, media_status_t status) { |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 74 | std::unique_lock<std::mutex> lock(mMutex); |
| 75 | mTranscodingFinished = true; |
| 76 | mStatus = status; |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 77 | mTranscodingFinishedCondition.notify_all(); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 78 | } |
| 79 | // ~MediaTrackTranscoderCallback |
| 80 | |
| 81 | media_status_t waitUntilFinished() { |
| 82 | std::unique_lock<std::mutex> lock(mMutex); |
| 83 | while (!mTranscodingFinished) { |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 84 | mTranscodingFinishedCondition.wait(lock); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 85 | } |
| 86 | return mStatus; |
| 87 | } |
| 88 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 89 | void waitUntilTrackFormatAvailable() { |
| 90 | std::unique_lock<std::mutex> lock(mMutex); |
| 91 | while (!mTrackFormatAvailable) { |
| 92 | mTrackFormatAvailableCondition.wait(lock); |
| 93 | } |
| 94 | } |
| 95 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 96 | private: |
| 97 | media_status_t mStatus = AMEDIA_OK; |
| 98 | std::mutex mMutex; |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 99 | std::condition_variable mTranscodingFinishedCondition; |
| 100 | std::condition_variable mTrackFormatAvailableCondition; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 101 | bool mTranscodingFinished = false; |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 102 | bool mTrackFormatAvailable = false; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | }; // namespace android |