title: Android Live Streaming using MJPG on HTTP or RTSP protocol
body: <p>I purchased ESP32 camera and installed code on ESP32 camera using Arduino IDE. I am able to see the feed on chrome and VLC. I am writing an android app which used Android MediaPlayer and Surface View to view the live streaming video. But in the android app I am not able to view the stream.</p>
  <p>In the ESP32 Camera, I tried with both HTTP and RTSP protocol. In both the cases, facing the following error in media player</p>
  <pre><code>12-01 09:57:58.446 20282 20282 E Cam_VideoStreamPreview: Mediaplayer info :: what = 701, extra = 0
  12-01 09:57:59.657 20282 20297 E MediaPlayerNative: error (1, -2147483648)
  12-01 09:57:59.657 20282 20282 E MediaPlayer: Error (1,-2147483648)
  </code></pre>
  <h3><strong>Media Player logic in code</strong></h3>
  <pre><code>//private const val STREAM_URI = &quot;http://192.168.1.11/stream&quot;
  //private const val STREAM_URI = &quot;http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4&quot;
  //private const val STREAM_URI = &quot;http://techslides.com/demos/sample-videos/small.mp4&quot;
  //private const val STREAM_URI = &quot;&quot;
  //private const val STREAM_URI = &quot;https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4&quot;
  //private const val STREAM_URI = &quot;http://192.168.1.8:8080/Downloads/Sintel.mp4&quot;
  private const val STREAM_URI = &quot;rtsp://192.168.1.11:8554/mjpeg/1&quot;
  
  val mediaPlayer = MediaPlayer().apply {
              setAudioAttributes(
                      AudioAttributes.Builder()
                              .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                              .setUsage(AudioAttributes.USAGE_MEDIA)
                              .build()
              )
              setDataSource(STREAM_URI)
          }
  
  </code></pre>
  <p>I then do <code>prepare</code> and then in that callback I will call the <code>start</code> method from mediaplayer.</p>
  <h3>Content Type Http Response</h3>
  <p>In the ESP32 camera, we use HTTP Live Streaming protocol. Output format will be in MJPG format. Initial header response will have <code>Content-Type</code> as <code>multipart/x-mixed-replace;boundary=123456789000000000000987654321</code>. All the chunked response will have <code>Content-Type</code> as <code>image/jpeg</code></p>
  <pre><code>*   Trying 192.168.1.11...
  * TCP_NODELAY set
  * Connected to 192.168.1.11 (192.168.1.11) port 80 (#0)
  &gt; GET /stream HTTP/1.1
  &gt; Host: 192.168.1.11
  &gt; User-Agent: curl/7.54.0
  &gt; Accept: */*
  &gt;
  &lt; HTTP/1.1 200 OK
  &lt; Content-Type: multipart/x-mixed-replace;boundary=123456789000000000000987654321
  &lt; Transfer-Encoding: chunked
  &lt; Access-Control-Allow-Origin: *
  &lt;
  </code></pre>
  <pre><code>HTTP/1.1 200 OK
  Content-Type: multipart/x-mixed-replace;boundary=123456789000000000000987654321
  Transfer-Encoding: chunked
  Access-Control-Allow-Origin: *
  
  Content-Type: image/jpeg
  Content-Length: 6503
  </code></pre>
  <p>I have the following doubts</p>
  <ol>
  <li>I checked the supported media requirements from android (<a href="https://developer.android.com/guide/topics/media/media-formats#network" rel="nofollow noreferrer">https://developer.android.com/guide/topics/media/media-formats#network</a> ). They support HLS and RTSP protocol. Whether the <code>Content-Type</code> would be a problem here?</li>
  <li>Whether there are any alternatives to <code>android media player</code>. Can I read the chunks myself and display in android? Whether such thing is possible in android?</li>
  </ol>