blob: 32a1a0af4c4d7b107e991070a84e40527354287a [file] [log] [blame]
Linus Nilsson0da327a2020-01-31 16:22:18 -08001/*
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 Zhang308e91f2020-06-10 15:27:56 -070018#include <media/MediaTrackTranscoderCallback.h>
Linus Nilsson0da327a2020-01-31 16:22:18 -080019
20#include <condition_variable>
21#include <memory>
22#include <mutex>
23
24namespace android {
25
26//
27// This file contains test utilities used by more than one track transcoder test.
28//
29
30class TrackTranscoderTestUtils {
31public:
32 static std::shared_ptr<AMediaFormat> getDefaultVideoDestinationFormat(
33 AMediaFormat* sourceFormat) {
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);
45 AMediaFormat_setInt32(destinationFormat, AMEDIAFORMAT_KEY_BIT_RATE, kBitRate);
46 AMediaFormat_setInt32(destinationFormat, AMEDIAFORMAT_KEY_COLOR_FORMAT,
47 kColorFormatSurface);
48
49 return std::shared_ptr<AMediaFormat>(destinationFormat,
50 std::bind(AMediaFormat_delete, std::placeholders::_1));
51 }
52};
53
54class TestCallback : public MediaTrackTranscoderCallback {
55public:
56 TestCallback() = default;
57 ~TestCallback() = default;
58
59 // MediaTrackTranscoderCallback
Linus Nilssoncab39d82020-05-14 16:32:21 -070060 void onTrackFinished(const MediaTrackTranscoder* transcoder __unused) {
Linus Nilsson0da327a2020-01-31 16:22:18 -080061 std::unique_lock<std::mutex> lock(mMutex);
62 mTranscodingFinished = true;
63 mCv.notify_all();
64 }
65
Linus Nilssoncab39d82020-05-14 16:32:21 -070066 void onTrackError(const MediaTrackTranscoder* transcoder __unused, media_status_t status) {
Linus Nilsson0da327a2020-01-31 16:22:18 -080067 std::unique_lock<std::mutex> lock(mMutex);
68 mTranscodingFinished = true;
69 mStatus = status;
70 mCv.notify_all();
71 }
72 // ~MediaTrackTranscoderCallback
73
74 media_status_t waitUntilFinished() {
75 std::unique_lock<std::mutex> lock(mMutex);
76 while (!mTranscodingFinished) {
77 mCv.wait(lock);
78 }
79 return mStatus;
80 }
81
82private:
83 media_status_t mStatus = AMEDIA_OK;
84 std::mutex mMutex;
85 std::condition_variable mCv;
86 bool mTranscodingFinished = false;
87};
88
89}; // namespace android