blob: 92ce60ac60b3131e5095dc6220973558408ad165 [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
Linus Nilsson800793f2020-07-31 16:16:38 -070048 mSourceFormat = std::shared_ptr<AMediaFormat>(mMediaSampleReader->getTrackFormat(mTrackIndex),
49 &AMediaFormat_delete);
Linus Nilsson0da327a2020-01-31 16:22:18 -080050 if (mSourceFormat == nullptr) {
51 LOG(ERROR) << "Unable to get format for track #" << mTrackIndex;
52 return AMEDIA_ERROR_MALFORMED;
53 }
54
55 media_status_t status = configureDestinationFormat(destinationFormat);
56 if (status != AMEDIA_OK) {
57 LOG(ERROR) << "configure failed with error " << status;
58 return status;
59 }
60
61 mState = CONFIGURED;
62 return AMEDIA_OK;
63}
64
65bool MediaTrackTranscoder::start() {
66 std::scoped_lock lock{mStateMutex};
67
68 if (mState != CONFIGURED) {
69 LOG(ERROR) << "TrackTranscoder must be configured before started";
70 return false;
71 }
72
73 mTranscodingThread = std::thread([this] {
74 media_status_t status = runTranscodeLoop();
75
76 // Notify the client.
77 if (auto callbacks = mTranscoderCallback.lock()) {
78 if (status != AMEDIA_OK) {
79 callbacks->onTrackError(this, status);
80 } else {
81 callbacks->onTrackFinished(this);
82 }
83 }
84 });
85
86 mState = STARTED;
87 return true;
88}
89
90bool MediaTrackTranscoder::stop() {
91 std::scoped_lock lock{mStateMutex};
92
93 if (mState == STARTED) {
94 abortTranscodeLoop();
Linus Nilsson6233fed2020-08-13 15:15:14 -070095 mMediaSampleReader->setEnforceSequentialAccess(false);
Linus Nilsson0da327a2020-01-31 16:22:18 -080096 mTranscodingThread.join();
Linus Nilssone4716f22020-07-10 16:07:57 -070097 mOutputQueue->abort(); // Wake up any threads waiting for samples.
Linus Nilsson0da327a2020-01-31 16:22:18 -080098 mState = STOPPED;
99 return true;
100 }
101
102 LOG(ERROR) << "TrackTranscoder must be started before stopped";
103 return false;
104}
105
Chong Zhanga2cc86b2020-06-17 16:56:49 -0700106void MediaTrackTranscoder::notifyTrackFormatAvailable() {
107 if (auto callbacks = mTranscoderCallback.lock()) {
108 callbacks->onTrackFormatAvailable(this);
109 }
110}
111
Linus Nilssoncab39d82020-05-14 16:32:21 -0700112std::shared_ptr<MediaSampleQueue> MediaTrackTranscoder::getOutputQueue() const {
113 return mOutputQueue;
114}
115
116} // namespace android