Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / ppapi / shared_impl / media_stream_frame_buffer.h
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.
4
5 #ifndef PPAPI_SHARED_IMPL_MEDIA_STREAM_FRAME_BUFFER_H_
6 #define PPAPI_SHARED_IMPL_MEDIA_STREAM_FRAME_BUFFER_H_
7
8 #include <deque>
9 #include <vector>
10
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/shared_memory.h"
14 #include "ppapi/shared_impl/media_stream_frame.h"
15 #include "ppapi/shared_impl/ppapi_shared_export.h"
16
17 namespace ppapi {
18
19 // This class is used by both read side and write side of a MediaStreamTrack to
20 // maintain a queue of frames for reading or writing.
21 //
22 // An example:
23 //  1. The writer calls the writer's |frame_buffer_.Dequeue()| to get a free
24 //     frame.
25 //  2. The writer fills data into the frame.
26 //  3. The writer sends the frame index to the reader via an IPC message.
27 //  4. The reader receives the frame index and calls the reader's
28 //     |frame_buffer.Enqueue()| to put the frame into the read's queue.
29 //  5. The reader calls reader's |frame_buffer_.Dequeue()| to get a received
30 //     frame.
31 //  6. When the frame from the step 5 is consumed, the reader sends the frame
32 //     index back to writer via an IPC message.
33 //  7. The writer receives the frame index and puts it back to the writer's free
34 //     frame queue by calling the writer's |frame_buffer_.Enqueue()|.
35 //  8. Go back to step 1.
36 class PPAPI_SHARED_EXPORT MediaStreamFrameBuffer {
37  public:
38   class PPAPI_SHARED_EXPORT Delegate {
39    public:
40     virtual ~Delegate();
41     // It is called when a new frame is enqueued.
42     virtual void OnNewFrameEnqueued();
43   };
44
45   // MediaStreamFrameBuffer doesn't own |delegate|, the caller should keep
46   // it alive during the MediaStreamFrameBuffer's lifecycle.
47   explicit MediaStreamFrameBuffer(Delegate* delegate);
48
49   ~MediaStreamFrameBuffer();
50
51   int32_t number_of_frames() const { return number_of_frames_; }
52
53   int32_t frame_size() const { return frame_size_; }
54
55   // Initializes shared memory for frames transmission.
56   bool SetFrames(int32_t number_of_frames,
57                  int32_t frame_size,
58                  scoped_ptr<base::SharedMemory> shm,
59                  bool enqueue_all_frames);
60
61   // Dequeues a frame from |frame_queue_|.
62   int32_t DequeueFrame();
63
64   // Puts a frame into |frame_queue_|.
65   void EnqueueFrame(int32_t index);
66
67   // Gets the frame address for the given frame index.
68   MediaStreamFrame* GetFramePointer(int32_t index);
69
70  private:
71   Delegate* delegate_;
72
73   // A queue of frame indexes.
74   std::deque<int32_t> frame_queue_;
75
76   // A vector of frame pointers. It is used for index to pointer converting.
77   std::vector<MediaStreamFrame*> frames_;
78
79   // The frame size in bytes.
80   int32_t frame_size_;
81
82   // The number of frames in the shared memory.
83   int32_t number_of_frames_;
84
85   // A memory block shared between renderer process and plugin process.
86   scoped_ptr<base::SharedMemory> shm_;
87
88   DISALLOW_COPY_AND_ASSIGN(MediaStreamFrameBuffer);
89 };
90
91 }  // namespace ppapi
92
93 #endif  // PPAPI_SHAERD_IMPL_MEDIA_STREAM_FRAME_BUFFER_H_