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