blob: b485826010857f73f1941bcf90485e5794aa1e83 [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// #define LOG_NDEBUG 0
18#define LOG_TAG "MediaTrackTranscoder"
19
20#include <android-base/logging.h>
21#include <media/MediaTrackTranscoder.h>
Chong Zhang308e91f2020-06-10 15:27:56 -070022#include <media/MediaTrackTranscoderCallback.h>
Linus Nilsson0da327a2020-01-31 16:22:18 -080023
24namespace android {
25
26media_status_t MediaTrackTranscoder::configure(
27 const std::shared_ptr<MediaSampleReader>& mediaSampleReader, int trackIndex,
28 const std::shared_ptr<AMediaFormat>& destinationFormat) {
29 std::scoped_lock lock{mStateMutex};
30
31 if (mState != UNINITIALIZED) {
32 LOG(ERROR) << "Configure can only be called once";
33 return AMEDIA_ERROR_UNSUPPORTED;
34 }
35
36 if (mediaSampleReader == nullptr) {
37 LOG(ERROR) << "MediaSampleReader is null";
38 return AMEDIA_ERROR_INVALID_PARAMETER;
39 }
40 if (trackIndex < 0 || trackIndex >= mediaSampleReader->getTrackCount()) {
41 LOG(ERROR) << "TrackIndex is invalid " << trackIndex;
42 return AMEDIA_ERROR_INVALID_PARAMETER;
43 }
44
45 mMediaSampleReader = mediaSampleReader;
46 mTrackIndex = trackIndex;
47
48 mSourceFormat =
49 std::shared_ptr<AMediaFormat>(mMediaSampleReader->getTrackFormat(mTrackIndex),
50 std::bind(AMediaFormat_delete, std::placeholders::_1));
51 if (mSourceFormat == nullptr) {
52 LOG(ERROR) << "Unable to get format for track #" << mTrackIndex;
53 return AMEDIA_ERROR_MALFORMED;
54 }
55
56 media_status_t status = configureDestinationFormat(destinationFormat);
57 if (status != AMEDIA_OK) {
58 LOG(ERROR) << "configure failed with error " << status;
59 return status;
60 }
61
62 mState = CONFIGURED;
63 return AMEDIA_OK;
64}
65
66bool MediaTrackTranscoder::start() {
67 std::scoped_lock lock{mStateMutex};
68
69 if (mState != CONFIGURED) {
70 LOG(ERROR) << "TrackTranscoder must be configured before started";
71 return false;
72 }
73
74 mTranscodingThread = std::thread([this] {
75 media_status_t status = runTranscodeLoop();
76
77 // Notify the client.
78 if (auto callbacks = mTranscoderCallback.lock()) {
79 if (status != AMEDIA_OK) {
80 callbacks->onTrackError(this, status);
81 } else {
82 callbacks->onTrackFinished(this);
83 }
84 }
85 });
86
87 mState = STARTED;
88 return true;
89}
90
91bool MediaTrackTranscoder::stop() {
92 std::scoped_lock lock{mStateMutex};
93
94 if (mState == STARTED) {
95 abortTranscodeLoop();
96 mTranscodingThread.join();
97 mState = STOPPED;
98 return true;
99 }
100
101 LOG(ERROR) << "TrackTranscoder must be started before stopped";
102 return false;
103}
104
Chong Zhanga2cc86b2020-06-17 16:56:49 -0700105void MediaTrackTranscoder::notifyTrackFormatAvailable() {
106 if (auto callbacks = mTranscoderCallback.lock()) {
107 callbacks->onTrackFormatAvailable(this);
108 }
109}
110
Linus Nilssoncab39d82020-05-14 16:32:21 -0700111std::shared_ptr<MediaSampleQueue> MediaTrackTranscoder::getOutputQueue() const {
112 return mOutputQueue;
113}
114
115} // namespace android