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; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 36 | static constexpr int32_t kBitRate = 2 * 1000 * 1000; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 37 | |
| 38 | AMediaFormat* destinationFormat = AMediaFormat_new(); |
| 39 | AMediaFormat_copy(destinationFormat, sourceFormat); |
| 40 | AMediaFormat_setFloat(destinationFormat, AMEDIAFORMAT_KEY_FRAME_RATE, kFrameRate); |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 41 | if (includeBitrate) { |
| 42 | AMediaFormat_setInt32(destinationFormat, AMEDIAFORMAT_KEY_BIT_RATE, kBitRate); |
| 43 | } |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 44 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 45 | return std::shared_ptr<AMediaFormat>(destinationFormat, &AMediaFormat_delete); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 46 | } |
| 47 | }; |
| 48 | |
| 49 | class TestCallback : public MediaTrackTranscoderCallback { |
| 50 | public: |
| 51 | TestCallback() = default; |
| 52 | ~TestCallback() = default; |
| 53 | |
| 54 | // MediaTrackTranscoderCallback |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 55 | void onTrackFormatAvailable(const MediaTrackTranscoder* transcoder __unused) { |
| 56 | std::unique_lock<std::mutex> lock(mMutex); |
| 57 | mTrackFormatAvailable = true; |
| 58 | mTrackFormatAvailableCondition.notify_all(); |
| 59 | } |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 60 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 61 | void onTrackFinished(const MediaTrackTranscoder* transcoder __unused) { |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 62 | std::unique_lock<std::mutex> lock(mMutex); |
| 63 | mTranscodingFinished = true; |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 64 | mTranscodingFinishedCondition.notify_all(); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 67 | virtual void onTrackStopped(const MediaTrackTranscoder* transcoder __unused) override { |
| 68 | std::unique_lock<std::mutex> lock(mMutex); |
| 69 | mTranscodingFinished = true; |
| 70 | mTranscodingStopped = true; |
| 71 | mTranscodingFinishedCondition.notify_all(); |
| 72 | } |
| 73 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 74 | void onTrackError(const MediaTrackTranscoder* transcoder __unused, media_status_t status) { |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 75 | std::unique_lock<std::mutex> lock(mMutex); |
| 76 | mTranscodingFinished = true; |
| 77 | mStatus = status; |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 78 | mTranscodingFinishedCondition.notify_all(); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 79 | } |
| 80 | // ~MediaTrackTranscoderCallback |
| 81 | |
| 82 | media_status_t waitUntilFinished() { |
| 83 | std::unique_lock<std::mutex> lock(mMutex); |
| 84 | while (!mTranscodingFinished) { |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 85 | mTranscodingFinishedCondition.wait(lock); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 86 | } |
| 87 | return mStatus; |
| 88 | } |
| 89 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 90 | void waitUntilTrackFormatAvailable() { |
| 91 | std::unique_lock<std::mutex> lock(mMutex); |
| 92 | while (!mTrackFormatAvailable) { |
| 93 | mTrackFormatAvailableCondition.wait(lock); |
| 94 | } |
| 95 | } |
| 96 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 97 | bool transcodingWasStopped() const { return mTranscodingFinished && mTranscodingStopped; } |
| 98 | bool transcodingFinished() const { |
| 99 | return mTranscodingFinished && !mTranscodingStopped && mStatus == AMEDIA_OK; |
| 100 | } |
| 101 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 102 | private: |
| 103 | media_status_t mStatus = AMEDIA_OK; |
| 104 | std::mutex mMutex; |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 105 | std::condition_variable mTranscodingFinishedCondition; |
| 106 | std::condition_variable mTrackFormatAvailableCondition; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 107 | bool mTranscodingFinished = false; |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 108 | bool mTranscodingStopped = false; |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 109 | bool mTrackFormatAvailable = false; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 112 | class OneShotSemaphore { |
| 113 | public: |
| 114 | void wait() { |
| 115 | std::unique_lock<std::mutex> lock(mMutex); |
| 116 | while (!mSignaled) { |
| 117 | mCondition.wait(lock); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void signal() { |
| 122 | std::unique_lock<std::mutex> lock(mMutex); |
| 123 | mSignaled = true; |
| 124 | mCondition.notify_all(); |
| 125 | } |
| 126 | |
| 127 | private: |
| 128 | std::mutex mMutex; |
| 129 | std::condition_variable mCondition; |
| 130 | bool mSignaled = false; |
| 131 | }; |
| 132 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 133 | }; // namespace android |