2 * Copyright 2014-2016, Dario Casalinuovo
3 * Copyright 1999, Be Incorporated
5 * This file may be used under the terms of the Be Sample Code License.
9 #include "MediaRecorderNode.h"
12 #include <scheduler.h>
13 #include <MediaRoster.h>
14 #include <MediaRosterEx.h>
15 #include <TimedEventQueue.h>
16 #include <TimeSource.h>
18 #include "MediaDebug.h"
21 BMediaRecorderNode::BMediaRecorderNode(const char* name
,
22 BMediaRecorder
* recorder
, media_type type
)
26 BBufferConsumer(type
),
33 fInput
.destination
.id
= 1;
34 fInput
.destination
.port
= ControlPort();
40 strcpy(fInput
.name
, str
.String());
44 BMediaRecorderNode::~BMediaRecorderNode()
51 BMediaRecorderNode::AddOn(int32
* id
) const
63 BMediaRecorderNode::NodeRegistered()
71 BMediaRecorderNode::SetRunMode(run_mode mode
)
77 if (mode
== BMediaNode::B_OFFLINE
)
78 priority
= B_OFFLINE_PROCESSING
;
80 switch(ConsumerType()) {
81 case B_MEDIA_RAW_AUDIO
:
82 case B_MEDIA_ENCODED_AUDIO
:
83 priority
= B_AUDIO_RECORDING
;
86 case B_MEDIA_RAW_VIDEO
:
87 case B_MEDIA_ENCODED_VIDEO
:
88 priority
= B_VIDEO_RECORDING
;
92 priority
= B_DEFAULT_MEDIA_PRIORITY
;
96 SetPriority(suggest_thread_priority(priority
));
98 BMediaNode::SetRunMode(mode
);
103 BMediaRecorderNode::SetAcceptedFormat(const media_format
& format
)
107 fInput
.format
= format
;
113 BMediaRecorderNode::AcceptedFormat() const
117 return fInput
.format
;
122 BMediaRecorderNode::GetInput(media_input
* outInput
)
126 fInput
.node
= Node();
132 BMediaRecorderNode::SetDataEnabled(bool enabled
)
138 SetOutputEnabled(fInput
.source
,
139 fInput
.destination
, enabled
, NULL
, &tag
);
144 BMediaRecorderNode::ActivateInternalConnect(bool connectMode
)
146 fConnectMode
= connectMode
;
151 BMediaRecorderNode::HandleEvent(const media_timed_event
* event
,
152 bigtime_t lateness
, bool realTimeEvent
)
156 // we ignore them all!
161 BMediaRecorderNode::Start(bigtime_t performanceTime
)
165 if (fRecorder
->fNotifyHook
)
166 (*fRecorder
->fNotifyHook
)(fRecorder
->fBufferCookie
,
167 BMediaRecorder::B_WILL_START
, performanceTime
);
169 fRecorder
->fRunning
= true;
174 BMediaRecorderNode::Stop(bigtime_t performanceTime
, bool immediate
)
178 if (fRecorder
->fNotifyHook
)
179 (*fRecorder
->fNotifyHook
)(fRecorder
->fBufferCookie
,
180 BMediaRecorder::B_WILL_STOP
, performanceTime
, immediate
);
182 fRecorder
->fRunning
= false;
187 BMediaRecorderNode::Seek(bigtime_t mediaTime
, bigtime_t performanceTime
)
191 if (fRecorder
->fNotifyHook
)
192 (*fRecorder
->fNotifyHook
)(fRecorder
->fBufferCookie
,
193 BMediaRecorder::B_WILL_SEEK
, performanceTime
, mediaTime
);
198 BMediaRecorderNode::TimeWarp(bigtime_t realTime
, bigtime_t performanceTime
)
202 // Since buffers will come pre-time-stamped, we only need to look
203 // at them, so we can ignore the time warp as a consumer.
204 if (fRecorder
->fNotifyHook
)
205 (*fRecorder
->fNotifyHook
)(fRecorder
->fBufferCookie
,
206 BMediaRecorder::B_WILL_TIMEWARP
, realTime
, performanceTime
);
211 BMediaRecorderNode::HandleMessage(int32 message
,
212 const void* data
, size_t size
)
216 if (BBufferConsumer::HandleMessage(message
, data
, size
) < 0
217 && BMediaEventLooper::HandleMessage(message
, data
, size
) < 0
218 && BMediaNode::HandleMessage(message
, data
, size
) < 0) {
219 HandleBadMessage(message
, data
, size
);
227 BMediaRecorderNode::AcceptFormat(const media_destination
& dest
,
228 media_format
* format
)
232 if (format_is_compatible(*format
, fOKFormat
))
237 return B_MEDIA_BAD_FORMAT
;
242 BMediaRecorderNode::GetNextInput(int32
* cookie
, media_input
* outInput
)
257 BMediaRecorderNode::DisposeInputCookie(int32 cookie
)
264 BMediaRecorderNode::BufferReceived(BBuffer
* buffer
)
268 fRecorder
->BufferReceived(buffer
->Data(), buffer
->SizeUsed(),
276 BMediaRecorderNode::ProducerDataStatus(
277 const media_destination
& forWhom
, int32 status
,
278 bigtime_t performanceTime
)
285 BMediaRecorderNode::GetLatencyFor(const media_destination
& forWhom
,
286 bigtime_t
* outLatency
, media_node_id
* outTimesource
)
291 *outTimesource
= TimeSource()->ID();
298 BMediaRecorderNode::Connected(const media_source
&producer
,
299 const media_destination
&where
, const media_format
&withFormat
,
300 media_input
* outInput
)
304 fInput
.source
= producer
;
305 fInput
.format
= withFormat
;
308 if (fConnectMode
== true) {
309 // This is a workaround needed for us to get the node
310 // so that our owner class can do it's operations.
312 BMediaRosterEx
* roster
= MediaRosterEx(BMediaRoster::CurrentRoster());
313 if (roster
->GetNodeFor(roster
->NodeIDFor(producer
.port
), &node
) != B_OK
)
314 return B_MEDIA_BAD_NODE
;
316 fRecorder
->fOutputNode
= node
;
317 fRecorder
->fReleaseOutputNode
= true;
319 fRecorder
->SetUpConnection(producer
);
320 fRecorder
->fConnected
= true;
327 BMediaRecorderNode::Disconnected(const media_source
& producer
,
328 const media_destination
& where
)
332 fInput
.source
= media_source::null
;
333 // Reset the connection mode
335 fRecorder
->fConnected
= false;
336 fInput
.format
= fOKFormat
;
341 BMediaRecorderNode::FormatChanged(const media_source
& producer
,
342 const media_destination
& consumer
, int32 tag
,
343 const media_format
& format
)
347 if (!format_is_compatible(format
, fOKFormat
))
348 return B_MEDIA_BAD_FORMAT
;
350 fInput
.format
= format
;