obs-studio/libobs/util/serializer.h

165 lines
3.7 KiB
C
Raw Normal View History

/*
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
2013-09-30 19:37:13 -07:00
#pragma once
2013-09-30 19:37:13 -07:00
#include "c99defs.h"
2013-09-30 19:37:13 -07:00
/*
* General programmable serialization functions. (A shared interface to
* various reading/writing to/from different inputs/outputs)
*/
#ifdef __cplusplus
extern "C" {
#endif
enum serialize_seek_type {
SERIALIZE_SEEK_START,
SERIALIZE_SEEK_CURRENT,
SERIALIZE_SEEK_END
};
struct serializer {
void *data;
size_t (*read)(void *, void *, size_t);
size_t (*write)(void *, const void *, size_t);
uint64_t (*seek)(void *, int64_t, enum serialize_seek_type);
uint64_t (*get_pos)(void *);
2013-09-30 19:37:13 -07:00
};
static inline size_t s_read(struct serializer *s, void *data, size_t size)
{
if (s && s->read && data && size)
return s->read(s->data, (void*)data, size);
return 0;
}
static inline size_t s_write(struct serializer *s, const void *data,
size_t size)
{
if (s && s->write && data && size)
return s->write(s->data, (void*)data, size);
return 0;
}
2013-09-30 19:37:13 -07:00
static inline size_t serialize(struct serializer *s, void *data, size_t len)
{
if (s) {
if (s->write)
Implement RTMP module (still needs drop code) - Implement the RTMP output module. This time around, we just use a simple FLV muxer, then just write to the stream with RTMP_Write. Easy and effective. - Fix the FLV muxer, the muxer now outputs proper FLV packets. - Output API: * When using encoders, automatically interleave encoded packets before sending it to the output. * Pair encoders and have them automatically wait for the other to start to ensure sync. * Change 'obs_output_signal_start_fail' to 'obs_output_signal_stop' because it was a bit confusing, and doing this makes a lot more sense for outputs that need to stop suddenly (disconnections/etc). - Encoder API: * Remove some unnecessary encoder functions from the actual API and make them internal. Most of the encoder functions are handled automatically by outputs anyway, so there's no real need to expose them and end up inadvertently confusing plugin writers. * Have audio encoders wait for the video encoder to get a frame, then start at the exact data point that the first video frame starts to ensure the most accrate sync of video/audio possible. * Add a required 'frame_size' callback for audio encoders that returns the expected number of frames desired to encode with. This way, the libobs encoder API can handle the circular buffering internally automatically for the encoder modules, so encoder writers don't have to do it themselves. - Fix a few bugs in the serializer interface. It was passing the wrong variable for the data in a few cases. - If a source has video, make obs_source_update defer the actual update callback until the tick function is called to prevent threading issues.
2014-04-07 22:00:10 -07:00
return s->write(s->data, data, len);
else if (s->read)
Implement RTMP module (still needs drop code) - Implement the RTMP output module. This time around, we just use a simple FLV muxer, then just write to the stream with RTMP_Write. Easy and effective. - Fix the FLV muxer, the muxer now outputs proper FLV packets. - Output API: * When using encoders, automatically interleave encoded packets before sending it to the output. * Pair encoders and have them automatically wait for the other to start to ensure sync. * Change 'obs_output_signal_start_fail' to 'obs_output_signal_stop' because it was a bit confusing, and doing this makes a lot more sense for outputs that need to stop suddenly (disconnections/etc). - Encoder API: * Remove some unnecessary encoder functions from the actual API and make them internal. Most of the encoder functions are handled automatically by outputs anyway, so there's no real need to expose them and end up inadvertently confusing plugin writers. * Have audio encoders wait for the video encoder to get a frame, then start at the exact data point that the first video frame starts to ensure the most accrate sync of video/audio possible. * Add a required 'frame_size' callback for audio encoders that returns the expected number of frames desired to encode with. This way, the libobs encoder API can handle the circular buffering internally automatically for the encoder modules, so encoder writers don't have to do it themselves. - Fix a few bugs in the serializer interface. It was passing the wrong variable for the data in a few cases. - If a source has video, make obs_source_update defer the actual update callback until the tick function is called to prevent threading issues.
2014-04-07 22:00:10 -07:00
return s->read(s->data, data, len);
}
2013-09-30 19:37:13 -07:00
return 0;
}
static inline uint64_t serializer_seek(struct serializer *s, int64_t offset,
enum serialize_seek_type seek_type)
{
if (s && s->seek)
Implement RTMP module (still needs drop code) - Implement the RTMP output module. This time around, we just use a simple FLV muxer, then just write to the stream with RTMP_Write. Easy and effective. - Fix the FLV muxer, the muxer now outputs proper FLV packets. - Output API: * When using encoders, automatically interleave encoded packets before sending it to the output. * Pair encoders and have them automatically wait for the other to start to ensure sync. * Change 'obs_output_signal_start_fail' to 'obs_output_signal_stop' because it was a bit confusing, and doing this makes a lot more sense for outputs that need to stop suddenly (disconnections/etc). - Encoder API: * Remove some unnecessary encoder functions from the actual API and make them internal. Most of the encoder functions are handled automatically by outputs anyway, so there's no real need to expose them and end up inadvertently confusing plugin writers. * Have audio encoders wait for the video encoder to get a frame, then start at the exact data point that the first video frame starts to ensure the most accrate sync of video/audio possible. * Add a required 'frame_size' callback for audio encoders that returns the expected number of frames desired to encode with. This way, the libobs encoder API can handle the circular buffering internally automatically for the encoder modules, so encoder writers don't have to do it themselves. - Fix a few bugs in the serializer interface. It was passing the wrong variable for the data in a few cases. - If a source has video, make obs_source_update defer the actual update callback until the tick function is called to prevent threading issues.
2014-04-07 22:00:10 -07:00
return s->seek(s->data, offset, seek_type);
2013-09-30 19:37:13 -07:00
return 0;
}
static inline uint64_t serializer_get_pos(struct serializer *s)
2013-09-30 19:37:13 -07:00
{
if (s && s->get_pos)
Implement RTMP module (still needs drop code) - Implement the RTMP output module. This time around, we just use a simple FLV muxer, then just write to the stream with RTMP_Write. Easy and effective. - Fix the FLV muxer, the muxer now outputs proper FLV packets. - Output API: * When using encoders, automatically interleave encoded packets before sending it to the output. * Pair encoders and have them automatically wait for the other to start to ensure sync. * Change 'obs_output_signal_start_fail' to 'obs_output_signal_stop' because it was a bit confusing, and doing this makes a lot more sense for outputs that need to stop suddenly (disconnections/etc). - Encoder API: * Remove some unnecessary encoder functions from the actual API and make them internal. Most of the encoder functions are handled automatically by outputs anyway, so there's no real need to expose them and end up inadvertently confusing plugin writers. * Have audio encoders wait for the video encoder to get a frame, then start at the exact data point that the first video frame starts to ensure the most accrate sync of video/audio possible. * Add a required 'frame_size' callback for audio encoders that returns the expected number of frames desired to encode with. This way, the libobs encoder API can handle the circular buffering internally automatically for the encoder modules, so encoder writers don't have to do it themselves. - Fix a few bugs in the serializer interface. It was passing the wrong variable for the data in a few cases. - If a source has video, make obs_source_update defer the actual update callback until the tick function is called to prevent threading issues.
2014-04-07 22:00:10 -07:00
return s->get_pos(s->data);
2013-09-30 19:37:13 -07:00
return 0;
}
/* formatted this to be similar to the AVIO layout that ffmpeg uses */
static inline void s_w8(struct serializer *s, uint8_t u8)
{
s_write(s, &u8, sizeof(uint8_t));
}
static inline void s_wl16(struct serializer *s, uint16_t u16)
{
s_w8(s, (uint8_t)u16);
s_w8(s, u16 >> 8);
}
static inline void s_wl24(struct serializer *s, uint32_t u24)
{
s_w8 (s, (uint8_t)u24);
s_wl16(s, (uint16_t)(u24 >> 8));
}
static inline void s_wl32(struct serializer *s, uint32_t u32)
2013-09-30 19:37:13 -07:00
{
s_wl16(s, (uint16_t)u32);
s_wl16(s, (uint16_t)(u32 >> 16));
2013-09-30 19:37:13 -07:00
}
static inline void s_wl64(struct serializer *s, uint64_t u64)
2013-09-30 19:37:13 -07:00
{
s_wl32(s, (uint32_t)u64);
s_wl32(s, (uint32_t)(u64 >> 32));
2013-09-30 19:37:13 -07:00
}
static inline void s_wlf(struct serializer *s, float f)
2013-09-30 19:37:13 -07:00
{
s_wl32(s, *(uint32_t*)&f);
2013-09-30 19:37:13 -07:00
}
static inline void s_wld(struct serializer *s, double d)
2013-09-30 19:37:13 -07:00
{
s_wl64(s, *(uint64_t*)&d);
2013-09-30 19:37:13 -07:00
}
static inline void s_wb16(struct serializer *s, uint16_t u16)
2013-09-30 19:37:13 -07:00
{
s_w8(s, u16 >> 8);
s_w8(s, (uint8_t)u16);
2013-09-30 19:37:13 -07:00
}
static inline void s_wb24(struct serializer *s, uint32_t u24)
2013-09-30 19:37:13 -07:00
{
s_wb16(s, (uint16_t)(u24 >> 8));
s_w8 (s, (uint8_t)u24);
2013-09-30 19:37:13 -07:00
}
static inline void s_wb32(struct serializer *s, uint32_t u32)
2013-09-30 19:37:13 -07:00
{
s_wb16(s, (uint16_t)(u32 >> 16));
s_wb16(s, (uint16_t)u32);
2013-09-30 19:37:13 -07:00
}
static inline void s_wb64(struct serializer *s, uint64_t u64)
2013-09-30 19:37:13 -07:00
{
s_wb32(s, (uint32_t)(u64 >> 32));
s_wb32(s, (uint32_t)u64);
2013-09-30 19:37:13 -07:00
}
static inline void s_wbf(struct serializer *s, float f)
2013-09-30 19:37:13 -07:00
{
s_wb32(s, *(uint32_t*)&f);
2013-09-30 19:37:13 -07:00
}
static inline void s_wbd(struct serializer *s, double d)
2013-09-30 19:37:13 -07:00
{
s_wb64(s, *(uint64_t*)&d);
2013-09-30 19:37:13 -07:00
}
#ifdef __cplusplus
}
#endif