44ca0e3f73e9fb358774b10bf0150f3310cb62df
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / MediaVision / MediaVisionSource.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Runtime.InteropServices;
19 using static Interop.MediaVision;
20
21 namespace Tizen.Multimedia
22 {
23     /// <summary>
24     /// This class represents media vision source. An instance of this class has to be created \n
25     /// to keep information on image or video frame data as raw buffer. It can be created based on \n
26     /// the media data stored in memory or using the BaseMediaPacket class. \n
27     /// It provides a set of getters which allow to retrieve such image parameters as its size or colorspace (see Colorspace enumeration).
28     /// </summary>
29     public class MediaVisionSource : IDisposable
30     {
31         internal IntPtr _sourceHandle = IntPtr.Zero;
32         private bool _disposed = false;
33
34         /// <summary>
35         /// The media vision source constructor
36         /// </summary>
37         /// <code>
38         /// MediaVisionSource source = new MediaVisionSource();
39         /// </code>
40         public MediaVisionSource()
41         {
42             int ret = Interop.MediaVision.MediaSource.Create(out _sourceHandle);
43             MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to create media vision source.");
44         }
45
46         /// <summary>
47         /// Destructor of the MediaVisionSource class.
48         /// </summary>
49         ~MediaVisionSource()
50         {
51             Dispose(false);
52         }
53
54         /// <summary>
55         /// Gets buffer of the media source.
56         /// </summary>
57         /// <code>
58         /// 
59         /// </code>
60         public byte[] Buffer
61         {
62             get
63             {
64                 IntPtr byteStrPtr;
65                 int byteStrSize;
66                 MediaVisionError ret = (MediaVisionError)Interop.MediaVision.MediaSource.GetBuffer(_sourceHandle, out byteStrPtr, out byteStrSize);
67                 if (ret != MediaVisionError.None)
68                 {
69                     Log.Error(MediaVisionLog.Tag, "[{0}] : Failed to get buffer data", ret.ToString());
70                     return null;
71                 }
72
73                 byte[] byteStr = new byte[byteStrSize];
74                 if (byteStrSize > 0)
75                 {
76                     Marshal.Copy(byteStrPtr, byteStr, 0, byteStrSize);
77                 }
78
79                 return byteStr;
80             }
81         }
82
83         /// <summary>
84         /// Gets height of the media source.
85         /// </summary>
86         /// <code>
87         /// 
88         /// </code>
89         public uint Height
90         {
91             get
92             {
93                 uint height = 0;
94                 MediaVisionError ret = (MediaVisionError)Interop.MediaVision.MediaSource.GetHeight(_sourceHandle, out height);
95                 if (ret != MediaVisionError.None)
96                 {
97                     Log.Error(MediaVisionLog.Tag, "[{0}] : Failed to get height", ret.ToString());
98                 }
99
100                 return height;
101             }
102         }
103
104         /// <summary>
105         /// Gets width of the media source.
106         /// </summary>
107         /// <code>
108         /// 
109         /// </code>
110         public uint Width
111         {
112             get
113             {
114                 uint width = 0;
115                 MediaVisionError ret = (MediaVisionError)Interop.MediaVision.MediaSource.GetWidth(_sourceHandle, out width);
116                 if (ret != MediaVisionError.None)
117                 {
118                     Log.Error(MediaVisionLog.Tag, "[{0}] : Failed to get width", ret.ToString());
119                 }
120
121                 return width;
122             }
123         }
124
125         /// <summary>
126         /// Gets colorspace of the media source.
127         /// </summary>
128         /// <code>
129         /// 
130         /// </code>
131         public Colorspace Colorspace
132         {
133             get
134             {
135                 Colorspace colorspace = Colorspace.Invalid;
136                 MediaVisionError ret = (MediaVisionError)Interop.MediaVision.MediaSource.GetColorspace(_sourceHandle, out colorspace);
137                 if (ret != MediaVisionError.None)
138                 {
139                     Log.Error(MediaVisionLog.Tag, "[{0}] : Failed to get colorspace", ret.ToString());
140                 }
141
142                 return colorspace;
143             }
144         }
145
146         /// <summary>
147         /// Fills the media source based on the media packet.
148         /// </summary>
149         /// <param name="mediaPacket">The media packet from which the source will be filled</param>
150         /// <code>
151         /// 
152         /// </code>
153         /*public void FillMediaPacket(BaseMediaPacket mediaPacket)
154         {
155             int ret = Interop.MediaVision.MediaSource.FillBuffer(out _sourceHandle, mediaPacket._mediaPacketHandle);
156             MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to fill media packet");
157         }*/
158
159         /// <summary>
160         /// Fills the media source based on the buffer and metadata.
161         /// </summary>
162         /// <param name="buffer">The buffer of image data</param>
163         /// <param name="width">The width of image data</param>
164         /// <param name="height">The height of image data</param>
165         /// <param name="colorspace">The image colorspace</param>
166         /// <seealso cref="Clear()"/>
167         /// <code>
168         /// 
169         /// </code>
170         public void FillBuffer(byte[] buffer, uint width, uint height, Colorspace colorspace)
171         {
172             int ret = Interop.MediaVision.MediaSource.FillBuffer(_sourceHandle, buffer, buffer.Length, width, height, colorspace);
173             MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to fill buffer");
174         }
175
176         /// <summary>
177         /// Clears the buffer of the media source.
178         /// </summary>
179         /// <seealso cref="FillBuffer()"/>
180         /// <code>
181         /// 
182         /// </code>
183         public void Clear()
184         {
185             int ret = Interop.MediaVision.MediaSource.Clear(_sourceHandle);
186             MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to clear media source buffer");
187         }
188
189         public void Dispose()
190         {
191             Dispose(true);
192             GC.SuppressFinalize(this);
193         }
194
195         protected virtual void Dispose(bool disposing)
196         {
197             if (_disposed)
198             {
199                 return;
200             }
201
202             if (disposing)
203             {
204                 // Free managed objects
205             }
206
207             Interop.MediaVision.MediaSource.Destroy(_sourceHandle);
208             _disposed = true;
209         }
210     }
211 }