Release 4.0.0-preview1-00201
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.MediaPlayer / Player / MediaBufferSource.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 static Interop;
19
20 namespace Tizen.Multimedia
21 {
22     /// <summary>
23     /// Represents a media source using memory.
24     /// </summary>
25     /// <remarks>
26     /// The buffer has to be filled with appropriate data which means it must be well-formatted.
27     /// If you provide invalid data, you won't receive an error until <see cref="Player.Start"/> is called.
28     /// </remarks>
29     /// <seealso cref="Player.SetSource(MediaSource)"/>
30     public sealed class MediaBufferSource : MediaSource
31     {
32         private byte[] _buffer;
33
34         /// <summary>
35         /// Initializes a new instance of the MediaBufferSource class with an allocated buffer.
36         /// </summary>
37         /// <param name="length">The value indicating the size of the buffer.</param>
38         /// <exception cref="ArgumentOutOfRangeException">
39         ///     <paramref name="length"/> is zero.\n
40         ///     -or-\n
41         ///     <paramref name="length"/> is less than zero.
42         /// </exception>
43         public MediaBufferSource(int length)
44         {
45             if (length <= 0)
46             {
47                 Log.Error(PlayerLog.Tag, "invalid length : " + length);
48                 throw new ArgumentOutOfRangeException(nameof(length), length,
49                     "length can't be equal to or less than zero.");
50             }
51             _buffer = new byte[length];
52         }
53
54         /// <summary>
55         /// Initializes a new instance of the MediaBufferSource class from the buffer.
56         /// </summary>
57         /// <param name="buffer">The source array to be copied into the buffer.</param>
58         /// <exception cref="ArgumentNullException"><paramref="buffer"/> is null.</exception>
59         public MediaBufferSource(byte[] buffer) : this(buffer, buffer == null ? 0 : buffer.Length)
60         {
61         }
62
63         //TODO remove default parameter.
64         /// <summary>
65         /// Initializes a new instance of the MediaBufferSource class from the buffer
66         /// with the specified length and the specified offset.
67         /// </summary>
68         /// <param name="buffer">The source array to be copied into the buffer.</param>
69         /// <param name="length">The value indicating the number of bytes to copy from the buffer.</param>
70         /// <param name="offset">The value indicating the offset in the buffer of the first byte to copy.</param>
71         /// <exception cref="ArgumentNullException"><paramref="buffer"/> is null.</exception>
72         /// <exception cref="ArgumentOutOfRangeException">
73         ///     <paramref name="offset"/> is less than zero.\n
74         ///     -or-\n
75         ///     <paramref name="length"/> is equal to or less than zero.\n
76         ///     -or-\n
77         ///     <paramref name="offset"/>+<paramref name="length"/> is greater than buffer.Length.
78         /// </exception>
79         public MediaBufferSource(byte[] buffer, int length, int offset = 0)
80         {
81             if (buffer == null)
82             {
83                 Log.Error(PlayerLog.Tag, "invalid buffer");
84                 throw new ArgumentNullException(nameof(buffer));
85             }
86             if (offset < 0)
87             {
88                 Log.Error(PlayerLog.Tag, "invalid offset : " + offset);
89                 throw new ArgumentOutOfRangeException(nameof(offset), offset, "offset can't be less than zero.");
90             }
91             if (length <= 0)
92             {
93                 Log.Error(PlayerLog.Tag, "invalid length : " + length);
94                 throw new ArgumentOutOfRangeException(nameof(length), length, "length can't be equal to or less than zero.");
95             }
96             if (length + offset > buffer.Length)
97             {
98                 Log.Error(PlayerLog.Tag, "invalid total length : " + (int)(length + offset));
99                 throw new ArgumentOutOfRangeException($"length + offset can't be greater than the length of the { nameof(buffer) }.");
100             }
101
102             _buffer = new byte[length];
103
104             Array.Copy(buffer, offset, _buffer, 0, length);
105         }
106
107         private MediaBufferSource()
108         {
109         }
110
111         /// <summary>
112         /// Creates a MediaBufferSource that wraps a byte array.
113         /// </summary>
114         /// <param name="buffer">The array to be wrapped.</param>
115         /// <returns>A MediaBufferSource wrapping the byte array.</returns>
116         public static MediaBufferSource Wrap(byte[] buffer)
117         {
118             if (buffer == null)
119             {
120                 Log.Error(PlayerLog.Tag, "invalid buffer");
121                 throw new ArgumentNullException(nameof(buffer));
122             }
123
124             MediaBufferSource source = new MediaBufferSource();
125             source._buffer = buffer;
126             return source;
127         }
128
129         /// <summary>
130         /// Gets the byte array of this buffer.
131         /// </summary>
132         public byte[] Buffer => _buffer;
133
134         internal override void OnAttached(Player player)
135         {
136             NativePlayer.SetMemoryBuffer(player.Handle, _buffer, _buffer.Length).
137                 ThrowIfFailed("Failed to set the memory buffer");
138         }
139     }
140 }
141