Move SoundConsumer to libmedia in the BPrivate namespace

Signed-off-by: Jérôme Duval <[email protected]>
This commit is contained in:
Hamish Morrison
2012-08-07 23:38:37 +02:00
committed by Jérôme Duval
parent d96e687652
commit 59b4be8cc1
8 changed files with 46 additions and 47 deletions
+151
View File
@@ -0,0 +1,151 @@
/*******************************************************************************
/
/ File: SoundConsumer.h
/
/ Description: Record sound from some sound-producing Node.
/
/ Copyright 1998, Be Incorporated, All Rights Reserved
/
*******************************************************************************/
#ifndef SOUND_CONSUMER_H
#define SOUND_CONSUMER_H
// To use this Consumer:
// 1. Create Record and Notify hooks, or subclass SoundConsumer
// if you'd rather use the inheritance hierarchy.
// * The Record function should do whatever you want to do
// when you receive a buffer.
// * The Notify function should handle whatever events
// you wish to handle (defined in SoundUtil.h).
// 2: Create an instance of SoundConsumer, giving it the
// appropriate hook functions. Or, create an instance of an
// appropriate subclass if you've made one.
// 3: Register your new Consumer with the MediaRoster.
// 4: Connect your Consumer to some Producer.
// 5: Start or Stop the Consumer if your hook functions
// implement behavior for these kinds of events.
// Seek the Consumer to set the offset of the timestamps that
// your Record function will see.
// 6: When you're done, disconnect the Consumer, then delete it.
#include <BufferConsumer.h>
#include "SoundUtils.h"
namespace BPrivate {
class SoundConsumer : public BBufferConsumer {
public:
SoundConsumer(const char* name,
SoundProcessFunc recordFunc = NULL,
SoundNotifyFunc notifyFunc = NULL,
void* cookie = NULL);
virtual ~SoundConsumer();
//This function is OK to call from any thread.
status_t SetHooks(SoundProcessFunc recordFunc = NULL,
SoundNotifyFunc notifyFunc = NULL,
void* cookie = NULL);
// The MediaNode interface
public:
virtual port_id ControlPort() const;
virtual BMediaAddOn* AddOn(int32* internalID) const;
// Who instantiated you -- or NULL for app class
protected:
virtual void Start(bigtime_t performanceTime);
virtual void Stop(bigtime_t performanceTime, bool immediate);
virtual void Seek(bigtime_t mediaTime,
bigtime_t performanceTime);
virtual void SetRunMode(run_mode mode);
virtual void TimeWarp(bigtime_t atRealTime,
bigtime_t to_performanceTime);
virtual void Preroll();
virtual void SetTimeSource(BTimeSource* timeSource);
virtual status_t HandleMessage(int32 message, const void* data,
size_t size);
// The BufferConsumer interface
virtual status_t AcceptFormat(const media_destination& dest,
media_format* format);
virtual status_t GetNextInput(int32* cookie,
media_input* _input);
virtual void DisposeInputCookie(int32 cookie);
virtual void BufferReceived(BBuffer* buffer);
virtual void ProducerDataStatus(
const media_destination& forWhom,
int32 status, bigtime_t atMediaTime);
virtual status_t GetLatencyFor(const media_destination& forWhom,
bigtime_t* _latency,
media_node_id* _timesource);
virtual status_t Connected(const media_source& producer,
const media_destination& where,
const media_format& format,
media_input* _input);
virtual void Disconnected(const media_source& producer,
const media_destination& where);
virtual status_t FormatChanged(const media_source& producer,
const media_destination& consumer,
int32 fromChangeCount,
const media_format& format);
protected:
// Functions called when no hooks are installed.
// OK to override instead of installing hooks.
virtual void Record(bigtime_t time, const void* data,
size_t size,
const media_raw_audio_format& format);
virtual void Notify(int32 cause, ...);
private:
SoundProcessFunc m_recordHook;
SoundNotifyFunc m_notifyHook;
void* m_cookie;
media_input m_input;
thread_id m_thread;
port_id m_port;
// The times we need to deal with
// My notation for times: tr = real time,
// tp = performance time, tm = media time.
bigtime_t m_trTimeout;
// how long to wait on the input port
bigtime_t m_tpSeekAt;
// when we Seek
bigtime_t m_tmSeekTo;
// target time for Seek
// The transformation from media to peformance time.
// d = p - m, so m + d = p.
// Media time is generally governed by the Seek
// function. In our node, we simply use media time as
// the time that we report to the record hook function.
// If we were a producer node, we might use media time
// to track where we were in playing a certain piece
// of media. But we aren't.
bigtime_t m_delta;
// State variables
bool m_seeking;
// a Seek is pending
// Functions to calculate timing values. OK to override.
// ProcessingLatency is the time it takes to process a buffer;
// TotalLatency is returned to producer; Timeout is passed
// to call to read_port_etc() in service thread loop.
virtual bigtime_t Timeout();
virtual bigtime_t ProcessingLatency();
virtual bigtime_t TotalLatency();
// The actual thread doing the work
static status_t ThreadEntry(void* cookie);
void ServiceThread();
void DoHookChange(void* message);
};
}
#endif // SOUND_CONSUMER_H
+56
View File
@@ -0,0 +1,56 @@
/*******************************************************************************
/
/ File: SoundUtils.h
/
/ Description: Utility functions for handling audio data.
/
/ Copyright 1998-1999, Be Incorporated, All Rights Reserved
/
*******************************************************************************/
#if ! defined( _SoundUtils_h )
#define _SoundUtils_h
#include <MediaDefs.h>
// Simple helper functions that come in handy when doing
// buffer calculations.
double us_to_s(bigtime_t usecs);
bigtime_t s_to_us(double secs);
int bytes_per_frame(const media_raw_audio_format & format);
int frames_per_buffer(const media_raw_audio_format & format);
bigtime_t buffer_duration(const media_raw_audio_format & format);
bigtime_t frames_duration(const media_raw_audio_format & format,
int64 num_frames);
int64 frames_for_duration(const media_raw_audio_format & format,
bigtime_t duration);
int buffers_for_duration(const media_raw_audio_format & format,
bigtime_t duration);
// This is a common hook function interface for
// SoundConsumer and SoundProducer to use.
typedef void (*SoundProcessFunc)(void * cookie,
bigtime_t timestamp, void * data, size_t datasize,
const media_raw_audio_format & format);
typedef void (*SoundNotifyFunc)(void * cookie,
int32 code, ...);
// These are special codes that we use in the Notify
// function hook.
enum {
B_WILL_START = 1, // performance_time
B_WILL_STOP, // performance_time immediate
B_WILL_SEEK, // performance_time media_time
B_WILL_TIMEWARP, // real_time performance_time
B_CONNECTED, // name (char*)
B_DISCONNECTED, //
B_FORMAT_CHANGED, // media_raw_audio_format*
B_NODE_DIES, // node will die!
B_HOOKS_CHANGED, //
B_OP_TIMED_OUT, // timeout that expired -- Consumer only
B_PRODUCER_DATA_STATUS, // status performance_time -- Consumer only
B_LATE_NOTICE // how_much performance_time -- Producer only
};
#endif /* _SoundUtils_h */