blob: 54b42fee7b152740d53df20f2d9df4cd4036741e [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
Chong Zhanga2cc86b2020-06-17 16:56:49 -070060 void onTrackFormatAvailable(const MediaTrackTranscoder* transcoder __unused) {}
61
Linus Nilssoncab39d82020-05-14 16:32:21 -070062 void onTrackFinished(const MediaTrackTranscoder* transcoder __unused) {
Linus Nilsson0da327a2020-01-31 16:22:18 -080063 std::unique_lock<std::mutex> lock(mMutex);
64 mTranscodingFinished = true;
65 mCv.notify_all();
66 }
67
Linus Nilssoncab39d82020-05-14 16:32:21 -070068 void onTrackError(const MediaTrackTranscoder* transcoder __unused, media_status_t status) {
Linus Nilsson0da327a2020-01-31 16:22:18 -080069 std::unique_lock<std::mutex> lock(mMutex);
70 mTranscodingFinished = true;
71 mStatus = status;
72 mCv.notify_all();
73 }
74 // ~MediaTrackTranscoderCallback
75
76 media_status_t waitUntilFinished() {
77 std::unique_lock<std::mutex> lock(mMutex);
78 while (!mTranscodingFinished) {
79 mCv.wait(lock);
80 }
81 return mStatus;
82 }
83
84private:
85 media_status_t mStatus = AMEDIA_OK;
86 std::mutex mMutex;
87 std::condition_variable mCv;
88 bool mTranscodingFinished = false;
89};
90
91}; // namespace android