diff options
Diffstat (limited to 'ppapi/cpp')
-rw-r--r-- | ppapi/cpp/media_stream_video_track.cc | 99 | ||||
-rw-r--r-- | ppapi/cpp/media_stream_video_track.h | 122 | ||||
-rw-r--r-- | ppapi/cpp/video_frame.cc | 77 | ||||
-rw-r--r-- | ppapi/cpp/video_frame.h | 75 |
4 files changed, 373 insertions, 0 deletions
diff --git a/ppapi/cpp/media_stream_video_track.cc b/ppapi/cpp/media_stream_video_track.cc new file mode 100644 index 0000000..e33333e --- /dev/null +++ b/ppapi/cpp/media_stream_video_track.cc @@ -0,0 +1,99 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ppapi/cpp/media_stream_video_track.h" + +#include "ppapi/c/pp_errors.h" +#include "ppapi/c/ppb_media_stream_video_track.h" +#include "ppapi/cpp/completion_callback.h" +#include "ppapi/cpp/module_impl.h" +#include "ppapi/cpp/var.h" +#include "ppapi/cpp/video_frame.h" + +namespace pp { + +namespace { + +template <> const char* interface_name<PPB_MediaStreamVideoTrack_0_1>() { + return PPB_MEDIASTREAMVIDEOTRACK_INTERFACE_0_1; +} + +} // namespace + +MediaStreamVideoTrack::MediaStreamVideoTrack() { +} + +MediaStreamVideoTrack::MediaStreamVideoTrack( + const MediaStreamVideoTrack& other) : Resource(other) { +} + +MediaStreamVideoTrack::MediaStreamVideoTrack(const Resource& resource) + : Resource(resource) { + PP_DCHECK(IsMediaStreamVideoTrack(resource)); +} + +MediaStreamVideoTrack::MediaStreamVideoTrack(PassRef, PP_Resource resource) + : Resource(PASS_REF, resource) { +} + +MediaStreamVideoTrack::~MediaStreamVideoTrack() { +} + +int32_t MediaStreamVideoTrack::Configure(uint32_t frame_buffer_size) { + if (has_interface<PPB_MediaStreamVideoTrack_0_1>()) { + return get_interface<PPB_MediaStreamVideoTrack_0_1>()->Configure( + pp_resource(), frame_buffer_size); + } + return PP_ERROR_NOINTERFACE; +} + +std::string MediaStreamVideoTrack::GetId() const { + if (has_interface<PPB_MediaStreamVideoTrack_0_1>()) { + pp::Var id(PASS_REF, get_interface<PPB_MediaStreamVideoTrack_0_1>()->GetId( + pp_resource())); + if (id.is_string()) + return id.AsString(); + } + return std::string(); +} + +bool MediaStreamVideoTrack::HasEnded() const { + if (has_interface<PPB_MediaStreamVideoTrack_0_1>()) { + return PP_ToBool(get_interface<PPB_MediaStreamVideoTrack_0_1>()->HasEnded( + pp_resource())); + } + return true; +} + +int32_t MediaStreamVideoTrack::GetFrame( + const CompletionCallbackWithOutput<VideoFrame>& cc) { + if (has_interface<PPB_MediaStreamVideoTrack_0_1>()) { + return get_interface<PPB_MediaStreamVideoTrack_0_1>()->GetFrame( + pp_resource(), cc.output(), cc.pp_completion_callback()); + } + return cc.MayForce(PP_ERROR_NOINTERFACE); +} + +int32_t MediaStreamVideoTrack::RecycleFrame(const VideoFrame& frame) { + if (has_interface<PPB_MediaStreamVideoTrack_0_1>()) { + return get_interface<PPB_MediaStreamVideoTrack_0_1>()->RecycleFrame( + pp_resource(), frame.pp_resource()); + } + return PP_ERROR_NOINTERFACE; +} + +void MediaStreamVideoTrack::Close() { + if (has_interface<PPB_MediaStreamVideoTrack_0_1>()) + get_interface<PPB_MediaStreamVideoTrack_0_1>()->Close(pp_resource()); +} + +bool MediaStreamVideoTrack::IsMediaStreamVideoTrack(const Resource& resource) { + if (has_interface<PPB_MediaStreamVideoTrack_0_1>()) { + return PP_ToBool(get_interface<PPB_MediaStreamVideoTrack_0_1>()-> + IsMediaStreamVideoTrack(resource.pp_resource())); + } + return false; +} + +} // namespace pp diff --git a/ppapi/cpp/media_stream_video_track.h b/ppapi/cpp/media_stream_video_track.h new file mode 100644 index 0000000..4151077 --- /dev/null +++ b/ppapi/cpp/media_stream_video_track.h @@ -0,0 +1,122 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PPAPI_CPP_MEDIA_STREAM_VIDEO_TRACK_H_ +#define PPAPI_CPP_MEDIA_STREAM_VIDEO_TRACK_H_ + +#include <string> + +#include "ppapi/c/pp_stdint.h" +#include "ppapi/cpp/resource.h" + +/// @file +/// This file defines the <code>MediaStreamVideoTrack</code> interface for a +/// video source resource, which receives video frames from a MediaStream video +/// track in the browser. + +namespace pp { + +class VideoFrame; +template <typename T> class CompletionCallbackWithOutput; + +/// The <code>MediaStreamVideoTrack</code> class contains methods for +/// receiving video frames from a MediaStream video track in the browser. +class MediaStreamVideoTrack : public Resource { + public: + /// Default constructor for creating an is_null() + /// <code>MediaStreamVideoTrack</code> object. + MediaStreamVideoTrack(); + + /// The copy constructor for <code>MediaStreamVideoTrack</code>. + /// + /// @param[in] other A reference to a <code>MediaStreamVideoTrack</code>. + MediaStreamVideoTrack(const MediaStreamVideoTrack& other); + + /// Constructs a <code>MediaStreamVideoTrack</code> from + /// a <code>Resource</code>. + /// + /// @param[in] resource A <code>Resource</code> containing a file system. + explicit MediaStreamVideoTrack(const Resource& resource); + + /// A constructor used when you have received a <code>PP_Resource</code> as a + /// return value that has had 1 ref added for you. + /// + /// @param[in] resource A <code>PPB_MediaStreamVideoTrack</code> resource. + MediaStreamVideoTrack(PassRef, PP_Resource resource); + + ~MediaStreamVideoTrack(); + + /// Configures underlying frame buffers for incoming frames. + /// If the application doesn't want to drop frames, then the + /// <code>max_buffered_frames</code> should be chosen such that inter-frame + /// processing time variability won't overrun the input buffer. If the buffer + /// is overfilled, then frames will be dropped. The application can detect + /// this by examining the timestamp on returned frames. + /// If <code>Configure()</code> is not used, default settings will be used. + /// + /// @param[in] max_buffered_frames The maximum number of video frames to + /// hold in the input buffer. + /// + /// @return An int32_t containing a result code from <code>pp_errors.h</code>. + int32_t Configure(uint32_t max_buffered_frames); + + /// Returns the track ID of the underlying MediaStream video track. + std::string GetId() const; + + /// Checks whether the underlying MediaStream track has ended. + /// Calls to GetFrame while the track has ended are safe to make and will + /// complete, but will fail. + bool HasEnded() const; + + /// Gets the next video frame from the MediaStream track. + /// If internal processing is slower than the incoming frame rate, new frames + /// will be dropped from the incoming stream. Once the input buffer is full, + /// frames will be dropped until <code>RecycleFrame()</code> is called to free + /// a spot for another frame to be buffered. + /// If there are no frames in the input buffer, + /// <code>PP_OK_COMPLETIONPENDING</code> will be returned immediately and the + /// <code>callback</code> will be called, when a new frame is received or some + /// error happens. + /// If the caller holds a frame returned by the previous call of + /// <code>GetFrame()</code>, <code>PP_ERROR_INPROGRESS</code> will be + /// returned. The caller should recycle the previous frame before getting + /// the next frame. + /// + /// @param[in] callback A <code>PP_CompletionCallback</code> to be called upon + /// completion of <code>GetFrame()</code>. If success, a VideoFrame will be + /// passed into the completion callback function. + /// + /// @return An int32_t containing a result code from <code>pp_errors.h</code>. + /// Returns PP_ERROR_NOMEMORY if <code>max_buffered_frames</code> frames + /// buffer was not allocated successfully. + int32_t GetFrame( + const CompletionCallbackWithOutput<VideoFrame>& callback); + + /// Recycles a frame returned by <code>GetFrame()</code>, so the track can + /// reuse the underlying buffer of this frame. And the frame will become + /// invalid. The caller should release all references it holds to + /// <code>frame</code> and not use it anymore. + /// + /// @param[in] frame A VideoFrame returned by <code>GetFrame()</code>. + /// + /// @return An int32_t containing a result code from <code>pp_errors.h</code>. + int32_t RecycleFrame(const VideoFrame& frame); + + /// Closes the MediaStream video track, and disconnects it from video source. + /// After calling <code>Close()</code>, no new frames will be received. + void Close(); + + /// Checks whether a <code>Resource</code> is a MediaStream video track, + /// to test whether it is appropriate for use with the + /// <code>MediaStreamVideoTrack</code> constructor. + /// + /// @param[in] resource A <code>Resource</code> to test. + /// + /// @return True if <code>resource</code> is a MediaStream video track. + static bool IsMediaStreamVideoTrack(const Resource& resource); +}; + +} // namespace pp + +#endif // PPAPI_CPP_MEDIA_STREAM_VIDEO_TRACK_H_ diff --git a/ppapi/cpp/video_frame.cc b/ppapi/cpp/video_frame.cc new file mode 100644 index 0000000..a3c9311 --- /dev/null +++ b/ppapi/cpp/video_frame.cc @@ -0,0 +1,77 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ppapi/cpp/video_frame.h" + +#include "ppapi/cpp/module.h" +#include "ppapi/cpp/module_impl.h" + +namespace pp { + +namespace { + +template <> const char* interface_name<PPB_VideoFrame_0_1>() { + return PPB_VIDEOFRAME_INTERFACE_0_1; +} + +} + +VideoFrame::VideoFrame() { +} + +VideoFrame::VideoFrame(const VideoFrame& other) : Resource(other) { +} + +VideoFrame::VideoFrame(const Resource& resource) : Resource(resource) { +} + +VideoFrame::VideoFrame(PassRef, PP_Resource resource) + : Resource(PASS_REF, resource) { +} + +VideoFrame::~VideoFrame() { +} + +PP_TimeDelta VideoFrame::GetTimestamp() const { + PP_TimeDelta timestamp = .0; + if (has_interface<PPB_VideoFrame_0_1>()) { + timestamp = get_interface<PPB_VideoFrame_0_1>()->GetTimestamp( + pp_resource()); + } + return timestamp; +} + +void VideoFrame::SetTimestamp(PP_TimeDelta timestamp) { + if (has_interface<PPB_VideoFrame_0_1>()) + get_interface<PPB_VideoFrame_0_1>()->SetTimestamp(pp_resource(), timestamp); +} + +PP_VideoFrame_Format VideoFrame::GetFormat() const { + if (has_interface<PPB_VideoFrame_0_1>()) + return get_interface<PPB_VideoFrame_0_1>()->GetFormat(pp_resource()); + return PP_VIDEOFRAME_FORMAT_UNKNOWN; +} + +bool VideoFrame::GetSize(Size* size) const { + if (has_interface<PPB_VideoFrame_0_1>()) + return PP_ToBool(get_interface<PPB_VideoFrame_0_1>()->GetSize( + pp_resource(), &size->pp_size())); + return false; +} + +void* VideoFrame::GetDataBuffer() { + if (has_interface<PPB_VideoFrame_0_1>()) + return get_interface<PPB_VideoFrame_0_1>()->GetDataBuffer(pp_resource()); + return NULL; +} + +uint32_t VideoFrame::GetDataBufferSize() const { + if (has_interface<PPB_VideoFrame_0_1>()) { + return get_interface<PPB_VideoFrame_0_1>()->GetDataBufferSize( + pp_resource()); + } + return 0; +} + +} // namespace pp diff --git a/ppapi/cpp/video_frame.h b/ppapi/cpp/video_frame.h new file mode 100644 index 0000000..0c3b3ea --- /dev/null +++ b/ppapi/cpp/video_frame.h @@ -0,0 +1,75 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PPAPI_CPP_VIDEO_FRAME_H_ +#define PPAPI_CPP_VIDEO_FRAME_H_ + +#include "ppapi/c/ppb_video_frame.h" +#include "ppapi/cpp/resource.h" +#include "ppapi/cpp/size.h" + +namespace pp { + +class VideoFrame : public Resource { + public: + /// Default constructor for creating an is_null() + /// <code>VideoFrame</code> object. + VideoFrame(); + + /// The copy constructor for <code>VideoFrame</code>. + /// + /// @param[in] other A reference to a <code>VideoFrame</code>. + VideoFrame(const VideoFrame& other); + + VideoFrame(const Resource& resource); + + /// A constructor used when you have received a <code>PP_Resource</code> as a + /// return value that has had 1 ref added for you. + /// + /// @para[in] resource A <code>PPB_VideoFrame</code> resource. + VideoFrame(PassRef, PP_Resource resource); + + virtual ~VideoFrame(); + + /// Gets the timestamp of the video frame. + /// + /// @return A <code>PP_TimeDelta</code> containing the timestamp of the video + /// frame. Given in seconds since the start of the containing video stream. + PP_TimeDelta GetTimestamp() const; + + /// Sets the timestamp of the video frame. Given in seconds since the + /// start of the containing video stream. + /// + /// @param[in] timestamp A <code>PP_TimeDelta</code> containing the timestamp + /// of the video frame. Given in seconds since the start of the containing + /// video stream. + void SetTimestamp(PP_TimeDelta timestamp); + + /// Gets the format of the video frame. + /// + /// @return A <code>PP_VideoFrame_Format</code> containing the format of the + /// video frame. + PP_VideoFrame_Format GetFormat() const; + + /// Gets the size of the video frame. + /// + /// @param[out] size A <code>Size</code>. + /// + /// @return True on success or false on failure. + bool GetSize(Size* size) const; + + /// Gets the data buffer for video frame pixels. + /// + /// @return A pointer to the beginning of the data buffer. + void* GetDataBuffer(); + + /// Gets the size of data buffer. + /// + /// @return The size of the data buffer. + uint32_t GetDataBufferSize() const; +}; + +} // namespace pp + +#endif // PPAPI_CPP_VIDEO_FRAME_H_ |