[MediaPlayer] Added ErrorHandler registration methods for internal use. (#33)
[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     /// <since_tizen> 3 </since_tizen>
31     public sealed class MediaBufferSource : MediaSource
32     {
33         private byte[] _buffer;
34
35         /// <summary>
36         /// Initializes a new instance of the MediaBufferSource class with an allocated buffer.
37         /// </summary>
38         /// <param name="length">The value indicating the size of the buffer.</param>
39         /// <exception cref="ArgumentOutOfRangeException">
40         ///     <paramref name="length"/> is zero.<br/>
41         ///     -or-<br/>
42         ///     <paramref name="length"/> is less than zero.
43         /// </exception>
44         /// <since_tizen> 3 </since_tizen>
45         public MediaBufferSource(int length)
46         {
47             if (length <= 0)
48             {
49                 Log.Error(PlayerLog.Tag, "invalid length : " + length);
50                 throw new ArgumentOutOfRangeException(nameof(length), length,
51                     "length can't be equal to or less than zero.");
52             }
53             _buffer = new byte[length];
54         }
55
56         /// <summary>
57         /// Initializes a new instance of the MediaBufferSource class from the buffer.
58         /// </summary>
59         /// <param name="buffer">The source array to be copied into the buffer.</param>
60         /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
61         /// <since_tizen> 3 </since_tizen>
62         public MediaBufferSource(byte[] buffer) : this(buffer, buffer == null ? 0 : buffer.Length)
63         {
64         }
65
66         //TODO remove default parameter.
67         /// <summary>
68         /// Initializes a new instance of the MediaBufferSource class from the buffer
69         /// with the specified length and the specified offset.
70         /// </summary>
71         /// <param name="buffer">The source array to be copied into the buffer.</param>
72         /// <param name="length">The value indicating the number of bytes to copy from the buffer.</param>
73         /// <param name="offset">The value indicating the offset in the buffer of the first byte to copy.</param>
74         /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
75         /// <exception cref="ArgumentOutOfRangeException">
76         ///     <paramref name="offset"/> is less than zero.<br/>
77         ///     -or-<br/>
78         ///     <paramref name="length"/> is equal to or less than zero.<br/>
79         ///     -or-<br/>
80         ///     <paramref name="offset"/>+<paramref name="length"/> is greater than buffer.Length.
81         /// </exception>
82         /// <since_tizen> 3 </since_tizen>
83         public MediaBufferSource(byte[] buffer, int length, int offset = 0)
84         {
85             if (buffer == null)
86             {
87                 throw new ArgumentNullException(nameof(buffer));
88             }
89
90             if (offset < 0)
91             {
92                 throw new ArgumentOutOfRangeException(nameof(offset), offset, "offset can't be less than zero.");
93             }
94             if (length <= 0)
95             {
96                 throw new ArgumentOutOfRangeException(nameof(length), length, "length can't be equal to or less than zero.");
97             }
98             if (length + offset > buffer.Length)
99             {
100                 throw new ArgumentOutOfRangeException($"length + offset can't be greater than the length of the { nameof(buffer) }.");
101             }
102
103             _buffer = new byte[length];
104
105             Array.Copy(buffer, offset, _buffer, 0, length);
106         }
107
108         private MediaBufferSource()
109         {
110         }
111
112         /// <summary>
113         /// Creates a MediaBufferSource that wraps a byte array.
114         /// </summary>
115         /// <param name="buffer">The array to be wrapped.</param>
116         /// <returns>A MediaBufferSource wrapping the byte array.</returns>
117         /// <since_tizen> 3 </since_tizen>
118         public static MediaBufferSource Wrap(byte[] buffer)
119         {
120             if (buffer == null)
121             {
122                 Log.Error(PlayerLog.Tag, "invalid buffer");
123                 throw new ArgumentNullException(nameof(buffer));
124             }
125
126             MediaBufferSource source = new MediaBufferSource();
127             source._buffer = buffer;
128             return source;
129         }
130
131         /// <summary>
132         /// Gets the byte array of this buffer.
133         /// </summary>
134         /// <since_tizen> 3 </since_tizen>
135         public byte[] Buffer => _buffer;
136
137         internal override void OnAttached(Player player)
138         {
139             NativePlayer.SetMemoryBuffer(player.Handle, _buffer, _buffer.Length).
140                 ThrowIfFailed(player, "Failed to set the memory buffer");
141         }
142     }
143 }
144