Remove libwebm and recorder code
This commit is contained in:
parent
f30962b944
commit
b1f9ce2dbd
@ -32,12 +32,8 @@ addons:
|
||||
- libjpeg-dev
|
||||
- libogg-dev
|
||||
- libopenal-dev
|
||||
- libpulse-dev
|
||||
- libpng-dev
|
||||
- libturbojpeg
|
||||
- libvorbis-dev
|
||||
- libvorbisenc2
|
||||
- libvpx-dev
|
||||
- libxrandr-dev
|
||||
- mesa-common-dev
|
||||
- pkg-config
|
||||
@ -57,7 +53,7 @@ before_script:
|
||||
script:
|
||||
- mkdir "build"
|
||||
- cd "build"
|
||||
- cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DSERVER_ONLY=$SERVER_ONLY -DCHECK_ASSETS=off -DDISABLE_VPX=on
|
||||
- cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DSERVER_ONLY=$SERVER_ONLY -DCHECK_ASSETS=off -DBUILD_RECORDER=off
|
||||
- make VERBOSE=1 -j $THREADS
|
||||
|
||||
notifications:
|
||||
|
@ -24,10 +24,6 @@ option(ENABLE_NETWORK_MULTIPLAYER "Enable network multiplayer. This will replace
|
||||
|
||||
CMAKE_DEPENDENT_OPTION(BUILD_RECORDER "Build opengl recorder" ON
|
||||
"NOT SERVER_ONLY;NOT USE_GLES2;NOT APPLE" OFF)
|
||||
CMAKE_DEPENDENT_OPTION(BUILD_RECORDER_WITH_SOUND "Build opengl recorder with sound" ON
|
||||
BUILD_RECORDER OFF)
|
||||
CMAKE_DEPENDENT_OPTION(BUILD_PULSE_WO_DL "If pulseaudio in your distro / system is optional, turn this off to load pulse with libdl"
|
||||
ON "BUILD_RECORDER_WITH_SOUND;UNIX" OFF)
|
||||
|
||||
if (UNIX AND NOT APPLE)
|
||||
option(USE_GLES2 "Use OpenGL ES2 renderer" OFF)
|
||||
@ -88,12 +84,6 @@ endif()
|
||||
|
||||
if(BUILD_RECORDER)
|
||||
add_definitions(-DENABLE_RECORDER)
|
||||
if(BUILD_RECORDER_WITH_SOUND)
|
||||
add_definitions(-DENABLE_REC_SOUND)
|
||||
if(BUILD_PULSE_WO_DL)
|
||||
add_definitions(-DENABLE_PULSE_WO_DL)
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Build the Bullet physics library
|
||||
@ -145,22 +135,12 @@ else()
|
||||
endif()
|
||||
|
||||
if(BUILD_RECORDER)
|
||||
find_library(TURBOJPEG_LIBRARY NAMES turbojpeg libturbojpeg PATHS "${PROJECT_SOURCE_DIR}/dependencies/lib")
|
||||
mark_as_advanced(TURBOJPEG_LIBRARY)
|
||||
if(UNIX)
|
||||
include(FindPkgConfig)
|
||||
pkg_check_modules(VPX vpx)
|
||||
else()
|
||||
find_path(VPX_INCLUDEDIR NAMES vpx/vpx_codec.h PATHS "${PROJECT_SOURCE_DIR}/dependencies/include")
|
||||
find_library(VPX_LIBRARIES NAMES libvpx PATHS "${PROJECT_SOURCE_DIR}/dependencies/lib")
|
||||
endif()
|
||||
include_directories(${VPX_INCLUDEDIR})
|
||||
if(BUILD_RECORDER_WITH_SOUND AND UNIX)
|
||||
pkg_check_modules(PULSEAUDIO libpulse)
|
||||
include_directories(${PULSEAUDIO_INCLUDEDIR})
|
||||
endif()
|
||||
add_subdirectory("${PROJECT_SOURCE_DIR}/lib/libwebm")
|
||||
include_directories("${PROJECT_SOURCE_DIR}/lib/libwebm")
|
||||
find_library(OPENGLRECORDER_LIBRARY NAMES openglrecorder libopenglrecorder PATHS "${PROJECT_SOURCE_DIR}/dependencies/lib")
|
||||
find_path(OPENGLRECORDER_INCLUDEDIR NAMES openglrecorder.h PATHS "${PROJECT_SOURCE_DIR}/dependencies/include")
|
||||
find_library(OPENGLRECORDER_LIBRARY NAMES openglrecorder libopenglrecorder PATHS "${PROJECT_SOURCE_DIR}/dependencies/lib")
|
||||
find_path(OPENGLRECORDER_INCLUDEDIR NAMES openglrecorder.h PATHS "${PROJECT_SOURCE_DIR}/dependencies/include")
|
||||
include_directories(${OPENGLRECORDER_INCLUDEDIR})
|
||||
mark_as_advanced(OPENGLRECORDER_LIBRARY OPENGLRECORDER_INCLUDEDIR)
|
||||
endif()
|
||||
|
||||
if(NOT SERVER_ONLY AND NOT USE_GLES2)
|
||||
@ -447,14 +427,7 @@ if(UNIX AND NOT APPLE)
|
||||
endif()
|
||||
|
||||
if(BUILD_RECORDER)
|
||||
target_link_libraries(supertuxkart webm ${TURBOJPEG_LIBRARY} ${VPX_LIBRARIES})
|
||||
if(BUILD_RECORDER_WITH_SOUND AND UNIX)
|
||||
if(BUILD_PULSE_WO_DL)
|
||||
target_link_libraries(supertuxkart ${PULSEAUDIO_LIBRARIES})
|
||||
else()
|
||||
target_link_libraries(supertuxkart dl)
|
||||
endif()
|
||||
endif()
|
||||
target_link_libraries(supertuxkart ${OPENGLRECORDER_LIBRARY})
|
||||
endif()
|
||||
|
||||
# FreeBSD does not search in /usr/local/lib, but at least Freetype is installed there :(
|
||||
|
@ -1,10 +0,0 @@
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
if (UNIX OR MINGW)
|
||||
add_definitions(-std=gnu++0x -O3)
|
||||
endif()
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
add_library(webm STATIC
|
||||
mkvmuxer/mkvmuxer.cc
|
||||
mkvmuxer/mkvmuxerutil.cc
|
||||
mkvmuxer/mkvwriter.cc
|
||||
)
|
@ -1,30 +0,0 @@
|
||||
Copyright (c) 2010, Google Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
* Neither the name of Google nor the names of its contributors may
|
||||
be used to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
@ -1,192 +0,0 @@
|
||||
// Copyright (c) 2012 The WebM project authors. All Rights Reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
// that can be found in the LICENSE file in the root of the source
|
||||
// tree. An additional intellectual property rights grant can be found
|
||||
// in the file PATENTS. All contributing project authors may
|
||||
// be found in the AUTHORS file in the root of the source tree.
|
||||
|
||||
#ifndef COMMON_WEBMIDS_H_
|
||||
#define COMMON_WEBMIDS_H_
|
||||
|
||||
namespace libwebm {
|
||||
|
||||
enum MkvId {
|
||||
kMkvEBML = 0x1A45DFA3,
|
||||
kMkvEBMLVersion = 0x4286,
|
||||
kMkvEBMLReadVersion = 0x42F7,
|
||||
kMkvEBMLMaxIDLength = 0x42F2,
|
||||
kMkvEBMLMaxSizeLength = 0x42F3,
|
||||
kMkvDocType = 0x4282,
|
||||
kMkvDocTypeVersion = 0x4287,
|
||||
kMkvDocTypeReadVersion = 0x4285,
|
||||
kMkvVoid = 0xEC,
|
||||
kMkvSignatureSlot = 0x1B538667,
|
||||
kMkvSignatureAlgo = 0x7E8A,
|
||||
kMkvSignatureHash = 0x7E9A,
|
||||
kMkvSignaturePublicKey = 0x7EA5,
|
||||
kMkvSignature = 0x7EB5,
|
||||
kMkvSignatureElements = 0x7E5B,
|
||||
kMkvSignatureElementList = 0x7E7B,
|
||||
kMkvSignedElement = 0x6532,
|
||||
// segment
|
||||
kMkvSegment = 0x18538067,
|
||||
// Meta Seek Information
|
||||
kMkvSeekHead = 0x114D9B74,
|
||||
kMkvSeek = 0x4DBB,
|
||||
kMkvSeekID = 0x53AB,
|
||||
kMkvSeekPosition = 0x53AC,
|
||||
// Segment Information
|
||||
kMkvInfo = 0x1549A966,
|
||||
kMkvTimecodeScale = 0x2AD7B1,
|
||||
kMkvDuration = 0x4489,
|
||||
kMkvDateUTC = 0x4461,
|
||||
kMkvTitle = 0x7BA9,
|
||||
kMkvMuxingApp = 0x4D80,
|
||||
kMkvWritingApp = 0x5741,
|
||||
// Cluster
|
||||
kMkvCluster = 0x1F43B675,
|
||||
kMkvTimecode = 0xE7,
|
||||
kMkvPrevSize = 0xAB,
|
||||
kMkvBlockGroup = 0xA0,
|
||||
kMkvBlock = 0xA1,
|
||||
kMkvBlockDuration = 0x9B,
|
||||
kMkvReferenceBlock = 0xFB,
|
||||
kMkvLaceNumber = 0xCC,
|
||||
kMkvSimpleBlock = 0xA3,
|
||||
kMkvBlockAdditions = 0x75A1,
|
||||
kMkvBlockMore = 0xA6,
|
||||
kMkvBlockAddID = 0xEE,
|
||||
kMkvBlockAdditional = 0xA5,
|
||||
kMkvDiscardPadding = 0x75A2,
|
||||
// Track
|
||||
kMkvTracks = 0x1654AE6B,
|
||||
kMkvTrackEntry = 0xAE,
|
||||
kMkvTrackNumber = 0xD7,
|
||||
kMkvTrackUID = 0x73C5,
|
||||
kMkvTrackType = 0x83,
|
||||
kMkvFlagEnabled = 0xB9,
|
||||
kMkvFlagDefault = 0x88,
|
||||
kMkvFlagForced = 0x55AA,
|
||||
kMkvFlagLacing = 0x9C,
|
||||
kMkvDefaultDuration = 0x23E383,
|
||||
kMkvMaxBlockAdditionID = 0x55EE,
|
||||
kMkvName = 0x536E,
|
||||
kMkvLanguage = 0x22B59C,
|
||||
kMkvCodecID = 0x86,
|
||||
kMkvCodecPrivate = 0x63A2,
|
||||
kMkvCodecName = 0x258688,
|
||||
kMkvCodecDelay = 0x56AA,
|
||||
kMkvSeekPreRoll = 0x56BB,
|
||||
// video
|
||||
kMkvVideo = 0xE0,
|
||||
kMkvFlagInterlaced = 0x9A,
|
||||
kMkvStereoMode = 0x53B8,
|
||||
kMkvAlphaMode = 0x53C0,
|
||||
kMkvPixelWidth = 0xB0,
|
||||
kMkvPixelHeight = 0xBA,
|
||||
kMkvPixelCropBottom = 0x54AA,
|
||||
kMkvPixelCropTop = 0x54BB,
|
||||
kMkvPixelCropLeft = 0x54CC,
|
||||
kMkvPixelCropRight = 0x54DD,
|
||||
kMkvDisplayWidth = 0x54B0,
|
||||
kMkvDisplayHeight = 0x54BA,
|
||||
kMkvDisplayUnit = 0x54B2,
|
||||
kMkvAspectRatioType = 0x54B3,
|
||||
kMkvFrameRate = 0x2383E3,
|
||||
// end video
|
||||
// colour
|
||||
kMkvColour = 0x55B0,
|
||||
kMkvMatrixCoefficients = 0x55B1,
|
||||
kMkvBitsPerChannel = 0x55B2,
|
||||
kMkvChromaSubsamplingHorz = 0x55B3,
|
||||
kMkvChromaSubsamplingVert = 0x55B4,
|
||||
kMkvCbSubsamplingHorz = 0x55B5,
|
||||
kMkvCbSubsamplingVert = 0x55B6,
|
||||
kMkvChromaSitingHorz = 0x55B7,
|
||||
kMkvChromaSitingVert = 0x55B8,
|
||||
kMkvRange = 0x55B9,
|
||||
kMkvTransferCharacteristics = 0x55BA,
|
||||
kMkvPrimaries = 0x55BB,
|
||||
kMkvMaxCLL = 0x55BC,
|
||||
kMkvMaxFALL = 0x55BD,
|
||||
// mastering metadata
|
||||
kMkvMasteringMetadata = 0x55D0,
|
||||
kMkvPrimaryRChromaticityX = 0x55D1,
|
||||
kMkvPrimaryRChromaticityY = 0x55D2,
|
||||
kMkvPrimaryGChromaticityX = 0x55D3,
|
||||
kMkvPrimaryGChromaticityY = 0x55D4,
|
||||
kMkvPrimaryBChromaticityX = 0x55D5,
|
||||
kMkvPrimaryBChromaticityY = 0x55D6,
|
||||
kMkvWhitePointChromaticityX = 0x55D7,
|
||||
kMkvWhitePointChromaticityY = 0x55D8,
|
||||
kMkvLuminanceMax = 0x55D9,
|
||||
kMkvLuminanceMin = 0x55DA,
|
||||
// end mastering metadata
|
||||
// end colour
|
||||
// projection
|
||||
kMkvProjection = 0x7670,
|
||||
kMkvProjectionType = 0x7671,
|
||||
kMkvProjectionPrivate = 0x7672,
|
||||
kMkvProjectionPoseYaw = 0x7673,
|
||||
kMkvProjectionPosePitch = 0x7674,
|
||||
kMkvProjectionPoseRoll = 0x7675,
|
||||
// end projection
|
||||
// audio
|
||||
kMkvAudio = 0xE1,
|
||||
kMkvSamplingFrequency = 0xB5,
|
||||
kMkvOutputSamplingFrequency = 0x78B5,
|
||||
kMkvChannels = 0x9F,
|
||||
kMkvBitDepth = 0x6264,
|
||||
// end audio
|
||||
// ContentEncodings
|
||||
kMkvContentEncodings = 0x6D80,
|
||||
kMkvContentEncoding = 0x6240,
|
||||
kMkvContentEncodingOrder = 0x5031,
|
||||
kMkvContentEncodingScope = 0x5032,
|
||||
kMkvContentEncodingType = 0x5033,
|
||||
kMkvContentCompression = 0x5034,
|
||||
kMkvContentCompAlgo = 0x4254,
|
||||
kMkvContentCompSettings = 0x4255,
|
||||
kMkvContentEncryption = 0x5035,
|
||||
kMkvContentEncAlgo = 0x47E1,
|
||||
kMkvContentEncKeyID = 0x47E2,
|
||||
kMkvContentSignature = 0x47E3,
|
||||
kMkvContentSigKeyID = 0x47E4,
|
||||
kMkvContentSigAlgo = 0x47E5,
|
||||
kMkvContentSigHashAlgo = 0x47E6,
|
||||
kMkvContentEncAESSettings = 0x47E7,
|
||||
kMkvAESSettingsCipherMode = 0x47E8,
|
||||
kMkvAESSettingsCipherInitData = 0x47E9,
|
||||
// end ContentEncodings
|
||||
// Cueing Data
|
||||
kMkvCues = 0x1C53BB6B,
|
||||
kMkvCuePoint = 0xBB,
|
||||
kMkvCueTime = 0xB3,
|
||||
kMkvCueTrackPositions = 0xB7,
|
||||
kMkvCueTrack = 0xF7,
|
||||
kMkvCueClusterPosition = 0xF1,
|
||||
kMkvCueBlockNumber = 0x5378,
|
||||
// Chapters
|
||||
kMkvChapters = 0x1043A770,
|
||||
kMkvEditionEntry = 0x45B9,
|
||||
kMkvChapterAtom = 0xB6,
|
||||
kMkvChapterUID = 0x73C4,
|
||||
kMkvChapterStringUID = 0x5654,
|
||||
kMkvChapterTimeStart = 0x91,
|
||||
kMkvChapterTimeEnd = 0x92,
|
||||
kMkvChapterDisplay = 0x80,
|
||||
kMkvChapString = 0x85,
|
||||
kMkvChapLanguage = 0x437C,
|
||||
kMkvChapCountry = 0x437E,
|
||||
// Tags
|
||||
kMkvTags = 0x1254C367,
|
||||
kMkvTag = 0x7373,
|
||||
kMkvSimpleTag = 0x67C8,
|
||||
kMkvTagName = 0x45A3,
|
||||
kMkvTagString = 0x4487
|
||||
};
|
||||
|
||||
} // namespace libwebm
|
||||
|
||||
#endif // COMMON_WEBMIDS_H_
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,28 +0,0 @@
|
||||
// Copyright (c) 2012 The WebM project authors. All Rights Reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
// that can be found in the LICENSE file in the root of the source
|
||||
// tree. An additional intellectual property rights grant can be found
|
||||
// in the file PATENTS. All contributing project authors may
|
||||
// be found in the AUTHORS file in the root of the source tree.
|
||||
|
||||
#ifndef MKVMUXER_MKVMUXERTYPES_H_
|
||||
#define MKVMUXER_MKVMUXERTYPES_H_
|
||||
|
||||
namespace mkvmuxer {
|
||||
typedef unsigned char uint8;
|
||||
typedef short int16;
|
||||
typedef int int32;
|
||||
typedef unsigned int uint32;
|
||||
typedef long long int64;
|
||||
typedef unsigned long long uint64;
|
||||
} // namespace mkvmuxer
|
||||
|
||||
// Copied from Chromium basictypes.h
|
||||
// A macro to disallow the copy constructor and operator= functions
|
||||
// This should be used in the private: declarations for a class
|
||||
#define LIBWEBM_DISALLOW_COPY_AND_ASSIGN(TypeName) \
|
||||
TypeName(const TypeName&); \
|
||||
void operator=(const TypeName&)
|
||||
|
||||
#endif // MKVMUXER_MKVMUXERTYPES_HPP_
|
@ -1,743 +0,0 @@
|
||||
// Copyright (c) 2012 The WebM project authors. All Rights Reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
// that can be found in the LICENSE file in the root of the source
|
||||
// tree. An additional intellectual property rights grant can be found
|
||||
// in the file PATENTS. All contributing project authors may
|
||||
// be found in the AUTHORS file in the root of the source tree.
|
||||
|
||||
#include "mkvmuxer/mkvmuxerutil.h"
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
#include <new>
|
||||
|
||||
#include "common/webmids.h"
|
||||
#include "mkvmuxer/mkvmuxer.h"
|
||||
#include "mkvmuxer/mkvwriter.h"
|
||||
|
||||
namespace mkvmuxer {
|
||||
|
||||
namespace {
|
||||
|
||||
// Date elements are always 8 octets in size.
|
||||
const int kDateElementSize = 8;
|
||||
|
||||
uint64 WriteBlock(IMkvWriter* writer, const Frame* const frame, int64 timecode,
|
||||
uint64 timecode_scale) {
|
||||
uint64 block_additional_elem_size = 0;
|
||||
uint64 block_addid_elem_size = 0;
|
||||
uint64 block_more_payload_size = 0;
|
||||
uint64 block_more_elem_size = 0;
|
||||
uint64 block_additions_payload_size = 0;
|
||||
uint64 block_additions_elem_size = 0;
|
||||
if (frame->additional()) {
|
||||
block_additional_elem_size =
|
||||
EbmlElementSize(libwebm::kMkvBlockAdditional, frame->additional(),
|
||||
frame->additional_length());
|
||||
block_addid_elem_size = EbmlElementSize(
|
||||
libwebm::kMkvBlockAddID, static_cast<uint64>(frame->add_id()));
|
||||
|
||||
block_more_payload_size =
|
||||
block_addid_elem_size + block_additional_elem_size;
|
||||
block_more_elem_size =
|
||||
EbmlMasterElementSize(libwebm::kMkvBlockMore, block_more_payload_size) +
|
||||
block_more_payload_size;
|
||||
block_additions_payload_size = block_more_elem_size;
|
||||
block_additions_elem_size =
|
||||
EbmlMasterElementSize(libwebm::kMkvBlockAdditions,
|
||||
block_additions_payload_size) +
|
||||
block_additions_payload_size;
|
||||
}
|
||||
|
||||
uint64 discard_padding_elem_size = 0;
|
||||
if (frame->discard_padding() != 0) {
|
||||
discard_padding_elem_size =
|
||||
EbmlElementSize(libwebm::kMkvDiscardPadding,
|
||||
static_cast<int64>(frame->discard_padding()));
|
||||
}
|
||||
|
||||
const uint64 reference_block_timestamp =
|
||||
frame->reference_block_timestamp() / timecode_scale;
|
||||
uint64 reference_block_elem_size = 0;
|
||||
if (!frame->is_key()) {
|
||||
reference_block_elem_size =
|
||||
EbmlElementSize(libwebm::kMkvReferenceBlock, reference_block_timestamp);
|
||||
}
|
||||
|
||||
const uint64 duration = frame->duration() / timecode_scale;
|
||||
uint64 block_duration_elem_size = 0;
|
||||
if (duration > 0)
|
||||
block_duration_elem_size =
|
||||
EbmlElementSize(libwebm::kMkvBlockDuration, duration);
|
||||
|
||||
const uint64 block_payload_size = 4 + frame->length();
|
||||
const uint64 block_elem_size =
|
||||
EbmlMasterElementSize(libwebm::kMkvBlock, block_payload_size) +
|
||||
block_payload_size;
|
||||
|
||||
const uint64 block_group_payload_size =
|
||||
block_elem_size + block_additions_elem_size + block_duration_elem_size +
|
||||
discard_padding_elem_size + reference_block_elem_size;
|
||||
|
||||
if (!WriteEbmlMasterElement(writer, libwebm::kMkvBlockGroup,
|
||||
block_group_payload_size)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!WriteEbmlMasterElement(writer, libwebm::kMkvBlock, block_payload_size))
|
||||
return 0;
|
||||
|
||||
if (WriteUInt(writer, frame->track_number()))
|
||||
return 0;
|
||||
|
||||
if (SerializeInt(writer, timecode, 2))
|
||||
return 0;
|
||||
|
||||
// For a Block, flags is always 0.
|
||||
if (SerializeInt(writer, 0, 1))
|
||||
return 0;
|
||||
|
||||
if (writer->Write(frame->frame(), static_cast<uint32>(frame->length())))
|
||||
return 0;
|
||||
|
||||
if (frame->additional()) {
|
||||
if (!WriteEbmlMasterElement(writer, libwebm::kMkvBlockAdditions,
|
||||
block_additions_payload_size)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!WriteEbmlMasterElement(writer, libwebm::kMkvBlockMore,
|
||||
block_more_payload_size))
|
||||
return 0;
|
||||
|
||||
if (!WriteEbmlElement(writer, libwebm::kMkvBlockAddID,
|
||||
static_cast<uint64>(frame->add_id())))
|
||||
return 0;
|
||||
|
||||
if (!WriteEbmlElement(writer, libwebm::kMkvBlockAdditional,
|
||||
frame->additional(), frame->additional_length())) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (frame->discard_padding() != 0 &&
|
||||
!WriteEbmlElement(writer, libwebm::kMkvDiscardPadding,
|
||||
static_cast<int64>(frame->discard_padding()))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!frame->is_key() &&
|
||||
!WriteEbmlElement(writer, libwebm::kMkvReferenceBlock,
|
||||
reference_block_timestamp)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (duration > 0 &&
|
||||
!WriteEbmlElement(writer, libwebm::kMkvBlockDuration, duration)) {
|
||||
return false;
|
||||
}
|
||||
return EbmlMasterElementSize(libwebm::kMkvBlockGroup,
|
||||
block_group_payload_size) +
|
||||
block_group_payload_size;
|
||||
}
|
||||
|
||||
uint64 WriteSimpleBlock(IMkvWriter* writer, const Frame* const frame,
|
||||
int64 timecode) {
|
||||
if (WriteID(writer, libwebm::kMkvSimpleBlock))
|
||||
return 0;
|
||||
|
||||
const int32 size = static_cast<int32>(frame->length()) + 4;
|
||||
if (WriteUInt(writer, size))
|
||||
return 0;
|
||||
|
||||
if (WriteUInt(writer, static_cast<uint64>(frame->track_number())))
|
||||
return 0;
|
||||
|
||||
if (SerializeInt(writer, timecode, 2))
|
||||
return 0;
|
||||
|
||||
uint64 flags = 0;
|
||||
if (frame->is_key())
|
||||
flags |= 0x80;
|
||||
|
||||
if (SerializeInt(writer, flags, 1))
|
||||
return 0;
|
||||
|
||||
if (writer->Write(frame->frame(), static_cast<uint32>(frame->length())))
|
||||
return 0;
|
||||
|
||||
return GetUIntSize(libwebm::kMkvSimpleBlock) + GetCodedUIntSize(size) + 4 +
|
||||
frame->length();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int32 GetCodedUIntSize(uint64 value) {
|
||||
if (value < 0x000000000000007FULL)
|
||||
return 1;
|
||||
else if (value < 0x0000000000003FFFULL)
|
||||
return 2;
|
||||
else if (value < 0x00000000001FFFFFULL)
|
||||
return 3;
|
||||
else if (value < 0x000000000FFFFFFFULL)
|
||||
return 4;
|
||||
else if (value < 0x00000007FFFFFFFFULL)
|
||||
return 5;
|
||||
else if (value < 0x000003FFFFFFFFFFULL)
|
||||
return 6;
|
||||
else if (value < 0x0001FFFFFFFFFFFFULL)
|
||||
return 7;
|
||||
return 8;
|
||||
}
|
||||
|
||||
int32 GetUIntSize(uint64 value) {
|
||||
if (value < 0x0000000000000100ULL)
|
||||
return 1;
|
||||
else if (value < 0x0000000000010000ULL)
|
||||
return 2;
|
||||
else if (value < 0x0000000001000000ULL)
|
||||
return 3;
|
||||
else if (value < 0x0000000100000000ULL)
|
||||
return 4;
|
||||
else if (value < 0x0000010000000000ULL)
|
||||
return 5;
|
||||
else if (value < 0x0001000000000000ULL)
|
||||
return 6;
|
||||
else if (value < 0x0100000000000000ULL)
|
||||
return 7;
|
||||
return 8;
|
||||
}
|
||||
|
||||
int32 GetIntSize(int64 value) {
|
||||
// Doubling the requested value ensures positive values with their high bit
|
||||
// set are written with 0-padding to avoid flipping the signedness.
|
||||
const uint64 v = (value < 0) ? value ^ -1LL : value;
|
||||
return GetUIntSize(2 * v);
|
||||
}
|
||||
|
||||
uint64 EbmlMasterElementSize(uint64 type, uint64 value) {
|
||||
// Size of EBML ID
|
||||
int32 ebml_size = GetUIntSize(type);
|
||||
|
||||
// Datasize
|
||||
ebml_size += GetCodedUIntSize(value);
|
||||
|
||||
return ebml_size;
|
||||
}
|
||||
|
||||
uint64 EbmlElementSize(uint64 type, int64 value) {
|
||||
// Size of EBML ID
|
||||
int32 ebml_size = GetUIntSize(type);
|
||||
|
||||
// Datasize
|
||||
ebml_size += GetIntSize(value);
|
||||
|
||||
// Size of Datasize
|
||||
ebml_size++;
|
||||
|
||||
return ebml_size;
|
||||
}
|
||||
|
||||
uint64 EbmlElementSize(uint64 type, uint64 value) {
|
||||
return EbmlElementSize(type, value, 0);
|
||||
}
|
||||
|
||||
uint64 EbmlElementSize(uint64 type, uint64 value, uint64 fixed_size) {
|
||||
// Size of EBML ID
|
||||
uint64 ebml_size = GetUIntSize(type);
|
||||
|
||||
// Datasize
|
||||
ebml_size += (fixed_size > 0) ? fixed_size : GetUIntSize(value);
|
||||
|
||||
// Size of Datasize
|
||||
ebml_size++;
|
||||
|
||||
return ebml_size;
|
||||
}
|
||||
|
||||
uint64 EbmlElementSize(uint64 type, float /* value */) {
|
||||
// Size of EBML ID
|
||||
uint64 ebml_size = GetUIntSize(type);
|
||||
|
||||
// Datasize
|
||||
ebml_size += sizeof(float);
|
||||
|
||||
// Size of Datasize
|
||||
ebml_size++;
|
||||
|
||||
return ebml_size;
|
||||
}
|
||||
|
||||
uint64 EbmlElementSize(uint64 type, const char* value) {
|
||||
if (!value)
|
||||
return 0;
|
||||
|
||||
// Size of EBML ID
|
||||
uint64 ebml_size = GetUIntSize(type);
|
||||
|
||||
// Datasize
|
||||
ebml_size += strlen(value);
|
||||
|
||||
// Size of Datasize
|
||||
ebml_size += GetCodedUIntSize(strlen(value));
|
||||
|
||||
return ebml_size;
|
||||
}
|
||||
|
||||
uint64 EbmlElementSize(uint64 type, const uint8* value, uint64 size) {
|
||||
if (!value)
|
||||
return 0;
|
||||
|
||||
// Size of EBML ID
|
||||
uint64 ebml_size = GetUIntSize(type);
|
||||
|
||||
// Datasize
|
||||
ebml_size += size;
|
||||
|
||||
// Size of Datasize
|
||||
ebml_size += GetCodedUIntSize(size);
|
||||
|
||||
return ebml_size;
|
||||
}
|
||||
|
||||
uint64 EbmlDateElementSize(uint64 type) {
|
||||
// Size of EBML ID
|
||||
uint64 ebml_size = GetUIntSize(type);
|
||||
|
||||
// Datasize
|
||||
ebml_size += kDateElementSize;
|
||||
|
||||
// Size of Datasize
|
||||
ebml_size++;
|
||||
|
||||
return ebml_size;
|
||||
}
|
||||
|
||||
int32 SerializeInt(IMkvWriter* writer, int64 value, int32 size) {
|
||||
if (!writer || size < 1 || size > 8)
|
||||
return -1;
|
||||
|
||||
for (int32 i = 1; i <= size; ++i) {
|
||||
const int32 byte_count = size - i;
|
||||
const int32 bit_count = byte_count * 8;
|
||||
|
||||
const int64 bb = value >> bit_count;
|
||||
const uint8 b = static_cast<uint8>(bb);
|
||||
|
||||
const int32 status = writer->Write(&b, 1);
|
||||
|
||||
if (status < 0)
|
||||
return status;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32 SerializeFloat(IMkvWriter* writer, float f) {
|
||||
if (!writer)
|
||||
return -1;
|
||||
|
||||
assert(sizeof(uint32) == sizeof(float));
|
||||
// This union is merely used to avoid a reinterpret_cast from float& to
|
||||
// uint32& which will result in violation of strict aliasing.
|
||||
union U32 {
|
||||
uint32 u32;
|
||||
float f;
|
||||
} value;
|
||||
value.f = f;
|
||||
|
||||
for (int32 i = 1; i <= 4; ++i) {
|
||||
const int32 byte_count = 4 - i;
|
||||
const int32 bit_count = byte_count * 8;
|
||||
|
||||
const uint8 byte = static_cast<uint8>(value.u32 >> bit_count);
|
||||
|
||||
const int32 status = writer->Write(&byte, 1);
|
||||
|
||||
if (status < 0)
|
||||
return status;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32 WriteUInt(IMkvWriter* writer, uint64 value) {
|
||||
if (!writer)
|
||||
return -1;
|
||||
|
||||
int32 size = GetCodedUIntSize(value);
|
||||
|
||||
return WriteUIntSize(writer, value, size);
|
||||
}
|
||||
|
||||
int32 WriteUIntSize(IMkvWriter* writer, uint64 value, int32 size) {
|
||||
if (!writer || size < 0 || size > 8)
|
||||
return -1;
|
||||
|
||||
if (size > 0) {
|
||||
const uint64 bit = 1LL << (size * 7);
|
||||
|
||||
if (value > (bit - 2))
|
||||
return -1;
|
||||
|
||||
value |= bit;
|
||||
} else {
|
||||
size = 1;
|
||||
int64 bit;
|
||||
|
||||
for (;;) {
|
||||
bit = 1LL << (size * 7);
|
||||
const uint64 max = bit - 2;
|
||||
|
||||
if (value <= max)
|
||||
break;
|
||||
|
||||
++size;
|
||||
}
|
||||
|
||||
if (size > 8)
|
||||
return false;
|
||||
|
||||
value |= bit;
|
||||
}
|
||||
|
||||
return SerializeInt(writer, value, size);
|
||||
}
|
||||
|
||||
int32 WriteID(IMkvWriter* writer, uint64 type) {
|
||||
if (!writer)
|
||||
return -1;
|
||||
|
||||
writer->ElementStartNotify(type, writer->Position());
|
||||
|
||||
const int32 size = GetUIntSize(type);
|
||||
|
||||
return SerializeInt(writer, type, size);
|
||||
}
|
||||
|
||||
bool WriteEbmlMasterElement(IMkvWriter* writer, uint64 type, uint64 size) {
|
||||
if (!writer)
|
||||
return false;
|
||||
|
||||
if (WriteID(writer, type))
|
||||
return false;
|
||||
|
||||
if (WriteUInt(writer, size))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteEbmlElement(IMkvWriter* writer, uint64 type, uint64 value) {
|
||||
return WriteEbmlElement(writer, type, value, 0);
|
||||
}
|
||||
|
||||
bool WriteEbmlElement(IMkvWriter* writer, uint64 type, uint64 value,
|
||||
uint64 fixed_size) {
|
||||
if (!writer)
|
||||
return false;
|
||||
|
||||
if (WriteID(writer, type))
|
||||
return false;
|
||||
|
||||
uint64 size = GetUIntSize(value);
|
||||
if (fixed_size > 0) {
|
||||
if (size > fixed_size)
|
||||
return false;
|
||||
size = fixed_size;
|
||||
}
|
||||
if (WriteUInt(writer, size))
|
||||
return false;
|
||||
|
||||
if (SerializeInt(writer, value, static_cast<int32>(size)))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteEbmlElement(IMkvWriter* writer, uint64 type, int64 value) {
|
||||
if (!writer)
|
||||
return false;
|
||||
|
||||
if (WriteID(writer, type))
|
||||
return 0;
|
||||
|
||||
const uint64 size = GetIntSize(value);
|
||||
if (WriteUInt(writer, size))
|
||||
return false;
|
||||
|
||||
if (SerializeInt(writer, value, static_cast<int32>(size)))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteEbmlElement(IMkvWriter* writer, uint64 type, float value) {
|
||||
if (!writer)
|
||||
return false;
|
||||
|
||||
if (WriteID(writer, type))
|
||||
return false;
|
||||
|
||||
if (WriteUInt(writer, 4))
|
||||
return false;
|
||||
|
||||
if (SerializeFloat(writer, value))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteEbmlElement(IMkvWriter* writer, uint64 type, const char* value) {
|
||||
if (!writer || !value)
|
||||
return false;
|
||||
|
||||
if (WriteID(writer, type))
|
||||
return false;
|
||||
|
||||
const uint64 length = strlen(value);
|
||||
if (WriteUInt(writer, length))
|
||||
return false;
|
||||
|
||||
if (writer->Write(value, static_cast<const uint32>(length)))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteEbmlElement(IMkvWriter* writer, uint64 type, const uint8* value,
|
||||
uint64 size) {
|
||||
if (!writer || !value || size < 1)
|
||||
return false;
|
||||
|
||||
if (WriteID(writer, type))
|
||||
return false;
|
||||
|
||||
if (WriteUInt(writer, size))
|
||||
return false;
|
||||
|
||||
if (writer->Write(value, static_cast<uint32>(size)))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteEbmlDateElement(IMkvWriter* writer, uint64 type, int64 value) {
|
||||
if (!writer)
|
||||
return false;
|
||||
|
||||
if (WriteID(writer, type))
|
||||
return false;
|
||||
|
||||
if (WriteUInt(writer, kDateElementSize))
|
||||
return false;
|
||||
|
||||
if (SerializeInt(writer, value, kDateElementSize))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint64 WriteFrame(IMkvWriter* writer, const Frame* const frame,
|
||||
Cluster* cluster) {
|
||||
if (!writer || !frame || !frame->IsValid() || !cluster ||
|
||||
!cluster->timecode_scale())
|
||||
return 0;
|
||||
|
||||
// Technically the timecode for a block can be less than the
|
||||
// timecode for the cluster itself (remember that block timecode
|
||||
// is a signed, 16-bit integer). However, as a simplification we
|
||||
// only permit non-negative cluster-relative timecodes for blocks.
|
||||
const int64 relative_timecode = cluster->GetRelativeTimecode(
|
||||
frame->timestamp() / cluster->timecode_scale());
|
||||
if (relative_timecode < 0 || relative_timecode > kMaxBlockTimecode)
|
||||
return 0;
|
||||
|
||||
return frame->CanBeSimpleBlock() ?
|
||||
WriteSimpleBlock(writer, frame, relative_timecode) :
|
||||
WriteBlock(writer, frame, relative_timecode,
|
||||
cluster->timecode_scale());
|
||||
}
|
||||
|
||||
uint64 WriteVoidElement(IMkvWriter* writer, uint64 size) {
|
||||
if (!writer)
|
||||
return false;
|
||||
|
||||
// Subtract one for the void ID and the coded size.
|
||||
uint64 void_entry_size = size - 1 - GetCodedUIntSize(size - 1);
|
||||
uint64 void_size = EbmlMasterElementSize(libwebm::kMkvVoid, void_entry_size) +
|
||||
void_entry_size;
|
||||
|
||||
if (void_size != size)
|
||||
return 0;
|
||||
|
||||
const int64 payload_position = writer->Position();
|
||||
if (payload_position < 0)
|
||||
return 0;
|
||||
|
||||
if (WriteID(writer, libwebm::kMkvVoid))
|
||||
return 0;
|
||||
|
||||
if (WriteUInt(writer, void_entry_size))
|
||||
return 0;
|
||||
|
||||
const uint8 value = 0;
|
||||
for (int32 i = 0; i < static_cast<int32>(void_entry_size); ++i) {
|
||||
if (writer->Write(&value, 1))
|
||||
return 0;
|
||||
}
|
||||
|
||||
const int64 stop_position = writer->Position();
|
||||
if (stop_position < 0 ||
|
||||
stop_position - payload_position != static_cast<int64>(void_size))
|
||||
return 0;
|
||||
|
||||
return void_size;
|
||||
}
|
||||
|
||||
void GetVersion(int32* major, int32* minor, int32* build, int32* revision) {
|
||||
*major = 0;
|
||||
*minor = 2;
|
||||
*build = 1;
|
||||
*revision = 0;
|
||||
}
|
||||
|
||||
uint64 MakeUID(unsigned int* seed) {
|
||||
uint64 uid = 0;
|
||||
|
||||
#ifdef __MINGW32__
|
||||
srand(*seed);
|
||||
#endif
|
||||
|
||||
for (int i = 0; i < 7; ++i) { // avoid problems with 8-byte values
|
||||
uid <<= 8;
|
||||
|
||||
// TODO(fgalligan): Move random number generation to platform specific code.
|
||||
#ifdef _MSC_VER
|
||||
(void)seed;
|
||||
const int32 nn = rand();
|
||||
#elif __ANDROID__
|
||||
(void)seed;
|
||||
int32 temp_num = 1;
|
||||
int fd = open("/dev/urandom", O_RDONLY);
|
||||
if (fd != -1) {
|
||||
read(fd, &temp_num, sizeof(temp_num));
|
||||
close(fd);
|
||||
}
|
||||
const int32 nn = temp_num;
|
||||
#elif defined __MINGW32__
|
||||
const int32 nn = rand();
|
||||
#else
|
||||
const int32 nn = rand_r(seed);
|
||||
#endif
|
||||
const int32 n = 0xFF & (nn >> 4); // throw away low-order bits
|
||||
|
||||
uid |= n;
|
||||
}
|
||||
|
||||
return uid;
|
||||
}
|
||||
|
||||
bool IsMatrixCoefficientsValueValid(uint64_t value) {
|
||||
switch (value) {
|
||||
case mkvmuxer::Colour::kGbr:
|
||||
case mkvmuxer::Colour::kBt709:
|
||||
case mkvmuxer::Colour::kUnspecifiedMc:
|
||||
case mkvmuxer::Colour::kReserved:
|
||||
case mkvmuxer::Colour::kFcc:
|
||||
case mkvmuxer::Colour::kBt470bg:
|
||||
case mkvmuxer::Colour::kSmpte170MMc:
|
||||
case mkvmuxer::Colour::kSmpte240MMc:
|
||||
case mkvmuxer::Colour::kYcocg:
|
||||
case mkvmuxer::Colour::kBt2020NonConstantLuminance:
|
||||
case mkvmuxer::Colour::kBt2020ConstantLuminance:
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IsChromaSitingHorzValueValid(uint64_t value) {
|
||||
switch (value) {
|
||||
case mkvmuxer::Colour::kUnspecifiedCsh:
|
||||
case mkvmuxer::Colour::kLeftCollocated:
|
||||
case mkvmuxer::Colour::kHalfCsh:
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IsChromaSitingVertValueValid(uint64_t value) {
|
||||
switch (value) {
|
||||
case mkvmuxer::Colour::kUnspecifiedCsv:
|
||||
case mkvmuxer::Colour::kTopCollocated:
|
||||
case mkvmuxer::Colour::kHalfCsv:
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IsColourRangeValueValid(uint64_t value) {
|
||||
switch (value) {
|
||||
case mkvmuxer::Colour::kUnspecifiedCr:
|
||||
case mkvmuxer::Colour::kBroadcastRange:
|
||||
case mkvmuxer::Colour::kFullRange:
|
||||
case mkvmuxer::Colour::kMcTcDefined:
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IsTransferCharacteristicsValueValid(uint64_t value) {
|
||||
switch (value) {
|
||||
case mkvmuxer::Colour::kIturBt709Tc:
|
||||
case mkvmuxer::Colour::kUnspecifiedTc:
|
||||
case mkvmuxer::Colour::kReservedTc:
|
||||
case mkvmuxer::Colour::kGamma22Curve:
|
||||
case mkvmuxer::Colour::kGamma28Curve:
|
||||
case mkvmuxer::Colour::kSmpte170MTc:
|
||||
case mkvmuxer::Colour::kSmpte240MTc:
|
||||
case mkvmuxer::Colour::kLinear:
|
||||
case mkvmuxer::Colour::kLog:
|
||||
case mkvmuxer::Colour::kLogSqrt:
|
||||
case mkvmuxer::Colour::kIec6196624:
|
||||
case mkvmuxer::Colour::kIturBt1361ExtendedColourGamut:
|
||||
case mkvmuxer::Colour::kIec6196621:
|
||||
case mkvmuxer::Colour::kIturBt202010bit:
|
||||
case mkvmuxer::Colour::kIturBt202012bit:
|
||||
case mkvmuxer::Colour::kSmpteSt2084:
|
||||
case mkvmuxer::Colour::kSmpteSt4281Tc:
|
||||
case mkvmuxer::Colour::kAribStdB67Hlg:
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IsPrimariesValueValid(uint64_t value) {
|
||||
switch (value) {
|
||||
case mkvmuxer::Colour::kReservedP0:
|
||||
case mkvmuxer::Colour::kIturBt709P:
|
||||
case mkvmuxer::Colour::kUnspecifiedP:
|
||||
case mkvmuxer::Colour::kReservedP3:
|
||||
case mkvmuxer::Colour::kIturBt470M:
|
||||
case mkvmuxer::Colour::kIturBt470Bg:
|
||||
case mkvmuxer::Colour::kSmpte170MP:
|
||||
case mkvmuxer::Colour::kSmpte240MP:
|
||||
case mkvmuxer::Colour::kFilm:
|
||||
case mkvmuxer::Colour::kIturBt2020:
|
||||
case mkvmuxer::Colour::kSmpteSt4281P:
|
||||
case mkvmuxer::Colour::kJedecP22Phosphors:
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace mkvmuxer
|
@ -1,112 +0,0 @@
|
||||
// Copyright (c) 2012 The WebM project authors. All Rights Reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
// that can be found in the LICENSE file in the root of the source
|
||||
// tree. An additional intellectual property rights grant can be found
|
||||
// in the file PATENTS. All contributing project authors may
|
||||
// be found in the AUTHORS file in the root of the source tree.
|
||||
#ifndef MKVMUXER_MKVMUXERUTIL_H_
|
||||
#define MKVMUXER_MKVMUXERUTIL_H_
|
||||
|
||||
#include "mkvmuxertypes.h"
|
||||
|
||||
#include "stdint.h"
|
||||
|
||||
namespace mkvmuxer {
|
||||
class Cluster;
|
||||
class Frame;
|
||||
class IMkvWriter;
|
||||
|
||||
// TODO(tomfinegan): mkvmuxer:: integer types continue to be used here because
|
||||
// changing them causes pain for downstream projects. It would be nice if a
|
||||
// solution that allows removal of the mkvmuxer:: integer types while avoiding
|
||||
// pain for downstream users of libwebm. Considering that mkvmuxerutil.{cc,h}
|
||||
// are really, for the great majority of cases, EBML size calculation and writer
|
||||
// functions, perhaps a more EBML focused utility would be the way to go as a
|
||||
// first step.
|
||||
|
||||
const uint64 kEbmlUnknownValue = 0x01FFFFFFFFFFFFFFULL;
|
||||
const int64 kMaxBlockTimecode = 0x07FFFLL;
|
||||
|
||||
// Writes out |value| in Big Endian order. Returns 0 on success.
|
||||
int32 SerializeInt(IMkvWriter* writer, int64 value, int32 size);
|
||||
|
||||
// Returns the size in bytes of the element.
|
||||
int32 GetUIntSize(uint64 value);
|
||||
int32 GetIntSize(int64 value);
|
||||
int32 GetCodedUIntSize(uint64 value);
|
||||
uint64 EbmlMasterElementSize(uint64 type, uint64 value);
|
||||
uint64 EbmlElementSize(uint64 type, int64 value);
|
||||
uint64 EbmlElementSize(uint64 type, uint64 value);
|
||||
uint64 EbmlElementSize(uint64 type, float value);
|
||||
uint64 EbmlElementSize(uint64 type, const char* value);
|
||||
uint64 EbmlElementSize(uint64 type, const uint8* value, uint64 size);
|
||||
uint64 EbmlDateElementSize(uint64 type);
|
||||
|
||||
// Returns the size in bytes of the element assuming that the element was
|
||||
// written using |fixed_size| bytes. If |fixed_size| is set to zero, then it
|
||||
// computes the necessary number of bytes based on |value|.
|
||||
uint64 EbmlElementSize(uint64 type, uint64 value, uint64 fixed_size);
|
||||
|
||||
// Creates an EBML coded number from |value| and writes it out. The size of
|
||||
// the coded number is determined by the value of |value|. |value| must not
|
||||
// be in a coded form. Returns 0 on success.
|
||||
int32 WriteUInt(IMkvWriter* writer, uint64 value);
|
||||
|
||||
// Creates an EBML coded number from |value| and writes it out. The size of
|
||||
// the coded number is determined by the value of |size|. |value| must not
|
||||
// be in a coded form. Returns 0 on success.
|
||||
int32 WriteUIntSize(IMkvWriter* writer, uint64 value, int32 size);
|
||||
|
||||
// Output an Mkv master element. Returns true if the element was written.
|
||||
bool WriteEbmlMasterElement(IMkvWriter* writer, uint64 value, uint64 size);
|
||||
|
||||
// Outputs an Mkv ID, calls |IMkvWriter::ElementStartNotify|, and passes the
|
||||
// ID to |SerializeInt|. Returns 0 on success.
|
||||
int32 WriteID(IMkvWriter* writer, uint64 type);
|
||||
|
||||
// Output an Mkv non-master element. Returns true if the element was written.
|
||||
bool WriteEbmlElement(IMkvWriter* writer, uint64 type, uint64 value);
|
||||
bool WriteEbmlElement(IMkvWriter* writer, uint64 type, int64 value);
|
||||
bool WriteEbmlElement(IMkvWriter* writer, uint64 type, float value);
|
||||
bool WriteEbmlElement(IMkvWriter* writer, uint64 type, const char* value);
|
||||
bool WriteEbmlElement(IMkvWriter* writer, uint64 type, const uint8* value,
|
||||
uint64 size);
|
||||
bool WriteEbmlDateElement(IMkvWriter* writer, uint64 type, int64 value);
|
||||
|
||||
// Output an Mkv non-master element using fixed size. The element will be
|
||||
// written out using exactly |fixed_size| bytes. If |fixed_size| is set to zero
|
||||
// then it computes the necessary number of bytes based on |value|. Returns true
|
||||
// if the element was written.
|
||||
bool WriteEbmlElement(IMkvWriter* writer, uint64 type, uint64 value,
|
||||
uint64 fixed_size);
|
||||
|
||||
// Output a Mkv Frame. It decides the correct element to write (Block vs
|
||||
// SimpleBlock) based on the parameters of the Frame.
|
||||
uint64 WriteFrame(IMkvWriter* writer, const Frame* const frame,
|
||||
Cluster* cluster);
|
||||
|
||||
// Output a void element. |size| must be the entire size in bytes that will be
|
||||
// void. The function will calculate the size of the void header and subtract
|
||||
// it from |size|.
|
||||
uint64 WriteVoidElement(IMkvWriter* writer, uint64 size);
|
||||
|
||||
// Returns the version number of the muxer in |major|, |minor|, |build|,
|
||||
// and |revision|.
|
||||
void GetVersion(int32* major, int32* minor, int32* build, int32* revision);
|
||||
|
||||
// Returns a random number to be used for UID, using |seed| to seed
|
||||
// the random-number generator (see POSIX rand_r() for semantics).
|
||||
uint64 MakeUID(unsigned int* seed);
|
||||
|
||||
// Colour field validation helpers. All return true when |value| is valid.
|
||||
bool IsMatrixCoefficientsValueValid(uint64_t value);
|
||||
bool IsChromaSitingHorzValueValid(uint64_t value);
|
||||
bool IsChromaSitingVertValueValid(uint64_t value);
|
||||
bool IsColourRangeValueValid(uint64_t value);
|
||||
bool IsTransferCharacteristicsValueValid(uint64_t value);
|
||||
bool IsPrimariesValueValid(uint64_t value);
|
||||
|
||||
} // namespace mkvmuxer
|
||||
|
||||
#endif // MKVMUXER_MKVMUXERUTIL_H_
|
@ -1,90 +0,0 @@
|
||||
// Copyright (c) 2012 The WebM project authors. All Rights Reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
// that can be found in the LICENSE file in the root of the source
|
||||
// tree. An additional intellectual property rights grant can be found
|
||||
// in the file PATENTS. All contributing project authors may
|
||||
// be found in the AUTHORS file in the root of the source tree.
|
||||
|
||||
#include "mkvmuxer/mkvwriter.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include <share.h> // for _SH_DENYWR
|
||||
#endif
|
||||
|
||||
namespace mkvmuxer {
|
||||
|
||||
MkvWriter::MkvWriter() : file_(NULL), writer_owns_file_(true) {}
|
||||
|
||||
MkvWriter::MkvWriter(FILE* fp) : file_(fp), writer_owns_file_(false) {}
|
||||
|
||||
MkvWriter::~MkvWriter() { Close(); }
|
||||
|
||||
int32 MkvWriter::Write(const void* buffer, uint32 length) {
|
||||
if (!file_)
|
||||
return -1;
|
||||
|
||||
if (length == 0)
|
||||
return 0;
|
||||
|
||||
if (buffer == NULL)
|
||||
return -1;
|
||||
|
||||
const size_t bytes_written = fwrite(buffer, 1, length, file_);
|
||||
|
||||
return (bytes_written == length) ? 0 : -1;
|
||||
}
|
||||
|
||||
bool MkvWriter::Open(const char* filename) {
|
||||
if (filename == NULL)
|
||||
return false;
|
||||
|
||||
if (file_)
|
||||
return false;
|
||||
|
||||
#ifdef _MSC_VER
|
||||
file_ = _fsopen(filename, "wb", _SH_DENYWR);
|
||||
#else
|
||||
file_ = fopen(filename, "wb");
|
||||
#endif
|
||||
if (file_ == NULL)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void MkvWriter::Close() {
|
||||
if (file_ && writer_owns_file_) {
|
||||
fclose(file_);
|
||||
}
|
||||
file_ = NULL;
|
||||
}
|
||||
|
||||
int64 MkvWriter::Position() const {
|
||||
if (!file_)
|
||||
return 0;
|
||||
|
||||
#ifdef _MSC_VER
|
||||
return _ftelli64(file_);
|
||||
#else
|
||||
return ftell(file_);
|
||||
#endif
|
||||
}
|
||||
|
||||
int32 MkvWriter::Position(int64 position) {
|
||||
if (!file_)
|
||||
return -1;
|
||||
|
||||
#ifdef _MSC_VER
|
||||
return _fseeki64(file_, position, SEEK_SET);
|
||||
#else
|
||||
return fseeko(file_, static_cast<off_t>(position), SEEK_SET);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool MkvWriter::Seekable() const { return true; }
|
||||
|
||||
void MkvWriter::ElementStartNotify(uint64, int64) {}
|
||||
|
||||
} // namespace mkvmuxer
|
@ -1,51 +0,0 @@
|
||||
// Copyright (c) 2012 The WebM project authors. All Rights Reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
// that can be found in the LICENSE file in the root of the source
|
||||
// tree. An additional intellectual property rights grant can be found
|
||||
// in the file PATENTS. All contributing project authors may
|
||||
// be found in the AUTHORS file in the root of the source tree.
|
||||
|
||||
#ifndef MKVMUXER_MKVWRITER_H_
|
||||
#define MKVMUXER_MKVWRITER_H_
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "mkvmuxer/mkvmuxer.h"
|
||||
#include "mkvmuxer/mkvmuxertypes.h"
|
||||
|
||||
namespace mkvmuxer {
|
||||
|
||||
// Default implementation of the IMkvWriter interface on Windows.
|
||||
class MkvWriter : public IMkvWriter {
|
||||
public:
|
||||
MkvWriter();
|
||||
explicit MkvWriter(FILE* fp);
|
||||
virtual ~MkvWriter();
|
||||
|
||||
// IMkvWriter interface
|
||||
virtual int64 Position() const;
|
||||
virtual int32 Position(int64 position);
|
||||
virtual bool Seekable() const;
|
||||
virtual int32 Write(const void* buffer, uint32 length);
|
||||
virtual void ElementStartNotify(uint64 element_id, int64 position);
|
||||
|
||||
// Creates and opens a file for writing. |filename| is the name of the file
|
||||
// to open. This function will overwrite the contents of |filename|. Returns
|
||||
// true on success.
|
||||
bool Open(const char* filename);
|
||||
|
||||
// Closes an opened file.
|
||||
void Close();
|
||||
|
||||
private:
|
||||
// File handle to output file.
|
||||
FILE* file_;
|
||||
bool writer_owns_file_;
|
||||
|
||||
LIBWEBM_DISALLOW_COPY_AND_ASSIGN(MkvWriter);
|
||||
};
|
||||
|
||||
} // namespace mkvmuxer
|
||||
|
||||
#endif // MKVMUXER_MKVWRITER_H_
|
File diff suppressed because it is too large
Load Diff
@ -57,7 +57,6 @@
|
||||
#include "modes/profile_world.hpp"
|
||||
#include "modes/world.hpp"
|
||||
#include "physics/physics.hpp"
|
||||
#include "recorder/openglrecorder.h"
|
||||
#include "scriptengine/property_animator.hpp"
|
||||
#include "states_screens/dialogs/confirm_resolution_dialog.hpp"
|
||||
#include "states_screens/state_manager.hpp"
|
||||
@ -67,8 +66,9 @@
|
||||
#include "utils/profiler.hpp"
|
||||
#include "utils/vs.hpp"
|
||||
|
||||
#include <irrlicht.h>
|
||||
#include <chrono>
|
||||
#include <irrlicht.h>
|
||||
#include <openglrecorder.h>
|
||||
|
||||
/* Build-time check that the Irrlicht we're building against works for us.
|
||||
* Should help prevent distros building against an incompatible library.
|
||||
|
@ -1,296 +0,0 @@
|
||||
#ifdef ENABLE_RECORDER
|
||||
#include "capture_library.hpp"
|
||||
|
||||
#include "mjpeg_writer.hpp"
|
||||
#include "mkv_writer.hpp"
|
||||
#include "recorder_private.hpp"
|
||||
#include "pulseaudio_recorder.hpp"
|
||||
#include "vpx_encoder.hpp"
|
||||
#include "wasapi_recorder.hpp"
|
||||
|
||||
const uint32_t E_GL_PIXEL_PACK_BUFFER = 0x88EB;
|
||||
const uint32_t E_GL_STREAM_READ = 0x88E1;
|
||||
const uint32_t E_GL_READ_ONLY = 0x88B8;
|
||||
const uint32_t E_GL_RGBA = 0x1908;
|
||||
const uint32_t E_GL_UNSIGNED_BYTE = 0x1401;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
CaptureLibrary::CaptureLibrary(RecorderConfig* rc)
|
||||
{
|
||||
m_recorder_cfg = rc;
|
||||
m_destroy.store(false);
|
||||
m_sound_stop.store(true);
|
||||
m_display_progress.store(false);
|
||||
m_compress_handle = tjInitCompress();
|
||||
m_decompress_handle = tjInitDecompress();
|
||||
if (m_recorder_cfg->m_triple_buffering > 0)
|
||||
{
|
||||
ogrGenBuffers(3, m_pbo);
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
ogrBindBuffer(E_GL_PIXEL_PACK_BUFFER, m_pbo[i]);
|
||||
ogrBufferData(E_GL_PIXEL_PACK_BUFFER, m_recorder_cfg->m_width *
|
||||
m_recorder_cfg->m_height * 4, NULL, E_GL_STREAM_READ);
|
||||
}
|
||||
ogrBindBuffer(E_GL_PIXEL_PACK_BUFFER, 0);
|
||||
}
|
||||
m_capture_thread = std::thread(CaptureLibrary::captureConversion, this);
|
||||
} // CaptureLibrary
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
CaptureLibrary::~CaptureLibrary()
|
||||
{
|
||||
m_destroy.store(true);
|
||||
addFrameBufferImage(NULL, ogrCapturing() > 0 ? -1 : 0);
|
||||
m_capture_thread.join();
|
||||
tjDestroy(m_compress_handle);
|
||||
tjDestroy(m_decompress_handle);
|
||||
if (m_recorder_cfg->m_triple_buffering > 0)
|
||||
{
|
||||
ogrDeleteBuffers(3, m_pbo);
|
||||
}
|
||||
} // ~CaptureLibrary
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void CaptureLibrary::reset()
|
||||
{
|
||||
runCallback(OGR_CBT_START_RECORDING, NULL);
|
||||
m_pbo_use = 0;
|
||||
m_accumulated_time = 0.;
|
||||
assert(m_sound_stop.load() && ogrCapturing() == 0);
|
||||
if (m_recorder_cfg->m_record_audio > 0)
|
||||
{
|
||||
m_sound_stop.store(false);
|
||||
m_audio_enc_thread = std::thread(Recorder::audioRecorder, this);
|
||||
}
|
||||
setCapturing(true);
|
||||
switch (m_recorder_cfg->m_video_format)
|
||||
{
|
||||
case OGR_VF_VP8:
|
||||
case OGR_VF_VP9:
|
||||
m_video_enc_thread = std::thread(Recorder::vpxEncoder, this);
|
||||
break;
|
||||
case OGR_VF_MJPEG:
|
||||
m_video_enc_thread = std::thread(Recorder::mjpegWriter, this);
|
||||
break;
|
||||
case OGR_VF_H264:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} // reset
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
int CaptureLibrary::bmpToJPG(uint8_t* raw, unsigned width, unsigned height,
|
||||
uint8_t** jpeg_buffer, unsigned long* jpeg_size)
|
||||
{
|
||||
int ret = 0;
|
||||
#ifdef TJFLAG_FASTDCT
|
||||
ret = tjCompress2(m_compress_handle, raw, width, 0, height, TJPF_RGBX,
|
||||
jpeg_buffer, jpeg_size, TJSAMP_420,
|
||||
m_recorder_cfg->m_record_jpg_quality, TJFLAG_FASTDCT);
|
||||
#else
|
||||
ret = tjCompress2(m_compress_handle, raw, width, 0, height, TJPF_RGBX,
|
||||
jpeg_buffer, jpeg_size, TJSAMP_420,
|
||||
m_recorder_cfg->m_record_jpg_quality, 0);
|
||||
#endif
|
||||
if (ret != 0)
|
||||
{
|
||||
char* err = tjGetErrorStr();
|
||||
printf("Jpeg encode error: %s.", err);
|
||||
return ret;
|
||||
}
|
||||
return ret;
|
||||
} // bmpToJPG
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
int CaptureLibrary::yuvConversion(uint8_t* jpeg_buffer, unsigned jpeg_size,
|
||||
uint8_t** yuv_buffer, unsigned* yuv_size)
|
||||
{
|
||||
int width, height;
|
||||
TJSAMP subsample;
|
||||
int ret = 0;
|
||||
ret = tjDecompressHeader2(m_decompress_handle, jpeg_buffer, jpeg_size,
|
||||
&width, &height, (int*)&subsample);
|
||||
if (ret != 0)
|
||||
{
|
||||
char* err = tjGetErrorStr();
|
||||
printf("Jpeg decode error: %s.", err);
|
||||
return ret;
|
||||
}
|
||||
*yuv_size = tjBufSizeYUV(width, height, subsample);
|
||||
*yuv_buffer = new uint8_t[*yuv_size];
|
||||
ret = tjDecompressToYUV(m_decompress_handle, jpeg_buffer, jpeg_size,
|
||||
*yuv_buffer, 0);
|
||||
if (ret != 0)
|
||||
{
|
||||
char* err = tjGetErrorStr();
|
||||
printf("YUV conversion error: %s.", err);
|
||||
return ret;
|
||||
}
|
||||
return ret;
|
||||
} // yuvConversion
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
int CaptureLibrary::getFrameCount(double rate)
|
||||
{
|
||||
const double frame_rate = 1. / double(m_recorder_cfg->m_record_fps);
|
||||
m_accumulated_time += rate;
|
||||
if (m_accumulated_time < frame_rate)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int frame_count = 0;
|
||||
while (m_accumulated_time >= frame_rate)
|
||||
{
|
||||
frame_count++;
|
||||
m_accumulated_time = m_accumulated_time - frame_rate;
|
||||
}
|
||||
return frame_count;
|
||||
} // getFrameCount
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void CaptureLibrary::capture()
|
||||
{
|
||||
int pbo_read = -1;
|
||||
if (m_pbo_use > 3 && m_pbo_use % 3 == 0)
|
||||
m_pbo_use = 3;
|
||||
auto rate = std::chrono::high_resolution_clock::now() - m_framerate_timer;
|
||||
m_framerate_timer = std::chrono::high_resolution_clock::now();
|
||||
const unsigned width = m_recorder_cfg->m_width;
|
||||
const unsigned height = m_recorder_cfg->m_height;
|
||||
const bool use_pbo = m_recorder_cfg->m_triple_buffering > 0;
|
||||
if (m_pbo_use >= 3)
|
||||
{
|
||||
int frame_count = getFrameCount(std::chrono::duration_cast
|
||||
<std::chrono::duration<double> >(rate).count());
|
||||
if (frame_count != 0)
|
||||
{
|
||||
const unsigned size = width * height * 4;
|
||||
uint8_t* fbi = new uint8_t[size];
|
||||
if (use_pbo)
|
||||
{
|
||||
pbo_read = m_pbo_use % 3;
|
||||
ogrBindBuffer(E_GL_PIXEL_PACK_BUFFER, m_pbo[pbo_read]);
|
||||
void* ptr = ogrMapBuffer(E_GL_PIXEL_PACK_BUFFER,
|
||||
E_GL_READ_ONLY);
|
||||
memcpy(fbi, ptr, size);
|
||||
ogrUnmapBuffer(E_GL_PIXEL_PACK_BUFFER);
|
||||
}
|
||||
else
|
||||
{
|
||||
ogrReadPixels(0, 0, width, height, E_GL_RGBA,
|
||||
E_GL_UNSIGNED_BYTE, fbi);
|
||||
}
|
||||
addFrameBufferImage(fbi, frame_count);
|
||||
}
|
||||
}
|
||||
int pbo_use = m_pbo_use++ % 3;
|
||||
if (!use_pbo)
|
||||
return;
|
||||
|
||||
assert(pbo_read == -1 || pbo_use == pbo_read);
|
||||
ogrBindBuffer(E_GL_PIXEL_PACK_BUFFER, m_pbo[pbo_use]);
|
||||
ogrReadPixels(0, 0, width, height, E_GL_RGBA, E_GL_UNSIGNED_BYTE, NULL);
|
||||
ogrBindBuffer(E_GL_PIXEL_PACK_BUFFER, 0);
|
||||
} // capture
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void CaptureLibrary::captureConversion(CaptureLibrary* cl)
|
||||
{
|
||||
setThreadName("captureConvert");
|
||||
while (true)
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(cl->m_fbi_list_mutex);
|
||||
cl->m_fbi_list_ready.wait(ul, [&cl]
|
||||
{ return !cl->m_fbi_list.empty(); });
|
||||
auto& p = cl->m_fbi_list.front();
|
||||
uint8_t* fbi = p.first;
|
||||
int frame_count = p.second;
|
||||
if (frame_count == -1)
|
||||
{
|
||||
cl->m_fbi_list.clear();
|
||||
ul.unlock();
|
||||
if (cl->m_recorder_cfg->m_record_audio > 0)
|
||||
{
|
||||
cl->m_sound_stop.store(true);
|
||||
cl->m_audio_enc_thread.join();
|
||||
}
|
||||
std::unique_lock<std::mutex> ulj(cl->m_jpg_list_mutex);
|
||||
if (!cl->m_destroy.load() && cl->m_jpg_list.size() > 100)
|
||||
{
|
||||
runCallback(OGR_CBT_WAIT_RECORDING, NULL);
|
||||
}
|
||||
cl->m_jpg_list.emplace_back((uint8_t*)NULL, 0, 0);
|
||||
cl->m_jpg_list_ready.notify_one();
|
||||
ulj.unlock();
|
||||
cl->m_display_progress.store(!cl->m_destroy.load());
|
||||
cl->m_video_enc_thread.join();
|
||||
cl->m_display_progress.store(false);
|
||||
std::string f = Recorder::writeMKV(getSavedName() + ".video",
|
||||
getSavedName() + ".audio");
|
||||
if (cl->m_destroy.load())
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (f.empty())
|
||||
{
|
||||
runCallback(OGR_CBT_ERROR_RECORDING, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
runCallback(OGR_CBT_SAVED_RECORDING, f.c_str());
|
||||
}
|
||||
setCapturing(false);
|
||||
continue;
|
||||
}
|
||||
else if (fbi == NULL)
|
||||
{
|
||||
cl->m_fbi_list.clear();
|
||||
ul.unlock();
|
||||
return;
|
||||
}
|
||||
const bool too_slow = cl->m_fbi_list.size() > 50;
|
||||
if (too_slow)
|
||||
{
|
||||
if (!cl->m_destroy.load())
|
||||
runCallback(OGR_CBT_SLOW_RECORDING, NULL);
|
||||
delete [] fbi;
|
||||
cl->m_fbi_list.pop_front();
|
||||
for (auto& p : cl->m_fbi_list)
|
||||
delete [] p.first;
|
||||
cl->m_fbi_list.clear();
|
||||
ul.unlock();
|
||||
continue;
|
||||
}
|
||||
cl->m_fbi_list.pop_front();
|
||||
ul.unlock();
|
||||
|
||||
uint8_t* orig_fbi = fbi;
|
||||
const unsigned width = cl->m_recorder_cfg->m_width;
|
||||
const unsigned height = cl->m_recorder_cfg->m_height;
|
||||
const int pitch = width * 4;
|
||||
uint8_t* p2 = fbi + (height - 1) * pitch;
|
||||
uint8_t* tmp_buf = new uint8_t[pitch];
|
||||
for (unsigned i = 0; i < height; i += 2)
|
||||
{
|
||||
memcpy(tmp_buf, fbi, pitch);
|
||||
memcpy(fbi, p2, pitch);
|
||||
memcpy(p2, tmp_buf, pitch);
|
||||
fbi += pitch;
|
||||
p2 -= pitch;
|
||||
}
|
||||
delete [] tmp_buf;
|
||||
|
||||
uint8_t* jpg = NULL;
|
||||
unsigned long jpg_size = 0;
|
||||
cl->bmpToJPG(orig_fbi, width, height, &jpg, &jpg_size);
|
||||
delete[] orig_fbi;
|
||||
|
||||
std::lock_guard<std::mutex> lg(cl->m_jpg_list_mutex);
|
||||
cl->m_jpg_list.emplace_back(jpg, jpg_size, frame_count);
|
||||
cl->m_jpg_list_ready.notify_one();
|
||||
}
|
||||
} // captureConversion
|
||||
|
||||
#endif
|
@ -1,118 +0,0 @@
|
||||
#ifdef ENABLE_RECORDER
|
||||
#ifndef HEADER_CAPTURE_LIBRARY_HPP
|
||||
#define HEADER_CAPTURE_LIBRARY_HPP
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER < 1700
|
||||
typedef unsigned char uint8_t;
|
||||
typedef unsigned short uint16_t;
|
||||
typedef __int32 int32_t;
|
||||
typedef unsigned __int32 uint32_t;
|
||||
typedef __int64 int64_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <cstring>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
#include <turbojpeg.h>
|
||||
|
||||
struct AudioEncoderData
|
||||
{
|
||||
enum AudioType { AT_FLOAT, AT_PCM };
|
||||
std::mutex* m_mutex;
|
||||
std::condition_variable* m_cv;
|
||||
std::list<int8_t*>* m_buf_list;
|
||||
uint32_t m_sample_rate;
|
||||
uint32_t m_channels;
|
||||
uint32_t m_audio_bitrate;
|
||||
AudioType m_audio_type;
|
||||
};
|
||||
|
||||
struct RecorderConfig;
|
||||
typedef std::list<std::tuple<uint8_t*, unsigned, int> > JPGList;
|
||||
|
||||
class CaptureLibrary
|
||||
{
|
||||
private:
|
||||
RecorderConfig* m_recorder_cfg;
|
||||
|
||||
std::atomic_bool m_destroy, m_display_progress, m_sound_stop;
|
||||
|
||||
tjhandle m_compress_handle, m_decompress_handle;
|
||||
|
||||
JPGList m_jpg_list;
|
||||
std::mutex m_jpg_list_mutex;
|
||||
std::condition_variable m_jpg_list_ready;
|
||||
|
||||
std::list<std::pair<uint8_t*, int> > m_fbi_list;
|
||||
std::mutex m_fbi_list_mutex;
|
||||
std::condition_variable m_fbi_list_ready;
|
||||
|
||||
std::thread m_capture_thread, m_audio_enc_thread, m_video_enc_thread;
|
||||
|
||||
uint32_t m_pbo[3];
|
||||
|
||||
unsigned m_pbo_use;
|
||||
|
||||
std::chrono::high_resolution_clock::time_point m_framerate_timer;
|
||||
|
||||
double m_accumulated_time;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
int getFrameCount(double rate);
|
||||
// ------------------------------------------------------------------------
|
||||
void addFrameBufferImage(uint8_t* fbi, int frame_count)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_fbi_list_mutex);
|
||||
m_fbi_list.emplace_back(fbi, frame_count);
|
||||
m_fbi_list_ready.notify_one();
|
||||
}
|
||||
|
||||
public:
|
||||
// ------------------------------------------------------------------------
|
||||
CaptureLibrary(RecorderConfig* rc);
|
||||
// ------------------------------------------------------------------------
|
||||
~CaptureLibrary();
|
||||
// ------------------------------------------------------------------------
|
||||
void capture();
|
||||
// ------------------------------------------------------------------------
|
||||
void stopCapture() { addFrameBufferImage(NULL, -1); }
|
||||
// ------------------------------------------------------------------------
|
||||
void reset();
|
||||
// ------------------------------------------------------------------------
|
||||
int bmpToJPG(uint8_t* raw, unsigned width, unsigned height,
|
||||
uint8_t** jpeg_buffer, unsigned long* jpeg_size);
|
||||
// ------------------------------------------------------------------------
|
||||
int yuvConversion(uint8_t* jpeg_buffer, unsigned jpeg_size,
|
||||
uint8_t** yuv_buffer, unsigned* yuv_size);
|
||||
// ------------------------------------------------------------------------
|
||||
JPGList* getJPGList() { return &m_jpg_list; }
|
||||
// ------------------------------------------------------------------------
|
||||
std::mutex* getJPGListMutex() { return &m_jpg_list_mutex; }
|
||||
// ------------------------------------------------------------------------
|
||||
std::condition_variable* getJPGListCV() { return &m_jpg_list_ready; }
|
||||
// ------------------------------------------------------------------------
|
||||
bool displayingProgress() const { return m_display_progress.load(); }
|
||||
// ------------------------------------------------------------------------
|
||||
bool getSoundStop() const { return m_sound_stop.load(); }
|
||||
// ------------------------------------------------------------------------
|
||||
bool getDestroy() const { return m_destroy.load(); }
|
||||
// ------------------------------------------------------------------------
|
||||
const RecorderConfig& getRecorderConfig() const { return *m_recorder_cfg; }
|
||||
// ------------------------------------------------------------------------
|
||||
static void captureConversion(CaptureLibrary* cl);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,67 +0,0 @@
|
||||
// SuperTuxKart - a fun racing game with go-kart
|
||||
// Copyright (C) 2017 SuperTuxKart-Team
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 3
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
#ifdef ENABLE_RECORDER
|
||||
|
||||
#include "capture_library.hpp"
|
||||
#include "recorder_private.hpp"
|
||||
|
||||
namespace Recorder
|
||||
{
|
||||
// ------------------------------------------------------------------------
|
||||
void mjpegWriter(CaptureLibrary* cl)
|
||||
{
|
||||
setThreadName("mjpegWriter");
|
||||
FILE* mjpeg_writer = fopen((getSavedName() + ".video").c_str(), "wb");
|
||||
if (mjpeg_writer == NULL)
|
||||
{
|
||||
printf("Failed to open file for writing mjpeg.\n");
|
||||
return;
|
||||
}
|
||||
int64_t frames_encoded = 0;
|
||||
while (true)
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(*cl->getJPGListMutex());
|
||||
cl->getJPGListCV()->wait(ul, [&cl]
|
||||
{ return !cl->getJPGList()->empty(); });
|
||||
auto& p = cl->getJPGList()->front();
|
||||
uint8_t* jpg = std::get<0>(p);
|
||||
uint32_t jpg_size = std::get<1>(p);
|
||||
int frame_count = std::get<2>(p);
|
||||
if (jpg == NULL)
|
||||
{
|
||||
cl->getJPGList()->clear();
|
||||
ul.unlock();
|
||||
break;
|
||||
}
|
||||
cl->getJPGList()->pop_front();
|
||||
ul.unlock();
|
||||
while (frame_count != 0)
|
||||
{
|
||||
fwrite(&jpg_size, 1, sizeof(uint32_t), mjpeg_writer);
|
||||
fwrite(&frames_encoded, 1, sizeof(int64_t), mjpeg_writer);
|
||||
fwrite(&jpg_size, 1, sizeof(uint32_t), mjpeg_writer);
|
||||
fwrite(jpg, 1, jpg_size, mjpeg_writer);
|
||||
frame_count--;
|
||||
frames_encoded++;
|
||||
}
|
||||
tjFree(jpg);
|
||||
}
|
||||
fclose(mjpeg_writer);
|
||||
} // mjpegWriter
|
||||
};
|
||||
#endif
|
@ -1,33 +0,0 @@
|
||||
|
||||
// SuperTuxKart - a fun racing game with go-kart
|
||||
// Copyright (C) 2017 SuperTuxKart-Team
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 3
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
#ifdef ENABLE_RECORDER
|
||||
|
||||
#ifndef HEADER_MJPEG_WRITER_HPP
|
||||
#define HEADER_MJPEG_WRITER_HPP
|
||||
|
||||
class CaptureLibrary;
|
||||
|
||||
namespace Recorder
|
||||
{
|
||||
void mjpegWriter(CaptureLibrary* cl);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,214 +0,0 @@
|
||||
// SuperTuxKart - a fun racing game with go-kart
|
||||
// Copyright (C) 2017 SuperTuxKart-Team
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 3
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
#ifdef ENABLE_RECORDER
|
||||
|
||||
#include "recorder_private.hpp"
|
||||
|
||||
#include <cstring>
|
||||
#include <list>
|
||||
#include <mkvmuxer/mkvmuxer.h>
|
||||
#include <mkvmuxer/mkvwriter.h>
|
||||
#include <mkvparser/mkvparser.h>
|
||||
#include <sys/stat.h>
|
||||
#include <vpx/vpx_encoder.h>
|
||||
|
||||
namespace Recorder
|
||||
{
|
||||
// ------------------------------------------------------------------------
|
||||
std::string writeMKV(const std::string& video, const std::string& audio)
|
||||
{
|
||||
std::string no_ext = video.substr(0, video.find_last_of("."));
|
||||
VideoFormat vf = getConfig()->m_video_format;
|
||||
std::string file_name = no_ext +
|
||||
(vf == OGR_VF_VP8 || vf == OGR_VF_VP9 ? ".webm" : ".mkv");
|
||||
mkvmuxer::MkvWriter writer;
|
||||
if (!writer.Open(file_name.c_str()))
|
||||
{
|
||||
printf("Error while opening output file.\n");
|
||||
return "";
|
||||
}
|
||||
mkvmuxer::Segment muxer_segment;
|
||||
if (!muxer_segment.Init(&writer))
|
||||
{
|
||||
printf("Could not initialize muxer segment.\n");;
|
||||
return "";
|
||||
}
|
||||
std::list<mkvmuxer::Frame*> audio_frames;
|
||||
uint8_t* buf = (uint8_t*)malloc(1024 * 1024);
|
||||
FILE* input = NULL;
|
||||
struct stat st;
|
||||
int result = stat(audio.c_str(), &st);
|
||||
if (result == 0)
|
||||
{
|
||||
input = fopen(audio.c_str(), "rb");
|
||||
uint32_t sample_rate, channels;
|
||||
fread(&sample_rate, 1, sizeof(uint32_t), input);
|
||||
fread(&channels, 1, sizeof(uint32_t), input);
|
||||
uint64_t aud_track = muxer_segment.AddAudioTrack(sample_rate, channels,
|
||||
0);
|
||||
if (!aud_track)
|
||||
{
|
||||
printf("Could not add audio track.\n");
|
||||
return "";
|
||||
}
|
||||
mkvmuxer::AudioTrack* const at = static_cast<mkvmuxer::AudioTrack*>
|
||||
(muxer_segment.GetTrackByNumber(aud_track));
|
||||
if (!at)
|
||||
{
|
||||
printf("Could not get audio track.\n");
|
||||
return "";
|
||||
}
|
||||
uint32_t codec_private_size;
|
||||
fread(&codec_private_size, 1, sizeof(uint32_t), input);
|
||||
fread(buf, 1, codec_private_size, input);
|
||||
if (!at->SetCodecPrivate(buf, codec_private_size))
|
||||
{
|
||||
printf("Could not add audio private data.\n");
|
||||
return "";
|
||||
}
|
||||
while (fread(buf, 1, 12, input) == 12)
|
||||
{
|
||||
uint32_t frame_size;
|
||||
int64_t timestamp;
|
||||
memcpy(&frame_size, buf, sizeof(uint32_t));
|
||||
memcpy(×tamp, buf + sizeof(uint32_t), sizeof(int64_t));
|
||||
fread(buf, 1, frame_size, input);
|
||||
mkvmuxer::Frame* audio_frame = new mkvmuxer::Frame();
|
||||
if (!audio_frame->Init(buf, frame_size))
|
||||
{
|
||||
printf("Failed to construct a frame.\n");
|
||||
return "";
|
||||
}
|
||||
audio_frame->set_track_number(aud_track);
|
||||
audio_frame->set_timestamp(timestamp);
|
||||
audio_frame->set_is_key(true);
|
||||
audio_frames.push_back(audio_frame);
|
||||
}
|
||||
fclose(input);
|
||||
if (remove(audio.c_str()) != 0)
|
||||
{
|
||||
printf("Failed to remove audio data file\n");
|
||||
}
|
||||
}
|
||||
uint64_t vid_track = muxer_segment.AddVideoTrack(getConfig()->m_width,
|
||||
getConfig()->m_height, 0);
|
||||
if (!vid_track)
|
||||
{
|
||||
printf("Could not add video track.\n");
|
||||
return "";
|
||||
}
|
||||
mkvmuxer::VideoTrack* const vt = static_cast<mkvmuxer::VideoTrack*>(
|
||||
muxer_segment.GetTrackByNumber(vid_track));
|
||||
if (!vt)
|
||||
{
|
||||
printf("Could not get video track.\n");
|
||||
return "";
|
||||
}
|
||||
vt->set_frame_rate(getConfig()->m_record_fps);
|
||||
switch (vf)
|
||||
{
|
||||
case OGR_VF_VP8:
|
||||
vt->set_codec_id("V_VP8");
|
||||
break;
|
||||
case OGR_VF_VP9:
|
||||
vt->set_codec_id("V_VP9");
|
||||
break;
|
||||
case OGR_VF_MJPEG:
|
||||
vt->set_codec_id("V_MJPEG");
|
||||
break;
|
||||
case OGR_VF_H264:
|
||||
vt->set_codec_id("V_MPEG4/ISO/AVC");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
input = fopen(video.c_str(), "rb");
|
||||
while (fread(buf, 1, 16, input) == 16)
|
||||
{
|
||||
uint32_t frame_size, flag;
|
||||
int64_t timestamp;
|
||||
memcpy(&frame_size, buf, sizeof(uint32_t));
|
||||
memcpy(×tamp, buf + sizeof(uint32_t), sizeof(int64_t));
|
||||
memcpy(&flag, buf + sizeof(uint32_t) + sizeof(int64_t),
|
||||
sizeof(uint32_t));
|
||||
timestamp *= 1000000000ll / getConfig()->m_record_fps;
|
||||
fread(buf, 1, frame_size, input);
|
||||
mkvmuxer::Frame muxer_frame;
|
||||
if (!muxer_frame.Init(buf, frame_size))
|
||||
{
|
||||
printf("Failed to construct a frame.\n");
|
||||
return "";
|
||||
}
|
||||
muxer_frame.set_track_number(vid_track);
|
||||
muxer_frame.set_timestamp(timestamp);
|
||||
if (vf == OGR_VF_VP8 || vf == OGR_VF_VP9)
|
||||
{
|
||||
muxer_frame.set_is_key((flag & VPX_FRAME_IS_KEY) != 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
muxer_frame.set_is_key(true);
|
||||
}
|
||||
mkvmuxer::Frame* cur_aud_frame =
|
||||
audio_frames.empty() ? NULL : audio_frames.front();
|
||||
if (cur_aud_frame != NULL)
|
||||
{
|
||||
while (cur_aud_frame->timestamp() < (uint64_t)timestamp)
|
||||
{
|
||||
if (!muxer_segment.AddGenericFrame(cur_aud_frame))
|
||||
{
|
||||
printf("Could not add audio frame.\n");
|
||||
return "";
|
||||
}
|
||||
delete cur_aud_frame;
|
||||
audio_frames.pop_front();
|
||||
if (audio_frames.empty())
|
||||
{
|
||||
cur_aud_frame = NULL;
|
||||
break;
|
||||
}
|
||||
cur_aud_frame = audio_frames.front();
|
||||
}
|
||||
}
|
||||
if (!muxer_segment.AddGenericFrame(&muxer_frame))
|
||||
{
|
||||
printf("Could not add video frame.\n");
|
||||
return "";
|
||||
}
|
||||
}
|
||||
free(buf);
|
||||
fclose(input);
|
||||
for (mkvmuxer::Frame* aud_frame : audio_frames)
|
||||
{
|
||||
delete aud_frame;
|
||||
}
|
||||
if (remove(video.c_str()) != 0)
|
||||
{
|
||||
printf("Failed to remove video data file\n");
|
||||
}
|
||||
if (!muxer_segment.Finalize())
|
||||
{
|
||||
printf("Finalization of segment failed.\n");
|
||||
return "";
|
||||
}
|
||||
writer.Close();
|
||||
return file_name;
|
||||
} // writeMKV
|
||||
};
|
||||
|
||||
#endif
|
@ -1,32 +0,0 @@
|
||||
|
||||
// SuperTuxKart - a fun racing game with go-kart
|
||||
// Copyright (C) 2017 SuperTuxKart-Team
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 3
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
#ifdef ENABLE_RECORDER
|
||||
|
||||
#ifndef HEADER_MKV_WRITER_HPP
|
||||
#define HEADER_MKV_WRITER_HPP
|
||||
#include <string>
|
||||
|
||||
namespace Recorder
|
||||
{
|
||||
std::string writeMKV(const std::string& video, const std::string& audio);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,246 +0,0 @@
|
||||
#ifdef ENABLE_RECORDER
|
||||
#ifndef HEADER_OPENGLRECORDER_H
|
||||
#define HEADER_OPENGLRECORDER_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/**
|
||||
* \mainpage libopenglrecorder
|
||||
*
|
||||
* libopenglrecorder is a library allowing (optional) async readback opengl
|
||||
* frame buffer with audio recording. It will do video and audio encoding
|
||||
* together. The user of this library has to setup opengl context himself
|
||||
* and load suitable callback. All function here should be called by the same
|
||||
* thread which created the opengl context.
|
||||
*/
|
||||
|
||||
/**
|
||||
* List of audio encoder supported by libopenglrecorder, if you want to record
|
||||
* without sound, just set m_record_audio in \ref RecorderConfig to 0 and use
|
||||
* any encoder below.
|
||||
*/
|
||||
enum AudioFormat
|
||||
{
|
||||
/**
|
||||
* Vorbis encoder by libvorbisenc.
|
||||
*/
|
||||
OGR_AF_VORBIS = 0,
|
||||
/**
|
||||
* Total numbers of audio encoder.
|
||||
*/
|
||||
OGR_AF_COUNT
|
||||
};
|
||||
|
||||
/**
|
||||
* List of video encoder supported by libopenglrecorder
|
||||
*/
|
||||
enum VideoFormat
|
||||
{
|
||||
/**
|
||||
* VP8 encoder by libvpx.
|
||||
*/
|
||||
OGR_VF_VP8 = 0,
|
||||
/**
|
||||
* VP9 encoder by libvpx. Notice: this is very slow.
|
||||
*/
|
||||
OGR_VF_VP9,
|
||||
/**
|
||||
* MJPEG encoder, it's provided by turbojpeg and will always present.
|
||||
*/
|
||||
OGR_VF_MJPEG,
|
||||
/**
|
||||
* H264 encoder by openh264.
|
||||
*/
|
||||
OGR_VF_H264,
|
||||
/**
|
||||
* Total numbers of video encoder.
|
||||
*/
|
||||
OGR_VF_COUNT
|
||||
};
|
||||
|
||||
/**
|
||||
* Callback which takes a string pointer to work with.
|
||||
*/
|
||||
typedef void(*StringCallback)(const char* s, void* user_data);
|
||||
/**
|
||||
* Callback which takes a int to work with.
|
||||
*/
|
||||
typedef void(*IntCallback)(const int i, void* user_data);
|
||||
/**
|
||||
* Callback which takes nothing (void) to work with.
|
||||
*/
|
||||
typedef void(*GeneralCallback)(void* user_data);
|
||||
|
||||
/**
|
||||
* List of callbacks currently using.
|
||||
*/
|
||||
enum CallBackType
|
||||
{
|
||||
/**
|
||||
* A \ref GeneralCallback which notify the starting of recording.
|
||||
*/
|
||||
OGR_CBT_START_RECORDING = 0,
|
||||
/**
|
||||
* A \ref StringCallback which notify the saved filename of recorded file.
|
||||
*/
|
||||
OGR_CBT_SAVED_RECORDING,
|
||||
/**
|
||||
* A \ref GeneralCallback which notify error when recording.
|
||||
*/
|
||||
OGR_CBT_ERROR_RECORDING,
|
||||
/**
|
||||
* A \ref IntCallback which the tells the progress percentage for video
|
||||
* encoding after the issue of \ref ogrStopCapture.
|
||||
*/
|
||||
OGR_CBT_PROGRESS_RECORDING,
|
||||
/**
|
||||
* A \ref GeneralCallback which notify user if there is still video
|
||||
* encoding happening after the issue of \ref ogrStopCapture.
|
||||
*/
|
||||
OGR_CBT_WAIT_RECORDING,
|
||||
/**
|
||||
* A \ref GeneralCallback which notify user if the coversion to jpeg
|
||||
* from opengl frame buffer image is too slow, so libopenglrecorder will
|
||||
* drop frames.
|
||||
*/
|
||||
OGR_CBT_SLOW_RECORDING,
|
||||
/**
|
||||
* Total callback numbers.
|
||||
*/
|
||||
OGR_CBT_COUNT
|
||||
};
|
||||
|
||||
/**
|
||||
* Settings for libopenglrecorder
|
||||
*/
|
||||
struct RecorderConfig
|
||||
{
|
||||
/**
|
||||
* 1 if triple buffering is used when capture the opengl frame buffer.
|
||||
* It will create 3 pixel buffer objects for async reading, recommend on.
|
||||
* 0 otherwise.
|
||||
*/
|
||||
unsigned int m_triple_buffering;
|
||||
/**
|
||||
* 1 if audio is recorded together, it will use wasapi in windows,
|
||||
* pulseaudio in linux. 0 means no audio will be recorded.
|
||||
*/
|
||||
unsigned int m_record_audio;
|
||||
/**
|
||||
* Width of the capture, it will be floored down to the closest integer divisble
|
||||
* by 8 if needed.
|
||||
*/
|
||||
unsigned int m_width;
|
||||
/**
|
||||
* Height of the capture, it will be floored down to the closest integer divisble
|
||||
* by 2 if needed.
|
||||
*/
|
||||
unsigned int m_height;
|
||||
/**
|
||||
* Encoder for video, see \ref VideoFormat.
|
||||
*/
|
||||
VideoFormat m_video_format;
|
||||
/**
|
||||
* Encoder for video, see \ref AudioFormat.
|
||||
*/
|
||||
AudioFormat m_audio_format;
|
||||
/**
|
||||
* Bitrate for audio encoding.
|
||||
*/
|
||||
unsigned int m_video_bitrate;
|
||||
/**
|
||||
* Bitrate for video encoding.
|
||||
*/
|
||||
unsigned int m_audio_bitrate;
|
||||
/**
|
||||
* Framerate for the video, 30 is recommended.
|
||||
*/
|
||||
unsigned int m_record_fps;
|
||||
/**
|
||||
* Jpeg quality for the captured image, from 0 to 100.
|
||||
*/
|
||||
unsigned int m_record_jpg_quality;
|
||||
};
|
||||
|
||||
/* List of opengl function used by libopenglrecorder: */
|
||||
typedef void(*ogrFucReadPixels)(int, int, int, int, unsigned int, unsigned int,
|
||||
void*);
|
||||
typedef void(*ogrFucGenBuffers)(int, unsigned int*);
|
||||
typedef void(*ogrFucBindBuffer)(unsigned int, unsigned int);
|
||||
typedef void(*ogrFucBufferData)(unsigned int, ptrdiff_t, const void*,
|
||||
unsigned int);
|
||||
typedef void(*ogrFucDeleteBuffers)(int, const unsigned int*);
|
||||
typedef void*(*ogrFucMapBuffer)(unsigned int, unsigned int);
|
||||
typedef unsigned char(*ogrFucUnmapBuffer)(unsigned int);
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
/**
|
||||
* Initialize the configuration, call this first before using the library.
|
||||
* \return 1 if succesfully configured, 0 otherwise and a default
|
||||
* configuration will be used.
|
||||
*/
|
||||
int ogrInitConfig(RecorderConfig*);
|
||||
/**
|
||||
* Set the full path with filename for saving the recorded video, excluding
|
||||
* extension, libopenglrecorder will automatically add .webm or .mkv as needed.
|
||||
*/
|
||||
void ogrSetSavedName(const char*);
|
||||
/**
|
||||
* Reset libopenglrecorder, call this before first \ref ogrCapture.
|
||||
*/
|
||||
void ogrPrepareCapture(void);
|
||||
/**
|
||||
* Capture the current frame buffer image as frame, make sure you have called
|
||||
* \ref ogrPrepareCapture first.
|
||||
*/
|
||||
void ogrCapture(void);
|
||||
/**
|
||||
* Stop the recorder of libopenglrecorder.
|
||||
*/
|
||||
void ogrStopCapture(void);
|
||||
/**
|
||||
* Destroy the recorder of libopenglrecorder.
|
||||
*/
|
||||
void ogrDestroy(void);
|
||||
/**
|
||||
* (Optional) Register the callback(s) for \ref GeneralCallback, you have to
|
||||
* make sure the enum CallBackType matches the callback type, see
|
||||
* \ref CallBackType.
|
||||
*/
|
||||
void ogrRegGeneralCallback(CallBackType, GeneralCallback, void*);
|
||||
/**
|
||||
* (Optional) Register the callback(s) for \ref StringCallback, you have to
|
||||
* make sure the enum CallBackType matches the callback type, see
|
||||
* \ref CallBackType.
|
||||
*/
|
||||
void ogrRegStringCallback(CallBackType, StringCallback, void*);
|
||||
/**
|
||||
* (Optional) Register the callback(s) for \ref IntCallback, you have to
|
||||
* make sure the enum CallBackType matches the callback type, see
|
||||
* \ref CallBackType.
|
||||
*/
|
||||
void ogrRegIntCallback(CallBackType, IntCallback, void*);
|
||||
/**
|
||||
* Return 1 if recording is happening in libopenglrecorder, 0 otherwise.
|
||||
*/
|
||||
int ogrCapturing(void);
|
||||
/**
|
||||
* Set opengl function for read pixels (always required).
|
||||
*/
|
||||
void ogrRegReadPixelsFunction(ogrFucReadPixels);
|
||||
/**
|
||||
* Set opengl functions for using PBOs (required if triple buffering is used).
|
||||
*/
|
||||
void ogrRegPBOFunctions(ogrFucGenBuffers, ogrFucBindBuffer, ogrFucBufferData,
|
||||
ogrFucDeleteBuffers, ogrFucMapBuffer,
|
||||
ogrFucUnmapBuffer);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,550 +0,0 @@
|
||||
// SuperTuxKart - a fun racing game with go-kart
|
||||
// Copyright (C) 2017 SuperTuxKart-Team
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 3
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
#if defined(ENABLE_REC_SOUND) && !defined(WIN32)
|
||||
|
||||
#include "capture_library.hpp"
|
||||
#include "recorder_private.hpp"
|
||||
#include "vorbis_encoder.hpp"
|
||||
|
||||
#include <pulse/pulseaudio.h>
|
||||
#include <string>
|
||||
|
||||
#ifndef ENABLE_PULSE_WO_DL
|
||||
#include <dlfcn.h>
|
||||
#endif
|
||||
|
||||
namespace Recorder
|
||||
{
|
||||
// ========================================================================
|
||||
void serverInfoCallBack(pa_context* c, const pa_server_info* i, void* data)
|
||||
{
|
||||
*(std::string*)data = i->default_sink_name;
|
||||
} // serverInfoCallBack
|
||||
// ========================================================================
|
||||
struct PulseAudioData
|
||||
{
|
||||
bool m_loaded;
|
||||
pa_mainloop* m_loop;
|
||||
pa_context* m_context;
|
||||
pa_stream* m_stream;
|
||||
pa_sample_spec m_sample_spec;
|
||||
std::string m_default_sink;
|
||||
#ifndef ENABLE_PULSE_WO_DL
|
||||
void* m_dl_handle;
|
||||
|
||||
typedef pa_stream* (*pa_stream_new_t)(pa_context*, const char*,
|
||||
const pa_sample_spec*, const pa_channel_map*);
|
||||
pa_stream_new_t pa_stream_new;
|
||||
|
||||
typedef int (*pa_stream_connect_record_t)(pa_stream*, const char*,
|
||||
const pa_buffer_attr*, pa_stream_flags_t);
|
||||
pa_stream_connect_record_t pa_stream_connect_record;
|
||||
|
||||
typedef pa_stream_state_t (*pa_stream_get_state_t)(pa_stream*);
|
||||
pa_stream_get_state_t pa_stream_get_state;
|
||||
|
||||
typedef size_t (*pa_stream_readable_size_t)(pa_stream*);
|
||||
pa_stream_readable_size_t pa_stream_readable_size;
|
||||
|
||||
typedef int (*pa_stream_peek_t)(pa_stream*, const void**, size_t*);
|
||||
pa_stream_peek_t pa_stream_peek;
|
||||
|
||||
typedef int (*pa_stream_drop_t)(pa_stream*);
|
||||
pa_stream_drop_t pa_stream_drop;
|
||||
|
||||
typedef int (*pa_stream_disconnect_t)(pa_stream*);
|
||||
pa_stream_disconnect_t pa_stream_disconnect;
|
||||
|
||||
typedef void (*pa_stream_unref_t)(pa_stream*);
|
||||
pa_stream_unref_t pa_stream_unref;
|
||||
|
||||
typedef pa_mainloop* (*pa_mainloop_new_t)(void);
|
||||
pa_mainloop_new_t pa_mainloop_new;
|
||||
|
||||
typedef pa_mainloop_api* (*pa_mainloop_get_api_t)(pa_mainloop*);
|
||||
pa_mainloop_get_api_t pa_mainloop_get_api;
|
||||
|
||||
typedef pa_context* (*pa_context_new_t)(pa_mainloop_api*, const char*);
|
||||
pa_context_new_t pa_context_new;
|
||||
|
||||
typedef int (*pa_context_connect_t)(pa_context*, const char*,
|
||||
pa_context_flags_t, const pa_spawn_api*);
|
||||
pa_context_connect_t pa_context_connect;
|
||||
|
||||
typedef int (*pa_mainloop_iterate_t)(pa_mainloop*, int, int*);
|
||||
pa_mainloop_iterate_t pa_mainloop_iterate;
|
||||
|
||||
typedef pa_context_state_t (*pa_context_get_state_t)(pa_context*);
|
||||
pa_context_get_state_t pa_context_get_state;
|
||||
|
||||
typedef pa_operation* (*pa_context_get_server_info_t)(pa_context*,
|
||||
pa_server_info_cb_t, void*);
|
||||
pa_context_get_server_info_t pa_context_get_server_info;
|
||||
|
||||
typedef pa_operation_state_t (*pa_operation_get_state_t)
|
||||
(pa_operation*);
|
||||
pa_operation_get_state_t pa_operation_get_state;
|
||||
|
||||
typedef void (*pa_operation_unref_t)(pa_operation*);
|
||||
pa_operation_unref_t pa_operation_unref;
|
||||
|
||||
typedef void (*pa_context_disconnect_t)(pa_context*);
|
||||
pa_context_disconnect_t pa_context_disconnect;
|
||||
|
||||
typedef void (*pa_context_unref_t)(pa_context*);
|
||||
pa_context_unref_t pa_context_unref;
|
||||
|
||||
typedef void (*pa_mainloop_free_t)(pa_mainloop*);
|
||||
pa_mainloop_free_t pa_mainloop_free;
|
||||
#endif
|
||||
// --------------------------------------------------------------------
|
||||
PulseAudioData()
|
||||
{
|
||||
m_loaded = false;
|
||||
m_loop = NULL;
|
||||
m_context = NULL;
|
||||
m_stream = NULL;
|
||||
#ifndef ENABLE_PULSE_WO_DL
|
||||
m_dl_handle = NULL;
|
||||
pa_stream_new = NULL;
|
||||
pa_stream_connect_record = NULL;
|
||||
pa_stream_get_state = NULL;
|
||||
pa_stream_readable_size = NULL;
|
||||
pa_stream_peek = NULL;
|
||||
pa_stream_drop = NULL;
|
||||
pa_stream_disconnect = NULL;
|
||||
pa_stream_unref = NULL;
|
||||
pa_mainloop_new = NULL;
|
||||
pa_mainloop_get_api = NULL;
|
||||
pa_context_new = NULL;
|
||||
pa_context_connect = NULL;
|
||||
pa_mainloop_iterate = NULL;
|
||||
pa_context_get_state = NULL;
|
||||
pa_context_get_server_info = NULL;
|
||||
pa_operation_get_state = NULL;
|
||||
pa_operation_unref = NULL;
|
||||
pa_context_disconnect = NULL;
|
||||
pa_context_unref = NULL;
|
||||
pa_mainloop_free = NULL;
|
||||
#endif
|
||||
} // PulseAudioData
|
||||
// --------------------------------------------------------------------
|
||||
#ifndef ENABLE_PULSE_WO_DL
|
||||
bool loadPulseAudioLibrary()
|
||||
{
|
||||
m_dl_handle = dlopen("libpulse.so", RTLD_LAZY);
|
||||
if (m_dl_handle == NULL)
|
||||
{
|
||||
printf("Failed to open PulseAudio library\n");
|
||||
return false;
|
||||
}
|
||||
pa_stream_new = (pa_stream_new_t)dlsym(m_dl_handle,
|
||||
"pa_stream_new");
|
||||
if (pa_stream_new == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_stream_new'\n");
|
||||
return false;
|
||||
}
|
||||
pa_stream_connect_record = (pa_stream_connect_record_t)dlsym
|
||||
(m_dl_handle, "pa_stream_connect_record");
|
||||
if (pa_stream_connect_record == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_stream_connect_record'\n");
|
||||
return false;
|
||||
}
|
||||
pa_stream_get_state = (pa_stream_get_state_t)dlsym(m_dl_handle,
|
||||
"pa_stream_get_state");
|
||||
if (pa_stream_get_state == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_stream_get_state'\n");
|
||||
return false;
|
||||
}
|
||||
pa_stream_readable_size = (pa_stream_readable_size_t)dlsym
|
||||
(m_dl_handle, "pa_stream_readable_size");
|
||||
if (pa_stream_readable_size == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_stream_readable_size'\n");
|
||||
return false;
|
||||
}
|
||||
pa_stream_peek = (pa_stream_peek_t)dlsym(m_dl_handle,
|
||||
"pa_stream_peek");
|
||||
if (pa_stream_peek == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_stream_peek'\n");
|
||||
return false;
|
||||
}
|
||||
pa_stream_drop = (pa_stream_drop_t)dlsym(m_dl_handle,
|
||||
"pa_stream_drop");
|
||||
if (pa_stream_drop == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_stream_drop'\n");
|
||||
return false;
|
||||
}
|
||||
pa_stream_disconnect = (pa_stream_disconnect_t)dlsym(m_dl_handle,
|
||||
"pa_stream_disconnect");
|
||||
if (pa_stream_disconnect == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_stream_disconnect'\n");
|
||||
return false;
|
||||
}
|
||||
pa_stream_unref = (pa_stream_unref_t)dlsym(m_dl_handle,
|
||||
"pa_stream_unref");
|
||||
if (pa_stream_unref == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_stream_unref'\n");
|
||||
return false;
|
||||
}
|
||||
pa_mainloop_new = (pa_mainloop_new_t)dlsym(m_dl_handle,
|
||||
"pa_mainloop_new");
|
||||
if (pa_mainloop_new == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_mainloop_new'\n");
|
||||
return false;
|
||||
}
|
||||
pa_mainloop_get_api = (pa_mainloop_get_api_t)dlsym(m_dl_handle,
|
||||
"pa_mainloop_get_api");
|
||||
if (pa_mainloop_get_api == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_mainloop_get_api'\n");
|
||||
return false;
|
||||
}
|
||||
pa_context_new = (pa_context_new_t)dlsym(m_dl_handle,
|
||||
"pa_context_new");
|
||||
if (pa_context_new == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_context_new'\n");
|
||||
return false;
|
||||
}
|
||||
pa_context_connect = (pa_context_connect_t)dlsym(m_dl_handle,
|
||||
"pa_context_connect");
|
||||
if (pa_context_connect == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_context_connect'\n");
|
||||
return false;
|
||||
}
|
||||
pa_mainloop_iterate = (pa_mainloop_iterate_t)dlsym(m_dl_handle,
|
||||
"pa_mainloop_iterate");
|
||||
if (pa_mainloop_iterate == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_mainloop_iterate'\n");
|
||||
return false;
|
||||
}
|
||||
pa_context_get_state = (pa_context_get_state_t)dlsym(m_dl_handle,
|
||||
"pa_context_get_state");
|
||||
if (pa_context_get_state == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_context_get_state'\n");
|
||||
return false;
|
||||
}
|
||||
pa_context_get_server_info = (pa_context_get_server_info_t)dlsym
|
||||
(m_dl_handle, "pa_context_get_server_info");
|
||||
if (pa_context_get_server_info == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_context_get_server_info'\n");
|
||||
return false;
|
||||
}
|
||||
pa_operation_get_state = (pa_operation_get_state_t)dlsym
|
||||
(m_dl_handle, "pa_operation_get_state");
|
||||
if (pa_operation_get_state == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_operation_get_state'\n");
|
||||
return false;
|
||||
}
|
||||
pa_operation_unref = (pa_operation_unref_t)dlsym(m_dl_handle,
|
||||
"pa_operation_unref");
|
||||
if (pa_operation_unref == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_operation_unref'\n");
|
||||
return false;
|
||||
}
|
||||
pa_context_disconnect = (pa_context_disconnect_t)dlsym(m_dl_handle,
|
||||
"pa_context_disconnect");
|
||||
if (pa_context_disconnect == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_context_disconnect'\n");
|
||||
return false;
|
||||
}
|
||||
pa_context_unref = (pa_context_unref_t)dlsym(m_dl_handle,
|
||||
"pa_context_unref");
|
||||
if (pa_context_unref == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_context_unref'\n");
|
||||
return false;
|
||||
}
|
||||
pa_mainloop_free = (pa_mainloop_free_t)dlsym(m_dl_handle,
|
||||
"pa_mainloop_free");
|
||||
if (pa_mainloop_free == NULL)
|
||||
{
|
||||
printf("Cannot load function 'pa_mainloop_free'\n");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} // loadPulseAudioLibrary
|
||||
#endif
|
||||
// --------------------------------------------------------------------
|
||||
bool load()
|
||||
{
|
||||
#ifndef ENABLE_PULSE_WO_DL
|
||||
if (!loadPulseAudioLibrary())
|
||||
{
|
||||
if (m_dl_handle != NULL)
|
||||
{
|
||||
dlclose(m_dl_handle);
|
||||
m_dl_handle = NULL;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
m_loop = pa_mainloop_new();
|
||||
if (m_loop == NULL)
|
||||
{
|
||||
printf("Failed to create mainloop\n");
|
||||
return false;
|
||||
}
|
||||
m_context = pa_context_new(pa_mainloop_get_api(m_loop),
|
||||
"audioRecord");
|
||||
if (m_context == NULL)
|
||||
{
|
||||
printf("Failed to create context\n");
|
||||
return false;
|
||||
}
|
||||
pa_context_connect(m_context, NULL, PA_CONTEXT_NOAUTOSPAWN , NULL);
|
||||
while (true)
|
||||
{
|
||||
while (pa_mainloop_iterate(m_loop, 0, NULL) > 0);
|
||||
pa_context_state_t state = pa_context_get_state(m_context);
|
||||
if (state == PA_CONTEXT_READY)
|
||||
break;
|
||||
if (!PA_CONTEXT_IS_GOOD(state))
|
||||
{
|
||||
printf("Failed to connect to context\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
pa_operation* pa_op = pa_context_get_server_info(m_context,
|
||||
serverInfoCallBack, &m_default_sink);
|
||||
enum pa_operation_state op_state;
|
||||
while ((op_state =
|
||||
pa_operation_get_state(pa_op)) == PA_OPERATION_RUNNING)
|
||||
pa_mainloop_iterate(m_loop, 0, NULL);
|
||||
pa_operation_unref(pa_op);
|
||||
if (m_default_sink.empty())
|
||||
{
|
||||
printf("Failed to get default sink\n");
|
||||
return false;
|
||||
}
|
||||
m_default_sink += ".monitor";
|
||||
m_sample_spec.format = PA_SAMPLE_S16LE;
|
||||
m_sample_spec.rate = 44100;
|
||||
m_sample_spec.channels = 2;
|
||||
|
||||
m_loaded = true;
|
||||
return true;
|
||||
} // load
|
||||
// --------------------------------------------------------------------
|
||||
void configAudioType(AudioEncoderData* aed)
|
||||
{
|
||||
aed->m_sample_rate = m_sample_spec.rate;
|
||||
aed->m_channels = m_sample_spec.channels;
|
||||
aed->m_audio_type = AudioEncoderData::AT_PCM;
|
||||
} // configAudioType
|
||||
// --------------------------------------------------------------------
|
||||
inline void mainLoopIterate()
|
||||
{
|
||||
while (pa_mainloop_iterate(m_loop, 0, NULL) > 0);
|
||||
} // mainLoopIterate
|
||||
// --------------------------------------------------------------------
|
||||
bool createRecordStream()
|
||||
{
|
||||
assert(m_stream == NULL);
|
||||
m_stream = pa_stream_new(m_context, "input", &m_sample_spec, NULL);
|
||||
if (m_stream == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
pa_buffer_attr buf_attr;
|
||||
const unsigned frag_size = 1024 * m_sample_spec.channels *
|
||||
sizeof(int16_t);
|
||||
buf_attr.fragsize = frag_size;
|
||||
const unsigned max_uint = -1;
|
||||
buf_attr.maxlength = max_uint;
|
||||
buf_attr.minreq = max_uint;
|
||||
buf_attr.prebuf = max_uint;
|
||||
buf_attr.tlength = max_uint;
|
||||
pa_stream_connect_record(m_stream, m_default_sink.c_str(),
|
||||
&buf_attr, (pa_stream_flags_t)(PA_STREAM_ADJUST_LATENCY));
|
||||
while (true)
|
||||
{
|
||||
mainLoopIterate();
|
||||
pa_stream_state_t state = pa_stream_get_state(m_stream);
|
||||
if (state == PA_STREAM_READY)
|
||||
break;
|
||||
if (!PA_STREAM_IS_GOOD(state))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} // createRecordStream
|
||||
// --------------------------------------------------------------------
|
||||
void removeRecordStream()
|
||||
{
|
||||
assert(m_stream != NULL);
|
||||
pa_stream_disconnect(m_stream);
|
||||
pa_stream_unref(m_stream);
|
||||
m_stream = NULL;
|
||||
} // removeRecordStream
|
||||
// --------------------------------------------------------------------
|
||||
inline size_t getReadableSize()
|
||||
{
|
||||
assert(m_stream != NULL);
|
||||
return pa_stream_readable_size(m_stream);
|
||||
} // removeRecordStream
|
||||
// --------------------------------------------------------------------
|
||||
inline void peekStream(const void** data, size_t* bytes)
|
||||
{
|
||||
assert(m_stream != NULL);
|
||||
pa_stream_peek(m_stream, data, bytes);
|
||||
} // peekStream
|
||||
// --------------------------------------------------------------------
|
||||
inline void dropStream()
|
||||
{
|
||||
assert(m_stream != NULL);
|
||||
pa_stream_drop(m_stream);
|
||||
} // dropStream
|
||||
// --------------------------------------------------------------------
|
||||
~PulseAudioData()
|
||||
{
|
||||
if (m_loaded)
|
||||
{
|
||||
if (m_context != NULL)
|
||||
{
|
||||
pa_context_disconnect(m_context);
|
||||
pa_context_unref(m_context);
|
||||
}
|
||||
if (m_loop != NULL)
|
||||
{
|
||||
pa_mainloop_free(m_loop);
|
||||
}
|
||||
#ifndef ENABLE_PULSE_WO_DL
|
||||
if (m_dl_handle != NULL)
|
||||
{
|
||||
dlclose(m_dl_handle);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
} // ~PulseAudioData
|
||||
};
|
||||
// ========================================================================
|
||||
PulseAudioData g_pa_data;
|
||||
// ========================================================================
|
||||
void audioRecorder(CaptureLibrary* cl)
|
||||
{
|
||||
setThreadName("audioRecorder");
|
||||
if (!g_pa_data.m_loaded)
|
||||
{
|
||||
if (!g_pa_data.load())
|
||||
{
|
||||
printf("Cannot load pulseaudio data.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (g_pa_data.createRecordStream() == false)
|
||||
{
|
||||
printf("Failed to create audio record stream.\n");
|
||||
if (g_pa_data.m_stream != NULL)
|
||||
{
|
||||
g_pa_data.removeRecordStream();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
std::list<int8_t*> pcm_data;
|
||||
std::mutex pcm_mutex;
|
||||
std::condition_variable pcm_cv;
|
||||
std::thread audio_enc_thread;
|
||||
|
||||
AudioEncoderData aed;
|
||||
g_pa_data.configAudioType(&aed);
|
||||
aed.m_buf_list = &pcm_data;
|
||||
aed.m_mutex = &pcm_mutex;
|
||||
aed.m_cv = &pcm_cv;
|
||||
aed.m_audio_bitrate = cl->getRecorderConfig().m_audio_bitrate;
|
||||
const unsigned frag_size = 1024 * g_pa_data.m_sample_spec.channels *
|
||||
sizeof(int16_t);
|
||||
|
||||
switch (cl->getRecorderConfig().m_audio_format)
|
||||
{
|
||||
case OGR_AF_VORBIS:
|
||||
audio_enc_thread = std::thread(vorbisEncoder, &aed);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
int8_t* each_pcm_buf = new int8_t[frag_size]();
|
||||
unsigned readed = 0;
|
||||
while (true)
|
||||
{
|
||||
if (cl->getSoundStop())
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(pcm_mutex);
|
||||
pcm_data.push_back(each_pcm_buf);
|
||||
pcm_data.push_back(NULL);
|
||||
pcm_cv.notify_one();
|
||||
break;
|
||||
}
|
||||
g_pa_data.mainLoopIterate();
|
||||
const void* data;
|
||||
size_t bytes;
|
||||
size_t readable = g_pa_data.getReadableSize();
|
||||
if (readable == 0)
|
||||
continue;
|
||||
g_pa_data.peekStream(&data, &bytes);
|
||||
if (data == NULL)
|
||||
{
|
||||
if (bytes > 0)
|
||||
g_pa_data.dropStream();
|
||||
continue;
|
||||
}
|
||||
bool buf_full = readed + (unsigned)bytes > frag_size;
|
||||
unsigned copy_size = buf_full ?
|
||||
frag_size - readed : (unsigned)bytes;
|
||||
memcpy(each_pcm_buf + readed, data, copy_size);
|
||||
if (buf_full)
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(pcm_mutex);
|
||||
pcm_data.push_back(each_pcm_buf);
|
||||
pcm_cv.notify_one();
|
||||
ul.unlock();
|
||||
each_pcm_buf = new int8_t[frag_size]();
|
||||
readed = (unsigned)bytes - copy_size;
|
||||
memcpy(each_pcm_buf, (uint8_t*)data + copy_size, readed);
|
||||
}
|
||||
else
|
||||
{
|
||||
readed += (unsigned)bytes;
|
||||
}
|
||||
g_pa_data.dropStream();
|
||||
}
|
||||
audio_enc_thread.join();
|
||||
g_pa_data.removeRecordStream();
|
||||
} // audioRecorder
|
||||
}
|
||||
#endif
|
@ -1,35 +0,0 @@
|
||||
// SuperTuxKart - a fun racing game with go-kart
|
||||
// Copyright (C) 2017 SuperTuxKart-Team
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 3
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
#ifndef WIN32
|
||||
|
||||
#ifndef HEADER_PULSEAUDIO_RECORD_HPP
|
||||
#define HEADER_PULSEAUDIO_RECORD_HPP
|
||||
|
||||
class CaptureLibrary;
|
||||
namespace Recorder
|
||||
{
|
||||
#ifdef ENABLE_REC_SOUND
|
||||
void audioRecorder(CaptureLibrary* cl);
|
||||
#else
|
||||
inline void audioRecorder(CaptureLibrary* cl) {}
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,340 +0,0 @@
|
||||
#ifdef ENABLE_RECORDER
|
||||
|
||||
#include "capture_library.hpp"
|
||||
#include "recorder_private.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
#include <cstring>
|
||||
|
||||
// ============================================================================
|
||||
ogrFucReadPixels ogrReadPixels = NULL;
|
||||
ogrFucGenBuffers ogrGenBuffers = NULL;
|
||||
ogrFucBindBuffer ogrBindBuffer = NULL;
|
||||
ogrFucBufferData ogrBufferData = NULL;
|
||||
ogrFucDeleteBuffers ogrDeleteBuffers = NULL;
|
||||
ogrFucMapBuffer ogrMapBuffer = NULL;
|
||||
ogrFucUnmapBuffer ogrUnmapBuffer = NULL;
|
||||
// ============================================================================
|
||||
std::unique_ptr<RecorderConfig> g_recorder_config(nullptr);
|
||||
// ============================================================================
|
||||
std::unique_ptr<CaptureLibrary> g_capture_library(nullptr);
|
||||
// ============================================================================
|
||||
std::atomic_bool g_capturing(false);
|
||||
// ============================================================================
|
||||
std::string g_saved_name;
|
||||
// ============================================================================
|
||||
StringCallback g_cb_saved_rec = NULL;
|
||||
// ============================================================================
|
||||
IntCallback g_cb_progress_rec = NULL;
|
||||
// ============================================================================
|
||||
GeneralCallback g_cb_wait_rec = NULL;
|
||||
// ============================================================================
|
||||
GeneralCallback g_cb_start_rec = NULL;
|
||||
// ============================================================================
|
||||
GeneralCallback g_cb_error_rec = NULL;
|
||||
// ============================================================================
|
||||
GeneralCallback g_cb_slow_rec = NULL;
|
||||
// ============================================================================
|
||||
std::array<void*, OGR_CBT_COUNT> g_all_user_data;
|
||||
// ============================================================================
|
||||
bool validateConfig(RecorderConfig* rc)
|
||||
{
|
||||
if (rc == NULL)
|
||||
return false;
|
||||
if (rc->m_triple_buffering > 1 || rc->m_record_audio > 1)
|
||||
return false;
|
||||
if (rc->m_width > 16384 || rc->m_height > 16384)
|
||||
return false;
|
||||
if (rc->m_video_format >= OGR_VF_COUNT ||
|
||||
rc->m_audio_format >= OGR_AF_COUNT)
|
||||
return false;
|
||||
if (rc->m_audio_bitrate == 0 || rc->m_video_bitrate == 0 ||
|
||||
rc->m_record_fps == 0)
|
||||
return false;
|
||||
if (rc->m_record_jpg_quality > 100)
|
||||
return false;
|
||||
return true;
|
||||
} // validateConfig
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
int ogrInitConfig(RecorderConfig* rc)
|
||||
{
|
||||
RecorderConfig* new_rc = new RecorderConfig;
|
||||
g_recorder_config.reset(new_rc);
|
||||
|
||||
if (!validateConfig(rc))
|
||||
{
|
||||
new_rc->m_triple_buffering = 1;
|
||||
new_rc->m_record_audio = 0;
|
||||
new_rc->m_width = 800;
|
||||
new_rc->m_height = 600;
|
||||
new_rc->m_video_format = OGR_VF_MJPEG;
|
||||
new_rc->m_audio_format = OGR_AF_VORBIS;
|
||||
new_rc->m_audio_bitrate = 112000;
|
||||
new_rc->m_video_bitrate = 100000;
|
||||
new_rc->m_record_fps = 30;
|
||||
new_rc->m_record_jpg_quality = 90;
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(new_rc, rc, sizeof(RecorderConfig));
|
||||
while (new_rc->m_width % 8 != 0)
|
||||
{
|
||||
new_rc->m_width--;
|
||||
}
|
||||
while (new_rc->m_height % 2 != 0)
|
||||
{
|
||||
new_rc->m_height--;
|
||||
}
|
||||
return 1;
|
||||
} // ogrInitConfig
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
RecorderConfig* getConfig()
|
||||
{
|
||||
assert(g_recorder_config.get() != nullptr);
|
||||
return g_recorder_config.get();
|
||||
} // getConfig
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void ogrSetSavedName(const char* name)
|
||||
{
|
||||
g_saved_name = name;
|
||||
} // ogrSetSavedName
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
const std::string& getSavedName()
|
||||
{
|
||||
return g_saved_name;
|
||||
} // getSavedName
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void ogrPrepareCapture(void)
|
||||
{
|
||||
assert(g_recorder_config.get() != nullptr && !g_saved_name.empty() &&
|
||||
ogrReadPixels != NULL);
|
||||
if (g_capture_library.get() == nullptr)
|
||||
{
|
||||
assert(g_recorder_config.get() != nullptr);
|
||||
g_capture_library.reset(new CaptureLibrary(getConfig()));
|
||||
}
|
||||
g_capture_library.get()->reset();
|
||||
} // ogrPrepareCapture
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void ogrCapture(void)
|
||||
{
|
||||
g_capture_library.get()->capture();
|
||||
} // ogrCapture
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void ogrStopCapture(void)
|
||||
{
|
||||
g_capture_library.get()->stopCapture();
|
||||
} // ogrStopCapture
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void ogrDestroy(void)
|
||||
{
|
||||
delete g_capture_library.release();
|
||||
} // ogrDestroy
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void ogrRegGeneralCallback(CallBackType cbt, GeneralCallback cb, void* data)
|
||||
{
|
||||
switch (cbt)
|
||||
{
|
||||
case OGR_CBT_ERROR_RECORDING:
|
||||
g_cb_error_rec = cb;
|
||||
g_all_user_data[OGR_CBT_ERROR_RECORDING] = data;
|
||||
break;
|
||||
case OGR_CBT_START_RECORDING:
|
||||
g_cb_start_rec = cb;
|
||||
g_all_user_data[OGR_CBT_START_RECORDING] = data;
|
||||
break;
|
||||
case OGR_CBT_SLOW_RECORDING:
|
||||
g_cb_slow_rec = cb;
|
||||
g_all_user_data[OGR_CBT_SLOW_RECORDING] = data;
|
||||
break;
|
||||
case OGR_CBT_WAIT_RECORDING:
|
||||
g_cb_wait_rec = cb;
|
||||
g_all_user_data[OGR_CBT_WAIT_RECORDING] = data;
|
||||
break;
|
||||
default:
|
||||
assert(false && "Wrong callback enum");
|
||||
break;
|
||||
}
|
||||
} // ogrRegGeneralCallback
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void ogrRegStringCallback(CallBackType cbt, StringCallback cb, void* data)
|
||||
{
|
||||
switch (cbt)
|
||||
{
|
||||
case OGR_CBT_SAVED_RECORDING:
|
||||
g_cb_saved_rec = cb;
|
||||
g_all_user_data[OGR_CBT_SAVED_RECORDING] = data;
|
||||
break;
|
||||
default:
|
||||
assert(false && "Wrong callback enum");
|
||||
break;
|
||||
}
|
||||
} // ogrRegStringCallback
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void ogrRegIntCallback(CallBackType cbt, IntCallback cb, void* data)
|
||||
{
|
||||
switch (cbt)
|
||||
{
|
||||
case OGR_CBT_PROGRESS_RECORDING:
|
||||
g_cb_progress_rec = cb;
|
||||
g_all_user_data[OGR_CBT_PROGRESS_RECORDING] = data;
|
||||
break;
|
||||
default:
|
||||
assert(false && "Wrong callback enum");
|
||||
break;
|
||||
}
|
||||
} // ogrRegIntCallback
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void runCallback(CallBackType cbt, const void* arg)
|
||||
{
|
||||
switch (cbt)
|
||||
{
|
||||
case OGR_CBT_START_RECORDING:
|
||||
{
|
||||
if (g_cb_start_rec == NULL) return;
|
||||
g_cb_start_rec(g_all_user_data[OGR_CBT_START_RECORDING]);
|
||||
break;
|
||||
}
|
||||
case OGR_CBT_SAVED_RECORDING:
|
||||
{
|
||||
if (g_cb_saved_rec == NULL) return;
|
||||
const char* s = (const char*)arg;
|
||||
g_cb_saved_rec(s, g_all_user_data[OGR_CBT_SAVED_RECORDING]);
|
||||
break;
|
||||
}
|
||||
case OGR_CBT_ERROR_RECORDING:
|
||||
{
|
||||
if (g_cb_error_rec == NULL) return;
|
||||
g_cb_error_rec(g_all_user_data[OGR_CBT_ERROR_RECORDING]);
|
||||
break;
|
||||
}
|
||||
case OGR_CBT_PROGRESS_RECORDING:
|
||||
{
|
||||
if (g_cb_progress_rec == NULL) return;
|
||||
const int* i = (const int*)arg;
|
||||
g_cb_progress_rec(*i, g_all_user_data[OGR_CBT_PROGRESS_RECORDING]);
|
||||
break;
|
||||
}
|
||||
case OGR_CBT_WAIT_RECORDING:
|
||||
{
|
||||
if (g_cb_wait_rec == NULL) return;
|
||||
g_cb_wait_rec(g_all_user_data[OGR_CBT_WAIT_RECORDING]);
|
||||
break;
|
||||
}
|
||||
case OGR_CBT_SLOW_RECORDING:
|
||||
{
|
||||
if (g_cb_slow_rec == NULL) return;
|
||||
g_cb_slow_rec(g_all_user_data[OGR_CBT_SLOW_RECORDING]);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} // runCallback
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
int ogrCapturing(void)
|
||||
{
|
||||
return g_capturing.load() ? 1 : 0;
|
||||
} // ogrCapturing
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void ogrRegReadPixelsFunction(ogrFucReadPixels read_pixels)
|
||||
{
|
||||
assert(read_pixels != NULL);
|
||||
ogrReadPixels = read_pixels;
|
||||
} // ogrRegReadPixelsFunction
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void ogrRegPBOFunctions(ogrFucGenBuffers gen_buffers,
|
||||
ogrFucBindBuffer bind_buffer,
|
||||
ogrFucBufferData buffer_data,
|
||||
ogrFucDeleteBuffers delete_buffers,
|
||||
ogrFucMapBuffer map_buffer,
|
||||
ogrFucUnmapBuffer unmap_buffer)
|
||||
{
|
||||
assert(gen_buffers != NULL);
|
||||
ogrGenBuffers = gen_buffers;
|
||||
assert(bind_buffer != NULL);
|
||||
ogrBindBuffer = bind_buffer;
|
||||
assert(buffer_data != NULL);
|
||||
ogrBufferData = buffer_data;
|
||||
assert(delete_buffers != NULL);
|
||||
ogrDeleteBuffers = delete_buffers;
|
||||
assert(map_buffer != NULL);
|
||||
ogrMapBuffer = map_buffer;
|
||||
assert(unmap_buffer != NULL);
|
||||
ogrUnmapBuffer = unmap_buffer;
|
||||
} // ogrRegPBOFunctions
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void setCapturing(bool val)
|
||||
{
|
||||
g_capturing.store(val);
|
||||
} // setCapturing
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
/** This function sets the name of this thread in the debugger.
|
||||
* \param name Name of the thread.
|
||||
*/
|
||||
#if defined(_MSC_VER) && defined(DEBUG)
|
||||
# define WIN32_LEAN_AND_MEAN
|
||||
# include <windows.h>
|
||||
|
||||
void setThreadName(const char *name)
|
||||
{
|
||||
const DWORD MS_VC_EXCEPTION=0x406D1388;
|
||||
#pragma pack(push,8)
|
||||
typedef struct tagTHREADNAME_INFO
|
||||
{
|
||||
DWORD dwType; // Must be 0x1000.
|
||||
LPCSTR szName; // Pointer to name (in user addr space).
|
||||
DWORD dwThreadID; // Thread ID (-1=caller thread).
|
||||
DWORD dwFlags; // Reserved for future use, must be zero.
|
||||
} THREADNAME_INFO;
|
||||
#pragma pack(pop)
|
||||
|
||||
THREADNAME_INFO info;
|
||||
info.dwType = 0x1000;
|
||||
info.szName = name;
|
||||
info.dwThreadID = -1;
|
||||
info.dwFlags = 0;
|
||||
|
||||
__try
|
||||
{
|
||||
RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR),
|
||||
(ULONG_PTR*)&info );
|
||||
}
|
||||
__except(EXCEPTION_EXECUTE_HANDLER)
|
||||
{
|
||||
}
|
||||
|
||||
} // setThreadName
|
||||
#elif defined(__linux__) && defined(__GLIBC__) && defined(__GLIBC_MINOR__)
|
||||
void setThreadName(const char* name)
|
||||
{
|
||||
#if __GLIBC__ > 2 || __GLIBC_MINOR__ > 11
|
||||
pthread_setname_np(pthread_self(), name);
|
||||
#endif
|
||||
} // setThreadName
|
||||
#else
|
||||
void setThreadName(const char* name)
|
||||
{
|
||||
} // setThreadName
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,25 +0,0 @@
|
||||
#ifdef ENABLE_RECORDER
|
||||
#ifndef HEADER_RECORDER_PRIVATE_HPP
|
||||
#define HEADER_RECORDER_PRIVATE_HPP
|
||||
|
||||
#include "openglrecorder.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
extern ogrFucReadPixels ogrReadPixels;
|
||||
extern ogrFucGenBuffers ogrGenBuffers;
|
||||
extern ogrFucBindBuffer ogrBindBuffer;
|
||||
extern ogrFucBufferData ogrBufferData;
|
||||
extern ogrFucDeleteBuffers ogrDeleteBuffers;
|
||||
extern ogrFucMapBuffer ogrMapBuffer;
|
||||
extern ogrFucUnmapBuffer ogrUnmapBuffer;
|
||||
|
||||
RecorderConfig* getConfig();
|
||||
const std::string& getSavedName();
|
||||
void setCapturing(bool val);
|
||||
void setThreadName(const char* name);
|
||||
void runCallback(CallBackType cbt, const void* arg);
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,145 +0,0 @@
|
||||
// SuperTuxKart - a fun racing game with go-kart
|
||||
// Copyright (C) 2017 SuperTuxKart-Team
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 3
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
#ifdef ENABLE_RECORDER
|
||||
|
||||
#include "capture_library.hpp"
|
||||
#include "recorder_private.hpp"
|
||||
|
||||
#include <ogg/ogg.h>
|
||||
#include <vorbis/vorbisenc.h>
|
||||
|
||||
namespace Recorder
|
||||
{
|
||||
void vorbisEncoder(AudioEncoderData* aed)
|
||||
{
|
||||
setThreadName("vorbisEncoder");
|
||||
vorbis_info vi;
|
||||
vorbis_dsp_state vd;
|
||||
vorbis_block vb;
|
||||
vorbis_info_init(&vi);
|
||||
vorbis_encode_init(&vi, aed->m_channels, aed->m_sample_rate, -1,
|
||||
aed->m_audio_bitrate, -1);
|
||||
vorbis_analysis_init(&vd, &vi);
|
||||
vorbis_block_init(&vd, &vb);
|
||||
vorbis_comment vc;
|
||||
vorbis_comment_init(&vc);
|
||||
vorbis_comment_add_tag(&vc, "Encoder",
|
||||
"Vorbis encoder by libopenglrecorder");
|
||||
ogg_packet header;
|
||||
ogg_packet header_comm;
|
||||
ogg_packet header_code;
|
||||
vorbis_analysis_headerout(&vd, &vc, &header, &header_comm,
|
||||
&header_code);
|
||||
if (header.bytes > 255 || header_comm.bytes > 255)
|
||||
{
|
||||
printf("Header is too long for vorbis.\n");
|
||||
return;
|
||||
}
|
||||
FILE* vb_data = fopen((getSavedName() + ".audio").c_str(), "wb");
|
||||
if (vb_data == NULL)
|
||||
{
|
||||
printf("Failed to open file for encoding vorbis.\n");
|
||||
return;
|
||||
}
|
||||
fwrite(&aed->m_sample_rate, 1, sizeof(uint32_t), vb_data);
|
||||
fwrite(&aed->m_channels, 1, sizeof(uint32_t), vb_data);
|
||||
const uint32_t all = header.bytes + header_comm.bytes +
|
||||
header_code.bytes + 3;
|
||||
fwrite(&all, 1, sizeof(uint32_t), vb_data);
|
||||
uint8_t size = 2;
|
||||
fwrite(&size, 1, sizeof(uint8_t), vb_data);
|
||||
size = (uint8_t)header.bytes;
|
||||
fwrite(&size, 1, sizeof(uint8_t), vb_data);
|
||||
size = (uint8_t)header_comm.bytes;
|
||||
fwrite(&size, 1, sizeof(uint8_t), vb_data);
|
||||
fwrite(header.packet, 1, header.bytes, vb_data);
|
||||
fwrite(header_comm.packet, 1, header_comm.bytes, vb_data);
|
||||
fwrite(header_code.packet, 1, header_code.bytes, vb_data);
|
||||
ogg_packet op;
|
||||
int64_t last_timestamp = 0;
|
||||
bool eos = false;
|
||||
while (eos == false)
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(*aed->m_mutex);
|
||||
aed->m_cv->wait(ul, [&aed] { return !aed->m_buf_list->empty(); });
|
||||
int8_t* audio_buf = aed->m_buf_list->front();
|
||||
aed->m_buf_list->pop_front();
|
||||
ul.unlock();
|
||||
if (audio_buf == NULL)
|
||||
{
|
||||
vorbis_analysis_wrote(&vd, 0);
|
||||
eos = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
float **buffer = vorbis_analysis_buffer(&vd, 1024);
|
||||
const unsigned channels = aed->m_channels;
|
||||
if (aed->m_audio_type == AudioEncoderData::AT_PCM)
|
||||
{
|
||||
for (unsigned j = 0; j < channels; j++)
|
||||
{
|
||||
for (unsigned i = 0; i < 1024; i++)
|
||||
{
|
||||
int8_t* each_channel =
|
||||
&audio_buf[i * channels * 2 + j * 2];
|
||||
buffer[j][i] = float((each_channel[1] << 8) |
|
||||
(0x00ff & (int)each_channel[0])) / 32768.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (unsigned j = 0; j < channels; j++)
|
||||
{
|
||||
for (unsigned i = 0; i < 1024; i++)
|
||||
{
|
||||
float* fbuf = reinterpret_cast<float*>(audio_buf);
|
||||
buffer[j][i] = fbuf[i * channels + j];
|
||||
}
|
||||
}
|
||||
}
|
||||
vorbis_analysis_wrote(&vd, 1024);
|
||||
}
|
||||
while (vorbis_analysis_blockout(&vd, &vb) == 1)
|
||||
{
|
||||
vorbis_analysis(&vb, NULL);
|
||||
vorbis_bitrate_addblock(&vb);
|
||||
while (vorbis_bitrate_flushpacket(&vd, &op))
|
||||
{
|
||||
if (op.granulepos > 0)
|
||||
{
|
||||
uint32_t frame_size = (uint32_t)op.bytes;
|
||||
fwrite(&frame_size, 1, sizeof(uint32_t), vb_data);
|
||||
fwrite(&last_timestamp, 1, sizeof(int64_t), vb_data);
|
||||
fwrite(op.packet, 1, frame_size, vb_data);
|
||||
double s = (double)op.granulepos /
|
||||
(double)aed->m_sample_rate * 1000000000.;
|
||||
last_timestamp = (int64_t)s;
|
||||
}
|
||||
}
|
||||
}
|
||||
delete[] audio_buf;
|
||||
}
|
||||
vorbis_block_clear(&vb);
|
||||
vorbis_dsp_clear(&vd);
|
||||
vorbis_comment_clear(&vc);
|
||||
vorbis_info_clear(&vi);
|
||||
fclose(vb_data);
|
||||
} // vorbisEncoder
|
||||
}
|
||||
#endif
|
@ -1,31 +0,0 @@
|
||||
// SuperTuxKart - a fun racing game with go-kart
|
||||
// Copyright (C) 2017 SuperTuxKart-Team
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 3
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
#ifdef ENABLE_RECORDER
|
||||
|
||||
#ifndef HEADER_VORBIS_ENCODE_HPP
|
||||
#define HEADER_VORBIS_ENCODE_HPP
|
||||
|
||||
struct AudioEncoderData;
|
||||
namespace Recorder
|
||||
{
|
||||
void vorbisEncoder(AudioEncoderData* aed);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,169 +0,0 @@
|
||||
// SuperTuxKart - a fun racing game with go-kart
|
||||
// Copyright (C) 2017 SuperTuxKart-Team
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 3
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
#if defined(ENABLE_RECORDER) && !defined(NO_VPX)
|
||||
|
||||
#include "capture_library.hpp"
|
||||
#include "recorder_private.hpp"
|
||||
|
||||
#include <vpx/vpx_encoder.h>
|
||||
#include <vpx/vp8cx.h>
|
||||
|
||||
namespace Recorder
|
||||
{
|
||||
// ------------------------------------------------------------------------
|
||||
int vpxEncodeFrame(vpx_codec_ctx_t *codec, vpx_image_t *img,
|
||||
int frame_index, FILE *out)
|
||||
{
|
||||
int got_pkts = 0;
|
||||
vpx_codec_iter_t iter = NULL;
|
||||
const vpx_codec_cx_pkt_t *pkt = NULL;
|
||||
const vpx_codec_err_t res = vpx_codec_encode(codec, img, frame_index,
|
||||
1, 0, VPX_DL_REALTIME);
|
||||
if (res != VPX_CODEC_OK)
|
||||
{
|
||||
printf("Failed to encode frame\n");
|
||||
return -1;
|
||||
}
|
||||
while ((pkt = vpx_codec_get_cx_data(codec, &iter)) != NULL)
|
||||
{
|
||||
got_pkts = 1;
|
||||
if (pkt->kind == VPX_CODEC_CX_FRAME_PKT)
|
||||
{
|
||||
fwrite(&pkt->data.frame.sz, 1, sizeof(uint32_t), out);
|
||||
fwrite(&pkt->data.frame.pts, 1, sizeof(int64_t), out);
|
||||
fwrite(&pkt->data.frame.flags, 1,
|
||||
sizeof(vpx_codec_frame_flags_t), out);
|
||||
fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, out);
|
||||
}
|
||||
}
|
||||
return got_pkts;
|
||||
} // vpxEncodeFrame
|
||||
// ------------------------------------------------------------------------
|
||||
void vpxEncoder(CaptureLibrary* cl)
|
||||
{
|
||||
setThreadName("vpxEncoder");
|
||||
FILE* vpx_data = fopen((getSavedName() + ".video").c_str(), "wb");
|
||||
if (vpx_data == NULL)
|
||||
{
|
||||
printf("Failed to open file for writing vpx.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
vpx_codec_ctx_t codec;
|
||||
vpx_codec_enc_cfg_t cfg;
|
||||
vpx_codec_iface_t* codec_if = NULL;
|
||||
switch (cl->getRecorderConfig().m_video_format)
|
||||
{
|
||||
case OGR_VF_VP8:
|
||||
codec_if = vpx_codec_vp8_cx();
|
||||
break;
|
||||
case OGR_VF_VP9:
|
||||
codec_if = vpx_codec_vp9_cx();
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
break;
|
||||
}
|
||||
vpx_codec_err_t res = vpx_codec_enc_config_default(codec_if, &cfg, 0);
|
||||
if (res > 0)
|
||||
{
|
||||
printf("Failed to get default vpx codec config.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
const unsigned width = cl->getRecorderConfig().m_width;
|
||||
const unsigned height = cl->getRecorderConfig().m_height;
|
||||
int frames_encoded = 0;
|
||||
cfg.g_w = width;
|
||||
cfg.g_h = height;
|
||||
cfg.g_timebase.num = 1;
|
||||
cfg.g_timebase.den = cl->getRecorderConfig().m_record_fps;
|
||||
cfg.rc_end_usage = VPX_VBR;
|
||||
cfg.rc_target_bitrate = cl->getRecorderConfig().m_video_bitrate;
|
||||
|
||||
if (vpx_codec_enc_init(&codec, codec_if, &cfg, 0) > 0)
|
||||
{
|
||||
printf("Failed to initialize vpx encoder\n");
|
||||
fclose(vpx_data);
|
||||
return;
|
||||
}
|
||||
float last_size = -1.0f;
|
||||
int cur_finished_count = 0;
|
||||
while (true)
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(*cl->getJPGListMutex());
|
||||
cl->getJPGListCV()->wait(ul, [&cl]
|
||||
{ return !cl->getJPGList()->empty(); });
|
||||
auto& p = cl->getJPGList()->front();
|
||||
uint8_t* jpg = std::get<0>(p);
|
||||
uint32_t jpg_size = std::get<1>(p);
|
||||
int frame_count = std::get<2>(p);
|
||||
if (jpg == NULL)
|
||||
{
|
||||
cl->getJPGList()->clear();
|
||||
ul.unlock();
|
||||
if (cl->displayingProgress())
|
||||
{
|
||||
int rate = 100;
|
||||
runCallback(OGR_CBT_PROGRESS_RECORDING, &rate);
|
||||
}
|
||||
break;
|
||||
}
|
||||
cl->getJPGList()->pop_front();
|
||||
ul.unlock();
|
||||
if (cl->displayingProgress())
|
||||
{
|
||||
if (last_size == -1.0f)
|
||||
last_size = (float)(cl->getJPGList()->size());
|
||||
cur_finished_count += frame_count;
|
||||
int rate = (int)(cur_finished_count / last_size * 100.0f);
|
||||
rate = rate > 100 ? 100 : rate;
|
||||
runCallback(OGR_CBT_PROGRESS_RECORDING, &rate);
|
||||
}
|
||||
uint8_t* yuv = NULL;
|
||||
unsigned yuv_size;
|
||||
int ret = cl->yuvConversion(jpg, jpg_size, &yuv,
|
||||
&yuv_size);
|
||||
if (ret < 0)
|
||||
{
|
||||
delete [] yuv;
|
||||
tjFree(jpg);
|
||||
continue;
|
||||
}
|
||||
assert(yuv_size != 0);
|
||||
tjFree(jpg);
|
||||
vpx_image_t each_frame;
|
||||
vpx_img_wrap(&each_frame, VPX_IMG_FMT_I420, width, height, 1, yuv);
|
||||
while (frame_count != 0)
|
||||
{
|
||||
vpxEncodeFrame(&codec, &each_frame, frames_encoded++, vpx_data);
|
||||
frame_count--;
|
||||
}
|
||||
delete [] yuv;
|
||||
}
|
||||
|
||||
while (vpxEncodeFrame(&codec, NULL, -1, vpx_data));
|
||||
if (vpx_codec_destroy(&codec))
|
||||
{
|
||||
printf("Failed to destroy vpx codec.\n");
|
||||
return;
|
||||
}
|
||||
fclose(vpx_data);
|
||||
} // vpxEncoder
|
||||
}
|
||||
#endif
|
@ -1,36 +0,0 @@
|
||||
|
||||
// SuperTuxKart - a fun racing game with go-kart
|
||||
// Copyright (C) 2017 SuperTuxKart-Team
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 3
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
#ifdef ENABLE_RECORDER
|
||||
|
||||
#ifndef HEADER_VPX_ENCODER_HPP
|
||||
#define HEADER_VPX_ENCODER_HPP
|
||||
|
||||
class CaptureLibrary;
|
||||
|
||||
namespace Recorder
|
||||
{
|
||||
#ifdef NO_VPX
|
||||
inline void vpxEncoder(CaptureLibrary* cl) {}
|
||||
#else
|
||||
void vpxEncoder(CaptureLibrary* cl);
|
||||
#endif
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,308 +0,0 @@
|
||||
// SuperTuxKart - a fun racing game with go-kart
|
||||
// Copyright (C) 2017 SuperTuxKart-Team
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 3
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
#if defined(ENABLE_REC_SOUND) && defined(WIN32)
|
||||
|
||||
#include "capture_library.hpp"
|
||||
#include "recorder_private.hpp"
|
||||
#include "vorbis_encoder.hpp"
|
||||
|
||||
#include <audioclient.h>
|
||||
#include <mmsystem.h>
|
||||
#include <mmreg.h>
|
||||
#include <mmdeviceapi.h>
|
||||
#include <windows.h>
|
||||
|
||||
#if defined (__MINGW32__) || defined(__CYGWIN__)
|
||||
#include <stdint.h>
|
||||
inline GUID uuidFromString(const char* s)
|
||||
{
|
||||
unsigned long p0;
|
||||
unsigned int p1, p2, p3, p4, p5, p6, p7, p8, p9, p10;
|
||||
sscanf(s, "%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
|
||||
&p0, &p1, &p2, &p3, &p4, &p5, &p6, &p7, &p8, &p9, &p10);
|
||||
GUID g = { p0, (uint16_t)p1, (uint16_t)p2, { (uint8_t)p3, (uint8_t)p4,
|
||||
(uint8_t)p5, (uint8_t)p6, (uint8_t)p7, (uint8_t)p8, (uint8_t)p9,
|
||||
(uint8_t)p10 }};
|
||||
return g;
|
||||
}
|
||||
#undef KSDATAFORMAT_SUBTYPE_PCM
|
||||
#define KSDATAFORMAT_SUBTYPE_PCM \
|
||||
uuidFromString("00000001-0000-0010-8000-00aa00389b71")
|
||||
#undef KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
|
||||
#define KSDATAFORMAT_SUBTYPE_IEEE_FLOAT \
|
||||
uuidFromString("00000003-0000-0010-8000-00aa00389b71")
|
||||
#endif
|
||||
|
||||
namespace Recorder
|
||||
{
|
||||
// ========================================================================
|
||||
const REFERENCE_TIME REFTIMES_PER_SEC = 10000000;
|
||||
// ========================================================================
|
||||
struct WasapiData
|
||||
{
|
||||
bool m_loaded;
|
||||
IMMDeviceEnumerator* m_dev_enum;
|
||||
IMMDevice* m_dev;
|
||||
IAudioClient* m_client;
|
||||
IAudioCaptureClient* m_capture_client;
|
||||
WAVEFORMATEX* m_wav_format;
|
||||
uint32_t m_buffer_size;
|
||||
// --------------------------------------------------------------------
|
||||
WasapiData()
|
||||
{
|
||||
m_loaded = false;
|
||||
m_dev_enum = NULL;
|
||||
m_dev = NULL;
|
||||
m_client = NULL;
|
||||
m_capture_client = NULL;
|
||||
m_wav_format = NULL;
|
||||
} // WasapiData
|
||||
// --------------------------------------------------------------------
|
||||
bool load()
|
||||
{
|
||||
HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL,
|
||||
CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
|
||||
(void**)&m_dev_enum);
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
hr = m_dev_enum->GetDefaultAudioEndpoint(eRender, eConsole,
|
||||
&m_dev);
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
hr = m_dev->Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL,
|
||||
(void**)&m_client);
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
hr = m_client->GetMixFormat(&m_wav_format);
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
hr = m_client->Initialize(AUDCLNT_SHAREMODE_SHARED,
|
||||
AUDCLNT_STREAMFLAGS_LOOPBACK, REFTIMES_PER_SEC, 0,
|
||||
m_wav_format, NULL);
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
hr = m_client->GetBufferSize(&m_buffer_size);
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
hr = m_client->GetService(__uuidof(IAudioCaptureClient),
|
||||
(void**)&m_capture_client);
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
m_loaded = true;
|
||||
return true;
|
||||
} // load
|
||||
// --------------------------------------------------------------------
|
||||
~WasapiData()
|
||||
{
|
||||
if (m_loaded)
|
||||
{
|
||||
CoTaskMemFree(m_wav_format);
|
||||
if (m_dev_enum)
|
||||
m_dev_enum->Release();
|
||||
if (m_dev)
|
||||
m_dev->Release();
|
||||
if (m_client)
|
||||
m_client->Release();
|
||||
if (m_capture_client)
|
||||
m_capture_client->Release();
|
||||
}
|
||||
} // ~WasapiData
|
||||
};
|
||||
// ========================================================================
|
||||
WasapiData g_wasapi_data;
|
||||
// ========================================================================
|
||||
void audioRecorder(CaptureLibrary* cl)
|
||||
{
|
||||
setThreadName("audioRecorder");
|
||||
if (!g_wasapi_data.m_loaded)
|
||||
{
|
||||
if (!g_wasapi_data.load())
|
||||
{
|
||||
printf("Failed to load wasapi data.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
AudioEncoderData aed = {};
|
||||
if (g_wasapi_data.m_wav_format->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
|
||||
{
|
||||
WAVEFORMATEXTENSIBLE* wav_for_ext =
|
||||
(WAVEFORMATEXTENSIBLE*)g_wasapi_data.m_wav_format;
|
||||
aed.m_channels = wav_for_ext->Format.nChannels;
|
||||
aed.m_sample_rate = wav_for_ext->Format.nSamplesPerSec;
|
||||
if (IsEqualGUID(KSDATAFORMAT_SUBTYPE_PCM, wav_for_ext->SubFormat))
|
||||
{
|
||||
aed.m_audio_type = AudioEncoderData::AT_PCM;
|
||||
if (wav_for_ext->Format.wBitsPerSample != 16)
|
||||
{
|
||||
printf("Only 16bit PCM is supported.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (IsEqualGUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, wav_for_ext
|
||||
->SubFormat))
|
||||
{
|
||||
aed.m_audio_type = AudioEncoderData::AT_FLOAT;
|
||||
if (wav_for_ext->Format.wBitsPerSample != 32)
|
||||
{
|
||||
printf("Only 32bit float is supported.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Unsupported audio input format.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (g_wasapi_data.m_wav_format->wFormatTag == WAVE_FORMAT_PCM)
|
||||
{
|
||||
aed.m_channels = g_wasapi_data.m_wav_format->nChannels;
|
||||
aed.m_sample_rate = g_wasapi_data.m_wav_format->nSamplesPerSec;
|
||||
aed.m_audio_type = AudioEncoderData::AT_PCM;
|
||||
if (g_wasapi_data.m_wav_format->wBitsPerSample != 16)
|
||||
{
|
||||
printf("Only 16bit PCM is supported.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Unsupported audio input format\n");
|
||||
return;
|
||||
}
|
||||
if (aed.m_sample_rate > 48000)
|
||||
{
|
||||
printf("Only support maximum 48000hz sample rate audio.\n");
|
||||
return;
|
||||
}
|
||||
HRESULT hr = g_wasapi_data.m_client->Reset();
|
||||
if (FAILED(hr))
|
||||
{
|
||||
printf("Failed to reset audio recorder.\n");
|
||||
return;
|
||||
}
|
||||
hr = g_wasapi_data.m_client->Start();
|
||||
if (FAILED(hr))
|
||||
{
|
||||
printf("Failed to start audio recorder.\n");
|
||||
return;
|
||||
}
|
||||
REFERENCE_TIME duration = REFTIMES_PER_SEC *
|
||||
g_wasapi_data.m_buffer_size / g_wasapi_data.m_wav_format
|
||||
->nSamplesPerSec;
|
||||
|
||||
std::list<int8_t*> audio_data;
|
||||
std::mutex audio_mutex;
|
||||
std::condition_variable audio_cv;
|
||||
std::thread audio_enc_thread;
|
||||
aed.m_buf_list = &audio_data;
|
||||
aed.m_mutex = &audio_mutex;
|
||||
aed.m_cv = &audio_cv;
|
||||
aed.m_audio_bitrate = cl->getRecorderConfig().m_audio_bitrate;
|
||||
|
||||
switch (cl->getRecorderConfig().m_audio_format)
|
||||
{
|
||||
case OGR_AF_VORBIS:
|
||||
audio_enc_thread = std::thread(vorbisEncoder, &aed);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
const unsigned frag_size = 1024 * aed.m_channels *
|
||||
(g_wasapi_data.m_wav_format->wBitsPerSample / 8);
|
||||
int8_t* each_audio_buf = new int8_t[frag_size]();
|
||||
unsigned readed = 0;
|
||||
while (true)
|
||||
{
|
||||
if (cl->getSoundStop())
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(audio_mutex);
|
||||
audio_data.push_back(each_audio_buf);
|
||||
audio_data.push_back(NULL);
|
||||
audio_cv.notify_one();
|
||||
break;
|
||||
}
|
||||
uint32_t packet_length = 0;
|
||||
hr = g_wasapi_data.m_capture_client->GetNextPacketSize(
|
||||
&packet_length);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
printf("Failed to get next audio packet size\n");
|
||||
}
|
||||
if (packet_length == 0)
|
||||
{
|
||||
REFERENCE_TIME sleep_time = duration / 10000 / 2;
|
||||
Sleep((uint32_t)sleep_time);
|
||||
continue;
|
||||
}
|
||||
BYTE* data;
|
||||
DWORD flags;
|
||||
hr = g_wasapi_data.m_capture_client->GetBuffer(&data,
|
||||
&packet_length, &flags, NULL, NULL);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
printf("Failed to get audio buffer\n");
|
||||
}
|
||||
const unsigned bytes = aed.m_channels * (g_wasapi_data.m_wav_format
|
||||
->wBitsPerSample / 8) * packet_length;
|
||||
bool buf_full = readed + bytes > frag_size;
|
||||
unsigned copy_size = buf_full ? frag_size - readed : bytes;
|
||||
if (!(flags & AUDCLNT_BUFFERFLAGS_SILENT))
|
||||
{
|
||||
memcpy(each_audio_buf + readed, data, copy_size);
|
||||
}
|
||||
if (buf_full)
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(audio_mutex);
|
||||
audio_data.push_back(each_audio_buf);
|
||||
audio_cv.notify_one();
|
||||
ul.unlock();
|
||||
each_audio_buf = new int8_t[frag_size]();
|
||||
readed = (unsigned)bytes - copy_size;
|
||||
if (!(flags & AUDCLNT_BUFFERFLAGS_SILENT))
|
||||
{
|
||||
memcpy(each_audio_buf, (uint8_t*)data + copy_size, readed);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
readed += bytes;
|
||||
}
|
||||
hr = g_wasapi_data.m_capture_client->ReleaseBuffer(packet_length);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
printf("Failed to release audio buffer\n");
|
||||
}
|
||||
}
|
||||
hr = g_wasapi_data.m_client->Stop();
|
||||
if (FAILED(hr))
|
||||
{
|
||||
printf("Failed to stop audio recorder\n");
|
||||
}
|
||||
audio_enc_thread.join();
|
||||
} // audioRecorder
|
||||
}
|
||||
#endif
|
@ -1,35 +0,0 @@
|
||||
// SuperTuxKart - a fun racing game with go-kart
|
||||
// Copyright (C) 2017 SuperTuxKart-Team
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 3
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
#ifdef WIN32
|
||||
|
||||
#ifndef HEADER_WASAPI_RECORD_HPP
|
||||
#define HEADER_WASAPI_RECORD_HPP
|
||||
|
||||
class CaptureLibrary;
|
||||
namespace Recorder
|
||||
{
|
||||
#ifdef ENABLE_REC_SOUND
|
||||
void audioRecorder(CaptureLibrary* cl);
|
||||
#else
|
||||
inline void audioRecorder(CaptureLibrary* cl) {}
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user