Add git cl format presubmit warning for extension and apps.
[chromium-blink-merge.git] / ppapi / proxy / media_stream_audio_track_resource.cc
blob10956ab30d9c8617000845a750553ffbb4c35e75
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ppapi/proxy/media_stream_audio_track_resource.h"
7 #include "ppapi/proxy/audio_frame_resource.h"
8 #include "ppapi/shared_impl/media_stream_frame.h"
9 #include "ppapi/shared_impl/var.h"
11 namespace ppapi {
12 namespace proxy {
14 MediaStreamAudioTrackResource::MediaStreamAudioTrackResource(
15 Connection connection,
16 PP_Instance instance,
17 int pending_renderer_id,
18 const std::string& id)
19 : MediaStreamTrackResourceBase(
20 connection, instance, pending_renderer_id, id),
21 get_frame_output_(NULL) {
24 MediaStreamAudioTrackResource::~MediaStreamAudioTrackResource() {
25 Close();
28 thunk::PPB_MediaStreamAudioTrack_API*
29 MediaStreamAudioTrackResource::AsPPB_MediaStreamAudioTrack_API() {
30 return this;
33 PP_Var MediaStreamAudioTrackResource::GetId() {
34 return StringVar::StringToPPVar(id());
37 PP_Bool MediaStreamAudioTrackResource::HasEnded() {
38 return PP_FromBool(has_ended());
41 int32_t MediaStreamAudioTrackResource::Configure(
42 const int32_t attrib_list[],
43 scoped_refptr<TrackedCallback> callback) {
44 // TODO(penghuang): Implement this function.
45 return PP_ERROR_NOTSUPPORTED;
48 int32_t MediaStreamAudioTrackResource::GetAttrib(
49 PP_MediaStreamAudioTrack_Attrib attrib,
50 int32_t* value) {
51 // TODO(penghuang): Implement this function.
52 return PP_ERROR_NOTSUPPORTED;
55 int32_t MediaStreamAudioTrackResource::GetFrame(
56 PP_Resource* frame,
57 scoped_refptr<TrackedCallback> callback) {
58 if (has_ended())
59 return PP_ERROR_FAILED;
61 if (TrackedCallback::IsPending(get_frame_callback_))
62 return PP_ERROR_INPROGRESS;
64 *frame = GetAudioFrame();
65 if (*frame)
66 return PP_OK;
68 // TODO(penghuang): Use the callback as hints to determine which thread will
69 // use the resource, so we could deliver frames to the target thread directly
70 // for better performance.
71 get_frame_output_ = frame;
72 get_frame_callback_ = callback;
73 return PP_OK_COMPLETIONPENDING;
76 int32_t MediaStreamAudioTrackResource::RecycleFrame(PP_Resource frame) {
77 FrameMap::iterator it = frames_.find(frame);
78 if (it == frames_.end())
79 return PP_ERROR_BADRESOURCE;
81 scoped_refptr<AudioFrameResource> frame_resource = it->second;
82 frames_.erase(it);
84 if (has_ended())
85 return PP_OK;
87 DCHECK_GE(frame_resource->GetFrameBufferIndex(), 0);
89 SendEnqueueFrameMessageToHost(frame_resource->GetFrameBufferIndex());
90 frame_resource->Invalidate();
91 return PP_OK;
94 void MediaStreamAudioTrackResource::Close() {
95 if (has_ended())
96 return;
98 if (TrackedCallback::IsPending(get_frame_callback_)) {
99 *get_frame_output_ = 0;
100 get_frame_callback_->PostAbort();
101 get_frame_callback_ = NULL;
102 get_frame_output_ = 0;
105 ReleaseFrames();
106 MediaStreamTrackResourceBase::CloseInternal();
109 void MediaStreamAudioTrackResource::OnNewFrameEnqueued() {
110 if (!TrackedCallback::IsPending(get_frame_callback_))
111 return;
113 *get_frame_output_ = GetAudioFrame();
114 int32_t result = *get_frame_output_ ? PP_OK : PP_ERROR_FAILED;
115 get_frame_output_ = NULL;
116 scoped_refptr<TrackedCallback> callback;
117 callback.swap(get_frame_callback_);
118 callback->Run(result);
121 PP_Resource MediaStreamAudioTrackResource::GetAudioFrame() {
122 int32_t index = frame_buffer()->DequeueFrame();
123 if (index < 0)
124 return 0;
126 MediaStreamFrame* frame = frame_buffer()->GetFramePointer(index);
127 DCHECK(frame);
128 scoped_refptr<AudioFrameResource> resource =
129 new AudioFrameResource(pp_instance(), index, frame);
130 // Add |pp_resource()| and |resource| into |frames_|.
131 // |frames_| uses scoped_ptr<> to hold a ref of |resource|. It keeps the
132 // resource alive.
133 frames_.insert(FrameMap::value_type(resource->pp_resource(), resource));
134 return resource->GetReference();
137 void MediaStreamAudioTrackResource::ReleaseFrames() {
138 FrameMap::iterator it = frames_.begin();
139 while (it != frames_.end()) {
140 // Just invalidate and release VideoFrameResorce, but keep PP_Resource.
141 // So plugin can still use |RecycleFrame()|.
142 it->second->Invalidate();
143 it->second = NULL;
147 } // namespace proxy
148 } // namespace ppapi