| 1 |
/* |
| 2 |
* Copyright (c) 2010-2022 Belledonne Communications SARL. |
| 3 |
* |
| 4 |
* This file is part of mediastreamer2 |
| 5 |
* (see https://gitlab.linphone.org/BC/public/mediastreamer2). |
| 6 |
* |
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Affero General Public License as |
| 9 |
* published by the Free Software Foundation, either version 3 of the |
| 10 |
* License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Affero General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Affero General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#pragma once |
| 22 |
|
| 23 |
#include <list> |
| 24 |
#include <memory> |
| 25 |
|
| 26 |
#include <VideoToolbox/VTDecompressionSession.h> |
| 27 |
|
| 28 |
#include "mediastreamer2/mscommon.h" |
| 29 |
#include "mediastreamer2/msvideo.h" |
| 30 |
|
| 31 |
#include "h26x-utils.h" |
| 32 |
#include "videotoolbox-utils.h" |
| 33 |
|
| 34 |
#include "h26x-decoder.h" |
| 35 |
|
| 36 |
namespace mediastreamer { |
| 37 |
|
| 38 |
class VideoToolboxDecoder : public H26xDecoder { |
| 39 |
public: |
| 40 |
VideoToolboxDecoder(const std::string &mime); |
| 41 |
~VideoToolboxDecoder(); |
| 42 |
|
| 43 |
bool feed(MSQueue *encodedFrame, uint64_t timestamp) override; |
| 44 |
Status fetch(mblk_t *&frame) override; |
| 45 |
|
| 46 |
void waitForKeyFrame() override { |
| 47 |
_freeze = true; |
| 48 |
} |
| 49 |
|
| 50 |
private: |
| 51 |
class InvalidSessionError : public std::runtime_error { |
| 52 |
public: |
| 53 |
InvalidSessionError() : std::runtime_error(toString(kVTInvalidSessionErr)) { |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
class Frame { |
| 58 |
public: |
| 59 |
Frame(mblk_t *data = nullptr) : _data(data) { |
| 60 |
} |
| 61 |
Frame(const Frame &src) : _data(src._data ? dupmsg(src._data) : nullptr) { |
| 62 |
} |
| 63 |
~Frame() { |
| 64 |
if (_data) freemsg(_data); |
| 65 |
} |
| 66 |
mblk_t *getData() const { |
| 67 |
return _data ? dupmsg(_data) : nullptr; |
| 68 |
} |
| 69 |
|
| 70 |
private: |
| 71 |
mblk_t *_data = nullptr; |
| 72 |
}; |
| 73 |
|
| 74 |
void createDecoder(); |
| 75 |
void destroyDecoder(); |
| 76 |
void decodeFrame(MSQueue *encodedFrame, uint64_t timestamp); |
| 77 |
void formatDescFromSpsPps(); |
| 78 |
|
| 79 |
static void outputCb(void *decompressionOutputRefCon, |
| 80 |
void *sourceFrameRefCon, |
| 81 |
OSStatus status, |
| 82 |
VTDecodeInfoFlags infoFlags, |
| 83 |
CVImageBufferRef imageBuffer, |
| 84 |
CMTime presentationTimeStamp, |
| 85 |
CMTime presentationDuration); |
| 86 |
|
| 87 |
VTDecompressionSessionRef _session = nullptr; |
| 88 |
CMFormatDescriptionRef _formatDesc = nullptr; |
| 89 |
MSYuvBufAllocator *_pixbufAllocator = nullptr; |
| 90 |
std::list<Frame> _queue; |
| 91 |
std::mutex _mutex; |
| 92 |
std::unique_ptr<H26xParameterSetsStore> _psStore; |
| 93 |
std::unique_ptr<H26xNaluHeader> _naluHeader; |
| 94 |
bool _freeze = true; |
| 95 |
bool _destroying = false; |
| 96 |
static const size_t _naluSizeLength = 4; |
| 97 |
}; |
| 98 |
|
| 99 |
} // namespace mediastreamer |