2 * Copyright (c) 2004-2007 Marcus Overhagen <marcus@overhagen.de>
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify,
8 * merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
28 #include <TimeSource.h>
29 #include <MediaRoster.h>
30 #include <BufferGroup.h>
36 #include "VideoNode.h"
37 #include "VideoView.h"
40 overlay_copy(uint32 lines
, void *dst
, uint32 dst_bpr
, const void *src
,
43 // bigtime_t start = system_time();
44 int len
= min_c(dst_bpr
, src_bpr
);
45 // int len4 = len / 4;
48 // this does not copy the last few bytes, if length is not aligned
52 : "c" (len4), "S" (src), "D" (dst)
56 const uint32 *s4 = (const uint32 *)src;
57 uint32 *d4 = (uint32 *)dst;
58 for (int i = 0; i < len4; i++)
62 const uint8 *s1 = (const uint8 *)s4;
63 uint8 *d1 = (uint8 *)d4;
68 memcpy(dst
, src
, len
);
69 src
= (char *)src
+ src_bpr
;
70 dst
= (char *)dst
+ dst_bpr
;
72 // printf("overlay copy: %.06f sec\n", (system_time() - start) / 1000000.0);
76 VideoNode::VideoNode(const char *name
, VideoView
*view
)
79 , BBufferConsumer(B_MEDIA_RAW_VIDEO
)
82 , fOverlayEnabled(true)
83 , fOverlayActive(false)
84 , fDirectOverlayBuffer(false)
86 , fBitmapLocker(new BLocker("Video Node Locker"))
91 VideoNode::~VideoNode()
100 VideoNode::AddOn(int32
*internal_id
) const
108 VideoNode::NodeRegistered()
110 fInput
.node
= Node();
111 fInput
.source
= media_source::null
;
112 fInput
.destination
.port
= ControlPort();
113 fInput
.destination
.id
= 0;
114 fInput
.format
.type
= B_MEDIA_RAW_VIDEO
;
115 fInput
.format
.u
.raw_video
= media_raw_video_format::wildcard
;
116 strcpy(fInput
.name
, "video in");
118 SetPriority(B_DISPLAY_PRIORITY
);
124 VideoNode::BufferReceived(BBuffer
* buffer
)
126 if (RunState() != B_STARTED
) {
130 if (fOverlayActive
&& fDirectOverlayBuffer
) {
131 HandleBuffer(buffer
);
133 media_timed_event
event(buffer
->Header()->start_time
,
134 BTimedEventQueue::B_HANDLE_BUFFER
,
136 BTimedEventQueue::B_RECYCLE_BUFFER
);
137 EventQueue()->AddEvent(event
);
143 VideoNode::GetNextInput(int32
*cookie
, media_input
*out_input
)
155 VideoNode::DisposeInputCookie(int32 cookie
)
162 VideoNode:: HandleMessage(int32 message
,
166 if (BBufferConsumer::HandleMessage(message
, data
, size
) == B_OK
167 || BMediaEventLooper::HandleMessage(message
, data
, size
) == B_OK
)
175 VideoNode::HandleEvent(const media_timed_event
*event
,
179 switch (event
->type
) {
180 case BTimedEventQueue::B_START
:
182 case BTimedEventQueue::B_STOP
:
183 EventQueue()->FlushEvents(event
->event_time
,
184 BTimedEventQueue::B_ALWAYS
, true,
185 BTimedEventQueue::B_HANDLE_BUFFER
);
187 case BTimedEventQueue::B_HANDLE_BUFFER
:
188 HandleBuffer((BBuffer
*)event
->pointer
);
191 printf("VideoNode::HandleEvent unknown event");
198 VideoNode::ProducerDataStatus(const media_destination
&dst
,
200 bigtime_t at_media_time
)
207 VideoNode::GetLatencyFor(const media_destination
&dst
,
208 bigtime_t
*out_latency
,
209 media_node_id
*out_id
)
211 if (dst
!= fInput
.destination
)
212 return B_MEDIA_BAD_DESTINATION
;
214 *out_latency
= 10000;
215 *out_id
= TimeSource()->ID();
220 VideoNode::AcceptFormat(const media_destination
&dst
,
221 media_format
*format
)
223 /* The connection process:
224 * BBufferProducer::FormatProposal
225 * we are here => BBufferConsumer::AcceptFormat
226 * BBufferProducer::PrepareToConnect
227 * BBufferConsumer::Connected
228 * BBufferProducer::Connect
231 if (dst
!= fInput
.destination
)
232 return B_MEDIA_BAD_DESTINATION
;
234 if (format
->type
== B_MEDIA_NO_TYPE
)
235 format
->type
= B_MEDIA_RAW_VIDEO
;
237 if (format
->type
!= B_MEDIA_RAW_VIDEO
)
238 return B_MEDIA_BAD_FORMAT
;
246 VideoNode::Connected(const media_source
&src
,
247 const media_destination
&dst
,
248 const media_format
&format
,
249 media_input
*out_input
)
251 /* The connection process:
252 * BBufferProducer::FormatProposal
253 * BBufferConsumer::AcceptFormat
254 * BBufferProducer::PrepareToConnect
255 * we are here => BBufferConsumer::Connected
256 * BBufferProducer::Connect
259 if (dst
!= fInput
.destination
)
260 return B_MEDIA_BAD_DESTINATION
;
263 fInput
.format
= format
;
265 if (fInput
.format
.u
.raw_video
.field_rate
< 1.0)
266 fInput
.format
.u
.raw_video
.field_rate
= 25.0;
268 color_space colorspace
= format
.u
.raw_video
.display
.format
;
269 BRect
frame(0, 0, format
.u
.raw_video
.display
.line_width
- 1,
270 format
.u
.raw_video
.display
.line_count
- 1);
274 err
= CreateBuffers(frame
, colorspace
, fOverlayEnabled
);
276 printf("VideoNode::Connected failed, fOverlayEnabled = %d\n",
289 VideoNode::Disconnected(const media_source
&src
,
290 const media_destination
&dst
)
292 if (src
!= fInput
.source
)
294 if (dst
!= fInput
.destination
)
299 // disconnect the connection
300 fInput
.source
= media_source::null
;
305 VideoNode::FormatChanged(const media_source
&src
,
306 const media_destination
&dst
,
307 int32 from_change_count
,
308 const media_format
&format
)
310 printf("VideoNode::FormatChanged enter\n");
311 if (src
!= fInput
.source
)
312 return B_MEDIA_BAD_SOURCE
;
313 if (dst
!= fInput
.destination
)
314 return B_MEDIA_BAD_DESTINATION
;
316 color_space colorspace
= format
.u
.raw_video
.display
.format
;
317 BRect
frame(0, 0, format
.u
.raw_video
.display
.line_width
- 1,
318 format
.u
.raw_video
.display
.line_count
- 1);
322 if (fOverlayEnabled
) {
323 fVideoView
->RemoveOverlay();
324 err
= CreateBuffers(frame
, colorspace
, true); // try overlay
326 printf("VideoNode::FormatChanged creating overlay buffer "
328 err
= CreateBuffers(frame
, colorspace
, false); // no overlay
331 err
= CreateBuffers(frame
, colorspace
, false); // no overlay
334 printf("VideoNode::FormatChanged failed (lost buffer group!)\n");
335 return B_MEDIA_BAD_FORMAT
;
338 fInput
.format
= format
;
340 printf("VideoNode::FormatChanged leave\n");
346 VideoNode::HandleBuffer(BBuffer
*buffer
)
348 // printf("VideoNode::HandleBuffer\n");
352 // bigtime_t start = system_time();
353 if (fOverlayActive
) {
354 if (B_OK
== fBitmap
->LockBits()) {
356 // memcpy(fBitmap->Bits(), buffer->Data(), fBitmap->BitsLength());
358 // fBitmap->SetBits(buffer->Data(), fBitmap->BitsLength(), 0,
359 // fInput.format.u.raw_video.display.format);
361 overlay_copy(fBitmap
->Bounds().IntegerHeight() + 1,
362 fBitmap
->Bits(), fBitmap
->BytesPerRow(),
364 fInput
.format
.u
.raw_video
.display
.bytes_per_row
);
367 fBitmap
->UnlockBits();
370 // memcpy(fBitmap->Bits(), buffer->Data(), fBitmap->BitsLength());
372 overlay_copy(fBitmap
->Bounds().IntegerHeight() + 1,
373 fBitmap
->Bits(), fBitmap
->BytesPerRow(),
375 fInput
.format
.u
.raw_video
.display
.bytes_per_row
);
377 // printf("overlay copy: %Ld usec\n", system_time() - start);
383 fVideoView
->DrawFrame();
388 VideoNode::SetOverlayEnabled(bool yesno
)
390 fOverlayEnabled
= yesno
;
395 VideoNode::LockBitmap()
397 fBitmapLocker
->Lock();
409 VideoNode::UnlockBitmap()
411 fBitmapLocker
->Unlock();
416 VideoNode::IsOverlayActive()
418 return fOverlayActive
;
423 VideoNode::CreateBuffers(BRect frame
, color_space cspace
, bool overlay
)
425 printf("VideoNode::CreateBuffers: frame %d,%d,%d,%d colorspace 0x%08x, "
426 "overlay %d\n", int(frame
.left
), int(frame
.top
), int(frame
.right
),
427 int(frame
.bottom
), int(cspace
), overlay
);
429 // int32 bytesPerRow = B_ANY_BYTES_PER_ROW;
430 // if (cspace == B_YCbCr422)
431 // bytesPerRow = (int(frame.Width()) + 1) * 2;
433 // printf("overlay bitmap: requesting: bytes per row: %d\n", bytesPerRow);
436 ASSERT(fBitmap
== 0);
437 uint32 flags
= overlay
? (B_BITMAP_WILL_OVERLAY
438 | B_BITMAP_RESERVE_OVERLAY_CHANNEL
) : 0;
439 // fBitmap = new BBitmap(frame, flags, cspace, bytesPerRow);
440 fBitmap
= new BBitmap(frame
, flags
, cspace
);
441 if (!(fBitmap
&& fBitmap
->InitCheck() == B_OK
&& fBitmap
->IsValid())) {
444 fOverlayActive
= false;
446 printf("VideoNode::CreateBuffers failed\n");
449 printf("overlay bitmap: got: bytes per row: %" B_PRId32
"\n",
450 fBitmap
->BytesPerRow());
451 fOverlayActive
= overlay
;
453 printf("VideoNode::CreateBuffers success\n");
459 VideoNode::DeleteBuffers()